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