xref: /linux/net/netfilter/nf_queue.c (revision 68993ced0f618e36cf33388f1e50223e5e6e78cc)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Rusty Russell (C)2000 -- This code is GPL.
4  * Patrick McHardy (c) 2006-2012
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/proc_fs.h>
12 #include <linux/skbuff.h>
13 #include <linux/netfilter.h>
14 #include <linux/netfilter_ipv4.h>
15 #include <linux/netfilter_ipv6.h>
16 #include <linux/netfilter_bridge.h>
17 #include <linux/seq_file.h>
18 #include <linux/rcupdate.h>
19 #include <net/protocol.h>
20 #include <net/netfilter/nf_queue.h>
21 #include <net/dst.h>
22 
23 #include "nf_internals.h"
24 
25 static const struct nf_queue_handler __rcu *nf_queue_handler;
26 
27 /*
28  * Hook for nfnetlink_queue to register its queue handler.
29  * We do this so that most of the NFQUEUE code can be modular.
30  *
31  * Once the queue is registered it must reinject all packets it
32  * receives, no matter what.
33  */
34 
nf_register_queue_handler(const struct nf_queue_handler * qh)35 void nf_register_queue_handler(const struct nf_queue_handler *qh)
36 {
37 	/* should never happen, we only have one queueing backend in kernel */
38 	WARN_ON(rcu_access_pointer(nf_queue_handler));
39 	rcu_assign_pointer(nf_queue_handler, qh);
40 }
41 EXPORT_SYMBOL(nf_register_queue_handler);
42 
43 /* The caller must flush their queue before this */
nf_unregister_queue_handler(void)44 void nf_unregister_queue_handler(void)
45 {
46 	RCU_INIT_POINTER(nf_queue_handler, NULL);
47 }
48 EXPORT_SYMBOL(nf_unregister_queue_handler);
49 
nf_queue_sock_put(struct sock * sk)50 static void nf_queue_sock_put(struct sock *sk)
51 {
52 #ifdef CONFIG_INET
53 	sock_gen_put(sk);
54 #else
55 	sock_put(sk);
56 #endif
57 }
58 
nf_queue_entry_release_refs(struct nf_queue_entry * entry)59 static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
60 {
61 	struct nf_hook_state *state = &entry->state;
62 
63 	/* Release those devices we held, or Alexey will kill me. */
64 	dev_put(entry->skb_dev);
65 	dev_put(state->in);
66 	dev_put(state->out);
67 	if (state->sk)
68 		nf_queue_sock_put(state->sk);
69 
70 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
71 	dev_put(entry->physin);
72 	dev_put(entry->physout);
73 #endif
74 }
75 
nf_queue_entry_free(struct nf_queue_entry * entry)76 void nf_queue_entry_free(struct nf_queue_entry *entry)
77 {
78 	nf_queue_entry_release_refs(entry);
79 	kfree(entry);
80 }
81 EXPORT_SYMBOL_GPL(nf_queue_entry_free);
82 
__nf_queue_entry_init_physdevs(struct nf_queue_entry * entry)83 static void __nf_queue_entry_init_physdevs(struct nf_queue_entry *entry)
84 {
85 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
86 	const struct sk_buff *skb = entry->skb;
87 
88 	if (nf_bridge_info_exists(skb)) {
89 		entry->physin = nf_bridge_get_physindev(skb, entry->state.net);
90 		entry->physout = nf_bridge_get_physoutdev(skb);
91 	} else {
92 		entry->physin = NULL;
93 		entry->physout = NULL;
94 	}
95 #endif
96 }
97 
98 /* Bump dev refs so they don't vanish while packet is out */
nf_queue_entry_get_refs(struct nf_queue_entry * entry)99 bool nf_queue_entry_get_refs(struct nf_queue_entry *entry)
100 {
101 	struct nf_hook_state *state = &entry->state;
102 
103 	if (state->sk && !refcount_inc_not_zero(&state->sk->sk_refcnt))
104 		return false;
105 
106 	dev_hold(entry->skb_dev);
107 	dev_hold(state->in);
108 	dev_hold(state->out);
109 
110 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
111 	dev_hold(entry->physin);
112 	dev_hold(entry->physout);
113 #endif
114 	return true;
115 }
116 EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
117 
nf_queue_nf_hook_drop(struct net * net)118 void nf_queue_nf_hook_drop(struct net *net)
119 {
120 	const struct nf_queue_handler *qh;
121 
122 	rcu_read_lock();
123 	qh = rcu_dereference(nf_queue_handler);
124 	if (qh)
125 		qh->nf_hook_drop(net);
126 	rcu_read_unlock();
127 }
128 EXPORT_SYMBOL_GPL(nf_queue_nf_hook_drop);
129 
nf_ip_saveroute(const struct sk_buff * skb,struct nf_queue_entry * entry)130 static void nf_ip_saveroute(const struct sk_buff *skb,
131 			    struct nf_queue_entry *entry)
132 {
133 	struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
134 
135 	if (entry->state.hook == NF_INET_LOCAL_OUT) {
136 		const struct iphdr *iph = ip_hdr(skb);
137 
138 		rt_info->tos = iph->tos;
139 		rt_info->daddr = iph->daddr;
140 		rt_info->saddr = iph->saddr;
141 		rt_info->mark = skb->mark;
142 	}
143 }
144 
nf_ip6_saveroute(const struct sk_buff * skb,struct nf_queue_entry * entry)145 static void nf_ip6_saveroute(const struct sk_buff *skb,
146 			     struct nf_queue_entry *entry)
147 {
148 	struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
149 
150 	if (entry->state.hook == NF_INET_LOCAL_OUT) {
151 		const struct ipv6hdr *iph = ipv6_hdr(skb);
152 
153 		rt_info->daddr = iph->daddr;
154 		rt_info->saddr = iph->saddr;
155 		rt_info->mark = skb->mark;
156 	}
157 }
158 
__nf_queue(struct sk_buff * skb,const struct nf_hook_state * state,unsigned int index,unsigned int queuenum)159 static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
160 		      unsigned int index, unsigned int queuenum)
161 {
162 	struct nf_queue_entry *entry = NULL;
163 	const struct nf_queue_handler *qh;
164 	unsigned int route_key_size;
165 	int status;
166 
167 	/* QUEUE == DROP if no one is waiting, to be safe. */
168 	qh = rcu_dereference(nf_queue_handler);
169 	if (!qh)
170 		return -ESRCH;
171 
172 	switch (state->pf) {
173 	case AF_INET:
174 		route_key_size = sizeof(struct ip_rt_info);
175 		break;
176 	case AF_INET6:
177 		route_key_size = sizeof(struct ip6_rt_info);
178 		break;
179 	default:
180 		route_key_size = 0;
181 		break;
182 	}
183 
184 	if (skb_sk_is_prefetched(skb)) {
185 		struct sock *sk = skb->sk;
186 
187 		if (!sk_is_refcounted(sk)) {
188 			if (!refcount_inc_not_zero(&sk->sk_refcnt))
189 				return -ENOTCONN;
190 
191 			/* drop refcount on skb_orphan */
192 			skb->destructor = sock_edemux;
193 		}
194 	}
195 
196 	entry = kmalloc(sizeof(*entry) + route_key_size, GFP_ATOMIC);
197 	if (!entry)
198 		return -ENOMEM;
199 
200 	if (skb_dst(skb) && !skb_dst_force(skb)) {
201 		kfree(entry);
202 		return -ENETDOWN;
203 	}
204 
205 	*entry = (struct nf_queue_entry) {
206 		.skb	= skb,
207 		.skb_dev = skb->dev,
208 		.state	= *state,
209 		.hook_index = index,
210 		.size	= sizeof(*entry) + route_key_size,
211 	};
212 	__nf_queue_entry_init_physdevs(entry);
213 
214 	if (!nf_queue_entry_get_refs(entry)) {
215 		kfree(entry);
216 		return -ENOTCONN;
217 	}
218 
219 	switch (entry->state.pf) {
220 	case AF_INET:
221 		nf_ip_saveroute(skb, entry);
222 		break;
223 	case AF_INET6:
224 		nf_ip6_saveroute(skb, entry);
225 		break;
226 	}
227 
228 	status = qh->outfn(entry, queuenum);
229 	if (status < 0) {
230 		nf_queue_entry_free(entry);
231 		return status;
232 	}
233 
234 	return 0;
235 }
236 
237 /* Packets leaving via this function must come back through nf_reinject(). */
nf_queue(struct sk_buff * skb,struct nf_hook_state * state,unsigned int index,unsigned int verdict)238 int nf_queue(struct sk_buff *skb, struct nf_hook_state *state,
239 	     unsigned int index, unsigned int verdict)
240 {
241 	int ret;
242 
243 	ret = __nf_queue(skb, state, index, verdict >> NF_VERDICT_QBITS);
244 	if (ret < 0) {
245 		if (ret == -ESRCH &&
246 		    (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
247 			return 1;
248 		kfree_skb(skb);
249 	}
250 
251 	return 0;
252 }
253 EXPORT_SYMBOL_GPL(nf_queue);
254