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