1 #include <linux/config.h>
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/proc_fs.h>
6 #include <linux/skbuff.h>
7 #include <linux/netfilter.h>
8 #include <linux/seq_file.h>
9 #include <net/protocol.h>
11 #include "nf_internals.h"
14 * A queue handler may be registered for each protocol. Each is protected by
15 * long term mutex. The handler must provide an an outfn() to accept packets
16 * for queueing and must reinject all packets it receives, no matter what.
18 static struct nf_queue_handler *queue_handler[NPROTO];
19 static struct nf_queue_rerouter *queue_rerouter;
21 static DEFINE_RWLOCK(queue_handler_lock);
23 int nf_register_queue_handler(int pf, struct nf_queue_handler *qh)
30 write_lock_bh(&queue_handler_lock);
31 if (queue_handler[pf])
34 queue_handler[pf] = qh;
37 write_unlock_bh(&queue_handler_lock);
41 EXPORT_SYMBOL(nf_register_queue_handler);
43 /* The caller must flush their queue before this */
44 int nf_unregister_queue_handler(int pf)
49 write_lock_bh(&queue_handler_lock);
50 queue_handler[pf] = NULL;
51 write_unlock_bh(&queue_handler_lock);
55 EXPORT_SYMBOL(nf_unregister_queue_handler);
57 int nf_register_queue_rerouter(int pf, struct nf_queue_rerouter *rer)
62 write_lock_bh(&queue_handler_lock);
63 memcpy(&queue_rerouter[pf], rer, sizeof(queue_rerouter[pf]));
64 write_unlock_bh(&queue_handler_lock);
68 EXPORT_SYMBOL_GPL(nf_register_queue_rerouter);
70 int nf_unregister_queue_rerouter(int pf)
75 write_lock_bh(&queue_handler_lock);
76 memset(&queue_rerouter[pf], 0, sizeof(queue_rerouter[pf]));
77 write_unlock_bh(&queue_handler_lock);
80 EXPORT_SYMBOL_GPL(nf_unregister_queue_rerouter);
82 void nf_unregister_queue_handlers(struct nf_queue_handler *qh)
86 write_lock_bh(&queue_handler_lock);
87 for (pf = 0; pf < NPROTO; pf++) {
88 if (queue_handler[pf] == qh)
89 queue_handler[pf] = NULL;
91 write_unlock_bh(&queue_handler_lock);
93 EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
96 * Any packet that leaves via this function must come back
97 * through nf_reinject().
99 int nf_queue(struct sk_buff **skb,
100 struct list_head *elem,
101 int pf, unsigned int hook,
102 struct net_device *indev,
103 struct net_device *outdev,
104 int (*okfn)(struct sk_buff *),
105 unsigned int queuenum)
108 struct nf_info *info;
109 #ifdef CONFIG_BRIDGE_NETFILTER
110 struct net_device *physindev = NULL;
111 struct net_device *physoutdev = NULL;
114 /* QUEUE == DROP if noone is waiting, to be safe. */
115 read_lock(&queue_handler_lock);
116 if (!queue_handler[pf]->outfn) {
117 read_unlock(&queue_handler_lock);
122 info = kmalloc(sizeof(*info)+queue_rerouter[pf].rer_size, GFP_ATOMIC);
125 printk(KERN_ERR "OOM queueing packet %p\n",
127 read_unlock(&queue_handler_lock);
132 *info = (struct nf_info) {
133 (struct nf_hook_ops *)elem, pf, hook, indev, outdev, okfn };
135 /* If it's going away, ignore hook. */
136 if (!try_module_get(info->elem->owner)) {
137 read_unlock(&queue_handler_lock);
142 /* Bump dev refs so they don't vanish while packet is out */
143 if (indev) dev_hold(indev);
144 if (outdev) dev_hold(outdev);
146 #ifdef CONFIG_BRIDGE_NETFILTER
147 if ((*skb)->nf_bridge) {
148 physindev = (*skb)->nf_bridge->physindev;
149 if (physindev) dev_hold(physindev);
150 physoutdev = (*skb)->nf_bridge->physoutdev;
151 if (physoutdev) dev_hold(physoutdev);
154 if (queue_rerouter[pf].save)
155 queue_rerouter[pf].save(*skb, info);
157 status = queue_handler[pf]->outfn(*skb, info, queuenum,
158 queue_handler[pf]->data);
160 if (status >= 0 && queue_rerouter[pf].reroute)
161 status = queue_rerouter[pf].reroute(skb, info);
163 read_unlock(&queue_handler_lock);
166 /* James M doesn't say fuck enough. */
167 if (indev) dev_put(indev);
168 if (outdev) dev_put(outdev);
169 #ifdef CONFIG_BRIDGE_NETFILTER
170 if (physindev) dev_put(physindev);
171 if (physoutdev) dev_put(physoutdev);
173 module_put(info->elem->owner);
183 void nf_reinject(struct sk_buff *skb, struct nf_info *info,
184 unsigned int verdict)
186 struct list_head *elem = &info->elem->list;
191 /* Release those devices we held, or Alexey will kill me. */
192 if (info->indev) dev_put(info->indev);
193 if (info->outdev) dev_put(info->outdev);
194 #ifdef CONFIG_BRIDGE_NETFILTER
195 if (skb->nf_bridge) {
196 if (skb->nf_bridge->physindev)
197 dev_put(skb->nf_bridge->physindev);
198 if (skb->nf_bridge->physoutdev)
199 dev_put(skb->nf_bridge->physoutdev);
203 /* Drop reference to owner of hook which queued us. */
204 module_put(info->elem->owner);
206 list_for_each_rcu(i, &nf_hooks[info->pf][info->hook]) {
211 if (elem == &nf_hooks[info->pf][info->hook]) {
212 /* The module which sent it to userspace is gone. */
213 NFDEBUG("%s: module disappeared, dropping packet.\n",
218 /* Continue traversal iff userspace said ok... */
219 if (verdict == NF_REPEAT) {
224 if (verdict == NF_ACCEPT) {
226 verdict = nf_iterate(&nf_hooks[info->pf][info->hook],
228 info->indev, info->outdev, &elem,
229 info->okfn, INT_MIN);
232 switch (verdict & NF_VERDICT_MASK) {
238 if (!nf_queue(&skb, elem, info->pf, info->hook,
239 info->indev, info->outdev, info->okfn,
240 verdict >> NF_VERDICT_BITS))
246 if (verdict == NF_DROP)
252 EXPORT_SYMBOL(nf_reinject);
254 #ifdef CONFIG_PROC_FS
255 static void *seq_start(struct seq_file *seq, loff_t *pos)
263 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
273 static void seq_stop(struct seq_file *s, void *v)
278 static int seq_show(struct seq_file *s, void *v)
282 struct nf_queue_handler *qh;
284 read_lock_bh(&queue_handler_lock);
285 qh = queue_handler[*pos];
287 ret = seq_printf(s, "%2lld NONE\n", *pos);
289 ret = seq_printf(s, "%2lld %s\n", *pos, qh->name);
290 read_unlock_bh(&queue_handler_lock);
295 static struct seq_operations nfqueue_seq_ops = {
302 static int nfqueue_open(struct inode *inode, struct file *file)
304 return seq_open(file, &nfqueue_seq_ops);
307 static struct file_operations nfqueue_file_ops = {
308 .owner = THIS_MODULE,
309 .open = nfqueue_open,
312 .release = seq_release,
317 int __init netfilter_queue_init(void)
319 #ifdef CONFIG_PROC_FS
320 struct proc_dir_entry *pde;
322 queue_rerouter = kmalloc(NPROTO * sizeof(struct nf_queue_rerouter),
327 #ifdef CONFIG_PROC_FS
328 pde = create_proc_entry("nf_queue", S_IRUGO, proc_net_netfilter);
330 kfree(queue_rerouter);
333 pde->proc_fops = &nfqueue_file_ops;
335 memset(queue_rerouter, 0, NPROTO * sizeof(struct nf_queue_rerouter));