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, int verdict);
177 _instance_destroy2(struct nfqnl_instance *inst, int lock)
179 /* first pull it out of the global list */
181 write_lock_bh(&instances_lock);
183 QDEBUG("removing instance %p (queuenum=%u) from hash\n",
184 inst, inst->queue_num);
185 hlist_del(&inst->hlist);
188 write_unlock_bh(&instances_lock);
190 /* then flush all pending skbs from the queue */
191 nfqnl_flush(inst, NF_DROP);
193 /* and finally put the refcount */
196 module_put(THIS_MODULE);
200 __instance_destroy(struct nfqnl_instance *inst)
202 _instance_destroy2(inst, 0);
206 instance_destroy(struct nfqnl_instance *inst)
208 _instance_destroy2(inst, 1);
214 issue_verdict(struct nfqnl_queue_entry *entry, int verdict)
216 QDEBUG("entering for entry %p, verdict %u\n", entry, verdict);
218 /* TCP input path (and probably other bits) assume to be called
219 * from softirq context, not from syscall, like issue_verdict is
220 * called. TCP input path deadlocks with locks taken from timer
221 * softirq, e.g. We therefore emulate this by local_bh_disable() */
224 nf_reinject(entry->skb, entry->info, verdict);
231 __enqueue_entry(struct nfqnl_instance *queue,
232 struct nfqnl_queue_entry *entry)
234 list_add_tail(&entry->list, &queue->queue_list);
235 queue->queue_total++;
239 * Find and return a queued entry matched by cmpfn, or return the last
240 * entry if cmpfn is NULL.
242 static inline struct nfqnl_queue_entry *
243 __find_entry(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
246 struct nfqnl_queue_entry *entry;
248 list_for_each_entry(entry, &queue->queue_list, list) {
249 if (!cmpfn || cmpfn(entry, data))
256 __dequeue_entry(struct nfqnl_instance *q, struct nfqnl_queue_entry *entry)
258 list_del(&entry->list);
262 static inline struct nfqnl_queue_entry *
263 __find_dequeue_entry(struct nfqnl_instance *queue,
264 nfqnl_cmpfn cmpfn, unsigned long data)
266 struct nfqnl_queue_entry *entry;
268 entry = __find_entry(queue, cmpfn, data);
272 __dequeue_entry(queue, entry);
278 __nfqnl_flush(struct nfqnl_instance *queue, int verdict)
280 struct nfqnl_queue_entry *entry;
282 while ((entry = __find_dequeue_entry(queue, NULL, 0)))
283 issue_verdict(entry, verdict);
287 __nfqnl_set_mode(struct nfqnl_instance *queue,
288 unsigned char mode, unsigned int range)
293 case NFQNL_COPY_NONE:
294 case NFQNL_COPY_META:
295 queue->copy_mode = mode;
296 queue->copy_range = 0;
299 case NFQNL_COPY_PACKET:
300 queue->copy_mode = mode;
301 /* we're using struct nlattr which has 16bit nla_len */
303 queue->copy_range = 0xffff;
305 queue->copy_range = range;
315 static struct nfqnl_queue_entry *
316 find_dequeue_entry(struct nfqnl_instance *queue,
317 nfqnl_cmpfn cmpfn, unsigned long data)
319 struct nfqnl_queue_entry *entry;
321 spin_lock_bh(&queue->lock);
322 entry = __find_dequeue_entry(queue, cmpfn, data);
323 spin_unlock_bh(&queue->lock);
329 nfqnl_flush(struct nfqnl_instance *queue, int verdict)
331 spin_lock_bh(&queue->lock);
332 __nfqnl_flush(queue, verdict);
333 spin_unlock_bh(&queue->lock);
336 static struct sk_buff *
337 nfqnl_build_packet_message(struct nfqnl_instance *queue,
338 struct nfqnl_queue_entry *entry, int *errp)
340 sk_buff_data_t old_tail;
344 struct nfqnl_msg_packet_hdr pmsg;
345 struct nlmsghdr *nlh;
346 struct nfgenmsg *nfmsg;
347 struct nf_info *entinf = entry->info;
348 struct sk_buff *entskb = entry->skb;
349 struct net_device *indev;
350 struct net_device *outdev;
355 size = NLMSG_ALIGN(sizeof(struct nfgenmsg))
356 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
357 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
358 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
359 #ifdef CONFIG_BRIDGE_NETFILTER
360 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
361 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
363 + nla_total_size(sizeof(u_int32_t)) /* mark */
364 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
365 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
367 outdev = entinf->outdev;
369 spin_lock_bh(&queue->lock);
371 switch (queue->copy_mode) {
372 case NFQNL_COPY_META:
373 case NFQNL_COPY_NONE:
377 case NFQNL_COPY_PACKET:
378 if ((entskb->ip_summed == CHECKSUM_PARTIAL ||
379 entskb->ip_summed == CHECKSUM_COMPLETE) &&
380 (*errp = skb_checksum_help(entskb))) {
381 spin_unlock_bh(&queue->lock);
384 if (queue->copy_range == 0
385 || queue->copy_range > entskb->len)
386 data_len = entskb->len;
388 data_len = queue->copy_range;
390 size += nla_total_size(data_len);
395 spin_unlock_bh(&queue->lock);
399 spin_unlock_bh(&queue->lock);
401 skb = alloc_skb(size, GFP_ATOMIC);
405 old_tail = skb->tail;
406 nlh = NLMSG_PUT(skb, 0, 0,
407 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
408 sizeof(struct nfgenmsg));
409 nfmsg = NLMSG_DATA(nlh);
410 nfmsg->nfgen_family = entinf->pf;
411 nfmsg->version = NFNETLINK_V0;
412 nfmsg->res_id = htons(queue->queue_num);
414 pmsg.packet_id = htonl(entry->id);
415 pmsg.hw_protocol = entskb->protocol;
416 pmsg.hook = entinf->hook;
418 NLA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
420 indev = entinf->indev;
422 tmp_uint = htonl(indev->ifindex);
423 #ifndef CONFIG_BRIDGE_NETFILTER
424 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
426 if (entinf->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 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint),
432 /* this is the bridge group "brX" */
433 tmp_uint = htonl(indev->br_port->br->dev->ifindex);
434 NLA_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 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
441 if (entskb->nf_bridge
442 && entskb->nf_bridge->physindev) {
443 tmp_uint = htonl(entskb->nf_bridge->physindev->ifindex);
444 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
445 sizeof(tmp_uint), &tmp_uint);
452 tmp_uint = htonl(outdev->ifindex);
453 #ifndef CONFIG_BRIDGE_NETFILTER
454 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
456 if (entinf->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 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
462 /* this is the bridge group "brX" */
463 tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
464 NLA_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 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
471 if (entskb->nf_bridge
472 && entskb->nf_bridge->physoutdev) {
473 tmp_uint = htonl(entskb->nf_bridge->physoutdev->ifindex);
474 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
475 sizeof(tmp_uint), &tmp_uint);
482 tmp_uint = htonl(entskb->mark);
483 NLA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
486 if (indev && entskb->dev) {
487 struct nfqnl_msg_packet_hw phw;
488 int len = dev_parse_header(entskb, phw.hw_addr);
490 phw.hw_addrlen = htons(len);
491 NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
495 if (entskb->tstamp.tv64) {
496 struct nfqnl_msg_packet_timestamp ts;
497 struct timeval tv = ktime_to_timeval(entskb->tstamp);
498 ts.sec = cpu_to_be64(tv.tv_sec);
499 ts.usec = cpu_to_be64(tv.tv_usec);
501 NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
506 int size = nla_attr_size(data_len);
508 if (skb_tailroom(skb) < nla_total_size(data_len)) {
509 printk(KERN_WARNING "nf_queue: no tailroom!\n");
513 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
514 nla->nla_type = NFQA_PAYLOAD;
517 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
521 nlh->nlmsg_len = skb->tail - old_tail;
530 printk(KERN_ERR "nf_queue: error creating packet message\n");
535 nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
536 unsigned int queuenum)
538 int status = -EINVAL;
539 struct sk_buff *nskb;
540 struct nfqnl_instance *queue;
541 struct nfqnl_queue_entry *entry;
545 queue = instance_lookup_get(queuenum);
547 QDEBUG("no queue instance matching\n");
551 if (queue->copy_mode == NFQNL_COPY_NONE) {
552 QDEBUG("mode COPY_NONE, aborting\n");
557 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
561 "nf_queue: OOM in nfqnl_enqueue_packet()\n");
568 entry->id = atomic_inc_return(&queue->id_sequence);
570 nskb = nfqnl_build_packet_message(queue, entry, &status);
574 spin_lock_bh(&queue->lock);
576 if (!queue->peer_pid)
577 goto err_out_free_nskb;
579 if (queue->queue_total >= queue->queue_maxlen) {
580 queue->queue_dropped++;
583 printk(KERN_WARNING "nf_queue: full at %d entries, "
584 "dropping packets(s). Dropped: %d\n",
585 queue->queue_total, queue->queue_dropped);
586 goto err_out_free_nskb;
589 /* nfnetlink_unicast will either free the nskb or add it to a socket */
590 status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
592 queue->queue_user_dropped++;
596 __enqueue_entry(queue, entry);
598 spin_unlock_bh(&queue->lock);
606 spin_unlock_bh(&queue->lock);
616 nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
621 diff = data_len - e->skb->len;
623 if (pskb_trim(e->skb, data_len))
625 } else if (diff > 0) {
626 if (data_len > 0xFFFF)
628 if (diff > skb_tailroom(e->skb)) {
629 err = pskb_expand_head(e->skb, 0,
630 diff - skb_tailroom(e->skb),
633 printk(KERN_WARNING "nf_queue: OOM "
634 "in mangle, dropping packet\n");
638 skb_put(e->skb, diff);
640 if (!skb_make_writable(e->skb, data_len))
642 skb_copy_to_linear_data(e->skb, data, data_len);
643 e->skb->ip_summed = CHECKSUM_NONE;
648 id_cmp(struct nfqnl_queue_entry *e, unsigned long id)
650 return (id == e->id);
654 nfqnl_set_mode(struct nfqnl_instance *queue,
655 unsigned char mode, unsigned int range)
659 spin_lock_bh(&queue->lock);
660 status = __nfqnl_set_mode(queue, mode, range);
661 spin_unlock_bh(&queue->lock);
667 dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex)
669 struct nf_info *entinf = entry->info;
672 if (entinf->indev->ifindex == ifindex)
675 if (entinf->outdev->ifindex == ifindex)
677 #ifdef CONFIG_BRIDGE_NETFILTER
678 if (entry->skb->nf_bridge) {
679 if (entry->skb->nf_bridge->physindev &&
680 entry->skb->nf_bridge->physindev->ifindex == ifindex)
682 if (entry->skb->nf_bridge->physoutdev &&
683 entry->skb->nf_bridge->physoutdev->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 if (dev->nd_net != &init_net)
731 /* Drop any packets associated with the downed device */
732 if (event == NETDEV_DOWN)
733 nfqnl_dev_drop(dev->ifindex);
737 static struct notifier_block nfqnl_dev_notifier = {
738 .notifier_call = nfqnl_rcv_dev_event,
742 nfqnl_rcv_nl_event(struct notifier_block *this,
743 unsigned long event, void *ptr)
745 struct netlink_notify *n = ptr;
747 if (event == NETLINK_URELEASE &&
748 n->protocol == NETLINK_NETFILTER && n->pid) {
751 /* destroy all instances for this pid */
752 write_lock_bh(&instances_lock);
753 for (i = 0; i < INSTANCE_BUCKETS; i++) {
754 struct hlist_node *tmp, *t2;
755 struct nfqnl_instance *inst;
756 struct hlist_head *head = &instance_table[i];
758 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
759 if ((n->net == &init_net) &&
760 (n->pid == inst->peer_pid))
761 __instance_destroy(inst);
764 write_unlock_bh(&instances_lock);
769 static struct notifier_block nfqnl_rtnl_notifier = {
770 .notifier_call = nfqnl_rcv_nl_event,
773 static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
774 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
775 [NFQA_MARK] = { .type = NLA_U32 },
776 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
780 nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
781 struct nlmsghdr *nlh, struct nlattr *nfqa[])
783 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
784 u_int16_t queue_num = ntohs(nfmsg->res_id);
786 struct nfqnl_msg_verdict_hdr *vhdr;
787 struct nfqnl_instance *queue;
788 unsigned int verdict;
789 struct nfqnl_queue_entry *entry;
792 queue = instance_lookup_get(queue_num);
796 if (queue->peer_pid != NETLINK_CB(skb).pid) {
801 if (!nfqa[NFQA_VERDICT_HDR]) {
806 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
807 verdict = ntohl(vhdr->verdict);
809 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
814 entry = find_dequeue_entry(queue, id_cmp, ntohl(vhdr->id));
820 if (nfqa[NFQA_PAYLOAD]) {
821 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
822 nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0)
827 entry->skb->mark = ntohl(*(__be32 *)
828 nla_data(nfqa[NFQA_MARK]));
830 issue_verdict(entry, verdict);
840 nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
841 struct nlmsghdr *nlh, struct nlattr *nfqa[])
846 static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
847 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
848 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
851 static const struct nf_queue_handler nfqh = {
853 .outfn = &nfqnl_enqueue_packet,
857 nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
858 struct nlmsghdr *nlh, struct nlattr *nfqa[])
860 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
861 u_int16_t queue_num = ntohs(nfmsg->res_id);
862 struct nfqnl_instance *queue;
865 QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
867 queue = instance_lookup_get(queue_num);
868 if (nfqa[NFQA_CFG_CMD]) {
869 struct nfqnl_msg_config_cmd *cmd;
870 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
871 QDEBUG("found CFG_CMD\n");
873 switch (cmd->command) {
874 case NFQNL_CFG_CMD_BIND:
878 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
882 case NFQNL_CFG_CMD_UNBIND:
886 if (queue->peer_pid != NETLINK_CB(skb).pid) {
891 instance_destroy(queue);
893 case NFQNL_CFG_CMD_PF_BIND:
894 QDEBUG("registering queue handler for pf=%u\n",
896 ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh);
898 case NFQNL_CFG_CMD_PF_UNBIND:
899 QDEBUG("unregistering queue handler for pf=%u\n",
901 ret = nf_unregister_queue_handler(ntohs(cmd->pf), &nfqh);
909 QDEBUG("no config command, and no instance ENOENT\n");
914 if (queue->peer_pid != NETLINK_CB(skb).pid) {
915 QDEBUG("no config command, and wrong pid\n");
921 if (nfqa[NFQA_CFG_PARAMS]) {
922 struct nfqnl_msg_config_params *params;
928 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
929 nfqnl_set_mode(queue, params->copy_mode,
930 ntohl(params->copy_range));
933 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
934 __be32 *queue_maxlen;
935 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
936 spin_lock_bh(&queue->lock);
937 queue->queue_maxlen = ntohl(*queue_maxlen);
938 spin_unlock_bh(&queue->lock);
946 static const 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 .policy = nfqa_verdict_policy },
952 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
953 .attr_count = NFQA_CFG_MAX,
954 .policy = nfqa_cfg_policy },
957 static const struct nfnetlink_subsystem nfqnl_subsys = {
959 .subsys_id = NFNL_SUBSYS_QUEUE,
960 .cb_count = NFQNL_MSG_MAX,
964 #ifdef CONFIG_PROC_FS
969 static struct hlist_node *get_first(struct seq_file *seq)
971 struct iter_state *st = seq->private;
976 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
977 if (!hlist_empty(&instance_table[st->bucket]))
978 return instance_table[st->bucket].first;
983 static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
985 struct iter_state *st = seq->private;
989 if (++st->bucket >= INSTANCE_BUCKETS)
992 h = instance_table[st->bucket].first;
997 static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
999 struct hlist_node *head;
1000 head = get_first(seq);
1003 while (pos && (head = get_next(seq, head)))
1005 return pos ? NULL : head;
1008 static void *seq_start(struct seq_file *seq, loff_t *pos)
1010 read_lock_bh(&instances_lock);
1011 return get_idx(seq, *pos);
1014 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1017 return get_next(s, v);
1020 static void seq_stop(struct seq_file *s, void *v)
1022 read_unlock_bh(&instances_lock);
1025 static int seq_show(struct seq_file *s, void *v)
1027 const struct nfqnl_instance *inst = v;
1029 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1031 inst->peer_pid, inst->queue_total,
1032 inst->copy_mode, inst->copy_range,
1033 inst->queue_dropped, inst->queue_user_dropped,
1034 atomic_read(&inst->id_sequence),
1035 atomic_read(&inst->use));
1038 static const struct seq_operations nfqnl_seq_ops = {
1045 static int nfqnl_open(struct inode *inode, struct file *file)
1047 return seq_open_private(file, &nfqnl_seq_ops,
1048 sizeof(struct iter_state));
1051 static const struct file_operations nfqnl_file_ops = {
1052 .owner = THIS_MODULE,
1055 .llseek = seq_lseek,
1056 .release = seq_release_private,
1059 #endif /* PROC_FS */
1061 static int __init nfnetlink_queue_init(void)
1063 int i, status = -ENOMEM;
1064 #ifdef CONFIG_PROC_FS
1065 struct proc_dir_entry *proc_nfqueue;
1068 for (i = 0; i < INSTANCE_BUCKETS; i++)
1069 INIT_HLIST_HEAD(&instance_table[i]);
1071 netlink_register_notifier(&nfqnl_rtnl_notifier);
1072 status = nfnetlink_subsys_register(&nfqnl_subsys);
1074 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1075 goto cleanup_netlink_notifier;
1078 #ifdef CONFIG_PROC_FS
1079 proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
1080 proc_net_netfilter);
1082 goto cleanup_subsys;
1083 proc_nfqueue->proc_fops = &nfqnl_file_ops;
1086 register_netdevice_notifier(&nfqnl_dev_notifier);
1089 #ifdef CONFIG_PROC_FS
1091 nfnetlink_subsys_unregister(&nfqnl_subsys);
1093 cleanup_netlink_notifier:
1094 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1098 static void __exit nfnetlink_queue_fini(void)
1100 nf_unregister_queue_handlers(&nfqh);
1101 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1102 #ifdef CONFIG_PROC_FS
1103 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1105 nfnetlink_subsys_unregister(&nfqnl_subsys);
1106 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1109 MODULE_DESCRIPTION("netfilter packet queue handler");
1110 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1111 MODULE_LICENSE("GPL");
1112 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1114 module_init(nfnetlink_queue_init);
1115 module_exit(nfnetlink_queue_fini);