1 #include <linux/kernel.h>
2 #include <linux/init.h>
3 #include <linux/module.h>
4 #include <linux/proc_fs.h>
5 #include <linux/skbuff.h>
6 #include <linux/netfilter.h>
7 #include <linux/seq_file.h>
8 #include <linux/rcupdate.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];
20 static DEFINE_RWLOCK(queue_handler_lock);
22 /* return EBUSY when somebody else is registered, return EEXIST if the
23 * same handler is registered, return 0 in case of success. */
24 int nf_register_queue_handler(int pf, struct nf_queue_handler *qh)
31 write_lock_bh(&queue_handler_lock);
32 if (queue_handler[pf] == qh)
34 else if (queue_handler[pf])
37 queue_handler[pf] = qh;
40 write_unlock_bh(&queue_handler_lock);
44 EXPORT_SYMBOL(nf_register_queue_handler);
46 /* The caller must flush their queue before this */
47 int nf_unregister_queue_handler(int pf, struct nf_queue_handler *qh)
52 write_lock_bh(&queue_handler_lock);
53 if (queue_handler[pf] != qh) {
54 write_unlock_bh(&queue_handler_lock);
58 queue_handler[pf] = NULL;
59 write_unlock_bh(&queue_handler_lock);
63 EXPORT_SYMBOL(nf_unregister_queue_handler);
65 void nf_unregister_queue_handlers(struct nf_queue_handler *qh)
69 write_lock_bh(&queue_handler_lock);
70 for (pf = 0; pf < NPROTO; pf++) {
71 if (queue_handler[pf] == qh)
72 queue_handler[pf] = NULL;
74 write_unlock_bh(&queue_handler_lock);
76 EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
79 * Any packet that leaves via this function must come back
80 * through nf_reinject().
82 static int __nf_queue(struct sk_buff *skb,
83 struct list_head *elem,
84 int pf, unsigned int hook,
85 struct net_device *indev,
86 struct net_device *outdev,
87 int (*okfn)(struct sk_buff *),
88 unsigned int queuenum)
92 #ifdef CONFIG_BRIDGE_NETFILTER
93 struct net_device *physindev = NULL;
94 struct net_device *physoutdev = NULL;
96 struct nf_afinfo *afinfo;
98 /* QUEUE == DROP if noone is waiting, to be safe. */
99 read_lock(&queue_handler_lock);
100 if (!queue_handler[pf]) {
101 read_unlock(&queue_handler_lock);
106 afinfo = nf_get_afinfo(pf);
108 read_unlock(&queue_handler_lock);
113 info = kmalloc(sizeof(*info) + afinfo->route_key_size, GFP_ATOMIC);
116 printk(KERN_ERR "OOM queueing packet %p\n",
118 read_unlock(&queue_handler_lock);
123 *info = (struct nf_info) {
124 (struct nf_hook_ops *)elem, pf, hook, indev, outdev, okfn };
126 /* If it's going away, ignore hook. */
127 if (!try_module_get(info->elem->owner)) {
128 read_unlock(&queue_handler_lock);
133 /* Bump dev refs so they don't vanish while packet is out */
134 if (indev) dev_hold(indev);
135 if (outdev) dev_hold(outdev);
137 #ifdef CONFIG_BRIDGE_NETFILTER
138 if (skb->nf_bridge) {
139 physindev = skb->nf_bridge->physindev;
140 if (physindev) dev_hold(physindev);
141 physoutdev = skb->nf_bridge->physoutdev;
142 if (physoutdev) dev_hold(physoutdev);
145 afinfo->saveroute(skb, info);
146 status = queue_handler[pf]->outfn(skb, info, queuenum,
147 queue_handler[pf]->data);
149 read_unlock(&queue_handler_lock);
152 /* James M doesn't say fuck enough. */
153 if (indev) dev_put(indev);
154 if (outdev) dev_put(outdev);
155 #ifdef CONFIG_BRIDGE_NETFILTER
156 if (physindev) dev_put(physindev);
157 if (physoutdev) dev_put(physoutdev);
159 module_put(info->elem->owner);
169 int nf_queue(struct sk_buff *skb,
170 struct list_head *elem,
171 int pf, unsigned int hook,
172 struct net_device *indev,
173 struct net_device *outdev,
174 int (*okfn)(struct sk_buff *),
175 unsigned int queuenum)
177 struct sk_buff *segs;
179 if (!skb_is_gso(skb))
180 return __nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
185 skb->protocol = htons(ETH_P_IP);
188 skb->protocol = htons(ETH_P_IPV6);
192 segs = skb_gso_segment(skb, 0);
194 if (unlikely(IS_ERR(segs)))
198 struct sk_buff *nskb = segs->next;
201 if (!__nf_queue(segs, elem, pf, hook, indev, outdev, okfn,
209 void nf_reinject(struct sk_buff *skb, struct nf_info *info,
210 unsigned int verdict)
212 struct list_head *elem = &info->elem->list;
214 struct nf_afinfo *afinfo;
218 /* Release those devices we held, or Alexey will kill me. */
219 if (info->indev) dev_put(info->indev);
220 if (info->outdev) dev_put(info->outdev);
221 #ifdef CONFIG_BRIDGE_NETFILTER
222 if (skb->nf_bridge) {
223 if (skb->nf_bridge->physindev)
224 dev_put(skb->nf_bridge->physindev);
225 if (skb->nf_bridge->physoutdev)
226 dev_put(skb->nf_bridge->physoutdev);
230 /* Drop reference to owner of hook which queued us. */
231 module_put(info->elem->owner);
233 list_for_each_rcu(i, &nf_hooks[info->pf][info->hook]) {
238 if (i == &nf_hooks[info->pf][info->hook]) {
239 /* The module which sent it to userspace is gone. */
240 NFDEBUG("%s: module disappeared, dropping packet.\n",
245 /* Continue traversal iff userspace said ok... */
246 if (verdict == NF_REPEAT) {
251 if (verdict == NF_ACCEPT) {
252 afinfo = nf_get_afinfo(info->pf);
253 if (!afinfo || afinfo->reroute(&skb, info) < 0)
257 if (verdict == NF_ACCEPT) {
259 verdict = nf_iterate(&nf_hooks[info->pf][info->hook],
261 info->indev, info->outdev, &elem,
262 info->okfn, INT_MIN);
265 switch (verdict & NF_VERDICT_MASK) {
272 if (!__nf_queue(skb, elem, info->pf, info->hook,
273 info->indev, info->outdev, info->okfn,
274 verdict >> NF_VERDICT_BITS))
284 EXPORT_SYMBOL(nf_reinject);
286 #ifdef CONFIG_PROC_FS
287 static void *seq_start(struct seq_file *seq, loff_t *pos)
295 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
305 static void seq_stop(struct seq_file *s, void *v)
310 static int seq_show(struct seq_file *s, void *v)
314 struct nf_queue_handler *qh;
316 read_lock_bh(&queue_handler_lock);
317 qh = queue_handler[*pos];
319 ret = seq_printf(s, "%2lld NONE\n", *pos);
321 ret = seq_printf(s, "%2lld %s\n", *pos, qh->name);
322 read_unlock_bh(&queue_handler_lock);
327 static struct seq_operations nfqueue_seq_ops = {
334 static int nfqueue_open(struct inode *inode, struct file *file)
336 return seq_open(file, &nfqueue_seq_ops);
339 static const struct file_operations nfqueue_file_ops = {
340 .owner = THIS_MODULE,
341 .open = nfqueue_open,
344 .release = seq_release,
349 int __init netfilter_queue_init(void)
351 #ifdef CONFIG_PROC_FS
352 struct proc_dir_entry *pde;
354 pde = create_proc_entry("nf_queue", S_IRUGO, proc_net_netfilter);
357 pde->proc_fops = &nfqueue_file_ops;