blob: 5d24b1fdb593b1e43bde32991903d41a065a0b34 [file] [log] [blame]
Patrick McHardyf229f6c2013-04-06 15:24:29 +02001/*
2 * Rusty Russell (C)2000 -- This code is GPL.
3 * Patrick McHardy (c) 2006-2012
4 */
5
Harald Weltef6ebe772005-08-09 20:21:49 -07006#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Harald Weltef6ebe772005-08-09 20:21:49 -07008#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>
Harald Weltebbd86b9f2005-08-09 20:23:11 -070013#include <linux/seq_file.h>
Patrick McHardy7a11b982006-02-27 13:03:24 -080014#include <linux/rcupdate.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070015#include <net/protocol.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080016#include <net/netfilter/nf_queue.h>
Eric Dumazet7fee2262010-05-11 23:19:48 +000017#include <net/dst.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070018
19#include "nf_internals.h"
20
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080021/*
Florian Westphal0360ae42012-11-23 06:22:21 +000022 * Hook for nfnetlink_queue to register its queue handler.
23 * We do this so that most of the NFQUEUE code can be modular.
24 *
25 * Once the queue is registered it must reinject all packets it
26 * receives, no matter what.
Harald Weltef6ebe772005-08-09 20:21:49 -070027 */
Florian Westphal0360ae42012-11-23 06:22:21 +000028static const struct nf_queue_handler __rcu *queue_handler __read_mostly;
Harald Weltef6ebe772005-08-09 20:21:49 -070029
Harald Welted72367b2005-08-09 20:23:36 -070030/* return EBUSY when somebody else is registered, return EEXIST if the
31 * same handler is registered, return 0 in case of success. */
Florian Westphal0360ae42012-11-23 06:22:21 +000032void nf_register_queue_handler(const struct nf_queue_handler *qh)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080033{
Florian Westphal0360ae42012-11-23 06:22:21 +000034 /* should never happen, we only have one queueing backend in kernel */
35 WARN_ON(rcu_access_pointer(queue_handler));
36 rcu_assign_pointer(queue_handler, qh);
Harald Weltef6ebe772005-08-09 20:21:49 -070037}
38EXPORT_SYMBOL(nf_register_queue_handler);
39
40/* The caller must flush their queue before this */
Florian Westphal0360ae42012-11-23 06:22:21 +000041void nf_unregister_queue_handler(void)
Harald Weltef6ebe772005-08-09 20:21:49 -070042{
Florian Westphal0360ae42012-11-23 06:22:21 +000043 RCU_INIT_POINTER(queue_handler, NULL);
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070044 synchronize_rcu();
Harald Weltef6ebe772005-08-09 20:21:49 -070045}
46EXPORT_SYMBOL(nf_unregister_queue_handler);
47
Florian Westphala5fedd432013-04-19 04:58:25 +000048void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
Patrick McHardydaaa8be2007-12-05 01:27:19 -080049{
50 /* Release those devices we held, or Alexey will kill me. */
51 if (entry->indev)
52 dev_put(entry->indev);
53 if (entry->outdev)
54 dev_put(entry->outdev);
55#ifdef CONFIG_BRIDGE_NETFILTER
56 if (entry->skb->nf_bridge) {
57 struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge;
58
59 if (nf_bridge->physindev)
60 dev_put(nf_bridge->physindev);
61 if (nf_bridge->physoutdev)
62 dev_put(nf_bridge->physoutdev);
63 }
64#endif
65 /* Drop reference to owner of hook which queued us. */
66 module_put(entry->elem->owner);
67}
Florian Westphala5fedd432013-04-19 04:58:25 +000068EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
Patrick McHardydaaa8be2007-12-05 01:27:19 -080069
Florian Westphal4bd60442013-04-19 04:58:23 +000070/* Bump dev refs so they don't vanish while packet is out */
Florian Westphala5fedd432013-04-19 04:58:25 +000071bool nf_queue_entry_get_refs(struct nf_queue_entry *entry)
Florian Westphal4bd60442013-04-19 04:58:23 +000072{
73 if (!try_module_get(entry->elem->owner))
74 return false;
75
76 if (entry->indev)
77 dev_hold(entry->indev);
78 if (entry->outdev)
79 dev_hold(entry->outdev);
80#ifdef CONFIG_BRIDGE_NETFILTER
81 if (entry->skb->nf_bridge) {
82 struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge;
83 struct net_device *physdev;
84
85 physdev = nf_bridge->physindev;
86 if (physdev)
87 dev_hold(physdev);
88 physdev = nf_bridge->physoutdev;
89 if (physdev)
90 dev_hold(physdev);
91 }
92#endif
93
94 return true;
95}
Florian Westphala5fedd432013-04-19 04:58:25 +000096EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
Florian Westphal4bd60442013-04-19 04:58:23 +000097
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080098/*
99 * Any packet that leaves via this function must come back
Harald Weltef6ebe772005-08-09 20:21:49 -0700100 * through nf_reinject().
101 */
Florian Westphala5fedd432013-04-19 04:58:25 +0000102int nf_queue(struct sk_buff *skb,
Michael Wang1c15b672012-08-22 20:00:06 +0000103 struct nf_hook_ops *elem,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200104 u_int8_t pf, unsigned int hook,
Patrick McHardy394f5452006-08-05 00:58:52 -0700105 struct net_device *indev,
106 struct net_device *outdev,
107 int (*okfn)(struct sk_buff *),
108 unsigned int queuenum)
Harald Weltef6ebe772005-08-09 20:21:49 -0700109{
Florian Westphalf1585082011-01-18 15:27:28 +0100110 int status = -ENOENT;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800111 struct nf_queue_entry *entry = NULL;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800112 const struct nf_afinfo *afinfo;
Patrick McHardye3ac5292007-12-05 01:23:57 -0800113 const struct nf_queue_handler *qh;
Harald Weltef6ebe772005-08-09 20:21:49 -0700114
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300115 /* QUEUE == DROP if no one is waiting, to be safe. */
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700116 rcu_read_lock();
117
Florian Westphal0360ae42012-11-23 06:22:21 +0000118 qh = rcu_dereference(queue_handler);
Florian Westphal94b27cc2011-01-18 16:08:30 +0100119 if (!qh) {
120 status = -ESRCH;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800121 goto err_unlock;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100122 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700123
Patrick McHardybce80322006-04-06 14:18:09 -0700124 afinfo = nf_get_afinfo(pf);
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800125 if (!afinfo)
126 goto err_unlock;
Patrick McHardybce80322006-04-06 14:18:09 -0700127
Patrick McHardy02f014d2007-12-05 01:26:33 -0800128 entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
Florian Westphalf1585082011-01-18 15:27:28 +0100129 if (!entry) {
130 status = -ENOMEM;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800131 goto err_unlock;
Florian Westphalf1585082011-01-18 15:27:28 +0100132 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700133
Patrick McHardy02f014d2007-12-05 01:26:33 -0800134 *entry = (struct nf_queue_entry) {
135 .skb = skb,
Michael Wang1c15b672012-08-22 20:00:06 +0000136 .elem = elem,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800137 .pf = pf,
138 .hook = hook,
139 .indev = indev,
140 .outdev = outdev,
141 .okfn = okfn,
Florian Westphala5fedd432013-04-19 04:58:25 +0000142 .size = sizeof(*entry) + afinfo->route_key_size,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800143 };
Harald Weltef6ebe772005-08-09 20:21:49 -0700144
Florian Westphal4bd60442013-04-19 04:58:23 +0000145 if (!nf_queue_entry_get_refs(entry)) {
Florian Westphal06cdb632011-01-18 15:28:38 +0100146 status = -ECANCELED;
147 goto err_unlock;
Harald Weltef6ebe772005-08-09 20:21:49 -0700148 }
Eric Dumazet7fee2262010-05-11 23:19:48 +0000149 skb_dst_force(skb);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800150 afinfo->saveroute(skb, entry);
151 status = qh->outfn(entry, queuenum);
Harald Weltef6ebe772005-08-09 20:21:49 -0700152
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700153 rcu_read_unlock();
Harald Weltef6ebe772005-08-09 20:21:49 -0700154
155 if (status < 0) {
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800156 nf_queue_entry_release_refs(entry);
157 goto err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700158 }
159
Florian Westphalf1585082011-01-18 15:27:28 +0100160 return 0;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800161
162err_unlock:
163 rcu_read_unlock();
164err:
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800165 kfree(entry);
Florian Westphalf1585082011-01-18 15:27:28 +0100166 return status;
Harald Weltef6ebe772005-08-09 20:21:49 -0700167}
168
Patrick McHardy02f014d2007-12-05 01:26:33 -0800169void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
Harald Weltef6ebe772005-08-09 20:21:49 -0700170{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800171 struct sk_buff *skb = entry->skb;
Michael Wang2a6decf2012-08-22 19:59:57 +0000172 struct nf_hook_ops *elem = entry->elem;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800173 const struct nf_afinfo *afinfo;
Florian Westphalf1585082011-01-18 15:27:28 +0100174 int err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700175
176 rcu_read_lock();
177
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800178 nf_queue_entry_release_refs(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700179
Harald Weltef6ebe772005-08-09 20:21:49 -0700180 /* Continue traversal iff userspace said ok... */
181 if (verdict == NF_REPEAT) {
Michael Wang2a6decf2012-08-22 19:59:57 +0000182 elem = list_entry(elem->list.prev, struct nf_hook_ops, list);
Harald Weltef6ebe772005-08-09 20:21:49 -0700183 verdict = NF_ACCEPT;
184 }
185
186 if (verdict == NF_ACCEPT) {
Patrick McHardy02f014d2007-12-05 01:26:33 -0800187 afinfo = nf_get_afinfo(entry->pf);
188 if (!afinfo || afinfo->reroute(skb, entry) < 0)
Patrick McHardy7a11b982006-02-27 13:03:24 -0800189 verdict = NF_DROP;
190 }
191
192 if (verdict == NF_ACCEPT) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700193 next_hook:
Patrick McHardy02f014d2007-12-05 01:26:33 -0800194 verdict = nf_iterate(&nf_hooks[entry->pf][entry->hook],
195 skb, entry->hook,
196 entry->indev, entry->outdev, &elem,
197 entry->okfn, INT_MIN);
Harald Weltef6ebe772005-08-09 20:21:49 -0700198 }
199
200 switch (verdict & NF_VERDICT_MASK) {
201 case NF_ACCEPT:
Patrick McHardy3bc38712006-07-24 22:52:47 -0700202 case NF_STOP:
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800203 local_bh_disable();
Patrick McHardy02f014d2007-12-05 01:26:33 -0800204 entry->okfn(skb);
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800205 local_bh_enable();
Harald Weltef6ebe772005-08-09 20:21:49 -0700206 break;
Harald Weltef6ebe772005-08-09 20:21:49 -0700207 case NF_QUEUE:
Florian Westphala5fedd432013-04-19 04:58:25 +0000208 err = nf_queue(skb, elem, entry->pf, entry->hook,
209 entry->indev, entry->outdev, entry->okfn,
210 verdict >> NF_VERDICT_QBITS);
Florian Westphal06cdb632011-01-18 15:28:38 +0100211 if (err < 0) {
212 if (err == -ECANCELED)
213 goto next_hook;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100214 if (err == -ESRCH &&
215 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
216 goto next_hook;
Florian Westphal06cdb632011-01-18 15:28:38 +0100217 kfree_skb(skb);
218 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700219 break;
Eric Dumazet64507fd2010-02-19 15:28:38 +0100220 case NF_STOLEN:
Julian Anastasovfad54442011-08-05 00:36:28 +0000221 break;
Patrick McHardy3bc38712006-07-24 22:52:47 -0700222 default:
223 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700224 }
225 rcu_read_unlock();
Patrick McHardy02f014d2007-12-05 01:26:33 -0800226 kfree(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700227}
228EXPORT_SYMBOL(nf_reinject);