[NETFILTER]: ip6_queue: resync dev-index based flushing
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / nf_queue.c
... / ...
CommitLineData
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>
11
12#include "nf_internals.h"
13
14/*
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.
18 */
19static const struct nf_queue_handler *queue_handler[NPROTO];
20
21static DEFINE_MUTEX(queue_handler_mutex);
22
23/* return EBUSY when somebody else is registered, return EEXIST if the
24 * same handler is registered, return 0 in case of success. */
25int nf_register_queue_handler(int pf, const struct nf_queue_handler *qh)
26{
27 int ret;
28
29 if (pf >= NPROTO)
30 return -EINVAL;
31
32 mutex_lock(&queue_handler_mutex);
33 if (queue_handler[pf] == qh)
34 ret = -EEXIST;
35 else if (queue_handler[pf])
36 ret = -EBUSY;
37 else {
38 rcu_assign_pointer(queue_handler[pf], qh);
39 ret = 0;
40 }
41 mutex_unlock(&queue_handler_mutex);
42
43 return ret;
44}
45EXPORT_SYMBOL(nf_register_queue_handler);
46
47/* The caller must flush their queue before this */
48int nf_unregister_queue_handler(int pf, const struct nf_queue_handler *qh)
49{
50 if (pf >= NPROTO)
51 return -EINVAL;
52
53 mutex_lock(&queue_handler_mutex);
54 if (queue_handler[pf] != qh) {
55 mutex_unlock(&queue_handler_mutex);
56 return -EINVAL;
57 }
58
59 rcu_assign_pointer(queue_handler[pf], NULL);
60 mutex_unlock(&queue_handler_mutex);
61
62 synchronize_rcu();
63
64 return 0;
65}
66EXPORT_SYMBOL(nf_unregister_queue_handler);
67
68void nf_unregister_queue_handlers(const struct nf_queue_handler *qh)
69{
70 int pf;
71
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);
76 }
77 mutex_unlock(&queue_handler_mutex);
78
79 synchronize_rcu();
80}
81EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
82
83/*
84 * Any packet that leaves via this function must come back
85 * through nf_reinject().
86 */
87static int __nf_queue(struct sk_buff *skb,
88 struct list_head *elem,
89 int pf, unsigned int hook,
90 struct net_device *indev,
91 struct net_device *outdev,
92 int (*okfn)(struct sk_buff *),
93 unsigned int queuenum)
94{
95 int status;
96 struct nf_info *info;
97#ifdef CONFIG_BRIDGE_NETFILTER
98 struct net_device *physindev = NULL;
99 struct net_device *physoutdev = NULL;
100#endif
101 struct nf_afinfo *afinfo;
102 const struct nf_queue_handler *qh;
103
104 /* QUEUE == DROP if noone is waiting, to be safe. */
105 rcu_read_lock();
106
107 qh = rcu_dereference(queue_handler[pf]);
108 if (!qh) {
109 rcu_read_unlock();
110 kfree_skb(skb);
111 return 1;
112 }
113
114 afinfo = nf_get_afinfo(pf);
115 if (!afinfo) {
116 rcu_read_unlock();
117 kfree_skb(skb);
118 return 1;
119 }
120
121 info = kmalloc(sizeof(*info) + afinfo->route_key_size, GFP_ATOMIC);
122 if (!info) {
123 if (net_ratelimit())
124 printk(KERN_ERR "OOM queueing packet %p\n",
125 skb);
126 rcu_read_unlock();
127 kfree_skb(skb);
128 return 1;
129 }
130
131 *info = (struct nf_info) {
132 (struct nf_hook_ops *)elem, pf, hook, indev, outdev, okfn };
133
134 /* If it's going away, ignore hook. */
135 if (!try_module_get(info->elem->owner)) {
136 rcu_read_unlock();
137 kfree(info);
138 return 0;
139 }
140
141 /* Bump dev refs so they don't vanish while packet is out */
142 if (indev)
143 dev_hold(indev);
144 if (outdev)
145 dev_hold(outdev);
146#ifdef CONFIG_BRIDGE_NETFILTER
147 if (skb->nf_bridge) {
148 physindev = skb->nf_bridge->physindev;
149 if (physindev)
150 dev_hold(physindev);
151 physoutdev = skb->nf_bridge->physoutdev;
152 if (physoutdev)
153 dev_hold(physoutdev);
154 }
155#endif
156 afinfo->saveroute(skb, info);
157 status = qh->outfn(skb, info, queuenum);
158
159 rcu_read_unlock();
160
161 if (status < 0) {
162 /* James M doesn't say fuck enough. */
163 if (indev)
164 dev_put(indev);
165 if (outdev)
166 dev_put(outdev);
167#ifdef CONFIG_BRIDGE_NETFILTER
168 if (physindev)
169 dev_put(physindev);
170 if (physoutdev)
171 dev_put(physoutdev);
172#endif
173 module_put(info->elem->owner);
174 kfree(info);
175 kfree_skb(skb);
176
177 return 1;
178 }
179
180 return 1;
181}
182
183int nf_queue(struct sk_buff *skb,
184 struct list_head *elem,
185 int pf, unsigned int hook,
186 struct net_device *indev,
187 struct net_device *outdev,
188 int (*okfn)(struct sk_buff *),
189 unsigned int queuenum)
190{
191 struct sk_buff *segs;
192
193 if (!skb_is_gso(skb))
194 return __nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
195 queuenum);
196
197 switch (pf) {
198 case AF_INET:
199 skb->protocol = htons(ETH_P_IP);
200 break;
201 case AF_INET6:
202 skb->protocol = htons(ETH_P_IPV6);
203 break;
204 }
205
206 segs = skb_gso_segment(skb, 0);
207 kfree_skb(skb);
208 if (unlikely(IS_ERR(segs)))
209 return 1;
210
211 do {
212 struct sk_buff *nskb = segs->next;
213
214 segs->next = NULL;
215 if (!__nf_queue(segs, elem, pf, hook, indev, outdev, okfn,
216 queuenum))
217 kfree_skb(segs);
218 segs = nskb;
219 } while (segs);
220 return 1;
221}
222
223void nf_reinject(struct sk_buff *skb, struct nf_info *info,
224 unsigned int verdict)
225{
226 struct list_head *elem = &info->elem->list;
227 struct nf_afinfo *afinfo;
228
229 rcu_read_lock();
230
231 /* Release those devices we held, or Alexey will kill me. */
232 if (info->indev)
233 dev_put(info->indev);
234 if (info->outdev)
235 dev_put(info->outdev);
236#ifdef CONFIG_BRIDGE_NETFILTER
237 if (skb->nf_bridge) {
238 if (skb->nf_bridge->physindev)
239 dev_put(skb->nf_bridge->physindev);
240 if (skb->nf_bridge->physoutdev)
241 dev_put(skb->nf_bridge->physoutdev);
242 }
243#endif
244
245 /* Drop reference to owner of hook which queued us. */
246 module_put(info->elem->owner);
247
248 /* Continue traversal iff userspace said ok... */
249 if (verdict == NF_REPEAT) {
250 elem = elem->prev;
251 verdict = NF_ACCEPT;
252 }
253
254 if (verdict == NF_ACCEPT) {
255 afinfo = nf_get_afinfo(info->pf);
256 if (!afinfo || afinfo->reroute(skb, info) < 0)
257 verdict = NF_DROP;
258 }
259
260 if (verdict == NF_ACCEPT) {
261 next_hook:
262 verdict = nf_iterate(&nf_hooks[info->pf][info->hook],
263 skb, info->hook,
264 info->indev, info->outdev, &elem,
265 info->okfn, INT_MIN);
266 }
267
268 switch (verdict & NF_VERDICT_MASK) {
269 case NF_ACCEPT:
270 case NF_STOP:
271 info->okfn(skb);
272 case NF_STOLEN:
273 break;
274 case NF_QUEUE:
275 if (!__nf_queue(skb, elem, info->pf, info->hook,
276 info->indev, info->outdev, info->okfn,
277 verdict >> NF_VERDICT_BITS))
278 goto next_hook;
279 break;
280 default:
281 kfree_skb(skb);
282 }
283 rcu_read_unlock();
284 kfree(info);
285 return;
286}
287EXPORT_SYMBOL(nf_reinject);
288
289#ifdef CONFIG_PROC_FS
290static void *seq_start(struct seq_file *seq, loff_t *pos)
291{
292 if (*pos >= NPROTO)
293 return NULL;
294
295 return pos;
296}
297
298static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
299{
300 (*pos)++;
301
302 if (*pos >= NPROTO)
303 return NULL;
304
305 return pos;
306}
307
308static void seq_stop(struct seq_file *s, void *v)
309{
310
311}
312
313static int seq_show(struct seq_file *s, void *v)
314{
315 int ret;
316 loff_t *pos = v;
317 const struct nf_queue_handler *qh;
318
319 rcu_read_lock();
320 qh = rcu_dereference(queue_handler[*pos]);
321 if (!qh)
322 ret = seq_printf(s, "%2lld NONE\n", *pos);
323 else
324 ret = seq_printf(s, "%2lld %s\n", *pos, qh->name);
325 rcu_read_unlock();
326
327 return ret;
328}
329
330static const struct seq_operations nfqueue_seq_ops = {
331 .start = seq_start,
332 .next = seq_next,
333 .stop = seq_stop,
334 .show = seq_show,
335};
336
337static int nfqueue_open(struct inode *inode, struct file *file)
338{
339 return seq_open(file, &nfqueue_seq_ops);
340}
341
342static const struct file_operations nfqueue_file_ops = {
343 .owner = THIS_MODULE,
344 .open = nfqueue_open,
345 .read = seq_read,
346 .llseek = seq_lseek,
347 .release = seq_release,
348};
349#endif /* PROC_FS */
350
351
352int __init netfilter_queue_init(void)
353{
354#ifdef CONFIG_PROC_FS
355 struct proc_dir_entry *pde;
356
357 pde = create_proc_entry("nf_queue", S_IRUGO, proc_net_netfilter);
358 if (!pde)
359 return -1;
360 pde->proc_fops = &nfqueue_file_ops;
361#endif
362 return 0;
363}
364