blob: 2e88032cd5ad22fb1e910966167340a1ba8761f6 [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>
Florian Westphalc737b7c2015-04-02 14:31:41 +020013#include <linux/netfilter_bridge.h>
Harald Weltebbd86b9f2005-08-09 20:23:11 -070014#include <linux/seq_file.h>
Patrick McHardy7a11b982006-02-27 13:03:24 -080015#include <linux/rcupdate.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070016#include <net/protocol.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080017#include <net/netfilter/nf_queue.h>
Eric Dumazet7fee2262010-05-11 23:19:48 +000018#include <net/dst.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070019
20#include "nf_internals.h"
21
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080022/*
Florian Westphal0360ae42012-11-23 06:22:21 +000023 * Hook for nfnetlink_queue to register its queue handler.
24 * We do this so that most of the NFQUEUE code can be modular.
25 *
26 * Once the queue is registered it must reinject all packets it
27 * receives, no matter what.
Harald Weltef6ebe772005-08-09 20:21:49 -070028 */
Florian Westphal0360ae42012-11-23 06:22:21 +000029static const struct nf_queue_handler __rcu *queue_handler __read_mostly;
Harald Weltef6ebe772005-08-09 20:21:49 -070030
Harald Welted72367b2005-08-09 20:23:36 -070031/* return EBUSY when somebody else is registered, return EEXIST if the
32 * same handler is registered, return 0 in case of success. */
Florian Westphal0360ae42012-11-23 06:22:21 +000033void nf_register_queue_handler(const struct nf_queue_handler *qh)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080034{
Florian Westphal0360ae42012-11-23 06:22:21 +000035 /* should never happen, we only have one queueing backend in kernel */
36 WARN_ON(rcu_access_pointer(queue_handler));
37 rcu_assign_pointer(queue_handler, qh);
Harald Weltef6ebe772005-08-09 20:21:49 -070038}
39EXPORT_SYMBOL(nf_register_queue_handler);
40
41/* The caller must flush their queue before this */
Florian Westphal0360ae42012-11-23 06:22:21 +000042void nf_unregister_queue_handler(void)
Harald Weltef6ebe772005-08-09 20:21:49 -070043{
Florian Westphal0360ae42012-11-23 06:22:21 +000044 RCU_INIT_POINTER(queue_handler, NULL);
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -070045 synchronize_rcu();
Harald Weltef6ebe772005-08-09 20:21:49 -070046}
47EXPORT_SYMBOL(nf_unregister_queue_handler);
48
Florian Westphala5fedd432013-04-19 04:58:25 +000049void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
Patrick McHardydaaa8be2007-12-05 01:27:19 -080050{
David S. Miller1d1de892015-04-03 16:31:01 -040051 struct nf_hook_state *state = &entry->state;
52
Patrick McHardydaaa8be2007-12-05 01:27:19 -080053 /* Release those devices we held, or Alexey will kill me. */
David S. Miller1d1de892015-04-03 16:31:01 -040054 if (state->in)
55 dev_put(state->in);
56 if (state->out)
57 dev_put(state->out);
David Miller1c984f8a2015-04-05 22:19:00 -040058 if (state->sk)
59 sock_put(state->sk);
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +020060#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Patrick McHardydaaa8be2007-12-05 01:27:19 -080061 if (entry->skb->nf_bridge) {
Florian Westphalc737b7c2015-04-02 14:31:41 +020062 struct net_device *physdev;
Patrick McHardydaaa8be2007-12-05 01:27:19 -080063
Florian Westphalc737b7c2015-04-02 14:31:41 +020064 physdev = nf_bridge_get_physindev(entry->skb);
65 if (physdev)
66 dev_put(physdev);
67 physdev = nf_bridge_get_physoutdev(entry->skb);
68 if (physdev)
69 dev_put(physdev);
Patrick McHardydaaa8be2007-12-05 01:27:19 -080070 }
71#endif
72 /* Drop reference to owner of hook which queued us. */
73 module_put(entry->elem->owner);
74}
Florian Westphala5fedd432013-04-19 04:58:25 +000075EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
Patrick McHardydaaa8be2007-12-05 01:27:19 -080076
Florian Westphal4bd60442013-04-19 04:58:23 +000077/* Bump dev refs so they don't vanish while packet is out */
Florian Westphala5fedd432013-04-19 04:58:25 +000078bool nf_queue_entry_get_refs(struct nf_queue_entry *entry)
Florian Westphal4bd60442013-04-19 04:58:23 +000079{
David S. Miller1d1de892015-04-03 16:31:01 -040080 struct nf_hook_state *state = &entry->state;
81
Florian Westphal4bd60442013-04-19 04:58:23 +000082 if (!try_module_get(entry->elem->owner))
83 return false;
84
David S. Miller1d1de892015-04-03 16:31:01 -040085 if (state->in)
86 dev_hold(state->in);
87 if (state->out)
88 dev_hold(state->out);
David Miller1c984f8a2015-04-05 22:19:00 -040089 if (state->sk)
90 sock_hold(state->sk);
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +020091#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Florian Westphal4bd60442013-04-19 04:58:23 +000092 if (entry->skb->nf_bridge) {
Florian Westphal4bd60442013-04-19 04:58:23 +000093 struct net_device *physdev;
94
Florian Westphalc737b7c2015-04-02 14:31:41 +020095 physdev = nf_bridge_get_physindev(entry->skb);
Florian Westphal4bd60442013-04-19 04:58:23 +000096 if (physdev)
97 dev_hold(physdev);
Florian Westphalc737b7c2015-04-02 14:31:41 +020098 physdev = nf_bridge_get_physoutdev(entry->skb);
Florian Westphal4bd60442013-04-19 04:58:23 +000099 if (physdev)
100 dev_hold(physdev);
101 }
102#endif
103
104 return true;
105}
Florian Westphala5fedd432013-04-19 04:58:25 +0000106EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
Florian Westphal4bd60442013-04-19 04:58:23 +0000107
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800108/*
109 * Any packet that leaves via this function must come back
Harald Weltef6ebe772005-08-09 20:21:49 -0700110 * through nf_reinject().
111 */
Florian Westphala5fedd432013-04-19 04:58:25 +0000112int nf_queue(struct sk_buff *skb,
David S. Millercfdfab32015-04-03 16:23:58 -0400113 struct nf_hook_ops *elem,
114 struct nf_hook_state *state,
115 unsigned int queuenum)
Harald Weltef6ebe772005-08-09 20:21:49 -0700116{
Florian Westphalf1585082011-01-18 15:27:28 +0100117 int status = -ENOENT;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800118 struct nf_queue_entry *entry = NULL;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800119 const struct nf_afinfo *afinfo;
Patrick McHardye3ac5292007-12-05 01:23:57 -0800120 const struct nf_queue_handler *qh;
Harald Weltef6ebe772005-08-09 20:21:49 -0700121
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300122 /* QUEUE == DROP if no one is waiting, to be safe. */
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700123 rcu_read_lock();
124
Florian Westphal0360ae42012-11-23 06:22:21 +0000125 qh = rcu_dereference(queue_handler);
Florian Westphal94b27cc2011-01-18 16:08:30 +0100126 if (!qh) {
127 status = -ESRCH;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800128 goto err_unlock;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100129 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700130
David S. Millercfdfab32015-04-03 16:23:58 -0400131 afinfo = nf_get_afinfo(state->pf);
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800132 if (!afinfo)
133 goto err_unlock;
Patrick McHardybce80322006-04-06 14:18:09 -0700134
Patrick McHardy02f014d2007-12-05 01:26:33 -0800135 entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
Florian Westphalf1585082011-01-18 15:27:28 +0100136 if (!entry) {
137 status = -ENOMEM;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800138 goto err_unlock;
Florian Westphalf1585082011-01-18 15:27:28 +0100139 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700140
Patrick McHardy02f014d2007-12-05 01:26:33 -0800141 *entry = (struct nf_queue_entry) {
142 .skb = skb,
Michael Wang1c15b672012-08-22 20:00:06 +0000143 .elem = elem,
David S. Miller1d1de892015-04-03 16:31:01 -0400144 .state = *state,
Florian Westphala5fedd432013-04-19 04:58:25 +0000145 .size = sizeof(*entry) + afinfo->route_key_size,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800146 };
Harald Weltef6ebe772005-08-09 20:21:49 -0700147
Florian Westphal4bd60442013-04-19 04:58:23 +0000148 if (!nf_queue_entry_get_refs(entry)) {
Florian Westphal06cdb632011-01-18 15:28:38 +0100149 status = -ECANCELED;
150 goto err_unlock;
Harald Weltef6ebe772005-08-09 20:21:49 -0700151 }
Eric Dumazet7fee2262010-05-11 23:19:48 +0000152 skb_dst_force(skb);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800153 afinfo->saveroute(skb, entry);
154 status = qh->outfn(entry, queuenum);
Harald Weltef6ebe772005-08-09 20:21:49 -0700155
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700156 rcu_read_unlock();
Harald Weltef6ebe772005-08-09 20:21:49 -0700157
158 if (status < 0) {
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800159 nf_queue_entry_release_refs(entry);
160 goto err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700161 }
162
Florian Westphalf1585082011-01-18 15:27:28 +0100163 return 0;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800164
165err_unlock:
166 rcu_read_unlock();
167err:
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800168 kfree(entry);
Florian Westphalf1585082011-01-18 15:27:28 +0100169 return status;
Harald Weltef6ebe772005-08-09 20:21:49 -0700170}
171
Patrick McHardy02f014d2007-12-05 01:26:33 -0800172void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
Harald Weltef6ebe772005-08-09 20:21:49 -0700173{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800174 struct sk_buff *skb = entry->skb;
Michael Wang2a6decf2012-08-22 19:59:57 +0000175 struct nf_hook_ops *elem = entry->elem;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800176 const struct nf_afinfo *afinfo;
Florian Westphalf1585082011-01-18 15:27:28 +0100177 int err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700178
179 rcu_read_lock();
180
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800181 nf_queue_entry_release_refs(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700182
Harald Weltef6ebe772005-08-09 20:21:49 -0700183 /* Continue traversal iff userspace said ok... */
184 if (verdict == NF_REPEAT) {
Michael Wang2a6decf2012-08-22 19:59:57 +0000185 elem = list_entry(elem->list.prev, struct nf_hook_ops, list);
Harald Weltef6ebe772005-08-09 20:21:49 -0700186 verdict = NF_ACCEPT;
187 }
188
189 if (verdict == NF_ACCEPT) {
David S. Miller1d1de892015-04-03 16:31:01 -0400190 afinfo = nf_get_afinfo(entry->state.pf);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800191 if (!afinfo || afinfo->reroute(skb, entry) < 0)
Patrick McHardy7a11b982006-02-27 13:03:24 -0800192 verdict = NF_DROP;
193 }
194
David S. Miller1d1de892015-04-03 16:31:01 -0400195 entry->state.thresh = INT_MIN;
David S. Millercfdfab32015-04-03 16:23:58 -0400196
Patrick McHardy7a11b982006-02-27 13:03:24 -0800197 if (verdict == NF_ACCEPT) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700198 next_hook:
David S. Miller1d1de892015-04-03 16:31:01 -0400199 verdict = nf_iterate(&nf_hooks[entry->state.pf][entry->state.hook],
200 skb, &entry->state, &elem);
Harald Weltef6ebe772005-08-09 20:21:49 -0700201 }
202
203 switch (verdict & NF_VERDICT_MASK) {
204 case NF_ACCEPT:
Patrick McHardy3bc38712006-07-24 22:52:47 -0700205 case NF_STOP:
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800206 local_bh_disable();
David Miller7026b1d2015-04-05 22:19:04 -0400207 entry->state.okfn(entry->state.sk, skb);
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800208 local_bh_enable();
Harald Weltef6ebe772005-08-09 20:21:49 -0700209 break;
Harald Weltef6ebe772005-08-09 20:21:49 -0700210 case NF_QUEUE:
David S. Miller1d1de892015-04-03 16:31:01 -0400211 err = nf_queue(skb, elem, &entry->state,
David S. Millercfdfab32015-04-03 16:23:58 -0400212 verdict >> NF_VERDICT_QBITS);
Florian Westphal06cdb632011-01-18 15:28:38 +0100213 if (err < 0) {
214 if (err == -ECANCELED)
215 goto next_hook;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100216 if (err == -ESRCH &&
217 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
218 goto next_hook;
Florian Westphal06cdb632011-01-18 15:28:38 +0100219 kfree_skb(skb);
220 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700221 break;
Eric Dumazet64507fd2010-02-19 15:28:38 +0100222 case NF_STOLEN:
Julian Anastasovfad54442011-08-05 00:36:28 +0000223 break;
Patrick McHardy3bc38712006-07-24 22:52:47 -0700224 default:
225 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700226 }
227 rcu_read_unlock();
Patrick McHardy02f014d2007-12-05 01:26:33 -0800228 kfree(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700229}
230EXPORT_SYMBOL(nf_reinject);