blob: 5baa8e24e6ac1b512250c676cf0caf115f0b17f9 [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
Patrick McHardydaaa8be2007-12-05 01:27:19 -080072}
Florian Westphala5fedd432013-04-19 04:58:25 +000073EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
Patrick McHardydaaa8be2007-12-05 01:27:19 -080074
Florian Westphal4bd60442013-04-19 04:58:23 +000075/* Bump dev refs so they don't vanish while packet is out */
Florian Westphaled78d092015-10-13 14:33:27 +020076void nf_queue_entry_get_refs(struct nf_queue_entry *entry)
Florian Westphal4bd60442013-04-19 04:58:23 +000077{
David S. Miller1d1de892015-04-03 16:31:01 -040078 struct nf_hook_state *state = &entry->state;
79
David S. Miller1d1de892015-04-03 16:31:01 -040080 if (state->in)
81 dev_hold(state->in);
82 if (state->out)
83 dev_hold(state->out);
David Miller1c984f82015-04-05 22:19:00 -040084 if (state->sk)
85 sock_hold(state->sk);
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +020086#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Florian Westphal4bd60442013-04-19 04:58:23 +000087 if (entry->skb->nf_bridge) {
Florian Westphal4bd60442013-04-19 04:58:23 +000088 struct net_device *physdev;
89
Florian Westphalc737b7c2015-04-02 14:31:41 +020090 physdev = nf_bridge_get_physindev(entry->skb);
Florian Westphal4bd60442013-04-19 04:58:23 +000091 if (physdev)
92 dev_hold(physdev);
Florian Westphalc737b7c2015-04-02 14:31:41 +020093 physdev = nf_bridge_get_physoutdev(entry->skb);
Florian Westphal4bd60442013-04-19 04:58:23 +000094 if (physdev)
95 dev_hold(physdev);
96 }
97#endif
Florian Westphal4bd60442013-04-19 04:58:23 +000098}
Florian Westphala5fedd432013-04-19 04:58:25 +000099EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
Florian Westphal4bd60442013-04-19 04:58:23 +0000100
Pablo Neira Ayuso2385eb02015-07-20 12:55:02 +0200101void nf_queue_nf_hook_drop(struct net *net, struct nf_hook_ops *ops)
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500102{
103 const struct nf_queue_handler *qh;
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500104
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500105 rcu_read_lock();
106 qh = rcu_dereference(queue_handler);
Pablo Neira Ayuso2385eb02015-07-20 12:55:02 +0200107 if (qh)
108 qh->nf_hook_drop(net, ops);
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500109 rcu_read_unlock();
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500110}
111
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800112/*
113 * Any packet that leaves via this function must come back
Harald Weltef6ebe772005-08-09 20:21:49 -0700114 * through nf_reinject().
115 */
Florian Westphala5fedd432013-04-19 04:58:25 +0000116int nf_queue(struct sk_buff *skb,
David S. Millercfdfab32015-04-03 16:23:58 -0400117 struct nf_hook_ops *elem,
118 struct nf_hook_state *state,
119 unsigned int queuenum)
Harald Weltef6ebe772005-08-09 20:21:49 -0700120{
Florian Westphalf1585082011-01-18 15:27:28 +0100121 int status = -ENOENT;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800122 struct nf_queue_entry *entry = NULL;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800123 const struct nf_afinfo *afinfo;
Patrick McHardye3ac5292007-12-05 01:23:57 -0800124 const struct nf_queue_handler *qh;
Harald Weltef6ebe772005-08-09 20:21:49 -0700125
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300126 /* QUEUE == DROP if no one is waiting, to be safe. */
Florian Westphal0360ae42012-11-23 06:22:21 +0000127 qh = rcu_dereference(queue_handler);
Florian Westphal94b27cc2011-01-18 16:08:30 +0100128 if (!qh) {
129 status = -ESRCH;
Florian Westphal81b43252015-10-13 14:33:28 +0200130 goto err;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100131 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700132
David S. Millercfdfab32015-04-03 16:23:58 -0400133 afinfo = nf_get_afinfo(state->pf);
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800134 if (!afinfo)
Florian Westphal81b43252015-10-13 14:33:28 +0200135 goto err;
Patrick McHardybce80322006-04-06 14:18:09 -0700136
Patrick McHardy02f014d2007-12-05 01:26:33 -0800137 entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
Florian Westphalf1585082011-01-18 15:27:28 +0100138 if (!entry) {
139 status = -ENOMEM;
Florian Westphal81b43252015-10-13 14:33:28 +0200140 goto err;
Florian Westphalf1585082011-01-18 15:27:28 +0100141 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700142
Patrick McHardy02f014d2007-12-05 01:26:33 -0800143 *entry = (struct nf_queue_entry) {
144 .skb = skb,
Michael Wang1c15b672012-08-22 20:00:06 +0000145 .elem = elem,
David S. Miller1d1de892015-04-03 16:31:01 -0400146 .state = *state,
Florian Westphala5fedd432013-04-19 04:58:25 +0000147 .size = sizeof(*entry) + afinfo->route_key_size,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800148 };
Harald Weltef6ebe772005-08-09 20:21:49 -0700149
Florian Westphaled78d092015-10-13 14:33:27 +0200150 nf_queue_entry_get_refs(entry);
Eric Dumazet7fee2262010-05-11 23:19:48 +0000151 skb_dst_force(skb);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800152 afinfo->saveroute(skb, entry);
153 status = qh->outfn(entry, queuenum);
Harald Weltef6ebe772005-08-09 20:21:49 -0700154
Harald Weltef6ebe772005-08-09 20:21:49 -0700155 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
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800162err:
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800163 kfree(entry);
Florian Westphalf1585082011-01-18 15:27:28 +0100164 return status;
Harald Weltef6ebe772005-08-09 20:21:49 -0700165}
166
Patrick McHardy02f014d2007-12-05 01:26:33 -0800167void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
Harald Weltef6ebe772005-08-09 20:21:49 -0700168{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800169 struct sk_buff *skb = entry->skb;
Michael Wang2a6decf2012-08-22 19:59:57 +0000170 struct nf_hook_ops *elem = entry->elem;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800171 const struct nf_afinfo *afinfo;
Florian Westphalf1585082011-01-18 15:27:28 +0100172 int err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700173
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800174 nf_queue_entry_release_refs(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700175
Harald Weltef6ebe772005-08-09 20:21:49 -0700176 /* Continue traversal iff userspace said ok... */
Florian Westphal7ceebfe2015-10-09 13:10:37 +0200177 if (verdict == NF_REPEAT)
178 verdict = elem->hook(elem->priv, skb, &entry->state);
Harald Weltef6ebe772005-08-09 20:21:49 -0700179
180 if (verdict == NF_ACCEPT) {
David S. Miller1d1de892015-04-03 16:31:01 -0400181 afinfo = nf_get_afinfo(entry->state.pf);
Eric W. Biedermand815d902015-09-25 15:07:28 -0500182 if (!afinfo || afinfo->reroute(entry->state.net, skb, entry) < 0)
Patrick McHardy7a11b982006-02-27 13:03:24 -0800183 verdict = NF_DROP;
184 }
185
David S. Miller1d1de892015-04-03 16:31:01 -0400186 entry->state.thresh = INT_MIN;
David S. Millercfdfab32015-04-03 16:23:58 -0400187
Patrick McHardy7a11b982006-02-27 13:03:24 -0800188 if (verdict == NF_ACCEPT) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700189 next_hook:
Eric W. Biedermanf3071702015-06-19 17:23:37 -0500190 verdict = nf_iterate(entry->state.hook_list,
David S. Miller1d1de892015-04-03 16:31:01 -0400191 skb, &entry->state, &elem);
Harald Weltef6ebe772005-08-09 20:21:49 -0700192 }
193
194 switch (verdict & NF_VERDICT_MASK) {
195 case NF_ACCEPT:
Patrick McHardy3bc38712006-07-24 22:52:47 -0700196 case NF_STOP:
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800197 local_bh_disable();
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500198 entry->state.okfn(entry->state.net, entry->state.sk, skb);
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800199 local_bh_enable();
Harald Weltef6ebe772005-08-09 20:21:49 -0700200 break;
Harald Weltef6ebe772005-08-09 20:21:49 -0700201 case NF_QUEUE:
David S. Miller1d1de892015-04-03 16:31:01 -0400202 err = nf_queue(skb, elem, &entry->state,
David S. Millercfdfab32015-04-03 16:23:58 -0400203 verdict >> NF_VERDICT_QBITS);
Florian Westphal06cdb632011-01-18 15:28:38 +0100204 if (err < 0) {
Florian Westphal94b27cc2011-01-18 16:08:30 +0100205 if (err == -ESRCH &&
206 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
207 goto next_hook;
Florian Westphal06cdb632011-01-18 15:28:38 +0100208 kfree_skb(skb);
209 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700210 break;
Eric Dumazet64507fd2010-02-19 15:28:38 +0100211 case NF_STOLEN:
Julian Anastasovfad54442011-08-05 00:36:28 +0000212 break;
Patrick McHardy3bc38712006-07-24 22:52:47 -0700213 default:
214 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700215 }
Florian Westphal81b43252015-10-13 14:33:28 +0200216
Patrick McHardy02f014d2007-12-05 01:26:33 -0800217 kfree(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700218}
219EXPORT_SYMBOL(nf_reinject);