powerpc/powernv: Report size of OPAL memcons log
[linux/fpc-iii.git] / net / netfilter / nf_queue.c
blob4a7662486f44bae527ed83e7e952c4d7195f3871
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.
30 /* return EBUSY when somebody else is registered, return EEXIST if the
31 * same handler is registered, return 0 in case of success. */
32 void nf_register_queue_handler(struct net *net, const struct nf_queue_handler *qh)
34 /* should never happen, we only have one queueing backend in kernel */
35 WARN_ON(rcu_access_pointer(net->nf.queue_handler));
36 rcu_assign_pointer(net->nf.queue_handler, qh);
38 EXPORT_SYMBOL(nf_register_queue_handler);
40 /* The caller must flush their queue before this */
41 void nf_unregister_queue_handler(struct net *net)
43 RCU_INIT_POINTER(net->nf.queue_handler, NULL);
45 EXPORT_SYMBOL(nf_unregister_queue_handler);
47 void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
49 struct nf_hook_state *state = &entry->state;
51 /* Release those devices we held, or Alexey will kill me. */
52 if (state->in)
53 dev_put(state->in);
54 if (state->out)
55 dev_put(state->out);
56 if (state->sk)
57 sock_put(state->sk);
58 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
59 if (entry->skb->nf_bridge) {
60 struct net_device *physdev;
62 physdev = nf_bridge_get_physindev(entry->skb);
63 if (physdev)
64 dev_put(physdev);
65 physdev = nf_bridge_get_physoutdev(entry->skb);
66 if (physdev)
67 dev_put(physdev);
69 #endif
71 EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
73 /* Bump dev refs so they don't vanish while packet is out */
74 void nf_queue_entry_get_refs(struct nf_queue_entry *entry)
76 struct nf_hook_state *state = &entry->state;
78 if (state->in)
79 dev_hold(state->in);
80 if (state->out)
81 dev_hold(state->out);
82 if (state->sk)
83 sock_hold(state->sk);
84 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
85 if (entry->skb->nf_bridge) {
86 struct net_device *physdev;
88 physdev = nf_bridge_get_physindev(entry->skb);
89 if (physdev)
90 dev_hold(physdev);
91 physdev = nf_bridge_get_physoutdev(entry->skb);
92 if (physdev)
93 dev_hold(physdev);
95 #endif
97 EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
99 void nf_queue_nf_hook_drop(struct net *net, const struct nf_hook_entry *entry)
101 const struct nf_queue_handler *qh;
103 rcu_read_lock();
104 qh = rcu_dereference(net->nf.queue_handler);
105 if (qh)
106 qh->nf_hook_drop(net, entry);
107 rcu_read_unlock();
110 static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
111 struct nf_hook_entry *hook_entry, unsigned int queuenum)
113 int status = -ENOENT;
114 struct nf_queue_entry *entry = NULL;
115 const struct nf_afinfo *afinfo;
116 const struct nf_queue_handler *qh;
117 struct net *net = state->net;
119 /* QUEUE == DROP if no one is waiting, to be safe. */
120 qh = rcu_dereference(net->nf.queue_handler);
121 if (!qh) {
122 status = -ESRCH;
123 goto err;
126 afinfo = nf_get_afinfo(state->pf);
127 if (!afinfo)
128 goto err;
130 entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
131 if (!entry) {
132 status = -ENOMEM;
133 goto err;
136 *entry = (struct nf_queue_entry) {
137 .skb = skb,
138 .state = *state,
139 .hook = hook_entry,
140 .size = sizeof(*entry) + afinfo->route_key_size,
143 nf_queue_entry_get_refs(entry);
144 skb_dst_force(skb);
145 afinfo->saveroute(skb, entry);
146 status = qh->outfn(entry, queuenum);
148 if (status < 0) {
149 nf_queue_entry_release_refs(entry);
150 goto err;
153 return 0;
155 err:
156 kfree(entry);
157 return status;
160 /* Packets leaving via this function must come back through nf_reinject(). */
161 int nf_queue(struct sk_buff *skb, struct nf_hook_state *state,
162 struct nf_hook_entry **entryp, unsigned int verdict)
164 struct nf_hook_entry *entry = *entryp;
165 int ret;
167 ret = __nf_queue(skb, state, entry, verdict >> NF_VERDICT_QBITS);
168 if (ret < 0) {
169 if (ret == -ESRCH &&
170 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS)) {
171 *entryp = rcu_dereference(entry->next);
172 return 1;
174 kfree_skb(skb);
177 return 0;
180 static unsigned int nf_iterate(struct sk_buff *skb,
181 struct nf_hook_state *state,
182 struct nf_hook_entry **entryp)
184 unsigned int verdict;
186 do {
187 repeat:
188 verdict = nf_hook_entry_hookfn((*entryp), skb, state);
189 if (verdict != NF_ACCEPT) {
190 if (verdict != NF_REPEAT)
191 return verdict;
192 goto repeat;
194 *entryp = rcu_dereference((*entryp)->next);
195 } while (*entryp);
197 return NF_ACCEPT;
200 void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
202 struct nf_hook_entry *hook_entry = entry->hook;
203 struct sk_buff *skb = entry->skb;
204 const struct nf_afinfo *afinfo;
205 int err;
207 nf_queue_entry_release_refs(entry);
209 /* Continue traversal iff userspace said ok... */
210 if (verdict == NF_REPEAT)
211 verdict = nf_hook_entry_hookfn(hook_entry, skb, &entry->state);
213 if (verdict == NF_ACCEPT) {
214 afinfo = nf_get_afinfo(entry->state.pf);
215 if (!afinfo || afinfo->reroute(entry->state.net, skb, entry) < 0)
216 verdict = NF_DROP;
219 if (verdict == NF_ACCEPT) {
220 hook_entry = rcu_dereference(hook_entry->next);
221 if (hook_entry)
222 next_hook:
223 verdict = nf_iterate(skb, &entry->state, &hook_entry);
226 switch (verdict & NF_VERDICT_MASK) {
227 case NF_ACCEPT:
228 case NF_STOP:
229 okfn:
230 local_bh_disable();
231 entry->state.okfn(entry->state.net, entry->state.sk, skb);
232 local_bh_enable();
233 break;
234 case NF_QUEUE:
235 err = nf_queue(skb, &entry->state, &hook_entry, verdict);
236 if (err == 1) {
237 if (hook_entry)
238 goto next_hook;
239 goto okfn;
241 break;
242 case NF_STOLEN:
243 break;
244 default:
245 kfree_skb(skb);
248 kfree(entry);
250 EXPORT_SYMBOL(nf_reinject);