]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/netfilter/nf_queue.c
Merge tag 'drm-intel-fixes-2020-08-20' of git://anongit.freedesktop.org/drm/drm-intel...
[mirror_ubuntu-jammy-kernel.git] / net / netfilter / nf_queue.c
CommitLineData
f229f6ce
PM
1/*
2 * Rusty Russell (C)2000 -- This code is GPL.
3 * Patrick McHardy (c) 2006-2012
4 */
5
f6ebe77f 6#include <linux/kernel.h>
5a0e3ad6 7#include <linux/slab.h>
f6ebe77f
HW
8#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>
7db9a51e
PNA
13#include <linux/netfilter_ipv4.h>
14#include <linux/netfilter_ipv6.h>
c737b7c4 15#include <linux/netfilter_bridge.h>
bbd86b9f 16#include <linux/seq_file.h>
7a11b984 17#include <linux/rcupdate.h>
f6ebe77f 18#include <net/protocol.h>
c01cd429 19#include <net/netfilter/nf_queue.h>
7fee226a 20#include <net/dst.h>
f6ebe77f
HW
21
22#include "nf_internals.h"
23
601e68e1 24/*
0360ae41
FW
25 * 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.
f6ebe77f 30 */
f6ebe77f 31
d72367b6
HW
32/* return EBUSY when somebody else is registered, return EEXIST if the
33 * same handler is registered, return 0 in case of success. */
dc3ee32e 34void nf_register_queue_handler(struct net *net, const struct nf_queue_handler *qh)
601e68e1 35{
0360ae41 36 /* should never happen, we only have one queueing backend in kernel */
dc3ee32e
EB
37 WARN_ON(rcu_access_pointer(net->nf.queue_handler));
38 rcu_assign_pointer(net->nf.queue_handler, qh);
f6ebe77f
HW
39}
40EXPORT_SYMBOL(nf_register_queue_handler);
41
42/* The caller must flush their queue before this */
dc3ee32e 43void nf_unregister_queue_handler(struct net *net)
f6ebe77f 44{
dc3ee32e 45 RCU_INIT_POINTER(net->nf.queue_handler, NULL);
f6ebe77f
HW
46}
47EXPORT_SYMBOL(nf_unregister_queue_handler);
48
dd3cc111 49static void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
daaa8be2 50{
1d1de89b
DM
51 struct nf_hook_state *state = &entry->state;
52
daaa8be2 53 /* Release those devices we held, or Alexey will kill me. */
1d1de89b
DM
54 if (state->in)
55 dev_put(state->in);
56 if (state->out)
57 dev_put(state->out);
1c984f8a
DM
58 if (state->sk)
59 sock_put(state->sk);
c4b0e771 60
119e52e6
FW
61#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
c4b0e771 67}
dd3cc111
FW
68
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);
c4b0e771 75
119e52e6 76static void __nf_queue_entry_init_physdevs(struct nf_queue_entry *entry)
c4b0e771 77{
1109a90c 78#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
119e52e6
FW
79 const struct sk_buff *skb = entry->skb;
80 struct nf_bridge_info *nf_bridge;
c4b0e771 81
119e52e6 82 nf_bridge = nf_bridge_info_get(skb);
c4b0e771 83 if (nf_bridge) {
119e52e6
FW
84 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;
daaa8be2
PM
89 }
90#endif
daaa8be2
PM
91}
92
4bd60443 93/* Bump dev refs so they don't vanish while packet is out */
ed78d09d 94void nf_queue_entry_get_refs(struct nf_queue_entry *entry)
4bd60443 95{
1d1de89b
DM
96 struct nf_hook_state *state = &entry->state;
97
1d1de89b
DM
98 if (state->in)
99 dev_hold(state->in);
100 if (state->out)
101 dev_hold(state->out);
1c984f8a
DM
102 if (state->sk)
103 sock_hold(state->sk);
4bd60443 104
119e52e6
FW
105#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
4bd60443 111}
a5fedd43 112EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
4bd60443 113
26888dfd 114void nf_queue_nf_hook_drop(struct net *net)
8405a8ff
EB
115{
116 const struct nf_queue_handler *qh;
8405a8ff 117
8405a8ff 118 rcu_read_lock();
dc3ee32e 119 qh = rcu_dereference(net->nf.queue_handler);
2385eb0c 120 if (qh)
26888dfd 121 qh->nf_hook_drop(net);
8405a8ff 122 rcu_read_unlock();
8405a8ff 123}
e2a75007 124EXPORT_SYMBOL_GPL(nf_queue_nf_hook_drop);
8405a8ff 125
7db9a51e
PNA
126static 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
7034b566 155static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
960632ec 156 unsigned int index, unsigned int queuenum)
f6ebe77f 157{
daaa8be2 158 struct nf_queue_entry *entry = NULL;
e3ac5298 159 const struct nf_queue_handler *qh;
dc3ee32e 160 struct net *net = state->net;
46435623 161 unsigned int route_key_size;
28f715b9 162 int status;
f6ebe77f 163
25985edc 164 /* QUEUE == DROP if no one is waiting, to be safe. */
dc3ee32e 165 qh = rcu_dereference(net->nf.queue_handler);
28f715b9
FW
166 if (!qh)
167 return -ESRCH;
f6ebe77f 168
46435623
PNA
169 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 }
bce8032e 180
46435623 181 entry = kmalloc(sizeof(*entry) + route_key_size, GFP_ATOMIC);
28f715b9
FW
182 if (!entry)
183 return -ENOMEM;
f6ebe77f 184
0b9173f4 185 if (skb_dst(skb) && !skb_dst_force(skb)) {
28f715b9
FW
186 kfree(entry);
187 return -ENETDOWN;
b60a7738
FW
188 }
189
02f014d8
PM
190 *entry = (struct nf_queue_entry) {
191 .skb = skb,
1d1de89b 192 .state = *state,
960632ec 193 .hook_index = index,
46435623 194 .size = sizeof(*entry) + route_key_size,
02f014d8 195 };
f6ebe77f 196
119e52e6
FW
197 __nf_queue_entry_init_physdevs(entry);
198
ed78d09d 199 nf_queue_entry_get_refs(entry);
7db9a51e
PNA
200
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
02f014d8 210 status = qh->outfn(entry, queuenum);
f6ebe77f 211 if (status < 0) {
28f715b9
FW
212 nf_queue_entry_free(entry);
213 return status;
f6ebe77f
HW
214 }
215
f1585086 216 return 0;
f6ebe77f
HW
217}
218
7034b566
PNA
219/* Packets leaving via this function must come back through nf_reinject(). */
220int nf_queue(struct sk_buff *skb, struct nf_hook_state *state,
0d9cb300 221 unsigned int index, unsigned int verdict)
7034b566 222{
7034b566
PNA
223 int ret;
224
0d9cb300 225 ret = __nf_queue(skb, state, index, verdict >> NF_VERDICT_QBITS);
7034b566
PNA
226 if (ret < 0) {
227 if (ret == -ESRCH &&
960632ec 228 (verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
7034b566 229 return 1;
7034b566
PNA
230 kfree_skb(skb);
231 }
232
233 return 0;
234}
971502d7 235EXPORT_SYMBOL_GPL(nf_queue);
7034b566 236
26dfab72
PNA
237static unsigned int nf_iterate(struct sk_buff *skb,
238 struct nf_hook_state *state,
960632ec
AC
239 const struct nf_hook_entries *hooks,
240 unsigned int *index)
26dfab72 241{
960632ec
AC
242 const struct nf_hook_entry *hook;
243 unsigned int verdict, i = *index;
26dfab72 244
960632ec
AC
245 while (i < hooks->num_hook_entries) {
246 hook = &hooks->hooks[i];
26dfab72 247repeat:
960632ec 248 verdict = nf_hook_entry_hookfn(hook, skb, state);
26dfab72 249 if (verdict != NF_ACCEPT) {
946c0d8e 250 *index = i;
26dfab72
PNA
251 if (verdict != NF_REPEAT)
252 return verdict;
253 goto repeat;
254 }
960632ec
AC
255 i++;
256 }
26dfab72 257
960632ec 258 *index = i;
26dfab72
PNA
259 return NF_ACCEPT;
260}
261
b0f38338
FW
262static struct nf_hook_entries *nf_hook_entries_head(const struct net *net, u8 pf, u8 hooknum)
263{
264 switch (pf) {
2a95183a 265#ifdef CONFIG_NETFILTER_FAMILY_BRIDGE
b0f38338
FW
266 case NFPROTO_BRIDGE:
267 return rcu_dereference(net->nf.hooks_bridge[hooknum]);
2a95183a 268#endif
b0f38338
FW
269 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
960632ec 281/* Caller must hold rcu read-side lock */
02f014d8 282void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
f6ebe77f 283{
960632ec
AC
284 const struct nf_hook_entry *hook_entry;
285 const struct nf_hook_entries *hooks;
02f014d8 286 struct sk_buff *skb = entry->skb;
960632ec
AC
287 const struct net *net;
288 unsigned int i;
f1585086 289 int err;
960632ec
AC
290 u8 pf;
291
292 net = entry->state.net;
293 pf = entry->state.pf;
294
b0f38338 295 hooks = nf_hook_entries_head(net, pf, entry->state.hook);
f6ebe77f 296
960632ec 297 i = entry->hook_index;
b0f38338 298 if (WARN_ON_ONCE(!hooks || i >= hooks->num_hook_entries)) {
960632ec 299 kfree_skb(skb);
af370ab3 300 nf_queue_entry_free(entry);
960632ec
AC
301 return;
302 }
303
304 hook_entry = &hooks->hooks[i];
305
f6ebe77f 306 /* Continue traversal iff userspace said ok... */
7ceebfe4 307 if (verdict == NF_REPEAT)
0aa8c57a 308 verdict = nf_hook_entry_hookfn(hook_entry, skb, &entry->state);
f6ebe77f 309
7a11b984 310 if (verdict == NF_ACCEPT) {
ce388f45 311 if (nf_reroute(skb, entry) < 0)
7a11b984
PM
312 verdict = NF_DROP;
313 }
314
f6ebe77f 315 if (verdict == NF_ACCEPT) {
7034b566 316next_hook:
960632ec
AC
317 ++i;
318 verdict = nf_iterate(skb, &entry->state, hooks, &i);
f6ebe77f
HW
319 }
320
321 switch (verdict & NF_VERDICT_MASK) {
322 case NF_ACCEPT:
3bc38712 323 case NF_STOP:
4b3d15ef 324 local_bh_disable();
0c4b51f0 325 entry->state.okfn(entry->state.net, entry->state.sk, skb);
4b3d15ef 326 local_bh_enable();
f6ebe77f 327 break;
f6ebe77f 328 case NF_QUEUE:
0d9cb300 329 err = nf_queue(skb, &entry->state, i, verdict);
960632ec
AC
330 if (err == 1)
331 goto next_hook;
f6ebe77f 332 break;
64507fdb 333 case NF_STOLEN:
fad54440 334 break;
3bc38712
PM
335 default:
336 kfree_skb(skb);
f6ebe77f 337 }
81b4325e 338
af370ab3 339 nf_queue_entry_free(entry);
f6ebe77f
HW
340}
341EXPORT_SYMBOL(nf_reinject);