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 <linux/rcupdate.h>
10 #include <net/protocol.h>
12 #include "nf_internals.h"
15 * A queue handler may be registered for each protocol. Each is protected by
16 * long term mutex. The handler must provide an an outfn() to accept packets
17 * for queueing and must reinject all packets it receives, no matter what.
19 static struct nf_queue_handler *queue_handler[NPROTO];
21 static DEFINE_RWLOCK(queue_handler_lock);
23 /* return EBUSY when somebody else is registered, return EEXIST if the
24 * same handler is registered, return 0 in case of success. */
25 int nf_register_queue_handler(int pf, struct nf_queue_handler *qh)
32 write_lock_bh(&queue_handler_lock);
33 if (queue_handler[pf] == qh)
35 else if (queue_handler[pf])
38 queue_handler[pf] = qh;
41 write_unlock_bh(&queue_handler_lock);
45 EXPORT_SYMBOL(nf_register_queue_handler);
47 /* The caller must flush their queue before this */
48 int nf_unregister_queue_handler(int pf)
53 write_lock_bh(&queue_handler_lock);
54 queue_handler[pf] = NULL;
55 write_unlock_bh(&queue_handler_lock);
59 EXPORT_SYMBOL(nf_unregister_queue_handler);
61 void nf_unregister_queue_handlers(struct nf_queue_handler *qh)
65 write_lock_bh(&queue_handler_lock);
66 for (pf = 0; pf < NPROTO; pf++) {
67 if (queue_handler[pf] == qh)
68 queue_handler[pf] = NULL;
70 write_unlock_bh(&queue_handler_lock);
72 EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
75 * Any packet that leaves via this function must come back
76 * through nf_reinject().
78 int nf_queue(struct sk_buff **skb,
79 struct list_head *elem,
80 int pf, unsigned int hook,
81 struct net_device *indev,
82 struct net_device *outdev,
83 int (*okfn)(struct sk_buff *),
84 unsigned int queuenum)
88 #ifdef CONFIG_BRIDGE_NETFILTER
89 struct net_device *physindev = NULL;
90 struct net_device *physoutdev = NULL;
92 struct nf_afinfo *afinfo;
94 /* QUEUE == DROP if noone is waiting, to be safe. */
95 read_lock(&queue_handler_lock);
96 if (!queue_handler[pf]) {
97 read_unlock(&queue_handler_lock);
102 afinfo = nf_get_afinfo(pf);
104 read_unlock(&queue_handler_lock);
109 info = kmalloc(sizeof(*info) + afinfo->route_key_size, GFP_ATOMIC);
112 printk(KERN_ERR "OOM queueing packet %p\n",
114 read_unlock(&queue_handler_lock);
119 *info = (struct nf_info) {
120 (struct nf_hook_ops *)elem, pf, hook, indev, outdev, okfn };
122 /* If it's going away, ignore hook. */
123 if (!try_module_get(info->elem->owner)) {
124 read_unlock(&queue_handler_lock);
129 /* Bump dev refs so they don't vanish while packet is out */
130 if (indev) dev_hold(indev);
131 if (outdev) dev_hold(outdev);
133 #ifdef CONFIG_BRIDGE_NETFILTER
134 if ((*skb)->nf_bridge) {
135 physindev = (*skb)->nf_bridge->physindev;
136 if (physindev) dev_hold(physindev);
137 physoutdev = (*skb)->nf_bridge->physoutdev;
138 if (physoutdev) dev_hold(physoutdev);
141 afinfo->saveroute(*skb, info);
142 status = queue_handler[pf]->outfn(*skb, info, queuenum,
143 queue_handler[pf]->data);
145 read_unlock(&queue_handler_lock);
148 /* James M doesn't say fuck enough. */
149 if (indev) dev_put(indev);
150 if (outdev) dev_put(outdev);
151 #ifdef CONFIG_BRIDGE_NETFILTER
152 if (physindev) dev_put(physindev);
153 if (physoutdev) dev_put(physoutdev);
155 module_put(info->elem->owner);
165 void nf_reinject(struct sk_buff *skb, struct nf_info *info,
166 unsigned int verdict)
168 struct list_head *elem = &info->elem->list;
170 struct nf_afinfo *afinfo;
174 /* Release those devices we held, or Alexey will kill me. */
175 if (info->indev) dev_put(info->indev);
176 if (info->outdev) dev_put(info->outdev);
177 #ifdef CONFIG_BRIDGE_NETFILTER
178 if (skb->nf_bridge) {
179 if (skb->nf_bridge->physindev)
180 dev_put(skb->nf_bridge->physindev);
181 if (skb->nf_bridge->physoutdev)
182 dev_put(skb->nf_bridge->physoutdev);
186 /* Drop reference to owner of hook which queued us. */
187 module_put(info->elem->owner);
189 list_for_each_rcu(i, &nf_hooks[info->pf][info->hook]) {
194 if (i == &nf_hooks[info->pf][info->hook]) {
195 /* The module which sent it to userspace is gone. */
196 NFDEBUG("%s: module disappeared, dropping packet.\n",
201 /* Continue traversal iff userspace said ok... */
202 if (verdict == NF_REPEAT) {
207 if (verdict == NF_ACCEPT) {
208 afinfo = nf_get_afinfo(info->pf);
209 if (!afinfo || afinfo->reroute(&skb, info) < 0)
213 if (verdict == NF_ACCEPT) {
215 verdict = nf_iterate(&nf_hooks[info->pf][info->hook],
217 info->indev, info->outdev, &elem,
218 info->okfn, INT_MIN);
221 switch (verdict & NF_VERDICT_MASK) {
227 if (!nf_queue(&skb, elem, info->pf, info->hook,
228 info->indev, info->outdev, info->okfn,
229 verdict >> NF_VERDICT_BITS))
235 if (verdict == NF_DROP)
241 EXPORT_SYMBOL(nf_reinject);
243 #ifdef CONFIG_PROC_FS
244 static void *seq_start(struct seq_file *seq, loff_t *pos)
252 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
262 static void seq_stop(struct seq_file *s, void *v)
267 static int seq_show(struct seq_file *s, void *v)
271 struct nf_queue_handler *qh;
273 read_lock_bh(&queue_handler_lock);
274 qh = queue_handler[*pos];
276 ret = seq_printf(s, "%2lld NONE\n", *pos);
278 ret = seq_printf(s, "%2lld %s\n", *pos, qh->name);
279 read_unlock_bh(&queue_handler_lock);
284 static struct seq_operations nfqueue_seq_ops = {
291 static int nfqueue_open(struct inode *inode, struct file *file)
293 return seq_open(file, &nfqueue_seq_ops);
296 static struct file_operations nfqueue_file_ops = {
297 .owner = THIS_MODULE,
298 .open = nfqueue_open,
301 .release = seq_release,
306 int __init netfilter_queue_init(void)
308 #ifdef CONFIG_PROC_FS
309 struct proc_dir_entry *pde;
311 pde = create_proc_entry("nf_queue", S_IRUGO, proc_net_netfilter);
314 pde->proc_fops = &nfqueue_file_ops;