media: vicodec: add V4L2_CID_MIN_BUFFERS_FOR_OUTPUT
[linux/fpc-iii.git] / net / netfilter / nf_queue.c
blob9dc1d6e04946f59ea34377a14834bd711c4cd092
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_ipv4.h>
14 #include <linux/netfilter_ipv6.h>
15 #include <linux/netfilter_bridge.h>
16 #include <linux/seq_file.h>
17 #include <linux/rcupdate.h>
18 #include <net/protocol.h>
19 #include <net/netfilter/nf_queue.h>
20 #include <net/dst.h>
22 #include "nf_internals.h"
25 * Hook for nfnetlink_queue to register its queue handler.
26 * We do this so that most of the NFQUEUE code can be modular.
28 * Once the queue is registered it must reinject all packets it
29 * receives, no matter what.
32 /* return EBUSY when somebody else is registered, return EEXIST if the
33 * same handler is registered, return 0 in case of success. */
34 void nf_register_queue_handler(struct net *net, const struct nf_queue_handler *qh)
36 /* should never happen, we only have one queueing backend in kernel */
37 WARN_ON(rcu_access_pointer(net->nf.queue_handler));
38 rcu_assign_pointer(net->nf.queue_handler, qh);
40 EXPORT_SYMBOL(nf_register_queue_handler);
42 /* The caller must flush their queue before this */
43 void nf_unregister_queue_handler(struct net *net)
45 RCU_INIT_POINTER(net->nf.queue_handler, NULL);
47 EXPORT_SYMBOL(nf_unregister_queue_handler);
49 static void nf_queue_entry_release_br_nf_refs(struct sk_buff *skb)
51 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
52 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
54 if (nf_bridge) {
55 struct net_device *physdev;
57 physdev = nf_bridge_get_physindev(skb);
58 if (physdev)
59 dev_put(physdev);
60 physdev = nf_bridge_get_physoutdev(skb);
61 if (physdev)
62 dev_put(physdev);
64 #endif
67 void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
69 struct nf_hook_state *state = &entry->state;
71 /* Release those devices we held, or Alexey will kill me. */
72 if (state->in)
73 dev_put(state->in);
74 if (state->out)
75 dev_put(state->out);
76 if (state->sk)
77 sock_put(state->sk);
79 nf_queue_entry_release_br_nf_refs(entry->skb);
81 EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
83 static void nf_queue_entry_get_br_nf_refs(struct sk_buff *skb)
85 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
86 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
88 if (nf_bridge) {
89 struct net_device *physdev;
91 physdev = nf_bridge_get_physindev(skb);
92 if (physdev)
93 dev_hold(physdev);
94 physdev = nf_bridge_get_physoutdev(skb);
95 if (physdev)
96 dev_hold(physdev);
98 #endif
101 /* Bump dev refs so they don't vanish while packet is out */
102 void nf_queue_entry_get_refs(struct nf_queue_entry *entry)
104 struct nf_hook_state *state = &entry->state;
106 if (state->in)
107 dev_hold(state->in);
108 if (state->out)
109 dev_hold(state->out);
110 if (state->sk)
111 sock_hold(state->sk);
113 nf_queue_entry_get_br_nf_refs(entry->skb);
115 EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
117 void nf_queue_nf_hook_drop(struct net *net)
119 const struct nf_queue_handler *qh;
121 rcu_read_lock();
122 qh = rcu_dereference(net->nf.queue_handler);
123 if (qh)
124 qh->nf_hook_drop(net);
125 rcu_read_unlock();
127 EXPORT_SYMBOL_GPL(nf_queue_nf_hook_drop);
129 static void nf_ip_saveroute(const struct sk_buff *skb,
130 struct nf_queue_entry *entry)
132 struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
134 if (entry->state.hook == NF_INET_LOCAL_OUT) {
135 const struct iphdr *iph = ip_hdr(skb);
137 rt_info->tos = iph->tos;
138 rt_info->daddr = iph->daddr;
139 rt_info->saddr = iph->saddr;
140 rt_info->mark = skb->mark;
144 static void nf_ip6_saveroute(const struct sk_buff *skb,
145 struct nf_queue_entry *entry)
147 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
149 if (entry->state.hook == NF_INET_LOCAL_OUT) {
150 const struct ipv6hdr *iph = ipv6_hdr(skb);
152 rt_info->daddr = iph->daddr;
153 rt_info->saddr = iph->saddr;
154 rt_info->mark = skb->mark;
158 static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
159 const struct nf_hook_entries *entries,
160 unsigned int index, unsigned int queuenum)
162 int status = -ENOENT;
163 struct nf_queue_entry *entry = NULL;
164 const struct nf_queue_handler *qh;
165 struct net *net = state->net;
166 unsigned int route_key_size;
168 /* QUEUE == DROP if no one is waiting, to be safe. */
169 qh = rcu_dereference(net->nf.queue_handler);
170 if (!qh) {
171 status = -ESRCH;
172 goto err;
175 switch (state->pf) {
176 case AF_INET:
177 route_key_size = sizeof(struct ip_rt_info);
178 break;
179 case AF_INET6:
180 route_key_size = sizeof(struct ip6_rt_info);
181 break;
182 default:
183 route_key_size = 0;
184 break;
187 entry = kmalloc(sizeof(*entry) + route_key_size, GFP_ATOMIC);
188 if (!entry) {
189 status = -ENOMEM;
190 goto err;
193 *entry = (struct nf_queue_entry) {
194 .skb = skb,
195 .state = *state,
196 .hook_index = index,
197 .size = sizeof(*entry) + route_key_size,
200 nf_queue_entry_get_refs(entry);
201 skb_dst_force(skb);
203 switch (entry->state.pf) {
204 case AF_INET:
205 nf_ip_saveroute(skb, entry);
206 break;
207 case AF_INET6:
208 nf_ip6_saveroute(skb, entry);
209 break;
212 status = qh->outfn(entry, queuenum);
214 if (status < 0) {
215 nf_queue_entry_release_refs(entry);
216 goto err;
219 return 0;
221 err:
222 kfree(entry);
223 return status;
226 /* Packets leaving via this function must come back through nf_reinject(). */
227 int nf_queue(struct sk_buff *skb, struct nf_hook_state *state,
228 const struct nf_hook_entries *entries, unsigned int index,
229 unsigned int verdict)
231 int ret;
233 ret = __nf_queue(skb, state, entries, index, verdict >> NF_VERDICT_QBITS);
234 if (ret < 0) {
235 if (ret == -ESRCH &&
236 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
237 return 1;
238 kfree_skb(skb);
241 return 0;
243 EXPORT_SYMBOL_GPL(nf_queue);
245 static unsigned int nf_iterate(struct sk_buff *skb,
246 struct nf_hook_state *state,
247 const struct nf_hook_entries *hooks,
248 unsigned int *index)
250 const struct nf_hook_entry *hook;
251 unsigned int verdict, i = *index;
253 while (i < hooks->num_hook_entries) {
254 hook = &hooks->hooks[i];
255 repeat:
256 verdict = nf_hook_entry_hookfn(hook, skb, state);
257 if (verdict != NF_ACCEPT) {
258 if (verdict != NF_REPEAT)
259 return verdict;
260 goto repeat;
262 i++;
265 *index = i;
266 return NF_ACCEPT;
269 static struct nf_hook_entries *nf_hook_entries_head(const struct net *net, u8 pf, u8 hooknum)
271 switch (pf) {
272 #ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
273 case NFPROTO_BRIDGE:
274 return rcu_dereference(net->nf.hooks_bridge[hooknum]);
275 #endif
276 case NFPROTO_IPV4:
277 return rcu_dereference(net->nf.hooks_ipv4[hooknum]);
278 case NFPROTO_IPV6:
279 return rcu_dereference(net->nf.hooks_ipv6[hooknum]);
280 default:
281 WARN_ON_ONCE(1);
282 return NULL;
285 return NULL;
288 /* Caller must hold rcu read-side lock */
289 void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
291 const struct nf_hook_entry *hook_entry;
292 const struct nf_hook_entries *hooks;
293 struct sk_buff *skb = entry->skb;
294 const struct net *net;
295 unsigned int i;
296 int err;
297 u8 pf;
299 net = entry->state.net;
300 pf = entry->state.pf;
302 hooks = nf_hook_entries_head(net, pf, entry->state.hook);
304 nf_queue_entry_release_refs(entry);
306 i = entry->hook_index;
307 if (WARN_ON_ONCE(!hooks || i >= hooks->num_hook_entries)) {
308 kfree_skb(skb);
309 kfree(entry);
310 return;
313 hook_entry = &hooks->hooks[i];
315 /* Continue traversal iff userspace said ok... */
316 if (verdict == NF_REPEAT)
317 verdict = nf_hook_entry_hookfn(hook_entry, skb, &entry->state);
319 if (verdict == NF_ACCEPT) {
320 if (nf_reroute(skb, entry) < 0)
321 verdict = NF_DROP;
324 if (verdict == NF_ACCEPT) {
325 next_hook:
326 ++i;
327 verdict = nf_iterate(skb, &entry->state, hooks, &i);
330 switch (verdict & NF_VERDICT_MASK) {
331 case NF_ACCEPT:
332 case NF_STOP:
333 local_bh_disable();
334 entry->state.okfn(entry->state.net, entry->state.sk, skb);
335 local_bh_enable();
336 break;
337 case NF_QUEUE:
338 err = nf_queue(skb, &entry->state, hooks, i, verdict);
339 if (err == 1)
340 goto next_hook;
341 break;
342 case NF_STOLEN:
343 break;
344 default:
345 kfree_skb(skb);
348 kfree(entry);
350 EXPORT_SYMBOL(nf_reinject);