Skip to content

Commit 10adffe

Browse files
committed
Merge pull request #25 from mimuret/feature/fast_rmatch
change rmatch method
2 parents eb60306 + ceb4d0d commit 10adffe

Some content is hidden

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

47 files changed

+131
-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

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;

test/3.1.1_check_ipv4_udp_input.sh

-3
This file was deleted.

test/3.1.2_check_ipv4_udp_prerouting.sh

-3
This file was deleted.

test/3.2.1_check_ipv4_tcp_input.sh

-3
This file was deleted.

test/3.2.2_check_ipv4_tcp_prerouting.sh

-3
This file was deleted.

test/3.3.1_check_ipv6_udp_input.sh

-3
This file was deleted.

test/3.3.2_check_ipv6_udp_prerouting.sh

-3
This file was deleted.

test/3.4.1_check_ipv6_tcp_input.sh

-3
This file was deleted.

test/3.4.2_check_ipv6_tcp_prerouting.sh

-3
This file was deleted.

test/4.1.1_check_no_ipv4_udp_input.sh

-3
This file was deleted.

test/4.1.2_check_no_ipv4_udp_prerouting.sh

-3
This file was deleted.

test/4.2.1_check_no_ipv4_tcp_input.sh

-3
This file was deleted.

test/4.2.2_check_no_ipv4_tcp_prerouting.sh

-3
This file was deleted.

test/4.3.1_check_no_ipv6_udp_input.sh

-3
This file was deleted.

test/4.3.2_check_no_ipv6_udp_mangle.sh

-3
This file was deleted.

test/4.4.1_check_no_ipv6_tcp_input.sh

-3
This file was deleted.

test/4.4.2_check_no_ipv6_tcp_prerouting.sh

-3
This file was deleted.

test/Makefile.am

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,18 +1 @@
1-
TESTS=1_module_load.sh \
2-
2_rule_create.sh \
3-
3.1.1_check_ipv4_udp_input.sh \
4-
3.1.2_check_ipv4_udp_prerouting.sh \
5-
3.2.1_check_ipv4_tcp_input.sh \
6-
3.2.2_check_ipv4_tcp_prerouting.sh \
7-
3.3.1_check_ipv6_udp_input.sh \
8-
3.3.2_check_ipv6_udp_prerouting.sh \
9-
3.4.1_check_ipv6_tcp_input.sh \
10-
3.4.2_check_ipv6_tcp_prerouting.sh \
11-
4.1.1_check_no_ipv4_udp_input.sh \
12-
4.1.2_check_no_ipv4_udp_prerouting.sh \
13-
4.2.1_check_no_ipv4_tcp_input.sh \
14-
4.2.2_check_no_ipv4_tcp_prerouting.sh \
15-
4.3.1_check_no_ipv6_udp_input.sh \
16-
4.3.2_check_no_ipv6_udp_mangle.sh \
17-
4.4.1_check_no_ipv6_tcp_input.sh \
18-
4.4.2_check_no_ipv6_tcp_prerouting.sh
1+
SUBDIRS = @RUN_TESTS@
File renamed without changes.

test/2_rule_create.sh test/common/2_rule_create.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function ipt() {
55
table=$2
66
chain=$3
77
act=$4
8-
./test-ipt.sh $cmd $table $chain $act
8+
../util/test-ipt.sh $cmd $table $chain $act
99
}
1010
function begin() {
1111
cmd=$1

test/common/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
TESTS=1_module_load.sh \
2+
2_rule_create.sh
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../util/query_match.sh iptables udp filter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../util/query_match.sh iptables udp mangle
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../util/query_match.sh iptables tcp filter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../util/query_match.sh iptables tcp mangle
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../util/query_nomatch.sh iptables udp filter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../util/query_nomatch.sh iptables udp mangle
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../util/query_nomatch.sh iptables tcp filter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../util/query_nomatch.sh iptables tcp mangle

test/ipv4/Makefile.am

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
TESTS = 3.1.1_check_ipv4_udp_input.sh \
2+
3.1.2_check_ipv4_udp_prerouting.sh \
3+
3.2.1_check_ipv4_tcp_input.sh \
4+
3.2.2_check_ipv4_tcp_prerouting.sh \
5+
4.1.1_check_no_ipv4_udp_input.sh \
6+
4.1.2_check_no_ipv4_udp_prerouting.sh \
7+
4.2.1_check_no_ipv4_tcp_input.sh \
8+
4.2.2_check_no_ipv4_tcp_prerouting.sh
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../util/query_match.sh ip6tables udp filter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../util/query_match.sh ip6tables udp mangle
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../util/query_match.sh ip6tables tcp filter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../util/query_match.sh ip6tables tcp mangle
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../query_nomatch.sh ip6tables udp filter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../query_nomatch.sh ip6tables udp mangle
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../query_nomatch.sh ip6tables tcp filter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
../query_nomatch.sh ip6tables tcp mangle

test/ipv6/Makefile.am

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
TESTS=3.3.1_check_ipv6_udp_input.sh \
2+
3.3.2_check_ipv6_udp_prerouting.sh \
3+
3.4.1_check_ipv6_tcp_input.sh \
4+
3.4.2_check_ipv6_tcp_prerouting.sh \
5+
4.3.1_check_no_ipv6_udp_input.sh \
6+
4.3.2_check_no_ipv6_udp_mangle.sh \
7+
4.4.1_check_no_ipv6_tcp_input.sh \
8+
4.4.2_check_no_ipv6_tcp_prerouting.sh
9+

test/query_match.sh test/util/query_match.sh

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ IPT=$1
44
PROTOCOL=$2
55
TABLE=$3
66

7-
. query_match_common.sh
7+
. ../util/query_match_common.sh
88

99
if [ "$TABLE" = "filter" ] ; then
1010
TARGET_CHAIN="INPUT"
@@ -15,10 +15,11 @@ fi
1515

1616
function match_check() {
1717
val=$1
18+
echo $val
1819
if [ "$val" != "0" ] ; then
19-
return 1
20+
return 0
2021
fi
21-
return 0
22+
return 1
2223
}
2324

2425
function main() {

0 commit comments

Comments
 (0)