blob: 34f628e16a4cf7e460342311cbdbf579199c158b [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 Miller1c984f82015-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 Miller1c984f82015-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
Pablo Neira Ayuso2385eb02015-07-20 12:55:02 +0200108void nf_queue_nf_hook_drop(struct net *net, struct nf_hook_ops *ops)
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500109{
110 const struct nf_queue_handler *qh;
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500111
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500112 rcu_read_lock();
113 qh = rcu_dereference(queue_handler);
Pablo Neira Ayuso2385eb02015-07-20 12:55:02 +0200114 if (qh)
115 qh->nf_hook_drop(net, ops);
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500116 rcu_read_unlock();
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500117}
118
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800119/*
120 * Any packet that leaves via this function must come back
Harald Weltef6ebe772005-08-09 20:21:49 -0700121 * through nf_reinject().
122 */
Florian Westphala5fedd432013-04-19 04:58:25 +0000123int nf_queue(struct sk_buff *skb,
David S. Millercfdfab32015-04-03 16:23:58 -0400124 struct nf_hook_ops *elem,
125 struct nf_hook_state *state,
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;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800130 const struct nf_afinfo *afinfo;
Patrick McHardye3ac5292007-12-05 01:23:57 -0800131 const struct nf_queue_handler *qh;
Harald Weltef6ebe772005-08-09 20:21:49 -0700132
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300133 /* QUEUE == DROP if no one is waiting, to be safe. */
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700134 rcu_read_lock();
135
Florian Westphal0360ae42012-11-23 06:22:21 +0000136 qh = rcu_dereference(queue_handler);
Florian Westphal94b27cc2011-01-18 16:08:30 +0100137 if (!qh) {
138 status = -ESRCH;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800139 goto err_unlock;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100140 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700141
David S. Millercfdfab32015-04-03 16:23:58 -0400142 afinfo = nf_get_afinfo(state->pf);
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800143 if (!afinfo)
144 goto err_unlock;
Patrick McHardybce80322006-04-06 14:18:09 -0700145
Patrick McHardy02f014d2007-12-05 01:26:33 -0800146 entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
Florian Westphalf1585082011-01-18 15:27:28 +0100147 if (!entry) {
148 status = -ENOMEM;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800149 goto err_unlock;
Florian Westphalf1585082011-01-18 15:27:28 +0100150 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700151
Patrick McHardy02f014d2007-12-05 01:26:33 -0800152 *entry = (struct nf_queue_entry) {
153 .skb = skb,
Michael Wang1c15b672012-08-22 20:00:06 +0000154 .elem = elem,
David S. Miller1d1de892015-04-03 16:31:01 -0400155 .state = *state,
Florian Westphala5fedd432013-04-19 04:58:25 +0000156 .size = sizeof(*entry) + afinfo->route_key_size,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800157 };
Harald Weltef6ebe772005-08-09 20:21:49 -0700158
Florian Westphal4bd60442013-04-19 04:58:23 +0000159 if (!nf_queue_entry_get_refs(entry)) {
Florian Westphal06cdb632011-01-18 15:28:38 +0100160 status = -ECANCELED;
161 goto err_unlock;
Harald Weltef6ebe772005-08-09 20:21:49 -0700162 }
Eric Dumazet7fee2262010-05-11 23:19:48 +0000163 skb_dst_force(skb);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800164 afinfo->saveroute(skb, entry);
165 status = qh->outfn(entry, queuenum);
Harald Weltef6ebe772005-08-09 20:21:49 -0700166
Yasuyuki Kozakai585426f2007-07-07 22:40:26 -0700167 rcu_read_unlock();
Harald Weltef6ebe772005-08-09 20:21:49 -0700168
169 if (status < 0) {
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800170 nf_queue_entry_release_refs(entry);
171 goto err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700172 }
173
Florian Westphalf1585082011-01-18 15:27:28 +0100174 return 0;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800175
176err_unlock:
177 rcu_read_unlock();
178err:
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800179 kfree(entry);
Florian Westphalf1585082011-01-18 15:27:28 +0100180 return status;
Harald Weltef6ebe772005-08-09 20:21:49 -0700181}
182
Patrick McHardy02f014d2007-12-05 01:26:33 -0800183void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
Harald Weltef6ebe772005-08-09 20:21:49 -0700184{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800185 struct sk_buff *skb = entry->skb;
Michael Wang2a6decf2012-08-22 19:59:57 +0000186 struct nf_hook_ops *elem = entry->elem;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800187 const struct nf_afinfo *afinfo;
Florian Westphalf1585082011-01-18 15:27:28 +0100188 int err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700189
190 rcu_read_lock();
191
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800192 nf_queue_entry_release_refs(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700193
Harald Weltef6ebe772005-08-09 20:21:49 -0700194 /* Continue traversal iff userspace said ok... */
195 if (verdict == NF_REPEAT) {
Michael Wang2a6decf2012-08-22 19:59:57 +0000196 elem = list_entry(elem->list.prev, struct nf_hook_ops, list);
Harald Weltef6ebe772005-08-09 20:21:49 -0700197 verdict = NF_ACCEPT;
198 }
199
200 if (verdict == NF_ACCEPT) {
David S. Miller1d1de892015-04-03 16:31:01 -0400201 afinfo = nf_get_afinfo(entry->state.pf);
Eric W. Biedermand815d902015-09-25 15:07:28 -0500202 if (!afinfo || afinfo->reroute(entry->state.net, skb, entry) < 0)
Patrick McHardy7a11b982006-02-27 13:03:24 -0800203 verdict = NF_DROP;
204 }
205
David S. Miller1d1de892015-04-03 16:31:01 -0400206 entry->state.thresh = INT_MIN;
David S. Millercfdfab32015-04-03 16:23:58 -0400207
Patrick McHardy7a11b982006-02-27 13:03:24 -0800208 if (verdict == NF_ACCEPT) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700209 next_hook:
Eric W. Biedermanf3071702015-06-19 17:23:37 -0500210 verdict = nf_iterate(entry->state.hook_list,
David S. Miller1d1de892015-04-03 16:31:01 -0400211 skb, &entry->state, &elem);
Harald Weltef6ebe772005-08-09 20:21:49 -0700212 }
213
214 switch (verdict & NF_VERDICT_MASK) {
215 case NF_ACCEPT:
Patrick McHardy3bc38712006-07-24 22:52:47 -0700216 case NF_STOP:
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800217 local_bh_disable();
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500218 entry->state.okfn(entry->state.net, entry->state.sk, skb);
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800219 local_bh_enable();
Harald Weltef6ebe772005-08-09 20:21:49 -0700220 break;
Harald Weltef6ebe772005-08-09 20:21:49 -0700221 case NF_QUEUE:
David S. Miller1d1de892015-04-03 16:31:01 -0400222 err = nf_queue(skb, elem, &entry->state,
David S. Millercfdfab32015-04-03 16:23:58 -0400223 verdict >> NF_VERDICT_QBITS);
Florian Westphal06cdb632011-01-18 15:28:38 +0100224 if (err < 0) {
225 if (err == -ECANCELED)
226 goto next_hook;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100227 if (err == -ESRCH &&
228 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
229 goto next_hook;
Florian Westphal06cdb632011-01-18 15:28:38 +0100230 kfree_skb(skb);
231 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700232 break;
Eric Dumazet64507fd2010-02-19 15:28:38 +0100233 case NF_STOLEN:
Julian Anastasovfad54442011-08-05 00:36:28 +0000234 break;
Patrick McHardy3bc38712006-07-24 22:52:47 -0700235 default:
236 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700237 }
238 rcu_read_unlock();
Patrick McHardy02f014d2007-12-05 01:26:33 -0800239 kfree(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700240}
241EXPORT_SYMBOL(nf_reinject);