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>
10 #include <net/netfilter/nf_queue.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 const struct nf_queue_handler *queue_handler[NPROTO];
21 static DEFINE_MUTEX(queue_handler_mutex);
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, const struct nf_queue_handler *qh)
32 mutex_lock(&queue_handler_mutex);
33 if (queue_handler[pf] == qh)
35 else if (queue_handler[pf])
38 rcu_assign_pointer(queue_handler[pf], qh);
41 mutex_unlock(&queue_handler_mutex);
45 EXPORT_SYMBOL(nf_register_queue_handler);
47 /* The caller must flush their queue before this */
48 int nf_unregister_queue_handler(int pf, const struct nf_queue_handler *qh)
53 mutex_lock(&queue_handler_mutex);
54 if (queue_handler[pf] && queue_handler[pf] != qh) {
55 mutex_unlock(&queue_handler_mutex);
59 rcu_assign_pointer(queue_handler[pf], NULL);
60 mutex_unlock(&queue_handler_mutex);
66 EXPORT_SYMBOL(nf_unregister_queue_handler);
68 void nf_unregister_queue_handlers(const struct nf_queue_handler *qh)
72 mutex_lock(&queue_handler_mutex);
73 for (pf = 0; pf < NPROTO; pf++) {
74 if (queue_handler[pf] == qh)
75 rcu_assign_pointer(queue_handler[pf], NULL);
77 mutex_unlock(&queue_handler_mutex);
81 EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
83 static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
85 /* Release those devices we held, or Alexey will kill me. */
87 dev_put(entry->indev);
89 dev_put(entry->outdev);
90 #ifdef CONFIG_BRIDGE_NETFILTER
91 if (entry->skb->nf_bridge) {
92 struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge;
94 if (nf_bridge->physindev)
95 dev_put(nf_bridge->physindev);
96 if (nf_bridge->physoutdev)
97 dev_put(nf_bridge->physoutdev);
100 /* Drop reference to owner of hook which queued us. */
101 module_put(entry->elem->owner);
105 * Any packet that leaves via this function must come back
106 * through nf_reinject().
108 static int __nf_queue(struct sk_buff *skb,
109 struct list_head *elem,
110 int pf, unsigned int hook,
111 struct net_device *indev,
112 struct net_device *outdev,
113 int (*okfn)(struct sk_buff *),
114 unsigned int queuenum)
117 struct nf_queue_entry *entry = NULL;
118 #ifdef CONFIG_BRIDGE_NETFILTER
119 struct net_device *physindev;
120 struct net_device *physoutdev;
122 const struct nf_afinfo *afinfo;
123 const struct nf_queue_handler *qh;
125 /* QUEUE == DROP if noone is waiting, to be safe. */
128 qh = rcu_dereference(queue_handler[pf]);
132 afinfo = nf_get_afinfo(pf);
136 entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
140 *entry = (struct nf_queue_entry) {
142 .elem = list_entry(elem, struct nf_hook_ops, list),
150 /* If it's going away, ignore hook. */
151 if (!try_module_get(entry->elem->owner)) {
157 /* Bump dev refs so they don't vanish while packet is out */
162 #ifdef CONFIG_BRIDGE_NETFILTER
163 if (skb->nf_bridge) {
164 physindev = skb->nf_bridge->physindev;
167 physoutdev = skb->nf_bridge->physoutdev;
169 dev_hold(physoutdev);
172 afinfo->saveroute(skb, entry);
173 status = qh->outfn(entry, queuenum);
178 nf_queue_entry_release_refs(entry);
192 int nf_queue(struct sk_buff *skb,
193 struct list_head *elem,
194 int pf, unsigned int hook,
195 struct net_device *indev,
196 struct net_device *outdev,
197 int (*okfn)(struct sk_buff *),
198 unsigned int queuenum)
200 struct sk_buff *segs;
202 if (!skb_is_gso(skb))
203 return __nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
208 skb->protocol = htons(ETH_P_IP);
211 skb->protocol = htons(ETH_P_IPV6);
215 segs = skb_gso_segment(skb, 0);
221 struct sk_buff *nskb = segs->next;
224 if (!__nf_queue(segs, elem, pf, hook, indev, outdev, okfn,
232 void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
234 struct sk_buff *skb = entry->skb;
235 struct list_head *elem = &entry->elem->list;
236 const struct nf_afinfo *afinfo;
240 nf_queue_entry_release_refs(entry);
242 /* Continue traversal iff userspace said ok... */
243 if (verdict == NF_REPEAT) {
248 if (verdict == NF_ACCEPT) {
249 afinfo = nf_get_afinfo(entry->pf);
250 if (!afinfo || afinfo->reroute(skb, entry) < 0)
254 if (verdict == NF_ACCEPT) {
256 verdict = nf_iterate(&nf_hooks[entry->pf][entry->hook],
258 entry->indev, entry->outdev, &elem,
259 entry->okfn, INT_MIN);
262 switch (verdict & NF_VERDICT_MASK) {
271 if (!__nf_queue(skb, elem, entry->pf, entry->hook,
272 entry->indev, entry->outdev, entry->okfn,
273 verdict >> NF_VERDICT_BITS))
283 EXPORT_SYMBOL(nf_reinject);
285 #ifdef CONFIG_PROC_FS
286 static void *seq_start(struct seq_file *seq, loff_t *pos)
294 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
304 static void seq_stop(struct seq_file *s, void *v)
309 static int seq_show(struct seq_file *s, void *v)
313 const struct nf_queue_handler *qh;
316 qh = rcu_dereference(queue_handler[*pos]);
318 ret = seq_printf(s, "%2lld NONE\n", *pos);
320 ret = seq_printf(s, "%2lld %s\n", *pos, qh->name);
326 static const struct seq_operations nfqueue_seq_ops = {
333 static int nfqueue_open(struct inode *inode, struct file *file)
335 return seq_open(file, &nfqueue_seq_ops);
338 static const struct file_operations nfqueue_file_ops = {
339 .owner = THIS_MODULE,
340 .open = nfqueue_open,
343 .release = seq_release,
348 int __init netfilter_queue_init(void)
350 #ifdef CONFIG_PROC_FS
351 if (!proc_create("nf_queue", S_IRUGO,
352 proc_net_netfilter, &nfqueue_file_ops))