ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 = 0;
45         
46         ct = ip_conntrack_get((struct sk_buff *)skb, &ctinfo);
47         if (!ct) {
48                 DEBUGP("ipt_helper: Eek! invalid conntrack?\n");
49                 return 0;
50         }
51
52         if (!ct->master) {
53                 DEBUGP("ipt_helper: conntrack %p has no master\n", ct);
54                 return 0;
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         ret = !strncmp(exp->expectant->helper->name, info->name, 
75                        strlen(exp->expectant->helper->name)) ^ info->invert;
76 out_unlock:
77         READ_UNLOCK(&ip_conntrack_lock);
78         return ret;
79 }
80
81 static int check(const char *tablename,
82                  const struct ipt_ip *ip,
83                  void *matchinfo,
84                  unsigned int matchsize,
85                  unsigned int hook_mask)
86 {
87         struct ipt_helper_info *info = matchinfo;
88
89         info->name[29] = '\0';
90
91         /* verify size */
92         if (matchsize != IPT_ALIGN(sizeof(struct ipt_helper_info)))
93                 return 0;
94
95         /* verify that we actually should match anything */
96         if ( strlen(info->name) == 0 )
97                 return 0;
98         
99         return 1;
100 }
101
102 static struct ipt_match helper_match = {
103         .name           = "helper",
104         .match          = &match,
105         .checkentry     = &check,
106         .me             = THIS_MODULE,
107 };
108
109 static int __init init(void)
110 {
111         need_ip_conntrack();
112         return ipt_register_match(&helper_match);
113 }
114
115 static void __exit fini(void)
116 {
117         ipt_unregister_match(&helper_match);
118 }
119
120 module_init(init);
121 module_exit(fini);
122