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