]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/netfilter/xt_helper.c
[NETFILTER]: Fix whitespace errors
[net-next-2.6.git] / net / netfilter / xt_helper.c
CommitLineData
1da177e4
LT
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>
9fb9cbb1 16#if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
1da177e4
LT
17#include <linux/netfilter_ipv4/ip_conntrack.h>
18#include <linux/netfilter_ipv4/ip_conntrack_core.h>
19#include <linux/netfilter_ipv4/ip_conntrack_helper.h>
9fb9cbb1
YK
20#else
21#include <net/netfilter/nf_conntrack.h>
22#include <net/netfilter/nf_conntrack_core.h>
23#include <net/netfilter/nf_conntrack_helper.h>
24#endif
2e4e6a17
HW
25#include <linux/netfilter/x_tables.h>
26#include <linux/netfilter/xt_helper.h>
fe0b9294 27#include <net/netfilter/nf_conntrack_compat.h>
1da177e4
LT
28
29MODULE_LICENSE("GPL");
30MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>");
31MODULE_DESCRIPTION("iptables helper match module");
2e4e6a17
HW
32MODULE_ALIAS("ipt_helper");
33MODULE_ALIAS("ip6t_helper");
1da177e4
LT
34
35#if 0
36#define DEBUGP printk
37#else
38#define DEBUGP(format, args...)
39#endif
40
9fb9cbb1 41#if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
1da177e4
LT
42static int
43match(const struct sk_buff *skb,
44 const struct net_device *in,
45 const struct net_device *out,
c4986734 46 const struct xt_match *match,
1da177e4
LT
47 const void *matchinfo,
48 int offset,
2e4e6a17 49 unsigned int protoff,
1da177e4
LT
50 int *hotdrop)
51{
2e4e6a17 52 const struct xt_helper_info *info = matchinfo;
1da177e4
LT
53 struct ip_conntrack *ct;
54 enum ip_conntrack_info ctinfo;
55 int ret = info->invert;
601e68e1 56
1da177e4
LT
57 ct = ip_conntrack_get((struct sk_buff *)skb, &ctinfo);
58 if (!ct) {
2e4e6a17 59 DEBUGP("xt_helper: Eek! invalid conntrack?\n");
1da177e4
LT
60 return ret;
61 }
62
63 if (!ct->master) {
2e4e6a17 64 DEBUGP("xt_helper: conntrack %p has no master\n", ct);
1da177e4
LT
65 return ret;
66 }
67
e45b1be8 68 read_lock_bh(&ip_conntrack_lock);
1da177e4 69 if (!ct->master->helper) {
601e68e1 70 DEBUGP("xt_helper: master ct %p has no helper\n",
1da177e4
LT
71 exp->expectant);
72 goto out_unlock;
73 }
74
601e68e1 75 DEBUGP("master's name = %s , info->name = %s\n",
1da177e4
LT
76 ct->master->helper->name, info->name);
77
78 if (info->name[0] == '\0')
79 ret ^= 1;
80 else
601e68e1
YH
81 ret ^= !strncmp(ct->master->helper->name, info->name,
82 strlen(ct->master->helper->name));
1da177e4 83out_unlock:
e45b1be8 84 read_unlock_bh(&ip_conntrack_lock);
1da177e4
LT
85 return ret;
86}
87
9fb9cbb1
YK
88#else /* CONFIG_IP_NF_CONNTRACK */
89
90static int
91match(const struct sk_buff *skb,
92 const struct net_device *in,
93 const struct net_device *out,
c4986734 94 const struct xt_match *match,
9fb9cbb1
YK
95 const void *matchinfo,
96 int offset,
2e4e6a17 97 unsigned int protoff,
9fb9cbb1
YK
98 int *hotdrop)
99{
2e4e6a17 100 const struct xt_helper_info *info = matchinfo;
9fb9cbb1 101 struct nf_conn *ct;
dc808fe2 102 struct nf_conn_help *master_help;
9fb9cbb1
YK
103 enum ip_conntrack_info ctinfo;
104 int ret = info->invert;
601e68e1 105
9fb9cbb1
YK
106 ct = nf_ct_get((struct sk_buff *)skb, &ctinfo);
107 if (!ct) {
2e4e6a17 108 DEBUGP("xt_helper: Eek! invalid conntrack?\n");
9fb9cbb1
YK
109 return ret;
110 }
111
112 if (!ct->master) {
2e4e6a17 113 DEBUGP("xt_helper: conntrack %p has no master\n", ct);
9fb9cbb1
YK
114 return ret;
115 }
116
117 read_lock_bh(&nf_conntrack_lock);
dc808fe2
HW
118 master_help = nfct_help(ct->master);
119 if (!master_help || !master_help->helper) {
601e68e1 120 DEBUGP("xt_helper: master ct %p has no helper\n",
9fb9cbb1
YK
121 exp->expectant);
122 goto out_unlock;
123 }
124
601e68e1 125 DEBUGP("master's name = %s , info->name = %s\n",
9fb9cbb1
YK
126 ct->master->helper->name, info->name);
127
128 if (info->name[0] == '\0')
129 ret ^= 1;
130 else
dc808fe2 131 ret ^= !strncmp(master_help->helper->name, info->name,
601e68e1 132 strlen(master_help->helper->name));
9fb9cbb1
YK
133out_unlock:
134 read_unlock_bh(&nf_conntrack_lock);
135 return ret;
136}
137#endif
138
1da177e4 139static int check(const char *tablename,
2e4e6a17 140 const void *inf,
c4986734 141 const struct xt_match *match,
1da177e4 142 void *matchinfo,
1da177e4
LT
143 unsigned int hook_mask)
144{
2e4e6a17 145 struct xt_helper_info *info = matchinfo;
1da177e4 146
b9f78f9f 147 if (nf_ct_l3proto_try_module_get(match->family) < 0) {
fe0b9294 148 printk(KERN_WARNING "can't load conntrack support for "
b9f78f9f
PNA
149 "proto=%d\n", match->family);
150 return 0;
151 }
1da177e4 152 info->name[29] = '\0';
1da177e4
LT
153 return 1;
154}
155
b9f78f9f 156static void
efa74165 157destroy(const struct xt_match *match, void *matchinfo)
b9f78f9f 158{
b9f78f9f 159 nf_ct_l3proto_module_put(match->family);
b9f78f9f
PNA
160}
161
4470bbc7
PM
162static struct xt_match xt_helper_match[] = {
163 {
164 .name = "helper",
165 .family = AF_INET,
166 .checkentry = check,
167 .match = match,
168 .destroy = destroy,
169 .matchsize = sizeof(struct xt_helper_info),
170 .me = THIS_MODULE,
171 },
172 {
173 .name = "helper",
174 .family = AF_INET6,
175 .checkentry = check,
176 .match = match,
177 .destroy = destroy,
178 .matchsize = sizeof(struct xt_helper_info),
179 .me = THIS_MODULE,
180 },
1da177e4
LT
181};
182
65b4b4e8 183static int __init xt_helper_init(void)
1da177e4 184{
4470bbc7
PM
185 return xt_register_matches(xt_helper_match,
186 ARRAY_SIZE(xt_helper_match));
1da177e4
LT
187}
188
65b4b4e8 189static void __exit xt_helper_fini(void)
1da177e4 190{
4470bbc7 191 xt_unregister_matches(xt_helper_match, ARRAY_SIZE(xt_helper_match));
1da177e4
LT
192}
193
65b4b4e8
AM
194module_init(xt_helper_init);
195module_exit(xt_helper_fini);
1da177e4 196