]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - net/netfilter/nf_queue.c
[NETFILTER]: nf_queue: move list_head/skb/id to struct nf_info
[mirror_ubuntu-bionic-kernel.git] / net / netfilter / nf_queue.c
index b1f2ace96f6d2acab781ee38d34671e986f379c3..d9d3dc4ce1a332a33a03367460b1ed68e9057a8a 100644 (file)
@@ -7,6 +7,7 @@
 #include <linux/seq_file.h>
 #include <linux/rcupdate.h>
 #include <net/protocol.h>
+#include <net/netfilter/nf_queue.h>
 
 #include "nf_internals.h"
 
  * long term mutex.  The handler must provide an an outfn() to accept packets
  * for queueing and must reinject all packets it receives, no matter what.
  */
-static struct nf_queue_handler *queue_handler[NPROTO];
+static const struct nf_queue_handler *queue_handler[NPROTO];
 
-static DEFINE_RWLOCK(queue_handler_lock);
+static DEFINE_MUTEX(queue_handler_mutex);
 
 /* return EBUSY when somebody else is registered, return EEXIST if the
  * same handler is registered, return 0 in case of success. */
-int nf_register_queue_handler(int pf, struct nf_queue_handler *qh)
+int nf_register_queue_handler(int pf, const struct nf_queue_handler *qh)
 {
        int ret;
 
        if (pf >= NPROTO)
                return -EINVAL;
 
-       write_lock_bh(&queue_handler_lock);
+       mutex_lock(&queue_handler_mutex);
        if (queue_handler[pf] == qh)
                ret = -EEXIST;
        else if (queue_handler[pf])
                ret = -EBUSY;
        else {
-               queue_handler[pf] = qh;
+               rcu_assign_pointer(queue_handler[pf], qh);
                ret = 0;
        }
-       write_unlock_bh(&queue_handler_lock);
+       mutex_unlock(&queue_handler_mutex);
 
        return ret;
 }
 EXPORT_SYMBOL(nf_register_queue_handler);
 
 /* The caller must flush their queue before this */
-int nf_unregister_queue_handler(int pf)
+int nf_unregister_queue_handler(int pf, const struct nf_queue_handler *qh)
 {
        if (pf >= NPROTO)
                return -EINVAL;
 
-       write_lock_bh(&queue_handler_lock);
-       queue_handler[pf] = NULL;
-       write_unlock_bh(&queue_handler_lock);
+       mutex_lock(&queue_handler_mutex);
+       if (queue_handler[pf] != qh) {
+               mutex_unlock(&queue_handler_mutex);
+               return -EINVAL;
+       }
+
+       rcu_assign_pointer(queue_handler[pf], NULL);
+       mutex_unlock(&queue_handler_mutex);
+
+       synchronize_rcu();
 
        return 0;
 }
 EXPORT_SYMBOL(nf_unregister_queue_handler);
 
-void nf_unregister_queue_handlers(struct nf_queue_handler *qh)
+void nf_unregister_queue_handlers(const struct nf_queue_handler *qh)
 {
        int pf;
 
-       write_lock_bh(&queue_handler_lock);
+       mutex_lock(&queue_handler_mutex);
        for (pf = 0; pf < NPROTO; pf++)  {
                if (queue_handler[pf] == qh)
-                       queue_handler[pf] = NULL;
+                       rcu_assign_pointer(queue_handler[pf], NULL);
        }
-       write_unlock_bh(&queue_handler_lock);
+       mutex_unlock(&queue_handler_mutex);
+
+       synchronize_rcu();
 }
 EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
 
@@ -83,76 +93,92 @@ static int __nf_queue(struct sk_buff *skb,
                      unsigned int queuenum)
 {
        int status;
-       struct nf_info *info;
+       struct nf_queue_entry *entry;
 #ifdef CONFIG_BRIDGE_NETFILTER
        struct net_device *physindev = NULL;
        struct net_device *physoutdev = NULL;
 #endif
        struct nf_afinfo *afinfo;
+       const struct nf_queue_handler *qh;
 
        /* QUEUE == DROP if noone is waiting, to be safe. */
-       read_lock(&queue_handler_lock);
-       if (!queue_handler[pf]) {
-               read_unlock(&queue_handler_lock);
+       rcu_read_lock();
+
+       qh = rcu_dereference(queue_handler[pf]);
+       if (!qh) {
+               rcu_read_unlock();
                kfree_skb(skb);
                return 1;
        }
 
        afinfo = nf_get_afinfo(pf);
        if (!afinfo) {
-               read_unlock(&queue_handler_lock);
+               rcu_read_unlock();
                kfree_skb(skb);
                return 1;
        }
 
-       info = kmalloc(sizeof(*info) + afinfo->route_key_size, GFP_ATOMIC);
-       if (!info) {
+       entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
+       if (!entry) {
                if (net_ratelimit())
                        printk(KERN_ERR "OOM queueing packet %p\n",
                               skb);
-               read_unlock(&queue_handler_lock);
+               rcu_read_unlock();
                kfree_skb(skb);
                return 1;
        }
 
-       *info = (struct nf_info) {
-               (struct nf_hook_ops *)elem, pf, hook, indev, outdev, okfn };
+       *entry = (struct nf_queue_entry) {
+               .skb    = skb,
+               .elem   = list_entry(elem, struct nf_hook_ops, list),
+               .pf     = pf,
+               .hook   = hook,
+               .indev  = indev,
+               .outdev = outdev,
+               .okfn   = okfn,
+       };
 
        /* If it's going away, ignore hook. */
-       if (!try_module_get(info->elem->owner)) {
-               read_unlock(&queue_handler_lock);
-               kfree(info);
+       if (!try_module_get(entry->elem->owner)) {
+               rcu_read_unlock();
+               kfree(entry);
                return 0;
        }
 
        /* Bump dev refs so they don't vanish while packet is out */
-       if (indev) dev_hold(indev);
-       if (outdev) dev_hold(outdev);
-
+       if (indev)
+               dev_hold(indev);
+       if (outdev)
+               dev_hold(outdev);
 #ifdef CONFIG_BRIDGE_NETFILTER
        if (skb->nf_bridge) {
                physindev = skb->nf_bridge->physindev;
-               if (physindev) dev_hold(physindev);
+               if (physindev)
+                       dev_hold(physindev);
                physoutdev = skb->nf_bridge->physoutdev;
-               if (physoutdev) dev_hold(physoutdev);
+               if (physoutdev)
+                       dev_hold(physoutdev);
        }
 #endif
-       afinfo->saveroute(skb, info);
-       status = queue_handler[pf]->outfn(skb, info, queuenum,
-                                         queue_handler[pf]->data);
+       afinfo->saveroute(skb, entry);
+       status = qh->outfn(entry, queuenum);
 
-       read_unlock(&queue_handler_lock);
+       rcu_read_unlock();
 
        if (status < 0) {
                /* James M doesn't say fuck enough. */
-               if (indev) dev_put(indev);
-               if (outdev) dev_put(outdev);
+               if (indev)
+                       dev_put(indev);
+               if (outdev)
+                       dev_put(outdev);
 #ifdef CONFIG_BRIDGE_NETFILTER
-               if (physindev) dev_put(physindev);
-               if (physoutdev) dev_put(physoutdev);
+               if (physindev)
+                       dev_put(physindev);
+               if (physoutdev)
+                       dev_put(physoutdev);
 #endif
-               module_put(info->elem->owner);
-               kfree(info);
+               module_put(entry->elem->owner);
+               kfree(entry);
                kfree_skb(skb);
 
                return 1;
@@ -201,18 +227,19 @@ int nf_queue(struct sk_buff *skb,
        return 1;
 }
 
-void nf_reinject(struct sk_buff *skb, struct nf_info *info,
-                unsigned int verdict)
+void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
 {
-       struct list_head *elem = &info->elem->list;
-       struct list_head *i;
+       struct sk_buff *skb = entry->skb;
+       struct list_head *elem = &entry->elem->list;
        struct nf_afinfo *afinfo;
 
        rcu_read_lock();
 
        /* Release those devices we held, or Alexey will kill me. */
-       if (info->indev) dev_put(info->indev);
-       if (info->outdev) dev_put(info->outdev);
+       if (entry->indev)
+               dev_put(entry->indev);
+       if (entry->outdev)
+               dev_put(entry->outdev);
 #ifdef CONFIG_BRIDGE_NETFILTER
        if (skb->nf_bridge) {
                if (skb->nf_bridge->physindev)
@@ -223,19 +250,7 @@ void nf_reinject(struct sk_buff *skb, struct nf_info *info,
 #endif
 
        /* Drop reference to owner of hook which queued us. */
-       module_put(info->elem->owner);
-
-       list_for_each_rcu(i, &nf_hooks[info->pf][info->hook]) {
-               if (i == elem)
-                       break;
-       }
-
-       if (i == &nf_hooks[info->pf][info->hook]) {
-               /* The module which sent it to userspace is gone. */
-               NFDEBUG("%s: module disappeared, dropping packet.\n",
-                       __FUNCTION__);
-               verdict = NF_DROP;
-       }
+       module_put(entry->elem->owner);
 
        /* Continue traversal iff userspace said ok... */
        if (verdict == NF_REPEAT) {
@@ -244,28 +259,28 @@ void nf_reinject(struct sk_buff *skb, struct nf_info *info,
        }
 
        if (verdict == NF_ACCEPT) {
-               afinfo = nf_get_afinfo(info->pf);
-               if (!afinfo || afinfo->reroute(&skb, info) < 0)
+               afinfo = nf_get_afinfo(entry->pf);
+               if (!afinfo || afinfo->reroute(skb, entry) < 0)
                        verdict = NF_DROP;
        }
 
        if (verdict == NF_ACCEPT) {
        next_hook:
-               verdict = nf_iterate(&nf_hooks[info->pf][info->hook],
-                                    &skb, info->hook,
-                                    info->indev, info->outdev, &elem,
-                                    info->okfn, INT_MIN);
+               verdict = nf_iterate(&nf_hooks[entry->pf][entry->hook],
+                                    skb, entry->hook,
+                                    entry->indev, entry->outdev, &elem,
+                                    entry->okfn, INT_MIN);
        }
 
        switch (verdict & NF_VERDICT_MASK) {
        case NF_ACCEPT:
        case NF_STOP:
-               info->okfn(skb);
+               entry->okfn(skb);
        case NF_STOLEN:
                break;
        case NF_QUEUE:
-               if (!__nf_queue(skb, elem, info->pf, info->hook,
-                               info->indev, info->outdev, info->okfn,
+               if (!__nf_queue(skb, elem, entry->pf, entry->hook,
+                               entry->indev, entry->outdev, entry->okfn,
                                verdict >> NF_VERDICT_BITS))
                        goto next_hook;
                break;
@@ -273,7 +288,7 @@ void nf_reinject(struct sk_buff *skb, struct nf_info *info,
                kfree_skb(skb);
        }
        rcu_read_unlock();
-       kfree(info);
+       kfree(entry);
        return;
 }
 EXPORT_SYMBOL(nf_reinject);
@@ -306,20 +321,20 @@ static int seq_show(struct seq_file *s, void *v)
 {
        int ret;
        loff_t *pos = v;
-       struct nf_queue_handler *qh;
+       const struct nf_queue_handler *qh;
 
-       read_lock_bh(&queue_handler_lock);
-       qh = queue_handler[*pos];
+       rcu_read_lock();
+       qh = rcu_dereference(queue_handler[*pos]);
        if (!qh)
                ret = seq_printf(s, "%2lld NONE\n", *pos);
        else
                ret = seq_printf(s, "%2lld %s\n", *pos, qh->name);
-       read_unlock_bh(&queue_handler_lock);
+       rcu_read_unlock();
 
        return ret;
 }
 
-static struct seq_operations nfqueue_seq_ops = {
+static const struct seq_operations nfqueue_seq_ops = {
        .start  = seq_start,
        .next   = seq_next,
        .stop   = seq_stop,