Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 1 | /* L3/L4 protocol support for nf_conntrack. */ |
| 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> |
Patrick McHardy | f229f6c | 2013-04-06 15:24:29 +0200 | [diff] [blame] | 6 | * (C) 2006-2012 Patrick McHardy <kaber@trash.net> |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/netfilter.h> |
| 15 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 17 | #include <linux/mutex.h> |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 18 | #include <linux/vmalloc.h> |
| 19 | #include <linux/stddef.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/percpu.h> |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 22 | #include <linux/notifier.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/netdevice.h> |
| 25 | |
| 26 | #include <net/netfilter/nf_conntrack.h> |
| 27 | #include <net/netfilter/nf_conntrack_l3proto.h> |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 28 | #include <net/netfilter/nf_conntrack_l4proto.h> |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 29 | #include <net/netfilter/nf_conntrack_core.h> |
| 30 | |
Arnd Bergmann | 0906a37 | 2010-03-09 20:59:15 +0100 | [diff] [blame] | 31 | static struct nf_conntrack_l4proto __rcu **nf_ct_protos[PF_MAX] __read_mostly; |
| 32 | struct nf_conntrack_l3proto __rcu *nf_ct_l3protos[AF_MAX] __read_mostly; |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 33 | EXPORT_SYMBOL_GPL(nf_ct_l3protos); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 34 | |
Patrick McHardy | b19caa0 | 2007-03-14 16:37:52 -0700 | [diff] [blame] | 35 | static DEFINE_MUTEX(nf_ct_proto_mutex); |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 36 | |
Patrick McHardy | b19caa0 | 2007-03-14 16:37:52 -0700 | [diff] [blame] | 37 | #ifdef CONFIG_SYSCTL |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 38 | static int |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 39 | nf_ct_register_sysctl(struct net *net, |
| 40 | struct ctl_table_header **header, |
| 41 | const char *path, |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 42 | struct ctl_table *table) |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 43 | { |
| 44 | if (*header == NULL) { |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 45 | *header = register_net_sysctl(net, path, table); |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 46 | if (*header == NULL) |
| 47 | return -ENOMEM; |
| 48 | } |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 49 | |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static void |
| 54 | nf_ct_unregister_sysctl(struct ctl_table_header **header, |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 55 | struct ctl_table **table, |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 56 | unsigned int users) |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 57 | { |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 58 | if (users > 0) |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 59 | return; |
Pavel Emelyanov | b3fd3ff | 2008-01-09 00:34:02 -0800 | [diff] [blame] | 60 | |
Eric W. Biederman | 5dd3df1 | 2012-04-19 13:24:33 +0000 | [diff] [blame] | 61 | unregister_net_sysctl_table(*header); |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 62 | kfree(*table); |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 63 | *header = NULL; |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 64 | *table = NULL; |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 65 | } |
| 66 | #endif |
| 67 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 68 | struct nf_conntrack_l4proto * |
| 69 | __nf_ct_l4proto_find(u_int16_t l3proto, u_int8_t l4proto) |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 70 | { |
| 71 | if (unlikely(l3proto >= AF_MAX || nf_ct_protos[l3proto] == NULL)) |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 72 | return &nf_conntrack_l4proto_generic; |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 73 | |
Patrick McHardy | 923f490 | 2007-02-12 11:12:57 -0800 | [diff] [blame] | 74 | return rcu_dereference(nf_ct_protos[l3proto][l4proto]); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 75 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 76 | EXPORT_SYMBOL_GPL(__nf_ct_l4proto_find); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 77 | |
| 78 | /* this is guaranteed to always return a valid protocol helper, since |
| 79 | * it falls back to generic_protocol */ |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 80 | struct nf_conntrack_l3proto * |
| 81 | nf_ct_l3proto_find_get(u_int16_t l3proto) |
| 82 | { |
| 83 | struct nf_conntrack_l3proto *p; |
| 84 | |
Patrick McHardy | 923f490 | 2007-02-12 11:12:57 -0800 | [diff] [blame] | 85 | rcu_read_lock(); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 86 | p = __nf_ct_l3proto_find(l3proto); |
| 87 | if (!try_module_get(p->me)) |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 88 | p = &nf_conntrack_l3proto_generic; |
Patrick McHardy | 923f490 | 2007-02-12 11:12:57 -0800 | [diff] [blame] | 89 | rcu_read_unlock(); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 90 | |
| 91 | return p; |
| 92 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 93 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_find_get); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 94 | |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 95 | int |
| 96 | nf_ct_l3proto_try_module_get(unsigned short l3proto) |
| 97 | { |
| 98 | int ret; |
| 99 | struct nf_conntrack_l3proto *p; |
| 100 | |
| 101 | retry: p = nf_ct_l3proto_find_get(l3proto); |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 102 | if (p == &nf_conntrack_l3proto_generic) { |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 103 | ret = request_module("nf_conntrack-%d", l3proto); |
| 104 | if (!ret) |
| 105 | goto retry; |
| 106 | |
| 107 | return -EPROTOTYPE; |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 112 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_try_module_get); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 113 | |
| 114 | void nf_ct_l3proto_module_put(unsigned short l3proto) |
| 115 | { |
| 116 | struct nf_conntrack_l3proto *p; |
| 117 | |
Patrick McHardy | 3b254c5 | 2010-05-10 17:45:56 +0200 | [diff] [blame] | 118 | /* rcu_read_lock not necessary since the caller holds a reference, but |
| 119 | * taken anyways to avoid lockdep warnings in __nf_ct_l3proto_find() |
| 120 | */ |
| 121 | rcu_read_lock(); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 122 | p = __nf_ct_l3proto_find(l3proto); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 123 | module_put(p->me); |
Patrick McHardy | 3b254c5 | 2010-05-10 17:45:56 +0200 | [diff] [blame] | 124 | rcu_read_unlock(); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 125 | } |
Patrick McHardy | 13b1833 | 2006-12-02 22:11:25 -0800 | [diff] [blame] | 126 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_module_put); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 127 | |
Pablo Neira Ayuso | c1ebd7d | 2012-03-22 23:40:01 +0100 | [diff] [blame] | 128 | struct nf_conntrack_l4proto * |
| 129 | nf_ct_l4proto_find_get(u_int16_t l3num, u_int8_t l4num) |
| 130 | { |
| 131 | struct nf_conntrack_l4proto *p; |
| 132 | |
| 133 | rcu_read_lock(); |
| 134 | p = __nf_ct_l4proto_find(l3num, l4num); |
| 135 | if (!try_module_get(p->me)) |
| 136 | p = &nf_conntrack_l4proto_generic; |
| 137 | rcu_read_unlock(); |
| 138 | |
| 139 | return p; |
| 140 | } |
| 141 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_find_get); |
| 142 | |
| 143 | void nf_ct_l4proto_put(struct nf_conntrack_l4proto *p) |
| 144 | { |
| 145 | module_put(p->me); |
| 146 | } |
| 147 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_put); |
| 148 | |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 149 | static int kill_l3proto(struct nf_conn *i, void *data) |
| 150 | { |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 151 | return nf_ct_l3num(i) == ((struct nf_conntrack_l3proto *)data)->l3proto; |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 154 | static int kill_l4proto(struct nf_conn *i, void *data) |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 155 | { |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 156 | struct nf_conntrack_l4proto *l4proto; |
| 157 | l4proto = (struct nf_conntrack_l4proto *)data; |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 158 | return nf_ct_protonum(i) == l4proto->l4proto && |
| 159 | nf_ct_l3num(i) == l4proto->l3proto; |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Gao feng | 6330750 | 2013-01-21 22:10:33 +0000 | [diff] [blame] | 162 | int nf_ct_l3proto_register(struct nf_conntrack_l3proto *proto) |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 163 | { |
| 164 | int ret = 0; |
Eric Dumazet | 0e60ebe | 2010-11-15 18:17:21 +0100 | [diff] [blame] | 165 | struct nf_conntrack_l3proto *old; |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 166 | |
Patrick McHardy | 0661cca | 2007-03-14 16:38:48 -0700 | [diff] [blame] | 167 | if (proto->l3proto >= AF_MAX) |
| 168 | return -EBUSY; |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 169 | |
Holger Eitzenberger | d0dba72 | 2009-03-25 18:24:48 +0100 | [diff] [blame] | 170 | if (proto->tuple_to_nlattr && !proto->nlattr_tuple_size) |
| 171 | return -EINVAL; |
| 172 | |
Patrick McHardy | b19caa0 | 2007-03-14 16:37:52 -0700 | [diff] [blame] | 173 | mutex_lock(&nf_ct_proto_mutex); |
Eric Dumazet | 0e60ebe | 2010-11-15 18:17:21 +0100 | [diff] [blame] | 174 | old = rcu_dereference_protected(nf_ct_l3protos[proto->l3proto], |
| 175 | lockdep_is_held(&nf_ct_proto_mutex)); |
| 176 | if (old != &nf_conntrack_l3proto_generic) { |
Martin Josefsson | ae5718f | 2006-11-29 02:35:08 +0100 | [diff] [blame] | 177 | ret = -EBUSY; |
| 178 | goto out_unlock; |
| 179 | } |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 180 | |
Holger Eitzenberger | d0dba72 | 2009-03-25 18:24:48 +0100 | [diff] [blame] | 181 | if (proto->nlattr_tuple_size) |
| 182 | proto->nla_size = 3 * proto->nlattr_tuple_size(); |
| 183 | |
Patrick McHardy | 0661cca | 2007-03-14 16:38:48 -0700 | [diff] [blame] | 184 | rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], proto); |
Martin Josefsson | ae5718f | 2006-11-29 02:35:08 +0100 | [diff] [blame] | 185 | |
| 186 | out_unlock: |
Patrick McHardy | b19caa0 | 2007-03-14 16:37:52 -0700 | [diff] [blame] | 187 | mutex_unlock(&nf_ct_proto_mutex); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 188 | return ret; |
Gao feng | 524a53e | 2012-05-28 21:04:10 +0000 | [diff] [blame] | 189 | |
| 190 | } |
Gao feng | 6330750 | 2013-01-21 22:10:33 +0000 | [diff] [blame] | 191 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_register); |
Gao feng | 524a53e | 2012-05-28 21:04:10 +0000 | [diff] [blame] | 192 | |
Gao feng | 6330750 | 2013-01-21 22:10:33 +0000 | [diff] [blame] | 193 | int nf_ct_l3proto_pernet_register(struct net *net, |
Gao feng | 524a53e | 2012-05-28 21:04:10 +0000 | [diff] [blame] | 194 | struct nf_conntrack_l3proto *proto) |
| 195 | { |
Pablo Neira Ayuso | adf0516 | 2016-08-12 13:47:06 +0200 | [diff] [blame] | 196 | int ret; |
Gao feng | 524a53e | 2012-05-28 21:04:10 +0000 | [diff] [blame] | 197 | |
Gao feng | 524a53e | 2012-05-28 21:04:10 +0000 | [diff] [blame] | 198 | if (proto->init_net) { |
| 199 | ret = proto->init_net(net); |
| 200 | if (ret < 0) |
| 201 | return ret; |
| 202 | } |
Gao feng | fa0f61f | 2012-06-21 04:36:38 +0000 | [diff] [blame] | 203 | |
Pablo Neira Ayuso | adf0516 | 2016-08-12 13:47:06 +0200 | [diff] [blame] | 204 | return 0; |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 205 | } |
Gao feng | 6330750 | 2013-01-21 22:10:33 +0000 | [diff] [blame] | 206 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_register); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 207 | |
Gao feng | 6330750 | 2013-01-21 22:10:33 +0000 | [diff] [blame] | 208 | void nf_ct_l3proto_unregister(struct nf_conntrack_l3proto *proto) |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 209 | { |
Patrick McHardy | fe3eb20 | 2007-02-12 11:14:28 -0800 | [diff] [blame] | 210 | BUG_ON(proto->l3proto >= AF_MAX); |
Martin Josefsson | ae5718f | 2006-11-29 02:35:08 +0100 | [diff] [blame] | 211 | |
Patrick McHardy | b19caa0 | 2007-03-14 16:37:52 -0700 | [diff] [blame] | 212 | mutex_lock(&nf_ct_proto_mutex); |
Eric Dumazet | 0e60ebe | 2010-11-15 18:17:21 +0100 | [diff] [blame] | 213 | BUG_ON(rcu_dereference_protected(nf_ct_l3protos[proto->l3proto], |
| 214 | lockdep_is_held(&nf_ct_proto_mutex) |
| 215 | ) != proto); |
Patrick McHardy | 923f490 | 2007-02-12 11:12:57 -0800 | [diff] [blame] | 216 | rcu_assign_pointer(nf_ct_l3protos[proto->l3proto], |
| 217 | &nf_conntrack_l3proto_generic); |
Patrick McHardy | 0661cca | 2007-03-14 16:38:48 -0700 | [diff] [blame] | 218 | mutex_unlock(&nf_ct_proto_mutex); |
| 219 | |
| 220 | synchronize_rcu(); |
Gao feng | 524a53e | 2012-05-28 21:04:10 +0000 | [diff] [blame] | 221 | } |
Gao feng | 6330750 | 2013-01-21 22:10:33 +0000 | [diff] [blame] | 222 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_unregister); |
Gao feng | 524a53e | 2012-05-28 21:04:10 +0000 | [diff] [blame] | 223 | |
Gao feng | 6330750 | 2013-01-21 22:10:33 +0000 | [diff] [blame] | 224 | void nf_ct_l3proto_pernet_unregister(struct net *net, |
Gao feng | 524a53e | 2012-05-28 21:04:10 +0000 | [diff] [blame] | 225 | struct nf_conntrack_l3proto *proto) |
| 226 | { |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 227 | /* Remove all contrack entries for this protocol */ |
Florian Westphal | c655bc6 | 2013-07-29 15:41:55 +0200 | [diff] [blame] | 228 | nf_ct_iterate_cleanup(net, kill_l3proto, proto, 0, 0); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 229 | } |
Gao feng | 6330750 | 2013-01-21 22:10:33 +0000 | [diff] [blame] | 230 | EXPORT_SYMBOL_GPL(nf_ct_l3proto_pernet_unregister); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 231 | |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 232 | static struct nf_proto_net *nf_ct_l4proto_net(struct net *net, |
| 233 | struct nf_conntrack_l4proto *l4proto) |
| 234 | { |
Pablo Neira Ayuso | 0891147 | 2012-06-29 05:23:24 +0000 | [diff] [blame] | 235 | if (l4proto->get_net_proto) { |
| 236 | /* statically built-in protocols use static per-net */ |
| 237 | return l4proto->get_net_proto(net); |
| 238 | } else if (l4proto->net_id) { |
| 239 | /* ... and loadable protocols use dynamic per-net */ |
| 240 | return net_generic(net, *l4proto->net_id); |
Gao feng | 15f585b | 2012-05-28 21:04:11 +0000 | [diff] [blame] | 241 | } |
| 242 | return NULL; |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static |
| 246 | int nf_ct_l4proto_register_sysctl(struct net *net, |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 247 | struct nf_proto_net *pn, |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 248 | struct nf_conntrack_l4proto *l4proto) |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 249 | { |
| 250 | int err = 0; |
| 251 | |
| 252 | #ifdef CONFIG_SYSCTL |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 253 | if (pn->ctl_table != NULL) { |
| 254 | err = nf_ct_register_sysctl(net, |
| 255 | &pn->ctl_table_header, |
Eric W. Biederman | f99e8f7 | 2012-04-19 13:43:55 +0000 | [diff] [blame] | 256 | "net/netfilter", |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 257 | pn->ctl_table); |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 258 | if (err < 0) { |
| 259 | if (!pn->users) { |
| 260 | kfree(pn->ctl_table); |
| 261 | pn->ctl_table = NULL; |
| 262 | } |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 263 | } |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 264 | } |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 265 | #endif /* CONFIG_SYSCTL */ |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 266 | return err; |
| 267 | } |
| 268 | |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 269 | static |
| 270 | void nf_ct_l4proto_unregister_sysctl(struct net *net, |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 271 | struct nf_proto_net *pn, |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 272 | struct nf_conntrack_l4proto *l4proto) |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 273 | { |
| 274 | #ifdef CONFIG_SYSCTL |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 275 | if (pn->ctl_table_header != NULL) |
| 276 | nf_ct_unregister_sysctl(&pn->ctl_table_header, |
| 277 | &pn->ctl_table, |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 278 | pn->users); |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 279 | #endif /* CONFIG_SYSCTL */ |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 282 | /* FIXME: Allow NULL functions and sub in pointers to generic for |
| 283 | them. --RR */ |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 284 | int nf_ct_l4proto_register(struct nf_conntrack_l4proto *l4proto) |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 285 | { |
| 286 | int ret = 0; |
| 287 | |
Patrick McHardy | 0661cca | 2007-03-14 16:38:48 -0700 | [diff] [blame] | 288 | if (l4proto->l3proto >= PF_MAX) |
| 289 | return -EBUSY; |
Martin Josefsson | ae5718f | 2006-11-29 02:35:08 +0100 | [diff] [blame] | 290 | |
Holger Eitzenberger | d0dba72 | 2009-03-25 18:24:48 +0100 | [diff] [blame] | 291 | if ((l4proto->to_nlattr && !l4proto->nlattr_size) |
| 292 | || (l4proto->tuple_to_nlattr && !l4proto->nlattr_tuple_size)) |
| 293 | return -EINVAL; |
| 294 | |
Patrick McHardy | b19caa0 | 2007-03-14 16:37:52 -0700 | [diff] [blame] | 295 | mutex_lock(&nf_ct_proto_mutex); |
Patrick McHardy | c6a1e61 | 2007-03-14 16:39:07 -0700 | [diff] [blame] | 296 | if (!nf_ct_protos[l4proto->l3proto]) { |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 297 | /* l3proto may be loaded latter. */ |
Eric Dumazet | c5d277d | 2010-11-15 19:45:13 +0100 | [diff] [blame] | 298 | struct nf_conntrack_l4proto __rcu **proto_array; |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 299 | int i; |
| 300 | |
Patrick McHardy | c6a1e61 | 2007-03-14 16:39:07 -0700 | [diff] [blame] | 301 | proto_array = kmalloc(MAX_NF_CT_PROTO * |
| 302 | sizeof(struct nf_conntrack_l4proto *), |
| 303 | GFP_KERNEL); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 304 | if (proto_array == NULL) { |
| 305 | ret = -ENOMEM; |
Patrick McHardy | b19caa0 | 2007-03-14 16:37:52 -0700 | [diff] [blame] | 306 | goto out_unlock; |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 307 | } |
Patrick McHardy | c6a1e61 | 2007-03-14 16:39:07 -0700 | [diff] [blame] | 308 | |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 309 | for (i = 0; i < MAX_NF_CT_PROTO; i++) |
Eric Dumazet | c5d277d | 2010-11-15 19:45:13 +0100 | [diff] [blame] | 310 | RCU_INIT_POINTER(proto_array[i], &nf_conntrack_l4proto_generic); |
Eric Dumazet | d817d29 | 2010-10-29 19:59:40 +0200 | [diff] [blame] | 311 | |
| 312 | /* Before making proto_array visible to lockless readers, |
| 313 | * we must make sure its content is committed to memory. |
| 314 | */ |
| 315 | smp_wmb(); |
| 316 | |
Patrick McHardy | c6a1e61 | 2007-03-14 16:39:07 -0700 | [diff] [blame] | 317 | nf_ct_protos[l4proto->l3proto] = proto_array; |
Eric Dumazet | 0e60ebe | 2010-11-15 18:17:21 +0100 | [diff] [blame] | 318 | } else if (rcu_dereference_protected( |
| 319 | nf_ct_protos[l4proto->l3proto][l4proto->l4proto], |
| 320 | lockdep_is_held(&nf_ct_proto_mutex) |
| 321 | ) != &nf_conntrack_l4proto_generic) { |
Patrick McHardy | c6a1e61 | 2007-03-14 16:39:07 -0700 | [diff] [blame] | 322 | ret = -EBUSY; |
| 323 | goto out_unlock; |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Holger Eitzenberger | d0dba72 | 2009-03-25 18:24:48 +0100 | [diff] [blame] | 326 | l4proto->nla_size = 0; |
| 327 | if (l4proto->nlattr_size) |
| 328 | l4proto->nla_size += l4proto->nlattr_size(); |
| 329 | if (l4proto->nlattr_tuple_size) |
| 330 | l4proto->nla_size += 3 * l4proto->nlattr_tuple_size(); |
| 331 | |
Patrick McHardy | c6a1e61 | 2007-03-14 16:39:07 -0700 | [diff] [blame] | 332 | rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto], |
| 333 | l4proto); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 334 | out_unlock: |
Patrick McHardy | b19caa0 | 2007-03-14 16:37:52 -0700 | [diff] [blame] | 335 | mutex_unlock(&nf_ct_proto_mutex); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 336 | return ret; |
| 337 | } |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 338 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_register); |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 339 | |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 340 | int nf_ct_l4proto_pernet_register(struct net *net, |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 341 | struct nf_conntrack_l4proto *l4proto) |
| 342 | { |
| 343 | int ret = 0; |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 344 | struct nf_proto_net *pn = NULL; |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 345 | |
Gao feng | fa0f61f | 2012-06-21 04:36:38 +0000 | [diff] [blame] | 346 | if (l4proto->init_net) { |
Gao feng | f1caad2 | 2012-06-21 04:36:39 +0000 | [diff] [blame] | 347 | ret = l4proto->init_net(net, l4proto->l3proto); |
Gao feng | fa0f61f | 2012-06-21 04:36:38 +0000 | [diff] [blame] | 348 | if (ret < 0) |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 349 | goto out; |
Gao feng | fa0f61f | 2012-06-21 04:36:38 +0000 | [diff] [blame] | 350 | } |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 351 | |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 352 | pn = nf_ct_l4proto_net(net, l4proto); |
| 353 | if (pn == NULL) |
| 354 | goto out; |
| 355 | |
| 356 | ret = nf_ct_l4proto_register_sysctl(net, pn, l4proto); |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 357 | if (ret < 0) |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 358 | goto out; |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 359 | |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 360 | pn->users++; |
| 361 | out: |
Gao feng | fa0f61f | 2012-06-21 04:36:38 +0000 | [diff] [blame] | 362 | return ret; |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 363 | } |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 364 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_register); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 365 | |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 366 | void nf_ct_l4proto_unregister(struct nf_conntrack_l4proto *l4proto) |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 367 | { |
Patrick McHardy | fe3eb20 | 2007-02-12 11:14:28 -0800 | [diff] [blame] | 368 | BUG_ON(l4proto->l3proto >= PF_MAX); |
Martin Josefsson | ae5718f | 2006-11-29 02:35:08 +0100 | [diff] [blame] | 369 | |
Patrick McHardy | b19caa0 | 2007-03-14 16:37:52 -0700 | [diff] [blame] | 370 | mutex_lock(&nf_ct_proto_mutex); |
Eric Dumazet | 0e60ebe | 2010-11-15 18:17:21 +0100 | [diff] [blame] | 371 | BUG_ON(rcu_dereference_protected( |
| 372 | nf_ct_protos[l4proto->l3proto][l4proto->l4proto], |
| 373 | lockdep_is_held(&nf_ct_proto_mutex) |
| 374 | ) != l4proto); |
Patrick McHardy | 923f490 | 2007-02-12 11:12:57 -0800 | [diff] [blame] | 375 | rcu_assign_pointer(nf_ct_protos[l4proto->l3proto][l4proto->l4proto], |
| 376 | &nf_conntrack_l4proto_generic); |
Patrick McHardy | 0661cca | 2007-03-14 16:38:48 -0700 | [diff] [blame] | 377 | mutex_unlock(&nf_ct_proto_mutex); |
| 378 | |
| 379 | synchronize_rcu(); |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 380 | } |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 381 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_unregister); |
Patrick McHardy | d62f9ed | 2006-11-29 02:35:17 +0100 | [diff] [blame] | 382 | |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 383 | void nf_ct_l4proto_pernet_unregister(struct net *net, |
Gao feng | 2c352f4 | 2012-05-28 21:04:09 +0000 | [diff] [blame] | 384 | struct nf_conntrack_l4proto *l4proto) |
| 385 | { |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 386 | struct nf_proto_net *pn = NULL; |
| 387 | |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 388 | pn = nf_ct_l4proto_net(net, l4proto); |
| 389 | if (pn == NULL) |
| 390 | return; |
| 391 | |
| 392 | pn->users--; |
| 393 | nf_ct_l4proto_unregister_sysctl(net, pn, l4proto); |
| 394 | |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 395 | /* Remove all contrack entries for this protocol */ |
Florian Westphal | c655bc6 | 2013-07-29 15:41:55 +0200 | [diff] [blame] | 396 | nf_ct_iterate_cleanup(net, kill_l4proto, l4proto, 0, 0); |
Martin Josefsson | 8f03dea | 2006-11-29 02:35:03 +0100 | [diff] [blame] | 397 | } |
Gao feng | c296bb4 | 2013-01-23 12:51:10 +0100 | [diff] [blame] | 398 | EXPORT_SYMBOL_GPL(nf_ct_l4proto_pernet_unregister); |
Patrick McHardy | ac5357e | 2007-03-14 16:38:25 -0700 | [diff] [blame] | 399 | |
Gao feng | 04d8700 | 2013-01-21 22:10:32 +0000 | [diff] [blame] | 400 | int nf_conntrack_proto_pernet_init(struct net *net) |
Patrick McHardy | ac5357e | 2007-03-14 16:38:25 -0700 | [diff] [blame] | 401 | { |
Patrick McHardy | ac5357e | 2007-03-14 16:38:25 -0700 | [diff] [blame] | 402 | int err; |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 403 | struct nf_proto_net *pn = nf_ct_l4proto_net(net, |
| 404 | &nf_conntrack_l4proto_generic); |
| 405 | |
Gao feng | f1caad2 | 2012-06-21 04:36:39 +0000 | [diff] [blame] | 406 | err = nf_conntrack_l4proto_generic.init_net(net, |
| 407 | nf_conntrack_l4proto_generic.l3proto); |
Gao feng | 15f585b | 2012-05-28 21:04:11 +0000 | [diff] [blame] | 408 | if (err < 0) |
| 409 | return err; |
| 410 | err = nf_ct_l4proto_register_sysctl(net, |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 411 | pn, |
Gao feng | 15f585b | 2012-05-28 21:04:11 +0000 | [diff] [blame] | 412 | &nf_conntrack_l4proto_generic); |
Patrick McHardy | ac5357e | 2007-03-14 16:38:25 -0700 | [diff] [blame] | 413 | if (err < 0) |
| 414 | return err; |
| 415 | |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 416 | pn->users++; |
Patrick McHardy | ac5357e | 2007-03-14 16:38:25 -0700 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
Gao feng | 04d8700 | 2013-01-21 22:10:32 +0000 | [diff] [blame] | 420 | void nf_conntrack_proto_pernet_fini(struct net *net) |
Patrick McHardy | ac5357e | 2007-03-14 16:38:25 -0700 | [diff] [blame] | 421 | { |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 422 | struct nf_proto_net *pn = nf_ct_l4proto_net(net, |
| 423 | &nf_conntrack_l4proto_generic); |
| 424 | |
| 425 | pn->users--; |
Gao feng | 15f585b | 2012-05-28 21:04:11 +0000 | [diff] [blame] | 426 | nf_ct_l4proto_unregister_sysctl(net, |
Gao feng | fa34fff | 2012-06-21 04:36:41 +0000 | [diff] [blame] | 427 | pn, |
Gao feng | 15f585b | 2012-05-28 21:04:11 +0000 | [diff] [blame] | 428 | &nf_conntrack_l4proto_generic); |
Gao feng | 04d8700 | 2013-01-21 22:10:32 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | int nf_conntrack_proto_init(void) |
| 432 | { |
| 433 | unsigned int i; |
| 434 | for (i = 0; i < AF_MAX; i++) |
| 435 | rcu_assign_pointer(nf_ct_l3protos[i], |
| 436 | &nf_conntrack_l3proto_generic); |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | void nf_conntrack_proto_fini(void) |
| 441 | { |
| 442 | unsigned int i; |
| 443 | /* free l3proto protocol tables */ |
| 444 | for (i = 0; i < PF_MAX; i++) |
| 445 | kfree(nf_ct_protos[i]); |
Patrick McHardy | ac5357e | 2007-03-14 16:38:25 -0700 | [diff] [blame] | 446 | } |