Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/skbuff.h> |
| 12 | #include <linux/netfilter.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 13 | #include <net/netfilter/nf_conntrack.h> |
| 14 | #include <net/netfilter/nf_conntrack_core.h> |
| 15 | #include <net/netfilter/nf_conntrack_helper.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 16 | #include <linux/netfilter/x_tables.h> |
| 17 | #include <linux/netfilter/xt_helper.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | MODULE_LICENSE("GPL"); |
| 20 | MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 21 | MODULE_DESCRIPTION("Xtables: Related connection matching"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 22 | MODULE_ALIAS("ipt_helper"); |
| 23 | MODULE_ALIAS("ip6t_helper"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 26 | static bool |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 27 | helper_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 Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 31 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 32 | const struct xt_helper_info *info = matchinfo; |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 33 | const struct nf_conn *ct; |
| 34 | const struct nf_conn_help *master_help; |
Patrick McHardy | 342b7e3 | 2007-07-07 22:39:16 -0700 | [diff] [blame] | 35 | const struct nf_conntrack_helper *helper; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 36 | enum ip_conntrack_info ctinfo; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 37 | bool ret = info->invert; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 38 | |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 39 | ct = nf_ct_get(skb, &ctinfo); |
Patrick McHardy | 342b7e3 | 2007-07-07 22:39:16 -0700 | [diff] [blame] | 40 | if (!ct || !ct->master) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 41 | return ret; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 42 | |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 43 | master_help = nfct_help(ct->master); |
Patrick McHardy | 342b7e3 | 2007-07-07 22:39:16 -0700 | [diff] [blame] | 44 | if (!master_help) |
| 45 | return ret; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 46 | |
Patrick McHardy | 342b7e3 | 2007-07-07 22:39:16 -0700 | [diff] [blame] | 47 | /* rcu_read_lock()ed by nf_hook_slow */ |
| 48 | helper = rcu_dereference(master_help->helper); |
| 49 | if (!helper) |
| 50 | return ret; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 51 | |
| 52 | if (info->name[0] == '\0') |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 53 | ret = !ret; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 54 | else |
Jan Engelhardt | 0ff4d77 | 2008-01-10 22:41:28 -0800 | [diff] [blame] | 55 | ret ^= !strncmp(helper->name, info->name, |
| 56 | strlen(helper->name)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 57 | return ret; |
| 58 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 59 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 60 | static bool |
| 61 | helper_mt_check(const char *tablename, const void *inf, |
| 62 | const struct xt_match *match, void *matchinfo, |
| 63 | unsigned int hook_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 65 | struct xt_helper_info *info = matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 67 | if (nf_ct_l3proto_try_module_get(match->family) < 0) { |
Yasuyuki Kozakai | fe0b929 | 2006-12-12 00:28:40 -0800 | [diff] [blame] | 68 | printk(KERN_WARNING "can't load conntrack support for " |
Jan Engelhardt | df54aae | 2007-12-17 22:43:15 -0800 | [diff] [blame] | 69 | "proto=%u\n", match->family); |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 70 | return false; |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 71 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | info->name[29] = '\0'; |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 73 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 76 | static void helper_mt_destroy(const struct xt_match *match, void *matchinfo) |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 77 | { |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 78 | nf_ct_l3proto_module_put(match->family); |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 81 | static struct xt_match helper_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 82 | { |
| 83 | .name = "helper", |
| 84 | .family = AF_INET, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 85 | .checkentry = helper_mt_check, |
| 86 | .match = helper_mt, |
| 87 | .destroy = helper_mt_destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 88 | .matchsize = sizeof(struct xt_helper_info), |
| 89 | .me = THIS_MODULE, |
| 90 | }, |
| 91 | { |
| 92 | .name = "helper", |
| 93 | .family = AF_INET6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 94 | .checkentry = helper_mt_check, |
| 95 | .match = helper_mt, |
| 96 | .destroy = helper_mt_destroy, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 97 | .matchsize = sizeof(struct xt_helper_info), |
| 98 | .me = THIS_MODULE, |
| 99 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | }; |
| 101 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 102 | static int __init helper_mt_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 104 | return xt_register_matches(helper_mt_reg, ARRAY_SIZE(helper_mt_reg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 107 | static void __exit helper_mt_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 109 | xt_unregister_matches(helper_mt_reg, ARRAY_SIZE(helper_mt_reg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 112 | module_init(helper_mt_init); |
| 113 | module_exit(helper_mt_exit); |