[NETFILTER]: Fix whitespace errors
[safe/jmp/linux-2.6] / net / netfilter / nfnetlink_queue.c
index 2403261..d9ce4a7 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/notifier.h>
 #include <linux/netdevice.h>
 #include <linux/netfilter.h>
+#include <linux/proc_fs.h>
 #include <linux/netfilter_ipv4.h>
 #include <linux/netfilter_ipv6.h>
 #include <linux/netfilter/nfnetlink.h>
 
 #include <asm/atomic.h>
 
+#ifdef CONFIG_BRIDGE_NETFILTER
+#include "../bridge/br_private.h"
+#endif
+
 #define NFQNL_QMAX_DEFAULT 1024
 
 #if 0
@@ -48,6 +53,7 @@ struct nfqnl_queue_entry {
 
 struct nfqnl_instance {
        struct hlist_node hlist;                /* global list of queues */
+       atomic_t use;
 
        int peer_pid;
        unsigned int queue_maxlen;
@@ -70,17 +76,6 @@ typedef int (*nfqnl_cmpfn)(struct nfqnl_queue_entry *, unsigned long);
 
 static DEFINE_RWLOCK(instances_lock);
 
-u_int64_t htonll(u_int64_t in)
-{
-       u_int64_t out;
-       int i;
-
-       for (i = 0; i < sizeof(u_int64_t); i++)
-               ((u_int8_t *)&out)[sizeof(u_int64_t)-1] = ((u_int8_t *)&in)[i];
-
-       return out;
-}
-
 #define INSTANCE_BUCKETS       16
 static struct hlist_head instance_table[INSTANCE_BUCKETS];
 
@@ -105,17 +100,28 @@ __instance_lookup(u_int16_t queue_num)
 }
 
 static struct nfqnl_instance *
-instance_lookup(u_int16_t queue_num)
+instance_lookup_get(u_int16_t queue_num)
 {
        struct nfqnl_instance *inst;
 
        read_lock_bh(&instances_lock);
        inst = __instance_lookup(queue_num);
+       if (inst)
+               atomic_inc(&inst->use);
        read_unlock_bh(&instances_lock);
 
        return inst;
 }
 
+static void
+instance_put(struct nfqnl_instance *inst)
+{
+       if (inst && atomic_dec_and_test(&inst->use)) {
+               QDEBUG("kfree(inst=%p)\n", inst);
+               kfree(inst);
+       }
+}
+
 static struct nfqnl_instance *
 instance_create(u_int16_t queue_num, int pid)
 {
@@ -123,31 +129,32 @@ instance_create(u_int16_t queue_num, int pid)
 
        QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid);
 
-       write_lock_bh(&instances_lock); 
+       write_lock_bh(&instances_lock);
        if (__instance_lookup(queue_num)) {
                inst = NULL;
                QDEBUG("aborting, instance already exists\n");
                goto out_unlock;
        }
 
-       inst = kmalloc(sizeof(*inst), GFP_ATOMIC);
+       inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
        if (!inst)
                goto out_unlock;
 
-       memset(inst, 0, sizeof(*inst));
        inst->queue_num = queue_num;
        inst->peer_pid = pid;
        inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
        inst->copy_range = 0xfffff;
        inst->copy_mode = NFQNL_COPY_NONE;
        atomic_set(&inst->id_sequence, 0);
-       inst->lock = SPIN_LOCK_UNLOCKED;
+       /* needs to be two, since we _put() after creation */
+       atomic_set(&inst->use, 2);
+       spin_lock_init(&inst->lock);
        INIT_LIST_HEAD(&inst->queue_list);
 
        if (!try_module_get(THIS_MODULE))
                goto out_free;
 
-       hlist_add_head(&inst->hlist, 
+       hlist_add_head(&inst->hlist,
                       &instance_table[instance_hashfn(queue_num)]);
 
        write_unlock_bh(&instances_lock);
@@ -182,8 +189,8 @@ _instance_destroy2(struct nfqnl_instance *inst, int lock)
        /* then flush all pending skbs from the queue */
        nfqnl_flush(inst, NF_DROP);
 
-       /* and finally free the data structure */
-       kfree(inst);
+       /* and finally put the refcount */
+       instance_put(inst);
 
        module_put(THIS_MODULE);
 }
@@ -232,14 +239,14 @@ __enqueue_entry(struct nfqnl_instance *queue,
  * entry if cmpfn is NULL.
  */
 static inline struct nfqnl_queue_entry *
-__find_entry(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, 
+__find_entry(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
                   unsigned long data)
 {
        struct list_head *p;
 
        list_for_each_prev(p, &queue->queue_list) {
                struct nfqnl_queue_entry *entry = (struct nfqnl_queue_entry *)p;
-               
+
                if (!cmpfn || cmpfn(entry, data))
                        return entry;
        }
@@ -272,7 +279,7 @@ static inline void
 __nfqnl_flush(struct nfqnl_instance *queue, int verdict)
 {
        struct nfqnl_queue_entry *entry;
-       
+
        while ((entry = __find_dequeue_entry(queue, NULL, 0)))
                issue_verdict(entry, verdict);
 }
@@ -282,14 +289,14 @@ __nfqnl_set_mode(struct nfqnl_instance *queue,
                 unsigned char mode, unsigned int range)
 {
        int status = 0;
-       
+
        switch (mode) {
        case NFQNL_COPY_NONE:
        case NFQNL_COPY_META:
                queue->copy_mode = mode;
                queue->copy_range = 0;
                break;
-               
+
        case NFQNL_COPY_PACKET:
                queue->copy_mode = mode;
                /* we're using struct nfattr which has 16bit nfa_len */
@@ -298,7 +305,7 @@ __nfqnl_set_mode(struct nfqnl_instance *queue,
                else
                        queue->copy_range = range;
                break;
-               
+
        default:
                status = -EINVAL;
 
@@ -311,7 +318,7 @@ find_dequeue_entry(struct nfqnl_instance *queue,
                         nfqnl_cmpfn cmpfn, unsigned long data)
 {
        struct nfqnl_queue_entry *entry;
-       
+
        spin_lock_bh(&queue->lock);
        entry = __find_dequeue_entry(queue, cmpfn, data);
        spin_unlock_bh(&queue->lock);
@@ -338,36 +345,53 @@ nfqnl_build_packet_message(struct nfqnl_instance *queue,
        struct nfqnl_msg_packet_hdr pmsg;
        struct nlmsghdr *nlh;
        struct nfgenmsg *nfmsg;
-       unsigned int tmp_uint;
+       struct nf_info *entinf = entry->info;
+       struct sk_buff *entskb = entry->skb;
+       struct net_device *indev;
+       struct net_device *outdev;
+       __be32 tmp_uint;
 
        QDEBUG("entered\n");
 
        /* all macros expand to constant values at compile time */
-       size =    NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hdr))
-               + NLMSG_SPACE(sizeof(u_int32_t))        /* ifindex */
-               + NLMSG_SPACE(sizeof(u_int32_t))        /* ifindex */
-               + NLMSG_SPACE(sizeof(u_int32_t))        /* mark */
-               + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hw))
-               + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_timestamp));
+       size =    NLMSG_SPACE(sizeof(struct nfgenmsg)) +
+               + NFA_SPACE(sizeof(struct nfqnl_msg_packet_hdr))
+               + NFA_SPACE(sizeof(u_int32_t))  /* ifindex */
+               + NFA_SPACE(sizeof(u_int32_t))  /* ifindex */
+#ifdef CONFIG_BRIDGE_NETFILTER
+               + NFA_SPACE(sizeof(u_int32_t))  /* ifindex */
+               + NFA_SPACE(sizeof(u_int32_t))  /* ifindex */
+#endif
+               + NFA_SPACE(sizeof(u_int32_t))  /* mark */
+               + NFA_SPACE(sizeof(struct nfqnl_msg_packet_hw))
+               + NFA_SPACE(sizeof(struct nfqnl_msg_packet_timestamp));
+
+       outdev = entinf->outdev;
 
        spin_lock_bh(&queue->lock);
-       
+
        switch (queue->copy_mode) {
        case NFQNL_COPY_META:
        case NFQNL_COPY_NONE:
                data_len = 0;
                break;
-       
+
        case NFQNL_COPY_PACKET:
-               if (queue->copy_range == 0 
-                   || queue->copy_range > entry->skb->len)
-                       data_len = entry->skb->len;
+               if ((entskb->ip_summed == CHECKSUM_PARTIAL ||
+                    entskb->ip_summed == CHECKSUM_COMPLETE) &&
+                   (*errp = skb_checksum_help(entskb))) {
+                       spin_unlock_bh(&queue->lock);
+                       return NULL;
+               }
+               if (queue->copy_range == 0
+                   || queue->copy_range > entskb->len)
+                       data_len = entskb->len;
                else
                        data_len = queue->copy_range;
-               
-               size += NLMSG_SPACE(data_len);
+
+               size += NFA_SPACE(data_len);
                break;
-       
+
        default:
                *errp = -EINVAL;
                spin_unlock_bh(&queue->lock);
@@ -379,53 +403,103 @@ nfqnl_build_packet_message(struct nfqnl_instance *queue,
        skb = alloc_skb(size, GFP_ATOMIC);
        if (!skb)
                goto nlmsg_failure;
-               
+
        old_tail= skb->tail;
-       nlh = NLMSG_PUT(skb, 0, 0, 
+       nlh = NLMSG_PUT(skb, 0, 0,
                        NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
                        sizeof(struct nfgenmsg));
        nfmsg = NLMSG_DATA(nlh);
-       nfmsg->nfgen_family = entry->info->pf;
+       nfmsg->nfgen_family = entinf->pf;
        nfmsg->version = NFNETLINK_V0;
        nfmsg->res_id = htons(queue->queue_num);
 
        pmsg.packet_id          = htonl(entry->id);
-       pmsg.hw_protocol        = htons(entry->skb->protocol);
-       pmsg.hook               = entry->info->hook;
+       pmsg.hw_protocol        = entskb->protocol;
+       pmsg.hook               = entinf->hook;
 
        NFA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
 
-       if (entry->info->indev) {
-               tmp_uint = htonl(entry->info->indev->ifindex);
+       indev = entinf->indev;
+       if (indev) {
+               tmp_uint = htonl(indev->ifindex);
+#ifndef CONFIG_BRIDGE_NETFILTER
                NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
+#else
+               if (entinf->pf == PF_BRIDGE) {
+                       /* Case 1: indev is physical input device, we need to
+                        * look for bridge group (when called from
+                        * netfilter_bridge) */
+                       NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint),
+                               &tmp_uint);
+                       /* this is the bridge group "brX" */
+                       tmp_uint = htonl(indev->br_port->br->dev->ifindex);
+                       NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
+                               &tmp_uint);
+               } else {
+                       /* Case 2: indev is bridge group, we need to look for
+                        * physical device (when called from ipv4) */
+                       NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
+                               &tmp_uint);
+                       if (entskb->nf_bridge
+                           && entskb->nf_bridge->physindev) {
+                               tmp_uint = htonl(entskb->nf_bridge->physindev->ifindex);
+                               NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
+                                       sizeof(tmp_uint), &tmp_uint);
+                       }
+               }
+#endif
        }
 
-       if (entry->info->outdev) {
-               tmp_uint = htonl(entry->info->outdev->ifindex);
+       if (outdev) {
+               tmp_uint = htonl(outdev->ifindex);
+#ifndef CONFIG_BRIDGE_NETFILTER
                NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
+#else
+               if (entinf->pf == PF_BRIDGE) {
+                       /* Case 1: outdev is physical output device, we need to
+                        * look for bridge group (when called from
+                        * netfilter_bridge) */
+                       NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
+                               &tmp_uint);
+                       /* this is the bridge group "brX" */
+                       tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
+                       NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
+                               &tmp_uint);
+               } else {
+                       /* Case 2: outdev is bridge group, we need to look for
+                        * physical output device (when called from ipv4) */
+                       NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
+                               &tmp_uint);
+                       if (entskb->nf_bridge
+                           && entskb->nf_bridge->physoutdev) {
+                               tmp_uint = htonl(entskb->nf_bridge->physoutdev->ifindex);
+                               NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
+                                       sizeof(tmp_uint), &tmp_uint);
+                       }
+               }
+#endif
        }
 
-       if (entry->skb->nfmark) {
-               tmp_uint = htonl(entry->skb->nfmark);
+       if (entskb->mark) {
+               tmp_uint = htonl(entskb->mark);
                NFA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
        }
 
-       if (entry->info->indev && entry->skb->dev
-           && entry->skb->dev->hard_header_parse) {
+       if (indev && entskb->dev
+           && entskb->dev->hard_header_parse) {
                struct nfqnl_msg_packet_hw phw;
 
-               phw.hw_addrlen =
-                       entry->skb->dev->hard_header_parse(entry->skb,
-                                                          phw.hw_addr);
-               phw.hw_addrlen = htons(phw.hw_addrlen);
+               int len = entskb->dev->hard_header_parse(entskb,
+                                                          phw.hw_addr);
+               phw.hw_addrlen = htons(len);
                NFA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
        }
 
-       if (entry->skb->stamp.tv_sec) {
+       if (entskb->tstamp.off_sec) {
                struct nfqnl_msg_packet_timestamp ts;
 
-               ts.sec = htonll(entry->skb->stamp.tv_sec);
-               ts.usec = htonll(entry->skb->stamp.tv_usec);
+               ts.sec = cpu_to_be64(entskb->tstamp.off_sec);
+               ts.usec = cpu_to_be64(entskb->tstamp.off_usec);
 
                NFA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
        }
@@ -443,10 +517,10 @@ nfqnl_build_packet_message(struct nfqnl_instance *queue,
                nfa->nfa_type = NFQA_PAYLOAD;
                nfa->nfa_len = size;
 
-               if (skb_copy_bits(entry->skb, 0, NFA_DATA(nfa), data_len))
+               if (skb_copy_bits(entskb, 0, NFA_DATA(nfa), data_len))
                        BUG();
        }
-               
+
        nlh->nlmsg_len = skb->tail - old_tail;
        return skb;
 
@@ -461,7 +535,7 @@ nfattr_failure:
 }
 
 static int
-nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info, 
+nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
                     unsigned int queuenum, void *data)
 {
        int status = -EINVAL;
@@ -471,7 +545,7 @@ nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
 
        QDEBUG("entered\n");
 
-       queue = instance_lookup(queuenum);
+       queue = instance_lookup_get(queuenum);
        if (!queue) {
                QDEBUG("no queue instance matching\n");
                return -EINVAL;
@@ -479,15 +553,17 @@ nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
 
        if (queue->copy_mode == NFQNL_COPY_NONE) {
                QDEBUG("mode COPY_NONE, aborting\n");
-               return -EAGAIN;
+               status = -EAGAIN;
+               goto err_out_put;
        }
 
        entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
        if (entry == NULL) {
                if (net_ratelimit())
-                       printk(KERN_ERR 
+                       printk(KERN_ERR
                                "nf_queue: OOM in nfqnl_enqueue_packet()\n");
-               return -ENOMEM;
+               status = -ENOMEM;
+               goto err_out_put;
        }
 
        entry->info = info;
@@ -497,18 +573,18 @@ nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
        nskb = nfqnl_build_packet_message(queue, entry, &status);
        if (nskb == NULL)
                goto err_out_free;
-               
+
        spin_lock_bh(&queue->lock);
-       
+
        if (!queue->peer_pid)
-               goto err_out_free_nskb; 
+               goto err_out_free_nskb;
 
        if (queue->queue_total >= queue->queue_maxlen) {
-                queue->queue_dropped++;
+               queue->queue_dropped++;
                status = -ENOSPC;
                if (net_ratelimit())
-                         printk(KERN_WARNING "ip_queue: full at %d entries, "
-                                "dropping packets(s). Dropped: %d\n", 
+                         printk(KERN_WARNING "nf_queue: full at %d entries, "
+                                "dropping packets(s). Dropped: %d\n",
                                 queue->queue_total, queue->queue_dropped);
                goto err_out_free_nskb;
        }
@@ -516,23 +592,26 @@ nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
        /* nfnetlink_unicast will either free the nskb or add it to a socket */
        status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
        if (status < 0) {
-               queue->queue_user_dropped++;
+               queue->queue_user_dropped++;
                goto err_out_unlock;
        }
 
        __enqueue_entry(queue, entry);
 
        spin_unlock_bh(&queue->lock);
+       instance_put(queue);
        return status;
 
 err_out_free_nskb:
-       kfree_skb(nskb); 
-       
+       kfree_skb(nskb);
+
 err_out_unlock:
        spin_unlock_bh(&queue->lock);
 
 err_out_free:
        kfree(entry);
+err_out_put:
+       instance_put(queue);
        return status;
 }
 
@@ -542,20 +621,21 @@ nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
        int diff;
 
        diff = data_len - e->skb->len;
-       if (diff < 0)
-               skb_trim(e->skb, data_len);
-       else if (diff > 0) {
+       if (diff < 0) {
+               if (pskb_trim(e->skb, data_len))
+                       return -ENOMEM;
+       } else if (diff > 0) {
                if (data_len > 0xFFFF)
                        return -EINVAL;
                if (diff > skb_tailroom(e->skb)) {
                        struct sk_buff *newskb;
-                       
+
                        newskb = skb_copy_expand(e->skb,
-                                                skb_headroom(e->skb),
-                                                diff,
-                                                GFP_ATOMIC);
+                                                skb_headroom(e->skb),
+                                                diff,
+                                                GFP_ATOMIC);
                        if (newskb == NULL) {
-                               printk(KERN_WARNING "ip_queue: OOM "
+                               printk(KERN_WARNING "nf_queue: OOM "
                                      "in mangle, dropping packet\n");
                                return -ENOMEM;
                        }
@@ -569,7 +649,7 @@ nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
        if (!skb_make_writable(&e->skb, data_len))
                return -ENOMEM;
        memcpy(e->skb->data, data, data_len);
-
+       e->skb->ip_summed = CHECKSUM_NONE;
        return 0;
 }
 
@@ -595,14 +675,24 @@ nfqnl_set_mode(struct nfqnl_instance *queue,
 static int
 dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex)
 {
-       if (entry->info->indev)
-               if (entry->info->indev->ifindex == ifindex)
+       struct nf_info *entinf = entry->info;
+
+       if (entinf->indev)
+               if (entinf->indev->ifindex == ifindex)
                        return 1;
-                       
-       if (entry->info->outdev)
-               if (entry->info->outdev->ifindex == ifindex)
+       if (entinf->outdev)
+               if (entinf->outdev->ifindex == ifindex)
                        return 1;
-
+#ifdef CONFIG_BRIDGE_NETFILTER
+       if (entry->skb->nf_bridge) {
+               if (entry->skb->nf_bridge->physindev &&
+                   entry->skb->nf_bridge->physindev->ifindex == ifindex)
+                       return 1;
+               if (entry->skb->nf_bridge->physoutdev &&
+                   entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
+                       return 1;
+       }
+#endif
        return 0;
 }
 
@@ -612,7 +702,7 @@ static void
 nfqnl_dev_drop(int ifindex)
 {
        int i;
-       
+
        QDEBUG("entering for ifindex %u\n", ifindex);
 
        /* this only looks like we have to hold the readlock for a way too long
@@ -627,7 +717,7 @@ nfqnl_dev_drop(int ifindex)
 
                hlist_for_each_entry(inst, tmp, head, hlist) {
                        struct nfqnl_queue_entry *entry;
-                       while ((entry = find_dequeue_entry(inst, dev_cmp, 
+                       while ((entry = find_dequeue_entry(inst, dev_cmp,
                                                           ifindex)) != NULL)
                                issue_verdict(entry, NF_DROP);
                }
@@ -685,6 +775,12 @@ static struct notifier_block nfqnl_rtnl_notifier = {
        .notifier_call  = nfqnl_rcv_nl_event,
 };
 
+static const int nfqa_verdict_min[NFQA_MAX] = {
+       [NFQA_VERDICT_HDR-1]    = sizeof(struct nfqnl_msg_verdict_hdr),
+       [NFQA_MARK-1]           = sizeof(u_int32_t),
+       [NFQA_PAYLOAD-1]        = 0,
+};
+
 static int
 nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
                   struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
@@ -696,26 +792,40 @@ nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
        struct nfqnl_instance *queue;
        unsigned int verdict;
        struct nfqnl_queue_entry *entry;
+       int err;
+
+       if (nfattr_bad_size(nfqa, NFQA_MAX, nfqa_verdict_min)) {
+               QDEBUG("bad attribute size\n");
+               return -EINVAL;
+       }
 
-       queue = instance_lookup(queue_num);
+       queue = instance_lookup_get(queue_num);
        if (!queue)
                return -ENODEV;
 
-       if (queue->peer_pid != NETLINK_CB(skb).pid)
-               return -EPERM;
+       if (queue->peer_pid != NETLINK_CB(skb).pid) {
+               err = -EPERM;
+               goto err_out_put;
+       }
 
-       if (!nfqa[NFQA_VERDICT_HDR-1])
-               return -EINVAL;
+       if (!nfqa[NFQA_VERDICT_HDR-1]) {
+               err = -EINVAL;
+               goto err_out_put;
+       }
 
        vhdr = NFA_DATA(nfqa[NFQA_VERDICT_HDR-1]);
        verdict = ntohl(vhdr->verdict);
 
-       if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT)
-               return -EINVAL;
+       if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
+               err = -EINVAL;
+               goto err_out_put;
+       }
 
        entry = find_dequeue_entry(queue, id_cmp, ntohl(vhdr->id));
-       if (entry == NULL)
-               return -ENOENT;
+       if (entry == NULL) {
+               err = -ENOENT;
+               goto err_out_put;
+       }
 
        if (nfqa[NFQA_PAYLOAD-1]) {
                if (nfqnl_mangle(NFA_DATA(nfqa[NFQA_PAYLOAD-1]),
@@ -724,10 +834,16 @@ nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
        }
 
        if (nfqa[NFQA_MARK-1])
-               skb->nfmark = ntohl(*(u_int32_t *)NFA_DATA(nfqa[NFQA_MARK-1]));
-               
+               entry->skb->mark = ntohl(*(__be32 *)
+                                        NFA_DATA(nfqa[NFQA_MARK-1]));
+
        issue_verdict(entry, verdict);
+       instance_put(queue);
        return 0;
+
+err_out_put:
+       instance_put(queue);
+       return err;
 }
 
 static int
@@ -737,6 +853,16 @@ nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
        return -ENOTSUPP;
 }
 
+static const int nfqa_cfg_min[NFQA_CFG_MAX] = {
+       [NFQA_CFG_CMD-1]        = sizeof(struct nfqnl_msg_config_cmd),
+       [NFQA_CFG_PARAMS-1]     = sizeof(struct nfqnl_msg_config_params),
+};
+
+static struct nf_queue_handler nfqh = {
+       .name   = "nf_queue",
+       .outfn  = &nfqnl_enqueue_packet,
+};
+
 static int
 nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
                  struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
@@ -744,10 +870,16 @@ nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
        struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
        u_int16_t queue_num = ntohs(nfmsg->res_id);
        struct nfqnl_instance *queue;
+       int ret = 0;
 
        QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
 
-       queue = instance_lookup(queue_num);
+       if (nfattr_bad_size(nfqa, NFQA_CFG_MAX, nfqa_cfg_min)) {
+               QDEBUG("bad attribute size\n");
+               return -EINVAL;
+       }
+
+       queue = instance_lookup_get(queue_num);
        if (nfqa[NFQA_CFG_CMD-1]) {
                struct nfqnl_msg_config_cmd *cmd;
                cmd = NFA_DATA(nfqa[NFQA_CFG_CMD-1]);
@@ -766,76 +898,204 @@ nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
                        if (!queue)
                                return -ENODEV;
 
-                       if (queue->peer_pid != NETLINK_CB(skb).pid)
-                               return -EPERM;
+                       if (queue->peer_pid != NETLINK_CB(skb).pid) {
+                               ret = -EPERM;
+                               goto out_put;
+                       }
 
                        instance_destroy(queue);
                        break;
                case NFQNL_CFG_CMD_PF_BIND:
                        QDEBUG("registering queue handler for pf=%u\n",
                                ntohs(cmd->pf));
-                       return nf_register_queue_handler(ntohs(cmd->pf),
-                                                        nfqnl_enqueue_packet,
-                                                        NULL);
-
+                       ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh);
                        break;
                case NFQNL_CFG_CMD_PF_UNBIND:
                        QDEBUG("unregistering queue handler for pf=%u\n",
                                ntohs(cmd->pf));
                        /* This is a bug and a feature.  We can unregister
                         * other handlers(!) */
-                       return nf_unregister_queue_handler(ntohs(cmd->pf));
+                       ret = nf_unregister_queue_handler(ntohs(cmd->pf));
                        break;
                default:
-                       return -EINVAL;
+                       ret = -EINVAL;
+                       break;
                }
        } else {
                if (!queue) {
                        QDEBUG("no config command, and no instance ENOENT\n");
-                       return -ENOENT;
+                       ret = -ENOENT;
+                       goto out_put;
                }
 
                if (queue->peer_pid != NETLINK_CB(skb).pid) {
                        QDEBUG("no config command, and wrong pid\n");
-                       return -EPERM;
+                       ret = -EPERM;
+                       goto out_put;
                }
        }
 
        if (nfqa[NFQA_CFG_PARAMS-1]) {
                struct nfqnl_msg_config_params *params;
-               params = NFA_DATA(nfqa[NFQA_CFG_PARAMS-1]);
 
+               if (!queue) {
+                       ret = -ENOENT;
+                       goto out_put;
+               }
+               params = NFA_DATA(nfqa[NFQA_CFG_PARAMS-1]);
                nfqnl_set_mode(queue, params->copy_mode,
                                ntohl(params->copy_range));
        }
 
-       return 0;
+       if (nfqa[NFQA_CFG_QUEUE_MAXLEN-1]) {
+               __be32 *queue_maxlen;
+               queue_maxlen = NFA_DATA(nfqa[NFQA_CFG_QUEUE_MAXLEN-1]);
+               spin_lock_bh(&queue->lock);
+               queue->queue_maxlen = ntohl(*queue_maxlen);
+               spin_unlock_bh(&queue->lock);
+       }
+
+out_put:
+       instance_put(queue);
+       return ret;
 }
 
 static struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
        [NFQNL_MSG_PACKET]      = { .call = nfqnl_recv_unsupp,
-                                   .cap_required = CAP_NET_ADMIN },
+                                   .attr_count = NFQA_MAX, },
        [NFQNL_MSG_VERDICT]     = { .call = nfqnl_recv_verdict,
-                                   .cap_required = CAP_NET_ADMIN },
+                                   .attr_count = NFQA_MAX, },
        [NFQNL_MSG_CONFIG]      = { .call = nfqnl_recv_config,
-                                   .cap_required = CAP_NET_ADMIN },
+                                   .attr_count = NFQA_CFG_MAX, },
 };
 
 static struct nfnetlink_subsystem nfqnl_subsys = {
        .name           = "nf_queue",
        .subsys_id      = NFNL_SUBSYS_QUEUE,
        .cb_count       = NFQNL_MSG_MAX,
-       .attr_count     = NFQA_MAX,
        .cb             = nfqnl_cb,
 };
 
-static int
-init_or_cleanup(int init)
+#ifdef CONFIG_PROC_FS
+struct iter_state {
+       unsigned int bucket;
+};
+
+static struct hlist_node *get_first(struct seq_file *seq)
+{
+       struct iter_state *st = seq->private;
+
+       if (!st)
+               return NULL;
+
+       for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
+               if (!hlist_empty(&instance_table[st->bucket]))
+                       return instance_table[st->bucket].first;
+       }
+       return NULL;
+}
+
+static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
+{
+       struct iter_state *st = seq->private;
+
+       h = h->next;
+       while (!h) {
+               if (++st->bucket >= INSTANCE_BUCKETS)
+                       return NULL;
+
+               h = instance_table[st->bucket].first;
+       }
+       return h;
+}
+
+static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
+{
+       struct hlist_node *head;
+       head = get_first(seq);
+
+       if (head)
+               while (pos && (head = get_next(seq, head)))
+                       pos--;
+       return pos ? NULL : head;
+}
+
+static void *seq_start(struct seq_file *seq, loff_t *pos)
+{
+       read_lock_bh(&instances_lock);
+       return get_idx(seq, *pos);
+}
+
+static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
+{
+       (*pos)++;
+       return get_next(s, v);
+}
+
+static void seq_stop(struct seq_file *s, void *v)
+{
+       read_unlock_bh(&instances_lock);
+}
+
+static int seq_show(struct seq_file *s, void *v)
+{
+       const struct nfqnl_instance *inst = v;
+
+       return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
+                         inst->queue_num,
+                         inst->peer_pid, inst->queue_total,
+                         inst->copy_mode, inst->copy_range,
+                         inst->queue_dropped, inst->queue_user_dropped,
+                         atomic_read(&inst->id_sequence),
+                         atomic_read(&inst->use));
+}
+
+static struct seq_operations nfqnl_seq_ops = {
+       .start  = seq_start,
+       .next   = seq_next,
+       .stop   = seq_stop,
+       .show   = seq_show,
+};
+
+static int nfqnl_open(struct inode *inode, struct file *file)
+{
+       struct seq_file *seq;
+       struct iter_state *is;
+       int ret;
+
+       is = kzalloc(sizeof(*is), GFP_KERNEL);
+       if (!is)
+               return -ENOMEM;
+       ret = seq_open(file, &nfqnl_seq_ops);
+       if (ret < 0)
+               goto out_free;
+       seq = file->private_data;
+       seq->private = is;
+       return ret;
+out_free:
+       kfree(is);
+       return ret;
+}
+
+static const struct file_operations nfqnl_file_ops = {
+       .owner   = THIS_MODULE,
+       .open    = nfqnl_open,
+       .read    = seq_read,
+       .llseek  = seq_lseek,
+       .release = seq_release_private,
+};
+
+#endif /* PROC_FS */
+
+static int __init nfnetlink_queue_init(void)
 {
-       int status = -ENOMEM;
-       
-       if (!init)
-               goto cleanup;
+       int i, status = -ENOMEM;
+#ifdef CONFIG_PROC_FS
+       struct proc_dir_entry *proc_nfqueue;
+#endif
+
+       for (i = 0; i < INSTANCE_BUCKETS; i++)
+               INIT_HLIST_HEAD(&instance_table[i]);
 
        netlink_register_notifier(&nfqnl_rtnl_notifier);
        status = nfnetlink_subsys_register(&nfqnl_subsys);
@@ -844,28 +1104,35 @@ init_or_cleanup(int init)
                goto cleanup_netlink_notifier;
        }
 
+#ifdef CONFIG_PROC_FS
+       proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
+                                        proc_net_netfilter);
+       if (!proc_nfqueue)
+               goto cleanup_subsys;
+       proc_nfqueue->proc_fops = &nfqnl_file_ops;
+#endif
+
        register_netdevice_notifier(&nfqnl_dev_notifier);
        return status;
 
-cleanup:
-       nf_unregister_queue_handlers(nfqnl_enqueue_packet);
-       unregister_netdevice_notifier(&nfqnl_dev_notifier);
+#ifdef CONFIG_PROC_FS
+cleanup_subsys:
        nfnetlink_subsys_unregister(&nfqnl_subsys);
-       
+#endif
 cleanup_netlink_notifier:
        netlink_unregister_notifier(&nfqnl_rtnl_notifier);
        return status;
 }
 
-static int __init init(void)
-{
-       
-       return init_or_cleanup(1);
-}
-
-static void __exit fini(void)
+static void __exit nfnetlink_queue_fini(void)
 {
-       init_or_cleanup(0);
+       nf_unregister_queue_handlers(&nfqh);
+       unregister_netdevice_notifier(&nfqnl_dev_notifier);
+#ifdef CONFIG_PROC_FS
+       remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
+#endif
+       nfnetlink_subsys_unregister(&nfqnl_subsys);
+       netlink_unregister_notifier(&nfqnl_rtnl_notifier);
 }
 
 MODULE_DESCRIPTION("netfilter packet queue handler");
@@ -873,5 +1140,5 @@ MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
 MODULE_LICENSE("GPL");
 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
 
-module_init(init);
-module_exit(fini);
+module_init(nfnetlink_queue_init);
+module_exit(nfnetlink_queue_fini);