blob: 4a7662486f44bae527ed83e7e952c4d7195f3871 [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 Miller1c984f8a2015-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 Miller1c984f8a2015-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
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100110static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
Pablo Neira Ayuso01886bd2016-11-03 10:56:35 +0100111 struct nf_hook_entry *hook_entry, unsigned int queuenum)
Harald Weltef6ebe772005-08-09 20:21:49 -0700112{
Florian Westphalf1585082011-01-18 15:27:28 +0100113 int status = -ENOENT;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800114 struct nf_queue_entry *entry = NULL;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800115 const struct nf_afinfo *afinfo;
Patrick McHardye3ac5292007-12-05 01:23:57 -0800116 const struct nf_queue_handler *qh;
Eric W. Biedermandc3ee322016-05-13 21:18:52 -0500117 struct net *net = state->net;
Harald Weltef6ebe772005-08-09 20:21:49 -0700118
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300119 /* QUEUE == DROP if no one is waiting, to be safe. */
Eric W. Biedermandc3ee322016-05-13 21:18:52 -0500120 qh = rcu_dereference(net->nf.queue_handler);
Florian Westphal94b27cc2011-01-18 16:08:30 +0100121 if (!qh) {
122 status = -ESRCH;
Florian Westphal81b43252015-10-13 14:33:28 +0200123 goto err;
Florian Westphal94b27cc2011-01-18 16:08:30 +0100124 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700125
David S. Millercfdfab32015-04-03 16:23:58 -0400126 afinfo = nf_get_afinfo(state->pf);
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800127 if (!afinfo)
Florian Westphal81b43252015-10-13 14:33:28 +0200128 goto err;
Patrick McHardybce80322006-04-06 14:18:09 -0700129
Patrick McHardy02f014d2007-12-05 01:26:33 -0800130 entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
Florian Westphalf1585082011-01-18 15:27:28 +0100131 if (!entry) {
132 status = -ENOMEM;
Florian Westphal81b43252015-10-13 14:33:28 +0200133 goto err;
Florian Westphalf1585082011-01-18 15:27:28 +0100134 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700135
Patrick McHardy02f014d2007-12-05 01:26:33 -0800136 *entry = (struct nf_queue_entry) {
137 .skb = skb,
David S. Miller1d1de892015-04-03 16:31:01 -0400138 .state = *state,
Pablo Neira Ayuso01886bd2016-11-03 10:56:35 +0100139 .hook = hook_entry,
Florian Westphala5fedd432013-04-19 04:58:25 +0000140 .size = sizeof(*entry) + afinfo->route_key_size,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800141 };
Harald Weltef6ebe772005-08-09 20:21:49 -0700142
Florian Westphaled78d092015-10-13 14:33:27 +0200143 nf_queue_entry_get_refs(entry);
Eric Dumazet7fee2262010-05-11 23:19:48 +0000144 skb_dst_force(skb);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800145 afinfo->saveroute(skb, entry);
146 status = qh->outfn(entry, queuenum);
Harald Weltef6ebe772005-08-09 20:21:49 -0700147
Harald Weltef6ebe772005-08-09 20:21:49 -0700148 if (status < 0) {
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800149 nf_queue_entry_release_refs(entry);
150 goto err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700151 }
152
Florian Westphalf1585082011-01-18 15:27:28 +0100153 return 0;
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800154
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800155err:
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800156 kfree(entry);
Florian Westphalf1585082011-01-18 15:27:28 +0100157 return status;
Harald Weltef6ebe772005-08-09 20:21:49 -0700158}
159
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100160/* Packets leaving via this function must come back through nf_reinject(). */
161int nf_queue(struct sk_buff *skb, struct nf_hook_state *state,
162 struct nf_hook_entry **entryp, unsigned int verdict)
163{
164 struct nf_hook_entry *entry = *entryp;
165 int ret;
166
Pablo Neira Ayuso01886bd2016-11-03 10:56:35 +0100167 ret = __nf_queue(skb, state, entry, verdict >> NF_VERDICT_QBITS);
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100168 if (ret < 0) {
169 if (ret == -ESRCH &&
170 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS)) {
171 *entryp = rcu_dereference(entry->next);
172 return 1;
173 }
174 kfree_skb(skb);
175 }
176
177 return 0;
178}
179
Pablo Neira Ayuso26dfab72016-11-03 10:56:39 +0100180static unsigned int nf_iterate(struct sk_buff *skb,
181 struct nf_hook_state *state,
182 struct nf_hook_entry **entryp)
183{
184 unsigned int verdict;
185
186 do {
187repeat:
Aaron Conole0aa8c572016-11-15 17:48:44 -0500188 verdict = nf_hook_entry_hookfn((*entryp), skb, state);
Pablo Neira Ayuso26dfab72016-11-03 10:56:39 +0100189 if (verdict != NF_ACCEPT) {
190 if (verdict != NF_REPEAT)
191 return verdict;
192 goto repeat;
193 }
194 *entryp = rcu_dereference((*entryp)->next);
195 } while (*entryp);
196
197 return NF_ACCEPT;
198}
199
Patrick McHardy02f014d2007-12-05 01:26:33 -0800200void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
Harald Weltef6ebe772005-08-09 20:21:49 -0700201{
Pablo Neira Ayuso01886bd2016-11-03 10:56:35 +0100202 struct nf_hook_entry *hook_entry = entry->hook;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800203 struct sk_buff *skb = entry->skb;
Patrick McHardy1e796fd2007-12-17 22:42:27 -0800204 const struct nf_afinfo *afinfo;
Florian Westphalf1585082011-01-18 15:27:28 +0100205 int err;
Harald Weltef6ebe772005-08-09 20:21:49 -0700206
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800207 nf_queue_entry_release_refs(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700208
Harald Weltef6ebe772005-08-09 20:21:49 -0700209 /* Continue traversal iff userspace said ok... */
Florian Westphal7ceebfe2015-10-09 13:10:37 +0200210 if (verdict == NF_REPEAT)
Aaron Conole0aa8c572016-11-15 17:48:44 -0500211 verdict = nf_hook_entry_hookfn(hook_entry, skb, &entry->state);
Harald Weltef6ebe772005-08-09 20:21:49 -0700212
213 if (verdict == NF_ACCEPT) {
David S. Miller1d1de892015-04-03 16:31:01 -0400214 afinfo = nf_get_afinfo(entry->state.pf);
Eric W. Biedermand815d902015-09-25 15:07:28 -0500215 if (!afinfo || afinfo->reroute(entry->state.net, skb, entry) < 0)
Patrick McHardy7a11b982006-02-27 13:03:24 -0800216 verdict = NF_DROP;
217 }
218
219 if (verdict == NF_ACCEPT) {
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100220 hook_entry = rcu_dereference(hook_entry->next);
221 if (hook_entry)
222next_hook:
223 verdict = nf_iterate(skb, &entry->state, &hook_entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700224 }
225
226 switch (verdict & NF_VERDICT_MASK) {
227 case NF_ACCEPT:
Patrick McHardy3bc38712006-07-24 22:52:47 -0700228 case NF_STOP:
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100229okfn:
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800230 local_bh_disable();
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500231 entry->state.okfn(entry->state.net, entry->state.sk, skb);
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800232 local_bh_enable();
Harald Weltef6ebe772005-08-09 20:21:49 -0700233 break;
Harald Weltef6ebe772005-08-09 20:21:49 -0700234 case NF_QUEUE:
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100235 err = nf_queue(skb, &entry->state, &hook_entry, verdict);
236 if (err == 1) {
237 if (hook_entry)
Florian Westphal94b27cc2011-01-18 16:08:30 +0100238 goto next_hook;
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100239 goto okfn;
Florian Westphal06cdb632011-01-18 15:28:38 +0100240 }
Harald Weltef6ebe772005-08-09 20:21:49 -0700241 break;
Eric Dumazet64507fd2010-02-19 15:28:38 +0100242 case NF_STOLEN:
Julian Anastasovfad54442011-08-05 00:36:28 +0000243 break;
Patrick McHardy3bc38712006-07-24 22:52:47 -0700244 default:
245 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700246 }
Florian Westphal81b43252015-10-13 14:33:28 +0200247
Patrick McHardy02f014d2007-12-05 01:26:33 -0800248 kfree(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700249}
250EXPORT_SYMBOL(nf_reinject);