perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / net / netfilter / nfnetlink.c
blob916913454624f2740212c62d1c0ce61bc8ae6f73
1 /* Netfilter messages via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>,
5 * (C) 2002-2005 by Harald Welte <laforge@gnumonks.org>
6 * (C) 2005-2017 by Pablo Neira Ayuso <pablo@netfilter.org>
8 * Initial netfilter messages via netlink development funded and
9 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
11 * Further development of this code funded by Astaro AG (http://www.astaro.com)
13 * This software may be used and distributed according to the terms
14 * of the GNU General Public License, incorporated herein by reference.
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/sockios.h>
23 #include <linux/net.h>
24 #include <linux/skbuff.h>
25 #include <linux/uaccess.h>
26 #include <net/sock.h>
27 #include <linux/init.h>
28 #include <linux/sched/signal.h>
30 #include <net/netlink.h>
31 #include <linux/netfilter/nfnetlink.h>
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
35 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
37 #define nfnl_dereference_protected(id) \
38 rcu_dereference_protected(table[(id)].subsys, \
39 lockdep_nfnl_is_held((id)))
41 #define NFNL_MAX_ATTR_COUNT 32
43 static struct {
44 struct mutex mutex;
45 const struct nfnetlink_subsystem __rcu *subsys;
46 } table[NFNL_SUBSYS_COUNT];
48 static const int nfnl_group2type[NFNLGRP_MAX+1] = {
49 [NFNLGRP_CONNTRACK_NEW] = NFNL_SUBSYS_CTNETLINK,
50 [NFNLGRP_CONNTRACK_UPDATE] = NFNL_SUBSYS_CTNETLINK,
51 [NFNLGRP_CONNTRACK_DESTROY] = NFNL_SUBSYS_CTNETLINK,
52 [NFNLGRP_CONNTRACK_EXP_NEW] = NFNL_SUBSYS_CTNETLINK_EXP,
53 [NFNLGRP_CONNTRACK_EXP_UPDATE] = NFNL_SUBSYS_CTNETLINK_EXP,
54 [NFNLGRP_CONNTRACK_EXP_DESTROY] = NFNL_SUBSYS_CTNETLINK_EXP,
55 [NFNLGRP_NFTABLES] = NFNL_SUBSYS_NFTABLES,
56 [NFNLGRP_ACCT_QUOTA] = NFNL_SUBSYS_ACCT,
57 [NFNLGRP_NFTRACE] = NFNL_SUBSYS_NFTABLES,
60 void nfnl_lock(__u8 subsys_id)
62 mutex_lock(&table[subsys_id].mutex);
64 EXPORT_SYMBOL_GPL(nfnl_lock);
66 void nfnl_unlock(__u8 subsys_id)
68 mutex_unlock(&table[subsys_id].mutex);
70 EXPORT_SYMBOL_GPL(nfnl_unlock);
72 #ifdef CONFIG_PROVE_LOCKING
73 bool lockdep_nfnl_is_held(u8 subsys_id)
75 return lockdep_is_held(&table[subsys_id].mutex);
77 EXPORT_SYMBOL_GPL(lockdep_nfnl_is_held);
78 #endif
80 int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n)
82 u8 cb_id;
84 /* Sanity-check attr_count size to avoid stack buffer overflow. */
85 for (cb_id = 0; cb_id < n->cb_count; cb_id++)
86 if (WARN_ON(n->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT))
87 return -EINVAL;
89 nfnl_lock(n->subsys_id);
90 if (table[n->subsys_id].subsys) {
91 nfnl_unlock(n->subsys_id);
92 return -EBUSY;
94 rcu_assign_pointer(table[n->subsys_id].subsys, n);
95 nfnl_unlock(n->subsys_id);
97 return 0;
99 EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
101 int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n)
103 nfnl_lock(n->subsys_id);
104 table[n->subsys_id].subsys = NULL;
105 nfnl_unlock(n->subsys_id);
106 synchronize_rcu();
107 return 0;
109 EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
111 static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u16 type)
113 u8 subsys_id = NFNL_SUBSYS_ID(type);
115 if (subsys_id >= NFNL_SUBSYS_COUNT)
116 return NULL;
118 return rcu_dereference(table[subsys_id].subsys);
121 static inline const struct nfnl_callback *
122 nfnetlink_find_client(u16 type, const struct nfnetlink_subsystem *ss)
124 u8 cb_id = NFNL_MSG_TYPE(type);
126 if (cb_id >= ss->cb_count)
127 return NULL;
129 return &ss->cb[cb_id];
132 int nfnetlink_has_listeners(struct net *net, unsigned int group)
134 return netlink_has_listeners(net->nfnl, group);
136 EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
138 int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid,
139 unsigned int group, int echo, gfp_t flags)
141 return nlmsg_notify(net->nfnl, skb, portid, group, echo, flags);
143 EXPORT_SYMBOL_GPL(nfnetlink_send);
145 int nfnetlink_set_err(struct net *net, u32 portid, u32 group, int error)
147 return netlink_set_err(net->nfnl, portid, group, error);
149 EXPORT_SYMBOL_GPL(nfnetlink_set_err);
151 int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u32 portid,
152 int flags)
154 return netlink_unicast(net->nfnl, skb, portid, flags);
156 EXPORT_SYMBOL_GPL(nfnetlink_unicast);
158 /* Process one complete nfnetlink message. */
159 static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
160 struct netlink_ext_ack *extack)
162 struct net *net = sock_net(skb->sk);
163 const struct nfnl_callback *nc;
164 const struct nfnetlink_subsystem *ss;
165 int type, err;
167 /* All the messages must at least contain nfgenmsg */
168 if (nlmsg_len(nlh) < sizeof(struct nfgenmsg))
169 return 0;
171 type = nlh->nlmsg_type;
172 replay:
173 rcu_read_lock();
174 ss = nfnetlink_get_subsys(type);
175 if (!ss) {
176 #ifdef CONFIG_MODULES
177 rcu_read_unlock();
178 request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
179 rcu_read_lock();
180 ss = nfnetlink_get_subsys(type);
181 if (!ss)
182 #endif
184 rcu_read_unlock();
185 return -EINVAL;
189 nc = nfnetlink_find_client(type, ss);
190 if (!nc) {
191 rcu_read_unlock();
192 return -EINVAL;
196 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
197 u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
198 struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1];
199 struct nlattr *attr = (void *)nlh + min_len;
200 int attrlen = nlh->nlmsg_len - min_len;
201 __u8 subsys_id = NFNL_SUBSYS_ID(type);
203 /* Sanity-check NFNL_MAX_ATTR_COUNT */
204 if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) {
205 rcu_read_unlock();
206 return -ENOMEM;
209 err = nla_parse(cda, ss->cb[cb_id].attr_count, attr, attrlen,
210 ss->cb[cb_id].policy, extack);
211 if (err < 0) {
212 rcu_read_unlock();
213 return err;
216 if (nc->call_rcu) {
217 err = nc->call_rcu(net, net->nfnl, skb, nlh,
218 (const struct nlattr **)cda,
219 extack);
220 rcu_read_unlock();
221 } else {
222 rcu_read_unlock();
223 nfnl_lock(subsys_id);
224 if (nfnl_dereference_protected(subsys_id) != ss ||
225 nfnetlink_find_client(type, ss) != nc)
226 err = -EAGAIN;
227 else if (nc->call)
228 err = nc->call(net, net->nfnl, skb, nlh,
229 (const struct nlattr **)cda,
230 extack);
231 else
232 err = -EINVAL;
233 nfnl_unlock(subsys_id);
235 if (err == -EAGAIN)
236 goto replay;
237 return err;
241 struct nfnl_err {
242 struct list_head head;
243 struct nlmsghdr *nlh;
244 int err;
245 struct netlink_ext_ack extack;
248 static int nfnl_err_add(struct list_head *list, struct nlmsghdr *nlh, int err,
249 const struct netlink_ext_ack *extack)
251 struct nfnl_err *nfnl_err;
253 nfnl_err = kmalloc(sizeof(struct nfnl_err), GFP_KERNEL);
254 if (nfnl_err == NULL)
255 return -ENOMEM;
257 nfnl_err->nlh = nlh;
258 nfnl_err->err = err;
259 nfnl_err->extack = *extack;
260 list_add_tail(&nfnl_err->head, list);
262 return 0;
265 static void nfnl_err_del(struct nfnl_err *nfnl_err)
267 list_del(&nfnl_err->head);
268 kfree(nfnl_err);
271 static void nfnl_err_reset(struct list_head *err_list)
273 struct nfnl_err *nfnl_err, *next;
275 list_for_each_entry_safe(nfnl_err, next, err_list, head)
276 nfnl_err_del(nfnl_err);
279 static void nfnl_err_deliver(struct list_head *err_list, struct sk_buff *skb)
281 struct nfnl_err *nfnl_err, *next;
283 list_for_each_entry_safe(nfnl_err, next, err_list, head) {
284 netlink_ack(skb, nfnl_err->nlh, nfnl_err->err,
285 &nfnl_err->extack);
286 nfnl_err_del(nfnl_err);
290 enum {
291 NFNL_BATCH_FAILURE = (1 << 0),
292 NFNL_BATCH_DONE = (1 << 1),
293 NFNL_BATCH_REPLAY = (1 << 2),
296 static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
297 u16 subsys_id, u32 genid)
299 struct sk_buff *oskb = skb;
300 struct net *net = sock_net(skb->sk);
301 const struct nfnetlink_subsystem *ss;
302 const struct nfnl_callback *nc;
303 struct netlink_ext_ack extack;
304 LIST_HEAD(err_list);
305 u32 status;
306 int err;
308 if (subsys_id >= NFNL_SUBSYS_COUNT)
309 return netlink_ack(skb, nlh, -EINVAL, NULL);
310 replay:
311 status = 0;
313 skb = netlink_skb_clone(oskb, GFP_KERNEL);
314 if (!skb)
315 return netlink_ack(oskb, nlh, -ENOMEM, NULL);
317 nfnl_lock(subsys_id);
318 ss = nfnl_dereference_protected(subsys_id);
319 if (!ss) {
320 #ifdef CONFIG_MODULES
321 nfnl_unlock(subsys_id);
322 request_module("nfnetlink-subsys-%d", subsys_id);
323 nfnl_lock(subsys_id);
324 ss = nfnl_dereference_protected(subsys_id);
325 if (!ss)
326 #endif
328 nfnl_unlock(subsys_id);
329 netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
330 return kfree_skb(skb);
334 if (!ss->valid_genid || !ss->commit || !ss->abort) {
335 nfnl_unlock(subsys_id);
336 netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
337 return kfree_skb(skb);
340 if (!try_module_get(ss->owner)) {
341 nfnl_unlock(subsys_id);
342 netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
343 return kfree_skb(skb);
346 if (!ss->valid_genid(net, genid)) {
347 module_put(ss->owner);
348 nfnl_unlock(subsys_id);
349 netlink_ack(oskb, nlh, -ERESTART, NULL);
350 return kfree_skb(skb);
353 nfnl_unlock(subsys_id);
355 while (skb->len >= nlmsg_total_size(0)) {
356 int msglen, type;
358 if (fatal_signal_pending(current)) {
359 nfnl_err_reset(&err_list);
360 err = -EINTR;
361 status = NFNL_BATCH_FAILURE;
362 goto done;
365 memset(&extack, 0, sizeof(extack));
366 nlh = nlmsg_hdr(skb);
367 err = 0;
369 if (nlh->nlmsg_len < NLMSG_HDRLEN ||
370 skb->len < nlh->nlmsg_len ||
371 nlmsg_len(nlh) < sizeof(struct nfgenmsg)) {
372 nfnl_err_reset(&err_list);
373 status |= NFNL_BATCH_FAILURE;
374 goto done;
377 /* Only requests are handled by the kernel */
378 if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
379 err = -EINVAL;
380 goto ack;
383 type = nlh->nlmsg_type;
384 if (type == NFNL_MSG_BATCH_BEGIN) {
385 /* Malformed: Batch begin twice */
386 nfnl_err_reset(&err_list);
387 status |= NFNL_BATCH_FAILURE;
388 goto done;
389 } else if (type == NFNL_MSG_BATCH_END) {
390 status |= NFNL_BATCH_DONE;
391 goto done;
392 } else if (type < NLMSG_MIN_TYPE) {
393 err = -EINVAL;
394 goto ack;
397 /* We only accept a batch with messages for the same
398 * subsystem.
400 if (NFNL_SUBSYS_ID(type) != subsys_id) {
401 err = -EINVAL;
402 goto ack;
405 nc = nfnetlink_find_client(type, ss);
406 if (!nc) {
407 err = -EINVAL;
408 goto ack;
412 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
413 u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
414 struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1];
415 struct nlattr *attr = (void *)nlh + min_len;
416 int attrlen = nlh->nlmsg_len - min_len;
418 /* Sanity-check NFTA_MAX_ATTR */
419 if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) {
420 err = -ENOMEM;
421 goto ack;
424 err = nla_parse(cda, ss->cb[cb_id].attr_count, attr,
425 attrlen, ss->cb[cb_id].policy, NULL);
426 if (err < 0)
427 goto ack;
429 if (nc->call_batch) {
430 err = nc->call_batch(net, net->nfnl, skb, nlh,
431 (const struct nlattr **)cda,
432 &extack);
435 /* The lock was released to autoload some module, we
436 * have to abort and start from scratch using the
437 * original skb.
439 if (err == -EAGAIN) {
440 status |= NFNL_BATCH_REPLAY;
441 goto done;
444 ack:
445 if (nlh->nlmsg_flags & NLM_F_ACK || err) {
446 /* Errors are delivered once the full batch has been
447 * processed, this avoids that the same error is
448 * reported several times when replaying the batch.
450 if (nfnl_err_add(&err_list, nlh, err, &extack) < 0) {
451 /* We failed to enqueue an error, reset the
452 * list of errors and send OOM to userspace
453 * pointing to the batch header.
455 nfnl_err_reset(&err_list);
456 netlink_ack(oskb, nlmsg_hdr(oskb), -ENOMEM,
457 NULL);
458 status |= NFNL_BATCH_FAILURE;
459 goto done;
461 /* We don't stop processing the batch on errors, thus,
462 * userspace gets all the errors that the batch
463 * triggers.
465 if (err)
466 status |= NFNL_BATCH_FAILURE;
469 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
470 if (msglen > skb->len)
471 msglen = skb->len;
472 skb_pull(skb, msglen);
474 done:
475 if (status & NFNL_BATCH_REPLAY) {
476 ss->abort(net, oskb);
477 nfnl_err_reset(&err_list);
478 kfree_skb(skb);
479 module_put(ss->owner);
480 goto replay;
481 } else if (status == NFNL_BATCH_DONE) {
482 err = ss->commit(net, oskb);
483 if (err == -EAGAIN) {
484 status |= NFNL_BATCH_REPLAY;
485 goto done;
486 } else if (err) {
487 ss->abort(net, oskb);
488 netlink_ack(oskb, nlmsg_hdr(oskb), err, NULL);
490 } else {
491 ss->abort(net, oskb);
493 if (ss->cleanup)
494 ss->cleanup(net);
496 nfnl_err_deliver(&err_list, oskb);
497 kfree_skb(skb);
498 module_put(ss->owner);
501 static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = {
502 [NFNL_BATCH_GENID] = { .type = NLA_U32 },
505 static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)
507 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
508 struct nlattr *attr = (void *)nlh + min_len;
509 struct nlattr *cda[NFNL_BATCH_MAX + 1];
510 int attrlen = nlh->nlmsg_len - min_len;
511 struct nfgenmsg *nfgenmsg;
512 int msglen, err;
513 u32 gen_id = 0;
514 u16 res_id;
516 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
517 if (msglen > skb->len)
518 msglen = skb->len;
520 if (skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))
521 return;
523 err = nla_parse(cda, NFNL_BATCH_MAX, attr, attrlen, nfnl_batch_policy,
524 NULL);
525 if (err < 0) {
526 netlink_ack(skb, nlh, err, NULL);
527 return;
529 if (cda[NFNL_BATCH_GENID])
530 gen_id = ntohl(nla_get_be32(cda[NFNL_BATCH_GENID]));
532 nfgenmsg = nlmsg_data(nlh);
533 skb_pull(skb, msglen);
534 /* Work around old nft using host byte order */
535 if (nfgenmsg->res_id == NFNL_SUBSYS_NFTABLES)
536 res_id = NFNL_SUBSYS_NFTABLES;
537 else
538 res_id = ntohs(nfgenmsg->res_id);
540 nfnetlink_rcv_batch(skb, nlh, res_id, gen_id);
543 static void nfnetlink_rcv(struct sk_buff *skb)
545 struct nlmsghdr *nlh = nlmsg_hdr(skb);
547 if (skb->len < NLMSG_HDRLEN ||
548 nlh->nlmsg_len < NLMSG_HDRLEN ||
549 skb->len < nlh->nlmsg_len)
550 return;
552 if (!netlink_net_capable(skb, CAP_NET_ADMIN)) {
553 netlink_ack(skb, nlh, -EPERM, NULL);
554 return;
557 if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN)
558 nfnetlink_rcv_skb_batch(skb, nlh);
559 else
560 netlink_rcv_skb(skb, nfnetlink_rcv_msg);
563 #ifdef CONFIG_MODULES
564 static int nfnetlink_bind(struct net *net, int group)
566 const struct nfnetlink_subsystem *ss;
567 int type;
569 if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX)
570 return 0;
572 type = nfnl_group2type[group];
574 rcu_read_lock();
575 ss = nfnetlink_get_subsys(type << 8);
576 rcu_read_unlock();
577 if (!ss)
578 request_module("nfnetlink-subsys-%d", type);
579 return 0;
581 #endif
583 static int __net_init nfnetlink_net_init(struct net *net)
585 struct sock *nfnl;
586 struct netlink_kernel_cfg cfg = {
587 .groups = NFNLGRP_MAX,
588 .input = nfnetlink_rcv,
589 #ifdef CONFIG_MODULES
590 .bind = nfnetlink_bind,
591 #endif
594 nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg);
595 if (!nfnl)
596 return -ENOMEM;
597 net->nfnl_stash = nfnl;
598 rcu_assign_pointer(net->nfnl, nfnl);
599 return 0;
602 static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list)
604 struct net *net;
606 list_for_each_entry(net, net_exit_list, exit_list)
607 RCU_INIT_POINTER(net->nfnl, NULL);
608 synchronize_net();
609 list_for_each_entry(net, net_exit_list, exit_list)
610 netlink_kernel_release(net->nfnl_stash);
613 static struct pernet_operations nfnetlink_net_ops = {
614 .init = nfnetlink_net_init,
615 .exit_batch = nfnetlink_net_exit_batch,
618 static int __init nfnetlink_init(void)
620 int i;
622 for (i = NFNLGRP_NONE + 1; i <= NFNLGRP_MAX; i++)
623 BUG_ON(nfnl_group2type[i] == NFNL_SUBSYS_NONE);
625 for (i=0; i<NFNL_SUBSYS_COUNT; i++)
626 mutex_init(&table[i].mutex);
628 return register_pernet_subsys(&nfnetlink_net_ops);
631 static void __exit nfnetlink_exit(void)
633 unregister_pernet_subsys(&nfnetlink_net_ops);
635 module_init(nfnetlink_init);
636 module_exit(nfnetlink_exit);