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>
27 #include <linux/init.h>
28 #include <linux/sched/signal.h>
30 #include <net/netlink.h>
31 #include <net/netns/generic.h>
32 #include <linux/netfilter/nfnetlink.h>
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
36 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
37 MODULE_DESCRIPTION("Netfilter messages via netlink socket");
39 #define nfnl_dereference_protected(id) \
40 rcu_dereference_protected(table[(id)].subsys, \
41 lockdep_nfnl_is_held((id)))
43 #define NFNL_MAX_ATTR_COUNT 32
45 static unsigned int nfnetlink_pernet_id __read_mostly;
53 const struct nfnetlink_subsystem __rcu *subsys;
54 } table[NFNL_SUBSYS_COUNT];
56 static struct lock_class_key nfnl_lockdep_keys[NFNL_SUBSYS_COUNT];
58 static const char *const nfnl_lockdep_names[NFNL_SUBSYS_COUNT] = {
59 [NFNL_SUBSYS_NONE] = "nfnl_subsys_none",
60 [NFNL_SUBSYS_CTNETLINK] = "nfnl_subsys_ctnetlink",
61 [NFNL_SUBSYS_CTNETLINK_EXP] = "nfnl_subsys_ctnetlink_exp",
62 [NFNL_SUBSYS_QUEUE] = "nfnl_subsys_queue",
63 [NFNL_SUBSYS_ULOG] = "nfnl_subsys_ulog",
64 [NFNL_SUBSYS_OSF] = "nfnl_subsys_osf",
65 [NFNL_SUBSYS_IPSET] = "nfnl_subsys_ipset",
66 [NFNL_SUBSYS_ACCT] = "nfnl_subsys_acct",
67 [NFNL_SUBSYS_CTNETLINK_TIMEOUT] = "nfnl_subsys_cttimeout",
68 [NFNL_SUBSYS_CTHELPER] = "nfnl_subsys_cthelper",
69 [NFNL_SUBSYS_NFTABLES] = "nfnl_subsys_nftables",
70 [NFNL_SUBSYS_NFT_COMPAT] = "nfnl_subsys_nftcompat",
73 static const int nfnl_group2type[NFNLGRP_MAX+1] = {
74 [NFNLGRP_CONNTRACK_NEW] = NFNL_SUBSYS_CTNETLINK,
75 [NFNLGRP_CONNTRACK_UPDATE] = NFNL_SUBSYS_CTNETLINK,
76 [NFNLGRP_CONNTRACK_DESTROY] = NFNL_SUBSYS_CTNETLINK,
77 [NFNLGRP_CONNTRACK_EXP_NEW] = NFNL_SUBSYS_CTNETLINK_EXP,
78 [NFNLGRP_CONNTRACK_EXP_UPDATE] = NFNL_SUBSYS_CTNETLINK_EXP,
79 [NFNLGRP_CONNTRACK_EXP_DESTROY] = NFNL_SUBSYS_CTNETLINK_EXP,
80 [NFNLGRP_NFTABLES] = NFNL_SUBSYS_NFTABLES,
81 [NFNLGRP_ACCT_QUOTA] = NFNL_SUBSYS_ACCT,
82 [NFNLGRP_NFTRACE] = NFNL_SUBSYS_NFTABLES,
85 static struct nfnl_net *nfnl_pernet(struct net *net)
87 return net_generic(net, nfnetlink_pernet_id);
90 void nfnl_lock(__u8 subsys_id)
92 mutex_lock(&table[subsys_id].mutex);
94 EXPORT_SYMBOL_GPL(nfnl_lock);
96 void nfnl_unlock(__u8 subsys_id)
98 mutex_unlock(&table[subsys_id].mutex);
100 EXPORT_SYMBOL_GPL(nfnl_unlock);
102 #ifdef CONFIG_PROVE_LOCKING
103 bool lockdep_nfnl_is_held(u8 subsys_id)
105 return lockdep_is_held(&table[subsys_id].mutex);
107 EXPORT_SYMBOL_GPL(lockdep_nfnl_is_held);
110 int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n)
114 /* Sanity-check attr_count size to avoid stack buffer overflow. */
115 for (cb_id = 0; cb_id < n->cb_count; cb_id++)
116 if (WARN_ON(n->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT))
119 nfnl_lock(n->subsys_id);
120 if (table[n->subsys_id].subsys) {
121 nfnl_unlock(n->subsys_id);
124 rcu_assign_pointer(table[n->subsys_id].subsys, n);
125 nfnl_unlock(n->subsys_id);
129 EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
131 int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n)
133 nfnl_lock(n->subsys_id);
134 table[n->subsys_id].subsys = NULL;
135 nfnl_unlock(n->subsys_id);
139 EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
141 static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u16 type)
143 u8 subsys_id = NFNL_SUBSYS_ID(type);
145 if (subsys_id >= NFNL_SUBSYS_COUNT)
148 return rcu_dereference(table[subsys_id].subsys);
151 static inline const struct nfnl_callback *
152 nfnetlink_find_client(u16 type, const struct nfnetlink_subsystem *ss)
154 u8 cb_id = NFNL_MSG_TYPE(type);
156 if (cb_id >= ss->cb_count)
159 return &ss->cb[cb_id];
162 int nfnetlink_has_listeners(struct net *net, unsigned int group)
164 struct nfnl_net *nfnlnet = nfnl_pernet(net);
166 return netlink_has_listeners(nfnlnet->nfnl, group);
168 EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
170 int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid,
171 unsigned int group, int echo, gfp_t flags)
173 struct nfnl_net *nfnlnet = nfnl_pernet(net);
175 return nlmsg_notify(nfnlnet->nfnl, skb, portid, group, echo, flags);
177 EXPORT_SYMBOL_GPL(nfnetlink_send);
179 int nfnetlink_set_err(struct net *net, u32 portid, u32 group, int error)
181 struct nfnl_net *nfnlnet = nfnl_pernet(net);
183 return netlink_set_err(nfnlnet->nfnl, portid, group, error);
185 EXPORT_SYMBOL_GPL(nfnetlink_set_err);
187 int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u32 portid)
189 struct nfnl_net *nfnlnet = nfnl_pernet(net);
192 err = nlmsg_unicast(nfnlnet->nfnl, skb, portid);
198 EXPORT_SYMBOL_GPL(nfnetlink_unicast);
200 void nfnetlink_broadcast(struct net *net, struct sk_buff *skb, __u32 portid,
201 __u32 group, gfp_t allocation)
203 struct nfnl_net *nfnlnet = nfnl_pernet(net);
205 netlink_broadcast(nfnlnet->nfnl, skb, portid, group, allocation);
207 EXPORT_SYMBOL_GPL(nfnetlink_broadcast);
209 /* Process one complete nfnetlink message. */
210 static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
211 struct netlink_ext_ack *extack)
213 struct net *net = sock_net(skb->sk);
214 const struct nfnl_callback *nc;
215 const struct nfnetlink_subsystem *ss;
218 /* All the messages must at least contain nfgenmsg */
219 if (nlmsg_len(nlh) < sizeof(struct nfgenmsg))
222 type = nlh->nlmsg_type;
226 ss = nfnetlink_get_subsys(type);
228 #ifdef CONFIG_MODULES
230 request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
232 ss = nfnetlink_get_subsys(type);
241 nc = nfnetlink_find_client(type, ss);
248 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
249 struct nfnl_net *nfnlnet = nfnl_pernet(net);
250 u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
251 struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1];
252 struct nlattr *attr = (void *)nlh + min_len;
253 int attrlen = nlh->nlmsg_len - min_len;
254 __u8 subsys_id = NFNL_SUBSYS_ID(type);
256 /* Sanity-check NFNL_MAX_ATTR_COUNT */
257 if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) {
262 err = nla_parse_deprecated(cda, ss->cb[cb_id].attr_count,
264 ss->cb[cb_id].policy, extack);
271 err = nc->call_rcu(net, nfnlnet->nfnl, skb, nlh,
272 (const struct nlattr **)cda,
277 nfnl_lock(subsys_id);
278 if (nfnl_dereference_protected(subsys_id) != ss ||
279 nfnetlink_find_client(type, ss) != nc)
282 err = nc->call(net, nfnlnet->nfnl, skb, nlh,
283 (const struct nlattr **)cda,
287 nfnl_unlock(subsys_id);
296 struct list_head head;
297 struct nlmsghdr *nlh;
299 struct netlink_ext_ack extack;
302 static int nfnl_err_add(struct list_head *list, struct nlmsghdr *nlh, int err,
303 const struct netlink_ext_ack *extack)
305 struct nfnl_err *nfnl_err;
307 nfnl_err = kmalloc(sizeof(struct nfnl_err), GFP_KERNEL);
308 if (nfnl_err == NULL)
313 nfnl_err->extack = *extack;
314 list_add_tail(&nfnl_err->head, list);
319 static void nfnl_err_del(struct nfnl_err *nfnl_err)
321 list_del(&nfnl_err->head);
325 static void nfnl_err_reset(struct list_head *err_list)
327 struct nfnl_err *nfnl_err, *next;
329 list_for_each_entry_safe(nfnl_err, next, err_list, head)
330 nfnl_err_del(nfnl_err);
333 static void nfnl_err_deliver(struct list_head *err_list, struct sk_buff *skb)
335 struct nfnl_err *nfnl_err, *next;
337 list_for_each_entry_safe(nfnl_err, next, err_list, head) {
338 netlink_ack(skb, nfnl_err->nlh, nfnl_err->err,
340 nfnl_err_del(nfnl_err);
345 NFNL_BATCH_FAILURE = (1 << 0),
346 NFNL_BATCH_DONE = (1 << 1),
347 NFNL_BATCH_REPLAY = (1 << 2),
350 static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
351 u16 subsys_id, u32 genid)
353 struct sk_buff *oskb = skb;
354 struct net *net = sock_net(skb->sk);
355 const struct nfnetlink_subsystem *ss;
356 const struct nfnl_callback *nc;
357 struct netlink_ext_ack extack;
362 if (subsys_id >= NFNL_SUBSYS_COUNT)
363 return netlink_ack(skb, nlh, -EINVAL, NULL);
367 skb = netlink_skb_clone(oskb, GFP_KERNEL);
369 return netlink_ack(oskb, nlh, -ENOMEM, NULL);
371 nfnl_lock(subsys_id);
372 ss = nfnl_dereference_protected(subsys_id);
374 #ifdef CONFIG_MODULES
375 nfnl_unlock(subsys_id);
376 request_module("nfnetlink-subsys-%d", subsys_id);
377 nfnl_lock(subsys_id);
378 ss = nfnl_dereference_protected(subsys_id);
382 nfnl_unlock(subsys_id);
383 netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
384 return kfree_skb(skb);
388 if (!ss->valid_genid || !ss->commit || !ss->abort) {
389 nfnl_unlock(subsys_id);
390 netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
391 return kfree_skb(skb);
394 if (!try_module_get(ss->owner)) {
395 nfnl_unlock(subsys_id);
396 netlink_ack(oskb, nlh, -EOPNOTSUPP, NULL);
397 return kfree_skb(skb);
400 if (!ss->valid_genid(net, genid)) {
401 module_put(ss->owner);
402 nfnl_unlock(subsys_id);
403 netlink_ack(oskb, nlh, -ERESTART, NULL);
404 return kfree_skb(skb);
407 nfnl_unlock(subsys_id);
409 while (skb->len >= nlmsg_total_size(0)) {
412 if (fatal_signal_pending(current)) {
413 nfnl_err_reset(&err_list);
415 status = NFNL_BATCH_FAILURE;
419 memset(&extack, 0, sizeof(extack));
420 nlh = nlmsg_hdr(skb);
423 if (nlh->nlmsg_len < NLMSG_HDRLEN ||
424 skb->len < nlh->nlmsg_len ||
425 nlmsg_len(nlh) < sizeof(struct nfgenmsg)) {
426 nfnl_err_reset(&err_list);
427 status |= NFNL_BATCH_FAILURE;
431 /* Only requests are handled by the kernel */
432 if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
437 type = nlh->nlmsg_type;
438 if (type == NFNL_MSG_BATCH_BEGIN) {
439 /* Malformed: Batch begin twice */
440 nfnl_err_reset(&err_list);
441 status |= NFNL_BATCH_FAILURE;
443 } else if (type == NFNL_MSG_BATCH_END) {
444 status |= NFNL_BATCH_DONE;
446 } else if (type < NLMSG_MIN_TYPE) {
451 /* We only accept a batch with messages for the same
454 if (NFNL_SUBSYS_ID(type) != subsys_id) {
459 nc = nfnetlink_find_client(type, ss);
466 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
467 u8 cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
468 struct nlattr *cda[NFNL_MAX_ATTR_COUNT + 1];
469 struct nlattr *attr = (void *)nlh + min_len;
470 int attrlen = nlh->nlmsg_len - min_len;
472 /* Sanity-check NFTA_MAX_ATTR */
473 if (ss->cb[cb_id].attr_count > NFNL_MAX_ATTR_COUNT) {
478 err = nla_parse_deprecated(cda,
479 ss->cb[cb_id].attr_count,
481 ss->cb[cb_id].policy, NULL);
485 if (nc->call_batch) {
486 struct nfnl_net *nfnlnet = nfnl_pernet(net);
488 err = nc->call_batch(net, nfnlnet->nfnl, skb, nlh,
489 (const struct nlattr **)cda,
493 /* The lock was released to autoload some module, we
494 * have to abort and start from scratch using the
497 if (err == -EAGAIN) {
498 status |= NFNL_BATCH_REPLAY;
503 if (nlh->nlmsg_flags & NLM_F_ACK || err) {
504 /* Errors are delivered once the full batch has been
505 * processed, this avoids that the same error is
506 * reported several times when replaying the batch.
508 if (nfnl_err_add(&err_list, nlh, err, &extack) < 0) {
509 /* We failed to enqueue an error, reset the
510 * list of errors and send OOM to userspace
511 * pointing to the batch header.
513 nfnl_err_reset(&err_list);
514 netlink_ack(oskb, nlmsg_hdr(oskb), -ENOMEM,
516 status |= NFNL_BATCH_FAILURE;
519 /* We don't stop processing the batch on errors, thus,
520 * userspace gets all the errors that the batch
524 status |= NFNL_BATCH_FAILURE;
527 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
528 if (msglen > skb->len)
530 skb_pull(skb, msglen);
533 if (status & NFNL_BATCH_REPLAY) {
534 ss->abort(net, oskb, NFNL_ABORT_AUTOLOAD);
535 nfnl_err_reset(&err_list);
537 module_put(ss->owner);
539 } else if (status == NFNL_BATCH_DONE) {
540 err = ss->commit(net, oskb);
541 if (err == -EAGAIN) {
542 status |= NFNL_BATCH_REPLAY;
545 ss->abort(net, oskb, NFNL_ABORT_NONE);
546 netlink_ack(oskb, nlmsg_hdr(oskb), err, NULL);
549 enum nfnl_abort_action abort_action;
551 if (status & NFNL_BATCH_FAILURE)
552 abort_action = NFNL_ABORT_NONE;
554 abort_action = NFNL_ABORT_VALIDATE;
556 err = ss->abort(net, oskb, abort_action);
557 if (err == -EAGAIN) {
558 nfnl_err_reset(&err_list);
560 module_put(ss->owner);
561 status |= NFNL_BATCH_FAILURE;
568 nfnl_err_deliver(&err_list, oskb);
570 module_put(ss->owner);
573 static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX + 1] = {
574 [NFNL_BATCH_GENID] = { .type = NLA_U32 },
577 static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)
579 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
580 struct nlattr *attr = (void *)nlh + min_len;
581 struct nlattr *cda[NFNL_BATCH_MAX + 1];
582 int attrlen = nlh->nlmsg_len - min_len;
583 struct nfgenmsg *nfgenmsg;
588 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
589 if (msglen > skb->len)
592 if (skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))
595 err = nla_parse_deprecated(cda, NFNL_BATCH_MAX, attr, attrlen,
596 nfnl_batch_policy, NULL);
598 netlink_ack(skb, nlh, err, NULL);
601 if (cda[NFNL_BATCH_GENID])
602 gen_id = ntohl(nla_get_be32(cda[NFNL_BATCH_GENID]));
604 nfgenmsg = nlmsg_data(nlh);
605 skb_pull(skb, msglen);
606 /* Work around old nft using host byte order */
607 if (nfgenmsg->res_id == NFNL_SUBSYS_NFTABLES)
608 res_id = NFNL_SUBSYS_NFTABLES;
610 res_id = ntohs(nfgenmsg->res_id);
612 nfnetlink_rcv_batch(skb, nlh, res_id, gen_id);
615 static void nfnetlink_rcv(struct sk_buff *skb)
617 struct nlmsghdr *nlh = nlmsg_hdr(skb);
619 if (skb->len < NLMSG_HDRLEN ||
620 nlh->nlmsg_len < NLMSG_HDRLEN ||
621 skb->len < nlh->nlmsg_len)
624 if (!netlink_net_capable(skb, CAP_NET_ADMIN)) {
625 netlink_ack(skb, nlh, -EPERM, NULL);
629 if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN)
630 nfnetlink_rcv_skb_batch(skb, nlh);
632 netlink_rcv_skb(skb, nfnetlink_rcv_msg);
635 #ifdef CONFIG_MODULES
636 static int nfnetlink_bind(struct net *net, int group)
638 const struct nfnetlink_subsystem *ss;
641 if (group <= NFNLGRP_NONE || group > NFNLGRP_MAX)
644 type = nfnl_group2type[group];
647 ss = nfnetlink_get_subsys(type << 8);
650 request_module_nowait("nfnetlink-subsys-%d", type);
655 static int __net_init nfnetlink_net_init(struct net *net)
657 struct nfnl_net *nfnlnet = nfnl_pernet(net);
658 struct netlink_kernel_cfg cfg = {
659 .groups = NFNLGRP_MAX,
660 .input = nfnetlink_rcv,
661 #ifdef CONFIG_MODULES
662 .bind = nfnetlink_bind,
666 nfnlnet->nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg);
672 static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list)
674 struct nfnl_net *nfnlnet;
677 list_for_each_entry(net, net_exit_list, exit_list) {
678 nfnlnet = nfnl_pernet(net);
680 netlink_kernel_release(nfnlnet->nfnl);
684 static struct pernet_operations nfnetlink_net_ops = {
685 .init = nfnetlink_net_init,
686 .exit_batch = nfnetlink_net_exit_batch,
687 .id = &nfnetlink_pernet_id,
688 .size = sizeof(struct nfnl_net),
691 static int __init nfnetlink_init(void)
695 for (i = NFNLGRP_NONE + 1; i <= NFNLGRP_MAX; i++)
696 BUG_ON(nfnl_group2type[i] == NFNL_SUBSYS_NONE);
698 for (i=0; i<NFNL_SUBSYS_COUNT; i++)
699 __mutex_init(&table[i].mutex, nfnl_lockdep_names[i], &nfnl_lockdep_keys[i]);
701 return register_pernet_subsys(&nfnetlink_net_ops);
704 static void __exit nfnetlink_exit(void)
706 unregister_pernet_subsys(&nfnetlink_net_ops);
708 module_init(nfnetlink_init);
709 module_exit(nfnetlink_exit);