Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 1 | /* Helper handling for netfilter. */ |
| 2 | |
| 3 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 4 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> |
| 5 | * (C) 2003,2004 USAGI/WIDE Project <http://www.linux-ipv6.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/netfilter.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/skbuff.h> |
| 16 | #include <linux/vmalloc.h> |
| 17 | #include <linux/stddef.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/random.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/netdevice.h> |
| 23 | |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 24 | #include <net/netfilter/nf_conntrack.h> |
| 25 | #include <net/netfilter/nf_conntrack_l3proto.h> |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 26 | #include <net/netfilter/nf_conntrack_l4proto.h> |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 27 | #include <net/netfilter/nf_conntrack_helper.h> |
| 28 | #include <net/netfilter/nf_conntrack_core.h> |
Yasuyuki Kozakai | ceceae1 | 2007-07-07 22:23:42 -0700 | [diff] [blame] | 29 | #include <net/netfilter/nf_conntrack_extend.h> |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 30 | |
Martin Josefsson | e2b7606 | 2006-11-29 02:35:04 +0100 | [diff] [blame] | 31 | static __read_mostly LIST_HEAD(helpers); |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 32 | |
| 33 | struct nf_conntrack_helper * |
| 34 | __nf_ct_helper_find(const struct nf_conntrack_tuple *tuple) |
| 35 | { |
| 36 | struct nf_conntrack_helper *h; |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 37 | struct nf_conntrack_tuple_mask mask = { .src.u.all = htons(0xFFFF) }; |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 38 | |
| 39 | list_for_each_entry(h, &helpers, list) { |
Patrick McHardy | d4156e8 | 2007-07-07 22:31:32 -0700 | [diff] [blame] | 40 | if (nf_ct_tuple_src_mask_cmp(tuple, &h->tuple, &mask)) |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 41 | return h; |
| 42 | } |
| 43 | return NULL; |
| 44 | } |
| 45 | |
| 46 | struct nf_conntrack_helper * |
| 47 | nf_ct_helper_find_get( const struct nf_conntrack_tuple *tuple) |
| 48 | { |
| 49 | struct nf_conntrack_helper *helper; |
| 50 | |
| 51 | /* need nf_conntrack_lock to assure that helper exists until |
| 52 | * try_module_get() is called */ |
| 53 | read_lock_bh(&nf_conntrack_lock); |
| 54 | |
| 55 | helper = __nf_ct_helper_find(tuple); |
| 56 | if (helper) { |
| 57 | /* need to increase module usage count to assure helper will |
| 58 | * not go away while the caller is e.g. busy putting a |
| 59 | * conntrack in the hash that uses the helper */ |
| 60 | if (!try_module_get(helper->me)) |
| 61 | helper = NULL; |
| 62 | } |
| 63 | |
| 64 | read_unlock_bh(&nf_conntrack_lock); |
| 65 | |
| 66 | return helper; |
| 67 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 68 | EXPORT_SYMBOL_GPL(nf_ct_helper_find_get); |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 69 | |
| 70 | void nf_ct_helper_put(struct nf_conntrack_helper *helper) |
| 71 | { |
| 72 | module_put(helper->me); |
| 73 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 74 | EXPORT_SYMBOL_GPL(nf_ct_helper_put); |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 75 | |
| 76 | struct nf_conntrack_helper * |
| 77 | __nf_conntrack_helper_find_byname(const char *name) |
| 78 | { |
| 79 | struct nf_conntrack_helper *h; |
| 80 | |
| 81 | list_for_each_entry(h, &helpers, list) { |
| 82 | if (!strcmp(h->name, name)) |
| 83 | return h; |
| 84 | } |
| 85 | |
| 86 | return NULL; |
| 87 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 88 | EXPORT_SYMBOL_GPL(__nf_conntrack_helper_find_byname); |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 89 | |
Patrick McHardy | b560580 | 2007-07-07 22:35:56 -0700 | [diff] [blame^] | 90 | struct nf_conn_help *nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp) |
| 91 | { |
| 92 | struct nf_conn_help *help; |
| 93 | |
| 94 | help = nf_ct_ext_add(ct, NF_CT_EXT_HELPER, gfp); |
| 95 | if (help) |
| 96 | INIT_HLIST_HEAD(&help->expectations); |
| 97 | else |
| 98 | pr_debug("failed to add helper extension area"); |
| 99 | return help; |
| 100 | } |
| 101 | EXPORT_SYMBOL_GPL(nf_ct_helper_ext_add); |
| 102 | |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 103 | static inline int unhelp(struct nf_conntrack_tuple_hash *i, |
| 104 | const struct nf_conntrack_helper *me) |
| 105 | { |
| 106 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(i); |
| 107 | struct nf_conn_help *help = nfct_help(ct); |
| 108 | |
| 109 | if (help && help->helper == me) { |
| 110 | nf_conntrack_event(IPCT_HELPER, ct); |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 111 | rcu_assign_pointer(help->helper, NULL); |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int nf_conntrack_helper_register(struct nf_conntrack_helper *me) |
| 117 | { |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 118 | BUG_ON(me->timeout == 0); |
| 119 | |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 120 | write_lock_bh(&nf_conntrack_lock); |
| 121 | list_add(&me->list, &helpers); |
| 122 | write_unlock_bh(&nf_conntrack_lock); |
| 123 | |
| 124 | return 0; |
| 125 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 126 | EXPORT_SYMBOL_GPL(nf_conntrack_helper_register); |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 127 | |
| 128 | void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me) |
| 129 | { |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 130 | struct nf_conntrack_tuple_hash *h; |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 131 | struct nf_conntrack_expect *exp; |
| 132 | struct hlist_node *n, *next; |
| 133 | unsigned int i; |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 134 | |
| 135 | /* Need write lock here, to delete helper. */ |
| 136 | write_lock_bh(&nf_conntrack_lock); |
| 137 | list_del(&me->list); |
| 138 | |
| 139 | /* Get rid of expectations */ |
Patrick McHardy | 31f1587 | 2007-07-07 22:35:21 -0700 | [diff] [blame] | 140 | for (i = 0; i < nf_ct_expect_hsize; i++) { |
| 141 | hlist_for_each_entry_safe(exp, n, next, |
| 142 | &nf_ct_expect_hash[i], hnode) { |
| 143 | struct nf_conn_help *help = nfct_help(exp->master); |
| 144 | if ((help->helper == me || exp->helper == me) && |
| 145 | del_timer(&exp->timeout)) { |
| 146 | nf_ct_unlink_expect(exp); |
| 147 | nf_ct_expect_put(exp); |
| 148 | } |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
| 152 | /* Get rid of expecteds, set helpers to NULL. */ |
Patrick McHardy | f205c5e | 2007-07-07 22:28:14 -0700 | [diff] [blame] | 153 | hlist_for_each_entry(h, n, &unconfirmed, hnode) |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 154 | unhelp(h, me); |
| 155 | for (i = 0; i < nf_conntrack_htable_size; i++) { |
Patrick McHardy | f205c5e | 2007-07-07 22:28:14 -0700 | [diff] [blame] | 156 | hlist_for_each_entry(h, n, &nf_conntrack_hash[i], hnode) |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 157 | unhelp(h, me); |
| 158 | } |
| 159 | write_unlock_bh(&nf_conntrack_lock); |
| 160 | |
| 161 | /* Someone could be still looking at the helper in a bh. */ |
| 162 | synchronize_net(); |
| 163 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 164 | EXPORT_SYMBOL_GPL(nf_conntrack_helper_unregister); |
Yasuyuki Kozakai | ceceae1 | 2007-07-07 22:23:42 -0700 | [diff] [blame] | 165 | |
Patrick McHardy | 61eb310 | 2007-07-07 22:27:06 -0700 | [diff] [blame] | 166 | static struct nf_ct_ext_type helper_extend __read_mostly = { |
Yasuyuki Kozakai | ceceae1 | 2007-07-07 22:23:42 -0700 | [diff] [blame] | 167 | .len = sizeof(struct nf_conn_help), |
| 168 | .align = __alignof__(struct nf_conn_help), |
| 169 | .id = NF_CT_EXT_HELPER, |
| 170 | }; |
| 171 | |
| 172 | int nf_conntrack_helper_init() |
| 173 | { |
| 174 | return nf_ct_extend_register(&helper_extend); |
| 175 | } |
| 176 | |
| 177 | void nf_conntrack_helper_fini() |
| 178 | { |
| 179 | nf_ct_extend_unregister(&helper_extend); |
| 180 | } |