blob: 96964a0070e11da46aa97b3fe94fb4f778e40418 [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
Aaron Conolee3b37f12016-09-21 11:35:07 -040099void nf_queue_nf_hook_drop(struct net *net, const struct nf_hook_entry *entry)
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)
Aaron Conolee3b37f12016-09-21 11:35:07 -0400106 qh->nf_hook_drop(net, entry);
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_state *state,
116 unsigned int queuenum)
Harald Weltef6ebe772005-08-09 20:21:49 -0700117{
Florian Westphalf1585082011-01-18 15:27:28 +0100118 int status = -ENOENT;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800119 struct nf_queue_entry *entry = NULL;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800120 const struct nf_afinfo *afinfo;
Patrick McHardye3ac5292007-12-05 01:23:57 -0800121 const struct nf_queue_handler *qh;
Eric W. Biedermandc3ee322016-05-13 21:18:52 -0500122 struct net *net = state->net;
Harald Weltef6ebe772005-08-09 20:21:49 -0700123
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300124 /* QUEUE == DROP if no one is waiting, to be safe. */
Eric W. Biedermandc3ee322016-05-13 21:18:52 -0500125 qh = rcu_dereference(net->nf.queue_handler);
Florian Westphal94b27cc2011-01-18 16:08:30 +0100126 if (!qh) {
127 status = -ESRCH;
Florian Westphal81b43252015-10-13 14:33:28 +0200128 goto err;
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)
Florian Westphal81b43252015-10-13 14:33:28 +0200133 goto err;
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;
Florian Westphal81b43252015-10-13 14:33:28 +0200138 goto err;
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,
David S. Miller1d1de892015-04-03 16:31:01 -0400143 .state = *state,
Florian Westphala5fedd432013-04-19 04:58:25 +0000144 .size = sizeof(*entry) + afinfo->route_key_size,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800145 };
Harald Weltef6ebe772005-08-09 20:21:49 -0700146
Florian Westphaled78d092015-10-13 14:33:27 +0200147 nf_queue_entry_get_refs(entry);
Eric Dumazet7fee2262010-05-11 23:19:48 +0000148 skb_dst_force(skb);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800149 afinfo->saveroute(skb, entry);
150 status = qh->outfn(entry, queuenum);
Harald Weltef6ebe772005-08-09 20:21:49 -0700151
Harald Weltef6ebe772005-08-09 20:21:49 -0700152 if (status < 0) {
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800153 nf_queue_entry_release_refs(entry);
154 goto err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700155 }
156
Florian Westphalf1585082011-01-18 15:27:28 +0100157 return 0;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800158
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800159err:
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800160 kfree(entry);
Florian Westphalf1585082011-01-18 15:27:28 +0100161 return status;
Harald Weltef6ebe772005-08-09 20:21:49 -0700162}
163
Patrick McHardy02f014d2007-12-05 01:26:33 -0800164void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
Harald Weltef6ebe772005-08-09 20:21:49 -0700165{
Aaron Conolee3b37f12016-09-21 11:35:07 -0400166 struct nf_hook_entry *hook_entry;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800167 struct sk_buff *skb = entry->skb;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800168 const struct nf_afinfo *afinfo;
Aaron Conolee3b37f12016-09-21 11:35:07 -0400169 struct nf_hook_ops *elem;
Florian Westphalf1585082011-01-18 15:27:28 +0100170 int err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700171
Aaron Conolee3b37f12016-09-21 11:35:07 -0400172 hook_entry = rcu_dereference(entry->state.hook_entries);
173 elem = &hook_entry->ops;
174
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800175 nf_queue_entry_release_refs(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700176
Harald Weltef6ebe772005-08-09 20:21:49 -0700177 /* Continue traversal iff userspace said ok... */
Florian Westphal7ceebfe2015-10-09 13:10:37 +0200178 if (verdict == NF_REPEAT)
179 verdict = elem->hook(elem->priv, skb, &entry->state);
Harald Weltef6ebe772005-08-09 20:21:49 -0700180
181 if (verdict == NF_ACCEPT) {
David S. Miller1d1de892015-04-03 16:31:01 -0400182 afinfo = nf_get_afinfo(entry->state.pf);
Eric W. Biedermand815d902015-09-25 15:07:28 -0500183 if (!afinfo || afinfo->reroute(entry->state.net, skb, entry) < 0)
Patrick McHardy7a11b982006-02-27 13:03:24 -0800184 verdict = NF_DROP;
185 }
186
David S. Miller1d1de892015-04-03 16:31:01 -0400187 entry->state.thresh = INT_MIN;
David S. Millercfdfab32015-04-03 16:23:58 -0400188
Patrick McHardy7a11b982006-02-27 13:03:24 -0800189 if (verdict == NF_ACCEPT) {
Harald Weltef6ebe772005-08-09 20:21:49 -0700190 next_hook:
Aaron Conolee3b37f12016-09-21 11:35:07 -0400191 verdict = nf_iterate(skb, &entry->state, &hook_entry);
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:
Aaron Conolee3b37f12016-09-21 11:35:07 -0400202 RCU_INIT_POINTER(entry->state.hook_entries, hook_entry);
203 err = nf_queue(skb, &entry->state,
David S. Millercfdfab32015-04-03 16:23:58 -0400204 verdict >> NF_VERDICT_QBITS);
Florian Westphal06cdb632011-01-18 15:28:38 +0100205 if (err < 0) {
Florian Westphal94b27cc2011-01-18 16:08:30 +0100206 if (err == -ESRCH &&
207 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
208 goto next_hook;
Florian Westphal06cdb632011-01-18 15:28:38 +0100209 kfree_skb(skb);
210 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700211 break;
Eric Dumazet64507fd2010-02-19 15:28:38 +0100212 case NF_STOLEN:
Julian Anastasovfad54442011-08-05 00:36:28 +0000213 break;
Patrick McHardy3bc38712006-07-24 22:52:47 -0700214 default:
215 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700216 }
Florian Westphal81b43252015-10-13 14:33:28 +0200217
Patrick McHardy02f014d2007-12-05 01:26:33 -0800218 kfree(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700219}
220EXPORT_SYMBOL(nf_reinject);