fix x86 lzma loader
[openwrt/openwrt.git] / target / linux / generic-2.6 / patches / 100-netfilter_layer7_2.8.patch
1 diff -urN linux.old/include/linux/netfilter_ipv4/ip_conntrack.h linux.dev/include/linux/netfilter_ipv4/ip_conntrack.h
2 --- linux.old/include/linux/netfilter_ipv4/ip_conntrack.h 2007-01-01 05:17:07.000000000 +0100
3 +++ linux.dev/include/linux/netfilter_ipv4/ip_conntrack.h 2007-01-01 05:18:48.000000000 +0100
4 @@ -127,6 +127,15 @@
5 /* Traversed often, so hopefully in different cacheline to top */
6 /* These are my tuples; original and reply */
7 struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
8 +
9 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
10 + struct {
11 + char * app_proto; /* e.g. "http". NULL before decision. "unknown" after decision if no match */
12 + char * app_data; /* application layer data so far. NULL after match decision */
13 + unsigned int app_data_len;
14 + } layer7;
15 +#endif
16 +
17 };
18
19 struct ip_conntrack_expect
20 diff -urN linux.old/include/linux/netfilter_ipv4/ipt_layer7.h linux.dev/include/linux/netfilter_ipv4/ipt_layer7.h
21 --- linux.old/include/linux/netfilter_ipv4/ipt_layer7.h 1970-01-01 01:00:00.000000000 +0100
22 +++ linux.dev/include/linux/netfilter_ipv4/ipt_layer7.h 2007-01-01 05:18:48.000000000 +0100
23 @@ -0,0 +1,26 @@
24 +/*
25 + By Matthew Strait <quadong@users.sf.net>, Dec 2003.
26 + http://l7-filter.sf.net
27 +
28 + This program is free software; you can redistribute it and/or
29 + modify it under the terms of the GNU General Public License
30 + as published by the Free Software Foundation; either version
31 + 2 of the License, or (at your option) any later version.
32 + http://www.gnu.org/licenses/gpl.txt
33 +*/
34 +
35 +#ifndef _IPT_LAYER7_H
36 +#define _IPT_LAYER7_H
37 +
38 +#define MAX_PATTERN_LEN 8192
39 +#define MAX_PROTOCOL_LEN 256
40 +
41 +typedef char *(*proc_ipt_search) (char *, char, char *);
42 +
43 +struct ipt_layer7_info {
44 + char protocol[MAX_PROTOCOL_LEN];
45 + char invert:1;
46 + char pattern[MAX_PATTERN_LEN];
47 +};
48 +
49 +#endif /* _IPT_LAYER7_H */
50 diff -urN linux.old/net/ipv4/netfilter/ip_conntrack_core.c linux.dev/net/ipv4/netfilter/ip_conntrack_core.c
51 --- linux.old/net/ipv4/netfilter/ip_conntrack_core.c 2007-01-01 05:17:07.000000000 +0100
52 +++ linux.dev/net/ipv4/netfilter/ip_conntrack_core.c 2007-01-01 05:18:48.000000000 +0100
53 @@ -337,6 +337,13 @@
54 * too. */
55 ip_ct_remove_expectations(ct);
56
57 + #if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
58 + if(ct->layer7.app_proto)
59 + kfree(ct->layer7.app_proto);
60 + if(ct->layer7.app_data)
61 + kfree(ct->layer7.app_data);
62 + #endif
63 +
64 /* We overload first tuple to link into unconfirmed list. */
65 if (!is_confirmed(ct)) {
66 BUG_ON(list_empty(&ct->tuplehash[IP_CT_DIR_ORIGINAL].list));
67 diff -urN linux.old/net/ipv4/netfilter/ip_conntrack_standalone.c linux.dev/net/ipv4/netfilter/ip_conntrack_standalone.c
68 --- linux.old/net/ipv4/netfilter/ip_conntrack_standalone.c 2007-01-01 05:17:07.000000000 +0100
69 +++ linux.dev/net/ipv4/netfilter/ip_conntrack_standalone.c 2007-01-01 05:18:48.000000000 +0100
70 @@ -192,6 +192,12 @@
71 return -ENOSPC;
72 #endif
73
74 +#if defined(CONFIG_IP_NF_MATCH_LAYER7) || defined(CONFIG_IP_NF_MATCH_LAYER7_MODULE)
75 + if(conntrack->layer7.app_proto)
76 + if (seq_printf(s, "l7proto=%s ",conntrack->layer7.app_proto))
77 + return 1;
78 +#endif
79 +
80 if (seq_printf(s, "use=%u\n", atomic_read(&conntrack->ct_general.use)))
81 return -ENOSPC;
82
83 diff -urN linux.old/net/ipv4/netfilter/ipt_layer7.c linux.dev/net/ipv4/netfilter/ipt_layer7.c
84 --- linux.old/net/ipv4/netfilter/ipt_layer7.c 1970-01-01 01:00:00.000000000 +0100
85 +++ linux.dev/net/ipv4/netfilter/ipt_layer7.c 2007-01-01 05:18:48.000000000 +0100
86 @@ -0,0 +1,573 @@
87 +/*
88 + Kernel module to match application layer (OSI layer 7) data in connections.
89 +
90 + http://l7-filter.sf.net
91 +
92 + By Matthew Strait and Ethan Sommer, 2003-2006.
93 +
94 + This program is free software; you can redistribute it and/or
95 + modify it under the terms of the GNU General Public License
96 + as published by the Free Software Foundation; either version
97 + 2 of the License, or (at your option) any later version.
98 + http://www.gnu.org/licenses/gpl.txt
99 +
100 + Based on ipt_string.c (C) 2000 Emmanuel Roger <winfield@freegates.be>
101 + and cls_layer7.c (C) 2003 Matthew Strait, Ethan Sommer, Justin Levandoski
102 +*/
103 +
104 +#include <linux/module.h>
105 +#include <linux/skbuff.h>
106 +#include <linux/netfilter_ipv4/ip_conntrack.h>
107 +#include <linux/proc_fs.h>
108 +#include <linux/ctype.h>
109 +#include <net/ip.h>
110 +#include <net/tcp.h>
111 +#include <linux/spinlock.h>
112 +
113 +#include "regexp/regexp.c"
114 +
115 +#include <linux/netfilter_ipv4/ipt_layer7.h>
116 +#include <linux/netfilter_ipv4/ip_tables.h>
117 +
118 +MODULE_AUTHOR("Matthew Strait <quadong@users.sf.net>, Ethan Sommer <sommere@users.sf.net>");
119 +MODULE_LICENSE("GPL");
120 +MODULE_DESCRIPTION("iptables application layer match module");
121 +MODULE_VERSION("2.0");
122 +
123 +static int maxdatalen = 2048; // this is the default
124 +module_param(maxdatalen, int, 0444);
125 +MODULE_PARM_DESC(maxdatalen, "maximum bytes of data looked at by l7-filter");
126 +
127 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
128 + #define DPRINTK(format,args...) printk(format,##args)
129 +#else
130 + #define DPRINTK(format,args...)
131 +#endif
132 +
133 +#define TOTAL_PACKETS master_conntrack->counters[IP_CT_DIR_ORIGINAL].packets + \
134 + master_conntrack->counters[IP_CT_DIR_REPLY].packets
135 +
136 +/* Number of packets whose data we look at.
137 +This can be modified through /proc/net/layer7_numpackets */
138 +static int num_packets = 10;
139 +
140 +static struct pattern_cache {
141 + char * regex_string;
142 + regexp * pattern;
143 + struct pattern_cache * next;
144 +} * first_pattern_cache = NULL;
145 +
146 +/* I'm new to locking. Here are my assumptions:
147 +
148 +- No one will write to /proc/net/layer7_numpackets over and over very fast;
149 + if they did, nothing awful would happen.
150 +
151 +- This code will never be processing the same packet twice at the same time,
152 + because iptables rules are traversed in order.
153 +
154 +- It doesn't matter if two packets from different connections are in here at
155 + the same time, because they don't share any data.
156 +
157 +- It _does_ matter if two packets from the same connection (or one from a
158 + master and one from its child) are here at the same time. In this case,
159 + we have to protect the conntracks and the list of compiled patterns.
160 +*/
161 +DEFINE_RWLOCK(ct_lock);
162 +DEFINE_SPINLOCK(list_lock);
163 +
164 +#ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
165 +/* Converts an unfriendly string into a friendly one by
166 +replacing unprintables with periods and all whitespace with " ". */
167 +static char * friendly_print(unsigned char * s)
168 +{
169 + char * f = kmalloc(strlen(s) + 1, GFP_ATOMIC);
170 + int i;
171 +
172 + if(!f) {
173 + if (net_ratelimit())
174 + printk(KERN_ERR "layer7: out of memory in friendly_print, bailing.\n");
175 + return NULL;
176 + }
177 +
178 + for(i = 0; i < strlen(s); i++){
179 + if(isprint(s[i]) && s[i] < 128) f[i] = s[i];
180 + else if(isspace(s[i])) f[i] = ' ';
181 + else f[i] = '.';
182 + }
183 + f[i] = '\0';
184 + return f;
185 +}
186 +
187 +static char dec2hex(int i)
188 +{
189 + switch (i) {
190 + case 0 ... 9:
191 + return (char)(i + '0');
192 + break;
193 + case 10 ... 15:
194 + return (char)(i - 10 + 'a');
195 + break;
196 + default:
197 + if (net_ratelimit())
198 + printk("Problem in dec2hex\n");
199 + return '\0';
200 + }
201 +}
202 +
203 +static char * hex_print(unsigned char * s)
204 +{
205 + char * g = kmalloc(strlen(s)*3 + 1, GFP_ATOMIC);
206 + int i;
207 +
208 + if(!g) {
209 + if (net_ratelimit())
210 + printk(KERN_ERR "layer7: out of memory in hex_print, bailing.\n");
211 + return NULL;
212 + }
213 +
214 + for(i = 0; i < strlen(s); i++) {
215 + g[i*3 ] = dec2hex(s[i]/16);
216 + g[i*3 + 1] = dec2hex(s[i]%16);
217 + g[i*3 + 2] = ' ';
218 + }
219 + g[i*3] = '\0';
220 +
221 + return g;
222 +}
223 +#endif // DEBUG
224 +
225 +/* Use instead of regcomp. As we expect to be seeing the same regexps over and
226 +over again, it make sense to cache the results. */
227 +static regexp * compile_and_cache(char * regex_string, char * protocol)
228 +{
229 + struct pattern_cache * node = first_pattern_cache;
230 + struct pattern_cache * last_pattern_cache = first_pattern_cache;
231 + struct pattern_cache * tmp;
232 + unsigned int len;
233 +
234 + while (node != NULL) {
235 + if (!strcmp(node->regex_string, regex_string))
236 + return node->pattern;
237 +
238 + last_pattern_cache = node;/* points at the last non-NULL node */
239 + node = node->next;
240 + }
241 +
242 + /* If we reach the end of the list, then we have not yet cached
243 + the pattern for this regex. Let's do that now.
244 + Be paranoid about running out of memory to avoid list corruption. */
245 + tmp = kmalloc(sizeof(struct pattern_cache), GFP_ATOMIC);
246 +
247 + if(!tmp) {
248 + if (net_ratelimit())
249 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
250 + return NULL;
251 + }
252 +
253 + tmp->regex_string = kmalloc(strlen(regex_string) + 1, GFP_ATOMIC);
254 + tmp->pattern = kmalloc(sizeof(struct regexp), GFP_ATOMIC);
255 + tmp->next = NULL;
256 +
257 + if(!tmp->regex_string || !tmp->pattern) {
258 + if (net_ratelimit())
259 + printk(KERN_ERR "layer7: out of memory in compile_and_cache, bailing.\n");
260 + kfree(tmp->regex_string);
261 + kfree(tmp->pattern);
262 + kfree(tmp);
263 + return NULL;
264 + }
265 +
266 + /* Ok. The new node is all ready now. */
267 + node = tmp;
268 +
269 + if(first_pattern_cache == NULL) /* list is empty */
270 + first_pattern_cache = node; /* make node the beginning */
271 + else
272 + last_pattern_cache->next = node; /* attach node to the end */
273 +
274 + /* copy the string and compile the regex */
275 + len = strlen(regex_string);
276 + DPRINTK("About to compile this: \"%s\"\n", regex_string);
277 + node->pattern = regcomp(regex_string, &len);
278 + if ( !node->pattern ) {
279 + if (net_ratelimit())
280 + printk(KERN_ERR "layer7: Error compiling regexp \"%s\" (%s)\n", regex_string, protocol);
281 + /* pattern is now cached as NULL, so we won't try again. */
282 + }
283 +
284 + strcpy(node->regex_string, regex_string);
285 + return node->pattern;
286 +}
287 +
288 +static int can_handle(const struct sk_buff *skb)
289 +{
290 + if(!skb->nh.iph) /* not IP */
291 + return 0;
292 + if(skb->nh.iph->protocol != IPPROTO_TCP &&
293 + skb->nh.iph->protocol != IPPROTO_UDP &&
294 + skb->nh.iph->protocol != IPPROTO_ICMP)
295 + return 0;
296 + return 1;
297 +}
298 +
299 +/* Returns offset the into the skb->data that the application data starts */
300 +static int app_data_offset(const struct sk_buff *skb)
301 +{
302 + /* In case we are ported somewhere (ebtables?) where skb->nh.iph
303 + isn't set, this can be gotten from 4*(skb->data[0] & 0x0f) as well. */
304 + int ip_hl = 4*skb->nh.iph->ihl;
305 +
306 + if( skb->nh.iph->protocol == IPPROTO_TCP ) {
307 + /* 12 == offset into TCP header for the header length field.
308 + Can't get this with skb->h.th->doff because the tcphdr
309 + struct doesn't get set when routing (this is confirmed to be
310 + true in Netfilter as well as QoS.) */
311 + int tcp_hl = 4*(skb->data[ip_hl + 12] >> 4);
312 +
313 + return ip_hl + tcp_hl;
314 + } else if( skb->nh.iph->protocol == IPPROTO_UDP ) {
315 + return ip_hl + 8; /* UDP header is always 8 bytes */
316 + } else if( skb->nh.iph->protocol == IPPROTO_ICMP ) {
317 + return ip_hl + 8; /* ICMP header is 8 bytes */
318 + } else {
319 + if (net_ratelimit())
320 + printk(KERN_ERR "layer7: tried to handle unknown protocol!\n");
321 + return ip_hl + 8; /* something reasonable */
322 + }
323 +}
324 +
325 +/* handles whether there's a match when we aren't appending data anymore */
326 +static int match_no_append(struct ip_conntrack * conntrack, struct ip_conntrack * master_conntrack,
327 + enum ip_conntrack_info ctinfo, enum ip_conntrack_info master_ctinfo,
328 + struct ipt_layer7_info * info)
329 +{
330 + /* If we're in here, throw the app data away */
331 + write_lock(&ct_lock);
332 + if(master_conntrack->layer7.app_data != NULL) {
333 +
334 + #ifdef CONFIG_IP_NF_MATCH_LAYER7_DEBUG
335 + if(!master_conntrack->layer7.app_proto) {
336 + char * f = friendly_print(master_conntrack->layer7.app_data);
337 + char * g = hex_print(master_conntrack->layer7.app_data);
338 + DPRINTK("\nl7-filter gave up after %d bytes (%d packets):\n%s\n",
339 + strlen(f), TOTAL_PACKETS, f);
340 + kfree(f);
341 + DPRINTK("In hex: %s\n", g);
342 + kfree(g);
343 + }
344 + #endif
345 +
346 + kfree(master_conntrack->layer7.app_data);
347 + master_conntrack->layer7.app_data = NULL; /* don't free again */
348 + }
349 + write_unlock(&ct_lock);
350 +
351 + if(master_conntrack->layer7.app_proto){
352 + /* Here child connections set their .app_proto (for /proc/net/ip_conntrack) */
353 + write_lock(&ct_lock);
354 + if(!conntrack->layer7.app_proto) {
355 + conntrack->layer7.app_proto = kmalloc(strlen(master_conntrack->layer7.app_proto)+1, GFP_ATOMIC);
356 + if(!conntrack->layer7.app_proto){
357 + if (net_ratelimit())
358 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
359 + write_unlock(&ct_lock);
360 + return 1;
361 + }
362 + strcpy(conntrack->layer7.app_proto, master_conntrack->layer7.app_proto);
363 + }
364 + write_unlock(&ct_lock);
365 +
366 + return (!strcmp(master_conntrack->layer7.app_proto, info->protocol));
367 + }
368 + else {
369 + /* If not classified, set to "unknown" to distinguish from
370 + connections that are still being tested. */
371 + write_lock(&ct_lock);
372 + master_conntrack->layer7.app_proto = kmalloc(strlen("unknown")+1, GFP_ATOMIC);
373 + if(!master_conntrack->layer7.app_proto){
374 + if (net_ratelimit())
375 + printk(KERN_ERR "layer7: out of memory in match_no_append, bailing.\n");
376 + write_unlock(&ct_lock);
377 + return 1;
378 + }
379 + strcpy(master_conntrack->layer7.app_proto, "unknown");
380 + write_unlock(&ct_lock);
381 + return 0;
382 + }
383 +}
384 +
385 +/* add the new app data to the conntrack. Return number of bytes added. */
386 +static int add_data(struct ip_conntrack * master_conntrack,
387 + char * app_data, int appdatalen)
388 +{
389 + int length = 0, i;
390 + int oldlength = master_conntrack->layer7.app_data_len;
391 +
392 + // This is a fix for a race condition by Deti Fliegl. However, I'm not
393 + // clear on whether the race condition exists or whether this really
394 + // fixes it. I might just be being dense... Anyway, if it's not really
395 + // a fix, all it does is waste a very small amount of time.
396 + if(!master_conntrack->layer7.app_data) return 0;
397 +
398 + /* Strip nulls. Make everything lower case (our regex lib doesn't
399 + do case insensitivity). Add it to the end of the current data. */
400 + for(i = 0; i < maxdatalen-oldlength-1 &&
401 + i < appdatalen; i++) {
402 + if(app_data[i] != '\0') {
403 + master_conntrack->layer7.app_data[length+oldlength] =
404 + /* the kernel version of tolower mungs 'upper ascii' */
405 + isascii(app_data[i])? tolower(app_data[i]) : app_data[i];
406 + length++;
407 + }
408 + }
409 +
410 + master_conntrack->layer7.app_data[length+oldlength] = '\0';
411 + master_conntrack->layer7.app_data_len = length + oldlength;
412 +
413 + return length;
414 +}
415 +
416 +/* Returns true on match and false otherwise. */
417 +static int match(const struct sk_buff *skbin,
418 + const struct net_device *in, const struct net_device *out,
419 + const struct xt_match *match, const void *matchinfo,
420 + int offset, unsigned int protoff, int *hotdrop)
421 +{
422 + /* sidestep const without getting a compiler warning... */
423 + struct sk_buff * skb = (struct sk_buff *)skbin;
424 +
425 + struct ipt_layer7_info * info = (struct ipt_layer7_info *)matchinfo;
426 + enum ip_conntrack_info master_ctinfo, ctinfo;
427 + struct ip_conntrack *master_conntrack, *conntrack;
428 + unsigned char * app_data;
429 + unsigned int pattern_result, appdatalen;
430 + regexp * comppattern;
431 +
432 + if(!can_handle(skb)){
433 + DPRINTK("layer7: This is some protocol I can't handle.\n");
434 + return info->invert;
435 + }
436 +
437 + /* Treat parent & all its children together as one connection, except
438 + for the purpose of setting conntrack->layer7.app_proto in the actual
439 + connection. This makes /proc/net/ip_conntrack more satisfying. */
440 + if(!(conntrack = ip_conntrack_get((struct sk_buff *)skb, &ctinfo)) ||
441 + !(master_conntrack = ip_conntrack_get((struct sk_buff *)skb, &master_ctinfo))) {
442 + //DPRINTK("layer7: packet is not from a known connection, giving up.\n");
443 + return info->invert;
444 + }
445 +
446 + /* Try to get a master conntrack (and its master etc) for FTP, etc. */
447 + while (master_ct(master_conntrack) != NULL)
448 + master_conntrack = master_ct(master_conntrack);
449 +
450 + /* if we've classified it or seen too many packets */
451 + if(TOTAL_PACKETS > num_packets ||
452 + master_conntrack->layer7.app_proto) {
453 +
454 + pattern_result = match_no_append(conntrack, master_conntrack, ctinfo, master_ctinfo, info);
455 +
456 + /* skb->cb[0] == seen. Avoid doing things twice if there are two l7
457 + rules. I'm not sure that using cb for this purpose is correct, although
458 + it says "put your private variables there". But it doesn't look like it
459 + is being used for anything else in the skbs that make it here. How can
460 + I write to cb without making the compiler angry? */
461 + skb->cb[0] = 1; /* marking it seen here is probably irrelevant, but consistant */
462 +
463 + return (pattern_result ^ info->invert);
464 + }
465 +
466 + if(skb_is_nonlinear(skb)){
467 + if(skb_linearize(skb) != 0){
468 + if (net_ratelimit())
469 + printk(KERN_ERR "layer7: failed to linearize packet, bailing.\n");
470 + return info->invert;
471 + }
472 + }
473 +
474 + /* now that the skb is linearized, it's safe to set these. */
475 + app_data = skb->data + app_data_offset(skb);
476 + appdatalen = skb->tail - app_data;
477 +
478 + spin_lock_bh(&list_lock);
479 + /* the return value gets checked later, when we're ready to use it */
480 + comppattern = compile_and_cache(info->pattern, info->protocol);
481 + spin_unlock_bh(&list_lock);
482 +
483 + /* On the first packet of a connection, allocate space for app data */
484 + write_lock(&ct_lock);
485 + if(TOTAL_PACKETS == 1 && !skb->cb[0] && !master_conntrack->layer7.app_data) {
486 + master_conntrack->layer7.app_data = kmalloc(maxdatalen, GFP_ATOMIC);
487 + if(!master_conntrack->layer7.app_data){
488 + if (net_ratelimit())
489 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
490 + write_unlock(&ct_lock);
491 + return info->invert;
492 + }
493 +
494 + master_conntrack->layer7.app_data[0] = '\0';
495 + }
496 + write_unlock(&ct_lock);
497 +
498 + /* Can be here, but unallocated, if numpackets is increased near
499 + the beginning of a connection */
500 + if(master_conntrack->layer7.app_data == NULL)
501 + return (info->invert); /* unmatched */
502 +
503 + if(!skb->cb[0]){
504 + int newbytes;
505 + write_lock(&ct_lock);
506 + newbytes = add_data(master_conntrack, app_data, appdatalen);
507 + write_unlock(&ct_lock);
508 +
509 + if(newbytes == 0) { /* didn't add any data */
510 + skb->cb[0] = 1;
511 + /* Didn't match before, not going to match now */
512 + return info->invert;
513 + }
514 + }
515 +
516 + /* If looking for "unknown", then never match. "Unknown" means that
517 + we've given up; we're still trying with these packets. */
518 + read_lock(&ct_lock);
519 + if(!strcmp(info->protocol, "unknown")) {
520 + pattern_result = 0;
521 + /* If the regexp failed to compile, don't bother running it */
522 + } else if(comppattern && regexec(comppattern, master_conntrack->layer7.app_data)) {
523 + DPRINTK("layer7: matched %s\n", info->protocol);
524 + pattern_result = 1;
525 + } else pattern_result = 0;
526 + read_unlock(&ct_lock);
527 +
528 + if(pattern_result) {
529 + write_lock(&ct_lock);
530 + master_conntrack->layer7.app_proto = kmalloc(strlen(info->protocol)+1, GFP_ATOMIC);
531 + if(!master_conntrack->layer7.app_proto){
532 + if (net_ratelimit())
533 + printk(KERN_ERR "layer7: out of memory in match, bailing.\n");
534 + write_unlock(&ct_lock);
535 + return (pattern_result ^ info->invert);
536 + }
537 + strcpy(master_conntrack->layer7.app_proto, info->protocol);
538 + write_unlock(&ct_lock);
539 + }
540 +
541 + /* mark the packet seen */
542 + skb->cb[0] = 1;
543 +
544 + return (pattern_result ^ info->invert);
545 +}
546 +
547 +static struct ipt_match layer7_match = {
548 + .name = "layer7",
549 + .match = &match,
550 + .matchsize = sizeof(struct ipt_layer7_info),
551 + .me = THIS_MODULE
552 +};
553 +
554 +/* taken from drivers/video/modedb.c */
555 +static int my_atoi(const char *s)
556 +{
557 + int val = 0;
558 +
559 + for (;; s++) {
560 + switch (*s) {
561 + case '0'...'9':
562 + val = 10*val+(*s-'0');
563 + break;
564 + default:
565 + return val;
566 + }
567 + }
568 +}
569 +
570 +/* write out num_packets to userland. */
571 +static int layer7_read_proc(char* page, char ** start, off_t off, int count,
572 + int* eof, void * data)
573 +{
574 + if(num_packets > 99 && net_ratelimit())
575 + printk(KERN_ERR "layer7: NOT REACHED. num_packets too big\n");
576 +
577 + page[0] = num_packets/10 + '0';
578 + page[1] = num_packets%10 + '0';
579 + page[2] = '\n';
580 + page[3] = '\0';
581 +
582 + *eof=1;
583 +
584 + return 3;
585 +}
586 +
587 +/* Read in num_packets from userland */
588 +static int layer7_write_proc(struct file* file, const char* buffer,
589 + unsigned long count, void *data)
590 +{
591 + char * foo = kmalloc(count, GFP_ATOMIC);
592 +
593 + if(!foo){
594 + if (net_ratelimit())
595 + printk(KERN_ERR "layer7: out of memory, bailing. num_packets unchanged.\n");
596 + return count;
597 + }
598 +
599 + if(copy_from_user(foo, buffer, count)) {
600 + return -EFAULT;
601 + }
602 +
603 +
604 + num_packets = my_atoi(foo);
605 + kfree (foo);
606 +
607 + /* This has an arbitrary limit to make the math easier. I'm lazy.
608 + But anyway, 99 is a LOT! If you want more, you're doing it wrong! */
609 + if(num_packets > 99) {
610 + printk(KERN_WARNING "layer7: num_packets can't be > 99.\n");
611 + num_packets = 99;
612 + } else if(num_packets < 1) {
613 + printk(KERN_WARNING "layer7: num_packets can't be < 1.\n");
614 + num_packets = 1;
615 + }
616 +
617 + return count;
618 +}
619 +
620 +/* register the proc file */
621 +static void layer7_init_proc(void)
622 +{
623 + struct proc_dir_entry* entry;
624 + entry = create_proc_entry("layer7_numpackets", 0644, proc_net);
625 + entry->read_proc = layer7_read_proc;
626 + entry->write_proc = layer7_write_proc;
627 +}
628 +
629 +static void layer7_cleanup_proc(void)
630 +{
631 + remove_proc_entry("layer7_numpackets", proc_net);
632 +}
633 +
634 +static int __init ipt_layer7_init(void)
635 +{
636 + need_conntrack();
637 +
638 + layer7_init_proc();
639 + if(maxdatalen < 1) {
640 + printk(KERN_WARNING "layer7: maxdatalen can't be < 1, using 1\n");
641 + maxdatalen = 1;
642 + }
643 + /* This is not a hard limit. It's just here to prevent people from
644 + bringing their slow machines to a grinding halt. */
645 + else if(maxdatalen > 65536) {
646 + printk(KERN_WARNING "layer7: maxdatalen can't be > 65536, using 65536\n");
647 + maxdatalen = 65536;
648 + }
649 + return ipt_register_match(&layer7_match);
650 +}
651 +
652 +static void __exit ipt_layer7_fini(void)
653 +{
654 + layer7_cleanup_proc();
655 + ipt_unregister_match(&layer7_match);
656 +}
657 +
658 +module_init(ipt_layer7_init);
659 +module_exit(ipt_layer7_fini);
660 diff -urN linux.old/net/ipv4/netfilter/Kconfig linux.dev/net/ipv4/netfilter/Kconfig
661 --- linux.old/net/ipv4/netfilter/Kconfig 2007-01-01 05:17:07.000000000 +0100
662 +++ linux.dev/net/ipv4/netfilter/Kconfig 2007-01-01 05:18:48.000000000 +0100
663 @@ -248,6 +248,24 @@
664
665 To compile it as a module, choose M here. If unsure, say N.
666
667 +config IP_NF_MATCH_LAYER7
668 + tristate "Layer 7 match support (EXPERIMENTAL)"
669 + depends on IP_NF_IPTABLES && IP_NF_CT_ACCT && IP_NF_CONNTRACK && EXPERIMENTAL
670 + help
671 + Say Y if you want to be able to classify connections (and their
672 + packets) based on regular expression matching of their application
673 + layer data. This is one way to classify applications such as
674 + peer-to-peer filesharing systems that do not always use the same
675 + port.
676 +
677 + To compile it as a module, choose M here. If unsure, say N.
678 +
679 +config IP_NF_MATCH_LAYER7_DEBUG
680 + bool "Layer 7 debugging output"
681 + depends on IP_NF_MATCH_LAYER7
682 + help
683 + Say Y to get lots of debugging output.
684 +
685 config IP_NF_MATCH_TOS
686 tristate "TOS match support"
687 depends on IP_NF_IPTABLES
688 diff -urN linux.old/net/ipv4/netfilter/Makefile linux.dev/net/ipv4/netfilter/Makefile
689 --- linux.old/net/ipv4/netfilter/Makefile 2007-01-01 05:17:07.000000000 +0100
690 +++ linux.dev/net/ipv4/netfilter/Makefile 2007-01-01 05:18:48.000000000 +0100
691 @@ -63,6 +63,8 @@
692 obj-$(CONFIG_IP_NF_MATCH_TTL) += ipt_ttl.o
693 obj-$(CONFIG_IP_NF_MATCH_ADDRTYPE) += ipt_addrtype.o
694
695 +obj-$(CONFIG_IP_NF_MATCH_LAYER7) += ipt_layer7.o
696 +
697 # targets
698 obj-$(CONFIG_IP_NF_TARGET_REJECT) += ipt_REJECT.o
699 obj-$(CONFIG_IP_NF_TARGET_TOS) += ipt_TOS.o
700 diff -urN linux.old/net/ipv4/netfilter/regexp/regexp.c linux.dev/net/ipv4/netfilter/regexp/regexp.c
701 --- linux.old/net/ipv4/netfilter/regexp/regexp.c 1970-01-01 01:00:00.000000000 +0100
702 +++ linux.dev/net/ipv4/netfilter/regexp/regexp.c 2007-01-01 05:18:48.000000000 +0100
703 @@ -0,0 +1,1197 @@
704 +/*
705 + * regcomp and regexec -- regsub and regerror are elsewhere
706 + * @(#)regexp.c 1.3 of 18 April 87
707 + *
708 + * Copyright (c) 1986 by University of Toronto.
709 + * Written by Henry Spencer. Not derived from licensed software.
710 + *
711 + * Permission is granted to anyone to use this software for any
712 + * purpose on any computer system, and to redistribute it freely,
713 + * subject to the following restrictions:
714 + *
715 + * 1. The author is not responsible for the consequences of use of
716 + * this software, no matter how awful, even if they arise
717 + * from defects in it.
718 + *
719 + * 2. The origin of this software must not be misrepresented, either
720 + * by explicit claim or by omission.
721 + *
722 + * 3. Altered versions must be plainly marked as such, and must not
723 + * be misrepresented as being the original software.
724 + *
725 + * Beware that some of this code is subtly aware of the way operator
726 + * precedence is structured in regular expressions. Serious changes in
727 + * regular-expression syntax might require a total rethink.
728 + *
729 + * This code was modified by Ethan Sommer to work within the kernel
730 + * (it now uses kmalloc etc..)
731 + *
732 + * Modified slightly by Matthew Strait to use more modern C.
733 + */
734 +
735 +#include "regexp.h"
736 +#include "regmagic.h"
737 +
738 +/* added by ethan and matt. Lets it work in both kernel and user space.
739 +(So iptables can use it, for instance.) Yea, it goes both ways... */
740 +#if __KERNEL__
741 + #define malloc(foo) kmalloc(foo,GFP_ATOMIC)
742 +#else
743 + #define printk(format,args...) printf(format,##args)
744 +#endif
745 +
746 +void regerror(char * s)
747 +{
748 + printk("<3>Regexp: %s\n", s);
749 + /* NOTREACHED */
750 +}
751 +
752 +/*
753 + * The "internal use only" fields in regexp.h are present to pass info from
754 + * compile to execute that permits the execute phase to run lots faster on
755 + * simple cases. They are:
756 + *
757 + * regstart char that must begin a match; '\0' if none obvious
758 + * reganch is the match anchored (at beginning-of-line only)?
759 + * regmust string (pointer into program) that match must include, or NULL
760 + * regmlen length of regmust string
761 + *
762 + * Regstart and reganch permit very fast decisions on suitable starting points
763 + * for a match, cutting down the work a lot. Regmust permits fast rejection
764 + * of lines that cannot possibly match. The regmust tests are costly enough
765 + * that regcomp() supplies a regmust only if the r.e. contains something
766 + * potentially expensive (at present, the only such thing detected is * or +
767 + * at the start of the r.e., which can involve a lot of backup). Regmlen is
768 + * supplied because the test in regexec() needs it and regcomp() is computing
769 + * it anyway.
770 + */
771 +
772 +/*
773 + * Structure for regexp "program". This is essentially a linear encoding
774 + * of a nondeterministic finite-state machine (aka syntax charts or
775 + * "railroad normal form" in parsing technology). Each node is an opcode
776 + * plus a "next" pointer, possibly plus an operand. "Next" pointers of
777 + * all nodes except BRANCH implement concatenation; a "next" pointer with
778 + * a BRANCH on both ends of it is connecting two alternatives. (Here we
779 + * have one of the subtle syntax dependencies: an individual BRANCH (as
780 + * opposed to a collection of them) is never concatenated with anything
781 + * because of operator precedence.) The operand of some types of node is
782 + * a literal string; for others, it is a node leading into a sub-FSM. In
783 + * particular, the operand of a BRANCH node is the first node of the branch.
784 + * (NB this is *not* a tree structure: the tail of the branch connects
785 + * to the thing following the set of BRANCHes.) The opcodes are:
786 + */
787 +
788 +/* definition number opnd? meaning */
789 +#define END 0 /* no End of program. */
790 +#define BOL 1 /* no Match "" at beginning of line. */
791 +#define EOL 2 /* no Match "" at end of line. */
792 +#define ANY 3 /* no Match any one character. */
793 +#define ANYOF 4 /* str Match any character in this string. */
794 +#define ANYBUT 5 /* str Match any character not in this string. */
795 +#define BRANCH 6 /* node Match this alternative, or the next... */
796 +#define BACK 7 /* no Match "", "next" ptr points backward. */
797 +#define EXACTLY 8 /* str Match this string. */
798 +#define NOTHING 9 /* no Match empty string. */
799 +#define STAR 10 /* node Match this (simple) thing 0 or more times. */
800 +#define PLUS 11 /* node Match this (simple) thing 1 or more times. */
801 +#define OPEN 20 /* no Mark this point in input as start of #n. */
802 + /* OPEN+1 is number 1, etc. */
803 +#define CLOSE 30 /* no Analogous to OPEN. */
804 +
805 +/*
806 + * Opcode notes:
807 + *
808 + * BRANCH The set of branches constituting a single choice are hooked
809 + * together with their "next" pointers, since precedence prevents
810 + * anything being concatenated to any individual branch. The
811 + * "next" pointer of the last BRANCH in a choice points to the
812 + * thing following the whole choice. This is also where the
813 + * final "next" pointer of each individual branch points; each
814 + * branch starts with the operand node of a BRANCH node.
815 + *
816 + * BACK Normal "next" pointers all implicitly point forward; BACK
817 + * exists to make loop structures possible.
818 + *
819 + * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
820 + * BRANCH structures using BACK. Simple cases (one character
821 + * per match) are implemented with STAR and PLUS for speed
822 + * and to minimize recursive plunges.
823 + *
824 + * OPEN,CLOSE ...are numbered at compile time.
825 + */
826 +
827 +/*
828 + * A node is one char of opcode followed by two chars of "next" pointer.
829 + * "Next" pointers are stored as two 8-bit pieces, high order first. The
830 + * value is a positive offset from the opcode of the node containing it.
831 + * An operand, if any, simply follows the node. (Note that much of the
832 + * code generation knows about this implicit relationship.)
833 + *
834 + * Using two bytes for the "next" pointer is vast overkill for most things,
835 + * but allows patterns to get big without disasters.
836 + */
837 +#define OP(p) (*(p))
838 +#define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
839 +#define OPERAND(p) ((p) + 3)
840 +
841 +/*
842 + * See regmagic.h for one further detail of program structure.
843 + */
844 +
845 +
846 +/*
847 + * Utility definitions.
848 + */
849 +#ifndef CHARBITS
850 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
851 +#else
852 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
853 +#endif
854 +
855 +#define FAIL(m) { regerror(m); return(NULL); }
856 +#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
857 +#define META "^$.[()|?+*\\"
858 +
859 +/*
860 + * Flags to be passed up and down.
861 + */
862 +#define HASWIDTH 01 /* Known never to match null string. */
863 +#define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
864 +#define SPSTART 04 /* Starts with * or +. */
865 +#define WORST 0 /* Worst case. */
866 +
867 +/*
868 + * Global work variables for regcomp().
869 + */
870 +struct match_globals {
871 +char *reginput; /* String-input pointer. */
872 +char *regbol; /* Beginning of input, for ^ check. */
873 +char **regstartp; /* Pointer to startp array. */
874 +char **regendp; /* Ditto for endp. */
875 +char *regparse; /* Input-scan pointer. */
876 +int regnpar; /* () count. */
877 +char regdummy;
878 +char *regcode; /* Code-emit pointer; &regdummy = don't. */
879 +long regsize; /* Code size. */
880 +};
881 +
882 +/*
883 + * Forward declarations for regcomp()'s friends.
884 + */
885 +#ifndef STATIC
886 +#define STATIC static
887 +#endif
888 +STATIC char *reg(struct match_globals *g, int paren,int *flagp);
889 +STATIC char *regbranch(struct match_globals *g, int *flagp);
890 +STATIC char *regpiece(struct match_globals *g, int *flagp);
891 +STATIC char *regatom(struct match_globals *g, int *flagp);
892 +STATIC char *regnode(struct match_globals *g, char op);
893 +STATIC char *regnext(struct match_globals *g, char *p);
894 +STATIC void regc(struct match_globals *g, char b);
895 +STATIC void reginsert(struct match_globals *g, char op, char *opnd);
896 +STATIC void regtail(struct match_globals *g, char *p, char *val);
897 +STATIC void regoptail(struct match_globals *g, char *p, char *val);
898 +
899 +
900 +__kernel_size_t my_strcspn(const char *s1,const char *s2)
901 +{
902 + char *scan1;
903 + char *scan2;
904 + int count;
905 +
906 + count = 0;
907 + for (scan1 = (char *)s1; *scan1 != '\0'; scan1++) {
908 + for (scan2 = (char *)s2; *scan2 != '\0';) /* ++ moved down. */
909 + if (*scan1 == *scan2++)
910 + return(count);
911 + count++;
912 + }
913 + return(count);
914 +}
915 +
916 +/*
917 + - regcomp - compile a regular expression into internal code
918 + *
919 + * We can't allocate space until we know how big the compiled form will be,
920 + * but we can't compile it (and thus know how big it is) until we've got a
921 + * place to put the code. So we cheat: we compile it twice, once with code
922 + * generation turned off and size counting turned on, and once "for real".
923 + * This also means that we don't allocate space until we are sure that the
924 + * thing really will compile successfully, and we never have to move the
925 + * code and thus invalidate pointers into it. (Note that it has to be in
926 + * one piece because free() must be able to free it all.)
927 + *
928 + * Beware that the optimization-preparation code in here knows about some
929 + * of the structure of the compiled regexp.
930 + */
931 +regexp *
932 +regcomp(char *exp,int *patternsize)
933 +{
934 + register regexp *r;
935 + register char *scan;
936 + register char *longest;
937 + register int len;
938 + int flags;
939 + struct match_globals g;
940 +
941 + /* commented out by ethan
942 + extern char *malloc();
943 + */
944 +
945 + if (exp == NULL)
946 + FAIL("NULL argument");
947 +
948 + /* First pass: determine size, legality. */
949 + g.regparse = exp;
950 + g.regnpar = 1;
951 + g.regsize = 0L;
952 + g.regcode = &g.regdummy;
953 + regc(&g, MAGIC);
954 + if (reg(&g, 0, &flags) == NULL)
955 + return(NULL);
956 +
957 + /* Small enough for pointer-storage convention? */
958 + if (g.regsize >= 32767L) /* Probably could be 65535L. */
959 + FAIL("regexp too big");
960 +
961 + /* Allocate space. */
962 + *patternsize=sizeof(regexp) + (unsigned)g.regsize;
963 + r = (regexp *)malloc(sizeof(regexp) + (unsigned)g.regsize);
964 + if (r == NULL)
965 + FAIL("out of space");
966 +
967 + /* Second pass: emit code. */
968 + g.regparse = exp;
969 + g.regnpar = 1;
970 + g.regcode = r->program;
971 + regc(&g, MAGIC);
972 + if (reg(&g, 0, &flags) == NULL)
973 + return(NULL);
974 +
975 + /* Dig out information for optimizations. */
976 + r->regstart = '\0'; /* Worst-case defaults. */
977 + r->reganch = 0;
978 + r->regmust = NULL;
979 + r->regmlen = 0;
980 + scan = r->program+1; /* First BRANCH. */
981 + if (OP(regnext(&g, scan)) == END) { /* Only one top-level choice. */
982 + scan = OPERAND(scan);
983 +
984 + /* Starting-point info. */
985 + if (OP(scan) == EXACTLY)
986 + r->regstart = *OPERAND(scan);
987 + else if (OP(scan) == BOL)
988 + r->reganch++;
989 +
990 + /*
991 + * If there's something expensive in the r.e., find the
992 + * longest literal string that must appear and make it the
993 + * regmust. Resolve ties in favor of later strings, since
994 + * the regstart check works with the beginning of the r.e.
995 + * and avoiding duplication strengthens checking. Not a
996 + * strong reason, but sufficient in the absence of others.
997 + */
998 + if (flags&SPSTART) {
999 + longest = NULL;
1000 + len = 0;
1001 + for (; scan != NULL; scan = regnext(&g, scan))
1002 + if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
1003 + longest = OPERAND(scan);
1004 + len = strlen(OPERAND(scan));
1005 + }
1006 + r->regmust = longest;
1007 + r->regmlen = len;
1008 + }
1009 + }
1010 +
1011 + return(r);
1012 +}
1013 +
1014 +/*
1015 + - reg - regular expression, i.e. main body or parenthesized thing
1016 + *
1017 + * Caller must absorb opening parenthesis.
1018 + *
1019 + * Combining parenthesis handling with the base level of regular expression
1020 + * is a trifle forced, but the need to tie the tails of the branches to what
1021 + * follows makes it hard to avoid.
1022 + */
1023 +static char *
1024 +reg(struct match_globals *g, int paren, int *flagp /* Parenthesized? */ )
1025 +{
1026 + register char *ret;
1027 + register char *br;
1028 + register char *ender;
1029 + register int parno = 0; /* 0 makes gcc happy */
1030 + int flags;
1031 +
1032 + *flagp = HASWIDTH; /* Tentatively. */
1033 +
1034 + /* Make an OPEN node, if parenthesized. */
1035 + if (paren) {
1036 + if (g->regnpar >= NSUBEXP)
1037 + FAIL("too many ()");
1038 + parno = g->regnpar;
1039 + g->regnpar++;
1040 + ret = regnode(g, OPEN+parno);
1041 + } else
1042 + ret = NULL;
1043 +
1044 + /* Pick up the branches, linking them together. */
1045 + br = regbranch(g, &flags);
1046 + if (br == NULL)
1047 + return(NULL);
1048 + if (ret != NULL)
1049 + regtail(g, ret, br); /* OPEN -> first. */
1050 + else
1051 + ret = br;
1052 + if (!(flags&HASWIDTH))
1053 + *flagp &= ~HASWIDTH;
1054 + *flagp |= flags&SPSTART;
1055 + while (*g->regparse == '|') {
1056 + g->regparse++;
1057 + br = regbranch(g, &flags);
1058 + if (br == NULL)
1059 + return(NULL);
1060 + regtail(g, ret, br); /* BRANCH -> BRANCH. */
1061 + if (!(flags&HASWIDTH))
1062 + *flagp &= ~HASWIDTH;
1063 + *flagp |= flags&SPSTART;
1064 + }
1065 +
1066 + /* Make a closing node, and hook it on the end. */
1067 + ender = regnode(g, (paren) ? CLOSE+parno : END);
1068 + regtail(g, ret, ender);
1069 +
1070 + /* Hook the tails of the branches to the closing node. */
1071 + for (br = ret; br != NULL; br = regnext(g, br))
1072 + regoptail(g, br, ender);
1073 +
1074 + /* Check for proper termination. */
1075 + if (paren && *g->regparse++ != ')') {
1076 + FAIL("unmatched ()");
1077 + } else if (!paren && *g->regparse != '\0') {
1078 + if (*g->regparse == ')') {
1079 + FAIL("unmatched ()");
1080 + } else
1081 + FAIL("junk on end"); /* "Can't happen". */
1082 + /* NOTREACHED */
1083 + }
1084 +
1085 + return(ret);
1086 +}
1087 +
1088 +/*
1089 + - regbranch - one alternative of an | operator
1090 + *
1091 + * Implements the concatenation operator.
1092 + */
1093 +static char *
1094 +regbranch(struct match_globals *g, int *flagp)
1095 +{
1096 + register char *ret;
1097 + register char *chain;
1098 + register char *latest;
1099 + int flags;
1100 +
1101 + *flagp = WORST; /* Tentatively. */
1102 +
1103 + ret = regnode(g, BRANCH);
1104 + chain = NULL;
1105 + while (*g->regparse != '\0' && *g->regparse != '|' && *g->regparse != ')') {
1106 + latest = regpiece(g, &flags);
1107 + if (latest == NULL)
1108 + return(NULL);
1109 + *flagp |= flags&HASWIDTH;
1110 + if (chain == NULL) /* First piece. */
1111 + *flagp |= flags&SPSTART;
1112 + else
1113 + regtail(g, chain, latest);
1114 + chain = latest;
1115 + }
1116 + if (chain == NULL) /* Loop ran zero times. */
1117 + (void) regnode(g, NOTHING);
1118 +
1119 + return(ret);
1120 +}
1121 +
1122 +/*
1123 + - regpiece - something followed by possible [*+?]
1124 + *
1125 + * Note that the branching code sequences used for ? and the general cases
1126 + * of * and + are somewhat optimized: they use the same NOTHING node as
1127 + * both the endmarker for their branch list and the body of the last branch.
1128 + * It might seem that this node could be dispensed with entirely, but the
1129 + * endmarker role is not redundant.
1130 + */
1131 +static char *
1132 +regpiece(struct match_globals *g, int *flagp)
1133 +{
1134 + register char *ret;
1135 + register char op;
1136 + register char *next;
1137 + int flags;
1138 +
1139 + ret = regatom(g, &flags);
1140 + if (ret == NULL)
1141 + return(NULL);
1142 +
1143 + op = *g->regparse;
1144 + if (!ISMULT(op)) {
1145 + *flagp = flags;
1146 + return(ret);
1147 + }
1148 +
1149 + if (!(flags&HASWIDTH) && op != '?')
1150 + FAIL("*+ operand could be empty");
1151 + *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
1152 +
1153 + if (op == '*' && (flags&SIMPLE))
1154 + reginsert(g, STAR, ret);
1155 + else if (op == '*') {
1156 + /* Emit x* as (x&|), where & means "self". */
1157 + reginsert(g, BRANCH, ret); /* Either x */
1158 + regoptail(g, ret, regnode(g, BACK)); /* and loop */
1159 + regoptail(g, ret, ret); /* back */
1160 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1161 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1162 + } else if (op == '+' && (flags&SIMPLE))
1163 + reginsert(g, PLUS, ret);
1164 + else if (op == '+') {
1165 + /* Emit x+ as x(&|), where & means "self". */
1166 + next = regnode(g, BRANCH); /* Either */
1167 + regtail(g, ret, next);
1168 + regtail(g, regnode(g, BACK), ret); /* loop back */
1169 + regtail(g, next, regnode(g, BRANCH)); /* or */
1170 + regtail(g, ret, regnode(g, NOTHING)); /* null. */
1171 + } else if (op == '?') {
1172 + /* Emit x? as (x|) */
1173 + reginsert(g, BRANCH, ret); /* Either x */
1174 + regtail(g, ret, regnode(g, BRANCH)); /* or */
1175 + next = regnode(g, NOTHING); /* null. */
1176 + regtail(g, ret, next);
1177 + regoptail(g, ret, next);
1178 + }
1179 + g->regparse++;
1180 + if (ISMULT(*g->regparse))
1181 + FAIL("nested *?+");
1182 +
1183 + return(ret);
1184 +}
1185 +
1186 +/*
1187 + - regatom - the lowest level
1188 + *
1189 + * Optimization: gobbles an entire sequence of ordinary characters so that
1190 + * it can turn them into a single node, which is smaller to store and
1191 + * faster to run. Backslashed characters are exceptions, each becoming a
1192 + * separate node; the code is simpler that way and it's not worth fixing.
1193 + */
1194 +static char *
1195 +regatom(struct match_globals *g, int *flagp)
1196 +{
1197 + register char *ret;
1198 + int flags;
1199 +
1200 + *flagp = WORST; /* Tentatively. */
1201 +
1202 + switch (*g->regparse++) {
1203 + case '^':
1204 + ret = regnode(g, BOL);
1205 + break;
1206 + case '$':
1207 + ret = regnode(g, EOL);
1208 + break;
1209 + case '.':
1210 + ret = regnode(g, ANY);
1211 + *flagp |= HASWIDTH|SIMPLE;
1212 + break;
1213 + case '[': {
1214 + register int class;
1215 + register int classend;
1216 +
1217 + if (*g->regparse == '^') { /* Complement of range. */
1218 + ret = regnode(g, ANYBUT);
1219 + g->regparse++;
1220 + } else
1221 + ret = regnode(g, ANYOF);
1222 + if (*g->regparse == ']' || *g->regparse == '-')
1223 + regc(g, *g->regparse++);
1224 + while (*g->regparse != '\0' && *g->regparse != ']') {
1225 + if (*g->regparse == '-') {
1226 + g->regparse++;
1227 + if (*g->regparse == ']' || *g->regparse == '\0')
1228 + regc(g, '-');
1229 + else {
1230 + class = UCHARAT(g->regparse-2)+1;
1231 + classend = UCHARAT(g->regparse);
1232 + if (class > classend+1)
1233 + FAIL("invalid [] range");
1234 + for (; class <= classend; class++)
1235 + regc(g, class);
1236 + g->regparse++;
1237 + }
1238 + } else
1239 + regc(g, *g->regparse++);
1240 + }
1241 + regc(g, '\0');
1242 + if (*g->regparse != ']')
1243 + FAIL("unmatched []");
1244 + g->regparse++;
1245 + *flagp |= HASWIDTH|SIMPLE;
1246 + }
1247 + break;
1248 + case '(':
1249 + ret = reg(g, 1, &flags);
1250 + if (ret == NULL)
1251 + return(NULL);
1252 + *flagp |= flags&(HASWIDTH|SPSTART);
1253 + break;
1254 + case '\0':
1255 + case '|':
1256 + case ')':
1257 + FAIL("internal urp"); /* Supposed to be caught earlier. */
1258 + break;
1259 + case '?':
1260 + case '+':
1261 + case '*':
1262 + FAIL("?+* follows nothing");
1263 + break;
1264 + case '\\':
1265 + if (*g->regparse == '\0')
1266 + FAIL("trailing \\");
1267 + ret = regnode(g, EXACTLY);
1268 + regc(g, *g->regparse++);
1269 + regc(g, '\0');
1270 + *flagp |= HASWIDTH|SIMPLE;
1271 + break;
1272 + default: {
1273 + register int len;
1274 + register char ender;
1275 +
1276 + g->regparse--;
1277 + len = my_strcspn((const char *)g->regparse, (const char *)META);
1278 + if (len <= 0)
1279 + FAIL("internal disaster");
1280 + ender = *(g->regparse+len);
1281 + if (len > 1 && ISMULT(ender))
1282 + len--; /* Back off clear of ?+* operand. */
1283 + *flagp |= HASWIDTH;
1284 + if (len == 1)
1285 + *flagp |= SIMPLE;
1286 + ret = regnode(g, EXACTLY);
1287 + while (len > 0) {
1288 + regc(g, *g->regparse++);
1289 + len--;
1290 + }
1291 + regc(g, '\0');
1292 + }
1293 + break;
1294 + }
1295 +
1296 + return(ret);
1297 +}
1298 +
1299 +/*
1300 + - regnode - emit a node
1301 + */
1302 +static char * /* Location. */
1303 +regnode(struct match_globals *g, char op)
1304 +{
1305 + register char *ret;
1306 + register char *ptr;
1307 +
1308 + ret = g->regcode;
1309 + if (ret == &g->regdummy) {
1310 + g->regsize += 3;
1311 + return(ret);
1312 + }
1313 +
1314 + ptr = ret;
1315 + *ptr++ = op;
1316 + *ptr++ = '\0'; /* Null "next" pointer. */
1317 + *ptr++ = '\0';
1318 + g->regcode = ptr;
1319 +
1320 + return(ret);
1321 +}
1322 +
1323 +/*
1324 + - regc - emit (if appropriate) a byte of code
1325 + */
1326 +static void
1327 +regc(struct match_globals *g, char b)
1328 +{
1329 + if (g->regcode != &g->regdummy)
1330 + *g->regcode++ = b;
1331 + else
1332 + g->regsize++;
1333 +}
1334 +
1335 +/*
1336 + - reginsert - insert an operator in front of already-emitted operand
1337 + *
1338 + * Means relocating the operand.
1339 + */
1340 +static void
1341 +reginsert(struct match_globals *g, char op, char* opnd)
1342 +{
1343 + register char *src;
1344 + register char *dst;
1345 + register char *place;
1346 +
1347 + if (g->regcode == &g->regdummy) {
1348 + g->regsize += 3;
1349 + return;
1350 + }
1351 +
1352 + src = g->regcode;
1353 + g->regcode += 3;
1354 + dst = g->regcode;
1355 + while (src > opnd)
1356 + *--dst = *--src;
1357 +
1358 + place = opnd; /* Op node, where operand used to be. */
1359 + *place++ = op;
1360 + *place++ = '\0';
1361 + *place++ = '\0';
1362 +}
1363 +
1364 +/*
1365 + - regtail - set the next-pointer at the end of a node chain
1366 + */
1367 +static void
1368 +regtail(struct match_globals *g, char *p, char *val)
1369 +{
1370 + register char *scan;
1371 + register char *temp;
1372 + register int offset;
1373 +
1374 + if (p == &g->regdummy)
1375 + return;
1376 +
1377 + /* Find last node. */
1378 + scan = p;
1379 + for (;;) {
1380 + temp = regnext(g, scan);
1381 + if (temp == NULL)
1382 + break;
1383 + scan = temp;
1384 + }
1385 +
1386 + if (OP(scan) == BACK)
1387 + offset = scan - val;
1388 + else
1389 + offset = val - scan;
1390 + *(scan+1) = (offset>>8)&0377;
1391 + *(scan+2) = offset&0377;
1392 +}
1393 +
1394 +/*
1395 + - regoptail - regtail on operand of first argument; nop if operandless
1396 + */
1397 +static void
1398 +regoptail(struct match_globals *g, char *p, char *val)
1399 +{
1400 + /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1401 + if (p == NULL || p == &g->regdummy || OP(p) != BRANCH)
1402 + return;
1403 + regtail(g, OPERAND(p), val);
1404 +}
1405 +
1406 +/*
1407 + * regexec and friends
1408 + */
1409 +
1410 +
1411 +/*
1412 + * Forwards.
1413 + */
1414 +STATIC int regtry(struct match_globals *g, regexp *prog, char *string);
1415 +STATIC int regmatch(struct match_globals *g, char *prog);
1416 +STATIC int regrepeat(struct match_globals *g, char *p);
1417 +
1418 +#ifdef DEBUG
1419 +int regnarrate = 0;
1420 +void regdump();
1421 +STATIC char *regprop(char *op);
1422 +#endif
1423 +
1424 +/*
1425 + - regexec - match a regexp against a string
1426 + */
1427 +int
1428 +regexec(regexp *prog, char *string)
1429 +{
1430 + register char *s;
1431 + struct match_globals g;
1432 +
1433 + /* Be paranoid... */
1434 + if (prog == NULL || string == NULL) {
1435 + printk("<3>Regexp: NULL parameter\n");
1436 + return(0);
1437 + }
1438 +
1439 + /* Check validity of program. */
1440 + if (UCHARAT(prog->program) != MAGIC) {
1441 + printk("<3>Regexp: corrupted program\n");
1442 + return(0);
1443 + }
1444 +
1445 + /* If there is a "must appear" string, look for it. */
1446 + if (prog->regmust != NULL) {
1447 + s = string;
1448 + while ((s = strchr(s, prog->regmust[0])) != NULL) {
1449 + if (strncmp(s, prog->regmust, prog->regmlen) == 0)
1450 + break; /* Found it. */
1451 + s++;
1452 + }
1453 + if (s == NULL) /* Not present. */
1454 + return(0);
1455 + }
1456 +
1457 + /* Mark beginning of line for ^ . */
1458 + g.regbol = string;
1459 +
1460 + /* Simplest case: anchored match need be tried only once. */
1461 + if (prog->reganch)
1462 + return(regtry(&g, prog, string));
1463 +
1464 + /* Messy cases: unanchored match. */
1465 + s = string;
1466 + if (prog->regstart != '\0')
1467 + /* We know what char it must start with. */
1468 + while ((s = strchr(s, prog->regstart)) != NULL) {
1469 + if (regtry(&g, prog, s))
1470 + return(1);
1471 + s++;
1472 + }
1473 + else
1474 + /* We don't -- general case. */
1475 + do {
1476 + if (regtry(&g, prog, s))
1477 + return(1);
1478 + } while (*s++ != '\0');
1479 +
1480 + /* Failure. */
1481 + return(0);
1482 +}
1483 +
1484 +/*
1485 + - regtry - try match at specific point
1486 + */
1487 +static int /* 0 failure, 1 success */
1488 +regtry(struct match_globals *g, regexp *prog, char *string)
1489 +{
1490 + register int i;
1491 + register char **sp;
1492 + register char **ep;
1493 +
1494 + g->reginput = string;
1495 + g->regstartp = prog->startp;
1496 + g->regendp = prog->endp;
1497 +
1498 + sp = prog->startp;
1499 + ep = prog->endp;
1500 + for (i = NSUBEXP; i > 0; i--) {
1501 + *sp++ = NULL;
1502 + *ep++ = NULL;
1503 + }
1504 + if (regmatch(g, prog->program + 1)) {
1505 + prog->startp[0] = string;
1506 + prog->endp[0] = g->reginput;
1507 + return(1);
1508 + } else
1509 + return(0);
1510 +}
1511 +
1512 +/*
1513 + - regmatch - main matching routine
1514 + *
1515 + * Conceptually the strategy is simple: check to see whether the current
1516 + * node matches, call self recursively to see whether the rest matches,
1517 + * and then act accordingly. In practice we make some effort to avoid
1518 + * recursion, in particular by going through "ordinary" nodes (that don't
1519 + * need to know whether the rest of the match failed) by a loop instead of
1520 + * by recursion.
1521 + */
1522 +static int /* 0 failure, 1 success */
1523 +regmatch(struct match_globals *g, char *prog)
1524 +{
1525 + register char *scan = prog; /* Current node. */
1526 + char *next; /* Next node. */
1527 +
1528 +#ifdef DEBUG
1529 + if (scan != NULL && regnarrate)
1530 + fprintf(stderr, "%s(\n", regprop(scan));
1531 +#endif
1532 + while (scan != NULL) {
1533 +#ifdef DEBUG
1534 + if (regnarrate)
1535 + fprintf(stderr, "%s...\n", regprop(scan));
1536 +#endif
1537 + next = regnext(g, scan);
1538 +
1539 + switch (OP(scan)) {
1540 + case BOL:
1541 + if (g->reginput != g->regbol)
1542 + return(0);
1543 + break;
1544 + case EOL:
1545 + if (*g->reginput != '\0')
1546 + return(0);
1547 + break;
1548 + case ANY:
1549 + if (*g->reginput == '\0')
1550 + return(0);
1551 + g->reginput++;
1552 + break;
1553 + case EXACTLY: {
1554 + register int len;
1555 + register char *opnd;
1556 +
1557 + opnd = OPERAND(scan);
1558 + /* Inline the first character, for speed. */
1559 + if (*opnd != *g->reginput)
1560 + return(0);
1561 + len = strlen(opnd);
1562 + if (len > 1 && strncmp(opnd, g->reginput, len) != 0)
1563 + return(0);
1564 + g->reginput += len;
1565 + }
1566 + break;
1567 + case ANYOF:
1568 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) == NULL)
1569 + return(0);
1570 + g->reginput++;
1571 + break;
1572 + case ANYBUT:
1573 + if (*g->reginput == '\0' || strchr(OPERAND(scan), *g->reginput) != NULL)
1574 + return(0);
1575 + g->reginput++;
1576 + break;
1577 + case NOTHING:
1578 + case BACK:
1579 + break;
1580 + case OPEN+1:
1581 + case OPEN+2:
1582 + case OPEN+3:
1583 + case OPEN+4:
1584 + case OPEN+5:
1585 + case OPEN+6:
1586 + case OPEN+7:
1587 + case OPEN+8:
1588 + case OPEN+9: {
1589 + register int no;
1590 + register char *save;
1591 +
1592 + no = OP(scan) - OPEN;
1593 + save = g->reginput;
1594 +
1595 + if (regmatch(g, next)) {
1596 + /*
1597 + * Don't set startp if some later
1598 + * invocation of the same parentheses
1599 + * already has.
1600 + */
1601 + if (g->regstartp[no] == NULL)
1602 + g->regstartp[no] = save;
1603 + return(1);
1604 + } else
1605 + return(0);
1606 + }
1607 + break;
1608 + case CLOSE+1:
1609 + case CLOSE+2:
1610 + case CLOSE+3:
1611 + case CLOSE+4:
1612 + case CLOSE+5:
1613 + case CLOSE+6:
1614 + case CLOSE+7:
1615 + case CLOSE+8:
1616 + case CLOSE+9:
1617 + {
1618 + register int no;
1619 + register char *save;
1620 +
1621 + no = OP(scan) - CLOSE;
1622 + save = g->reginput;
1623 +
1624 + if (regmatch(g, next)) {
1625 + /*
1626 + * Don't set endp if some later
1627 + * invocation of the same parentheses
1628 + * already has.
1629 + */
1630 + if (g->regendp[no] == NULL)
1631 + g->regendp[no] = save;
1632 + return(1);
1633 + } else
1634 + return(0);
1635 + }
1636 + break;
1637 + case BRANCH: {
1638 + register char *save;
1639 +
1640 + if (OP(next) != BRANCH) /* No choice. */
1641 + next = OPERAND(scan); /* Avoid recursion. */
1642 + else {
1643 + do {
1644 + save = g->reginput;
1645 + if (regmatch(g, OPERAND(scan)))
1646 + return(1);
1647 + g->reginput = save;
1648 + scan = regnext(g, scan);
1649 + } while (scan != NULL && OP(scan) == BRANCH);
1650 + return(0);
1651 + /* NOTREACHED */
1652 + }
1653 + }
1654 + break;
1655 + case STAR:
1656 + case PLUS: {
1657 + register char nextch;
1658 + register int no;
1659 + register char *save;
1660 + register int min;
1661 +
1662 + /*
1663 + * Lookahead to avoid useless match attempts
1664 + * when we know what character comes next.
1665 + */
1666 + nextch = '\0';
1667 + if (OP(next) == EXACTLY)
1668 + nextch = *OPERAND(next);
1669 + min = (OP(scan) == STAR) ? 0 : 1;
1670 + save = g->reginput;
1671 + no = regrepeat(g, OPERAND(scan));
1672 + while (no >= min) {
1673 + /* If it could work, try it. */
1674 + if (nextch == '\0' || *g->reginput == nextch)
1675 + if (regmatch(g, next))
1676 + return(1);
1677 + /* Couldn't or didn't -- back up. */
1678 + no--;
1679 + g->reginput = save + no;
1680 + }
1681 + return(0);
1682 + }
1683 + break;
1684 + case END:
1685 + return(1); /* Success! */
1686 + break;
1687 + default:
1688 + printk("<3>Regexp: memory corruption\n");
1689 + return(0);
1690 + break;
1691 + }
1692 +
1693 + scan = next;
1694 + }
1695 +
1696 + /*
1697 + * We get here only if there's trouble -- normally "case END" is
1698 + * the terminating point.
1699 + */
1700 + printk("<3>Regexp: corrupted pointers\n");
1701 + return(0);
1702 +}
1703 +
1704 +/*
1705 + - regrepeat - repeatedly match something simple, report how many
1706 + */
1707 +static int
1708 +regrepeat(struct match_globals *g, char *p)
1709 +{
1710 + register int count = 0;
1711 + register char *scan;
1712 + register char *opnd;
1713 +
1714 + scan = g->reginput;
1715 + opnd = OPERAND(p);
1716 + switch (OP(p)) {
1717 + case ANY:
1718 + count = strlen(scan);
1719 + scan += count;
1720 + break;
1721 + case EXACTLY:
1722 + while (*opnd == *scan) {
1723 + count++;
1724 + scan++;
1725 + }
1726 + break;
1727 + case ANYOF:
1728 + while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1729 + count++;
1730 + scan++;
1731 + }
1732 + break;
1733 + case ANYBUT:
1734 + while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1735 + count++;
1736 + scan++;
1737 + }
1738 + break;
1739 + default: /* Oh dear. Called inappropriately. */
1740 + printk("<3>Regexp: internal foulup\n");
1741 + count = 0; /* Best compromise. */
1742 + break;
1743 + }
1744 + g->reginput = scan;
1745 +
1746 + return(count);
1747 +}
1748 +
1749 +/*
1750 + - regnext - dig the "next" pointer out of a node
1751 + */
1752 +static char*
1753 +regnext(struct match_globals *g, char *p)
1754 +{
1755 + register int offset;
1756 +
1757 + if (p == &g->regdummy)
1758 + return(NULL);
1759 +
1760 + offset = NEXT(p);
1761 + if (offset == 0)
1762 + return(NULL);
1763 +
1764 + if (OP(p) == BACK)
1765 + return(p-offset);
1766 + else
1767 + return(p+offset);
1768 +}
1769 +
1770 +#ifdef DEBUG
1771 +
1772 +STATIC char *regprop();
1773 +
1774 +/*
1775 + - regdump - dump a regexp onto stdout in vaguely comprehensible form
1776 + */
1777 +void
1778 +regdump(regexp *r)
1779 +{
1780 + register char *s;
1781 + register char op = EXACTLY; /* Arbitrary non-END op. */
1782 + register char *next;
1783 + /* extern char *strchr(); */
1784 +
1785 +
1786 + s = r->program + 1;
1787 + while (op != END) { /* While that wasn't END last time... */
1788 + op = OP(s);
1789 + printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1790 + next = regnext(s);
1791 + if (next == NULL) /* Next ptr. */
1792 + printf("(0)");
1793 + else
1794 + printf("(%d)", (s-r->program)+(next-s));
1795 + s += 3;
1796 + if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1797 + /* Literal string, where present. */
1798 + while (*s != '\0') {
1799 + putchar(*s);
1800 + s++;
1801 + }
1802 + s++;
1803 + }
1804 + putchar('\n');
1805 + }
1806 +
1807 + /* Header fields of interest. */
1808 + if (r->regstart != '\0')
1809 + printf("start `%c' ", r->regstart);
1810 + if (r->reganch)
1811 + printf("anchored ");
1812 + if (r->regmust != NULL)
1813 + printf("must have \"%s\"", r->regmust);
1814 + printf("\n");
1815 +}
1816 +
1817 +/*
1818 + - regprop - printable representation of opcode
1819 + */
1820 +static char *
1821 +regprop(char *op)
1822 +{
1823 +#define BUFLEN 50
1824 + register char *p;
1825 + static char buf[BUFLEN];
1826 +
1827 + strcpy(buf, ":");
1828 +
1829 + switch (OP(op)) {
1830 + case BOL:
1831 + p = "BOL";
1832 + break;
1833 + case EOL:
1834 + p = "EOL";
1835 + break;
1836 + case ANY:
1837 + p = "ANY";
1838 + break;
1839 + case ANYOF:
1840 + p = "ANYOF";
1841 + break;
1842 + case ANYBUT:
1843 + p = "ANYBUT";
1844 + break;
1845 + case BRANCH:
1846 + p = "BRANCH";
1847 + break;
1848 + case EXACTLY:
1849 + p = "EXACTLY";
1850 + break;
1851 + case NOTHING:
1852 + p = "NOTHING";
1853 + break;
1854 + case BACK:
1855 + p = "BACK";
1856 + break;
1857 + case END:
1858 + p = "END";
1859 + break;
1860 + case OPEN+1:
1861 + case OPEN+2:
1862 + case OPEN+3:
1863 + case OPEN+4:
1864 + case OPEN+5:
1865 + case OPEN+6:
1866 + case OPEN+7:
1867 + case OPEN+8:
1868 + case OPEN+9:
1869 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "OPEN%d", OP(op)-OPEN);
1870 + p = NULL;
1871 + break;
1872 + case CLOSE+1:
1873 + case CLOSE+2:
1874 + case CLOSE+3:
1875 + case CLOSE+4:
1876 + case CLOSE+5:
1877 + case CLOSE+6:
1878 + case CLOSE+7:
1879 + case CLOSE+8:
1880 + case CLOSE+9:
1881 + snprintf(buf+strlen(buf),BUFLEN-strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1882 + p = NULL;
1883 + break;
1884 + case STAR:
1885 + p = "STAR";
1886 + break;
1887 + case PLUS:
1888 + p = "PLUS";
1889 + break;
1890 + default:
1891 + printk("<3>Regexp: corrupted opcode\n");
1892 + break;
1893 + }
1894 + if (p != NULL)
1895 + strncat(buf, p, BUFLEN-strlen(buf));
1896 + return(buf);
1897 +}
1898 +#endif
1899 +
1900 +
1901 diff -urN linux.old/net/ipv4/netfilter/regexp/regexp.h linux.dev/net/ipv4/netfilter/regexp/regexp.h
1902 --- linux.old/net/ipv4/netfilter/regexp/regexp.h 1970-01-01 01:00:00.000000000 +0100
1903 +++ linux.dev/net/ipv4/netfilter/regexp/regexp.h 2007-01-01 05:18:48.000000000 +0100
1904 @@ -0,0 +1,41 @@
1905 +/*
1906 + * Definitions etc. for regexp(3) routines.
1907 + *
1908 + * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
1909 + * not the System V one.
1910 + */
1911 +
1912 +#ifndef REGEXP_H
1913 +#define REGEXP_H
1914 +
1915 +
1916 +/*
1917 +http://www.opensource.apple.com/darwinsource/10.3/expect-1/expect/expect.h ,
1918 +which contains a version of this library, says:
1919 +
1920 + *
1921 + * NSUBEXP must be at least 10, and no greater than 117 or the parser
1922 + * will not work properly.
1923 + *
1924 +
1925 +However, it looks rather like this library is limited to 10. If you think
1926 +otherwise, let us know.
1927 +*/
1928 +
1929 +#define NSUBEXP 10
1930 +typedef struct regexp {
1931 + char *startp[NSUBEXP];
1932 + char *endp[NSUBEXP];
1933 + char regstart; /* Internal use only. */
1934 + char reganch; /* Internal use only. */
1935 + char *regmust; /* Internal use only. */
1936 + int regmlen; /* Internal use only. */
1937 + char program[1]; /* Unwarranted chumminess with compiler. */
1938 +} regexp;
1939 +
1940 +regexp * regcomp(char *exp, int *patternsize);
1941 +int regexec(regexp *prog, char *string);
1942 +void regsub(regexp *prog, char *source, char *dest);
1943 +void regerror(char *s);
1944 +
1945 +#endif
1946 diff -urN linux.old/net/ipv4/netfilter/regexp/regmagic.h linux.dev/net/ipv4/netfilter/regexp/regmagic.h
1947 --- linux.old/net/ipv4/netfilter/regexp/regmagic.h 1970-01-01 01:00:00.000000000 +0100
1948 +++ linux.dev/net/ipv4/netfilter/regexp/regmagic.h 2007-01-01 05:18:48.000000000 +0100
1949 @@ -0,0 +1,5 @@
1950 +/*
1951 + * The first byte of the regexp internal "program" is actually this magic
1952 + * number; the start node begins in the second byte.
1953 + */
1954 +#define MAGIC 0234
1955 diff -urN linux.old/net/ipv4/netfilter/regexp/regsub.c linux.dev/net/ipv4/netfilter/regexp/regsub.c
1956 --- linux.old/net/ipv4/netfilter/regexp/regsub.c 1970-01-01 01:00:00.000000000 +0100
1957 +++ linux.dev/net/ipv4/netfilter/regexp/regsub.c 2007-01-01 05:18:48.000000000 +0100
1958 @@ -0,0 +1,95 @@
1959 +/*
1960 + * regsub
1961 + * @(#)regsub.c 1.3 of 2 April 86
1962 + *
1963 + * Copyright (c) 1986 by University of Toronto.
1964 + * Written by Henry Spencer. Not derived from licensed software.
1965 + *
1966 + * Permission is granted to anyone to use this software for any
1967 + * purpose on any computer system, and to redistribute it freely,
1968 + * subject to the following restrictions:
1969 + *
1970 + * 1. The author is not responsible for the consequences of use of
1971 + * this software, no matter how awful, even if they arise
1972 + * from defects in it.
1973 + *
1974 + * 2. The origin of this software must not be misrepresented, either
1975 + * by explicit claim or by omission.
1976 + *
1977 + * 3. Altered versions must be plainly marked as such, and must not
1978 + * be misrepresented as being the original software.
1979 + *
1980 + *
1981 + * This code was modified by Ethan Sommer to work within the kernel
1982 + * (it now uses kmalloc etc..)
1983 + *
1984 + */
1985 +#include "regexp.h"
1986 +#include "regmagic.h"
1987 +#include <linux/string.h>
1988 +
1989 +
1990 +#ifndef CHARBITS
1991 +#define UCHARAT(p) ((int)*(unsigned char *)(p))
1992 +#else
1993 +#define UCHARAT(p) ((int)*(p)&CHARBITS)
1994 +#endif
1995 +
1996 +#if 0
1997 +//void regerror(char * s)
1998 +//{
1999 +// printk("regexp(3): %s", s);
2000 +// /* NOTREACHED */
2001 +//}
2002 +#endif
2003 +
2004 +/*
2005 + - regsub - perform substitutions after a regexp match
2006 + */
2007 +void
2008 +regsub(regexp * prog, char * source, char * dest)
2009 +{
2010 + register char *src;
2011 + register char *dst;
2012 + register char c;
2013 + register int no;
2014 + register int len;
2015 +
2016 + /* Not necessary and gcc doesn't like it -MLS */
2017 + /*extern char *strncpy();*/
2018 +
2019 + if (prog == NULL || source == NULL || dest == NULL) {
2020 + regerror("NULL parm to regsub");
2021 + return;
2022 + }
2023 + if (UCHARAT(prog->program) != MAGIC) {
2024 + regerror("damaged regexp fed to regsub");
2025 + return;
2026 + }
2027 +
2028 + src = source;
2029 + dst = dest;
2030 + while ((c = *src++) != '\0') {
2031 + if (c == '&')
2032 + no = 0;
2033 + else if (c == '\\' && '0' <= *src && *src <= '9')
2034 + no = *src++ - '0';
2035 + else
2036 + no = -1;
2037 +
2038 + if (no < 0) { /* Ordinary character. */
2039 + if (c == '\\' && (*src == '\\' || *src == '&'))
2040 + c = *src++;
2041 + *dst++ = c;
2042 + } else if (prog->startp[no] != NULL && prog->endp[no] != NULL) {
2043 + len = prog->endp[no] - prog->startp[no];
2044 + (void) strncpy(dst, prog->startp[no], len);
2045 + dst += len;
2046 + if (len != 0 && *(dst-1) == '\0') { /* strncpy hit NUL. */
2047 + regerror("damaged match string");
2048 + return;
2049 + }
2050 + }
2051 + }
2052 + *dst++ = '\0';
2053 +}