fix some netfilter extensions on 2.6.25
[openwrt/openwrt.git] / target / linux / generic-2.6 / patches-2.6.25 / 101-netfilter_layer7_pktmatch.patch
1 Index: linux-2.6.24/include/linux/netfilter/xt_layer7.h
2 ===================================================================
3 --- linux-2.6.24.orig/include/linux/netfilter/xt_layer7.h
4 +++ linux-2.6.24/include/linux/netfilter/xt_layer7.h
5 @@ -8,6 +8,7 @@ struct xt_layer7_info {
6 char protocol[MAX_PROTOCOL_LEN];
7 char pattern[MAX_PATTERN_LEN];
8 u_int8_t invert;
9 + u_int8_t pkt;
10 };
11
12 #endif /* _XT_LAYER7_H */
13 Index: linux-2.6.24/net/netfilter/xt_layer7.c
14 ===================================================================
15 --- linux-2.6.24.orig/net/netfilter/xt_layer7.c
16 +++ linux-2.6.24/net/netfilter/xt_layer7.c
17 @@ -297,34 +297,36 @@ static int match_no_append(struct nf_con
18 }
19
20 /* add the new app data to the conntrack. Return number of bytes added. */
21 -static int add_data(struct nf_conn * master_conntrack,
22 - char * app_data, int appdatalen)
23 +static int add_datastr(char *target, int offset, char *app_data, int len)
24 {
25 int length = 0, i;
26 - int oldlength = master_conntrack->layer7.app_data_len;
27 -
28 - /* This is a fix for a race condition by Deti Fliegl. However, I'm not
29 - clear on whether the race condition exists or whether this really
30 - fixes it. I might just be being dense... Anyway, if it's not really
31 - a fix, all it does is waste a very small amount of time. */
32 - if(!master_conntrack->layer7.app_data) return 0;
33 +
34 + if (!target) return 0;
35
36 /* Strip nulls. Make everything lower case (our regex lib doesn't
37 do case insensitivity). Add it to the end of the current data. */
38 - for(i = 0; i < maxdatalen-oldlength-1 &&
39 - i < appdatalen; i++) {
40 + for(i = 0; i < maxdatalen-offset-1 && i < len; i++) {
41 if(app_data[i] != '\0') {
42 /* the kernel version of tolower mungs 'upper ascii' */
43 - master_conntrack->layer7.app_data[length+oldlength] =
44 + target[length+offset] =
45 isascii(app_data[i])?
46 tolower(app_data[i]) : app_data[i];
47 length++;
48 }
49 }
50 + target[length+offset] = '\0';
51 +
52 + return length;
53 +}
54
55 - master_conntrack->layer7.app_data[length+oldlength] = '\0';
56 - master_conntrack->layer7.app_data_len = length + oldlength;
57 +/* add the new app data to the conntrack. Return number of bytes added. */
58 +static int add_data(struct nf_conn * master_conntrack,
59 + char * app_data, int appdatalen)
60 +{
61 + int length;
62
63 + length = add_datastr(master_conntrack->layer7.app_data, master_conntrack->layer7.app_data_len, app_data, appdatalen);
64 + master_conntrack->layer7.app_data_len += length;
65 return length;
66 }
67
68 @@ -411,7 +413,7 @@ match(const struct sk_buff *skbin,
69 const struct xt_layer7_info * info = matchinfo;
70 enum ip_conntrack_info master_ctinfo, ctinfo;
71 struct nf_conn *master_conntrack, *conntrack;
72 - unsigned char * app_data;
73 + unsigned char *app_data, *tmp_data;
74 unsigned int pattern_result, appdatalen;
75 regexp * comppattern;
76
77 @@ -439,8 +441,8 @@ match(const struct sk_buff *skbin,
78 master_conntrack = master_ct(master_conntrack);
79
80 /* if we've classified it or seen too many packets */
81 - if(TOTAL_PACKETS > num_packets ||
82 - master_conntrack->layer7.app_proto) {
83 + if(!info->pkt && (TOTAL_PACKETS > num_packets ||
84 + master_conntrack->layer7.app_proto)) {
85
86 pattern_result = match_no_append(conntrack, master_conntrack,
87 ctinfo, master_ctinfo, info);
88 @@ -473,6 +475,25 @@ match(const struct sk_buff *skbin,
89 /* the return value gets checked later, when we're ready to use it */
90 comppattern = compile_and_cache(info->pattern, info->protocol);
91
92 + if (info->pkt) {
93 + tmp_data = kmalloc(maxdatalen, GFP_ATOMIC);
94 + if(!tmp_data){
95 + if (net_ratelimit())
96 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
97 + return info->invert;
98 + }
99 +
100 + tmp_data[0] = '\0';
101 + add_datastr(tmp_data, 0, app_data, appdatalen);
102 + pattern_result = ((comppattern && regexec(comppattern, tmp_data)) ? 1 : 0);
103 +
104 + kfree(tmp_data);
105 + tmp_data = NULL;
106 + spin_unlock_bh(&l7_lock);
107 +
108 + return (pattern_result ^ info->invert);
109 + }
110 +
111 /* On the first packet of a connection, allocate space for app data */
112 if(TOTAL_PACKETS == 1 && !skb->cb[0] &&
113 !master_conntrack->layer7.app_data){