blob: 407d1d5da8a1740329b1964433ffca5f1fe3d971 [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>
Yasuyuki Kozakaife0b9292006-12-12 00:28:40 -080027#include <net/netfilter/nf_conntrack_compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29MODULE_LICENSE("GPL");
30MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>");
31MODULE_DESCRIPTION("iptables helper match module");
Harald Welte2e4e6a12006-01-12 13:30:04 -080032MODULE_ALIAS("ipt_helper");
33MODULE_ALIAS("ip6t_helper");
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#if 0
36#define DEBUGP printk
37#else
38#define DEBUGP(format, args...)
39#endif
40
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080041#if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static int
43match(const struct sk_buff *skb,
44 const struct net_device *in,
45 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080046 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 const void *matchinfo,
48 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080049 unsigned int protoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 int *hotdrop)
51{
Harald Welte2e4e6a12006-01-12 13:30:04 -080052 const struct xt_helper_info *info = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 struct ip_conntrack *ct;
54 enum ip_conntrack_info ctinfo;
55 int ret = info->invert;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 ct = ip_conntrack_get((struct sk_buff *)skb, &ctinfo);
58 if (!ct) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080059 DEBUGP("xt_helper: Eek! invalid conntrack?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return ret;
61 }
62
63 if (!ct->master) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080064 DEBUGP("xt_helper: conntrack %p has no master\n", ct);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return ret;
66 }
67
Patrick McHardye45b1be2005-06-21 14:01:30 -070068 read_lock_bh(&ip_conntrack_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (!ct->master->helper) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080070 DEBUGP("xt_helper: master ct %p has no helper\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 exp->expectant);
72 goto out_unlock;
73 }
74
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080075 DEBUGP("master's name = %s , info->name = %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 ct->master->helper->name, info->name);
77
78 if (info->name[0] == '\0')
79 ret ^= 1;
80 else
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080081 ret ^= !strncmp(ct->master->helper->name, info->name,
82 strlen(ct->master->helper->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -070083out_unlock:
Patrick McHardye45b1be2005-06-21 14:01:30 -070084 read_unlock_bh(&ip_conntrack_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return ret;
86}
87
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080088#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,
Patrick McHardyc4986732006-03-20 18:02:56 -080094 const struct xt_match *match,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080095 const void *matchinfo,
96 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080097 unsigned int protoff,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080098 int *hotdrop)
99{
Harald Welte2e4e6a12006-01-12 13:30:04 -0800100 const struct xt_helper_info *info = matchinfo;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800101 struct nf_conn *ct;
Harald Weltedc808fe2006-03-20 17:56:32 -0800102 struct nf_conn_help *master_help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800103 enum ip_conntrack_info ctinfo;
104 int ret = info->invert;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800105
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800106 ct = nf_ct_get((struct sk_buff *)skb, &ctinfo);
107 if (!ct) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800108 DEBUGP("xt_helper: Eek! invalid conntrack?\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800109 return ret;
110 }
111
112 if (!ct->master) {
Harald Welte2e4e6a12006-01-12 13:30:04 -0800113 DEBUGP("xt_helper: conntrack %p has no master\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800114 return ret;
115 }
116
117 read_lock_bh(&nf_conntrack_lock);
Harald Weltedc808fe2006-03-20 17:56:32 -0800118 master_help = nfct_help(ct->master);
119 if (!master_help || !master_help->helper) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800120 DEBUGP("xt_helper: master ct %p has no helper\n",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800121 exp->expectant);
122 goto out_unlock;
123 }
124
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800125 DEBUGP("master's name = %s , info->name = %s\n",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800126 ct->master->helper->name, info->name);
127
128 if (info->name[0] == '\0')
129 ret ^= 1;
130 else
Harald Weltedc808fe2006-03-20 17:56:32 -0800131 ret ^= !strncmp(master_help->helper->name, info->name,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800132 strlen(master_help->helper->name));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800133out_unlock:
134 read_unlock_bh(&nf_conntrack_lock);
135 return ret;
136}
137#endif
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static int check(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800140 const void *inf,
Patrick McHardyc4986732006-03-20 18:02:56 -0800141 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 void *matchinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 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 (nf_ct_l3proto_try_module_get(match->family) < 0) {
Yasuyuki Kozakaife0b9292006-12-12 00:28:40 -0800148 printk(KERN_WARNING "can't load conntrack support for "
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800149 "proto=%d\n", match->family);
150 return 0;
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 info->name[29] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return 1;
154}
155
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800156static void
Patrick McHardyefa74162006-08-22 00:36:37 -0700157destroy(const struct xt_match *match, void *matchinfo)
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800158{
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800159 nf_ct_l3proto_module_put(match->family);
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800160}
161
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700162static 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 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181};
182
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800183static int __init xt_helper_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700185 return xt_register_matches(xt_helper_match,
186 ARRAY_SIZE(xt_helper_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800189static void __exit xt_helper_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700191 xt_unregister_matches(xt_helper_match, ARRAY_SIZE(xt_helper_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800194module_init(xt_helper_init);
195module_exit(xt_helper_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196