s390: add 'install' target to 'make help'
[linux/fpc-iii.git] / net / netfilter / nf_queue.c
blob5baa8e24e6ac1b512250c676cf0caf115f0b17f9
1 /*
2 * Rusty Russell (C)2000 -- This code is GPL.
3 * Patrick McHardy (c) 2006-2012
4 */
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/proc_fs.h>
11 #include <linux/skbuff.h>
12 #include <linux/netfilter.h>
13 #include <linux/netfilter_bridge.h>
14 #include <linux/seq_file.h>
15 #include <linux/rcupdate.h>
16 #include <net/protocol.h>
17 #include <net/netfilter/nf_queue.h>
18 #include <net/dst.h>
20 #include "nf_internals.h"
23 * Hook for nfnetlink_queue to register its queue handler.
24 * We do this so that most of the NFQUEUE code can be modular.
26 * Once the queue is registered it must reinject all packets it
27 * receives, no matter what.
29 static const struct nf_queue_handler __rcu *queue_handler __read_mostly;
31 /* return EBUSY when somebody else is registered, return EEXIST if the
32 * same handler is registered, return 0 in case of success. */
33 void nf_register_queue_handler(const struct nf_queue_handler *qh)
35 /* should never happen, we only have one queueing backend in kernel */
36 WARN_ON(rcu_access_pointer(queue_handler));
37 rcu_assign_pointer(queue_handler, qh);
39 EXPORT_SYMBOL(nf_register_queue_handler);
41 /* The caller must flush their queue before this */
42 void nf_unregister_queue_handler(void)
44 RCU_INIT_POINTER(queue_handler, NULL);
45 synchronize_rcu();
47 EXPORT_SYMBOL(nf_unregister_queue_handler);
49 void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
51 struct nf_hook_state *state = &entry->state;
53 /* Release those devices we held, or Alexey will kill me. */
54 if (state->in)
55 dev_put(state->in);
56 if (state->out)
57 dev_put(state->out);
58 if (state->sk)
59 sock_put(state->sk);
60 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
61 if (entry->skb->nf_bridge) {
62 struct net_device *physdev;
64 physdev = nf_bridge_get_physindev(entry->skb);
65 if (physdev)
66 dev_put(physdev);
67 physdev = nf_bridge_get_physoutdev(entry->skb);
68 if (physdev)
69 dev_put(physdev);
71 #endif
73 EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
75 /* Bump dev refs so they don't vanish while packet is out */
76 void nf_queue_entry_get_refs(struct nf_queue_entry *entry)
78 struct nf_hook_state *state = &entry->state;
80 if (state->in)
81 dev_hold(state->in);
82 if (state->out)
83 dev_hold(state->out);
84 if (state->sk)
85 sock_hold(state->sk);
86 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
87 if (entry->skb->nf_bridge) {
88 struct net_device *physdev;
90 physdev = nf_bridge_get_physindev(entry->skb);
91 if (physdev)
92 dev_hold(physdev);
93 physdev = nf_bridge_get_physoutdev(entry->skb);
94 if (physdev)
95 dev_hold(physdev);
97 #endif
99 EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
101 void nf_queue_nf_hook_drop(struct net *net, struct nf_hook_ops *ops)
103 const struct nf_queue_handler *qh;
105 rcu_read_lock();
106 qh = rcu_dereference(queue_handler);
107 if (qh)
108 qh->nf_hook_drop(net, ops);
109 rcu_read_unlock();
113 * Any packet that leaves via this function must come back
114 * through nf_reinject().
116 int nf_queue(struct sk_buff *skb,
117 struct nf_hook_ops *elem,
118 struct nf_hook_state *state,
119 unsigned int queuenum)
121 int status = -ENOENT;
122 struct nf_queue_entry *entry = NULL;
123 const struct nf_afinfo *afinfo;
124 const struct nf_queue_handler *qh;
126 /* QUEUE == DROP if no one is waiting, to be safe. */
127 qh = rcu_dereference(queue_handler);
128 if (!qh) {
129 status = -ESRCH;
130 goto err;
133 afinfo = nf_get_afinfo(state->pf);
134 if (!afinfo)
135 goto err;
137 entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
138 if (!entry) {
139 status = -ENOMEM;
140 goto err;
143 *entry = (struct nf_queue_entry) {
144 .skb = skb,
145 .elem = elem,
146 .state = *state,
147 .size = sizeof(*entry) + afinfo->route_key_size,
150 nf_queue_entry_get_refs(entry);
151 skb_dst_force(skb);
152 afinfo->saveroute(skb, entry);
153 status = qh->outfn(entry, queuenum);
155 if (status < 0) {
156 nf_queue_entry_release_refs(entry);
157 goto err;
160 return 0;
162 err:
163 kfree(entry);
164 return status;
167 void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
169 struct sk_buff *skb = entry->skb;
170 struct nf_hook_ops *elem = entry->elem;
171 const struct nf_afinfo *afinfo;
172 int err;
174 nf_queue_entry_release_refs(entry);
176 /* Continue traversal iff userspace said ok... */
177 if (verdict == NF_REPEAT)
178 verdict = elem->hook(elem->priv, skb, &entry->state);
180 if (verdict == NF_ACCEPT) {
181 afinfo = nf_get_afinfo(entry->state.pf);
182 if (!afinfo || afinfo->reroute(entry->state.net, skb, entry) < 0)
183 verdict = NF_DROP;
186 entry->state.thresh = INT_MIN;
188 if (verdict == NF_ACCEPT) {
189 next_hook:
190 verdict = nf_iterate(entry->state.hook_list,
191 skb, &entry->state, &elem);
194 switch (verdict & NF_VERDICT_MASK) {
195 case NF_ACCEPT:
196 case NF_STOP:
197 local_bh_disable();
198 entry->state.okfn(entry->state.net, entry->state.sk, skb);
199 local_bh_enable();
200 break;
201 case NF_QUEUE:
202 err = nf_queue(skb, elem, &entry->state,
203 verdict >> NF_VERDICT_QBITS);
204 if (err < 0) {
205 if (err == -ESRCH &&
206 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
207 goto next_hook;
208 kfree_skb(skb);
210 break;
211 case NF_STOLEN:
212 break;
213 default:
214 kfree_skb(skb);
217 kfree(entry);
219 EXPORT_SYMBOL(nf_reinject);