blob: d812c1235b30372b35f7b2866b113a9878b27147 [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/*
Florian Westphal0360ae42012-11-23 06:22:21 +000017 * Hook for nfnetlink_queue to register its queue handler.
18 * We do this so that most of the NFQUEUE code can be modular.
19 *
20 * Once the queue is registered it must reinject all packets it
21 * receives, no matter what.
Harald Weltef6ebe772005-08-09 20:21:49 -070022 */
Florian Westphal0360ae42012-11-23 06:22:21 +000023static const struct nf_queue_handler __rcu *queue_handler __read_mostly;
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. */
Florian Westphal0360ae42012-11-23 06:22:21 +000027void nf_register_queue_handler(const struct nf_queue_handler *qh)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080028{
Florian Westphal0360ae42012-11-23 06:22:21 +000029 /* should never happen, we only have one queueing backend in kernel */
30 WARN_ON(rcu_access_pointer(queue_handler));
31 rcu_assign_pointer(queue_handler, qh);
Harald Weltef6ebe772005-08-09 20:21:49 -070032}
33EXPORT_SYMBOL(nf_register_queue_handler);
34
35/* The caller must flush their queue before this */
Florian Westphal0360ae42012-11-23 06:22:21 +000036void nf_unregister_queue_handler(void)
Harald Weltef6ebe772005-08-09 20:21:49 -070037{
Florian Westphal0360ae42012-11-23 06:22:21 +000038 RCU_INIT_POINTER(queue_handler, NULL);
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070039 synchronize_rcu();
Harald Weltef6ebe772005-08-09 20:21:49 -070040}
41EXPORT_SYMBOL(nf_unregister_queue_handler);
42
Patrick McHardydaaa8be2007-12-05 01:27:19 -080043static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
44{
45 /* Release those devices we held, or Alexey will kill me. */
46 if (entry->indev)
47 dev_put(entry->indev);
48 if (entry->outdev)
49 dev_put(entry->outdev);
50#ifdef CONFIG_BRIDGE_NETFILTER
51 if (entry->skb->nf_bridge) {
52 struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge;
53
54 if (nf_bridge->physindev)
55 dev_put(nf_bridge->physindev);
56 if (nf_bridge->physoutdev)
57 dev_put(nf_bridge->physoutdev);
58 }
59#endif
60 /* Drop reference to owner of hook which queued us. */
61 module_put(entry->elem->owner);
62}
63
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080064/*
65 * Any packet that leaves via this function must come back
Harald Weltef6ebe772005-08-09 20:21:49 -070066 * through nf_reinject().
67 */
Patrick McHardy394f5452006-08-05 00:58:52 -070068static int __nf_queue(struct sk_buff *skb,
Michael Wang1c15b672012-08-22 20:00:06 +000069 struct nf_hook_ops *elem,
Jan Engelhardt76108ce2008-10-08 11:35:00 +020070 u_int8_t pf, unsigned int hook,
Patrick McHardy394f5452006-08-05 00:58:52 -070071 struct net_device *indev,
72 struct net_device *outdev,
73 int (*okfn)(struct sk_buff *),
74 unsigned int queuenum)
Harald Weltef6ebe772005-08-09 20:21:49 -070075{
Florian Westphalf1585082011-01-18 15:27:28 +010076 int status = -ENOENT;
Patrick McHardydaaa8be2007-12-05 01:27:19 -080077 struct nf_queue_entry *entry = NULL;
Harald Weltef6ebe772005-08-09 20:21:49 -070078#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydaaa8be2007-12-05 01:27:19 -080079 struct net_device *physindev;
80 struct net_device *physoutdev;
Harald Weltef6ebe772005-08-09 20:21:49 -070081#endif
Patrick McHardy1e796fd2007-12-17 22:42:27 -080082 const struct nf_afinfo *afinfo;
Patrick McHardye3ac5292007-12-05 01:23:57 -080083 const struct nf_queue_handler *qh;
Harald Weltef6ebe772005-08-09 20:21:49 -070084
Lucas De Marchi25985ed2011-03-30 22:57:33 -030085 /* QUEUE == DROP if no one is waiting, to be safe. */
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070086 rcu_read_lock();
87
Florian Westphal0360ae42012-11-23 06:22:21 +000088 qh = rcu_dereference(queue_handler);
Florian Westphal94b27cc2011-01-18 16:08:30 +010089 if (!qh) {
90 status = -ESRCH;
Patrick McHardydaaa8be2007-12-05 01:27:19 -080091 goto err_unlock;
Florian Westphal94b27cc2011-01-18 16:08:30 +010092 }
Harald Weltef6ebe772005-08-09 20:21:49 -070093
Patrick McHardybce80322006-04-06 14:18:09 -070094 afinfo = nf_get_afinfo(pf);
Patrick McHardydaaa8be2007-12-05 01:27:19 -080095 if (!afinfo)
96 goto err_unlock;
Patrick McHardybce80322006-04-06 14:18:09 -070097
Patrick McHardy02f014d2007-12-05 01:26:33 -080098 entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
Florian Westphalf1585082011-01-18 15:27:28 +010099 if (!entry) {
100 status = -ENOMEM;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800101 goto err_unlock;
Florian Westphalf1585082011-01-18 15:27:28 +0100102 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700103
Patrick McHardy02f014d2007-12-05 01:26:33 -0800104 *entry = (struct nf_queue_entry) {
105 .skb = skb,
Michael Wang1c15b672012-08-22 20:00:06 +0000106 .elem = elem,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800107 .pf = pf,
108 .hook = hook,
109 .indev = indev,
110 .outdev = outdev,
111 .okfn = okfn,
112 };
Harald Weltef6ebe772005-08-09 20:21:49 -0700113
114 /* If it's going away, ignore hook. */
Patrick McHardy02f014d2007-12-05 01:26:33 -0800115 if (!try_module_get(entry->elem->owner)) {
Florian Westphal06cdb632011-01-18 15:28:38 +0100116 status = -ECANCELED;
117 goto err_unlock;
Harald Weltef6ebe772005-08-09 20:21:49 -0700118 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700119 /* Bump dev refs so they don't vanish while packet is out */
Patrick McHardy8b1cf0d2007-12-05 01:23:17 -0800120 if (indev)
121 dev_hold(indev);
122 if (outdev)
123 dev_hold(outdev);
Harald Weltef6ebe772005-08-09 20:21:49 -0700124#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardy394f5452006-08-05 00:58:52 -0700125 if (skb->nf_bridge) {
126 physindev = skb->nf_bridge->physindev;
Patrick McHardy8b1cf0d2007-12-05 01:23:17 -0800127 if (physindev)
128 dev_hold(physindev);
Patrick McHardy394f5452006-08-05 00:58:52 -0700129 physoutdev = skb->nf_bridge->physoutdev;
Patrick McHardy8b1cf0d2007-12-05 01:23:17 -0800130 if (physoutdev)
131 dev_hold(physoutdev);
Harald Weltef6ebe772005-08-09 20:21:49 -0700132 }
133#endif
Eric Dumazet7fee2262010-05-11 23:19:48 +0000134 skb_dst_force(skb);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800135 afinfo->saveroute(skb, entry);
136 status = qh->outfn(entry, queuenum);
Harald Weltef6ebe772005-08-09 20:21:49 -0700137
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700138 rcu_read_unlock();
Harald Weltef6ebe772005-08-09 20:21:49 -0700139
140 if (status < 0) {
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800141 nf_queue_entry_release_refs(entry);
142 goto err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700143 }
144
Florian Westphalf1585082011-01-18 15:27:28 +0100145 return 0;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800146
147err_unlock:
148 rcu_read_unlock();
149err:
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800150 kfree(entry);
Florian Westphalf1585082011-01-18 15:27:28 +0100151 return status;
Harald Weltef6ebe772005-08-09 20:21:49 -0700152}
153
Florian Westphala8db7b22012-02-06 13:23:10 +0100154#ifdef CONFIG_BRIDGE_NETFILTER
155/* When called from bridge netfilter, skb->data must point to MAC header
156 * before calling skb_gso_segment(). Else, original MAC header is lost
157 * and segmented skbs will be sent to wrong destination.
158 */
159static void nf_bridge_adjust_skb_data(struct sk_buff *skb)
160{
161 if (skb->nf_bridge)
162 __skb_push(skb, skb->network_header - skb->mac_header);
163}
164
165static void nf_bridge_adjust_segmented_data(struct sk_buff *skb)
166{
167 if (skb->nf_bridge)
168 __skb_pull(skb, skb->network_header - skb->mac_header);
169}
170#else
171#define nf_bridge_adjust_skb_data(s) do {} while (0)
172#define nf_bridge_adjust_segmented_data(s) do {} while (0)
173#endif
174
Patrick McHardy394f5452006-08-05 00:58:52 -0700175int nf_queue(struct sk_buff *skb,
Michael Wang1c15b672012-08-22 20:00:06 +0000176 struct nf_hook_ops *elem,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200177 u_int8_t pf, unsigned int hook,
Patrick McHardy394f5452006-08-05 00:58:52 -0700178 struct net_device *indev,
179 struct net_device *outdev,
180 int (*okfn)(struct sk_buff *),
181 unsigned int queuenum)
182{
183 struct sk_buff *segs;
Florian Westphala8db7b22012-02-06 13:23:10 +0100184 int err = -EINVAL;
Florian Westphalf1585082011-01-18 15:27:28 +0100185 unsigned int queued;
Patrick McHardy394f5452006-08-05 00:58:52 -0700186
187 if (!skb_is_gso(skb))
188 return __nf_queue(skb, elem, pf, hook, indev, outdev, okfn,
189 queuenum);
190
191 switch (pf) {
Jan Engelhardt4b1e27e2009-04-14 14:26:49 +0200192 case NFPROTO_IPV4:
Patrick McHardy394f5452006-08-05 00:58:52 -0700193 skb->protocol = htons(ETH_P_IP);
194 break;
Jan Engelhardt4b1e27e2009-04-14 14:26:49 +0200195 case NFPROTO_IPV6:
Patrick McHardy394f5452006-08-05 00:58:52 -0700196 skb->protocol = htons(ETH_P_IPV6);
197 break;
198 }
199
Florian Westphala8db7b22012-02-06 13:23:10 +0100200 nf_bridge_adjust_skb_data(skb);
Patrick McHardy394f5452006-08-05 00:58:52 -0700201 segs = skb_gso_segment(skb, 0);
Florian Westphalf1585082011-01-18 15:27:28 +0100202 /* Does not use PTR_ERR to limit the number of error codes that can be
203 * returned by nf_queue. For instance, callers rely on -ECANCELED to mean
204 * 'ignore this hook'.
205 */
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700206 if (IS_ERR(segs))
Florian Westphala8db7b22012-02-06 13:23:10 +0100207 goto out_err;
Florian Westphalf1585082011-01-18 15:27:28 +0100208 queued = 0;
209 err = 0;
Patrick McHardy394f5452006-08-05 00:58:52 -0700210 do {
211 struct sk_buff *nskb = segs->next;
212
213 segs->next = NULL;
Florian Westphala8db7b22012-02-06 13:23:10 +0100214 if (err == 0) {
215 nf_bridge_adjust_segmented_data(segs);
Florian Westphalf1585082011-01-18 15:27:28 +0100216 err = __nf_queue(segs, elem, pf, hook, indev,
217 outdev, okfn, queuenum);
Florian Westphala8db7b22012-02-06 13:23:10 +0100218 }
Florian Westphalf1585082011-01-18 15:27:28 +0100219 if (err == 0)
220 queued++;
221 else
Patrick McHardy394f5452006-08-05 00:58:52 -0700222 kfree_skb(segs);
223 segs = nskb;
224 } while (segs);
Florian Westphalf1585082011-01-18 15:27:28 +0100225
Florian Westphala8db7b22012-02-06 13:23:10 +0100226 if (queued) {
Florian Westphal06cdb632011-01-18 15:28:38 +0100227 kfree_skb(skb);
Florian Westphala8db7b22012-02-06 13:23:10 +0100228 return 0;
229 }
230 out_err:
231 nf_bridge_adjust_segmented_data(skb);
Florian Westphalf1585082011-01-18 15:27:28 +0100232 return err;
Patrick McHardy394f5452006-08-05 00:58:52 -0700233}
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;
Michael Wang2a6decf2012-08-22 19:59:57 +0000238 struct nf_hook_ops *elem = entry->elem;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800239 const struct nf_afinfo *afinfo;
Florian Westphalf1585082011-01-18 15:27:28 +0100240 int err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700241
242 rcu_read_lock();
243
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800244 nf_queue_entry_release_refs(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700245
Harald Weltef6ebe772005-08-09 20:21:49 -0700246 /* Continue traversal iff userspace said ok... */
247 if (verdict == NF_REPEAT) {
Michael Wang2a6decf2012-08-22 19:59:57 +0000248 elem = list_entry(elem->list.prev, struct nf_hook_ops, list);
Harald Weltef6ebe772005-08-09 20:21:49 -0700249 verdict = NF_ACCEPT;
250 }
251
252 if (verdict == NF_ACCEPT) {
Patrick McHardy02f014d2007-12-05 01:26:33 -0800253 afinfo = nf_get_afinfo(entry->pf);
254 if (!afinfo || afinfo->reroute(skb, entry) < 0)
Patrick McHardy7a11b982006-02-27 13:03:24 -0800255 verdict = NF_DROP;
256 }
257
258 if (verdict == NF_ACCEPT) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700259 next_hook:
Patrick McHardy02f014d2007-12-05 01:26:33 -0800260 verdict = nf_iterate(&nf_hooks[entry->pf][entry->hook],
261 skb, entry->hook,
262 entry->indev, entry->outdev, &elem,
263 entry->okfn, INT_MIN);
Harald Weltef6ebe772005-08-09 20:21:49 -0700264 }
265
266 switch (verdict & NF_VERDICT_MASK) {
267 case NF_ACCEPT:
Patrick McHardy3bc38712006-07-24 22:52:47 -0700268 case NF_STOP:
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800269 local_bh_disable();
Patrick McHardy02f014d2007-12-05 01:26:33 -0800270 entry->okfn(skb);
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800271 local_bh_enable();
Harald Weltef6ebe772005-08-09 20:21:49 -0700272 break;
Harald Weltef6ebe772005-08-09 20:21:49 -0700273 case NF_QUEUE:
Michael Wang1c15b672012-08-22 20:00:06 +0000274 err = __nf_queue(skb, elem, entry->pf, entry->hook,
Florian Westphalf1585082011-01-18 15:27:28 +0100275 entry->indev, entry->outdev, entry->okfn,
Florian Westphalf615df72011-01-18 15:52:14 +0100276 verdict >> NF_VERDICT_QBITS);
Florian Westphal06cdb632011-01-18 15:28:38 +0100277 if (err < 0) {
278 if (err == -ECANCELED)
279 goto next_hook;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100280 if (err == -ESRCH &&
281 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
282 goto next_hook;
Florian Westphal06cdb632011-01-18 15:28:38 +0100283 kfree_skb(skb);
284 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700285 break;
Eric Dumazet64507fd2010-02-19 15:28:38 +0100286 case NF_STOLEN:
Julian Anastasovfad54442011-08-05 00:36:28 +0000287 break;
Patrick McHardy3bc38712006-07-24 22:52:47 -0700288 default:
289 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700290 }
291 rcu_read_unlock();
Patrick McHardy02f014d2007-12-05 01:26:33 -0800292 kfree(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700293}
294EXPORT_SYMBOL(nf_reinject);