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