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; |
| 37 | |
| 38 | list_for_each_entry(h, &helpers, list) { |
| 39 | if (nf_ct_tuple_mask_cmp(tuple, &h->tuple, &h->mask)) |
| 40 | return h; |
| 41 | } |
| 42 | return NULL; |
| 43 | } |
| 44 | |
| 45 | struct nf_conntrack_helper * |
| 46 | nf_ct_helper_find_get( const struct nf_conntrack_tuple *tuple) |
| 47 | { |
| 48 | struct nf_conntrack_helper *helper; |
| 49 | |
| 50 | /* need nf_conntrack_lock to assure that helper exists until |
| 51 | * try_module_get() is called */ |
| 52 | read_lock_bh(&nf_conntrack_lock); |
| 53 | |
| 54 | helper = __nf_ct_helper_find(tuple); |
| 55 | if (helper) { |
| 56 | /* need to increase module usage count to assure helper will |
| 57 | * not go away while the caller is e.g. busy putting a |
| 58 | * conntrack in the hash that uses the helper */ |
| 59 | if (!try_module_get(helper->me)) |
| 60 | helper = NULL; |
| 61 | } |
| 62 | |
| 63 | read_unlock_bh(&nf_conntrack_lock); |
| 64 | |
| 65 | return helper; |
| 66 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 67 | EXPORT_SYMBOL_GPL(nf_ct_helper_find_get); |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 68 | |
| 69 | void nf_ct_helper_put(struct nf_conntrack_helper *helper) |
| 70 | { |
| 71 | module_put(helper->me); |
| 72 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 73 | EXPORT_SYMBOL_GPL(nf_ct_helper_put); |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 74 | |
| 75 | struct nf_conntrack_helper * |
| 76 | __nf_conntrack_helper_find_byname(const char *name) |
| 77 | { |
| 78 | struct nf_conntrack_helper *h; |
| 79 | |
| 80 | list_for_each_entry(h, &helpers, list) { |
| 81 | if (!strcmp(h->name, name)) |
| 82 | return h; |
| 83 | } |
| 84 | |
| 85 | return NULL; |
| 86 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 87 | EXPORT_SYMBOL_GPL(__nf_conntrack_helper_find_byname); |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 88 | |
| 89 | static inline int unhelp(struct nf_conntrack_tuple_hash *i, |
| 90 | const struct nf_conntrack_helper *me) |
| 91 | { |
| 92 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(i); |
| 93 | struct nf_conn_help *help = nfct_help(ct); |
| 94 | |
| 95 | if (help && help->helper == me) { |
| 96 | nf_conntrack_event(IPCT_HELPER, ct); |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 97 | rcu_assign_pointer(help->helper, NULL); |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 98 | } |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | int nf_conntrack_helper_register(struct nf_conntrack_helper *me) |
| 103 | { |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 104 | BUG_ON(me->timeout == 0); |
| 105 | |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 106 | write_lock_bh(&nf_conntrack_lock); |
| 107 | list_add(&me->list, &helpers); |
| 108 | write_unlock_bh(&nf_conntrack_lock); |
| 109 | |
| 110 | return 0; |
| 111 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 112 | EXPORT_SYMBOL_GPL(nf_conntrack_helper_register); |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 113 | |
| 114 | void nf_conntrack_helper_unregister(struct nf_conntrack_helper *me) |
| 115 | { |
| 116 | unsigned int i; |
| 117 | struct nf_conntrack_tuple_hash *h; |
| 118 | struct nf_conntrack_expect *exp, *tmp; |
| 119 | |
| 120 | /* Need write lock here, to delete helper. */ |
| 121 | write_lock_bh(&nf_conntrack_lock); |
| 122 | list_del(&me->list); |
| 123 | |
| 124 | /* Get rid of expectations */ |
| 125 | list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list, list) { |
| 126 | struct nf_conn_help *help = nfct_help(exp->master); |
Patrick McHardy | 9457d85 | 2006-12-02 22:05:25 -0800 | [diff] [blame] | 127 | if ((help->helper == me || exp->helper == me) && |
| 128 | del_timer(&exp->timeout)) { |
Martin Josefsson | 7e5d03b | 2006-11-29 02:34:59 +0100 | [diff] [blame] | 129 | nf_ct_unlink_expect(exp); |
| 130 | nf_conntrack_expect_put(exp); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /* Get rid of expecteds, set helpers to NULL. */ |
| 135 | list_for_each_entry(h, &unconfirmed, list) |
| 136 | unhelp(h, me); |
| 137 | for (i = 0; i < nf_conntrack_htable_size; i++) { |
| 138 | list_for_each_entry(h, &nf_conntrack_hash[i], list) |
| 139 | unhelp(h, me); |
| 140 | } |
| 141 | write_unlock_bh(&nf_conntrack_lock); |
| 142 | |
| 143 | /* Someone could be still looking at the helper in a bh. */ |
| 144 | synchronize_net(); |
| 145 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 146 | EXPORT_SYMBOL_GPL(nf_conntrack_helper_unregister); |
Yasuyuki Kozakai | ceceae1 | 2007-07-07 22:23:42 -0700 | [diff] [blame^] | 147 | |
| 148 | struct nf_ct_ext_type helper_extend = { |
| 149 | .len = sizeof(struct nf_conn_help), |
| 150 | .align = __alignof__(struct nf_conn_help), |
| 151 | .id = NF_CT_EXT_HELPER, |
| 152 | }; |
| 153 | |
| 154 | int nf_conntrack_helper_init() |
| 155 | { |
| 156 | return nf_ct_extend_register(&helper_extend); |
| 157 | } |
| 158 | |
| 159 | void nf_conntrack_helper_fini() |
| 160 | { |
| 161 | nf_ct_extend_unregister(&helper_extend); |
| 162 | } |