2 * This is a module which is used for queueing packets and communicating with
3 * userspace via nfnetlink.
5 * (C) 2005 by Harald Welte <laforge@netfilter.org>
6 * (C) 2007 by Patrick McHardy <kaber@trash.net>
8 * Based on the old ipv4-only ip_queue.c:
9 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
10 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/module.h>
21 #include <linux/skbuff.h>
22 #include <linux/init.h>
23 #include <linux/spinlock.h>
24 #include <linux/slab.h>
25 #include <linux/notifier.h>
26 #include <linux/netdevice.h>
27 #include <linux/netfilter.h>
28 #include <linux/proc_fs.h>
29 #include <linux/netfilter_ipv4.h>
30 #include <linux/netfilter_ipv6.h>
31 #include <linux/netfilter_bridge.h>
32 #include <linux/netfilter/nfnetlink.h>
33 #include <linux/netfilter/nfnetlink_queue.h>
34 #include <linux/netfilter/nf_conntrack_common.h>
35 #include <linux/list.h>
37 #include <net/tcp_states.h>
38 #include <net/netfilter/nf_queue.h>
39 #include <net/netns/generic.h>
41 #include <linux/atomic.h>
43 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
44 #include "../bridge/br_private.h"
47 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
48 #include <net/netfilter/nf_conntrack.h>
51 #define NFQNL_QMAX_DEFAULT 1024
53 /* We're using struct nlattr which has 16bit nla_len. Note that nla_len
54 * includes the header length. Thus, the maximum packet length that we
55 * support is 65531 bytes. We send truncated packets if the specified length
56 * is larger than that. Userspace can check for presence of NFQA_CAP_LEN
57 * attribute to detect truncation.
59 #define NFQNL_MAX_COPY_RANGE (0xffff - NLA_HDRLEN)
61 struct nfqnl_instance
{
62 struct hlist_node hlist
; /* global list of queues */
66 unsigned int queue_maxlen
;
67 unsigned int copy_range
;
68 unsigned int queue_dropped
;
69 unsigned int queue_user_dropped
;
72 u_int16_t queue_num
; /* number of this queue */
74 u_int32_t flags
; /* Set using NFQA_CFG_FLAGS */
76 * Following fields are dirtied for each queued packet,
77 * keep them in same cache line if possible.
79 spinlock_t lock ____cacheline_aligned_in_smp
;
80 unsigned int queue_total
;
81 unsigned int id_sequence
; /* 'sequence' of pkt ids */
82 struct list_head queue_list
; /* packets in queue */
85 typedef int (*nfqnl_cmpfn
)(struct nf_queue_entry
*, unsigned long);
87 static unsigned int nfnl_queue_net_id __read_mostly
;
89 #define INSTANCE_BUCKETS 16
90 struct nfnl_queue_net
{
91 spinlock_t instances_lock
;
92 struct hlist_head instance_table
[INSTANCE_BUCKETS
];
95 static struct nfnl_queue_net
*nfnl_queue_pernet(struct net
*net
)
97 return net_generic(net
, nfnl_queue_net_id
);
100 static inline u_int8_t
instance_hashfn(u_int16_t queue_num
)
102 return ((queue_num
>> 8) ^ queue_num
) % INSTANCE_BUCKETS
;
105 static struct nfqnl_instance
*
106 instance_lookup(struct nfnl_queue_net
*q
, u_int16_t queue_num
)
108 struct hlist_head
*head
;
109 struct nfqnl_instance
*inst
;
111 head
= &q
->instance_table
[instance_hashfn(queue_num
)];
112 hlist_for_each_entry_rcu(inst
, head
, hlist
) {
113 if (inst
->queue_num
== queue_num
)
119 static struct nfqnl_instance
*
120 instance_create(struct nfnl_queue_net
*q
, u_int16_t queue_num
, u32 portid
)
122 struct nfqnl_instance
*inst
;
126 spin_lock(&q
->instances_lock
);
127 if (instance_lookup(q
, queue_num
)) {
132 inst
= kzalloc(sizeof(*inst
), GFP_ATOMIC
);
138 inst
->queue_num
= queue_num
;
139 inst
->peer_portid
= portid
;
140 inst
->queue_maxlen
= NFQNL_QMAX_DEFAULT
;
141 inst
->copy_range
= NFQNL_MAX_COPY_RANGE
;
142 inst
->copy_mode
= NFQNL_COPY_NONE
;
143 spin_lock_init(&inst
->lock
);
144 INIT_LIST_HEAD(&inst
->queue_list
);
146 if (!try_module_get(THIS_MODULE
)) {
151 h
= instance_hashfn(queue_num
);
152 hlist_add_head_rcu(&inst
->hlist
, &q
->instance_table
[h
]);
154 spin_unlock(&q
->instances_lock
);
161 spin_unlock(&q
->instances_lock
);
165 static void nfqnl_flush(struct nfqnl_instance
*queue
, nfqnl_cmpfn cmpfn
,
169 instance_destroy_rcu(struct rcu_head
*head
)
171 struct nfqnl_instance
*inst
= container_of(head
, struct nfqnl_instance
,
174 nfqnl_flush(inst
, NULL
, 0);
176 module_put(THIS_MODULE
);
180 __instance_destroy(struct nfqnl_instance
*inst
)
182 hlist_del_rcu(&inst
->hlist
);
183 call_rcu(&inst
->rcu
, instance_destroy_rcu
);
187 instance_destroy(struct nfnl_queue_net
*q
, struct nfqnl_instance
*inst
)
189 spin_lock(&q
->instances_lock
);
190 __instance_destroy(inst
);
191 spin_unlock(&q
->instances_lock
);
195 __enqueue_entry(struct nfqnl_instance
*queue
, struct nf_queue_entry
*entry
)
197 list_add_tail(&entry
->list
, &queue
->queue_list
);
198 queue
->queue_total
++;
202 __dequeue_entry(struct nfqnl_instance
*queue
, struct nf_queue_entry
*entry
)
204 list_del(&entry
->list
);
205 queue
->queue_total
--;
208 static struct nf_queue_entry
*
209 find_dequeue_entry(struct nfqnl_instance
*queue
, unsigned int id
)
211 struct nf_queue_entry
*entry
= NULL
, *i
;
213 spin_lock_bh(&queue
->lock
);
215 list_for_each_entry(i
, &queue
->queue_list
, list
) {
223 __dequeue_entry(queue
, entry
);
225 spin_unlock_bh(&queue
->lock
);
230 static void nfqnl_reinject(struct nf_queue_entry
*entry
, unsigned int verdict
)
232 struct nf_ct_hook
*ct_hook
;
235 if (verdict
== NF_ACCEPT
||
236 verdict
== NF_REPEAT
||
237 verdict
== NF_STOP
) {
239 ct_hook
= rcu_dereference(nf_ct_hook
);
241 err
= ct_hook
->update(entry
->state
.net
, entry
->skb
);
247 nf_reinject(entry
, verdict
);
251 nfqnl_flush(struct nfqnl_instance
*queue
, nfqnl_cmpfn cmpfn
, unsigned long data
)
253 struct nf_queue_entry
*entry
, *next
;
255 spin_lock_bh(&queue
->lock
);
256 list_for_each_entry_safe(entry
, next
, &queue
->queue_list
, list
) {
257 if (!cmpfn
|| cmpfn(entry
, data
)) {
258 list_del(&entry
->list
);
259 queue
->queue_total
--;
260 nfqnl_reinject(entry
, NF_DROP
);
263 spin_unlock_bh(&queue
->lock
);
267 nfqnl_put_packet_info(struct sk_buff
*nlskb
, struct sk_buff
*packet
,
272 if (packet
->ip_summed
== CHECKSUM_PARTIAL
)
273 flags
= NFQA_SKB_CSUMNOTREADY
;
274 else if (csum_verify
)
275 flags
= NFQA_SKB_CSUM_NOTVERIFIED
;
277 if (skb_is_gso(packet
))
278 flags
|= NFQA_SKB_GSO
;
280 return flags
? nla_put_be32(nlskb
, NFQA_SKB_INFO
, htonl(flags
)) : 0;
283 static int nfqnl_put_sk_uidgid(struct sk_buff
*skb
, struct sock
*sk
)
285 const struct cred
*cred
;
287 if (!sk_fullsock(sk
))
290 read_lock_bh(&sk
->sk_callback_lock
);
291 if (sk
->sk_socket
&& sk
->sk_socket
->file
) {
292 cred
= sk
->sk_socket
->file
->f_cred
;
293 if (nla_put_be32(skb
, NFQA_UID
,
294 htonl(from_kuid_munged(&init_user_ns
, cred
->fsuid
))))
295 goto nla_put_failure
;
296 if (nla_put_be32(skb
, NFQA_GID
,
297 htonl(from_kgid_munged(&init_user_ns
, cred
->fsgid
))))
298 goto nla_put_failure
;
300 read_unlock_bh(&sk
->sk_callback_lock
);
304 read_unlock_bh(&sk
->sk_callback_lock
);
308 static u32
nfqnl_get_sk_secctx(struct sk_buff
*skb
, char **secdata
)
311 #if IS_ENABLED(CONFIG_NETWORK_SECMARK)
312 if (!skb
|| !sk_fullsock(skb
->sk
))
315 read_lock_bh(&skb
->sk
->sk_callback_lock
);
318 security_secid_to_secctx(skb
->secmark
, secdata
, &seclen
);
320 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
325 static u32
nfqnl_get_bridge_size(struct nf_queue_entry
*entry
)
327 struct sk_buff
*entskb
= entry
->skb
;
330 if (entry
->state
.pf
!= PF_BRIDGE
|| !skb_mac_header_was_set(entskb
))
333 if (skb_vlan_tag_present(entskb
))
334 nlalen
+= nla_total_size(nla_total_size(sizeof(__be16
)) +
335 nla_total_size(sizeof(__be16
)));
337 if (entskb
->network_header
> entskb
->mac_header
)
338 nlalen
+= nla_total_size((entskb
->network_header
-
339 entskb
->mac_header
));
344 static int nfqnl_put_bridge(struct nf_queue_entry
*entry
, struct sk_buff
*skb
)
346 struct sk_buff
*entskb
= entry
->skb
;
348 if (entry
->state
.pf
!= PF_BRIDGE
|| !skb_mac_header_was_set(entskb
))
351 if (skb_vlan_tag_present(entskb
)) {
354 nest
= nla_nest_start(skb
, NFQA_VLAN
| NLA_F_NESTED
);
356 goto nla_put_failure
;
358 if (nla_put_be16(skb
, NFQA_VLAN_TCI
, htons(entskb
->vlan_tci
)) ||
359 nla_put_be16(skb
, NFQA_VLAN_PROTO
, entskb
->vlan_proto
))
360 goto nla_put_failure
;
362 nla_nest_end(skb
, nest
);
365 if (entskb
->mac_header
< entskb
->network_header
) {
366 int len
= (int)(entskb
->network_header
- entskb
->mac_header
);
368 if (nla_put(skb
, NFQA_L2HDR
, len
, skb_mac_header(entskb
)))
369 goto nla_put_failure
;
378 static struct sk_buff
*
379 nfqnl_build_packet_message(struct net
*net
, struct nfqnl_instance
*queue
,
380 struct nf_queue_entry
*entry
,
381 __be32
**packet_id_ptr
)
384 size_t data_len
= 0, cap_len
= 0;
385 unsigned int hlen
= 0;
388 struct nfqnl_msg_packet_hdr
*pmsg
;
389 struct nlmsghdr
*nlh
;
390 struct nfgenmsg
*nfmsg
;
391 struct sk_buff
*entskb
= entry
->skb
;
392 struct net_device
*indev
;
393 struct net_device
*outdev
;
394 struct nf_conn
*ct
= NULL
;
395 enum ip_conntrack_info
uninitialized_var(ctinfo
);
396 struct nfnl_ct_hook
*nfnl_ct
;
398 char *secdata
= NULL
;
401 size
= nlmsg_total_size(sizeof(struct nfgenmsg
))
402 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr
))
403 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
404 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
405 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
406 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
407 + nla_total_size(sizeof(u_int32_t
)) /* ifindex */
409 + nla_total_size(sizeof(u_int32_t
)) /* mark */
410 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw
))
411 + nla_total_size(sizeof(u_int32_t
)) /* skbinfo */
412 + nla_total_size(sizeof(u_int32_t
)); /* cap_len */
415 size
+= nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp
));
417 size
+= nfqnl_get_bridge_size(entry
);
419 if (entry
->state
.hook
<= NF_INET_FORWARD
||
420 (entry
->state
.hook
== NF_INET_POST_ROUTING
&& entskb
->sk
== NULL
))
421 csum_verify
= !skb_csum_unnecessary(entskb
);
425 outdev
= entry
->state
.out
;
427 switch ((enum nfqnl_config_mode
)READ_ONCE(queue
->copy_mode
)) {
428 case NFQNL_COPY_META
:
429 case NFQNL_COPY_NONE
:
432 case NFQNL_COPY_PACKET
:
433 if (!(queue
->flags
& NFQA_CFG_F_GSO
) &&
434 entskb
->ip_summed
== CHECKSUM_PARTIAL
&&
435 skb_checksum_help(entskb
))
438 data_len
= READ_ONCE(queue
->copy_range
);
439 if (data_len
> entskb
->len
)
440 data_len
= entskb
->len
;
442 hlen
= skb_zerocopy_headlen(entskb
);
443 hlen
= min_t(unsigned int, hlen
, data_len
);
444 size
+= sizeof(struct nlattr
) + hlen
;
445 cap_len
= entskb
->len
;
449 nfnl_ct
= rcu_dereference(nfnl_ct_hook
);
451 if (queue
->flags
& NFQA_CFG_F_CONNTRACK
) {
452 if (nfnl_ct
!= NULL
) {
453 ct
= nfnl_ct
->get_ct(entskb
, &ctinfo
);
455 size
+= nfnl_ct
->build_size(ct
);
459 if (queue
->flags
& NFQA_CFG_F_UID_GID
) {
460 size
+= (nla_total_size(sizeof(u_int32_t
)) /* uid */
461 + nla_total_size(sizeof(u_int32_t
))); /* gid */
464 if ((queue
->flags
& NFQA_CFG_F_SECCTX
) && entskb
->sk
) {
465 seclen
= nfqnl_get_sk_secctx(entskb
, &secdata
);
467 size
+= nla_total_size(seclen
);
470 skb
= alloc_skb(size
, GFP_ATOMIC
);
472 skb_tx_error(entskb
);
476 nlh
= nlmsg_put(skb
, 0, 0,
477 nfnl_msg_type(NFNL_SUBSYS_QUEUE
, NFQNL_MSG_PACKET
),
478 sizeof(struct nfgenmsg
), 0);
480 skb_tx_error(entskb
);
484 nfmsg
= nlmsg_data(nlh
);
485 nfmsg
->nfgen_family
= entry
->state
.pf
;
486 nfmsg
->version
= NFNETLINK_V0
;
487 nfmsg
->res_id
= htons(queue
->queue_num
);
489 nla
= __nla_reserve(skb
, NFQA_PACKET_HDR
, sizeof(*pmsg
));
490 pmsg
= nla_data(nla
);
491 pmsg
->hw_protocol
= entskb
->protocol
;
492 pmsg
->hook
= entry
->state
.hook
;
493 *packet_id_ptr
= &pmsg
->packet_id
;
495 indev
= entry
->state
.in
;
497 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
498 if (nla_put_be32(skb
, NFQA_IFINDEX_INDEV
, htonl(indev
->ifindex
)))
499 goto nla_put_failure
;
501 if (entry
->state
.pf
== PF_BRIDGE
) {
502 /* Case 1: indev is physical input device, we need to
503 * look for bridge group (when called from
504 * netfilter_bridge) */
505 if (nla_put_be32(skb
, NFQA_IFINDEX_PHYSINDEV
,
506 htonl(indev
->ifindex
)) ||
507 /* this is the bridge group "brX" */
508 /* rcu_read_lock()ed by __nf_queue */
509 nla_put_be32(skb
, NFQA_IFINDEX_INDEV
,
510 htonl(br_port_get_rcu(indev
)->br
->dev
->ifindex
)))
511 goto nla_put_failure
;
515 /* Case 2: indev is bridge group, we need to look for
516 * physical device (when called from ipv4) */
517 if (nla_put_be32(skb
, NFQA_IFINDEX_INDEV
,
518 htonl(indev
->ifindex
)))
519 goto nla_put_failure
;
521 physinif
= nf_bridge_get_physinif(entskb
);
523 nla_put_be32(skb
, NFQA_IFINDEX_PHYSINDEV
,
525 goto nla_put_failure
;
531 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
532 if (nla_put_be32(skb
, NFQA_IFINDEX_OUTDEV
, htonl(outdev
->ifindex
)))
533 goto nla_put_failure
;
535 if (entry
->state
.pf
== PF_BRIDGE
) {
536 /* Case 1: outdev is physical output device, we need to
537 * look for bridge group (when called from
538 * netfilter_bridge) */
539 if (nla_put_be32(skb
, NFQA_IFINDEX_PHYSOUTDEV
,
540 htonl(outdev
->ifindex
)) ||
541 /* this is the bridge group "brX" */
542 /* rcu_read_lock()ed by __nf_queue */
543 nla_put_be32(skb
, NFQA_IFINDEX_OUTDEV
,
544 htonl(br_port_get_rcu(outdev
)->br
->dev
->ifindex
)))
545 goto nla_put_failure
;
549 /* Case 2: outdev is bridge group, we need to look for
550 * physical output device (when called from ipv4) */
551 if (nla_put_be32(skb
, NFQA_IFINDEX_OUTDEV
,
552 htonl(outdev
->ifindex
)))
553 goto nla_put_failure
;
555 physoutif
= nf_bridge_get_physoutif(entskb
);
557 nla_put_be32(skb
, NFQA_IFINDEX_PHYSOUTDEV
,
559 goto nla_put_failure
;
565 nla_put_be32(skb
, NFQA_MARK
, htonl(entskb
->mark
)))
566 goto nla_put_failure
;
568 if (indev
&& entskb
->dev
&&
569 entskb
->mac_header
!= entskb
->network_header
) {
570 struct nfqnl_msg_packet_hw phw
;
573 memset(&phw
, 0, sizeof(phw
));
574 len
= dev_parse_header(entskb
, phw
.hw_addr
);
576 phw
.hw_addrlen
= htons(len
);
577 if (nla_put(skb
, NFQA_HWADDR
, sizeof(phw
), &phw
))
578 goto nla_put_failure
;
582 if (nfqnl_put_bridge(entry
, skb
) < 0)
583 goto nla_put_failure
;
585 if (entskb
->tstamp
) {
586 struct nfqnl_msg_packet_timestamp ts
;
587 struct timespec64 kts
= ktime_to_timespec64(entskb
->tstamp
);
589 ts
.sec
= cpu_to_be64(kts
.tv_sec
);
590 ts
.usec
= cpu_to_be64(kts
.tv_nsec
/ NSEC_PER_USEC
);
592 if (nla_put(skb
, NFQA_TIMESTAMP
, sizeof(ts
), &ts
))
593 goto nla_put_failure
;
596 if ((queue
->flags
& NFQA_CFG_F_UID_GID
) && entskb
->sk
&&
597 nfqnl_put_sk_uidgid(skb
, entskb
->sk
) < 0)
598 goto nla_put_failure
;
600 if (seclen
&& nla_put(skb
, NFQA_SECCTX
, seclen
, secdata
))
601 goto nla_put_failure
;
603 if (ct
&& nfnl_ct
->build(skb
, ct
, ctinfo
, NFQA_CT
, NFQA_CT_INFO
) < 0)
604 goto nla_put_failure
;
606 if (cap_len
> data_len
&&
607 nla_put_be32(skb
, NFQA_CAP_LEN
, htonl(cap_len
)))
608 goto nla_put_failure
;
610 if (nfqnl_put_packet_info(skb
, entskb
, csum_verify
))
611 goto nla_put_failure
;
616 if (skb_tailroom(skb
) < sizeof(*nla
) + hlen
)
617 goto nla_put_failure
;
619 nla
= skb_put(skb
, sizeof(*nla
));
620 nla
->nla_type
= NFQA_PAYLOAD
;
621 nla
->nla_len
= nla_attr_size(data_len
);
623 if (skb_zerocopy(skb
, entskb
, data_len
, hlen
))
624 goto nla_put_failure
;
627 nlh
->nlmsg_len
= skb
->len
;
629 security_release_secctx(secdata
, seclen
);
633 skb_tx_error(entskb
);
635 net_err_ratelimited("nf_queue: error creating packet message\n");
638 security_release_secctx(secdata
, seclen
);
642 static bool nf_ct_drop_unconfirmed(const struct nf_queue_entry
*entry
)
644 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
645 static const unsigned long flags
= IPS_CONFIRMED
| IPS_DYING
;
646 const struct nf_conn
*ct
= (void *)skb_nfct(entry
->skb
);
648 if (ct
&& ((ct
->status
& flags
) == IPS_DYING
))
655 __nfqnl_enqueue_packet(struct net
*net
, struct nfqnl_instance
*queue
,
656 struct nf_queue_entry
*entry
)
658 struct sk_buff
*nskb
;
660 __be32
*packet_id_ptr
;
663 nskb
= nfqnl_build_packet_message(net
, queue
, entry
, &packet_id_ptr
);
668 spin_lock_bh(&queue
->lock
);
670 if (nf_ct_drop_unconfirmed(entry
))
671 goto err_out_free_nskb
;
673 if (queue
->queue_total
>= queue
->queue_maxlen
) {
674 if (queue
->flags
& NFQA_CFG_F_FAIL_OPEN
) {
678 queue
->queue_dropped
++;
679 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
682 goto err_out_free_nskb
;
684 entry
->id
= ++queue
->id_sequence
;
685 *packet_id_ptr
= htonl(entry
->id
);
687 /* nfnetlink_unicast will either free the nskb or add it to a socket */
688 err
= nfnetlink_unicast(nskb
, net
, queue
->peer_portid
, MSG_DONTWAIT
);
690 if (queue
->flags
& NFQA_CFG_F_FAIL_OPEN
) {
694 queue
->queue_user_dropped
++;
699 __enqueue_entry(queue
, entry
);
701 spin_unlock_bh(&queue
->lock
);
707 spin_unlock_bh(&queue
->lock
);
709 nfqnl_reinject(entry
, NF_ACCEPT
);
714 static struct nf_queue_entry
*
715 nf_queue_entry_dup(struct nf_queue_entry
*e
)
717 struct nf_queue_entry
*entry
= kmemdup(e
, e
->size
, GFP_ATOMIC
);
719 nf_queue_entry_get_refs(entry
);
723 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
724 /* When called from bridge netfilter, skb->data must point to MAC header
725 * before calling skb_gso_segment(). Else, original MAC header is lost
726 * and segmented skbs will be sent to wrong destination.
728 static void nf_bridge_adjust_skb_data(struct sk_buff
*skb
)
731 __skb_push(skb
, skb
->network_header
- skb
->mac_header
);
734 static void nf_bridge_adjust_segmented_data(struct sk_buff
*skb
)
737 __skb_pull(skb
, skb
->network_header
- skb
->mac_header
);
740 #define nf_bridge_adjust_skb_data(s) do {} while (0)
741 #define nf_bridge_adjust_segmented_data(s) do {} while (0)
744 static void free_entry(struct nf_queue_entry
*entry
)
746 nf_queue_entry_release_refs(entry
);
751 __nfqnl_enqueue_packet_gso(struct net
*net
, struct nfqnl_instance
*queue
,
752 struct sk_buff
*skb
, struct nf_queue_entry
*entry
)
755 struct nf_queue_entry
*entry_seg
;
757 nf_bridge_adjust_segmented_data(skb
);
759 if (skb
->next
== NULL
) { /* last packet, no need to copy entry */
760 struct sk_buff
*gso_skb
= entry
->skb
;
762 ret
= __nfqnl_enqueue_packet(net
, queue
, entry
);
764 entry
->skb
= gso_skb
;
770 entry_seg
= nf_queue_entry_dup(entry
);
772 entry_seg
->skb
= skb
;
773 ret
= __nfqnl_enqueue_packet(net
, queue
, entry_seg
);
775 free_entry(entry_seg
);
781 nfqnl_enqueue_packet(struct nf_queue_entry
*entry
, unsigned int queuenum
)
784 struct nfqnl_instance
*queue
;
785 struct sk_buff
*skb
, *segs
;
787 struct net
*net
= entry
->state
.net
;
788 struct nfnl_queue_net
*q
= nfnl_queue_pernet(net
);
790 /* rcu_read_lock()ed by nf_hook_thresh */
791 queue
= instance_lookup(q
, queuenum
);
795 if (queue
->copy_mode
== NFQNL_COPY_NONE
)
800 switch (entry
->state
.pf
) {
802 skb
->protocol
= htons(ETH_P_IP
);
805 skb
->protocol
= htons(ETH_P_IPV6
);
809 if ((queue
->flags
& NFQA_CFG_F_GSO
) || !skb_is_gso(skb
))
810 return __nfqnl_enqueue_packet(net
, queue
, entry
);
812 nf_bridge_adjust_skb_data(skb
);
813 segs
= skb_gso_segment(skb
, 0);
814 /* Does not use PTR_ERR to limit the number of error codes that can be
815 * returned by nf_queue. For instance, callers rely on -ESRCH to
816 * mean 'ignore this hook'.
818 if (IS_ERR_OR_NULL(segs
))
823 struct sk_buff
*nskb
= segs
->next
;
825 err
= __nfqnl_enqueue_packet_gso(net
, queue
,
835 if (err
) /* some segments are already queued */
841 nf_bridge_adjust_segmented_data(skb
);
846 nfqnl_mangle(void *data
, int data_len
, struct nf_queue_entry
*e
, int diff
)
848 struct sk_buff
*nskb
;
851 if (pskb_trim(e
->skb
, data_len
))
853 } else if (diff
> 0) {
854 if (data_len
> 0xFFFF)
856 if (diff
> skb_tailroom(e
->skb
)) {
857 nskb
= skb_copy_expand(e
->skb
, skb_headroom(e
->skb
),
864 skb_put(e
->skb
, diff
);
866 if (!skb_make_writable(e
->skb
, data_len
))
868 skb_copy_to_linear_data(e
->skb
, data
, data_len
);
869 e
->skb
->ip_summed
= CHECKSUM_NONE
;
874 nfqnl_set_mode(struct nfqnl_instance
*queue
,
875 unsigned char mode
, unsigned int range
)
879 spin_lock_bh(&queue
->lock
);
881 case NFQNL_COPY_NONE
:
882 case NFQNL_COPY_META
:
883 queue
->copy_mode
= mode
;
884 queue
->copy_range
= 0;
887 case NFQNL_COPY_PACKET
:
888 queue
->copy_mode
= mode
;
889 if (range
== 0 || range
> NFQNL_MAX_COPY_RANGE
)
890 queue
->copy_range
= NFQNL_MAX_COPY_RANGE
;
892 queue
->copy_range
= range
;
899 spin_unlock_bh(&queue
->lock
);
905 dev_cmp(struct nf_queue_entry
*entry
, unsigned long ifindex
)
908 if (entry
->state
.in
->ifindex
== ifindex
)
910 if (entry
->state
.out
)
911 if (entry
->state
.out
->ifindex
== ifindex
)
913 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
914 if (entry
->skb
->nf_bridge
) {
915 int physinif
, physoutif
;
917 physinif
= nf_bridge_get_physinif(entry
->skb
);
918 physoutif
= nf_bridge_get_physoutif(entry
->skb
);
920 if (physinif
== ifindex
|| physoutif
== ifindex
)
927 /* drop all packets with either indev or outdev == ifindex from all queue
930 nfqnl_dev_drop(struct net
*net
, int ifindex
)
933 struct nfnl_queue_net
*q
= nfnl_queue_pernet(net
);
937 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++) {
938 struct nfqnl_instance
*inst
;
939 struct hlist_head
*head
= &q
->instance_table
[i
];
941 hlist_for_each_entry_rcu(inst
, head
, hlist
)
942 nfqnl_flush(inst
, dev_cmp
, ifindex
);
949 nfqnl_rcv_dev_event(struct notifier_block
*this,
950 unsigned long event
, void *ptr
)
952 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
954 /* Drop any packets associated with the downed device */
955 if (event
== NETDEV_DOWN
)
956 nfqnl_dev_drop(dev_net(dev
), dev
->ifindex
);
960 static struct notifier_block nfqnl_dev_notifier
= {
961 .notifier_call
= nfqnl_rcv_dev_event
,
964 static void nfqnl_nf_hook_drop(struct net
*net
)
966 struct nfnl_queue_net
*q
= nfnl_queue_pernet(net
);
969 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++) {
970 struct nfqnl_instance
*inst
;
971 struct hlist_head
*head
= &q
->instance_table
[i
];
973 hlist_for_each_entry_rcu(inst
, head
, hlist
)
974 nfqnl_flush(inst
, NULL
, 0);
979 nfqnl_rcv_nl_event(struct notifier_block
*this,
980 unsigned long event
, void *ptr
)
982 struct netlink_notify
*n
= ptr
;
983 struct nfnl_queue_net
*q
= nfnl_queue_pernet(n
->net
);
985 if (event
== NETLINK_URELEASE
&& n
->protocol
== NETLINK_NETFILTER
) {
988 /* destroy all instances for this portid */
989 spin_lock(&q
->instances_lock
);
990 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++) {
991 struct hlist_node
*t2
;
992 struct nfqnl_instance
*inst
;
993 struct hlist_head
*head
= &q
->instance_table
[i
];
995 hlist_for_each_entry_safe(inst
, t2
, head
, hlist
) {
996 if (n
->portid
== inst
->peer_portid
)
997 __instance_destroy(inst
);
1000 spin_unlock(&q
->instances_lock
);
1005 static struct notifier_block nfqnl_rtnl_notifier
= {
1006 .notifier_call
= nfqnl_rcv_nl_event
,
1009 static const struct nla_policy nfqa_vlan_policy
[NFQA_VLAN_MAX
+ 1] = {
1010 [NFQA_VLAN_TCI
] = { .type
= NLA_U16
},
1011 [NFQA_VLAN_PROTO
] = { .type
= NLA_U16
},
1014 static const struct nla_policy nfqa_verdict_policy
[NFQA_MAX
+1] = {
1015 [NFQA_VERDICT_HDR
] = { .len
= sizeof(struct nfqnl_msg_verdict_hdr
) },
1016 [NFQA_MARK
] = { .type
= NLA_U32
},
1017 [NFQA_PAYLOAD
] = { .type
= NLA_UNSPEC
},
1018 [NFQA_CT
] = { .type
= NLA_UNSPEC
},
1019 [NFQA_EXP
] = { .type
= NLA_UNSPEC
},
1020 [NFQA_VLAN
] = { .type
= NLA_NESTED
},
1023 static const struct nla_policy nfqa_verdict_batch_policy
[NFQA_MAX
+1] = {
1024 [NFQA_VERDICT_HDR
] = { .len
= sizeof(struct nfqnl_msg_verdict_hdr
) },
1025 [NFQA_MARK
] = { .type
= NLA_U32
},
1028 static struct nfqnl_instance
*
1029 verdict_instance_lookup(struct nfnl_queue_net
*q
, u16 queue_num
, u32 nlportid
)
1031 struct nfqnl_instance
*queue
;
1033 queue
= instance_lookup(q
, queue_num
);
1035 return ERR_PTR(-ENODEV
);
1037 if (queue
->peer_portid
!= nlportid
)
1038 return ERR_PTR(-EPERM
);
1043 static struct nfqnl_msg_verdict_hdr
*
1044 verdicthdr_get(const struct nlattr
* const nfqa
[])
1046 struct nfqnl_msg_verdict_hdr
*vhdr
;
1047 unsigned int verdict
;
1049 if (!nfqa
[NFQA_VERDICT_HDR
])
1052 vhdr
= nla_data(nfqa
[NFQA_VERDICT_HDR
]);
1053 verdict
= ntohl(vhdr
->verdict
) & NF_VERDICT_MASK
;
1054 if (verdict
> NF_MAX_VERDICT
|| verdict
== NF_STOLEN
)
1059 static int nfq_id_after(unsigned int id
, unsigned int max
)
1061 return (int)(id
- max
) > 0;
1064 static int nfqnl_recv_verdict_batch(struct net
*net
, struct sock
*ctnl
,
1065 struct sk_buff
*skb
,
1066 const struct nlmsghdr
*nlh
,
1067 const struct nlattr
* const nfqa
[],
1068 struct netlink_ext_ack
*extack
)
1070 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1071 struct nf_queue_entry
*entry
, *tmp
;
1072 unsigned int verdict
, maxid
;
1073 struct nfqnl_msg_verdict_hdr
*vhdr
;
1074 struct nfqnl_instance
*queue
;
1075 LIST_HEAD(batch_list
);
1076 u16 queue_num
= ntohs(nfmsg
->res_id
);
1077 struct nfnl_queue_net
*q
= nfnl_queue_pernet(net
);
1079 queue
= verdict_instance_lookup(q
, queue_num
,
1080 NETLINK_CB(skb
).portid
);
1082 return PTR_ERR(queue
);
1084 vhdr
= verdicthdr_get(nfqa
);
1088 verdict
= ntohl(vhdr
->verdict
);
1089 maxid
= ntohl(vhdr
->id
);
1091 spin_lock_bh(&queue
->lock
);
1093 list_for_each_entry_safe(entry
, tmp
, &queue
->queue_list
, list
) {
1094 if (nfq_id_after(entry
->id
, maxid
))
1096 __dequeue_entry(queue
, entry
);
1097 list_add_tail(&entry
->list
, &batch_list
);
1100 spin_unlock_bh(&queue
->lock
);
1102 if (list_empty(&batch_list
))
1105 list_for_each_entry_safe(entry
, tmp
, &batch_list
, list
) {
1106 if (nfqa
[NFQA_MARK
])
1107 entry
->skb
->mark
= ntohl(nla_get_be32(nfqa
[NFQA_MARK
]));
1109 nfqnl_reinject(entry
, verdict
);
1114 static struct nf_conn
*nfqnl_ct_parse(struct nfnl_ct_hook
*nfnl_ct
,
1115 const struct nlmsghdr
*nlh
,
1116 const struct nlattr
* const nfqa
[],
1117 struct nf_queue_entry
*entry
,
1118 enum ip_conntrack_info
*ctinfo
)
1122 ct
= nfnl_ct
->get_ct(entry
->skb
, ctinfo
);
1126 if (nfnl_ct
->parse(nfqa
[NFQA_CT
], ct
) < 0)
1130 nfnl_ct
->attach_expect(nfqa
[NFQA_EXP
], ct
,
1131 NETLINK_CB(entry
->skb
).portid
,
1136 static int nfqa_parse_bridge(struct nf_queue_entry
*entry
,
1137 const struct nlattr
* const nfqa
[])
1139 if (nfqa
[NFQA_VLAN
]) {
1140 struct nlattr
*tb
[NFQA_VLAN_MAX
+ 1];
1143 err
= nla_parse_nested(tb
, NFQA_VLAN_MAX
, nfqa
[NFQA_VLAN
],
1144 nfqa_vlan_policy
, NULL
);
1148 if (!tb
[NFQA_VLAN_TCI
] || !tb
[NFQA_VLAN_PROTO
])
1151 entry
->skb
->vlan_tci
= ntohs(nla_get_be16(tb
[NFQA_VLAN_TCI
]));
1152 entry
->skb
->vlan_proto
= nla_get_be16(tb
[NFQA_VLAN_PROTO
]);
1155 if (nfqa
[NFQA_L2HDR
]) {
1156 int mac_header_len
= entry
->skb
->network_header
-
1157 entry
->skb
->mac_header
;
1159 if (mac_header_len
!= nla_len(nfqa
[NFQA_L2HDR
]))
1161 else if (mac_header_len
> 0)
1162 memcpy(skb_mac_header(entry
->skb
),
1163 nla_data(nfqa
[NFQA_L2HDR
]),
1170 static int nfqnl_recv_verdict(struct net
*net
, struct sock
*ctnl
,
1171 struct sk_buff
*skb
,
1172 const struct nlmsghdr
*nlh
,
1173 const struct nlattr
* const nfqa
[],
1174 struct netlink_ext_ack
*extack
)
1176 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1177 u_int16_t queue_num
= ntohs(nfmsg
->res_id
);
1178 struct nfqnl_msg_verdict_hdr
*vhdr
;
1179 struct nfqnl_instance
*queue
;
1180 unsigned int verdict
;
1181 struct nf_queue_entry
*entry
;
1182 enum ip_conntrack_info
uninitialized_var(ctinfo
);
1183 struct nfnl_ct_hook
*nfnl_ct
;
1184 struct nf_conn
*ct
= NULL
;
1185 struct nfnl_queue_net
*q
= nfnl_queue_pernet(net
);
1188 queue
= verdict_instance_lookup(q
, queue_num
,
1189 NETLINK_CB(skb
).portid
);
1191 return PTR_ERR(queue
);
1193 vhdr
= verdicthdr_get(nfqa
);
1197 verdict
= ntohl(vhdr
->verdict
);
1199 entry
= find_dequeue_entry(queue
, ntohl(vhdr
->id
));
1203 /* rcu lock already held from nfnl->call_rcu. */
1204 nfnl_ct
= rcu_dereference(nfnl_ct_hook
);
1206 if (nfqa
[NFQA_CT
]) {
1207 if (nfnl_ct
!= NULL
)
1208 ct
= nfqnl_ct_parse(nfnl_ct
, nlh
, nfqa
, entry
, &ctinfo
);
1211 if (entry
->state
.pf
== PF_BRIDGE
) {
1212 err
= nfqa_parse_bridge(entry
, nfqa
);
1217 if (nfqa
[NFQA_PAYLOAD
]) {
1218 u16 payload_len
= nla_len(nfqa
[NFQA_PAYLOAD
]);
1219 int diff
= payload_len
- entry
->skb
->len
;
1221 if (nfqnl_mangle(nla_data(nfqa
[NFQA_PAYLOAD
]),
1222 payload_len
, entry
, diff
) < 0)
1226 nfnl_ct
->seq_adjust(entry
->skb
, ct
, ctinfo
, diff
);
1229 if (nfqa
[NFQA_MARK
])
1230 entry
->skb
->mark
= ntohl(nla_get_be32(nfqa
[NFQA_MARK
]));
1232 nfqnl_reinject(entry
, verdict
);
1236 static int nfqnl_recv_unsupp(struct net
*net
, struct sock
*ctnl
,
1237 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1238 const struct nlattr
* const nfqa
[],
1239 struct netlink_ext_ack
*extack
)
1244 static const struct nla_policy nfqa_cfg_policy
[NFQA_CFG_MAX
+1] = {
1245 [NFQA_CFG_CMD
] = { .len
= sizeof(struct nfqnl_msg_config_cmd
) },
1246 [NFQA_CFG_PARAMS
] = { .len
= sizeof(struct nfqnl_msg_config_params
) },
1247 [NFQA_CFG_QUEUE_MAXLEN
] = { .type
= NLA_U32
},
1248 [NFQA_CFG_MASK
] = { .type
= NLA_U32
},
1249 [NFQA_CFG_FLAGS
] = { .type
= NLA_U32
},
1252 static const struct nf_queue_handler nfqh
= {
1253 .outfn
= nfqnl_enqueue_packet
,
1254 .nf_hook_drop
= nfqnl_nf_hook_drop
,
1257 static int nfqnl_recv_config(struct net
*net
, struct sock
*ctnl
,
1258 struct sk_buff
*skb
, const struct nlmsghdr
*nlh
,
1259 const struct nlattr
* const nfqa
[],
1260 struct netlink_ext_ack
*extack
)
1262 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
1263 u_int16_t queue_num
= ntohs(nfmsg
->res_id
);
1264 struct nfqnl_instance
*queue
;
1265 struct nfqnl_msg_config_cmd
*cmd
= NULL
;
1266 struct nfnl_queue_net
*q
= nfnl_queue_pernet(net
);
1267 __u32 flags
= 0, mask
= 0;
1270 if (nfqa
[NFQA_CFG_CMD
]) {
1271 cmd
= nla_data(nfqa
[NFQA_CFG_CMD
]);
1273 /* Obsolete commands without queue context */
1274 switch (cmd
->command
) {
1275 case NFQNL_CFG_CMD_PF_BIND
: return 0;
1276 case NFQNL_CFG_CMD_PF_UNBIND
: return 0;
1280 /* Check if we support these flags in first place, dependencies should
1281 * be there too not to break atomicity.
1283 if (nfqa
[NFQA_CFG_FLAGS
]) {
1284 if (!nfqa
[NFQA_CFG_MASK
]) {
1285 /* A mask is needed to specify which flags are being
1291 flags
= ntohl(nla_get_be32(nfqa
[NFQA_CFG_FLAGS
]));
1292 mask
= ntohl(nla_get_be32(nfqa
[NFQA_CFG_MASK
]));
1294 if (flags
>= NFQA_CFG_F_MAX
)
1297 #if !IS_ENABLED(CONFIG_NETWORK_SECMARK)
1298 if (flags
& mask
& NFQA_CFG_F_SECCTX
)
1301 if ((flags
& mask
& NFQA_CFG_F_CONNTRACK
) &&
1302 !rcu_access_pointer(nfnl_ct_hook
)) {
1303 #ifdef CONFIG_MODULES
1304 nfnl_unlock(NFNL_SUBSYS_QUEUE
);
1305 request_module("ip_conntrack_netlink");
1306 nfnl_lock(NFNL_SUBSYS_QUEUE
);
1307 if (rcu_access_pointer(nfnl_ct_hook
))
1315 queue
= instance_lookup(q
, queue_num
);
1316 if (queue
&& queue
->peer_portid
!= NETLINK_CB(skb
).portid
) {
1318 goto err_out_unlock
;
1322 switch (cmd
->command
) {
1323 case NFQNL_CFG_CMD_BIND
:
1326 goto err_out_unlock
;
1328 queue
= instance_create(q
, queue_num
,
1329 NETLINK_CB(skb
).portid
);
1330 if (IS_ERR(queue
)) {
1331 ret
= PTR_ERR(queue
);
1332 goto err_out_unlock
;
1335 case NFQNL_CFG_CMD_UNBIND
:
1338 goto err_out_unlock
;
1340 instance_destroy(q
, queue
);
1341 goto err_out_unlock
;
1342 case NFQNL_CFG_CMD_PF_BIND
:
1343 case NFQNL_CFG_CMD_PF_UNBIND
:
1347 goto err_out_unlock
;
1353 goto err_out_unlock
;
1356 if (nfqa
[NFQA_CFG_PARAMS
]) {
1357 struct nfqnl_msg_config_params
*params
=
1358 nla_data(nfqa
[NFQA_CFG_PARAMS
]);
1360 nfqnl_set_mode(queue
, params
->copy_mode
,
1361 ntohl(params
->copy_range
));
1364 if (nfqa
[NFQA_CFG_QUEUE_MAXLEN
]) {
1365 __be32
*queue_maxlen
= nla_data(nfqa
[NFQA_CFG_QUEUE_MAXLEN
]);
1367 spin_lock_bh(&queue
->lock
);
1368 queue
->queue_maxlen
= ntohl(*queue_maxlen
);
1369 spin_unlock_bh(&queue
->lock
);
1372 if (nfqa
[NFQA_CFG_FLAGS
]) {
1373 spin_lock_bh(&queue
->lock
);
1374 queue
->flags
&= ~mask
;
1375 queue
->flags
|= flags
& mask
;
1376 spin_unlock_bh(&queue
->lock
);
1384 static const struct nfnl_callback nfqnl_cb
[NFQNL_MSG_MAX
] = {
1385 [NFQNL_MSG_PACKET
] = { .call_rcu
= nfqnl_recv_unsupp
,
1386 .attr_count
= NFQA_MAX
, },
1387 [NFQNL_MSG_VERDICT
] = { .call_rcu
= nfqnl_recv_verdict
,
1388 .attr_count
= NFQA_MAX
,
1389 .policy
= nfqa_verdict_policy
},
1390 [NFQNL_MSG_CONFIG
] = { .call
= nfqnl_recv_config
,
1391 .attr_count
= NFQA_CFG_MAX
,
1392 .policy
= nfqa_cfg_policy
},
1393 [NFQNL_MSG_VERDICT_BATCH
]={ .call_rcu
= nfqnl_recv_verdict_batch
,
1394 .attr_count
= NFQA_MAX
,
1395 .policy
= nfqa_verdict_batch_policy
},
1398 static const struct nfnetlink_subsystem nfqnl_subsys
= {
1400 .subsys_id
= NFNL_SUBSYS_QUEUE
,
1401 .cb_count
= NFQNL_MSG_MAX
,
1405 #ifdef CONFIG_PROC_FS
1407 struct seq_net_private p
;
1408 unsigned int bucket
;
1411 static struct hlist_node
*get_first(struct seq_file
*seq
)
1413 struct iter_state
*st
= seq
->private;
1415 struct nfnl_queue_net
*q
;
1420 net
= seq_file_net(seq
);
1421 q
= nfnl_queue_pernet(net
);
1422 for (st
->bucket
= 0; st
->bucket
< INSTANCE_BUCKETS
; st
->bucket
++) {
1423 if (!hlist_empty(&q
->instance_table
[st
->bucket
]))
1424 return q
->instance_table
[st
->bucket
].first
;
1429 static struct hlist_node
*get_next(struct seq_file
*seq
, struct hlist_node
*h
)
1431 struct iter_state
*st
= seq
->private;
1432 struct net
*net
= seq_file_net(seq
);
1436 struct nfnl_queue_net
*q
;
1438 if (++st
->bucket
>= INSTANCE_BUCKETS
)
1441 q
= nfnl_queue_pernet(net
);
1442 h
= q
->instance_table
[st
->bucket
].first
;
1447 static struct hlist_node
*get_idx(struct seq_file
*seq
, loff_t pos
)
1449 struct hlist_node
*head
;
1450 head
= get_first(seq
);
1453 while (pos
&& (head
= get_next(seq
, head
)))
1455 return pos
? NULL
: head
;
1458 static void *seq_start(struct seq_file
*s
, loff_t
*pos
)
1459 __acquires(nfnl_queue_pernet(seq_file_net(s
))->instances_lock
)
1461 spin_lock(&nfnl_queue_pernet(seq_file_net(s
))->instances_lock
);
1462 return get_idx(s
, *pos
);
1465 static void *seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
1468 return get_next(s
, v
);
1471 static void seq_stop(struct seq_file
*s
, void *v
)
1472 __releases(nfnl_queue_pernet(seq_file_net(s
))->instances_lock
)
1474 spin_unlock(&nfnl_queue_pernet(seq_file_net(s
))->instances_lock
);
1477 static int seq_show(struct seq_file
*s
, void *v
)
1479 const struct nfqnl_instance
*inst
= v
;
1481 seq_printf(s
, "%5u %6u %5u %1u %5u %5u %5u %8u %2d\n",
1483 inst
->peer_portid
, inst
->queue_total
,
1484 inst
->copy_mode
, inst
->copy_range
,
1485 inst
->queue_dropped
, inst
->queue_user_dropped
,
1486 inst
->id_sequence
, 1);
1490 static const struct seq_operations nfqnl_seq_ops
= {
1496 #endif /* PROC_FS */
1498 static int __net_init
nfnl_queue_net_init(struct net
*net
)
1501 struct nfnl_queue_net
*q
= nfnl_queue_pernet(net
);
1503 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++)
1504 INIT_HLIST_HEAD(&q
->instance_table
[i
]);
1506 spin_lock_init(&q
->instances_lock
);
1508 #ifdef CONFIG_PROC_FS
1509 if (!proc_create_net("nfnetlink_queue", 0440, net
->nf
.proc_netfilter
,
1510 &nfqnl_seq_ops
, sizeof(struct iter_state
)))
1513 nf_register_queue_handler(net
, &nfqh
);
1517 static void __net_exit
nfnl_queue_net_exit(struct net
*net
)
1519 struct nfnl_queue_net
*q
= nfnl_queue_pernet(net
);
1522 nf_unregister_queue_handler(net
);
1523 #ifdef CONFIG_PROC_FS
1524 remove_proc_entry("nfnetlink_queue", net
->nf
.proc_netfilter
);
1526 for (i
= 0; i
< INSTANCE_BUCKETS
; i
++)
1527 WARN_ON_ONCE(!hlist_empty(&q
->instance_table
[i
]));
1530 static void nfnl_queue_net_exit_batch(struct list_head
*net_exit_list
)
1535 static struct pernet_operations nfnl_queue_net_ops
= {
1536 .init
= nfnl_queue_net_init
,
1537 .exit
= nfnl_queue_net_exit
,
1538 .exit_batch
= nfnl_queue_net_exit_batch
,
1539 .id
= &nfnl_queue_net_id
,
1540 .size
= sizeof(struct nfnl_queue_net
),
1543 static int __init
nfnetlink_queue_init(void)
1547 status
= register_pernet_subsys(&nfnl_queue_net_ops
);
1549 pr_err("failed to register pernet ops\n");
1553 netlink_register_notifier(&nfqnl_rtnl_notifier
);
1554 status
= nfnetlink_subsys_register(&nfqnl_subsys
);
1556 pr_err("failed to create netlink socket\n");
1557 goto cleanup_netlink_notifier
;
1560 status
= register_netdevice_notifier(&nfqnl_dev_notifier
);
1562 pr_err("failed to register netdevice notifier\n");
1563 goto cleanup_netlink_subsys
;
1568 cleanup_netlink_subsys
:
1569 nfnetlink_subsys_unregister(&nfqnl_subsys
);
1570 cleanup_netlink_notifier
:
1571 netlink_unregister_notifier(&nfqnl_rtnl_notifier
);
1572 unregister_pernet_subsys(&nfnl_queue_net_ops
);
1577 static void __exit
nfnetlink_queue_fini(void)
1579 unregister_netdevice_notifier(&nfqnl_dev_notifier
);
1580 nfnetlink_subsys_unregister(&nfqnl_subsys
);
1581 netlink_unregister_notifier(&nfqnl_rtnl_notifier
);
1582 unregister_pernet_subsys(&nfnl_queue_net_ops
);
1584 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1587 MODULE_DESCRIPTION("netfilter packet queue handler");
1588 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1589 MODULE_LICENSE("GPL");
1590 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE
);
1592 module_init(nfnetlink_queue_init
);
1593 module_exit(nfnetlink_queue_fini
);