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