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 <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
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
);
80 int nfnetlink_subsys_register(const struct nfnetlink_subsystem
*n
)
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
))
89 nfnl_lock(n
->subsys_id
);
90 if (table
[n
->subsys_id
].subsys
) {
91 nfnl_unlock(n
->subsys_id
);
94 rcu_assign_pointer(table
[n
->subsys_id
].subsys
, n
);
95 nfnl_unlock(n
->subsys_id
);
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
);
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
)
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
)
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
,
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
;
167 /* All the messages must at least contain nfgenmsg */
168 if (nlmsg_len(nlh
) < sizeof(struct nfgenmsg
))
171 type
= nlh
->nlmsg_type
;
174 ss
= nfnetlink_get_subsys(type
);
176 #ifdef CONFIG_MODULES
178 request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type
));
180 ss
= nfnetlink_get_subsys(type
);
189 nc
= nfnetlink_find_client(type
, ss
);
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
) {
209 err
= nla_parse(cda
, ss
->cb
[cb_id
].attr_count
, attr
, attrlen
,
210 ss
->cb
[cb_id
].policy
, extack
);
217 err
= nc
->call_rcu(net
, net
->nfnl
, skb
, nlh
,
218 (const struct nlattr
**)cda
,
223 nfnl_lock(subsys_id
);
224 if (nfnl_dereference_protected(subsys_id
) != ss
||
225 nfnetlink_find_client(type
, ss
) != nc
)
228 err
= nc
->call(net
, net
->nfnl
, skb
, nlh
,
229 (const struct nlattr
**)cda
,
233 nfnl_unlock(subsys_id
);
242 struct list_head head
;
243 struct nlmsghdr
*nlh
;
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
)
259 nfnl_err
->extack
= *extack
;
260 list_add_tail(&nfnl_err
->head
, list
);
265 static void nfnl_err_del(struct nfnl_err
*nfnl_err
)
267 list_del(&nfnl_err
->head
);
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
,
286 nfnl_err_del(nfnl_err
);
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
;
308 if (subsys_id
>= NFNL_SUBSYS_COUNT
)
309 return netlink_ack(skb
, nlh
, -EINVAL
, NULL
);
313 skb
= netlink_skb_clone(oskb
, GFP_KERNEL
);
315 return netlink_ack(oskb
, nlh
, -ENOMEM
, NULL
);
317 nfnl_lock(subsys_id
);
318 ss
= nfnl_dereference_protected(subsys_id
);
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
);
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)) {
358 if (fatal_signal_pending(current
)) {
359 nfnl_err_reset(&err_list
);
361 status
= NFNL_BATCH_FAILURE
;
365 memset(&extack
, 0, sizeof(extack
));
366 nlh
= nlmsg_hdr(skb
);
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
;
377 /* Only requests are handled by the kernel */
378 if (!(nlh
->nlmsg_flags
& NLM_F_REQUEST
)) {
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
;
389 } else if (type
== NFNL_MSG_BATCH_END
) {
390 status
|= NFNL_BATCH_DONE
;
392 } else if (type
< NLMSG_MIN_TYPE
) {
397 /* We only accept a batch with messages for the same
400 if (NFNL_SUBSYS_ID(type
) != subsys_id
) {
405 nc
= nfnetlink_find_client(type
, ss
);
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
) {
424 err
= nla_parse(cda
, ss
->cb
[cb_id
].attr_count
, attr
,
425 attrlen
, ss
->cb
[cb_id
].policy
, NULL
);
429 if (nc
->call_batch
) {
430 err
= nc
->call_batch(net
, net
->nfnl
, skb
, nlh
,
431 (const struct nlattr
**)cda
,
435 /* The lock was released to autoload some module, we
436 * have to abort and start from scratch using the
439 if (err
== -EAGAIN
) {
440 status
|= NFNL_BATCH_REPLAY
;
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
,
458 status
|= NFNL_BATCH_FAILURE
;
461 /* We don't stop processing the batch on errors, thus,
462 * userspace gets all the errors that the batch
466 status
|= NFNL_BATCH_FAILURE
;
469 msglen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
470 if (msglen
> skb
->len
)
472 skb_pull(skb
, msglen
);
475 if (status
& NFNL_BATCH_REPLAY
) {
476 ss
->abort(net
, oskb
);
477 nfnl_err_reset(&err_list
);
479 module_put(ss
->owner
);
481 } else if (status
== NFNL_BATCH_DONE
) {
482 err
= ss
->commit(net
, oskb
);
483 if (err
== -EAGAIN
) {
484 status
|= NFNL_BATCH_REPLAY
;
487 ss
->abort(net
, oskb
);
488 netlink_ack(oskb
, nlmsg_hdr(oskb
), err
, NULL
);
491 ss
->abort(net
, oskb
);
496 nfnl_err_deliver(&err_list
, oskb
);
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
;
516 msglen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
517 if (msglen
> skb
->len
)
520 if (skb
->len
< NLMSG_HDRLEN
+ sizeof(struct nfgenmsg
))
523 err
= nla_parse(cda
, NFNL_BATCH_MAX
, attr
, attrlen
, nfnl_batch_policy
,
526 netlink_ack(skb
, nlh
, err
, NULL
);
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
;
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
)
552 if (!netlink_net_capable(skb
, CAP_NET_ADMIN
)) {
553 netlink_ack(skb
, nlh
, -EPERM
, NULL
);
557 if (nlh
->nlmsg_type
== NFNL_MSG_BATCH_BEGIN
)
558 nfnetlink_rcv_skb_batch(skb
, nlh
);
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
;
569 if (group
<= NFNLGRP_NONE
|| group
> NFNLGRP_MAX
)
572 type
= nfnl_group2type
[group
];
575 ss
= nfnetlink_get_subsys(type
<< 8);
578 request_module("nfnetlink-subsys-%d", type
);
583 static int __net_init
nfnetlink_net_init(struct net
*net
)
586 struct netlink_kernel_cfg cfg
= {
587 .groups
= NFNLGRP_MAX
,
588 .input
= nfnetlink_rcv
,
589 #ifdef CONFIG_MODULES
590 .bind
= nfnetlink_bind
,
594 nfnl
= netlink_kernel_create(net
, NETLINK_NETFILTER
, &cfg
);
597 net
->nfnl_stash
= nfnl
;
598 rcu_assign_pointer(net
->nfnl
, nfnl
);
602 static void __net_exit
nfnetlink_net_exit_batch(struct list_head
*net_exit_list
)
606 list_for_each_entry(net
, net_exit_list
, exit_list
)
607 RCU_INIT_POINTER(net
->nfnl
, NULL
);
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)
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
);