drm/msm/dpu: use encoder type to identify display type
[linux/fpc-iii.git] / net / netfilter / nfnetlink_queue.c
blobd33094f4ec41e380c57b57b26975f263cfe6bf8f
1 /*
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>
36 #include <net/sock.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"
45 #endif
47 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
48 #include <net/netfilter/nf_conntrack.h>
49 #endif
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 */
63 struct rcu_head rcu;
65 u32 peer_portid;
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 */
73 u_int8_t copy_mode;
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)
114 return inst;
116 return NULL;
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;
123 unsigned int h;
124 int err;
126 spin_lock(&q->instances_lock);
127 if (instance_lookup(q, queue_num)) {
128 err = -EEXIST;
129 goto out_unlock;
132 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
133 if (!inst) {
134 err = -ENOMEM;
135 goto out_unlock;
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)) {
147 err = -EAGAIN;
148 goto out_free;
151 h = instance_hashfn(queue_num);
152 hlist_add_head_rcu(&inst->hlist, &q->instance_table[h]);
154 spin_unlock(&q->instances_lock);
156 return inst;
158 out_free:
159 kfree(inst);
160 out_unlock:
161 spin_unlock(&q->instances_lock);
162 return ERR_PTR(err);
165 static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
166 unsigned long data);
168 static void
169 instance_destroy_rcu(struct rcu_head *head)
171 struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
172 rcu);
174 nfqnl_flush(inst, NULL, 0);
175 kfree(inst);
176 module_put(THIS_MODULE);
179 static void
180 __instance_destroy(struct nfqnl_instance *inst)
182 hlist_del_rcu(&inst->hlist);
183 call_rcu(&inst->rcu, instance_destroy_rcu);
186 static void
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);
194 static inline void
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++;
201 static void
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) {
216 if (i->id == id) {
217 entry = i;
218 break;
222 if (entry)
223 __dequeue_entry(queue, entry);
225 spin_unlock_bh(&queue->lock);
227 return entry;
230 static void nfqnl_reinject(struct nf_queue_entry *entry, unsigned int verdict)
232 struct nf_ct_hook *ct_hook;
233 int err;
235 if (verdict == NF_ACCEPT ||
236 verdict == NF_REPEAT ||
237 verdict == NF_STOP) {
238 rcu_read_lock();
239 ct_hook = rcu_dereference(nf_ct_hook);
240 if (ct_hook) {
241 err = ct_hook->update(entry->state.net, entry->skb);
242 if (err < 0)
243 verdict = NF_DROP;
245 rcu_read_unlock();
247 nf_reinject(entry, verdict);
250 static void
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);
266 static int
267 nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet,
268 bool csum_verify)
270 __u32 flags = 0;
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))
288 return 0;
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);
301 return 0;
303 nla_put_failure:
304 read_unlock_bh(&sk->sk_callback_lock);
305 return -1;
308 static u32 nfqnl_get_sk_secctx(struct sk_buff *skb, char **secdata)
310 u32 seclen = 0;
311 #if IS_ENABLED(CONFIG_NETWORK_SECMARK)
312 if (!skb || !sk_fullsock(skb->sk))
313 return 0;
315 read_lock_bh(&skb->sk->sk_callback_lock);
317 if (skb->secmark)
318 security_secid_to_secctx(skb->secmark, secdata, &seclen);
320 read_unlock_bh(&skb->sk->sk_callback_lock);
321 #endif
322 return seclen;
325 static u32 nfqnl_get_bridge_size(struct nf_queue_entry *entry)
327 struct sk_buff *entskb = entry->skb;
328 u32 nlalen = 0;
330 if (entry->state.pf != PF_BRIDGE || !skb_mac_header_was_set(entskb))
331 return 0;
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));
341 return nlalen;
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))
349 return 0;
351 if (skb_vlan_tag_present(entskb)) {
352 struct nlattr *nest;
354 nest = nla_nest_start(skb, NFQA_VLAN | NLA_F_NESTED);
355 if (!nest)
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;
372 return 0;
374 nla_put_failure:
375 return -1;
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)
383 size_t size;
384 size_t data_len = 0, cap_len = 0;
385 unsigned int hlen = 0;
386 struct sk_buff *skb;
387 struct nlattr *nla;
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;
397 bool csum_verify;
398 char *secdata = NULL;
399 u32 seclen = 0;
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 */
408 #endif
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 */
414 if (entskb->tstamp)
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);
422 else
423 csum_verify = false;
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:
430 break;
432 case NFQNL_COPY_PACKET:
433 if (!(queue->flags & NFQA_CFG_F_GSO) &&
434 entskb->ip_summed == CHECKSUM_PARTIAL &&
435 skb_checksum_help(entskb))
436 return NULL;
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;
446 break;
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);
454 if (ct != NULL)
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);
466 if (seclen)
467 size += nla_total_size(seclen);
470 skb = alloc_skb(size, GFP_ATOMIC);
471 if (!skb) {
472 skb_tx_error(entskb);
473 goto nlmsg_failure;
476 nlh = nlmsg_put(skb, 0, 0,
477 nfnl_msg_type(NFNL_SUBSYS_QUEUE, NFQNL_MSG_PACKET),
478 sizeof(struct nfgenmsg), 0);
479 if (!nlh) {
480 skb_tx_error(entskb);
481 kfree_skb(skb);
482 goto nlmsg_failure;
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;
496 if (indev) {
497 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
498 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
499 goto nla_put_failure;
500 #else
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;
512 } else {
513 int physinif;
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);
522 if (physinif &&
523 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
524 htonl(physinif)))
525 goto nla_put_failure;
527 #endif
530 if (outdev) {
531 #if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
532 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
533 goto nla_put_failure;
534 #else
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;
546 } else {
547 int physoutif;
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);
556 if (physoutif &&
557 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
558 htonl(physoutif)))
559 goto nla_put_failure;
561 #endif
564 if (entskb->mark &&
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;
571 int len;
573 memset(&phw, 0, sizeof(phw));
574 len = dev_parse_header(entskb, phw.hw_addr);
575 if (len) {
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;
613 if (data_len) {
614 struct nlattr *nla;
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;
628 if (seclen)
629 security_release_secctx(secdata, seclen);
630 return skb;
632 nla_put_failure:
633 skb_tx_error(entskb);
634 kfree_skb(skb);
635 net_err_ratelimited("nf_queue: error creating packet message\n");
636 nlmsg_failure:
637 if (seclen)
638 security_release_secctx(secdata, seclen);
639 return NULL;
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))
649 return true;
650 #endif
651 return false;
654 static int
655 __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,
656 struct nf_queue_entry *entry)
658 struct sk_buff *nskb;
659 int err = -ENOBUFS;
660 __be32 *packet_id_ptr;
661 int failopen = 0;
663 nskb = nfqnl_build_packet_message(net, queue, entry, &packet_id_ptr);
664 if (nskb == NULL) {
665 err = -ENOMEM;
666 goto err_out;
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) {
675 failopen = 1;
676 err = 0;
677 } else {
678 queue->queue_dropped++;
679 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
680 queue->queue_total);
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);
689 if (err < 0) {
690 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
691 failopen = 1;
692 err = 0;
693 } else {
694 queue->queue_user_dropped++;
696 goto err_out_unlock;
699 __enqueue_entry(queue, entry);
701 spin_unlock_bh(&queue->lock);
702 return 0;
704 err_out_free_nskb:
705 kfree_skb(nskb);
706 err_out_unlock:
707 spin_unlock_bh(&queue->lock);
708 if (failopen)
709 nfqnl_reinject(entry, NF_ACCEPT);
710 err_out:
711 return err;
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);
718 if (entry)
719 nf_queue_entry_get_refs(entry);
720 return 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)
730 if (skb->nf_bridge)
731 __skb_push(skb, skb->network_header - skb->mac_header);
734 static void nf_bridge_adjust_segmented_data(struct sk_buff *skb)
736 if (skb->nf_bridge)
737 __skb_pull(skb, skb->network_header - skb->mac_header);
739 #else
740 #define nf_bridge_adjust_skb_data(s) do {} while (0)
741 #define nf_bridge_adjust_segmented_data(s) do {} while (0)
742 #endif
744 static void free_entry(struct nf_queue_entry *entry)
746 nf_queue_entry_release_refs(entry);
747 kfree(entry);
750 static int
751 __nfqnl_enqueue_packet_gso(struct net *net, struct nfqnl_instance *queue,
752 struct sk_buff *skb, struct nf_queue_entry *entry)
754 int ret = -ENOMEM;
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;
761 entry->skb = skb;
762 ret = __nfqnl_enqueue_packet(net, queue, entry);
763 if (ret)
764 entry->skb = gso_skb;
765 return ret;
768 skb->next = NULL;
770 entry_seg = nf_queue_entry_dup(entry);
771 if (entry_seg) {
772 entry_seg->skb = skb;
773 ret = __nfqnl_enqueue_packet(net, queue, entry_seg);
774 if (ret)
775 free_entry(entry_seg);
777 return ret;
780 static int
781 nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
783 unsigned int queued;
784 struct nfqnl_instance *queue;
785 struct sk_buff *skb, *segs;
786 int err = -ENOBUFS;
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);
792 if (!queue)
793 return -ESRCH;
795 if (queue->copy_mode == NFQNL_COPY_NONE)
796 return -EINVAL;
798 skb = entry->skb;
800 switch (entry->state.pf) {
801 case NFPROTO_IPV4:
802 skb->protocol = htons(ETH_P_IP);
803 break;
804 case NFPROTO_IPV6:
805 skb->protocol = htons(ETH_P_IPV6);
806 break;
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))
819 goto out_err;
820 queued = 0;
821 err = 0;
822 do {
823 struct sk_buff *nskb = segs->next;
824 if (err == 0)
825 err = __nfqnl_enqueue_packet_gso(net, queue,
826 segs, entry);
827 if (err == 0)
828 queued++;
829 else
830 kfree_skb(segs);
831 segs = nskb;
832 } while (segs);
834 if (queued) {
835 if (err) /* some segments are already queued */
836 free_entry(entry);
837 kfree_skb(skb);
838 return 0;
840 out_err:
841 nf_bridge_adjust_segmented_data(skb);
842 return err;
845 static int
846 nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff)
848 struct sk_buff *nskb;
850 if (diff < 0) {
851 if (pskb_trim(e->skb, data_len))
852 return -ENOMEM;
853 } else if (diff > 0) {
854 if (data_len > 0xFFFF)
855 return -EINVAL;
856 if (diff > skb_tailroom(e->skb)) {
857 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
858 diff, GFP_ATOMIC);
859 if (!nskb)
860 return -ENOMEM;
861 kfree_skb(e->skb);
862 e->skb = nskb;
864 skb_put(e->skb, diff);
866 if (!skb_make_writable(e->skb, data_len))
867 return -ENOMEM;
868 skb_copy_to_linear_data(e->skb, data, data_len);
869 e->skb->ip_summed = CHECKSUM_NONE;
870 return 0;
873 static int
874 nfqnl_set_mode(struct nfqnl_instance *queue,
875 unsigned char mode, unsigned int range)
877 int status = 0;
879 spin_lock_bh(&queue->lock);
880 switch (mode) {
881 case NFQNL_COPY_NONE:
882 case NFQNL_COPY_META:
883 queue->copy_mode = mode;
884 queue->copy_range = 0;
885 break;
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;
891 else
892 queue->copy_range = range;
893 break;
895 default:
896 status = -EINVAL;
899 spin_unlock_bh(&queue->lock);
901 return status;
904 static int
905 dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
907 if (entry->state.in)
908 if (entry->state.in->ifindex == ifindex)
909 return 1;
910 if (entry->state.out)
911 if (entry->state.out->ifindex == ifindex)
912 return 1;
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)
921 return 1;
923 #endif
924 return 0;
927 /* drop all packets with either indev or outdev == ifindex from all queue
928 * instances */
929 static void
930 nfqnl_dev_drop(struct net *net, int ifindex)
932 int i;
933 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
935 rcu_read_lock();
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);
945 rcu_read_unlock();
948 static int
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);
957 return NOTIFY_DONE;
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);
967 int i;
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);
978 static int
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) {
986 int i;
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);
1002 return NOTIFY_DONE;
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);
1034 if (!queue)
1035 return ERR_PTR(-ENODEV);
1037 if (queue->peer_portid != nlportid)
1038 return ERR_PTR(-EPERM);
1040 return queue;
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])
1050 return NULL;
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)
1055 return NULL;
1056 return vhdr;
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);
1081 if (IS_ERR(queue))
1082 return PTR_ERR(queue);
1084 vhdr = verdicthdr_get(nfqa);
1085 if (!vhdr)
1086 return -EINVAL;
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))
1095 break;
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))
1103 return -ENOENT;
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);
1111 return 0;
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)
1120 struct nf_conn *ct;
1122 ct = nfnl_ct->get_ct(entry->skb, ctinfo);
1123 if (ct == NULL)
1124 return NULL;
1126 if (nfnl_ct->parse(nfqa[NFQA_CT], ct) < 0)
1127 return NULL;
1129 if (nfqa[NFQA_EXP])
1130 nfnl_ct->attach_expect(nfqa[NFQA_EXP], ct,
1131 NETLINK_CB(entry->skb).portid,
1132 nlmsg_report(nlh));
1133 return ct;
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];
1141 int err;
1143 err = nla_parse_nested(tb, NFQA_VLAN_MAX, nfqa[NFQA_VLAN],
1144 nfqa_vlan_policy, NULL);
1145 if (err < 0)
1146 return err;
1148 if (!tb[NFQA_VLAN_TCI] || !tb[NFQA_VLAN_PROTO])
1149 return -EINVAL;
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]))
1160 return -EINVAL;
1161 else if (mac_header_len > 0)
1162 memcpy(skb_mac_header(entry->skb),
1163 nla_data(nfqa[NFQA_L2HDR]),
1164 mac_header_len);
1167 return 0;
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);
1186 int err;
1188 queue = verdict_instance_lookup(q, queue_num,
1189 NETLINK_CB(skb).portid);
1190 if (IS_ERR(queue))
1191 return PTR_ERR(queue);
1193 vhdr = verdicthdr_get(nfqa);
1194 if (!vhdr)
1195 return -EINVAL;
1197 verdict = ntohl(vhdr->verdict);
1199 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
1200 if (entry == NULL)
1201 return -ENOENT;
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);
1213 if (err < 0)
1214 return err;
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)
1223 verdict = NF_DROP;
1225 if (ct && diff)
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);
1233 return 0;
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)
1241 return -ENOTSUPP;
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;
1268 int ret = 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
1286 * changed.
1288 return -EINVAL;
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)
1295 return -EOPNOTSUPP;
1297 #if !IS_ENABLED(CONFIG_NETWORK_SECMARK)
1298 if (flags & mask & NFQA_CFG_F_SECCTX)
1299 return -EOPNOTSUPP;
1300 #endif
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))
1308 return -EAGAIN;
1309 #endif
1310 return -EOPNOTSUPP;
1314 rcu_read_lock();
1315 queue = instance_lookup(q, queue_num);
1316 if (queue && queue->peer_portid != NETLINK_CB(skb).portid) {
1317 ret = -EPERM;
1318 goto err_out_unlock;
1321 if (cmd != NULL) {
1322 switch (cmd->command) {
1323 case NFQNL_CFG_CMD_BIND:
1324 if (queue) {
1325 ret = -EBUSY;
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;
1334 break;
1335 case NFQNL_CFG_CMD_UNBIND:
1336 if (!queue) {
1337 ret = -ENODEV;
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:
1344 break;
1345 default:
1346 ret = -ENOTSUPP;
1347 goto err_out_unlock;
1351 if (!queue) {
1352 ret = -ENODEV;
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);
1379 err_out_unlock:
1380 rcu_read_unlock();
1381 return ret;
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 = {
1399 .name = "nf_queue",
1400 .subsys_id = NFNL_SUBSYS_QUEUE,
1401 .cb_count = NFQNL_MSG_MAX,
1402 .cb = nfqnl_cb,
1405 #ifdef CONFIG_PROC_FS
1406 struct iter_state {
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;
1414 struct net *net;
1415 struct nfnl_queue_net *q;
1417 if (!st)
1418 return NULL;
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;
1426 return NULL;
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);
1434 h = h->next;
1435 while (!h) {
1436 struct nfnl_queue_net *q;
1438 if (++st->bucket >= INSTANCE_BUCKETS)
1439 return NULL;
1441 q = nfnl_queue_pernet(net);
1442 h = q->instance_table[st->bucket].first;
1444 return h;
1447 static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
1449 struct hlist_node *head;
1450 head = get_first(seq);
1452 if (head)
1453 while (pos && (head = get_next(seq, head)))
1454 pos--;
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)
1467 (*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",
1482 inst->queue_num,
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);
1487 return 0;
1490 static const struct seq_operations nfqnl_seq_ops = {
1491 .start = seq_start,
1492 .next = seq_next,
1493 .stop = seq_stop,
1494 .show = seq_show,
1496 #endif /* PROC_FS */
1498 static int __net_init nfnl_queue_net_init(struct net *net)
1500 unsigned int i;
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)))
1511 return -ENOMEM;
1512 #endif
1513 nf_register_queue_handler(net, &nfqh);
1514 return 0;
1517 static void __net_exit nfnl_queue_net_exit(struct net *net)
1519 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
1520 unsigned int i;
1522 nf_unregister_queue_handler(net);
1523 #ifdef CONFIG_PROC_FS
1524 remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter);
1525 #endif
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)
1532 synchronize_rcu();
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)
1545 int status;
1547 status = register_pernet_subsys(&nfnl_queue_net_ops);
1548 if (status < 0) {
1549 pr_err("failed to register pernet ops\n");
1550 goto out;
1553 netlink_register_notifier(&nfqnl_rtnl_notifier);
1554 status = nfnetlink_subsys_register(&nfqnl_subsys);
1555 if (status < 0) {
1556 pr_err("failed to create netlink socket\n");
1557 goto cleanup_netlink_notifier;
1560 status = register_netdevice_notifier(&nfqnl_dev_notifier);
1561 if (status < 0) {
1562 pr_err("failed to register netdevice notifier\n");
1563 goto cleanup_netlink_subsys;
1566 return status;
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);
1573 out:
1574 return status;
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);