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