blob: bbd1209694b89bb1164c39ce581f54b24f61e8d4 [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>
Pablo Neira Ayuso7db9a512017-12-20 16:12:55 +010013#include <linux/netfilter_ipv4.h>
14#include <linux/netfilter_ipv6.h>
Florian Westphalc737b7c2015-04-02 14:31:41 +020015#include <linux/netfilter_bridge.h>
Harald Weltebbd86b9f2005-08-09 20:23:11 -070016#include <linux/seq_file.h>
Patrick McHardy7a11b982006-02-27 13:03:24 -080017#include <linux/rcupdate.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070018#include <net/protocol.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080019#include <net/netfilter/nf_queue.h>
Eric Dumazet7fee2262010-05-11 23:19:48 +000020#include <net/dst.h>
Harald Weltef6ebe772005-08-09 20:21:49 -070021
22#include "nf_internals.h"
23
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080024/*
Florian Westphal0360ae42012-11-23 06:22:21 +000025 * Hook for nfnetlink_queue to register its queue handler.
26 * We do this so that most of the NFQUEUE code can be modular.
27 *
28 * Once the queue is registered it must reinject all packets it
29 * receives, no matter what.
Harald Weltef6ebe772005-08-09 20:21:49 -070030 */
Harald Weltef6ebe772005-08-09 20:21:49 -070031
Harald Welted72367b2005-08-09 20:23:36 -070032/* return EBUSY when somebody else is registered, return EEXIST if the
33 * same handler is registered, return 0 in case of success. */
Eric W. Biedermandc3ee322016-05-13 21:18:52 -050034void nf_register_queue_handler(struct net *net, const struct nf_queue_handler *qh)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080035{
Florian Westphal0360ae42012-11-23 06:22:21 +000036 /* should never happen, we only have one queueing backend in kernel */
Eric W. Biedermandc3ee322016-05-13 21:18:52 -050037 WARN_ON(rcu_access_pointer(net->nf.queue_handler));
38 rcu_assign_pointer(net->nf.queue_handler, qh);
Harald Weltef6ebe772005-08-09 20:21:49 -070039}
40EXPORT_SYMBOL(nf_register_queue_handler);
41
42/* The caller must flush their queue before this */
Eric W. Biedermandc3ee322016-05-13 21:18:52 -050043void nf_unregister_queue_handler(struct net *net)
Harald Weltef6ebe772005-08-09 20:21:49 -070044{
Eric W. Biedermandc3ee322016-05-13 21:18:52 -050045 RCU_INIT_POINTER(net->nf.queue_handler, NULL);
Harald Weltef6ebe772005-08-09 20:21:49 -070046}
47EXPORT_SYMBOL(nf_unregister_queue_handler);
48
Florian Westphaldd3cc112020-03-27 03:24:46 +010049static void 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 Miller1c984f8a2015-04-05 22:19:00 -040058 if (state->sk)
59 sock_put(state->sk);
Florian Westphalc4b0e772018-12-18 17:15:15 +010060
Florian Westphal119e52e2020-03-27 03:24:47 +010061#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
62 if (entry->physin)
63 dev_put(entry->physin);
64 if (entry->physout)
65 dev_put(entry->physout);
66#endif
Florian Westphalc4b0e772018-12-18 17:15:15 +010067}
Florian Westphaldd3cc112020-03-27 03:24:46 +010068
69void nf_queue_entry_free(struct nf_queue_entry *entry)
70{
71 nf_queue_entry_release_refs(entry);
72 kfree(entry);
73}
74EXPORT_SYMBOL_GPL(nf_queue_entry_free);
Florian Westphalc4b0e772018-12-18 17:15:15 +010075
Florian Westphal119e52e2020-03-27 03:24:47 +010076static void __nf_queue_entry_init_physdevs(struct nf_queue_entry *entry)
Florian Westphalc4b0e772018-12-18 17:15:15 +010077{
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +020078#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Florian Westphal119e52e2020-03-27 03:24:47 +010079 const struct sk_buff *skb = entry->skb;
80 struct nf_bridge_info *nf_bridge;
Florian Westphalc4b0e772018-12-18 17:15:15 +010081
Florian Westphal119e52e2020-03-27 03:24:47 +010082 nf_bridge = nf_bridge_info_get(skb);
Florian Westphalc4b0e772018-12-18 17:15:15 +010083 if (nf_bridge) {
Florian Westphal119e52e2020-03-27 03:24:47 +010084 entry->physin = nf_bridge_get_physindev(skb);
85 entry->physout = nf_bridge_get_physoutdev(skb);
86 } else {
87 entry->physin = NULL;
88 entry->physout = NULL;
Patrick McHardydaaa8be2007-12-05 01:27:19 -080089 }
90#endif
Patrick McHardydaaa8be2007-12-05 01:27:19 -080091}
92
Florian Westphal4bd60442013-04-19 04:58:23 +000093/* Bump dev refs so they don't vanish while packet is out */
Florian Westphaled78d092015-10-13 14:33:27 +020094void nf_queue_entry_get_refs(struct nf_queue_entry *entry)
Florian Westphal4bd60442013-04-19 04:58:23 +000095{
David S. Miller1d1de892015-04-03 16:31:01 -040096 struct nf_hook_state *state = &entry->state;
97
David S. Miller1d1de892015-04-03 16:31:01 -040098 if (state->in)
99 dev_hold(state->in);
100 if (state->out)
101 dev_hold(state->out);
David Miller1c984f8a2015-04-05 22:19:00 -0400102 if (state->sk)
103 sock_hold(state->sk);
Florian Westphal4bd60442013-04-19 04:58:23 +0000104
Florian Westphal119e52e2020-03-27 03:24:47 +0100105#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
106 if (entry->physin)
107 dev_hold(entry->physin);
108 if (entry->physout)
109 dev_hold(entry->physout);
110#endif
Florian Westphal4bd60442013-04-19 04:58:23 +0000111}
Florian Westphala5fedd432013-04-19 04:58:25 +0000112EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
Florian Westphal4bd60442013-04-19 04:58:23 +0000113
Florian Westphal26888df2017-12-01 00:21:03 +0100114void nf_queue_nf_hook_drop(struct net *net)
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500115{
116 const struct nf_queue_handler *qh;
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500117
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500118 rcu_read_lock();
Eric W. Biedermandc3ee322016-05-13 21:18:52 -0500119 qh = rcu_dereference(net->nf.queue_handler);
Pablo Neira Ayuso2385eb02015-07-20 12:55:02 +0200120 if (qh)
Florian Westphal26888df2017-12-01 00:21:03 +0100121 qh->nf_hook_drop(net);
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500122 rcu_read_unlock();
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500123}
Florian Westphale2a75002017-07-26 00:02:33 +0200124EXPORT_SYMBOL_GPL(nf_queue_nf_hook_drop);
Eric W. Biederman8405a8f2015-06-19 14:03:39 -0500125
Pablo Neira Ayuso7db9a512017-12-20 16:12:55 +0100126static void nf_ip_saveroute(const struct sk_buff *skb,
127 struct nf_queue_entry *entry)
128{
129 struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
130
131 if (entry->state.hook == NF_INET_LOCAL_OUT) {
132 const struct iphdr *iph = ip_hdr(skb);
133
134 rt_info->tos = iph->tos;
135 rt_info->daddr = iph->daddr;
136 rt_info->saddr = iph->saddr;
137 rt_info->mark = skb->mark;
138 }
139}
140
141static void nf_ip6_saveroute(const struct sk_buff *skb,
142 struct nf_queue_entry *entry)
143{
144 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
145
146 if (entry->state.hook == NF_INET_LOCAL_OUT) {
147 const struct ipv6hdr *iph = ipv6_hdr(skb);
148
149 rt_info->daddr = iph->daddr;
150 rt_info->saddr = iph->saddr;
151 rt_info->mark = skb->mark;
152 }
153}
154
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100155static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
Aaron Conole960632e2017-08-24 00:08:32 +0200156 unsigned int index, unsigned int queuenum)
Harald Weltef6ebe772005-08-09 20:21:49 -0700157{
Patrick McHardydaaa8be2007-12-05 01:27:19 -0800158 struct nf_queue_entry *entry = NULL;
Patrick McHardye3ac5292007-12-05 01:23:57 -0800159 const struct nf_queue_handler *qh;
Eric W. Biedermandc3ee322016-05-13 21:18:52 -0500160 struct net *net = state->net;
Pablo Neira Ayuso46435622017-11-27 22:58:37 +0100161 unsigned int route_key_size;
Florian Westphal28f715b2020-03-27 03:24:49 +0100162 int status;
Harald Weltef6ebe772005-08-09 20:21:49 -0700163
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300164 /* QUEUE == DROP if no one is waiting, to be safe. */
Eric W. Biedermandc3ee322016-05-13 21:18:52 -0500165 qh = rcu_dereference(net->nf.queue_handler);
Florian Westphal28f715b2020-03-27 03:24:49 +0100166 if (!qh)
167 return -ESRCH;
Harald Weltef6ebe772005-08-09 20:21:49 -0700168
Pablo Neira Ayuso46435622017-11-27 22:58:37 +0100169 switch (state->pf) {
170 case AF_INET:
171 route_key_size = sizeof(struct ip_rt_info);
172 break;
173 case AF_INET6:
174 route_key_size = sizeof(struct ip6_rt_info);
175 break;
176 default:
177 route_key_size = 0;
178 break;
179 }
Patrick McHardybce80322006-04-06 14:18:09 -0700180
Pablo Neira Ayuso46435622017-11-27 22:58:37 +0100181 entry = kmalloc(sizeof(*entry) + route_key_size, GFP_ATOMIC);
Florian Westphal28f715b2020-03-27 03:24:49 +0100182 if (!entry)
183 return -ENOMEM;
Harald Weltef6ebe772005-08-09 20:21:49 -0700184
Marco Oliverio0b9173f2019-12-02 19:54:30 +0100185 if (skb_dst(skb) && !skb_dst_force(skb)) {
Florian Westphal28f715b2020-03-27 03:24:49 +0100186 kfree(entry);
187 return -ENETDOWN;
Florian Westphalb60a7732019-06-26 20:40:45 +0200188 }
189
Patrick McHardy02f014d2007-12-05 01:26:33 -0800190 *entry = (struct nf_queue_entry) {
191 .skb = skb,
David S. Miller1d1de892015-04-03 16:31:01 -0400192 .state = *state,
Aaron Conole960632e2017-08-24 00:08:32 +0200193 .hook_index = index,
Pablo Neira Ayuso46435622017-11-27 22:58:37 +0100194 .size = sizeof(*entry) + route_key_size,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800195 };
Harald Weltef6ebe772005-08-09 20:21:49 -0700196
Florian Westphal119e52e2020-03-27 03:24:47 +0100197 __nf_queue_entry_init_physdevs(entry);
198
Florian Westphaled78d092015-10-13 14:33:27 +0200199 nf_queue_entry_get_refs(entry);
Pablo Neira Ayuso7db9a512017-12-20 16:12:55 +0100200
201 switch (entry->state.pf) {
202 case AF_INET:
203 nf_ip_saveroute(skb, entry);
204 break;
205 case AF_INET6:
206 nf_ip6_saveroute(skb, entry);
207 break;
208 }
209
Patrick McHardy02f014d2007-12-05 01:26:33 -0800210 status = qh->outfn(entry, queuenum);
Harald Weltef6ebe772005-08-09 20:21:49 -0700211 if (status < 0) {
Florian Westphal28f715b2020-03-27 03:24:49 +0100212 nf_queue_entry_free(entry);
213 return status;
Harald Weltef6ebe772005-08-09 20:21:49 -0700214 }
215
Florian Westphalf1585082011-01-18 15:27:28 +0100216 return 0;
Harald Weltef6ebe772005-08-09 20:21:49 -0700217}
218
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100219/* Packets leaving via this function must come back through nf_reinject(). */
220int nf_queue(struct sk_buff *skb, struct nf_hook_state *state,
Florian Westphal0d9cb302019-07-02 20:41:14 +0200221 unsigned int index, unsigned int verdict)
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100222{
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100223 int ret;
224
Florian Westphal0d9cb302019-07-02 20:41:14 +0200225 ret = __nf_queue(skb, state, index, verdict >> NF_VERDICT_QBITS);
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100226 if (ret < 0) {
227 if (ret == -ESRCH &&
Aaron Conole960632e2017-08-24 00:08:32 +0200228 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100229 return 1;
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100230 kfree_skb(skb);
231 }
232
233 return 0;
234}
Florian Westphal971502d2019-04-11 16:36:41 +0200235EXPORT_SYMBOL_GPL(nf_queue);
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100236
Pablo Neira Ayuso26dfab72016-11-03 10:56:39 +0100237static unsigned int nf_iterate(struct sk_buff *skb,
238 struct nf_hook_state *state,
Aaron Conole960632e2017-08-24 00:08:32 +0200239 const struct nf_hook_entries *hooks,
240 unsigned int *index)
Pablo Neira Ayuso26dfab72016-11-03 10:56:39 +0100241{
Aaron Conole960632e2017-08-24 00:08:32 +0200242 const struct nf_hook_entry *hook;
243 unsigned int verdict, i = *index;
Pablo Neira Ayuso26dfab72016-11-03 10:56:39 +0100244
Aaron Conole960632e2017-08-24 00:08:32 +0200245 while (i < hooks->num_hook_entries) {
246 hook = &hooks->hooks[i];
Pablo Neira Ayuso26dfab72016-11-03 10:56:39 +0100247repeat:
Aaron Conole960632e2017-08-24 00:08:32 +0200248 verdict = nf_hook_entry_hookfn(hook, skb, state);
Pablo Neira Ayuso26dfab72016-11-03 10:56:39 +0100249 if (verdict != NF_ACCEPT) {
Jagdish Motwani946c0d82019-05-13 23:47:40 +0530250 *index = i;
Pablo Neira Ayuso26dfab72016-11-03 10:56:39 +0100251 if (verdict != NF_REPEAT)
252 return verdict;
253 goto repeat;
254 }
Aaron Conole960632e2017-08-24 00:08:32 +0200255 i++;
256 }
Pablo Neira Ayuso26dfab72016-11-03 10:56:39 +0100257
Aaron Conole960632e2017-08-24 00:08:32 +0200258 *index = i;
Pablo Neira Ayuso26dfab72016-11-03 10:56:39 +0100259 return NF_ACCEPT;
260}
261
Florian Westphalb0f38332017-12-03 00:58:47 +0100262static struct nf_hook_entries *nf_hook_entries_head(const struct net *net, u8 pf, u8 hooknum)
263{
264 switch (pf) {
Florian Westphal2a951832017-12-07 16:28:26 +0100265#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
Florian Westphalb0f38332017-12-03 00:58:47 +0100266 case NFPROTO_BRIDGE:
267 return rcu_dereference(net->nf.hooks_bridge[hooknum]);
Florian Westphal2a951832017-12-07 16:28:26 +0100268#endif
Florian Westphalb0f38332017-12-03 00:58:47 +0100269 case NFPROTO_IPV4:
270 return rcu_dereference(net->nf.hooks_ipv4[hooknum]);
271 case NFPROTO_IPV6:
272 return rcu_dereference(net->nf.hooks_ipv6[hooknum]);
273 default:
274 WARN_ON_ONCE(1);
275 return NULL;
276 }
277
278 return NULL;
279}
280
Aaron Conole960632e2017-08-24 00:08:32 +0200281/* Caller must hold rcu read-side lock */
Patrick McHardy02f014d2007-12-05 01:26:33 -0800282void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
Harald Weltef6ebe772005-08-09 20:21:49 -0700283{
Aaron Conole960632e2017-08-24 00:08:32 +0200284 const struct nf_hook_entry *hook_entry;
285 const struct nf_hook_entries *hooks;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800286 struct sk_buff *skb = entry->skb;
Aaron Conole960632e2017-08-24 00:08:32 +0200287 const struct net *net;
288 unsigned int i;
Florian Westphalf1585082011-01-18 15:27:28 +0100289 int err;
Aaron Conole960632e2017-08-24 00:08:32 +0200290 u8 pf;
291
292 net = entry->state.net;
293 pf = entry->state.pf;
294
Florian Westphalb0f38332017-12-03 00:58:47 +0100295 hooks = nf_hook_entries_head(net, pf, entry->state.hook);
Harald Weltef6ebe772005-08-09 20:21:49 -0700296
Aaron Conole960632e2017-08-24 00:08:32 +0200297 i = entry->hook_index;
Florian Westphalb0f38332017-12-03 00:58:47 +0100298 if (WARN_ON_ONCE(!hooks || i >= hooks->num_hook_entries)) {
Aaron Conole960632e2017-08-24 00:08:32 +0200299 kfree_skb(skb);
Florian Westphalaf370ab2020-03-27 03:24:48 +0100300 nf_queue_entry_free(entry);
Aaron Conole960632e2017-08-24 00:08:32 +0200301 return;
302 }
303
304 hook_entry = &hooks->hooks[i];
305
Harald Weltef6ebe772005-08-09 20:21:49 -0700306 /* Continue traversal iff userspace said ok... */
Florian Westphal7ceebfe2015-10-09 13:10:37 +0200307 if (verdict == NF_REPEAT)
Aaron Conole0aa8c572016-11-15 17:48:44 -0500308 verdict = nf_hook_entry_hookfn(hook_entry, skb, &entry->state);
Harald Weltef6ebe772005-08-09 20:21:49 -0700309
310 if (verdict == NF_ACCEPT) {
Pablo Neira Ayusoce388f42017-11-27 22:50:26 +0100311 if (nf_reroute(skb, entry) < 0)
Patrick McHardy7a11b982006-02-27 13:03:24 -0800312 verdict = NF_DROP;
313 }
314
315 if (verdict == NF_ACCEPT) {
Pablo Neira Ayuso7034b562016-10-17 18:05:32 +0100316next_hook:
Aaron Conole960632e2017-08-24 00:08:32 +0200317 ++i;
318 verdict = nf_iterate(skb, &entry->state, hooks, &i);
Harald Weltef6ebe772005-08-09 20:21:49 -0700319 }
320
321 switch (verdict & NF_VERDICT_MASK) {
322 case NF_ACCEPT:
Patrick McHardy3bc38712006-07-24 22:52:47 -0700323 case NF_STOP:
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800324 local_bh_disable();
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500325 entry->state.okfn(entry->state.net, entry->state.sk, skb);
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800326 local_bh_enable();
Harald Weltef6ebe772005-08-09 20:21:49 -0700327 break;
Harald Weltef6ebe772005-08-09 20:21:49 -0700328 case NF_QUEUE:
Florian Westphal0d9cb302019-07-02 20:41:14 +0200329 err = nf_queue(skb, &entry->state, i, verdict);
Aaron Conole960632e2017-08-24 00:08:32 +0200330 if (err == 1)
331 goto next_hook;
Harald Weltef6ebe772005-08-09 20:21:49 -0700332 break;
Eric Dumazet64507fd2010-02-19 15:28:38 +0100333 case NF_STOLEN:
Julian Anastasovfad54442011-08-05 00:36:28 +0000334 break;
Patrick McHardy3bc38712006-07-24 22:52:47 -0700335 default:
336 kfree_skb(skb);
Harald Weltef6ebe772005-08-09 20:21:49 -0700337 }
Florian Westphal81b43252015-10-13 14:33:28 +0200338
Florian Westphalaf370ab2020-03-27 03:24:48 +0100339 nf_queue_entry_free(entry);
Harald Weltef6ebe772005-08-09 20:21:49 -0700340}
341EXPORT_SYMBOL(nf_reinject);