blob: 799c2a43e3b900285e4b52270e862f6428d86cd9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080016#if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080020#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
Harald Welte2e4e6a12006-01-12 13:30:04 -080025#include <linux/netfilter/x_tables.h>
26#include <linux/netfilter/xt_helper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28MODULE_LICENSE("GPL");
29MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>");
30MODULE_DESCRIPTION("iptables helper match module");
Harald Welte2e4e6a12006-01-12 13:30:04 -080031MODULE_ALIAS("ipt_helper");
32MODULE_ALIAS("ip6t_helper");
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#if 0
35#define DEBUGP printk
36#else
37#define DEBUGP(format, args...)
38#endif
39
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080040#if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static int
42match(const struct sk_buff *skb,
43 const struct net_device *in,
44 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080045 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 const void *matchinfo,
47 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080048 unsigned int protoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 int *hotdrop)
50{
Harald Welte2e4e6a12006-01-12 13:30:04 -080051 const struct xt_helper_info *info = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 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) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080058 DEBUGP("xt_helper: Eek! invalid conntrack?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return ret;
60 }
61
62 if (!ct->master) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080063 DEBUGP("xt_helper: conntrack %p has no master\n", ct);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return ret;
65 }
66
Patrick McHardye45b1be2005-06-21 14:01:30 -070067 read_lock_bh(&ip_conntrack_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (!ct->master->helper) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080069 DEBUGP("xt_helper: master ct %p has no helper\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 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:
Patrick McHardye45b1be2005-06-21 14:01:30 -070083 read_unlock_bh(&ip_conntrack_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return ret;
85}
86
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080087#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,
Patrick McHardyc4986732006-03-20 18:02:56 -080093 const struct xt_match *match,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080094 const void *matchinfo,
95 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080096 unsigned int protoff,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080097 int *hotdrop)
98{
Harald Welte2e4e6a12006-01-12 13:30:04 -080099 const struct xt_helper_info *info = matchinfo;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800100 struct nf_conn *ct;
Harald Weltedc808fe2006-03-20 17:56:32 -0800101 struct nf_conn_help *master_help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800102 enum ip_conntrack_info ctinfo;
103 int ret = info->invert;
104
105 ct = nf_ct_get((struct sk_buff *)skb, &ctinfo);
106 if (!ct) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800107 DEBUGP("xt_helper: Eek! invalid conntrack?\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800108 return ret;
109 }
110
111 if (!ct->master) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800112 DEBUGP("xt_helper: conntrack %p has no master\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800113 return ret;
114 }
115
116 read_lock_bh(&nf_conntrack_lock);
Harald Weltedc808fe2006-03-20 17:56:32 -0800117 master_help = nfct_help(ct->master);
118 if (!master_help || !master_help->helper) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800119 DEBUGP("xt_helper: master ct %p has no helper\n",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800120 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
Harald Weltedc808fe2006-03-20 17:56:32 -0800130 ret ^= !strncmp(master_help->helper->name, info->name,
131 strlen(master_help->helper->name));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800132out_unlock:
133 read_unlock_bh(&nf_conntrack_lock);
134 return ret;
135}
136#endif
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static int check(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800139 const void *inf,
Patrick McHardyc4986732006-03-20 18:02:56 -0800140 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 void *matchinfo,
142 unsigned int matchsize,
143 unsigned int hook_mask)
144{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800145 struct xt_helper_info *info = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800147#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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 info->name[29] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return 1;
156}
157
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800158static 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
Harald Welte2e4e6a12006-01-12 13:30:04 -0800166static struct xt_match helper_match = {
167 .name = "helper",
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800168 .match = match,
169 .matchsize = sizeof(struct xt_helper_info),
170 .checkentry = check,
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800171 .destroy = destroy,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800172 .family = AF_INET,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800173 .me = THIS_MODULE,
174};
175static struct xt_match helper6_match = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 .name = "helper",
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800177 .match = match,
178 .matchsize = sizeof(struct xt_helper_info),
179 .checkentry = check,
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800180 .destroy = destroy,
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800181 .family = AF_INET6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .me = THIS_MODULE,
183};
184
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800185static int __init xt_helper_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800187 int ret;
188 need_conntrack();
189
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800190 ret = xt_register_match(&helper_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800191 if (ret < 0)
192 return ret;
193
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800194 ret = xt_register_match(&helper6_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800195 if (ret < 0)
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800196 xt_unregister_match(&helper_match);
Harald Welte2e4e6a12006-01-12 13:30:04 -0800197
198 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800201static void __exit xt_helper_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Pablo Neira Ayusoa45049c2006-03-22 13:55:40 -0800203 xt_unregister_match(&helper_match);
204 xt_unregister_match(&helper6_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800207module_init(xt_helper_init);
208module_exit(xt_helper_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209