blob: 5b466cd1272feaf0d9b66caa42a542c8370bd1bf [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 */
Arnd Bergmann0906a372010-03-09 20:59:15 +010021static const struct nf_queue_handler __rcu *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;
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010030 const struct nf_queue_handler *old;
Harald Weltef6ebe772005-08-09 20:21:49 -070031
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020032 if (pf >= ARRAY_SIZE(queue_handler))
Harald Weltef6ebe772005-08-09 20:21:49 -070033 return -EINVAL;
34
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070035 mutex_lock(&queue_handler_mutex);
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010036 old = rcu_dereference_protected(queue_handler[pf],
37 lockdep_is_held(&queue_handler_mutex));
38 if (old == qh)
Harald Welted72367b2005-08-09 20:23:36 -070039 ret = -EEXIST;
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010040 else if (old)
Harald Weltef6ebe772005-08-09 20:21:49 -070041 ret = -EBUSY;
42 else {
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070043 rcu_assign_pointer(queue_handler[pf], qh);
Harald Weltef6ebe772005-08-09 20:21:49 -070044 ret = 0;
45 }
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070046 mutex_unlock(&queue_handler_mutex);
Harald Weltef6ebe772005-08-09 20:21:49 -070047
48 return ret;
49}
50EXPORT_SYMBOL(nf_register_queue_handler);
51
52/* The caller must flush their queue before this */
Jan Engelhardt76108ce2008-10-08 11:35:00 +020053int nf_unregister_queue_handler(u_int8_t pf, const struct nf_queue_handler *qh)
Harald Weltef6ebe772005-08-09 20:21:49 -070054{
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010055 const struct nf_queue_handler *old;
56
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020057 if (pf >= ARRAY_SIZE(queue_handler))
Harald Weltef6ebe772005-08-09 20:21:49 -070058 return -EINVAL;
59
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070060 mutex_lock(&queue_handler_mutex);
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010061 old = rcu_dereference_protected(queue_handler[pf],
62 lockdep_is_held(&queue_handler_mutex));
63 if (old && old != qh) {
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070064 mutex_unlock(&queue_handler_mutex);
Yasuyuki Kozakaice7663d2007-07-07 22:40:08 -070065 return -EINVAL;
66 }
67
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070068 rcu_assign_pointer(queue_handler[pf], NULL);
69 mutex_unlock(&queue_handler_mutex);
70
71 synchronize_rcu();
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080072
Harald Weltef6ebe772005-08-09 20:21:49 -070073 return 0;
74}
75EXPORT_SYMBOL(nf_unregister_queue_handler);
76
Patrick McHardye3ac5292007-12-05 01:23:57 -080077void nf_unregister_queue_handlers(const struct nf_queue_handler *qh)
Harald Weltef6ebe772005-08-09 20:21:49 -070078{
Jan Engelhardt76108ce2008-10-08 11:35:00 +020079 u_int8_t pf;
Harald Weltef6ebe772005-08-09 20:21:49 -070080
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070081 mutex_lock(&queue_handler_mutex);
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +020082 for (pf = 0; pf < ARRAY_SIZE(queue_handler); pf++) {
Eric Dumazet0e60ebe2010-11-15 18:17:21 +010083 if (rcu_dereference_protected(
84 queue_handler[pf],
85 lockdep_is_held(&queue_handler_mutex)
86 ) == qh)
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070087 rcu_assign_pointer(queue_handler[pf], NULL);
Harald Weltef6ebe772005-08-09 20:21:49 -070088 }
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070089 mutex_unlock(&queue_handler_mutex);
90
91 synchronize_rcu();
Harald Weltef6ebe772005-08-09 20:21:49 -070092}
93EXPORT_SYMBOL_GPL(nf_unregister_queue_handlers);
94
Patrick McHardydaaa8be2007-12-05 01:27:19 -080095static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
96{
97 /* Release those devices we held, or Alexey will kill me. */
98 if (entry->indev)
99 dev_put(entry->indev);
100 if (entry->outdev)
101 dev_put(entry->outdev);
102#ifdef CONFIG_BRIDGE_NETFILTER
103 if (entry->skb->nf_bridge) {
104 struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge;
105
106 if (nf_bridge->physindev)
107 dev_put(nf_bridge->physindev);
108 if (nf_bridge->physoutdev)
109 dev_put(nf_bridge->physoutdev);
110 }
111#endif
112 /* Drop reference to owner of hook which queued us. */
113 module_put(entry->elem->owner);
114}
115
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800116/*
117 * Any packet that leaves via this function must come back
Harald Weltef6ebe772005-08-09 20:21:49 -0700118 * through nf_reinject().
119 */
Patrick McHardy394f5452006-08-05 00:58:52 -0700120static int __nf_queue(struct sk_buff *skb,
121 struct list_head *elem,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200122 u_int8_t pf, unsigned int hook,
Patrick McHardy394f5452006-08-05 00:58:52 -0700123 struct net_device *indev,
124 struct net_device *outdev,
125 int (*okfn)(struct sk_buff *),
126 unsigned int queuenum)
Harald Weltef6ebe772005-08-09 20:21:49 -0700127{
Florian Westphalf1585082011-01-18 15:27:28 +0100128 int status = -ENOENT;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800129 struct nf_queue_entry *entry = NULL;
Harald Weltef6ebe772005-08-09 20:21:49 -0700130#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800131 struct net_device *physindev;
132 struct net_device *physoutdev;
Harald Weltef6ebe772005-08-09 20:21:49 -0700133#endif
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800134 const struct nf_afinfo *afinfo;
Patrick McHardye3ac5292007-12-05 01:23:57 -0800135 const struct nf_queue_handler *qh;
Harald Weltef6ebe772005-08-09 20:21:49 -0700136
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300137 /* QUEUE == DROP if no one is waiting, to be safe. */
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700138 rcu_read_lock();
139
140 qh = rcu_dereference(queue_handler[pf]);
Florian Westphal94b27cc2011-01-18 16:08:30 +0100141 if (!qh) {
142 status = -ESRCH;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800143 goto err_unlock;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100144 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700145
Patrick McHardybce80322006-04-06 14:18:09 -0700146 afinfo = nf_get_afinfo(pf);
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800147 if (!afinfo)
148 goto err_unlock;
Patrick McHardybce80322006-04-06 14:18:09 -0700149
Patrick McHardy02f014d2007-12-05 01:26:33 -0800150 entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
Florian Westphalf1585082011-01-18 15:27:28 +0100151 if (!entry) {
152 status = -ENOMEM;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800153 goto err_unlock;
Florian Westphalf1585082011-01-18 15:27:28 +0100154 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700155
Patrick McHardy02f014d2007-12-05 01:26:33 -0800156 *entry = (struct nf_queue_entry) {
157 .skb = skb,
158 .elem = list_entry(elem, struct nf_hook_ops, list),
159 .pf = pf,
160 .hook = hook,
161 .indev = indev,
162 .outdev = outdev,
163 .okfn = okfn,
164 };
Harald Weltef6ebe772005-08-09 20:21:49 -0700165
166 /* If it's going away, ignore hook. */
Patrick McHardy02f014d2007-12-05 01:26:33 -0800167 if (!try_module_get(entry->elem->owner)) {
Florian Westphal06cdb632011-01-18 15:28:38 +0100168 status = -ECANCELED;
169 goto err_unlock;
Harald Weltef6ebe772005-08-09 20:21:49 -0700170 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700171 /* Bump dev refs so they don't vanish while packet is out */
Patrick McHardy8b1cf0d2007-12-05 01:23:17 -0800172 if (indev)
173 dev_hold(indev);
174 if (outdev)
175 dev_hold(outdev);
Harald Weltef6ebe772005-08-09 20:21:49 -0700176#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardy394f5452006-08-05 00:58:52 -0700177 if (skb->nf_bridge) {
178 physindev = skb->nf_bridge->physindev;
Patrick McHardy8b1cf0d2007-12-05 01:23:17 -0800179 if (physindev)
180 dev_hold(physindev);
Patrick McHardy394f5452006-08-05 00:58:52 -0700181 physoutdev = skb->nf_bridge->physoutdev;
Patrick McHardy8b1cf0d2007-12-05 01:23:17 -0800182 if (physoutdev)
183 dev_hold(physoutdev);
Harald Weltef6ebe772005-08-09 20:21:49 -0700184 }
185#endif
Eric Dumazet7fee2262010-05-11 23:19:48 +0000186 skb_dst_force(skb);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800187 afinfo->saveroute(skb, entry);
188 status = qh->outfn(entry, queuenum);
Harald Weltef6ebe772005-08-09 20:21:49 -0700189
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700190 rcu_read_unlock();
Harald Weltef6ebe772005-08-09 20:21:49 -0700191
192 if (status < 0) {
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800193 nf_queue_entry_release_refs(entry);
194 goto err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700195 }
196
Florian Westphalf1585082011-01-18 15:27:28 +0100197 return 0;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800198
199err_unlock:
200 rcu_read_unlock();
201err:
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800202 kfree(entry);
Florian Westphalf1585082011-01-18 15:27:28 +0100203 return status;
Harald Weltef6ebe772005-08-09 20:21:49 -0700204}
205
Patrick McHardy394f5452006-08-05 00:58:52 -0700206int nf_queue(struct sk_buff *skb,
207 struct list_head *elem,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200208 u_int8_t pf, unsigned int hook,
Patrick McHardy394f5452006-08-05 00:58:52 -0700209 struct net_device *indev,
210 struct net_device *outdev,
211 int (*okfn)(struct sk_buff *),
212 unsigned int queuenum)
213{
214 struct sk_buff *segs;
Florian Westphalf1585082011-01-18 15:27:28 +0100215 int err;
216 unsigned int queued;
Patrick McHardy394f5452006-08-05 00:58:52 -0700217
218 if (!skb_is_gso(skb))
219 return __nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
220 queuenum);
221
222 switch (pf) {
Jan Engelhardt4b1e27e2009-04-14 14:26:49 +0200223 case NFPROTO_IPV4:
Patrick McHardy394f5452006-08-05 00:58:52 -0700224 skb->protocol = htons(ETH_P_IP);
225 break;
Jan Engelhardt4b1e27e2009-04-14 14:26:49 +0200226 case NFPROTO_IPV6:
Patrick McHardy394f5452006-08-05 00:58:52 -0700227 skb->protocol = htons(ETH_P_IPV6);
228 break;
229 }
230
231 segs = skb_gso_segment(skb, 0);
Florian Westphalf1585082011-01-18 15:27:28 +0100232 /* Does not use PTR_ERR to limit the number of error codes that can be
233 * returned by nf_queue. For instance, callers rely on -ECANCELED to mean
234 * 'ignore this hook'.
235 */
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700236 if (IS_ERR(segs))
Florian Westphalf1585082011-01-18 15:27:28 +0100237 return -EINVAL;
Patrick McHardy394f5452006-08-05 00:58:52 -0700238
Florian Westphalf1585082011-01-18 15:27:28 +0100239 queued = 0;
240 err = 0;
Patrick McHardy394f5452006-08-05 00:58:52 -0700241 do {
242 struct sk_buff *nskb = segs->next;
243
244 segs->next = NULL;
Florian Westphalf1585082011-01-18 15:27:28 +0100245 if (err == 0)
246 err = __nf_queue(segs, elem, pf, hook, indev,
247 outdev, okfn, queuenum);
248 if (err == 0)
249 queued++;
250 else
Patrick McHardy394f5452006-08-05 00:58:52 -0700251 kfree_skb(segs);
252 segs = nskb;
253 } while (segs);
Florian Westphalf1585082011-01-18 15:27:28 +0100254
Florian Westphal06cdb632011-01-18 15:28:38 +0100255 /* also free orig skb if only some segments were queued */
Florian Westphalf1585082011-01-18 15:27:28 +0100256 if (unlikely(err && queued))
257 err = 0;
Florian Westphal06cdb632011-01-18 15:28:38 +0100258 if (err == 0)
259 kfree_skb(skb);
Florian Westphalf1585082011-01-18 15:27:28 +0100260 return err;
Patrick McHardy394f5452006-08-05 00:58:52 -0700261}
262
Patrick McHardy02f014d2007-12-05 01:26:33 -0800263void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
Harald Weltef6ebe772005-08-09 20:21:49 -0700264{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800265 struct sk_buff *skb = entry->skb;
266 struct list_head *elem = &entry->elem->list;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800267 const struct nf_afinfo *afinfo;
Florian Westphalf1585082011-01-18 15:27:28 +0100268 int err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700269
270 rcu_read_lock();
271
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800272 nf_queue_entry_release_refs(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700273
Harald Weltef6ebe772005-08-09 20:21:49 -0700274 /* Continue traversal iff userspace said ok... */
275 if (verdict == NF_REPEAT) {
276 elem = elem->prev;
277 verdict = NF_ACCEPT;
278 }
279
280 if (verdict == NF_ACCEPT) {
Patrick McHardy02f014d2007-12-05 01:26:33 -0800281 afinfo = nf_get_afinfo(entry->pf);
282 if (!afinfo || afinfo->reroute(skb, entry) < 0)
Patrick McHardy7a11b982006-02-27 13:03:24 -0800283 verdict = NF_DROP;
284 }
285
286 if (verdict == NF_ACCEPT) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700287 next_hook:
Patrick McHardy02f014d2007-12-05 01:26:33 -0800288 verdict = nf_iterate(&nf_hooks[entry->pf][entry->hook],
289 skb, entry->hook,
290 entry->indev, entry->outdev, &elem,
291 entry->okfn, INT_MIN);
Harald Weltef6ebe772005-08-09 20:21:49 -0700292 }
293
294 switch (verdict & NF_VERDICT_MASK) {
295 case NF_ACCEPT:
Patrick McHardy3bc38712006-07-24 22:52:47 -0700296 case NF_STOP:
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800297 local_bh_disable();
Patrick McHardy02f014d2007-12-05 01:26:33 -0800298 entry->okfn(skb);
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800299 local_bh_enable();
Harald Weltef6ebe772005-08-09 20:21:49 -0700300 break;
Harald Weltef6ebe772005-08-09 20:21:49 -0700301 case NF_QUEUE:
Florian Westphalf1585082011-01-18 15:27:28 +0100302 err = __nf_queue(skb, elem, entry->pf, entry->hook,
303 entry->indev, entry->outdev, entry->okfn,
Florian Westphalf615df72011-01-18 15:52:14 +0100304 verdict >> NF_VERDICT_QBITS);
Florian Westphal06cdb632011-01-18 15:28:38 +0100305 if (err < 0) {
306 if (err == -ECANCELED)
307 goto next_hook;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100308 if (err == -ESRCH &&
309 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
310 goto next_hook;
Florian Westphal06cdb632011-01-18 15:28:38 +0100311 kfree_skb(skb);
312 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700313 break;
Eric Dumazet64507fd2010-02-19 15:28:38 +0100314 case NF_STOLEN:
Patrick McHardy3bc38712006-07-24 22:52:47 -0700315 default:
316 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700317 }
318 rcu_read_unlock();
Patrick McHardy02f014d2007-12-05 01:26:33 -0800319 kfree(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700320}
321EXPORT_SYMBOL(nf_reinject);
322
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700323#ifdef CONFIG_PROC_FS
324static void *seq_start(struct seq_file *seq, loff_t *pos)
325{
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +0200326 if (*pos >= ARRAY_SIZE(queue_handler))
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700327 return NULL;
328
329 return pos;
330}
331
332static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
333{
334 (*pos)++;
335
Jan Engelhardt7e9c6ee2008-10-08 11:35:00 +0200336 if (*pos >= ARRAY_SIZE(queue_handler))
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700337 return NULL;
338
339 return pos;
340}
341
342static void seq_stop(struct seq_file *s, void *v)
343{
344
345}
346
347static int seq_show(struct seq_file *s, void *v)
348{
349 int ret;
350 loff_t *pos = v;
Patrick McHardye3ac5292007-12-05 01:23:57 -0800351 const struct nf_queue_handler *qh;
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700352
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700353 rcu_read_lock();
354 qh = rcu_dereference(queue_handler[*pos]);
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700355 if (!qh)
356 ret = seq_printf(s, "%2lld NONE\n", *pos);
357 else
358 ret = seq_printf(s, "%2lld %s\n", *pos, qh->name);
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700359 rcu_read_unlock();
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700360
361 return ret;
362}
363
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700364static const struct seq_operations nfqueue_seq_ops = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700365 .start = seq_start,
366 .next = seq_next,
367 .stop = seq_stop,
368 .show = seq_show,
369};
370
371static int nfqueue_open(struct inode *inode, struct file *file)
372{
373 return seq_open(file, &nfqueue_seq_ops);
374}
375
Arjan van de Venda7071d2007-02-12 00:55:36 -0800376static const struct file_operations nfqueue_file_ops = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700377 .owner = THIS_MODULE,
378 .open = nfqueue_open,
379 .read = seq_read,
380 .llseek = seq_lseek,
381 .release = seq_release,
382};
383#endif /* PROC_FS */
384
385
Harald Weltef6ebe772005-08-09 20:21:49 -0700386int __init netfilter_queue_init(void)
387{
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700388#ifdef CONFIG_PROC_FS
Denis V. Lunev8eeee8b2008-03-27 16:55:53 -0700389 if (!proc_create("nf_queue", S_IRUGO,
390 proc_net_netfilter, &nfqueue_file_ops))
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700391 return -1;
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700392#endif
Harald Weltef6ebe772005-08-09 20:21:49 -0700393 return 0;
394}
395