blob: 78b3cf9c519ca86e66b0ba4ae88c6797a7518c3f [file] [log] [blame]
Harald Weltef6ebe772005-08-09 20:21:49 -07001#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09002#include <linux/slab.h>
Harald Weltef6ebe772005-08-09 20:21:49 -07003#include <linux/init.h>
4#include <linux/module.h>
5#include <linux/proc_fs.h>
6#include <linux/skbuff.h>
7#include <linux/netfilter.h>
Harald Weltebbd86b9f2005-08-09 20:23:11 -07008#include <linux/seq_file.h>
Patrick McHardy7a11b982006-02-27 13:03:24 -08009#include <linux/rcupdate.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070010#include <net/protocol.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080011#include <net/netfilter/nf_queue.h>
Eric Dumazet7fee2262010-05-11 23:19:48 +000012#include <net/dst.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070013
14#include "nf_internals.h"
15
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080016/*
Harald Weltef6ebe772005-08-09 20:21:49 -070017 * A queue handler may be registered for each protocol. Each is protected by
18 * long term mutex. The handler must provide an an outfn() to accept packets
19 * for queueing and must reinject all packets it receives, no matter what.
20 */
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020021static const struct nf_queue_handler *queue_handler[NFPROTO_NUMPROTO] __read_mostly;
Harald Weltef6ebe772005-08-09 20:21:49 -070022
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070023static DEFINE_MUTEX(queue_handler_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070024
Harald Welted72367b2005-08-09 20:23:36 -070025/* return EBUSY when somebody else is registered, return EEXIST if the
26 * same handler is registered, return 0 in case of success. */
Jan Engelhardt76108ce2008-10-08 11:35:00 +020027int nf_register_queue_handler(u_int8_t pf, const struct nf_queue_handler *qh)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080028{
Harald Weltef6ebe772005-08-09 20:21:49 -070029 int ret;
30
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020031 if (pf >= ARRAY_SIZE(queue_handler))
Harald Weltef6ebe772005-08-09 20:21:49 -070032 return -EINVAL;
33
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070034 mutex_lock(&queue_handler_mutex);
Harald Welted72367b2005-08-09 20:23:36 -070035 if (queue_handler[pf] == qh)
36 ret = -EEXIST;
37 else if (queue_handler[pf])
Harald Weltef6ebe772005-08-09 20:21:49 -070038 ret = -EBUSY;
39 else {
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070040 rcu_assign_pointer(queue_handler[pf], qh);
Harald Weltef6ebe772005-08-09 20:21:49 -070041 ret = 0;
42 }
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070043 mutex_unlock(&queue_handler_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070044
45 return ret;
46}
47EXPORT_SYMBOL(nf_register_queue_handler);
48
49/* The caller must flush their queue before this */
Jan Engelhardt76108ce2008-10-08 11:35:00 +020050int nf_unregister_queue_handler(u_int8_t pf, const struct nf_queue_handler *qh)
Harald Weltef6ebe772005-08-09 20:21:49 -070051{
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020052 if (pf >= ARRAY_SIZE(queue_handler))
Harald Weltef6ebe772005-08-09 20:21:49 -070053 return -EINVAL;
54
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070055 mutex_lock(&queue_handler_mutex);
Patrick McHardy94be1a32008-03-10 16:45:05 -070056 if (queue_handler[pf] && queue_handler[pf] != qh) {
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070057 mutex_unlock(&queue_handler_mutex);
Yasuyuki Kozakaice7663d2007-07-07 22:40:08 -070058 return -EINVAL;
59 }
60
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070061 rcu_assign_pointer(queue_handler[pf], NULL);
62 mutex_unlock(&queue_handler_mutex);
63
64 synchronize_rcu();
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080065
Harald Weltef6ebe772005-08-09 20:21:49 -070066 return 0;
67}
68EXPORT_SYMBOL(nf_unregister_queue_handler);
69
Patrick McHardye3ac5292007-12-05 01:23:57 -080070void nf_unregister_queue_handlers(const struct nf_queue_handler *qh)
Harald Weltef6ebe772005-08-09 20:21:49 -070071{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020072 u_int8_t pf;
Harald Weltef6ebe772005-08-09 20:21:49 -070073
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070074 mutex_lock(&queue_handler_mutex);
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020075 for (pf = 0; pf < ARRAY_SIZE(queue_handler); pf++) {
Harald Weltebbd86b9f2005-08-09 20:23:11 -070076 if (queue_handler[pf] == qh)
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070077 rcu_assign_pointer(queue_handler[pf], NULL);
Harald Weltef6ebe772005-08-09 20:21:49 -070078 }
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070079 mutex_unlock(&queue_handler_mutex);
80
81 synchronize_rcu();
Harald Weltef6ebe772005-08-09 20:21:49 -070082}
83EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
84
Patrick McHardydaaa8be2007-12-05 01:27:19 -080085static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
86{
87 /* Release those devices we held, or Alexey will kill me. */
88 if (entry->indev)
89 dev_put(entry->indev);
90 if (entry->outdev)
91 dev_put(entry->outdev);
92#ifdef CONFIG_BRIDGE_NETFILTER
93 if (entry->skb->nf_bridge) {
94 struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge;
95
96 if (nf_bridge->physindev)
97 dev_put(nf_bridge->physindev);
98 if (nf_bridge->physoutdev)
99 dev_put(nf_bridge->physoutdev);
100 }
101#endif
102 /* Drop reference to owner of hook which queued us. */
103 module_put(entry->elem->owner);
104}
105
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800106/*
107 * Any packet that leaves via this function must come back
Harald Weltef6ebe772005-08-09 20:21:49 -0700108 * through nf_reinject().
109 */
Patrick McHardy394f5452006-08-05 00:58:52 -0700110static int __nf_queue(struct sk_buff *skb,
111 struct list_head *elem,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200112 u_int8_t pf, unsigned int hook,
Patrick McHardy394f5452006-08-05 00:58:52 -0700113 struct net_device *indev,
114 struct net_device *outdev,
115 int (*okfn)(struct sk_buff *),
116 unsigned int queuenum)
Harald Weltef6ebe772005-08-09 20:21:49 -0700117{
118 int status;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800119 struct nf_queue_entry *entry = NULL;
Harald Weltef6ebe772005-08-09 20:21:49 -0700120#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800121 struct net_device *physindev;
122 struct net_device *physoutdev;
Harald Weltef6ebe772005-08-09 20:21:49 -0700123#endif
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800124 const struct nf_afinfo *afinfo;
Patrick McHardye3ac5292007-12-05 01:23:57 -0800125 const struct nf_queue_handler *qh;
Harald Weltef6ebe772005-08-09 20:21:49 -0700126
127 /* QUEUE == DROP if noone is waiting, to be safe. */
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700128 rcu_read_lock();
129
130 qh = rcu_dereference(queue_handler[pf]);
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800131 if (!qh)
132 goto err_unlock;
Harald Weltef6ebe772005-08-09 20:21:49 -0700133
Patrick McHardybce80322006-04-06 14:18:09 -0700134 afinfo = nf_get_afinfo(pf);
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800135 if (!afinfo)
136 goto err_unlock;
Patrick McHardybce80322006-04-06 14:18:09 -0700137
Patrick McHardy02f014d2007-12-05 01:26:33 -0800138 entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800139 if (!entry)
140 goto err_unlock;
Harald Weltef6ebe772005-08-09 20:21:49 -0700141
Patrick McHardy02f014d2007-12-05 01:26:33 -0800142 *entry = (struct nf_queue_entry) {
143 .skb = skb,
144 .elem = list_entry(elem, struct nf_hook_ops, list),
145 .pf = pf,
146 .hook = hook,
147 .indev = indev,
148 .outdev = outdev,
149 .okfn = okfn,
150 };
Harald Weltef6ebe772005-08-09 20:21:49 -0700151
152 /* If it's going away, ignore hook. */
Patrick McHardy02f014d2007-12-05 01:26:33 -0800153 if (!try_module_get(entry->elem->owner)) {
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700154 rcu_read_unlock();
Patrick McHardy02f014d2007-12-05 01:26:33 -0800155 kfree(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700156 return 0;
157 }
158
159 /* Bump dev refs so they don't vanish while packet is out */
Patrick McHardy8b1cf0d2007-12-05 01:23:17 -0800160 if (indev)
161 dev_hold(indev);
162 if (outdev)
163 dev_hold(outdev);
Harald Weltef6ebe772005-08-09 20:21:49 -0700164#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardy394f5452006-08-05 00:58:52 -0700165 if (skb->nf_bridge) {
166 physindev = skb->nf_bridge->physindev;
Patrick McHardy8b1cf0d2007-12-05 01:23:17 -0800167 if (physindev)
168 dev_hold(physindev);
Patrick McHardy394f5452006-08-05 00:58:52 -0700169 physoutdev = skb->nf_bridge->physoutdev;
Patrick McHardy8b1cf0d2007-12-05 01:23:17 -0800170 if (physoutdev)
171 dev_hold(physoutdev);
Harald Weltef6ebe772005-08-09 20:21:49 -0700172 }
173#endif
Eric Dumazet7fee2262010-05-11 23:19:48 +0000174 skb_dst_force(skb);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800175 afinfo->saveroute(skb, entry);
176 status = qh->outfn(entry, queuenum);
Harald Weltef6ebe772005-08-09 20:21:49 -0700177
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700178 rcu_read_unlock();
Harald Weltef6ebe772005-08-09 20:21:49 -0700179
180 if (status < 0) {
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800181 nf_queue_entry_release_refs(entry);
182 goto err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700183 }
184
185 return 1;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800186
187err_unlock:
188 rcu_read_unlock();
189err:
190 kfree_skb(skb);
191 kfree(entry);
192 return 1;
Harald Weltef6ebe772005-08-09 20:21:49 -0700193}
194
Patrick McHardy394f5452006-08-05 00:58:52 -0700195int nf_queue(struct sk_buff *skb,
196 struct list_head *elem,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200197 u_int8_t pf, unsigned int hook,
Patrick McHardy394f5452006-08-05 00:58:52 -0700198 struct net_device *indev,
199 struct net_device *outdev,
200 int (*okfn)(struct sk_buff *),
201 unsigned int queuenum)
202{
203 struct sk_buff *segs;
204
205 if (!skb_is_gso(skb))
206 return __nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
207 queuenum);
208
209 switch (pf) {
Jan Engelhardt4b1e27e2009-04-14 14:26:49 +0200210 case NFPROTO_IPV4:
Patrick McHardy394f5452006-08-05 00:58:52 -0700211 skb->protocol = htons(ETH_P_IP);
212 break;
Jan Engelhardt4b1e27e2009-04-14 14:26:49 +0200213 case NFPROTO_IPV6:
Patrick McHardy394f5452006-08-05 00:58:52 -0700214 skb->protocol = htons(ETH_P_IPV6);
215 break;
216 }
217
218 segs = skb_gso_segment(skb, 0);
219 kfree_skb(skb);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700220 if (IS_ERR(segs))
Patrick McHardy394f5452006-08-05 00:58:52 -0700221 return 1;
222
223 do {
224 struct sk_buff *nskb = segs->next;
225
226 segs->next = NULL;
227 if (!__nf_queue(segs, elem, pf, hook, indev, outdev, okfn,
228 queuenum))
229 kfree_skb(segs);
230 segs = nskb;
231 } while (segs);
232 return 1;
233}
234
Patrick McHardy02f014d2007-12-05 01:26:33 -0800235void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
Harald Weltef6ebe772005-08-09 20:21:49 -0700236{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800237 struct sk_buff *skb = entry->skb;
238 struct list_head *elem = &entry->elem->list;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800239 const struct nf_afinfo *afinfo;
Harald Weltef6ebe772005-08-09 20:21:49 -0700240
241 rcu_read_lock();
242
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800243 nf_queue_entry_release_refs(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700244
Harald Weltef6ebe772005-08-09 20:21:49 -0700245 /* Continue traversal iff userspace said ok... */
246 if (verdict == NF_REPEAT) {
247 elem = elem->prev;
248 verdict = NF_ACCEPT;
249 }
250
251 if (verdict == NF_ACCEPT) {
Patrick McHardy02f014d2007-12-05 01:26:33 -0800252 afinfo = nf_get_afinfo(entry->pf);
253 if (!afinfo || afinfo->reroute(skb, entry) < 0)
Patrick McHardy7a11b982006-02-27 13:03:24 -0800254 verdict = NF_DROP;
255 }
256
257 if (verdict == NF_ACCEPT) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700258 next_hook:
Patrick McHardy02f014d2007-12-05 01:26:33 -0800259 verdict = nf_iterate(&nf_hooks[entry->pf][entry->hook],
260 skb, entry->hook,
261 entry->indev, entry->outdev, &elem,
262 entry->okfn, INT_MIN);
Harald Weltef6ebe772005-08-09 20:21:49 -0700263 }
264
265 switch (verdict & NF_VERDICT_MASK) {
266 case NF_ACCEPT:
Patrick McHardy3bc38712006-07-24 22:52:47 -0700267 case NF_STOP:
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800268 local_bh_disable();
Patrick McHardy02f014d2007-12-05 01:26:33 -0800269 entry->okfn(skb);
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800270 local_bh_enable();
Harald Weltef6ebe772005-08-09 20:21:49 -0700271 break;
Harald Weltef6ebe772005-08-09 20:21:49 -0700272 case NF_QUEUE:
Patrick McHardy02f014d2007-12-05 01:26:33 -0800273 if (!__nf_queue(skb, elem, entry->pf, entry->hook,
274 entry->indev, entry->outdev, entry->okfn,
Patrick McHardy394f5452006-08-05 00:58:52 -0700275 verdict >> NF_VERDICT_BITS))
Harald Weltef6ebe772005-08-09 20:21:49 -0700276 goto next_hook;
277 break;
Eric Dumazet64507fd2010-02-19 15:28:38 +0100278 case NF_STOLEN:
Patrick McHardy3bc38712006-07-24 22:52:47 -0700279 default:
280 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700281 }
282 rcu_read_unlock();
Patrick McHardy02f014d2007-12-05 01:26:33 -0800283 kfree(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700284}
285EXPORT_SYMBOL(nf_reinject);
286
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700287#ifdef CONFIG_PROC_FS
288static void *seq_start(struct seq_file *seq, loff_t *pos)
289{
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +0200290 if (*pos >= ARRAY_SIZE(queue_handler))
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700291 return NULL;
292
293 return pos;
294}
295
296static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
297{
298 (*pos)++;
299
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +0200300 if (*pos >= ARRAY_SIZE(queue_handler))
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700301 return NULL;
302
303 return pos;
304}
305
306static void seq_stop(struct seq_file *s, void *v)
307{
308
309}
310
311static int seq_show(struct seq_file *s, void *v)
312{
313 int ret;
314 loff_t *pos = v;
Patrick McHardye3ac5292007-12-05 01:23:57 -0800315 const struct nf_queue_handler *qh;
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700316
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700317 rcu_read_lock();
318 qh = rcu_dereference(queue_handler[*pos]);
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700319 if (!qh)
320 ret = seq_printf(s, "%2lld NONE\n", *pos);
321 else
322 ret = seq_printf(s, "%2lld %s\n", *pos, qh->name);
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700323 rcu_read_unlock();
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700324
325 return ret;
326}
327
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700328static const struct seq_operations nfqueue_seq_ops = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700329 .start = seq_start,
330 .next = seq_next,
331 .stop = seq_stop,
332 .show = seq_show,
333};
334
335static int nfqueue_open(struct inode *inode, struct file *file)
336{
337 return seq_open(file, &nfqueue_seq_ops);
338}
339
Arjan van de Venda7071d2007-02-12 00:55:36 -0800340static const struct file_operations nfqueue_file_ops = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700341 .owner = THIS_MODULE,
342 .open = nfqueue_open,
343 .read = seq_read,
344 .llseek = seq_lseek,
345 .release = seq_release,
346};
347#endif /* PROC_FS */
348
349
Harald Weltef6ebe772005-08-09 20:21:49 -0700350int __init netfilter_queue_init(void)
351{
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700352#ifdef CONFIG_PROC_FS
Denis V. Lunev8eeee8b2008-03-27 16:55:53 -0700353 if (!proc_create("nf_queue", S_IRUGO,
354 proc_net_netfilter, &nfqueue_file_ops))
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700355 return -1;
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700356#endif
Harald Weltef6ebe772005-08-09 20:21:49 -0700357 return 0;
358}
359