2 * This is a module which is used for queueing packets and communicating with
3 * userspace via nfetlink.
5 * (C) 2005 by Harald Welte <laforge@netfilter.org>
7 * Based on the old ipv4-only ip_queue.c:
8 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
9 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/init.h>
19 #include <linux/spinlock.h>
20 #include <linux/notifier.h>
21 #include <linux/netdevice.h>
22 #include <linux/netfilter.h>
23 #include <linux/proc_fs.h>
24 #include <linux/netfilter_ipv4.h>
25 #include <linux/netfilter_ipv6.h>
26 #include <linux/netfilter/nfnetlink.h>
27 #include <linux/netfilter/nfnetlink_queue.h>
28 #include <linux/list.h>
30 #include <net/netfilter/nf_queue.h>
32 #include <asm/atomic.h>
34 #ifdef CONFIG_BRIDGE_NETFILTER
35 #include "../bridge/br_private.h"
38 #define NFQNL_QMAX_DEFAULT 1024
41 #define QDEBUG(x, args ...) printk(KERN_DEBUG "%s(%d):%s(): " x, \
42 __FILE__, __LINE__, __FUNCTION__, \
45 #define QDEBUG(x, ...)
48 struct nfqnl_queue_entry {
49 struct list_head list;
55 struct nfqnl_instance {
56 struct hlist_node hlist; /* global list of queues */
60 unsigned int queue_maxlen;
61 unsigned int copy_range;
62 unsigned int queue_total;
63 unsigned int queue_dropped;
64 unsigned int queue_user_dropped;
66 atomic_t id_sequence; /* 'sequence' of pkt ids */
68 u_int16_t queue_num; /* number of this queue */
73 struct list_head queue_list; /* packets in queue */
76 typedef int (*nfqnl_cmpfn)(struct nfqnl_queue_entry *, unsigned long);
78 static DEFINE_RWLOCK(instances_lock);
80 #define INSTANCE_BUCKETS 16
81 static struct hlist_head instance_table[INSTANCE_BUCKETS];
83 static inline u_int8_t instance_hashfn(u_int16_t queue_num)
85 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
88 static struct nfqnl_instance *
89 __instance_lookup(u_int16_t queue_num)
91 struct hlist_head *head;
92 struct hlist_node *pos;
93 struct nfqnl_instance *inst;
95 head = &instance_table[instance_hashfn(queue_num)];
96 hlist_for_each_entry(inst, pos, head, hlist) {
97 if (inst->queue_num == queue_num)
103 static struct nfqnl_instance *
104 instance_lookup_get(u_int16_t queue_num)
106 struct nfqnl_instance *inst;
108 read_lock_bh(&instances_lock);
109 inst = __instance_lookup(queue_num);
111 atomic_inc(&inst->use);
112 read_unlock_bh(&instances_lock);
118 instance_put(struct nfqnl_instance *inst)
120 if (inst && atomic_dec_and_test(&inst->use)) {
121 QDEBUG("kfree(inst=%p)\n", inst);
126 static struct nfqnl_instance *
127 instance_create(u_int16_t queue_num, int pid)
129 struct nfqnl_instance *inst;
131 QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid);
133 write_lock_bh(&instances_lock);
134 if (__instance_lookup(queue_num)) {
136 QDEBUG("aborting, instance already exists\n");
140 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
144 inst->queue_num = queue_num;
145 inst->peer_pid = pid;
146 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
147 inst->copy_range = 0xfffff;
148 inst->copy_mode = NFQNL_COPY_NONE;
149 atomic_set(&inst->id_sequence, 0);
150 /* needs to be two, since we _put() after creation */
151 atomic_set(&inst->use, 2);
152 spin_lock_init(&inst->lock);
153 INIT_LIST_HEAD(&inst->queue_list);
155 if (!try_module_get(THIS_MODULE))
158 hlist_add_head(&inst->hlist,
159 &instance_table[instance_hashfn(queue_num)]);
161 write_unlock_bh(&instances_lock);
163 QDEBUG("successfully created new instance\n");
170 write_unlock_bh(&instances_lock);
174 static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
178 _instance_destroy2(struct nfqnl_instance *inst, int lock)
180 /* first pull it out of the global list */
182 write_lock_bh(&instances_lock);
184 QDEBUG("removing instance %p (queuenum=%u) from hash\n",
185 inst, inst->queue_num);
186 hlist_del(&inst->hlist);
189 write_unlock_bh(&instances_lock);
191 /* then flush all pending skbs from the queue */
192 nfqnl_flush(inst, NULL, 0);
194 /* and finally put the refcount */
197 module_put(THIS_MODULE);
201 __instance_destroy(struct nfqnl_instance *inst)
203 _instance_destroy2(inst, 0);
207 instance_destroy(struct nfqnl_instance *inst)
209 _instance_destroy2(inst, 1);
215 issue_verdict(struct nfqnl_queue_entry *entry, int verdict)
217 QDEBUG("entering for entry %p, verdict %u\n", entry, verdict);
219 /* TCP input path (and probably other bits) assume to be called
220 * from softirq context, not from syscall, like issue_verdict is
221 * called. TCP input path deadlocks with locks taken from timer
222 * softirq, e.g. We therefore emulate this by local_bh_disable() */
225 nf_reinject(entry->skb, entry->info, verdict);
232 __enqueue_entry(struct nfqnl_instance *queue,
233 struct nfqnl_queue_entry *entry)
235 list_add_tail(&entry->list, &queue->queue_list);
236 queue->queue_total++;
240 __nfqnl_set_mode(struct nfqnl_instance *queue,
241 unsigned char mode, unsigned int range)
246 case NFQNL_COPY_NONE:
247 case NFQNL_COPY_META:
248 queue->copy_mode = mode;
249 queue->copy_range = 0;
252 case NFQNL_COPY_PACKET:
253 queue->copy_mode = mode;
254 /* we're using struct nlattr which has 16bit nla_len */
256 queue->copy_range = 0xffff;
258 queue->copy_range = range;
268 static struct nfqnl_queue_entry *
269 find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
271 struct nfqnl_queue_entry *entry = NULL, *i;
273 spin_lock_bh(&queue->lock);
275 list_for_each_entry(i, &queue->queue_list, list) {
283 list_del(&entry->list);
284 queue->queue_total--;
287 spin_unlock_bh(&queue->lock);
293 nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
295 struct nfqnl_queue_entry *entry, *next;
297 spin_lock_bh(&queue->lock);
298 list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
299 if (!cmpfn || cmpfn(entry, data)) {
300 list_del(&entry->list);
301 queue->queue_total--;
302 issue_verdict(entry, NF_DROP);
305 spin_unlock_bh(&queue->lock);
308 static struct sk_buff *
309 nfqnl_build_packet_message(struct nfqnl_instance *queue,
310 struct nfqnl_queue_entry *entry, int *errp)
312 sk_buff_data_t old_tail;
316 struct nfqnl_msg_packet_hdr pmsg;
317 struct nlmsghdr *nlh;
318 struct nfgenmsg *nfmsg;
319 struct nf_info *entinf = entry->info;
320 struct sk_buff *entskb = entry->skb;
321 struct net_device *indev;
322 struct net_device *outdev;
327 size = NLMSG_ALIGN(sizeof(struct nfgenmsg))
328 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
329 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
330 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
331 #ifdef CONFIG_BRIDGE_NETFILTER
332 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
333 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
335 + nla_total_size(sizeof(u_int32_t)) /* mark */
336 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
337 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
339 outdev = entinf->outdev;
341 spin_lock_bh(&queue->lock);
343 switch (queue->copy_mode) {
344 case NFQNL_COPY_META:
345 case NFQNL_COPY_NONE:
349 case NFQNL_COPY_PACKET:
350 if ((entskb->ip_summed == CHECKSUM_PARTIAL ||
351 entskb->ip_summed == CHECKSUM_COMPLETE) &&
352 (*errp = skb_checksum_help(entskb))) {
353 spin_unlock_bh(&queue->lock);
356 if (queue->copy_range == 0
357 || queue->copy_range > entskb->len)
358 data_len = entskb->len;
360 data_len = queue->copy_range;
362 size += nla_total_size(data_len);
367 spin_unlock_bh(&queue->lock);
371 spin_unlock_bh(&queue->lock);
373 skb = alloc_skb(size, GFP_ATOMIC);
377 old_tail = skb->tail;
378 nlh = NLMSG_PUT(skb, 0, 0,
379 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
380 sizeof(struct nfgenmsg));
381 nfmsg = NLMSG_DATA(nlh);
382 nfmsg->nfgen_family = entinf->pf;
383 nfmsg->version = NFNETLINK_V0;
384 nfmsg->res_id = htons(queue->queue_num);
386 pmsg.packet_id = htonl(entry->id);
387 pmsg.hw_protocol = entskb->protocol;
388 pmsg.hook = entinf->hook;
390 NLA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
392 indev = entinf->indev;
394 tmp_uint = htonl(indev->ifindex);
395 #ifndef CONFIG_BRIDGE_NETFILTER
396 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
398 if (entinf->pf == PF_BRIDGE) {
399 /* Case 1: indev is physical input device, we need to
400 * look for bridge group (when called from
401 * netfilter_bridge) */
402 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint),
404 /* this is the bridge group "brX" */
405 tmp_uint = htonl(indev->br_port->br->dev->ifindex);
406 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
409 /* Case 2: indev is bridge group, we need to look for
410 * physical device (when called from ipv4) */
411 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
413 if (entskb->nf_bridge
414 && entskb->nf_bridge->physindev) {
415 tmp_uint = htonl(entskb->nf_bridge->physindev->ifindex);
416 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
417 sizeof(tmp_uint), &tmp_uint);
424 tmp_uint = htonl(outdev->ifindex);
425 #ifndef CONFIG_BRIDGE_NETFILTER
426 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
428 if (entinf->pf == PF_BRIDGE) {
429 /* Case 1: outdev is physical output device, we need to
430 * look for bridge group (when called from
431 * netfilter_bridge) */
432 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
434 /* this is the bridge group "brX" */
435 tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
436 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
439 /* Case 2: outdev is bridge group, we need to look for
440 * physical output device (when called from ipv4) */
441 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
443 if (entskb->nf_bridge
444 && entskb->nf_bridge->physoutdev) {
445 tmp_uint = htonl(entskb->nf_bridge->physoutdev->ifindex);
446 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
447 sizeof(tmp_uint), &tmp_uint);
454 tmp_uint = htonl(entskb->mark);
455 NLA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
458 if (indev && entskb->dev) {
459 struct nfqnl_msg_packet_hw phw;
460 int len = dev_parse_header(entskb, phw.hw_addr);
462 phw.hw_addrlen = htons(len);
463 NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
467 if (entskb->tstamp.tv64) {
468 struct nfqnl_msg_packet_timestamp ts;
469 struct timeval tv = ktime_to_timeval(entskb->tstamp);
470 ts.sec = cpu_to_be64(tv.tv_sec);
471 ts.usec = cpu_to_be64(tv.tv_usec);
473 NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
478 int size = nla_attr_size(data_len);
480 if (skb_tailroom(skb) < nla_total_size(data_len)) {
481 printk(KERN_WARNING "nf_queue: no tailroom!\n");
485 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
486 nla->nla_type = NFQA_PAYLOAD;
489 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
493 nlh->nlmsg_len = skb->tail - old_tail;
502 printk(KERN_ERR "nf_queue: error creating packet message\n");
507 nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
508 unsigned int queuenum)
510 int status = -EINVAL;
511 struct sk_buff *nskb;
512 struct nfqnl_instance *queue;
513 struct nfqnl_queue_entry *entry;
517 queue = instance_lookup_get(queuenum);
519 QDEBUG("no queue instance matching\n");
523 if (queue->copy_mode == NFQNL_COPY_NONE) {
524 QDEBUG("mode COPY_NONE, aborting\n");
529 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
533 "nf_queue: OOM in nfqnl_enqueue_packet()\n");
540 entry->id = atomic_inc_return(&queue->id_sequence);
542 nskb = nfqnl_build_packet_message(queue, entry, &status);
546 spin_lock_bh(&queue->lock);
548 if (!queue->peer_pid)
549 goto err_out_free_nskb;
551 if (queue->queue_total >= queue->queue_maxlen) {
552 queue->queue_dropped++;
555 printk(KERN_WARNING "nf_queue: full at %d entries, "
556 "dropping packets(s). Dropped: %d\n",
557 queue->queue_total, queue->queue_dropped);
558 goto err_out_free_nskb;
561 /* nfnetlink_unicast will either free the nskb or add it to a socket */
562 status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
564 queue->queue_user_dropped++;
568 __enqueue_entry(queue, entry);
570 spin_unlock_bh(&queue->lock);
578 spin_unlock_bh(&queue->lock);
588 nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
593 diff = data_len - e->skb->len;
595 if (pskb_trim(e->skb, data_len))
597 } else if (diff > 0) {
598 if (data_len > 0xFFFF)
600 if (diff > skb_tailroom(e->skb)) {
601 err = pskb_expand_head(e->skb, 0,
602 diff - skb_tailroom(e->skb),
605 printk(KERN_WARNING "nf_queue: OOM "
606 "in mangle, dropping packet\n");
610 skb_put(e->skb, diff);
612 if (!skb_make_writable(e->skb, data_len))
614 skb_copy_to_linear_data(e->skb, data, data_len);
615 e->skb->ip_summed = CHECKSUM_NONE;
620 nfqnl_set_mode(struct nfqnl_instance *queue,
621 unsigned char mode, unsigned int range)
625 spin_lock_bh(&queue->lock);
626 status = __nfqnl_set_mode(queue, mode, range);
627 spin_unlock_bh(&queue->lock);
633 dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex)
635 struct nf_info *entinf = entry->info;
638 if (entinf->indev->ifindex == ifindex)
641 if (entinf->outdev->ifindex == ifindex)
643 #ifdef CONFIG_BRIDGE_NETFILTER
644 if (entry->skb->nf_bridge) {
645 if (entry->skb->nf_bridge->physindev &&
646 entry->skb->nf_bridge->physindev->ifindex == ifindex)
648 if (entry->skb->nf_bridge->physoutdev &&
649 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
656 /* drop all packets with either indev or outdev == ifindex from all queue
659 nfqnl_dev_drop(int ifindex)
663 QDEBUG("entering for ifindex %u\n", ifindex);
665 /* this only looks like we have to hold the readlock for a way too long
666 * time, issue_verdict(), nf_reinject(), ... - but we always only
667 * issue NF_DROP, which is processed directly in nf_reinject() */
668 read_lock_bh(&instances_lock);
670 for (i = 0; i < INSTANCE_BUCKETS; i++) {
671 struct hlist_node *tmp;
672 struct nfqnl_instance *inst;
673 struct hlist_head *head = &instance_table[i];
675 hlist_for_each_entry(inst, tmp, head, hlist)
676 nfqnl_flush(inst, dev_cmp, ifindex);
679 read_unlock_bh(&instances_lock);
682 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
685 nfqnl_rcv_dev_event(struct notifier_block *this,
686 unsigned long event, void *ptr)
688 struct net_device *dev = ptr;
690 if (dev->nd_net != &init_net)
693 /* Drop any packets associated with the downed device */
694 if (event == NETDEV_DOWN)
695 nfqnl_dev_drop(dev->ifindex);
699 static struct notifier_block nfqnl_dev_notifier = {
700 .notifier_call = nfqnl_rcv_dev_event,
704 nfqnl_rcv_nl_event(struct notifier_block *this,
705 unsigned long event, void *ptr)
707 struct netlink_notify *n = ptr;
709 if (event == NETLINK_URELEASE &&
710 n->protocol == NETLINK_NETFILTER && n->pid) {
713 /* destroy all instances for this pid */
714 write_lock_bh(&instances_lock);
715 for (i = 0; i < INSTANCE_BUCKETS; i++) {
716 struct hlist_node *tmp, *t2;
717 struct nfqnl_instance *inst;
718 struct hlist_head *head = &instance_table[i];
720 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
721 if ((n->net == &init_net) &&
722 (n->pid == inst->peer_pid))
723 __instance_destroy(inst);
726 write_unlock_bh(&instances_lock);
731 static struct notifier_block nfqnl_rtnl_notifier = {
732 .notifier_call = nfqnl_rcv_nl_event,
735 static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
736 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
737 [NFQA_MARK] = { .type = NLA_U32 },
738 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
742 nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
743 struct nlmsghdr *nlh, struct nlattr *nfqa[])
745 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
746 u_int16_t queue_num = ntohs(nfmsg->res_id);
748 struct nfqnl_msg_verdict_hdr *vhdr;
749 struct nfqnl_instance *queue;
750 unsigned int verdict;
751 struct nfqnl_queue_entry *entry;
754 queue = instance_lookup_get(queue_num);
758 if (queue->peer_pid != NETLINK_CB(skb).pid) {
763 if (!nfqa[NFQA_VERDICT_HDR]) {
768 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
769 verdict = ntohl(vhdr->verdict);
771 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
776 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
782 if (nfqa[NFQA_PAYLOAD]) {
783 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
784 nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0)
789 entry->skb->mark = ntohl(*(__be32 *)
790 nla_data(nfqa[NFQA_MARK]));
792 issue_verdict(entry, verdict);
802 nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
803 struct nlmsghdr *nlh, struct nlattr *nfqa[])
808 static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
809 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
810 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
813 static const struct nf_queue_handler nfqh = {
815 .outfn = &nfqnl_enqueue_packet,
819 nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
820 struct nlmsghdr *nlh, struct nlattr *nfqa[])
822 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
823 u_int16_t queue_num = ntohs(nfmsg->res_id);
824 struct nfqnl_instance *queue;
827 QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
829 queue = instance_lookup_get(queue_num);
830 if (nfqa[NFQA_CFG_CMD]) {
831 struct nfqnl_msg_config_cmd *cmd;
832 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
833 QDEBUG("found CFG_CMD\n");
835 switch (cmd->command) {
836 case NFQNL_CFG_CMD_BIND:
840 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
844 case NFQNL_CFG_CMD_UNBIND:
848 if (queue->peer_pid != NETLINK_CB(skb).pid) {
853 instance_destroy(queue);
855 case NFQNL_CFG_CMD_PF_BIND:
856 QDEBUG("registering queue handler for pf=%u\n",
858 ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh);
860 case NFQNL_CFG_CMD_PF_UNBIND:
861 QDEBUG("unregistering queue handler for pf=%u\n",
863 ret = nf_unregister_queue_handler(ntohs(cmd->pf), &nfqh);
871 QDEBUG("no config command, and no instance ENOENT\n");
876 if (queue->peer_pid != NETLINK_CB(skb).pid) {
877 QDEBUG("no config command, and wrong pid\n");
883 if (nfqa[NFQA_CFG_PARAMS]) {
884 struct nfqnl_msg_config_params *params;
890 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
891 nfqnl_set_mode(queue, params->copy_mode,
892 ntohl(params->copy_range));
895 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
896 __be32 *queue_maxlen;
897 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
898 spin_lock_bh(&queue->lock);
899 queue->queue_maxlen = ntohl(*queue_maxlen);
900 spin_unlock_bh(&queue->lock);
908 static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
909 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
910 .attr_count = NFQA_MAX, },
911 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
912 .attr_count = NFQA_MAX,
913 .policy = nfqa_verdict_policy },
914 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
915 .attr_count = NFQA_CFG_MAX,
916 .policy = nfqa_cfg_policy },
919 static const struct nfnetlink_subsystem nfqnl_subsys = {
921 .subsys_id = NFNL_SUBSYS_QUEUE,
922 .cb_count = NFQNL_MSG_MAX,
926 #ifdef CONFIG_PROC_FS
931 static struct hlist_node *get_first(struct seq_file *seq)
933 struct iter_state *st = seq->private;
938 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
939 if (!hlist_empty(&instance_table[st->bucket]))
940 return instance_table[st->bucket].first;
945 static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
947 struct iter_state *st = seq->private;
951 if (++st->bucket >= INSTANCE_BUCKETS)
954 h = instance_table[st->bucket].first;
959 static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
961 struct hlist_node *head;
962 head = get_first(seq);
965 while (pos && (head = get_next(seq, head)))
967 return pos ? NULL : head;
970 static void *seq_start(struct seq_file *seq, loff_t *pos)
972 read_lock_bh(&instances_lock);
973 return get_idx(seq, *pos);
976 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
979 return get_next(s, v);
982 static void seq_stop(struct seq_file *s, void *v)
984 read_unlock_bh(&instances_lock);
987 static int seq_show(struct seq_file *s, void *v)
989 const struct nfqnl_instance *inst = v;
991 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
993 inst->peer_pid, inst->queue_total,
994 inst->copy_mode, inst->copy_range,
995 inst->queue_dropped, inst->queue_user_dropped,
996 atomic_read(&inst->id_sequence),
997 atomic_read(&inst->use));
1000 static const struct seq_operations nfqnl_seq_ops = {
1007 static int nfqnl_open(struct inode *inode, struct file *file)
1009 return seq_open_private(file, &nfqnl_seq_ops,
1010 sizeof(struct iter_state));
1013 static const struct file_operations nfqnl_file_ops = {
1014 .owner = THIS_MODULE,
1017 .llseek = seq_lseek,
1018 .release = seq_release_private,
1021 #endif /* PROC_FS */
1023 static int __init nfnetlink_queue_init(void)
1025 int i, status = -ENOMEM;
1026 #ifdef CONFIG_PROC_FS
1027 struct proc_dir_entry *proc_nfqueue;
1030 for (i = 0; i < INSTANCE_BUCKETS; i++)
1031 INIT_HLIST_HEAD(&instance_table[i]);
1033 netlink_register_notifier(&nfqnl_rtnl_notifier);
1034 status = nfnetlink_subsys_register(&nfqnl_subsys);
1036 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1037 goto cleanup_netlink_notifier;
1040 #ifdef CONFIG_PROC_FS
1041 proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
1042 proc_net_netfilter);
1044 goto cleanup_subsys;
1045 proc_nfqueue->proc_fops = &nfqnl_file_ops;
1048 register_netdevice_notifier(&nfqnl_dev_notifier);
1051 #ifdef CONFIG_PROC_FS
1053 nfnetlink_subsys_unregister(&nfqnl_subsys);
1055 cleanup_netlink_notifier:
1056 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1060 static void __exit nfnetlink_queue_fini(void)
1062 nf_unregister_queue_handlers(&nfqh);
1063 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1064 #ifdef CONFIG_PROC_FS
1065 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1067 nfnetlink_subsys_unregister(&nfqnl_subsys);
1068 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1071 MODULE_DESCRIPTION("netfilter packet queue handler");
1072 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1073 MODULE_LICENSE("GPL");
1074 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1076 module_init(nfnetlink_queue_init);
1077 module_exit(nfnetlink_queue_fini);