Skip to content

Commit 8c4ab8c

Browse files
committed
Merge pull request #26 from mimuret/develop
change qname match method.
2 parents d601bd6 + 10adffe commit 8c4ab8c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+11459
-101
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
language: c
2+
sudo: required
23
before_install:
34
- sudo apt-get -qq update
45
- sudo apt-get install -y bind9
56
install: sudo ./install-dependencies.sh --debug
67
compiler: gcc
78
script:
89
- ./autogen.sh
9-
- ./configure --libdir=/lib
10+
- ./configure --disable-check-ipv6
1011
- make
1112
- sudo make install
1213
- sudo make check

Makefile.am

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# -*- Makefile -*-
22
#
33
SUBDIRS = include extensions modules test
4+
5+
depend:
6+
./install-dependencies.sh

configure.ac

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
AC_PREREQ([2.63])
55
m4_define([VERSION_MAJOR],[1])
6-
m4_define([VERSION_MINOR],[1])
6+
m4_define([VERSION_MINOR],[2])
77
m4_define([VERSION_MICRO],[0])
88
AC_INIT(iptables-ext-dns,m4_defn([VERSION_MAJOR]).m4_defn([VERSION_MINOR]).m4_defn([VERSION_MICRO]), [email protected], iptables-ext-dns)
99
AC_SUBST(VERSION_INFO, [VERSION_MAJOR:VERSION_MINOR:VERSION_MICRO])
@@ -31,9 +31,8 @@ AC_HEADER_STDBOOL
3131
# Checks for library functions.
3232
AC_CHECK_FUNCS([strcasecmp strdup])
3333

34-
AC_CONFIG_FILES([Makefile extensions/Makefile test/Makefile])
3534

36-
AC_ARG_ENABLE(debug, [ --enable-debug trun on debugging [default no]],,enable_debug=no)
35+
AC_ARG_ENABLE(debug, [ --enable-debug trun on debugging [default no]],,enable_debug=no)
3736
AC_MSG_CHECKING(whether to enable debuging)
3837
if test x$enable_debug = xyes; then
3938
AC_MSG_RESULT(yes)
@@ -43,7 +42,20 @@ if test x$enable_debug = xyes; then
4342
else
4443
AC_MSG_RESULT(no)
4544
fi
45+
46+
AC_ARG_ENABLE(check-ipv6, [ --enable-check-ipv6 trun on ipv6 check [default yes]],,enable_v6check=yes)
47+
AC_MSG_CHECKING(whether to enable check-ipv6)
48+
if test x$enable_v6check = xyes; then
49+
AC_MSG_RESULT(yes)
50+
AC_SUBST(RUN_TESTS, ["common ipv4 ipv6"])
51+
else
52+
AC_MSG_RESULT(no)
53+
AC_SUBST(RUN_TESTS, ["common ipv4"])
54+
fi
55+
4656
AC_CHECK_FILE([/etc/redhat-release],[AC_SUBST(libdir,[/lib64])],[])
4757
AC_CHECK_FILE([/etc/debian_version],[AC_SUBST(libdir,[/lib])],[])
58+
59+
AC_CONFIG_FILES([Makefile extensions/Makefile test/Makefile test/common/Makefile test/ipv4/Makefile test/ipv6/Makefile iptables-ext-dns.spec])
4860
LT_INIT
4961
AC_OUTPUT

extensions/libxt_dns.c

+11
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ static void dns_init(struct xt_entry_match *m) {
8686
data->rcode = 0x00;
8787

8888
data->qname[0] = 0;
89+
data->qname_size = 1;
8990
data->qtype = 0xffff;
9091

9192
data->invflags = 0x0000;
@@ -126,6 +127,15 @@ static void parse_qname(const char *flag, uint8_t *qname) {
126127
xtables_error(PARAMETER_PROBLEM, "Invalid qname %s '%s'", flag, qname);
127128
}
128129
}
130+
static int qname_size(const uint8_t *qname) {
131+
uint8_t len = 0;
132+
uint8_t llen = 255;
133+
while (llen != 0 && len < XT_DNS_MAXSIZE) {
134+
llen = *(qname + len);
135+
len += llen + 1;
136+
}
137+
return len;
138+
}
129139

130140
static int dns_parse(int c, char **argv, int invert, unsigned int *flags,
131141
const void *entry, struct xt_entry_match **match) {
@@ -229,6 +239,7 @@ static int dns_parse(int c, char **argv, int invert, unsigned int *flags,
229239
xtables_error(PARAMETER_PROBLEM, "Only one `--qname' allowed");
230240
}
231241
parse_qname(optarg, data->qname);
242+
data->qname_size = qname_size(data->qname);
232243
data->setflags |= XT_DNS_FLAG_QNAME;
233244
if (invert) {
234245
data->invflags |= XT_DNS_FLAG_QNAME;

include/xt_dns.h

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ struct xt_dns {
2323

2424
uint16_t invflags; /* Inverse Flags */
2525
uint16_t setflags; /* Set Confitional flag */
26+
27+
uint8_t qname_size;
2628
};
2729

2830
#define XT_DNS_FLAG_QR 0x0001

modules/xt_dns.c

+14-16
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ MODULE_ALIAS("ip6t_dns");
3737
#define XT_PARAM const struct xt_match_param
3838
#define HOTDROP(par) *par->hotdrop = true
3939
#endif
40-
4140
static bool dns_mt(const struct sk_buff *skb, XT_PARAM *par, int16_t offset) {
4241
const struct dns_h *dh; // dns header working pointer
4342
struct dns_h _dnsh; // dns header buffer
4443

4544
uint16_t qlen; // qname length, MAX 255
46-
uint16_t mlen; // match qname length, MAX 255
4745
uint8_t llen; // label length, MAX 63
4846

47+
int q, m; // tmp var
48+
4949
uint8_t *qname; // qname working pointer
5050
uint8_t _qname[XT_DNS_MAXSIZE]; // qname buffer
5151
uint16_t qtype; // qtype buffer
@@ -106,18 +106,18 @@ static bool dns_mt(const struct sk_buff *skb, XT_PARAM *par, int16_t offset) {
106106
DEBUG_PRINT("not match RCODE");
107107
return false;
108108
}
109-
DEBUG_PRINT("xt_dns: bit check done");
109+
DEBUG_PRINT("xt_dns: done checking bits.");
110110
if ((dnsinfo->setflags & XT_DNS_FLAG_QNAME) ||
111111
(dnsinfo->maxsize < XT_DNS_FLAG_QNAME_MAXSIZE)) {
112-
DEBUG_PRINT("xt_dns: start parse qname");
112+
DEBUG_PRINT("xt_dns: start parse qname.");
113113
qname = _qname;
114114
qlen = 0;
115115
llen = 255;
116116
while (llen != 0 && qlen < XT_DNS_MAXSIZE) {
117117
// read label size
118118
if (skb_copy_bits(skb, offset, &llen, sizeof(uint8_t)) < 0 ||
119119
llen > XT_DNS_LABEL_MAXSIZE) {
120-
DEBUG_PRINT("xt_dns: invalid label len.");
120+
DEBUG_PRINT("xt_dns: invalid label len %u->%x.", offset, llen);
121121
HOTDROP(par);
122122
return false;
123123
}
@@ -148,19 +148,17 @@ static bool dns_mt(const struct sk_buff *skb, XT_PARAM *par, int16_t offset) {
148148
return false;
149149
}
150150
if (dnsinfo->setflags & XT_DNS_FLAG_QNAME) {
151-
qlen = mlen = 0;
152-
DEBUG_PRINT("start qname matching.");
153-
while (qlen < XT_DNS_MAXSIZE && qname[qlen] != 0 &&
154-
dnsinfo->qname[mlen] != 0) {
155-
if (tolower(qname[qlen++]) != dnsinfo->qname[mlen++]) {
156-
if (dnsinfo->rmatch) {
157-
mlen = 0;
158-
} else {
159-
break;
160-
}
151+
q = qlen - 1;
152+
m = dnsinfo->qname_size - 1;
153+
DEBUG_PRINT("start qname matching. q=%d,m=%d", q, m);
154+
while (q >= 0 && m >= 0) {
155+
DEBUG_PRINT("qm: qname[%d]=%d match[%d] = %d", q, qname[q], m,
156+
dnsinfo->qname[m]);
157+
if (tolower(qname[q--]) != dnsinfo->qname[m--]) {
158+
break;
161159
}
162160
}
163-
if (!FWINVDNS((qname[qlen] == 0 && dnsinfo->qname[mlen] == 0),
161+
if (!FWINVDNS((m < 0 && (q < 0 || dnsinfo->rmatch)),
164162
XT_DNS_FLAG_QNAME)) {
165163
DEBUG_PRINT("not match qname");
166164
return false;

0 commit comments

Comments
 (0)