blob: b19ad20a705ca7845ad21a5d272accba190f3f38 [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 */
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. */
Eric W. Biedermandc3ee322016-05-13 21:18:52 -050032void nf_register_queue_handler(struct net *net, 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 */
Eric W. Biedermandc3ee322016-05-13 21:18:52 -050035 WARN_ON(rcu_access_pointer(net->nf.queue_handler));
36 rcu_assign_pointer(net->nf.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 */
Eric W. Biedermandc3ee322016-05-13 21:18:52 -050041void nf_unregister_queue_handler(struct net *net)
Harald Weltef6ebe772005-08-09 20:21:49 -070042{
Eric W. Biedermandc3ee322016-05-13 21:18:52 -050043 RCU_INIT_POINTER(net->nf.queue_handler, NULL);
Harald Weltef6ebe772005-08-09 20:21:49 -070044}
45EXPORT_SYMBOL(nf_unregister_queue_handler);
46
Florian Westphala5fedd432013-04-19 04:58:25 +000047void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
Patrick McHardydaaa8be2007-12-05 01:27:19 -080048{
David S. Miller1d1de892015-04-03 16:31:01 -040049 struct nf_hook_state *state = &entry->state;
50
Patrick McHardydaaa8be2007-12-05 01:27:19 -080051 /* Release those devices we held, or Alexey will kill me. */
David S. Miller1d1de892015-04-03 16:31:01 -040052 if (state->in)
53 dev_put(state->in);
54 if (state->out)
55 dev_put(state->out);
David Miller1c984f82015-04-05 22:19:00 -040056 if (state->sk)
57 sock_put(state->sk);
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +020058#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Patrick McHardydaaa8be2007-12-05 01:27:19 -080059 if (entry->skb->nf_bridge) {
Florian Westphalc737b7c2015-04-02 14:31:41 +020060 struct net_device *physdev;
Patrick McHardydaaa8be2007-12-05 01:27:19 -080061
Florian Westphalc737b7c2015-04-02 14:31:41 +020062 physdev = nf_bridge_get_physindev(entry->skb);
63 if (physdev)
64 dev_put(physdev);
65 physdev = nf_bridge_get_physoutdev(entry->skb);
66 if (physdev)
67 dev_put(physdev);
Patrick McHardydaaa8be2007-12-05 01:27:19 -080068 }
69#endif
Patrick McHardydaaa8be2007-12-05 01:27:19 -080070}
Florian Westphala5fedd432013-04-19 04:58:25 +000071EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
Patrick McHardydaaa8be2007-12-05 01:27:19 -080072
Florian Westphal4bd60442013-04-19 04:58:23 +000073/* Bump dev refs so they don't vanish while packet is out */
Florian Westphaled78d092015-10-13 14:33:27 +020074void nf_queue_entry_get_refs(struct nf_queue_entry *entry)
Florian Westphal4bd60442013-04-19 04:58:23 +000075{
David S. Miller1d1de892015-04-03 16:31:01 -040076 struct nf_hook_state *state = &entry->state;
77
David S. Miller1d1de892015-04-03 16:31:01 -040078 if (state->in)
79 dev_hold(state->in);
80 if (state->out)
81 dev_hold(state->out);
David Miller1c984f82015-04-05 22:19:00 -040082 if (state->sk)
83 sock_hold(state->sk);
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +020084#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Florian Westphal4bd60442013-04-19 04:58:23 +000085 if (entry->skb->nf_bridge) {
Florian Westphal4bd60442013-04-19 04:58:23 +000086 struct net_device *physdev;
87
Florian Westphalc737b7c2015-04-02 14:31:41 +020088 physdev = nf_bridge_get_physindev(entry->skb);
Florian Westphal4bd60442013-04-19 04:58:23 +000089 if (physdev)
90 dev_hold(physdev);
Florian Westphalc737b7c2015-04-02 14:31:41 +020091 physdev = nf_bridge_get_physoutdev(entry->skb);
Florian Westphal4bd60442013-04-19 04:58:23 +000092 if (physdev)
93 dev_hold(physdev);
94 }
95#endif
Florian Westphal4bd60442013-04-19 04:58:23 +000096}
Florian Westphala5fedd432013-04-19 04:58:25 +000097EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
Florian Westphal4bd60442013-04-19 04:58:23 +000098
Pablo Neira Ayuso2385eb02015-07-20 12:55:02 +020099void nf_queue_nf_hook_drop(struct net *net, struct nf_hook_ops *ops)
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500100{
101 const struct nf_queue_handler *qh;
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500102
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500103 rcu_read_lock();
Eric W. Biedermandc3ee322016-05-13 21:18:52 -0500104 qh = rcu_dereference(net->nf.queue_handler);
Pablo Neira Ayuso2385eb02015-07-20 12:55:02 +0200105 if (qh)
106 qh->nf_hook_drop(net, ops);
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500107 rcu_read_unlock();
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500108}
109
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800110/*
111 * Any packet that leaves via this function must come back
Harald Weltef6ebe772005-08-09 20:21:49 -0700112 * through nf_reinject().
113 */
Florian Westphala5fedd432013-04-19 04:58:25 +0000114int nf_queue(struct sk_buff *skb,
David S. Millercfdfab32015-04-03 16:23:58 -0400115 struct nf_hook_ops *elem,
116 struct nf_hook_state *state,
117 unsigned int queuenum)
Harald Weltef6ebe772005-08-09 20:21:49 -0700118{
Florian Westphalf1585082011-01-18 15:27:28 +0100119 int status = -ENOENT;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800120 struct nf_queue_entry *entry = NULL;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800121 const struct nf_afinfo *afinfo;
Patrick McHardye3ac5292007-12-05 01:23:57 -0800122 const struct nf_queue_handler *qh;
Eric W. Biedermandc3ee322016-05-13 21:18:52 -0500123 struct net *net = state->net;
Harald Weltef6ebe772005-08-09 20:21:49 -0700124
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300125 /* QUEUE == DROP if no one is waiting, to be safe. */
Eric W. Biedermandc3ee322016-05-13 21:18:52 -0500126 qh = rcu_dereference(net->nf.queue_handler);
Florian Westphal94b27cc2011-01-18 16:08:30 +0100127 if (!qh) {
128 status = -ESRCH;
Florian Westphal81b43252015-10-13 14:33:28 +0200129 goto err;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100130 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700131
David S. Millercfdfab32015-04-03 16:23:58 -0400132 afinfo = nf_get_afinfo(state->pf);
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800133 if (!afinfo)
Florian Westphal81b43252015-10-13 14:33:28 +0200134 goto err;
Patrick McHardybce80322006-04-06 14:18:09 -0700135
Patrick McHardy02f014d2007-12-05 01:26:33 -0800136 entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
Florian Westphalf1585082011-01-18 15:27:28 +0100137 if (!entry) {
138 status = -ENOMEM;
Florian Westphal81b43252015-10-13 14:33:28 +0200139 goto err;
Florian Westphalf1585082011-01-18 15:27:28 +0100140 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700141
Patrick McHardy02f014d2007-12-05 01:26:33 -0800142 *entry = (struct nf_queue_entry) {
143 .skb = skb,
Michael Wang1c15b672012-08-22 20:00:06 +0000144 .elem = elem,
David S. Miller1d1de892015-04-03 16:31:01 -0400145 .state = *state,
Florian Westphala5fedd432013-04-19 04:58:25 +0000146 .size = sizeof(*entry) + afinfo->route_key_size,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800147 };
Harald Weltef6ebe772005-08-09 20:21:49 -0700148
Florian Westphaled78d092015-10-13 14:33:27 +0200149 nf_queue_entry_get_refs(entry);
Eric Dumazet7fee2262010-05-11 23:19:48 +0000150 skb_dst_force(skb);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800151 afinfo->saveroute(skb, entry);
152 status = qh->outfn(entry, queuenum);
Harald Weltef6ebe772005-08-09 20:21:49 -0700153
Harald Weltef6ebe772005-08-09 20:21:49 -0700154 if (status < 0) {
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800155 nf_queue_entry_release_refs(entry);
156 goto err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700157 }
158
Florian Westphalf1585082011-01-18 15:27:28 +0100159 return 0;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800160
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800161err:
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800162 kfree(entry);
Florian Westphalf1585082011-01-18 15:27:28 +0100163 return status;
Harald Weltef6ebe772005-08-09 20:21:49 -0700164}
165
Patrick McHardy02f014d2007-12-05 01:26:33 -0800166void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
Harald Weltef6ebe772005-08-09 20:21:49 -0700167{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800168 struct sk_buff *skb = entry->skb;
Michael Wang2a6decf2012-08-22 19:59:57 +0000169 struct nf_hook_ops *elem = entry->elem;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800170 const struct nf_afinfo *afinfo;
Florian Westphalf1585082011-01-18 15:27:28 +0100171 int err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700172
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800173 nf_queue_entry_release_refs(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700174
Harald Weltef6ebe772005-08-09 20:21:49 -0700175 /* Continue traversal iff userspace said ok... */
Florian Westphal7ceebfe2015-10-09 13:10:37 +0200176 if (verdict == NF_REPEAT)
177 verdict = elem->hook(elem->priv, skb, &entry->state);
Harald Weltef6ebe772005-08-09 20:21:49 -0700178
179 if (verdict == NF_ACCEPT) {
David S. Miller1d1de892015-04-03 16:31:01 -0400180 afinfo = nf_get_afinfo(entry->state.pf);
Eric W. Biedermand815d902015-09-25 15:07:28 -0500181 if (!afinfo || afinfo->reroute(entry->state.net, skb, entry) < 0)
Patrick McHardy7a11b982006-02-27 13:03:24 -0800182 verdict = NF_DROP;
183 }
184
David S. Miller1d1de892015-04-03 16:31:01 -0400185 entry->state.thresh = INT_MIN;
David S. Millercfdfab32015-04-03 16:23:58 -0400186
Patrick McHardy7a11b982006-02-27 13:03:24 -0800187 if (verdict == NF_ACCEPT) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700188 next_hook:
Eric W. Biedermanf3071702015-06-19 17:23:37 -0500189 verdict = nf_iterate(entry->state.hook_list,
David S. Miller1d1de892015-04-03 16:31:01 -0400190 skb, &entry->state, &elem);
Harald Weltef6ebe772005-08-09 20:21:49 -0700191 }
192
193 switch (verdict & NF_VERDICT_MASK) {
194 case NF_ACCEPT:
Patrick McHardy3bc38712006-07-24 22:52:47 -0700195 case NF_STOP:
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800196 local_bh_disable();
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500197 entry->state.okfn(entry->state.net, entry->state.sk, skb);
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800198 local_bh_enable();
Harald Weltef6ebe772005-08-09 20:21:49 -0700199 break;
Harald Weltef6ebe772005-08-09 20:21:49 -0700200 case NF_QUEUE:
David S. Miller1d1de892015-04-03 16:31:01 -0400201 err = nf_queue(skb, elem, &entry->state,
David S. Millercfdfab32015-04-03 16:23:58 -0400202 verdict >> NF_VERDICT_QBITS);
Florian Westphal06cdb632011-01-18 15:28:38 +0100203 if (err < 0) {
Florian Westphal94b27cc2011-01-18 16:08:30 +0100204 if (err == -ESRCH &&
205 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
206 goto next_hook;
Florian Westphal06cdb632011-01-18 15:28:38 +0100207 kfree_skb(skb);
208 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700209 break;
Eric Dumazet64507fd2010-02-19 15:28:38 +0100210 case NF_STOLEN:
Julian Anastasovfad54442011-08-05 00:36:28 +0000211 break;
Patrick McHardy3bc38712006-07-24 22:52:47 -0700212 default:
213 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700214 }
Florian Westphal81b43252015-10-13 14:33:28 +0200215
Patrick McHardy02f014d2007-12-05 01:26:33 -0800216 kfree(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700217}
218EXPORT_SYMBOL(nf_reinject);