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 #define INSTANCE_BUCKETS 16
80 static struct hlist_head instance_table[INSTANCE_BUCKETS];
82 static inline u_int8_t instance_hashfn(u_int16_t queue_num)
84 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
87 static struct nfqnl_instance *
88 __instance_lookup(u_int16_t queue_num)
90 struct hlist_head *head;
91 struct hlist_node *pos;
92 struct nfqnl_instance *inst;
94 head = &instance_table[instance_hashfn(queue_num)];
95 hlist_for_each_entry(inst, pos, head, hlist) {
96 if (inst->queue_num == queue_num)
102 static struct nfqnl_instance *
103 instance_lookup_get(u_int16_t queue_num)
105 struct nfqnl_instance *inst;
107 read_lock_bh(&instances_lock);
108 inst = __instance_lookup(queue_num);
110 atomic_inc(&inst->use);
111 read_unlock_bh(&instances_lock);
117 instance_put(struct nfqnl_instance *inst)
119 if (inst && atomic_dec_and_test(&inst->use)) {
120 QDEBUG("kfree(inst=%p)\n", inst);
125 static struct nfqnl_instance *
126 instance_create(u_int16_t queue_num, int pid)
128 struct nfqnl_instance *inst;
130 QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid);
132 write_lock_bh(&instances_lock);
133 if (__instance_lookup(queue_num)) {
135 QDEBUG("aborting, instance already exists\n");
139 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
143 inst->queue_num = queue_num;
144 inst->peer_pid = pid;
145 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
146 inst->copy_range = 0xfffff;
147 inst->copy_mode = NFQNL_COPY_NONE;
148 atomic_set(&inst->id_sequence, 0);
149 /* needs to be two, since we _put() after creation */
150 atomic_set(&inst->use, 2);
151 spin_lock_init(&inst->lock);
152 INIT_LIST_HEAD(&inst->queue_list);
154 if (!try_module_get(THIS_MODULE))
157 hlist_add_head(&inst->hlist,
158 &instance_table[instance_hashfn(queue_num)]);
160 write_unlock_bh(&instances_lock);
162 QDEBUG("successfully created new instance\n");
169 write_unlock_bh(&instances_lock);
173 static void nfqnl_flush(struct nfqnl_instance *queue, int verdict);
176 _instance_destroy2(struct nfqnl_instance *inst, int lock)
178 /* first pull it out of the global list */
180 write_lock_bh(&instances_lock);
182 QDEBUG("removing instance %p (queuenum=%u) from hash\n",
183 inst, inst->queue_num);
184 hlist_del(&inst->hlist);
187 write_unlock_bh(&instances_lock);
189 /* then flush all pending skbs from the queue */
190 nfqnl_flush(inst, NF_DROP);
192 /* and finally put the refcount */
195 module_put(THIS_MODULE);
199 __instance_destroy(struct nfqnl_instance *inst)
201 _instance_destroy2(inst, 0);
205 instance_destroy(struct nfqnl_instance *inst)
207 _instance_destroy2(inst, 1);
213 issue_verdict(struct nfqnl_queue_entry *entry, int verdict)
215 QDEBUG("entering for entry %p, verdict %u\n", entry, verdict);
217 /* TCP input path (and probably other bits) assume to be called
218 * from softirq context, not from syscall, like issue_verdict is
219 * called. TCP input path deadlocks with locks taken from timer
220 * softirq, e.g. We therefore emulate this by local_bh_disable() */
223 nf_reinject(entry->skb, entry->info, verdict);
230 __enqueue_entry(struct nfqnl_instance *queue,
231 struct nfqnl_queue_entry *entry)
233 list_add(&entry->list, &queue->queue_list);
234 queue->queue_total++;
238 * Find and return a queued entry matched by cmpfn, or return the last
239 * entry if cmpfn is NULL.
241 static inline struct nfqnl_queue_entry *
242 __find_entry(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
247 list_for_each_prev(p, &queue->queue_list) {
248 struct nfqnl_queue_entry *entry = (struct nfqnl_queue_entry *)p;
250 if (!cmpfn || cmpfn(entry, data))
257 __dequeue_entry(struct nfqnl_instance *q, struct nfqnl_queue_entry *entry)
259 list_del(&entry->list);
263 static inline struct nfqnl_queue_entry *
264 __find_dequeue_entry(struct nfqnl_instance *queue,
265 nfqnl_cmpfn cmpfn, unsigned long data)
267 struct nfqnl_queue_entry *entry;
269 entry = __find_entry(queue, cmpfn, data);
273 __dequeue_entry(queue, entry);
279 __nfqnl_flush(struct nfqnl_instance *queue, int verdict)
281 struct nfqnl_queue_entry *entry;
283 while ((entry = __find_dequeue_entry(queue, NULL, 0)))
284 issue_verdict(entry, verdict);
288 __nfqnl_set_mode(struct nfqnl_instance *queue,
289 unsigned char mode, unsigned int range)
294 case NFQNL_COPY_NONE:
295 case NFQNL_COPY_META:
296 queue->copy_mode = mode;
297 queue->copy_range = 0;
300 case NFQNL_COPY_PACKET:
301 queue->copy_mode = mode;
302 /* we're using struct nfattr which has 16bit nfa_len */
304 queue->copy_range = 0xffff;
306 queue->copy_range = range;
316 static struct nfqnl_queue_entry *
317 find_dequeue_entry(struct nfqnl_instance *queue,
318 nfqnl_cmpfn cmpfn, unsigned long data)
320 struct nfqnl_queue_entry *entry;
322 spin_lock_bh(&queue->lock);
323 entry = __find_dequeue_entry(queue, cmpfn, data);
324 spin_unlock_bh(&queue->lock);
330 nfqnl_flush(struct nfqnl_instance *queue, int verdict)
332 spin_lock_bh(&queue->lock);
333 __nfqnl_flush(queue, verdict);
334 spin_unlock_bh(&queue->lock);
337 static struct sk_buff *
338 nfqnl_build_packet_message(struct nfqnl_instance *queue,
339 struct nfqnl_queue_entry *entry, int *errp)
341 unsigned char *old_tail;
345 struct nfqnl_msg_packet_hdr pmsg;
346 struct nlmsghdr *nlh;
347 struct nfgenmsg *nfmsg;
348 struct nf_info *entinf = entry->info;
349 struct sk_buff *entskb = entry->skb;
350 struct net_device *indev;
351 struct net_device *outdev;
352 unsigned int tmp_uint;
356 /* all macros expand to constant values at compile time */
357 size = NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hdr))
358 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
359 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
360 #ifdef CONFIG_BRIDGE_NETFILTER
361 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
362 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
364 + NLMSG_SPACE(sizeof(u_int32_t)) /* mark */
365 + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hw))
366 + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_timestamp));
368 outdev = entinf->outdev;
370 spin_lock_bh(&queue->lock);
372 switch (queue->copy_mode) {
373 case NFQNL_COPY_META:
374 case NFQNL_COPY_NONE:
378 case NFQNL_COPY_PACKET:
379 if (entskb->ip_summed == CHECKSUM_HW &&
380 (*errp = skb_checksum_help(entskb,
382 spin_unlock_bh(&queue->lock);
385 if (queue->copy_range == 0
386 || queue->copy_range > entskb->len)
387 data_len = entskb->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 = entinf->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(entskb->protocol);
417 pmsg.hook = entinf->hook;
419 NFA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
421 indev = entinf->indev;
423 tmp_uint = htonl(indev->ifindex);
424 #ifndef CONFIG_BRIDGE_NETFILTER
425 NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
427 if (entinf->pf == PF_BRIDGE) {
428 /* Case 1: indev is physical input device, we need to
429 * look for bridge group (when called from
430 * netfilter_bridge) */
431 NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint),
433 /* this is the bridge group "brX" */
434 tmp_uint = htonl(indev->br_port->br->dev->ifindex);
435 NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
438 /* Case 2: indev is bridge group, we need to look for
439 * physical device (when called from ipv4) */
440 NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
442 if (entskb->nf_bridge
443 && entskb->nf_bridge->physindev) {
444 tmp_uint = htonl(entskb->nf_bridge->physindev->ifindex);
445 NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
446 sizeof(tmp_uint), &tmp_uint);
453 tmp_uint = htonl(outdev->ifindex);
454 #ifndef CONFIG_BRIDGE_NETFILTER
455 NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
457 if (entinf->pf == PF_BRIDGE) {
458 /* Case 1: outdev is physical output device, we need to
459 * look for bridge group (when called from
460 * netfilter_bridge) */
461 NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
463 /* this is the bridge group "brX" */
464 tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
465 NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
468 /* Case 2: outdev is bridge group, we need to look for
469 * physical output device (when called from ipv4) */
470 NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
472 if (entskb->nf_bridge
473 && entskb->nf_bridge->physoutdev) {
474 tmp_uint = htonl(entskb->nf_bridge->physoutdev->ifindex);
475 NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
476 sizeof(tmp_uint), &tmp_uint);
482 if (entskb->nfmark) {
483 tmp_uint = htonl(entskb->nfmark);
484 NFA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
487 if (indev && entskb->dev
488 && entskb->dev->hard_header_parse) {
489 struct nfqnl_msg_packet_hw phw;
492 entskb->dev->hard_header_parse(entskb,
494 phw.hw_addrlen = htons(phw.hw_addrlen);
495 NFA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
498 if (entskb->tstamp.off_sec) {
499 struct nfqnl_msg_packet_timestamp ts;
501 ts.sec = cpu_to_be64(entskb->tstamp.off_sec);
502 ts.usec = cpu_to_be64(entskb->tstamp.off_usec);
504 NFA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
509 int size = NFA_LENGTH(data_len);
511 if (skb_tailroom(skb) < (int)NFA_SPACE(data_len)) {
512 printk(KERN_WARNING "nf_queue: no tailroom!\n");
516 nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
517 nfa->nfa_type = NFQA_PAYLOAD;
520 if (skb_copy_bits(entskb, 0, NFA_DATA(nfa), data_len))
524 nlh->nlmsg_len = skb->tail - old_tail;
533 printk(KERN_ERR "nf_queue: error creating packet message\n");
538 nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
539 unsigned int queuenum, void *data)
541 int status = -EINVAL;
542 struct sk_buff *nskb;
543 struct nfqnl_instance *queue;
544 struct nfqnl_queue_entry *entry;
548 queue = instance_lookup_get(queuenum);
550 QDEBUG("no queue instance matching\n");
554 if (queue->copy_mode == NFQNL_COPY_NONE) {
555 QDEBUG("mode COPY_NONE, aborting\n");
560 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
564 "nf_queue: OOM in nfqnl_enqueue_packet()\n");
571 entry->id = atomic_inc_return(&queue->id_sequence);
573 nskb = nfqnl_build_packet_message(queue, entry, &status);
577 spin_lock_bh(&queue->lock);
579 if (!queue->peer_pid)
580 goto err_out_free_nskb;
582 if (queue->queue_total >= queue->queue_maxlen) {
583 queue->queue_dropped++;
586 printk(KERN_WARNING "ip_queue: full at %d entries, "
587 "dropping packets(s). Dropped: %d\n",
588 queue->queue_total, queue->queue_dropped);
589 goto err_out_free_nskb;
592 /* nfnetlink_unicast will either free the nskb or add it to a socket */
593 status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
595 queue->queue_user_dropped++;
599 __enqueue_entry(queue, entry);
601 spin_unlock_bh(&queue->lock);
609 spin_unlock_bh(&queue->lock);
619 nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
623 diff = data_len - e->skb->len;
625 skb_trim(e->skb, data_len);
627 if (data_len > 0xFFFF)
629 if (diff > skb_tailroom(e->skb)) {
630 struct sk_buff *newskb;
632 newskb = skb_copy_expand(e->skb,
633 skb_headroom(e->skb),
636 if (newskb == NULL) {
637 printk(KERN_WARNING "ip_queue: OOM "
638 "in mangle, dropping packet\n");
642 skb_set_owner_w(newskb, e->skb->sk);
646 skb_put(e->skb, diff);
648 if (!skb_make_writable(&e->skb, data_len))
650 memcpy(e->skb->data, data, data_len);
651 e->skb->ip_summed = CHECKSUM_NONE;
656 id_cmp(struct nfqnl_queue_entry *e, unsigned long id)
658 return (id == e->id);
662 nfqnl_set_mode(struct nfqnl_instance *queue,
663 unsigned char mode, unsigned int range)
667 spin_lock_bh(&queue->lock);
668 status = __nfqnl_set_mode(queue, mode, range);
669 spin_unlock_bh(&queue->lock);
675 dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex)
677 struct nf_info *entinf = entry->info;
680 if (entinf->indev->ifindex == ifindex)
684 if (entinf->outdev->ifindex == ifindex)
690 /* drop all packets with either indev or outdev == ifindex from all queue
693 nfqnl_dev_drop(int ifindex)
697 QDEBUG("entering for ifindex %u\n", ifindex);
699 /* this only looks like we have to hold the readlock for a way too long
700 * time, issue_verdict(), nf_reinject(), ... - but we always only
701 * issue NF_DROP, which is processed directly in nf_reinject() */
702 read_lock_bh(&instances_lock);
704 for (i = 0; i < INSTANCE_BUCKETS; i++) {
705 struct hlist_node *tmp;
706 struct nfqnl_instance *inst;
707 struct hlist_head *head = &instance_table[i];
709 hlist_for_each_entry(inst, tmp, head, hlist) {
710 struct nfqnl_queue_entry *entry;
711 while ((entry = find_dequeue_entry(inst, dev_cmp,
713 issue_verdict(entry, NF_DROP);
717 read_unlock_bh(&instances_lock);
720 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
723 nfqnl_rcv_dev_event(struct notifier_block *this,
724 unsigned long event, void *ptr)
726 struct net_device *dev = ptr;
728 /* Drop any packets associated with the downed device */
729 if (event == NETDEV_DOWN)
730 nfqnl_dev_drop(dev->ifindex);
734 static struct notifier_block nfqnl_dev_notifier = {
735 .notifier_call = nfqnl_rcv_dev_event,
739 nfqnl_rcv_nl_event(struct notifier_block *this,
740 unsigned long event, void *ptr)
742 struct netlink_notify *n = ptr;
744 if (event == NETLINK_URELEASE &&
745 n->protocol == NETLINK_NETFILTER && n->pid) {
748 /* destroy all instances for this pid */
749 write_lock_bh(&instances_lock);
750 for (i = 0; i < INSTANCE_BUCKETS; i++) {
751 struct hlist_node *tmp, *t2;
752 struct nfqnl_instance *inst;
753 struct hlist_head *head = &instance_table[i];
755 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
756 if (n->pid == inst->peer_pid)
757 __instance_destroy(inst);
760 write_unlock_bh(&instances_lock);
765 static struct notifier_block nfqnl_rtnl_notifier = {
766 .notifier_call = nfqnl_rcv_nl_event,
769 static const int nfqa_verdict_min[NFQA_MAX] = {
770 [NFQA_VERDICT_HDR-1] = sizeof(struct nfqnl_msg_verdict_hdr),
771 [NFQA_MARK-1] = sizeof(u_int32_t),
772 [NFQA_PAYLOAD-1] = 0,
776 nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
777 struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
779 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
780 u_int16_t queue_num = ntohs(nfmsg->res_id);
782 struct nfqnl_msg_verdict_hdr *vhdr;
783 struct nfqnl_instance *queue;
784 unsigned int verdict;
785 struct nfqnl_queue_entry *entry;
788 if (nfattr_bad_size(nfqa, NFQA_MAX, nfqa_verdict_min)) {
789 QDEBUG("bad attribute size\n");
793 queue = instance_lookup_get(queue_num);
797 if (queue->peer_pid != NETLINK_CB(skb).pid) {
802 if (!nfqa[NFQA_VERDICT_HDR-1]) {
807 vhdr = NFA_DATA(nfqa[NFQA_VERDICT_HDR-1]);
808 verdict = ntohl(vhdr->verdict);
810 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
815 entry = find_dequeue_entry(queue, id_cmp, ntohl(vhdr->id));
821 if (nfqa[NFQA_PAYLOAD-1]) {
822 if (nfqnl_mangle(NFA_DATA(nfqa[NFQA_PAYLOAD-1]),
823 NFA_PAYLOAD(nfqa[NFQA_PAYLOAD-1]), entry) < 0)
827 if (nfqa[NFQA_MARK-1])
828 entry->skb->nfmark = ntohl(*(u_int32_t *)
829 NFA_DATA(nfqa[NFQA_MARK-1]));
831 issue_verdict(entry, verdict);
841 nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
842 struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
847 static const int nfqa_cfg_min[NFQA_CFG_MAX] = {
848 [NFQA_CFG_CMD-1] = sizeof(struct nfqnl_msg_config_cmd),
849 [NFQA_CFG_PARAMS-1] = sizeof(struct nfqnl_msg_config_params),
852 static struct nf_queue_handler nfqh = {
854 .outfn = &nfqnl_enqueue_packet,
858 nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
859 struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
861 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
862 u_int16_t queue_num = ntohs(nfmsg->res_id);
863 struct nfqnl_instance *queue;
866 QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
868 if (nfattr_bad_size(nfqa, NFQA_CFG_MAX, nfqa_cfg_min)) {
869 QDEBUG("bad attribute size\n");
873 queue = instance_lookup_get(queue_num);
874 if (nfqa[NFQA_CFG_CMD-1]) {
875 struct nfqnl_msg_config_cmd *cmd;
876 cmd = NFA_DATA(nfqa[NFQA_CFG_CMD-1]);
877 QDEBUG("found CFG_CMD\n");
879 switch (cmd->command) {
880 case NFQNL_CFG_CMD_BIND:
884 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
888 case NFQNL_CFG_CMD_UNBIND:
892 if (queue->peer_pid != NETLINK_CB(skb).pid) {
897 instance_destroy(queue);
899 case NFQNL_CFG_CMD_PF_BIND:
900 QDEBUG("registering queue handler for pf=%u\n",
902 ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh);
904 case NFQNL_CFG_CMD_PF_UNBIND:
905 QDEBUG("unregistering queue handler for pf=%u\n",
907 /* This is a bug and a feature. We can unregister
908 * other handlers(!) */
909 ret = nf_unregister_queue_handler(ntohs(cmd->pf));
917 QDEBUG("no config command, and no instance ENOENT\n");
922 if (queue->peer_pid != NETLINK_CB(skb).pid) {
923 QDEBUG("no config command, and wrong pid\n");
929 if (nfqa[NFQA_CFG_PARAMS-1]) {
930 struct nfqnl_msg_config_params *params;
936 params = NFA_DATA(nfqa[NFQA_CFG_PARAMS-1]);
937 nfqnl_set_mode(queue, params->copy_mode,
938 ntohl(params->copy_range));
946 static struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
947 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
948 .attr_count = NFQA_MAX, },
949 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
950 .attr_count = NFQA_MAX, },
951 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
952 .attr_count = NFQA_CFG_MAX, },
955 static struct nfnetlink_subsystem nfqnl_subsys = {
957 .subsys_id = NFNL_SUBSYS_QUEUE,
958 .cb_count = NFQNL_MSG_MAX,
962 #ifdef CONFIG_PROC_FS
967 static struct hlist_node *get_first(struct seq_file *seq)
969 struct iter_state *st = seq->private;
974 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
975 if (!hlist_empty(&instance_table[st->bucket]))
976 return instance_table[st->bucket].first;
981 static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
983 struct iter_state *st = seq->private;
987 if (++st->bucket >= INSTANCE_BUCKETS)
990 h = instance_table[st->bucket].first;
995 static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
997 struct hlist_node *head;
998 head = get_first(seq);
1001 while (pos && (head = get_next(seq, head)))
1003 return pos ? NULL : head;
1006 static void *seq_start(struct seq_file *seq, loff_t *pos)
1008 read_lock_bh(&instances_lock);
1009 return get_idx(seq, *pos);
1012 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1015 return get_next(s, v);
1018 static void seq_stop(struct seq_file *s, void *v)
1020 read_unlock_bh(&instances_lock);
1023 static int seq_show(struct seq_file *s, void *v)
1025 const struct nfqnl_instance *inst = v;
1027 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1029 inst->peer_pid, inst->queue_total,
1030 inst->copy_mode, inst->copy_range,
1031 inst->queue_dropped, inst->queue_user_dropped,
1032 atomic_read(&inst->id_sequence),
1033 atomic_read(&inst->use));
1036 static struct seq_operations nfqnl_seq_ops = {
1043 static int nfqnl_open(struct inode *inode, struct file *file)
1045 struct seq_file *seq;
1046 struct iter_state *is;
1049 is = kzalloc(sizeof(*is), GFP_KERNEL);
1052 ret = seq_open(file, &nfqnl_seq_ops);
1055 seq = file->private_data;
1063 static struct file_operations nfqnl_file_ops = {
1064 .owner = THIS_MODULE,
1067 .llseek = seq_lseek,
1068 .release = seq_release_private,
1071 #endif /* PROC_FS */
1074 init_or_cleanup(int init)
1076 int i, status = -ENOMEM;
1077 #ifdef CONFIG_PROC_FS
1078 struct proc_dir_entry *proc_nfqueue;
1084 for (i = 0; i < INSTANCE_BUCKETS; i++)
1085 INIT_HLIST_HEAD(&instance_table[i]);
1087 netlink_register_notifier(&nfqnl_rtnl_notifier);
1088 status = nfnetlink_subsys_register(&nfqnl_subsys);
1090 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1091 goto cleanup_netlink_notifier;
1094 #ifdef CONFIG_PROC_FS
1095 proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
1096 proc_net_netfilter);
1098 goto cleanup_subsys;
1099 proc_nfqueue->proc_fops = &nfqnl_file_ops;
1102 register_netdevice_notifier(&nfqnl_dev_notifier);
1107 nf_unregister_queue_handlers(&nfqh);
1108 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1109 #ifdef CONFIG_PROC_FS
1110 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1113 nfnetlink_subsys_unregister(&nfqnl_subsys);
1114 cleanup_netlink_notifier:
1115 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1119 static int __init init(void)
1122 return init_or_cleanup(1);
1125 static void __exit fini(void)
1130 MODULE_DESCRIPTION("netfilter packet queue handler");
1131 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1132 MODULE_LICENSE("GPL");
1133 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);