Make L7-filter compile with kernel 2.6.27 (#4253)
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.27 / 101-netfilter_layer7_pktmatch.patch
1 diff --git a/include/linux/netfilter/xt_layer7.h b/include/linux/netfilter/xt_layer7.h
2 index 147cd64..c38d3c4 100644
3 --- a/include/linux/netfilter/xt_layer7.h
4 +++ b/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 diff --git a/net/netfilter/xt_layer7.c b/net/netfilter/xt_layer7.c
14 index a9b88d3..e0e962d 100644
15 --- a/net/netfilter/xt_layer7.c
16 +++ b/net/netfilter/xt_layer7.c
17 @@ -314,33 +314,35 @@ static int match_no_append(struct nf_conn * conntrack,
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 +       if (!target) return 0;
34  
35         /* Strip nulls. Make everything lower case (our regex lib doesn't
36         do case insensitivity).  Add it to the end of the current data. */
37 -       for(i = 0; i < maxdatalen-oldlength-1 &&
38 -                  i < appdatalen; i++) {
39 +       for(i = 0; i < maxdatalen-offset-1 && i < len; i++) {
40                 if(app_data[i] != '\0') {
41                         /* the kernel version of tolower mungs 'upper ascii' */
42 -                       master_conntrack->layer7.app_data[length+oldlength] =
43 +                       target[length+offset] =
44                                 isascii(app_data[i])? 
45                                         tolower(app_data[i]) : app_data[i];
46                         length++;
47                 }
48         }
49 +       target[length+offset] = '\0';
50 +
51 +       return length;
52 +}
53 +
54 +/* add the new app data to the conntrack.  Return number of bytes added. */
55 +static int add_data(struct nf_conn * master_conntrack,
56 +                    char * app_data, int appdatalen)
57 +{
58 +       int length;
59  
60 -       master_conntrack->layer7.app_data[length+oldlength] = '\0';
61 -       master_conntrack->layer7.app_data_len = length + oldlength;
62 +       length = add_datastr(master_conntrack->layer7.app_data, master_conntrack->layer7.app_data_len, app_data, appdatalen);
63 +       master_conntrack->layer7.app_data_len += length;
64  
65         return length;
66  }
67 @@ -428,7 +430,7 @@ match(const struct sk_buff *skbin,
68         const struct xt_layer7_info * info = matchinfo;
69         enum ip_conntrack_info master_ctinfo, ctinfo;
70         struct nf_conn *master_conntrack, *conntrack;
71 -       unsigned char * app_data;
72 +       unsigned char *app_data, *tmp_data;
73         unsigned int pattern_result, appdatalen;
74         regexp * comppattern;
75  
76 @@ -456,8 +458,8 @@ match(const struct sk_buff *skbin,
77                 master_conntrack = master_ct(master_conntrack);
78  
79         /* if we've classified it or seen too many packets */
80 -       if(total_acct_packets(master_conntrack) > num_packets ||
81 -          master_conntrack->layer7.app_proto) {
82 +       if(!info->pkt && (total_acct_packets(master_conntrack) > num_packets ||
83 +          master_conntrack->layer7.app_proto)) {
84  
85                 pattern_result = match_no_append(conntrack, master_conntrack, 
86                                                  ctinfo, master_ctinfo, info);
87 @@ -490,6 +492,25 @@ match(const struct sk_buff *skbin,
88         /* the return value gets checked later, when we're ready to use it */
89         comppattern = compile_and_cache(info->pattern, info->protocol);
90  
91 +       if (info->pkt) {
92 +               tmp_data = kmalloc(maxdatalen, GFP_ATOMIC);
93 +               if(!tmp_data){
94 +                       if (net_ratelimit())
95 +                               printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
96 +                       return info->invert;
97 +               }
98 +
99 +               tmp_data[0] = '\0';
100 +               add_datastr(tmp_data, 0, app_data, appdatalen);
101 +               pattern_result = ((comppattern && regexec(comppattern, tmp_data)) ? 1 : 0);
102 +
103 +               kfree(tmp_data);
104 +               tmp_data = NULL;
105 +               spin_unlock_bh(&l7_lock);
106 +
107 +               return (pattern_result ^ info->invert);
108 +       }
109 +
110         /* On the first packet of a connection, allocate space for app data */
111         if(total_acct_packets(master_conntrack) == 1 && !skb->cb[0] && 
112            !master_conntrack->layer7.app_data){