upgrade layer7 to the latest version and add fixes for 2.6.21 and 2.6.22-rc - compile...
[openwrt.git] / target / linux / generic-2.6 / patches / 101-netfilter_layer7_pktmatch.patch
1 Index: linux-2.6.21.5/include/linux/netfilter_ipv4/ipt_layer7.h
2 ===================================================================
3 --- linux-2.6.21.5.orig/include/linux/netfilter_ipv4/ipt_layer7.h       2007-07-02 00:43:58.271086750 +0200
4 +++ linux-2.6.21.5/include/linux/netfilter_ipv4/ipt_layer7.h    2007-07-02 01:36:08.914739750 +0200
5 @@ -21,6 +21,7 @@
6      char protocol[MAX_PROTOCOL_LEN];
7      char invert:1;
8      char pattern[MAX_PATTERN_LEN];
9 +    char pkt;
10  };
11  
12  #endif /* _IPT_LAYER7_H */
13 Index: linux-2.6.21.5/net/ipv4/netfilter/ipt_layer7.c
14 ===================================================================
15 --- linux-2.6.21.5.orig/net/ipv4/netfilter/ipt_layer7.c 2007-07-02 01:27:54.195821750 +0200
16 +++ linux-2.6.21.5/net/ipv4/netfilter/ipt_layer7.c      2007-07-02 01:37:01.990056750 +0200
17 @@ -299,33 +299,34 @@
18         }
19  }
20  
21 -/* add the new app data to the conntrack.  Return number of bytes added. */
22 -static int add_data(struct ip_conntrack * master_conntrack,
23 -                       char * app_data, int appdatalen)
24 +static int add_datastr(char *target, int offset, char *app_data, int len)
25  {
26         int length = 0, i;
27 -       int oldlength = master_conntrack->layer7.app_data_len;
28 -
29 -       // This is a fix for a race condition by Deti Fliegl. However, I'm not 
30 -       // clear on whether the race condition exists or whether this really 
31 -       // fixes it.  I might just be being dense... Anyway, if it's not really 
32 -       // a fix, all it does is waste a very small amount of time.
33 -       if(!master_conntrack->layer7.app_data) return 0;
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 -                       master_conntrack->layer7.app_data[length+oldlength] =
43 +                       target[length+offset] =
44                                 /* the kernel version of tolower mungs 'upper ascii' */
45                                 isascii(app_data[i])? tolower(app_data[i]) : app_data[i];
46                         length++;
47                 }
48         }
49 +       target[length+offset] = '\0';
50  
51 -       master_conntrack->layer7.app_data[length+oldlength] = '\0';
52 -       master_conntrack->layer7.app_data_len = length + oldlength;
53 +       return length;
54 +}
55 +
56 +/* add the new app data to the conntrack.  Return number of bytes added. */
57 +static int add_data(struct ip_conntrack * master_conntrack,
58 +                       char * app_data, int appdatalen)
59 +{
60 +       int length;
61 +
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 @@ -343,7 +344,7 @@
68         enum ip_conntrack_info master_ctinfo, ctinfo;
69         struct nf_conn *master_conntrack;
70         struct ip_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 @@ -365,8 +366,8 @@
77                 master_conntrack = master_ct(master_conntrack);
78  
79         /* if we've classified it or seen too many packets */
80 -       if(TOTAL_PACKETS > num_packets ||
81 -          master_conntrack->layer7.app_proto) {
82 +       if(!info->pkt && (TOTAL_PACKETS > num_packets ||
83 +               master_conntrack->layer7.app_proto)) {
84  
85                 pattern_result = match_no_append(conntrack, master_conntrack, ctinfo, master_ctinfo, info);
86  
87 @@ -396,6 +397,23 @@
88         comppattern = compile_and_cache(info->pattern, info->protocol);
89         spin_unlock_bh(&list_lock);
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 +               kfree(tmp_data);
103 +               tmp_data = NULL;
104 +
105 +               return (pattern_result ^ info->invert);
106 +       }
107 +
108         /* On the first packet of a connection, allocate space for app data */
109         write_lock(&ct_lock);
110         if(TOTAL_PACKETS == 1 && !skb->cb[0] && !master_conntrack->layer7.app_data) {