blob: 0a1f4c6bcdef415afae94b369cefb4ab133492bd [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
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080027match(const struct sk_buff *skb,
28 const struct net_device *in,
29 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080030 const struct xt_match *match,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080031 const void *matchinfo,
32 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080033 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070034 bool *hotdrop)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080035{
Harald Welte2e4e6a12006-01-12 13:30:04 -080036 const struct xt_helper_info *info = matchinfo;
Jan Engelhardta47362a2007-07-07 22:16:55 -070037 const struct nf_conn *ct;
38 const struct nf_conn_help *master_help;
Patrick McHardy342b7e32007-07-07 22:39:16 -070039 const struct nf_conntrack_helper *helper;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080040 enum ip_conntrack_info ctinfo;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070041 bool ret = info->invert;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080042
Jan Engelhardta47362a2007-07-07 22:16:55 -070043 ct = nf_ct_get(skb, &ctinfo);
Patrick McHardy342b7e32007-07-07 22:39:16 -070044 if (!ct || !ct->master)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080045 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080046
Harald Weltedc808fe2006-03-20 17:56:32 -080047 master_help = nfct_help(ct->master);
Patrick McHardy342b7e32007-07-07 22:39:16 -070048 if (!master_help)
49 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080050
Patrick McHardy342b7e32007-07-07 22:39:16 -070051 /* rcu_read_lock()ed by nf_hook_slow */
52 helper = rcu_dereference(master_help->helper);
53 if (!helper)
54 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080055
56 if (info->name[0] == '\0')
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070057 ret = !ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080058 else
Harald Weltedc808fe2006-03-20 17:56:32 -080059 ret ^= !strncmp(master_help->helper->name, info->name,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080060 strlen(master_help->helper->name));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080061 return ret;
62}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080063
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070064static bool check(const char *tablename,
65 const void *inf,
66 const struct xt_match *match,
67 void *matchinfo,
68 unsigned int hook_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Harald Welte2e4e6a12006-01-12 13:30:04 -080070 struct xt_helper_info *info = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080072 if (nf_ct_l3proto_try_module_get(match->family) < 0) {
Yasuyuki Kozakaife0b9292006-12-12 00:28:40 -080073 printk(KERN_WARNING "can't load conntrack support for "
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080074 "proto=%d\n", match->family);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070075 return false;
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 info->name[29] = '\0';
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070078 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080081static void
Patrick McHardyefa74162006-08-22 00:36:37 -070082destroy(const struct xt_match *match, void *matchinfo)
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080083{
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080084 nf_ct_l3proto_module_put(match->family);
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080085}
86
Patrick McHardy9f15c532007-07-07 22:22:02 -070087static struct xt_match xt_helper_match[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070088 {
89 .name = "helper",
90 .family = AF_INET,
91 .checkentry = check,
92 .match = match,
93 .destroy = destroy,
94 .matchsize = sizeof(struct xt_helper_info),
95 .me = THIS_MODULE,
96 },
97 {
98 .name = "helper",
99 .family = AF_INET6,
100 .checkentry = check,
101 .match = match,
102 .destroy = destroy,
103 .matchsize = sizeof(struct xt_helper_info),
104 .me = THIS_MODULE,
105 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800108static int __init xt_helper_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700110 return xt_register_matches(xt_helper_match,
111 ARRAY_SIZE(xt_helper_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800114static void __exit xt_helper_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700116 xt_unregister_matches(xt_helper_match, ARRAY_SIZE(xt_helper_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800119module_init(xt_helper_init);
120module_exit(xt_helper_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121