#include <linux/proc_fs.h>
#include <linux/skbuff.h>
#include <linux/netfilter.h>
+#include <linux/seq_file.h>
+#include <linux/rcupdate.h>
#include <net/protocol.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_t {
- nf_queue_outfn_t outfn;
- void *data;
-} queue_handler[NPROTO];
-
-static struct nf_queue_rerouter *queue_rerouter;
+static struct nf_queue_handler *queue_handler[NPROTO];
+static struct nf_queue_rerouter *queue_rerouter[NPROTO];
static DEFINE_RWLOCK(queue_handler_lock);
-
-int nf_register_queue_handler(int pf, nf_queue_outfn_t outfn, void *data)
+/* 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 ret;
return -EINVAL;
write_lock_bh(&queue_handler_lock);
- if (queue_handler[pf].outfn)
+ if (queue_handler[pf] == qh)
+ ret = -EEXIST;
+ else if (queue_handler[pf])
ret = -EBUSY;
else {
- queue_handler[pf].outfn = outfn;
- queue_handler[pf].data = data;
+ queue_handler[pf] = qh;
ret = 0;
}
write_unlock_bh(&queue_handler_lock);
return -EINVAL;
write_lock_bh(&queue_handler_lock);
- queue_handler[pf].outfn = NULL;
- queue_handler[pf].data = NULL;
+ queue_handler[pf] = NULL;
write_unlock_bh(&queue_handler_lock);
return 0;
return -EINVAL;
write_lock_bh(&queue_handler_lock);
- memcpy(&queue_rerouter[pf], rer, sizeof(queue_rerouter[pf]));
+ rcu_assign_pointer(queue_rerouter[pf], rer);
write_unlock_bh(&queue_handler_lock);
return 0;
return -EINVAL;
write_lock_bh(&queue_handler_lock);
- memset(&queue_rerouter[pf], 0, sizeof(queue_rerouter[pf]));
+ rcu_assign_pointer(queue_rerouter[pf], NULL);
write_unlock_bh(&queue_handler_lock);
+ synchronize_rcu();
return 0;
}
EXPORT_SYMBOL_GPL(nf_unregister_queue_rerouter);
-void nf_unregister_queue_handlers(nf_queue_outfn_t outfn)
+void nf_unregister_queue_handlers(struct nf_queue_handler *qh)
{
int pf;
write_lock_bh(&queue_handler_lock);
for (pf = 0; pf < NPROTO; pf++) {
- if (queue_handler[pf].outfn == outfn) {
- queue_handler[pf].outfn = NULL;
- queue_handler[pf].data = NULL;
- }
+ if (queue_handler[pf] == qh)
+ queue_handler[pf] = NULL;
}
write_unlock_bh(&queue_handler_lock);
}
struct net_device *physindev = NULL;
struct net_device *physoutdev = NULL;
#endif
+ struct nf_queue_rerouter *rerouter;
/* QUEUE == DROP if noone is waiting, to be safe. */
read_lock(&queue_handler_lock);
- if (!queue_handler[pf].outfn) {
+ if (!queue_handler[pf]) {
read_unlock(&queue_handler_lock);
kfree_skb(*skb);
return 1;
}
- info = kmalloc(sizeof(*info)+queue_rerouter[pf].rer_size, GFP_ATOMIC);
+ info = kmalloc(sizeof(*info)+queue_rerouter[pf]->rer_size, GFP_ATOMIC);
if (!info) {
if (net_ratelimit())
printk(KERN_ERR "OOM queueing packet %p\n",
if (physoutdev) dev_hold(physoutdev);
}
#endif
- if (queue_rerouter[pf].save)
- queue_rerouter[pf].save(*skb, info);
-
- status = queue_handler[pf].outfn(*skb, info, queuenum,
- queue_handler[pf].data);
+ rerouter = rcu_dereference(queue_rerouter[pf]);
+ if (rerouter)
+ rerouter->save(*skb, info);
- if (status >= 0 && queue_rerouter[pf].reroute)
- status = queue_rerouter[pf].reroute(skb, info);
+ status = queue_handler[pf]->outfn(*skb, info, queuenum,
+ queue_handler[pf]->data);
read_unlock(&queue_handler_lock);
{
struct list_head *elem = &info->elem->list;
struct list_head *i;
+ struct nf_queue_rerouter *rerouter;
rcu_read_lock();
break;
}
- if (elem == &nf_hooks[info->pf][info->hook]) {
+ 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_ACCEPT;
}
+ if (verdict == NF_ACCEPT) {
+ rerouter = rcu_dereference(queue_rerouter[info->pf]);
+ if (rerouter && rerouter->reroute(&skb, info) < 0)
+ verdict = NF_DROP;
+ }
+
if (verdict == NF_ACCEPT) {
next_hook:
verdict = nf_iterate(&nf_hooks[info->pf][info->hook],
}
EXPORT_SYMBOL(nf_reinject);
-int __init netfilter_queue_init(void)
+#ifdef CONFIG_PROC_FS
+static void *seq_start(struct seq_file *seq, loff_t *pos)
+{
+ if (*pos >= NPROTO)
+ return NULL;
+
+ return pos;
+}
+
+static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
+{
+ (*pos)++;
+
+ if (*pos >= NPROTO)
+ return NULL;
+
+ return pos;
+}
+
+static void seq_stop(struct seq_file *s, void *v)
+{
+
+}
+
+static int seq_show(struct seq_file *s, void *v)
{
- queue_rerouter = kmalloc(NPROTO * sizeof(struct nf_queue_rerouter),
- GFP_KERNEL);
- if (!queue_rerouter)
- return -ENOMEM;
+ int ret;
+ loff_t *pos = v;
+ struct nf_queue_handler *qh;
- memset(queue_rerouter, 0, NPROTO * sizeof(struct nf_queue_rerouter));
+ read_lock_bh(&queue_handler_lock);
+ qh = 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);
+ return ret;
+}
+
+static struct seq_operations nfqueue_seq_ops = {
+ .start = seq_start,
+ .next = seq_next,
+ .stop = seq_stop,
+ .show = seq_show,
+};
+
+static int nfqueue_open(struct inode *inode, struct file *file)
+{
+ return seq_open(file, &nfqueue_seq_ops);
+}
+
+static struct file_operations nfqueue_file_ops = {
+ .owner = THIS_MODULE,
+ .open = nfqueue_open,
+ .read = seq_read,
+ .llseek = seq_lseek,
+ .release = seq_release,
+};
+#endif /* PROC_FS */
+
+
+int __init netfilter_queue_init(void)
+{
+#ifdef CONFIG_PROC_FS
+ struct proc_dir_entry *pde;
+
+ pde = create_proc_entry("nf_queue", S_IRUGO, proc_net_netfilter);
+ if (!pde)
+ return -1;
+ pde->proc_fops = &nfqueue_file_ops;
+#endif
return 0;
}