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>
31 #include <asm/atomic.h>
33 #ifdef CONFIG_BRIDGE_NETFILTER
34 #include "../bridge/br_private.h"
37 #define NFQNL_QMAX_DEFAULT 1024
40 #define QDEBUG(x, args ...) printk(KERN_DEBUG "%s(%d):%s(): " x, \
41 __FILE__, __LINE__, __FUNCTION__, \
44 #define QDEBUG(x, ...)
47 struct nfqnl_queue_entry {
48 struct list_head list;
54 struct nfqnl_instance {
55 struct hlist_node hlist; /* global list of queues */
59 unsigned int queue_maxlen;
60 unsigned int copy_range;
61 unsigned int queue_total;
62 unsigned int queue_dropped;
63 unsigned int queue_user_dropped;
65 atomic_t id_sequence; /* 'sequence' of pkt ids */
67 u_int16_t queue_num; /* number of this queue */
72 struct list_head queue_list; /* packets in queue */
75 typedef int (*nfqnl_cmpfn)(struct nfqnl_queue_entry *, unsigned long);
77 static DEFINE_RWLOCK(instances_lock);
79 u_int64_t htonll(u_int64_t in)
84 for (i = 0; i < sizeof(u_int64_t); i++)
85 ((u_int8_t *)&out)[sizeof(u_int64_t)-1] = ((u_int8_t *)&in)[i];
90 #define INSTANCE_BUCKETS 16
91 static struct hlist_head instance_table[INSTANCE_BUCKETS];
93 static inline u_int8_t instance_hashfn(u_int16_t queue_num)
95 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
98 static struct nfqnl_instance *
99 __instance_lookup(u_int16_t queue_num)
101 struct hlist_head *head;
102 struct hlist_node *pos;
103 struct nfqnl_instance *inst;
105 head = &instance_table[instance_hashfn(queue_num)];
106 hlist_for_each_entry(inst, pos, head, hlist) {
107 if (inst->queue_num == queue_num)
113 static struct nfqnl_instance *
114 instance_lookup_get(u_int16_t queue_num)
116 struct nfqnl_instance *inst;
118 read_lock_bh(&instances_lock);
119 inst = __instance_lookup(queue_num);
121 atomic_inc(&inst->use);
122 read_unlock_bh(&instances_lock);
128 instance_put(struct nfqnl_instance *inst)
130 if (inst && atomic_dec_and_test(&inst->use)) {
131 QDEBUG("kfree(inst=%p)\n", inst);
136 static struct nfqnl_instance *
137 instance_create(u_int16_t queue_num, int pid)
139 struct nfqnl_instance *inst;
141 QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid);
143 write_lock_bh(&instances_lock);
144 if (__instance_lookup(queue_num)) {
146 QDEBUG("aborting, instance already exists\n");
150 inst = kmalloc(sizeof(*inst), GFP_ATOMIC);
154 memset(inst, 0, sizeof(*inst));
155 inst->queue_num = queue_num;
156 inst->peer_pid = pid;
157 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
158 inst->copy_range = 0xfffff;
159 inst->copy_mode = NFQNL_COPY_NONE;
160 atomic_set(&inst->id_sequence, 0);
161 /* needs to be two, since we _put() after creation */
162 atomic_set(&inst->use, 2);
163 inst->lock = SPIN_LOCK_UNLOCKED;
164 INIT_LIST_HEAD(&inst->queue_list);
166 if (!try_module_get(THIS_MODULE))
169 hlist_add_head(&inst->hlist,
170 &instance_table[instance_hashfn(queue_num)]);
172 write_unlock_bh(&instances_lock);
174 QDEBUG("successfully created new instance\n");
181 write_unlock_bh(&instances_lock);
185 static void nfqnl_flush(struct nfqnl_instance *queue, int verdict);
188 _instance_destroy2(struct nfqnl_instance *inst, int lock)
190 /* first pull it out of the global list */
192 write_lock_bh(&instances_lock);
194 QDEBUG("removing instance %p (queuenum=%u) from hash\n",
195 inst, inst->queue_num);
196 hlist_del(&inst->hlist);
199 write_unlock_bh(&instances_lock);
201 /* then flush all pending skbs from the queue */
202 nfqnl_flush(inst, NF_DROP);
204 /* and finally put the refcount */
207 module_put(THIS_MODULE);
211 __instance_destroy(struct nfqnl_instance *inst)
213 _instance_destroy2(inst, 0);
217 instance_destroy(struct nfqnl_instance *inst)
219 _instance_destroy2(inst, 1);
225 issue_verdict(struct nfqnl_queue_entry *entry, int verdict)
227 QDEBUG("entering for entry %p, verdict %u\n", entry, verdict);
229 /* TCP input path (and probably other bits) assume to be called
230 * from softirq context, not from syscall, like issue_verdict is
231 * called. TCP input path deadlocks with locks taken from timer
232 * softirq, e.g. We therefore emulate this by local_bh_disable() */
235 nf_reinject(entry->skb, entry->info, verdict);
242 __enqueue_entry(struct nfqnl_instance *queue,
243 struct nfqnl_queue_entry *entry)
245 list_add(&entry->list, &queue->queue_list);
246 queue->queue_total++;
250 * Find and return a queued entry matched by cmpfn, or return the last
251 * entry if cmpfn is NULL.
253 static inline struct nfqnl_queue_entry *
254 __find_entry(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
259 list_for_each_prev(p, &queue->queue_list) {
260 struct nfqnl_queue_entry *entry = (struct nfqnl_queue_entry *)p;
262 if (!cmpfn || cmpfn(entry, data))
269 __dequeue_entry(struct nfqnl_instance *q, struct nfqnl_queue_entry *entry)
271 list_del(&entry->list);
275 static inline struct nfqnl_queue_entry *
276 __find_dequeue_entry(struct nfqnl_instance *queue,
277 nfqnl_cmpfn cmpfn, unsigned long data)
279 struct nfqnl_queue_entry *entry;
281 entry = __find_entry(queue, cmpfn, data);
285 __dequeue_entry(queue, entry);
291 __nfqnl_flush(struct nfqnl_instance *queue, int verdict)
293 struct nfqnl_queue_entry *entry;
295 while ((entry = __find_dequeue_entry(queue, NULL, 0)))
296 issue_verdict(entry, verdict);
300 __nfqnl_set_mode(struct nfqnl_instance *queue,
301 unsigned char mode, unsigned int range)
306 case NFQNL_COPY_NONE:
307 case NFQNL_COPY_META:
308 queue->copy_mode = mode;
309 queue->copy_range = 0;
312 case NFQNL_COPY_PACKET:
313 queue->copy_mode = mode;
314 /* we're using struct nfattr which has 16bit nfa_len */
316 queue->copy_range = 0xffff;
318 queue->copy_range = range;
328 static struct nfqnl_queue_entry *
329 find_dequeue_entry(struct nfqnl_instance *queue,
330 nfqnl_cmpfn cmpfn, unsigned long data)
332 struct nfqnl_queue_entry *entry;
334 spin_lock_bh(&queue->lock);
335 entry = __find_dequeue_entry(queue, cmpfn, data);
336 spin_unlock_bh(&queue->lock);
342 nfqnl_flush(struct nfqnl_instance *queue, int verdict)
344 spin_lock_bh(&queue->lock);
345 __nfqnl_flush(queue, verdict);
346 spin_unlock_bh(&queue->lock);
349 static struct sk_buff *
350 nfqnl_build_packet_message(struct nfqnl_instance *queue,
351 struct nfqnl_queue_entry *entry, int *errp)
353 unsigned char *old_tail;
357 struct nfqnl_msg_packet_hdr pmsg;
358 struct nlmsghdr *nlh;
359 struct nfgenmsg *nfmsg;
360 unsigned int tmp_uint;
364 /* all macros expand to constant values at compile time */
365 size = NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hdr))
366 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
367 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
368 #ifdef CONFIG_BRIDGE_NETFILTER
369 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
370 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
372 + NLMSG_SPACE(sizeof(u_int32_t)) /* mark */
373 + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hw))
374 + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_timestamp));
376 spin_lock_bh(&queue->lock);
378 switch (queue->copy_mode) {
379 case NFQNL_COPY_META:
380 case NFQNL_COPY_NONE:
384 case NFQNL_COPY_PACKET:
385 if (queue->copy_range == 0
386 || queue->copy_range > entry->skb->len)
387 data_len = entry->skb->len;
389 data_len = queue->copy_range;
391 size += NLMSG_SPACE(data_len);
396 spin_unlock_bh(&queue->lock);
400 spin_unlock_bh(&queue->lock);
402 skb = alloc_skb(size, GFP_ATOMIC);
407 nlh = NLMSG_PUT(skb, 0, 0,
408 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
409 sizeof(struct nfgenmsg));
410 nfmsg = NLMSG_DATA(nlh);
411 nfmsg->nfgen_family = entry->info->pf;
412 nfmsg->version = NFNETLINK_V0;
413 nfmsg->res_id = htons(queue->queue_num);
415 pmsg.packet_id = htonl(entry->id);
416 pmsg.hw_protocol = htons(entry->skb->protocol);
417 pmsg.hook = entry->info->hook;
419 NFA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
421 if (entry->info->indev) {
422 tmp_uint = htonl(entry->info->indev->ifindex);
423 #ifndef CONFIG_BRIDGE_NETFILTER
424 NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
426 if (entry->info->pf == PF_BRIDGE) {
427 /* Case 1: indev is physical input device, we need to
428 * look for bridge group (when called from
429 * netfilter_bridge) */
430 NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint),
432 /* this is the bridge group "brX" */
433 tmp_uint = htonl(entry->info->indev->br_port->br->dev->ifindex);
434 NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
437 /* Case 2: indev is bridge group, we need to look for
438 * physical device (when called from ipv4) */
439 NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
441 if (entry->skb->nf_bridge
442 && entry->skb->nf_bridge->physindev) {
443 tmp_uint = htonl(entry->skb->nf_bridge->physindev->ifindex);
444 NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
445 sizeof(tmp_uint), &tmp_uint);
451 if (entry->info->outdev) {
452 tmp_uint = htonl(entry->info->outdev->ifindex);
453 #ifndef CONFIG_BRIDGE_NETFILTER
454 NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
456 if (entry->info->pf == PF_BRIDGE) {
457 /* Case 1: outdev is physical output device, we need to
458 * look for bridge group (when called from
459 * netfilter_bridge) */
460 NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
462 /* this is the bridge group "brX" */
463 tmp_uint = htonl(entry->info->outdev->br_port->br->dev->ifindex);
464 NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
467 /* Case 2: outdev is bridge group, we need to look for
468 * physical output device (when called from ipv4) */
469 NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
471 if (entry->skb->nf_bridge
472 && entry->skb->nf_bridge->physoutdev) {
473 tmp_uint = htonl(entry->skb->nf_bridge->physoutdev->ifindex);
474 NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
475 sizeof(tmp_uint), &tmp_uint);
481 if (entry->skb->nfmark) {
482 tmp_uint = htonl(entry->skb->nfmark);
483 NFA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
486 if (entry->info->indev && entry->skb->dev
487 && entry->skb->dev->hard_header_parse) {
488 struct nfqnl_msg_packet_hw phw;
491 entry->skb->dev->hard_header_parse(entry->skb,
493 phw.hw_addrlen = htons(phw.hw_addrlen);
494 NFA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
497 if (entry->skb->stamp.tv_sec) {
498 struct nfqnl_msg_packet_timestamp ts;
500 ts.sec = htonll(entry->skb->stamp.tv_sec);
501 ts.usec = htonll(entry->skb->stamp.tv_usec);
503 NFA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
508 int size = NFA_LENGTH(data_len);
510 if (skb_tailroom(skb) < (int)NFA_SPACE(data_len)) {
511 printk(KERN_WARNING "nf_queue: no tailroom!\n");
515 nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
516 nfa->nfa_type = NFQA_PAYLOAD;
519 if (skb_copy_bits(entry->skb, 0, NFA_DATA(nfa), data_len))
523 nlh->nlmsg_len = skb->tail - old_tail;
532 printk(KERN_ERR "nf_queue: error creating packet message\n");
537 nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
538 unsigned int queuenum, void *data)
540 int status = -EINVAL;
541 struct sk_buff *nskb;
542 struct nfqnl_instance *queue;
543 struct nfqnl_queue_entry *entry;
547 queue = instance_lookup_get(queuenum);
549 QDEBUG("no queue instance matching\n");
553 if (queue->copy_mode == NFQNL_COPY_NONE) {
554 QDEBUG("mode COPY_NONE, aborting\n");
559 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
563 "nf_queue: OOM in nfqnl_enqueue_packet()\n");
570 entry->id = atomic_inc_return(&queue->id_sequence);
572 nskb = nfqnl_build_packet_message(queue, entry, &status);
576 spin_lock_bh(&queue->lock);
578 if (!queue->peer_pid)
579 goto err_out_free_nskb;
581 if (queue->queue_total >= queue->queue_maxlen) {
582 queue->queue_dropped++;
585 printk(KERN_WARNING "ip_queue: full at %d entries, "
586 "dropping packets(s). Dropped: %d\n",
587 queue->queue_total, queue->queue_dropped);
588 goto err_out_free_nskb;
591 /* nfnetlink_unicast will either free the nskb or add it to a socket */
592 status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
594 queue->queue_user_dropped++;
598 __enqueue_entry(queue, entry);
600 spin_unlock_bh(&queue->lock);
608 spin_unlock_bh(&queue->lock);
618 nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
622 diff = data_len - e->skb->len;
624 skb_trim(e->skb, data_len);
626 if (data_len > 0xFFFF)
628 if (diff > skb_tailroom(e->skb)) {
629 struct sk_buff *newskb;
631 newskb = skb_copy_expand(e->skb,
632 skb_headroom(e->skb),
635 if (newskb == NULL) {
636 printk(KERN_WARNING "ip_queue: OOM "
637 "in mangle, dropping packet\n");
641 skb_set_owner_w(newskb, e->skb->sk);
645 skb_put(e->skb, diff);
647 if (!skb_make_writable(&e->skb, data_len))
649 memcpy(e->skb->data, data, data_len);
655 id_cmp(struct nfqnl_queue_entry *e, unsigned long id)
657 return (id == e->id);
661 nfqnl_set_mode(struct nfqnl_instance *queue,
662 unsigned char mode, unsigned int range)
666 spin_lock_bh(&queue->lock);
667 status = __nfqnl_set_mode(queue, mode, range);
668 spin_unlock_bh(&queue->lock);
674 dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex)
676 if (entry->info->indev)
677 if (entry->info->indev->ifindex == ifindex)
680 if (entry->info->outdev)
681 if (entry->info->outdev->ifindex == ifindex)
687 /* drop all packets with either indev or outdev == ifindex from all queue
690 nfqnl_dev_drop(int ifindex)
694 QDEBUG("entering for ifindex %u\n", ifindex);
696 /* this only looks like we have to hold the readlock for a way too long
697 * time, issue_verdict(), nf_reinject(), ... - but we always only
698 * issue NF_DROP, which is processed directly in nf_reinject() */
699 read_lock_bh(&instances_lock);
701 for (i = 0; i < INSTANCE_BUCKETS; i++) {
702 struct hlist_node *tmp;
703 struct nfqnl_instance *inst;
704 struct hlist_head *head = &instance_table[i];
706 hlist_for_each_entry(inst, tmp, head, hlist) {
707 struct nfqnl_queue_entry *entry;
708 while ((entry = find_dequeue_entry(inst, dev_cmp,
710 issue_verdict(entry, NF_DROP);
714 read_unlock_bh(&instances_lock);
717 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
720 nfqnl_rcv_dev_event(struct notifier_block *this,
721 unsigned long event, void *ptr)
723 struct net_device *dev = ptr;
725 /* Drop any packets associated with the downed device */
726 if (event == NETDEV_DOWN)
727 nfqnl_dev_drop(dev->ifindex);
731 static struct notifier_block nfqnl_dev_notifier = {
732 .notifier_call = nfqnl_rcv_dev_event,
736 nfqnl_rcv_nl_event(struct notifier_block *this,
737 unsigned long event, void *ptr)
739 struct netlink_notify *n = ptr;
741 if (event == NETLINK_URELEASE &&
742 n->protocol == NETLINK_NETFILTER && n->pid) {
745 /* destroy all instances for this pid */
746 write_lock_bh(&instances_lock);
747 for (i = 0; i < INSTANCE_BUCKETS; i++) {
748 struct hlist_node *tmp, *t2;
749 struct nfqnl_instance *inst;
750 struct hlist_head *head = &instance_table[i];
752 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
753 if (n->pid == inst->peer_pid)
754 __instance_destroy(inst);
757 write_unlock_bh(&instances_lock);
762 static struct notifier_block nfqnl_rtnl_notifier = {
763 .notifier_call = nfqnl_rcv_nl_event,
766 static const int nfqa_verdict_min[NFQA_MAX] = {
767 [NFQA_VERDICT_HDR-1] = sizeof(struct nfqnl_msg_verdict_hdr),
768 [NFQA_MARK-1] = sizeof(u_int32_t),
769 [NFQA_PAYLOAD-1] = 0,
773 nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
774 struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
776 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
777 u_int16_t queue_num = ntohs(nfmsg->res_id);
779 struct nfqnl_msg_verdict_hdr *vhdr;
780 struct nfqnl_instance *queue;
781 unsigned int verdict;
782 struct nfqnl_queue_entry *entry;
785 if (nfattr_bad_size(nfqa, NFQA_MAX, nfqa_verdict_min)) {
786 QDEBUG("bad attribute size\n");
790 queue = instance_lookup_get(queue_num);
794 if (queue->peer_pid != NETLINK_CB(skb).pid) {
799 if (!nfqa[NFQA_VERDICT_HDR-1]) {
804 vhdr = NFA_DATA(nfqa[NFQA_VERDICT_HDR-1]);
805 verdict = ntohl(vhdr->verdict);
807 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
812 entry = find_dequeue_entry(queue, id_cmp, ntohl(vhdr->id));
818 if (nfqa[NFQA_PAYLOAD-1]) {
819 if (nfqnl_mangle(NFA_DATA(nfqa[NFQA_PAYLOAD-1]),
820 NFA_PAYLOAD(nfqa[NFQA_PAYLOAD-1]), entry) < 0)
824 if (nfqa[NFQA_MARK-1])
825 skb->nfmark = ntohl(*(u_int32_t *)NFA_DATA(nfqa[NFQA_MARK-1]));
827 issue_verdict(entry, verdict);
837 nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
838 struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
843 static const int nfqa_cfg_min[NFQA_CFG_MAX] = {
844 [NFQA_CFG_CMD-1] = sizeof(struct nfqnl_msg_config_cmd),
845 [NFQA_CFG_PARAMS-1] = sizeof(struct nfqnl_msg_config_params),
848 static struct nf_queue_handler nfqh = {
850 .outfn = &nfqnl_enqueue_packet,
854 nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
855 struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
857 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
858 u_int16_t queue_num = ntohs(nfmsg->res_id);
859 struct nfqnl_instance *queue;
862 QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
864 if (nfattr_bad_size(nfqa, NFQA_CFG_MAX, nfqa_cfg_min)) {
865 QDEBUG("bad attribute size\n");
869 queue = instance_lookup_get(queue_num);
870 if (nfqa[NFQA_CFG_CMD-1]) {
871 struct nfqnl_msg_config_cmd *cmd;
872 cmd = NFA_DATA(nfqa[NFQA_CFG_CMD-1]);
873 QDEBUG("found CFG_CMD\n");
875 switch (cmd->command) {
876 case NFQNL_CFG_CMD_BIND:
880 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
884 case NFQNL_CFG_CMD_UNBIND:
888 if (queue->peer_pid != NETLINK_CB(skb).pid) {
893 instance_destroy(queue);
895 case NFQNL_CFG_CMD_PF_BIND:
896 QDEBUG("registering queue handler for pf=%u\n",
898 ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh);
900 case NFQNL_CFG_CMD_PF_UNBIND:
901 QDEBUG("unregistering queue handler for pf=%u\n",
903 /* This is a bug and a feature. We can unregister
904 * other handlers(!) */
905 ret = nf_unregister_queue_handler(ntohs(cmd->pf));
913 QDEBUG("no config command, and no instance ENOENT\n");
918 if (queue->peer_pid != NETLINK_CB(skb).pid) {
919 QDEBUG("no config command, and wrong pid\n");
925 if (nfqa[NFQA_CFG_PARAMS-1]) {
926 struct nfqnl_msg_config_params *params;
927 params = NFA_DATA(nfqa[NFQA_CFG_PARAMS-1]);
929 nfqnl_set_mode(queue, params->copy_mode,
930 ntohl(params->copy_range));
938 static struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
939 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
940 .attr_count = NFQA_MAX,
941 .cap_required = CAP_NET_ADMIN },
942 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
943 .attr_count = NFQA_MAX,
944 .cap_required = CAP_NET_ADMIN },
945 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
946 .attr_count = NFQA_CFG_MAX,
947 .cap_required = CAP_NET_ADMIN },
950 static struct nfnetlink_subsystem nfqnl_subsys = {
952 .subsys_id = NFNL_SUBSYS_QUEUE,
953 .cb_count = NFQNL_MSG_MAX,
957 #ifdef CONFIG_PROC_FS
962 static struct hlist_node *get_first(struct seq_file *seq)
964 struct iter_state *st = seq->private;
969 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
970 if (!hlist_empty(&instance_table[st->bucket]))
971 return instance_table[st->bucket].first;
976 static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
978 struct iter_state *st = seq->private;
982 if (++st->bucket >= INSTANCE_BUCKETS)
985 h = instance_table[st->bucket].first;
990 static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
992 struct hlist_node *head;
993 head = get_first(seq);
996 while (pos && (head = get_next(seq, head)))
998 return pos ? NULL : head;
1001 static void *seq_start(struct seq_file *seq, loff_t *pos)
1003 read_lock_bh(&instances_lock);
1004 return get_idx(seq, *pos);
1007 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1010 return get_next(s, v);
1013 static void seq_stop(struct seq_file *s, void *v)
1015 read_unlock_bh(&instances_lock);
1018 static int seq_show(struct seq_file *s, void *v)
1020 const struct nfqnl_instance *inst = v;
1022 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1024 inst->peer_pid, inst->queue_total,
1025 inst->copy_mode, inst->copy_range,
1026 inst->queue_dropped, inst->queue_user_dropped,
1027 atomic_read(&inst->id_sequence),
1028 atomic_read(&inst->use));
1031 static struct seq_operations nfqnl_seq_ops = {
1038 static int nfqnl_open(struct inode *inode, struct file *file)
1040 struct seq_file *seq;
1041 struct iter_state *is;
1044 is = kmalloc(sizeof(*is), GFP_KERNEL);
1047 memset(is, 0, sizeof(*is));
1048 ret = seq_open(file, &nfqnl_seq_ops);
1051 seq = file->private_data;
1059 static struct file_operations nfqnl_file_ops = {
1060 .owner = THIS_MODULE,
1063 .llseek = seq_lseek,
1064 .release = seq_release_private,
1067 #endif /* PROC_FS */
1070 init_or_cleanup(int init)
1072 int i, status = -ENOMEM;
1073 #ifdef CONFIG_PROC_FS
1074 struct proc_dir_entry *proc_nfqueue;
1080 for (i = 0; i < INSTANCE_BUCKETS; i++)
1081 INIT_HLIST_HEAD(&instance_table[i]);
1083 netlink_register_notifier(&nfqnl_rtnl_notifier);
1084 status = nfnetlink_subsys_register(&nfqnl_subsys);
1086 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1087 goto cleanup_netlink_notifier;
1090 #ifdef CONFIG_PROC_FS
1091 proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
1092 proc_net_netfilter);
1094 goto cleanup_subsys;
1095 proc_nfqueue->proc_fops = &nfqnl_file_ops;
1098 register_netdevice_notifier(&nfqnl_dev_notifier);
1103 nf_unregister_queue_handlers(&nfqh);
1104 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1105 #ifdef CONFIG_PROC_FS
1106 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1109 nfnetlink_subsys_unregister(&nfqnl_subsys);
1110 cleanup_netlink_notifier:
1111 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1115 static int __init init(void)
1118 return init_or_cleanup(1);
1121 static void __exit fini(void)
1126 MODULE_DESCRIPTION("netfilter packet queue handler");
1127 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1128 MODULE_LICENSE("GPL");
1129 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);