blob: f342788a5766489dc35c5652036baca041541c99 [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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/module.h>
11#include <linux/skbuff.h>
12#include <linux/netfilter.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080013#include <net/netfilter/nf_conntrack.h>
14#include <net/netfilter/nf_conntrack_core.h>
15#include <net/netfilter/nf_conntrack_helper.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080016#include <linux/netfilter/x_tables.h>
17#include <linux/netfilter/xt_helper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19MODULE_LICENSE("GPL");
20MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>");
21MODULE_DESCRIPTION("iptables helper match module");
Harald Welte2e4e6a12006-01-12 13:30:04 -080022MODULE_ALIAS("ipt_helper");
23MODULE_ALIAS("ip6t_helper");
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070026static bool
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080027helper_mt(const struct sk_buff *skb, const struct net_device *in,
28 const struct net_device *out, const struct xt_match *match,
29 const void *matchinfo, int offset, unsigned int protoff,
30 bool *hotdrop)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080031{
Harald Welte2e4e6a12006-01-12 13:30:04 -080032 const struct xt_helper_info *info = matchinfo;
Jan Engelhardta47362a2007-07-07 22:16:55 -070033 const struct nf_conn *ct;
34 const struct nf_conn_help *master_help;
Patrick McHardy342b7e32007-07-07 22:39:16 -070035 const struct nf_conntrack_helper *helper;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080036 enum ip_conntrack_info ctinfo;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070037 bool ret = info->invert;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080038
Jan Engelhardta47362a2007-07-07 22:16:55 -070039 ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy342b7e32007-07-07 22:39:16 -070040 if (!ct || !ct->master)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080041 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080042
Harald Weltedc808fe2006-03-20 17:56:32 -080043 master_help = nfct_help(ct->master);
Patrick McHardy342b7e32007-07-07 22:39:16 -070044 if (!master_help)
45 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080046
Patrick McHardy342b7e32007-07-07 22:39:16 -070047 /* rcu_read_lock()ed by nf_hook_slow */
48 helper = rcu_dereference(master_help->helper);
49 if (!helper)
50 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080051
52 if (info->name[0] == '\0')
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070053 ret = !ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080054 else
Jan Engelhardt0ff4d772008-01-10 22:41:28 -080055 ret ^= !strncmp(helper->name, info->name,
56 strlen(helper->name));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080057 return ret;
58}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080059
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080060static bool
61helper_mt_check(const char *tablename, const void *inf,
62 const struct xt_match *match, void *matchinfo,
63 unsigned int hook_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Harald Welte2e4e6a12006-01-12 13:30:04 -080065 struct xt_helper_info *info = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080067 if (nf_ct_l3proto_try_module_get(match->family) < 0) {
Yasuyuki Kozakaife0b9292006-12-12 00:28:40 -080068 printk(KERN_WARNING "can't load conntrack support for "
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080069 "proto=%d\n", match->family);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070070 return false;
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 info->name[29] = '\0';
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070073 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080076static void helper_mt_destroy(const struct xt_match *match, void *matchinfo)
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080077{
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080078 nf_ct_l3proto_module_put(match->family);
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080079}
80
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080081static struct xt_match helper_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070082 {
83 .name = "helper",
84 .family = AF_INET,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080085 .checkentry = helper_mt_check,
86 .match = helper_mt,
87 .destroy = helper_mt_destroy,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070088 .matchsize = sizeof(struct xt_helper_info),
89 .me = THIS_MODULE,
90 },
91 {
92 .name = "helper",
93 .family = AF_INET6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080094 .checkentry = helper_mt_check,
95 .match = helper_mt,
96 .destroy = helper_mt_destroy,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070097 .matchsize = sizeof(struct xt_helper_info),
98 .me = THIS_MODULE,
99 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800102static int __init helper_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800104 return xt_register_matches(helper_mt_reg, ARRAY_SIZE(helper_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800107static void __exit helper_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800109 xt_unregister_matches(helper_mt_reg, ARRAY_SIZE(helper_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800112module_init(helper_mt_init);
113module_exit(helper_mt_exit);