VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / net / ipv4 / netfilter / ipt_helper.c
1 /* iptables module to match on related connections */
2 /*
3  * (C) 2001 Martin Josefsson <gandalf@wlug.westbo.se>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  *   19 Mar 2002 Harald Welte <laforge@gnumonks.org>:
10  *               - Port to newnat infrastructure
11  */
12
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/netfilter.h>
16 #include <linux/netfilter_ipv4/ip_conntrack.h>
17 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
18 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
19 #include <linux/netfilter_ipv4/ip_tables.h>
20 #include <linux/netfilter_ipv4/ipt_helper.h>
21
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>");
24 MODULE_DESCRIPTION("iptables helper match module");
25
26 #if 0
27 #define DEBUGP printk
28 #else
29 #define DEBUGP(format, args...)
30 #endif
31
32 static int
33 match(const struct sk_buff *skb,
34       const struct net_device *in,
35       const struct net_device *out,
36       const void *matchinfo,
37       int offset,
38       int *hotdrop)
39 {
40         const struct ipt_helper_info *info = matchinfo;
41         struct ip_conntrack_expect *exp;
42         struct ip_conntrack *ct;
43         enum ip_conntrack_info ctinfo;
44         int ret = info->invert;
45         
46         ct = ip_conntrack_get((struct sk_buff *)skb, &ctinfo);
47         if (!ct) {
48                 DEBUGP("ipt_helper: Eek! invalid conntrack?\n");
49                 return ret;
50         }
51
52         if (!ct->master) {
53                 DEBUGP("ipt_helper: conntrack %p has no master\n", ct);
54                 return ret;
55         }
56
57         exp = ct->master;
58         READ_LOCK(&ip_conntrack_lock);
59         if (!exp->expectant) {
60                 DEBUGP("ipt_helper: expectation %p without expectant !?!\n", 
61                         exp);
62                 goto out_unlock;
63         }
64
65         if (!exp->expectant->helper) {
66                 DEBUGP("ipt_helper: master ct %p has no helper\n", 
67                         exp->expectant);
68                 goto out_unlock;
69         }
70
71         DEBUGP("master's name = %s , info->name = %s\n", 
72                 exp->expectant->helper->name, info->name);
73
74         if (info->name[0] == '\0')
75                 ret ^= 1;
76         else
77                 ret ^= !strncmp(exp->expectant->helper->name, info->name, 
78                                 strlen(exp->expectant->helper->name));
79 out_unlock:
80         READ_UNLOCK(&ip_conntrack_lock);
81         return ret;
82 }
83
84 static int check(const char *tablename,
85                  const struct ipt_ip *ip,
86                  void *matchinfo,
87                  unsigned int matchsize,
88                  unsigned int hook_mask)
89 {
90         struct ipt_helper_info *info = matchinfo;
91
92         info->name[29] = '\0';
93
94         /* verify size */
95         if (matchsize != IPT_ALIGN(sizeof(struct ipt_helper_info)))
96                 return 0;
97
98         return 1;
99 }
100
101 static struct ipt_match helper_match = {
102         .name           = "helper",
103         .match          = &match,
104         .checkentry     = &check,
105         .me             = THIS_MODULE,
106 };
107
108 static int __init init(void)
109 {
110         return ipt_register_match(&helper_match);
111 }
112
113 static void __exit fini(void)
114 {
115         ipt_unregister_match(&helper_match);
116 }
117
118 module_init(init);
119 module_exit(fini);
120