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