]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/netfilter/nfnetlink.c
Merge tag 'for-linus-20170825' of git://git.infradead.org/linux-mtd
[mirror_ubuntu-artful-kernel.git] / net / netfilter / nfnetlink.c
CommitLineData
f9e815b3
HW
1/* Netfilter messages via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
3 *
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>,
5 * (C) 2002-2005 by Harald Welte <laforge@gnumonks.org>
8c4d4e8b 6 * (C) 2005-2017 by Pablo Neira Ayuso <pablo@netfilter.org>
f9e815b3
HW
7 *
8 * Initial netfilter messages via netlink development funded and
9 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
10 *
11 * Further development of this code funded by Astaro AG (http://www.astaro.com)
12 *
13 * This software may be used and distributed according to the terms
14 * of the GNU General Public License, incorporated herein by reference.
15 */
16
f9e815b3
HW
17#include <linux/module.h>
18#include <linux/types.h>
19#include <linux/socket.h>
20#include <linux/kernel.h>
f9e815b3
HW
21#include <linux/string.h>
22#include <linux/sockios.h>
23#include <linux/net.h>
f9e815b3 24#include <linux/skbuff.h>
7c0f6ba6 25#include <linux/uaccess.h>
f9e815b3
HW
26#include <net/sock.h>
27#include <linux/init.h>
f9e815b3 28
573ce260 29#include <net/netlink.h>
f9e815b3
HW
30#include <linux/netfilter/nfnetlink.h>
31
32MODULE_LICENSE("GPL");
4fdb3bb7
HW
33MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
34MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
f9e815b3 35
9c55d3b5
FW
36#define nfnl_dereference_protected(id) \
37 rcu_dereference_protected(table[(id)].subsys, \
38 lockdep_nfnl_is_held((id)))
39
f9e815b3
HW
40static char __initdata nfversion[] = "0.30";
41
c14b78e7
PNA
42static struct {
43 struct mutex mutex;
44 const struct nfnetlink_subsystem __rcu *subsys;
45} table[NFNL_SUBSYS_COUNT];
f9e815b3 46
03292745
PNA
47static const int nfnl_group2type[NFNLGRP_MAX+1] = {
48 [NFNLGRP_CONNTRACK_NEW] = NFNL_SUBSYS_CTNETLINK,
49 [NFNLGRP_CONNTRACK_UPDATE] = NFNL_SUBSYS_CTNETLINK,
50 [NFNLGRP_CONNTRACK_DESTROY] = NFNL_SUBSYS_CTNETLINK,
51 [NFNLGRP_CONNTRACK_EXP_NEW] = NFNL_SUBSYS_CTNETLINK_EXP,
52 [NFNLGRP_CONNTRACK_EXP_UPDATE] = NFNL_SUBSYS_CTNETLINK_EXP,
53 [NFNLGRP_CONNTRACK_EXP_DESTROY] = NFNL_SUBSYS_CTNETLINK_EXP,
97840cb6
PNA
54 [NFNLGRP_NFTABLES] = NFNL_SUBSYS_NFTABLES,
55 [NFNLGRP_ACCT_QUOTA] = NFNL_SUBSYS_ACCT,
33d5a7b1 56 [NFNLGRP_NFTRACE] = NFNL_SUBSYS_NFTABLES,
03292745
PNA
57};
58
c14b78e7 59void nfnl_lock(__u8 subsys_id)
f9e815b3 60{
c14b78e7 61 mutex_lock(&table[subsys_id].mutex);
f9e815b3 62}
e6a7d3c0 63EXPORT_SYMBOL_GPL(nfnl_lock);
f9e815b3 64
c14b78e7 65void nfnl_unlock(__u8 subsys_id)
a3c5029c 66{
c14b78e7 67 mutex_unlock(&table[subsys_id].mutex);
f9e815b3 68}
e6a7d3c0 69EXPORT_SYMBOL_GPL(nfnl_unlock);
f9e815b3 70
0eb5db7a 71#ifdef CONFIG_PROVE_LOCKING
875e0829 72bool lockdep_nfnl_is_held(u8 subsys_id)
0eb5db7a
PM
73{
74 return lockdep_is_held(&table[subsys_id].mutex);
75}
76EXPORT_SYMBOL_GPL(lockdep_nfnl_is_held);
77#endif
78
7c8d4cb4 79int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n)
f9e815b3 80{
c14b78e7
PNA
81 nfnl_lock(n->subsys_id);
82 if (table[n->subsys_id].subsys) {
83 nfnl_unlock(n->subsys_id);
0ab43f84
HW
84 return -EBUSY;
85 }
c14b78e7
PNA
86 rcu_assign_pointer(table[n->subsys_id].subsys, n);
87 nfnl_unlock(n->subsys_id);
f9e815b3
HW
88
89 return 0;
90}
f4bc177f 91EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
f9e815b3 92
7c8d4cb4 93int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n)
f9e815b3 94{
c14b78e7
PNA
95 nfnl_lock(n->subsys_id);
96 table[n->subsys_id].subsys = NULL;
97 nfnl_unlock(n->subsys_id);
6b75e3e8 98 synchronize_rcu();
f9e815b3
HW
99 return 0;
100}
f4bc177f 101EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
f9e815b3 102
b745d035 103static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u16 type)
f9e815b3 104{
b745d035 105 u8 subsys_id = NFNL_SUBSYS_ID(type);
f9e815b3 106
ac0f1d98 107 if (subsys_id >= NFNL_SUBSYS_COUNT)
f9e815b3
HW
108 return NULL;
109
c14b78e7 110 return rcu_dereference(table[subsys_id].subsys);
f9e815b3
HW
111}
112
7c8d4cb4 113static inline const struct nfnl_callback *
b745d035 114nfnetlink_find_client(u16 type, const struct nfnetlink_subsystem *ss)
f9e815b3 115{
b745d035 116 u8 cb_id = NFNL_MSG_TYPE(type);
601e68e1 117
67ca3966 118 if (cb_id >= ss->cb_count)
f9e815b3 119 return NULL;
f9e815b3
HW
120
121 return &ss->cb[cb_id];
122}
123
cd8c20b6 124int nfnetlink_has_listeners(struct net *net, unsigned int group)
a2427692 125{
cd8c20b6 126 return netlink_has_listeners(net->nfnl, group);
a2427692
PM
127}
128EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
129
ec464e5d 130int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid,
95c96174 131 unsigned int group, int echo, gfp_t flags)
f9e815b3 132{
ec464e5d 133 return nlmsg_notify(net->nfnl, skb, portid, group, echo, flags);
f9e815b3 134}
f4bc177f 135EXPORT_SYMBOL_GPL(nfnetlink_send);
f9e815b3 136
ec464e5d 137int nfnetlink_set_err(struct net *net, u32 portid, u32 group, int error)
dd5b6ce6 138{
ec464e5d 139 return netlink_set_err(net->nfnl, portid, group, error);
dd5b6ce6
PNA
140}
141EXPORT_SYMBOL_GPL(nfnetlink_set_err);
142
ec464e5d
PM
143int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u32 portid,
144 int flags)
f9e815b3 145{
ec464e5d 146 return netlink_unicast(net->nfnl, skb, portid, flags);
f9e815b3 147}
f4bc177f 148EXPORT_SYMBOL_GPL(nfnetlink_unicast);
f9e815b3
HW
149
150/* Process one complete nfnetlink message. */
2d4bc933
JB
151static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
152 struct netlink_ext_ack *extack)
f9e815b3 153{
cd8c20b6 154 struct net *net = sock_net(skb->sk);
7c8d4cb4
PM
155 const struct nfnl_callback *nc;
156 const struct nfnetlink_subsystem *ss;
1d00a4eb 157 int type, err;
f9e815b3 158
f9e815b3 159 /* All the messages must at least contain nfgenmsg */
573ce260 160 if (nlmsg_len(nlh) < sizeof(struct nfgenmsg))
f9e815b3 161 return 0;
f9e815b3
HW
162
163 type = nlh->nlmsg_type;
e6a7d3c0 164replay:
6b75e3e8 165 rcu_read_lock();
f9e815b3 166 ss = nfnetlink_get_subsys(type);
0ab43f84 167 if (!ss) {
95a5afca 168#ifdef CONFIG_MODULES
6b75e3e8 169 rcu_read_unlock();
37d2e7a2 170 request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
6b75e3e8 171 rcu_read_lock();
37d2e7a2 172 ss = nfnetlink_get_subsys(type);
0ab43f84
HW
173 if (!ss)
174#endif
6b75e3e8
ED
175 {
176 rcu_read_unlock();
1d00a4eb 177 return -EINVAL;
6b75e3e8 178 }
0ab43f84 179 }
f9e815b3
HW
180
181 nc = nfnetlink_find_client(type, ss);
6b75e3e8
ED
182 if (!nc) {
183 rcu_read_unlock();
1d00a4eb 184 return -EINVAL;
6b75e3e8 185 }
f9e815b3 186
f9e815b3 187 {
573ce260 188 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
b745d035 189 u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
f49c857f
PNA
190 struct nlattr *cda[ss->cb[cb_id].attr_count + 1];
191 struct nlattr *attr = (void *)nlh + min_len;
192 int attrlen = nlh->nlmsg_len - min_len;
c14b78e7 193 __u8 subsys_id = NFNL_SUBSYS_ID(type);
f49c857f 194
fceb6435 195 err = nla_parse(cda, ss->cb[cb_id].attr_count, attr, attrlen,
fe52145f 196 ss->cb[cb_id].policy, extack);
4009e188
TB
197 if (err < 0) {
198 rcu_read_unlock();
f49c857f 199 return err;
4009e188 200 }
601e68e1 201
6b75e3e8 202 if (nc->call_rcu) {
7b8002a1 203 err = nc->call_rcu(net, net->nfnl, skb, nlh,
04ba724b
PNA
204 (const struct nlattr **)cda,
205 extack);
6b75e3e8
ED
206 rcu_read_unlock();
207 } else {
208 rcu_read_unlock();
c14b78e7 209 nfnl_lock(subsys_id);
9c55d3b5 210 if (nfnl_dereference_protected(subsys_id) != ss ||
6b75e3e8
ED
211 nfnetlink_find_client(type, ss) != nc)
212 err = -EAGAIN;
59560a38 213 else if (nc->call)
7b8002a1 214 err = nc->call(net, net->nfnl, skb, nlh,
04ba724b
PNA
215 (const struct nlattr **)cda,
216 extack);
59560a38
TB
217 else
218 err = -EINVAL;
c14b78e7 219 nfnl_unlock(subsys_id);
6b75e3e8 220 }
e6a7d3c0
PNA
221 if (err == -EAGAIN)
222 goto replay;
223 return err;
f9e815b3 224 }
f9e815b3
HW
225}
226
cbb8125e
PNA
227struct nfnl_err {
228 struct list_head head;
229 struct nlmsghdr *nlh;
230 int err;
04ba724b 231 struct netlink_ext_ack extack;
cbb8125e
PNA
232};
233
04ba724b
PNA
234static int nfnl_err_add(struct list_head *list, struct nlmsghdr *nlh, int err,
235 const struct netlink_ext_ack *extack)
cbb8125e
PNA
236{
237 struct nfnl_err *nfnl_err;
238
239 nfnl_err = kmalloc(sizeof(struct nfnl_err), GFP_KERNEL);
240 if (nfnl_err == NULL)
241 return -ENOMEM;
242
243 nfnl_err->nlh = nlh;
244 nfnl_err->err = err;
04ba724b 245 nfnl_err->extack = *extack;
cbb8125e
PNA
246 list_add_tail(&nfnl_err->head, list);
247
248 return 0;
249}
250
251static void nfnl_err_del(struct nfnl_err *nfnl_err)
252{
253 list_del(&nfnl_err->head);
254 kfree(nfnl_err);
255}
256
257static void nfnl_err_reset(struct list_head *err_list)
258{
259 struct nfnl_err *nfnl_err, *next;
260
261 list_for_each_entry_safe(nfnl_err, next, err_list, head)
262 nfnl_err_del(nfnl_err);
263}
264
265static void nfnl_err_deliver(struct list_head *err_list, struct sk_buff *skb)
266{
267 struct nfnl_err *nfnl_err, *next;
268
269 list_for_each_entry_safe(nfnl_err, next, err_list, head) {
04ba724b
PNA
270 netlink_ack(skb, nfnl_err->nlh, nfnl_err->err,
271 &nfnl_err->extack);
cbb8125e
PNA
272 nfnl_err_del(nfnl_err);
273 }
274}
275
6742b9e3
PNA
276enum {
277 NFNL_BATCH_FAILURE = (1 << 0),
278 NFNL_BATCH_DONE = (1 << 1),
279 NFNL_BATCH_REPLAY = (1 << 2),
280};
281
0628b123 282static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
8c4d4e8b 283 u16 subsys_id, u32 genid)
0628b123 284{
0f816232 285 struct sk_buff *oskb = skb;
0628b123
PNA
286 struct net *net = sock_net(skb->sk);
287 const struct nfnetlink_subsystem *ss;
288 const struct nfnl_callback *nc;
04ba724b 289 struct netlink_ext_ack extack;
4eba8b78 290 LIST_HEAD(err_list);
6742b9e3 291 u32 status;
0628b123
PNA
292 int err;
293
294 if (subsys_id >= NFNL_SUBSYS_COUNT)
2d4bc933 295 return netlink_ack(skb, nlh, -EINVAL, NULL);
0628b123 296replay:
6742b9e3
PNA
297 status = 0;
298
0f816232
DJ
299 skb = netlink_skb_clone(oskb, GFP_KERNEL);
300 if (!skb)
2d4bc933 301 return netlink_ack(oskb, nlh, -ENOMEM, NULL);
0628b123 302
0628b123 303 nfnl_lock(subsys_id);
9c55d3b5 304 ss = nfnl_dereference_protected(subsys_id);
0628b123
PNA
305 if (!ss) {
306#ifdef CONFIG_MODULES
307 nfnl_unlock(subsys_id);
308 request_module("nfnetlink-subsys-%d", subsys_id);
309 nfnl_lock(subsys_id);
9c55d3b5 310 ss = nfnl_dereference_protected(subsys_id);
0628b123
PNA
311 if (!ss)
312#endif
313 {
314 nfnl_unlock(subsys_id);
2d4bc933 315 netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
0f816232 316 return kfree_skb(skb);
0628b123
PNA
317 }
318 }
319
320 if (!ss->commit || !ss->abort) {
321 nfnl_unlock(subsys_id);
2d4bc933 322 netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
ecd15dd7 323 return kfree_skb(skb);
0628b123
PNA
324 }
325
8c4d4e8b
PNA
326 if (genid && ss->valid_genid && !ss->valid_genid(net, genid)) {
327 nfnl_unlock(subsys_id);
2d4bc933 328 netlink_ack(oskb, nlh, -ERESTART, NULL);
8c4d4e8b
PNA
329 return kfree_skb(skb);
330 }
331
0628b123
PNA
332 while (skb->len >= nlmsg_total_size(0)) {
333 int msglen, type;
334
04ba724b 335 memset(&extack, 0, sizeof(extack));
0628b123
PNA
336 nlh = nlmsg_hdr(skb);
337 err = 0;
338
c58d6c93
PT
339 if (nlh->nlmsg_len < NLMSG_HDRLEN ||
340 skb->len < nlh->nlmsg_len ||
341 nlmsg_len(nlh) < sizeof(struct nfgenmsg)) {
342 nfnl_err_reset(&err_list);
343 status |= NFNL_BATCH_FAILURE;
344 goto done;
0628b123
PNA
345 }
346
347 /* Only requests are handled by the kernel */
348 if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
349 err = -EINVAL;
350 goto ack;
351 }
352
353 type = nlh->nlmsg_type;
354 if (type == NFNL_MSG_BATCH_BEGIN) {
355 /* Malformed: Batch begin twice */
cbb8125e 356 nfnl_err_reset(&err_list);
6742b9e3 357 status |= NFNL_BATCH_FAILURE;
0628b123
PNA
358 goto done;
359 } else if (type == NFNL_MSG_BATCH_END) {
6742b9e3 360 status |= NFNL_BATCH_DONE;
0628b123
PNA
361 goto done;
362 } else if (type < NLMSG_MIN_TYPE) {
363 err = -EINVAL;
364 goto ack;
365 }
366
367 /* We only accept a batch with messages for the same
368 * subsystem.
369 */
370 if (NFNL_SUBSYS_ID(type) != subsys_id) {
371 err = -EINVAL;
372 goto ack;
373 }
374
375 nc = nfnetlink_find_client(type, ss);
376 if (!nc) {
377 err = -EINVAL;
378 goto ack;
379 }
380
381 {
382 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
b745d035 383 u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
0628b123
PNA
384 struct nlattr *cda[ss->cb[cb_id].attr_count + 1];
385 struct nlattr *attr = (void *)nlh + min_len;
386 int attrlen = nlh->nlmsg_len - min_len;
387
fceb6435
JB
388 err = nla_parse(cda, ss->cb[cb_id].attr_count, attr,
389 attrlen, ss->cb[cb_id].policy, NULL);
0628b123
PNA
390 if (err < 0)
391 goto ack;
392
393 if (nc->call_batch) {
633c9a84 394 err = nc->call_batch(net, net->nfnl, skb, nlh,
04ba724b
PNA
395 (const struct nlattr **)cda,
396 &extack);
0628b123
PNA
397 }
398
399 /* The lock was released to autoload some module, we
400 * have to abort and start from scratch using the
401 * original skb.
402 */
403 if (err == -EAGAIN) {
6742b9e3
PNA
404 status |= NFNL_BATCH_REPLAY;
405 goto next;
0628b123
PNA
406 }
407 }
408ack:
409 if (nlh->nlmsg_flags & NLM_F_ACK || err) {
cbb8125e
PNA
410 /* Errors are delivered once the full batch has been
411 * processed, this avoids that the same error is
412 * reported several times when replaying the batch.
413 */
04ba724b 414 if (nfnl_err_add(&err_list, nlh, err, &extack) < 0) {
cbb8125e
PNA
415 /* We failed to enqueue an error, reset the
416 * list of errors and send OOM to userspace
417 * pointing to the batch header.
418 */
419 nfnl_err_reset(&err_list);
2d4bc933
JB
420 netlink_ack(oskb, nlmsg_hdr(oskb), -ENOMEM,
421 NULL);
6742b9e3 422 status |= NFNL_BATCH_FAILURE;
cbb8125e
PNA
423 goto done;
424 }
0628b123
PNA
425 /* We don't stop processing the batch on errors, thus,
426 * userspace gets all the errors that the batch
427 * triggers.
428 */
0628b123 429 if (err)
6742b9e3 430 status |= NFNL_BATCH_FAILURE;
0628b123 431 }
6742b9e3 432next:
0628b123
PNA
433 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
434 if (msglen > skb->len)
435 msglen = skb->len;
436 skb_pull(skb, msglen);
437 }
438done:
6742b9e3 439 if (status & NFNL_BATCH_REPLAY) {
5913beaf 440 ss->abort(net, oskb);
6742b9e3
PNA
441 nfnl_err_reset(&err_list);
442 nfnl_unlock(subsys_id);
443 kfree_skb(skb);
444 goto replay;
445 } else if (status == NFNL_BATCH_DONE) {
5913beaf 446 ss->commit(net, oskb);
6742b9e3 447 } else {
5913beaf 448 ss->abort(net, oskb);
6742b9e3 449 }
0628b123 450
cbb8125e 451 nfnl_err_deliver(&err_list, oskb);
0628b123 452 nfnl_unlock(subsys_id);
0f816232 453 kfree_skb(skb);
0628b123
PNA
454}
455
8c4d4e8b
PNA
456static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = {
457 [NFNL_BATCH_GENID] = { .type = NLA_U32 },
458};
459
48656835 460static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)
f9e815b3 461{
8c4d4e8b
PNA
462 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
463 struct nlattr *attr = (void *)nlh + min_len;
464 struct nlattr *cda[NFNL_BATCH_MAX + 1];
465 int attrlen = nlh->nlmsg_len - min_len;
48656835 466 struct nfgenmsg *nfgenmsg;
8c4d4e8b
PNA
467 int msglen, err;
468 u32 gen_id = 0;
b745d035 469 u16 res_id;
0628b123 470
48656835
PNA
471 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
472 if (msglen > skb->len)
473 msglen = skb->len;
474
f55ce7b0 475 if (skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))
48656835
PNA
476 return;
477
fceb6435
JB
478 err = nla_parse(cda, NFNL_BATCH_MAX, attr, attrlen, nfnl_batch_policy,
479 NULL);
8c4d4e8b 480 if (err < 0) {
2d4bc933 481 netlink_ack(skb, nlh, err, NULL);
8c4d4e8b
PNA
482 return;
483 }
484 if (cda[NFNL_BATCH_GENID])
485 gen_id = ntohl(nla_get_be32(cda[NFNL_BATCH_GENID]));
486
48656835
PNA
487 nfgenmsg = nlmsg_data(nlh);
488 skb_pull(skb, msglen);
489 /* Work around old nft using host byte order */
490 if (nfgenmsg->res_id == NFNL_SUBSYS_NFTABLES)
491 res_id = NFNL_SUBSYS_NFTABLES;
492 else
493 res_id = ntohs(nfgenmsg->res_id);
494
8c4d4e8b 495 nfnetlink_rcv_batch(skb, nlh, res_id, gen_id);
48656835
PNA
496}
497
498static void nfnetlink_rcv(struct sk_buff *skb)
499{
500 struct nlmsghdr *nlh = nlmsg_hdr(skb);
501
f55ce7b0
MJ
502 if (skb->len < NLMSG_HDRLEN ||
503 nlh->nlmsg_len < NLMSG_HDRLEN ||
0628b123
PNA
504 skb->len < nlh->nlmsg_len)
505 return;
506
90f62cf3 507 if (!netlink_net_capable(skb, CAP_NET_ADMIN)) {
2d4bc933 508 netlink_ack(skb, nlh, -EPERM, NULL);
cdbe7c2d
JB
509 return;
510 }
511
48656835
PNA
512 if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN)
513 nfnetlink_rcv_skb_batch(skb, nlh);
514 else
d4ef3835 515 netlink_rcv_skb(skb, nfnetlink_rcv_msg);
f9e815b3
HW
516}
517
03292745 518#ifdef CONFIG_MODULES
023e2cfa 519static int nfnetlink_bind(struct net *net, int group)
03292745
PNA
520{
521 const struct nfnetlink_subsystem *ss;
97840cb6
PNA
522 int type;
523
524 if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX)
62924af2 525 return 0;
97840cb6
PNA
526
527 type = nfnl_group2type[group];
03292745
PNA
528
529 rcu_read_lock();
dbc3617f 530 ss = nfnetlink_get_subsys(type << 8);
03292745 531 rcu_read_unlock();
bfe4bc71
RGB
532 if (!ss)
533 request_module("nfnetlink-subsys-%d", type);
4f520900 534 return 0;
03292745
PNA
535}
536#endif
537
cd8c20b6 538static int __net_init nfnetlink_net_init(struct net *net)
f9e815b3 539{
cd8c20b6 540 struct sock *nfnl;
a31f2d17
PNA
541 struct netlink_kernel_cfg cfg = {
542 .groups = NFNLGRP_MAX,
543 .input = nfnetlink_rcv,
03292745
PNA
544#ifdef CONFIG_MODULES
545 .bind = nfnetlink_bind,
546#endif
a31f2d17 547 };
cd8c20b6 548
9f00d977 549 nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg);
cd8c20b6
AD
550 if (!nfnl)
551 return -ENOMEM;
552 net->nfnl_stash = nfnl;
cf778b00 553 rcu_assign_pointer(net->nfnl, nfnl);
cd8c20b6 554 return 0;
f9e815b3
HW
555}
556
cd8c20b6 557static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list)
f9e815b3 558{
cd8c20b6 559 struct net *net;
f9e815b3 560
cd8c20b6 561 list_for_each_entry(net, net_exit_list, exit_list)
a9b3cd7f 562 RCU_INIT_POINTER(net->nfnl, NULL);
cd8c20b6
AD
563 synchronize_net();
564 list_for_each_entry(net, net_exit_list, exit_list)
565 netlink_kernel_release(net->nfnl_stash);
566}
f9e815b3 567
cd8c20b6
AD
568static struct pernet_operations nfnetlink_net_ops = {
569 .init = nfnetlink_net_init,
570 .exit_batch = nfnetlink_net_exit_batch,
571};
572
573static int __init nfnetlink_init(void)
574{
c14b78e7
PNA
575 int i;
576
97840cb6
PNA
577 for (i = NFNLGRP_NONE + 1; i <= NFNLGRP_MAX; i++)
578 BUG_ON(nfnl_group2type[i] == NFNL_SUBSYS_NONE);
579
c14b78e7
PNA
580 for (i=0; i<NFNL_SUBSYS_COUNT; i++)
581 mutex_init(&table[i].mutex);
582
654d0fbd 583 pr_info("Netfilter messages via NETLINK v%s.\n", nfversion);
cd8c20b6 584 return register_pernet_subsys(&nfnetlink_net_ops);
f9e815b3
HW
585}
586
cd8c20b6
AD
587static void __exit nfnetlink_exit(void)
588{
654d0fbd 589 pr_info("Removing netfilter NETLINK layer.\n");
cd8c20b6
AD
590 unregister_pernet_subsys(&nfnetlink_net_ops);
591}
f9e815b3
HW
592module_init(nfnetlink_init);
593module_exit(nfnetlink_exit);