]> git.ipfire.org Git - people/ms/linux.git/blame - net/netfilter/nfnetlink_queue.c
NFQUEUE: Hold RCU read lock while calling nf_reinject
[people/ms/linux.git] / net / netfilter / nfnetlink_queue.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
7af4cc3f
HW
2/*
3 * This is a module which is used for queueing packets and communicating with
67137f3c 4 * userspace via nfnetlink.
7af4cc3f
HW
5 *
6 * (C) 2005 by Harald Welte <laforge@netfilter.org>
4ad9d4fa 7 * (C) 2007 by Patrick McHardy <kaber@trash.net>
7af4cc3f
HW
8 *
9 * Based on the old ipv4-only ip_queue.c:
10 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
11 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
7af4cc3f 12 */
5191d70f
AS
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
7af4cc3f
HW
16#include <linux/module.h>
17#include <linux/skbuff.h>
18#include <linux/init.h>
19#include <linux/spinlock.h>
5a0e3ad6 20#include <linux/slab.h>
7af4cc3f
HW
21#include <linux/notifier.h>
22#include <linux/netdevice.h>
23#include <linux/netfilter.h>
838ab636 24#include <linux/proc_fs.h>
7af4cc3f
HW
25#include <linux/netfilter_ipv4.h>
26#include <linux/netfilter_ipv6.h>
c737b7c4 27#include <linux/netfilter_bridge.h>
7af4cc3f
HW
28#include <linux/netfilter/nfnetlink.h>
29#include <linux/netfilter/nfnetlink_queue.h>
b7bd1809 30#include <linux/netfilter/nf_conntrack_common.h>
7af4cc3f
HW
31#include <linux/list.h>
32#include <net/sock.h>
83111e7f 33#include <net/tcp_states.h>
c01cd429 34#include <net/netfilter/nf_queue.h>
e8179610 35#include <net/netns/generic.h>
7af4cc3f 36
60063497 37#include <linux/atomic.h>
7af4cc3f 38
1109a90c 39#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
fbcd923c
HW
40#include "../bridge/br_private.h"
41#endif
42
5da773a3
FW
43#if IS_ENABLED(CONFIG_NF_CONNTRACK)
44#include <net/netfilter/nf_conntrack.h>
45#endif
46
7af4cc3f
HW
47#define NFQNL_QMAX_DEFAULT 1024
48
9cefbbc9
FW
49/* We're using struct nlattr which has 16bit nla_len. Note that nla_len
50 * includes the header length. Thus, the maximum packet length that we
51 * support is 65531 bytes. We send truncated packets if the specified length
52 * is larger than that. Userspace can check for presence of NFQA_CAP_LEN
53 * attribute to detect truncation.
54 */
55#define NFQNL_MAX_COPY_RANGE (0xffff - NLA_HDRLEN)
56
7af4cc3f
HW
57struct nfqnl_instance {
58 struct hlist_node hlist; /* global list of queues */
9872bec7 59 struct rcu_head rcu;
7af4cc3f 60
cc6bc448 61 u32 peer_portid;
7af4cc3f
HW
62 unsigned int queue_maxlen;
63 unsigned int copy_range;
7af4cc3f
HW
64 unsigned int queue_dropped;
65 unsigned int queue_user_dropped;
66
7af4cc3f
HW
67
68 u_int16_t queue_num; /* number of this queue */
69 u_int8_t copy_mode;
fdb694a0 70 u_int32_t flags; /* Set using NFQA_CFG_FLAGS */
c463ac97
ED
71/*
72 * Following fields are dirtied for each queued packet,
73 * keep them in same cache line if possible.
74 */
886bc503 75 spinlock_t lock ____cacheline_aligned_in_smp;
c463ac97 76 unsigned int queue_total;
5863702a 77 unsigned int id_sequence; /* 'sequence' of pkt ids */
7af4cc3f
HW
78 struct list_head queue_list; /* packets in queue */
79};
80
02f014d8 81typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
7af4cc3f 82
c7d03a00 83static unsigned int nfnl_queue_net_id __read_mostly;
7af4cc3f 84
7af4cc3f 85#define INSTANCE_BUCKETS 16
e8179610
G
86struct nfnl_queue_net {
87 spinlock_t instances_lock;
88 struct hlist_head instance_table[INSTANCE_BUCKETS];
89};
90
91static struct nfnl_queue_net *nfnl_queue_pernet(struct net *net)
92{
93 return net_generic(net, nfnl_queue_net_id);
94}
7af4cc3f
HW
95
96static inline u_int8_t instance_hashfn(u_int16_t queue_num)
97{
1cdb0905 98 return ((queue_num >> 8) ^ queue_num) % INSTANCE_BUCKETS;
7af4cc3f
HW
99}
100
101static struct nfqnl_instance *
e8179610 102instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num)
7af4cc3f
HW
103{
104 struct hlist_head *head;
7af4cc3f
HW
105 struct nfqnl_instance *inst;
106
e8179610 107 head = &q->instance_table[instance_hashfn(queue_num)];
b67bfe0d 108 hlist_for_each_entry_rcu(inst, head, hlist) {
7af4cc3f
HW
109 if (inst->queue_num == queue_num)
110 return inst;
111 }
112 return NULL;
113}
114
7af4cc3f 115static struct nfqnl_instance *
cc6bc448 116instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, u32 portid)
7af4cc3f 117{
baab2ce7 118 struct nfqnl_instance *inst;
9872bec7 119 unsigned int h;
baab2ce7 120 int err;
7af4cc3f 121
e8179610
G
122 spin_lock(&q->instances_lock);
123 if (instance_lookup(q, queue_num)) {
baab2ce7 124 err = -EEXIST;
7af4cc3f 125 goto out_unlock;
baab2ce7 126 }
7af4cc3f 127
10dfdc69 128 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
baab2ce7
PM
129 if (!inst) {
130 err = -ENOMEM;
7af4cc3f 131 goto out_unlock;
baab2ce7 132 }
7af4cc3f 133
7af4cc3f 134 inst->queue_num = queue_num;
15e47304 135 inst->peer_portid = portid;
7af4cc3f 136 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
9cefbbc9 137 inst->copy_range = NFQNL_MAX_COPY_RANGE;
7af4cc3f 138 inst->copy_mode = NFQNL_COPY_NONE;
181a46a5 139 spin_lock_init(&inst->lock);
7af4cc3f
HW
140 INIT_LIST_HEAD(&inst->queue_list);
141
baab2ce7
PM
142 if (!try_module_get(THIS_MODULE)) {
143 err = -EAGAIN;
7af4cc3f 144 goto out_free;
baab2ce7 145 }
7af4cc3f 146
9872bec7 147 h = instance_hashfn(queue_num);
e8179610 148 hlist_add_head_rcu(&inst->hlist, &q->instance_table[h]);
7af4cc3f 149
e8179610 150 spin_unlock(&q->instances_lock);
7af4cc3f 151
7af4cc3f
HW
152 return inst;
153
154out_free:
155 kfree(inst);
156out_unlock:
e8179610 157 spin_unlock(&q->instances_lock);
baab2ce7 158 return ERR_PTR(err);
7af4cc3f
HW
159}
160
b43d8d85
PM
161static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
162 unsigned long data);
7af4cc3f
HW
163
164static void
9872bec7 165instance_destroy_rcu(struct rcu_head *head)
7af4cc3f 166{
9872bec7
PM
167 struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
168 rcu);
7af4cc3f 169
b43d8d85 170 nfqnl_flush(inst, NULL, 0);
9872bec7 171 kfree(inst);
7af4cc3f
HW
172 module_put(THIS_MODULE);
173}
174
9872bec7 175static void
7af4cc3f
HW
176__instance_destroy(struct nfqnl_instance *inst)
177{
9872bec7
PM
178 hlist_del_rcu(&inst->hlist);
179 call_rcu(&inst->rcu, instance_destroy_rcu);
7af4cc3f
HW
180}
181
9872bec7 182static void
e8179610 183instance_destroy(struct nfnl_queue_net *q, struct nfqnl_instance *inst)
7af4cc3f 184{
e8179610 185 spin_lock(&q->instances_lock);
9872bec7 186 __instance_destroy(inst);
e8179610 187 spin_unlock(&q->instances_lock);
7af4cc3f
HW
188}
189
7af4cc3f 190static inline void
02f014d8 191__enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
7af4cc3f 192{
0ac41e81 193 list_add_tail(&entry->list, &queue->queue_list);
7af4cc3f
HW
194 queue->queue_total++;
195}
196
97d32cf9
FW
197static void
198__dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
199{
200 list_del(&entry->list);
201 queue->queue_total--;
202}
203
02f014d8 204static struct nf_queue_entry *
b43d8d85 205find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
7af4cc3f 206{
02f014d8 207 struct nf_queue_entry *entry = NULL, *i;
601e68e1 208
7af4cc3f 209 spin_lock_bh(&queue->lock);
b43d8d85
PM
210
211 list_for_each_entry(i, &queue->queue_list, list) {
212 if (i->id == id) {
213 entry = i;
214 break;
215 }
216 }
217
97d32cf9
FW
218 if (entry)
219 __dequeue_entry(queue, entry);
b43d8d85 220
7af4cc3f
HW
221 spin_unlock_bh(&queue->lock);
222
223 return entry;
224}
225
368982cd
PNA
226static void nfqnl_reinject(struct nf_queue_entry *entry, unsigned int verdict)
227{
228 struct nf_ct_hook *ct_hook;
229 int err;
230
4ecd5474
MT
231 rcu_read_lock();
232
368982cd 233 if (verdict == NF_ACCEPT ||
ad18d7bf 234 verdict == NF_REPEAT ||
368982cd 235 verdict == NF_STOP) {
368982cd
PNA
236 ct_hook = rcu_dereference(nf_ct_hook);
237 if (ct_hook) {
238 err = ct_hook->update(entry->state.net, entry->skb);
239 if (err < 0)
240 verdict = NF_DROP;
241 }
368982cd
PNA
242 }
243 nf_reinject(entry, verdict);
4ecd5474 244 rcu_read_unlock();
368982cd
PNA
245}
246
7af4cc3f 247static void
b43d8d85 248nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
7af4cc3f 249{
02f014d8 250 struct nf_queue_entry *entry, *next;
b43d8d85 251
7af4cc3f 252 spin_lock_bh(&queue->lock);
b43d8d85
PM
253 list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
254 if (!cmpfn || cmpfn(entry, data)) {
255 list_del(&entry->list);
256 queue->queue_total--;
368982cd 257 nfqnl_reinject(entry, NF_DROP);
b43d8d85
PM
258 }
259 }
7af4cc3f
HW
260 spin_unlock_bh(&queue->lock);
261}
262
496e4ae7
FW
263static int
264nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet,
265 bool csum_verify)
7237190d
FW
266{
267 __u32 flags = 0;
268
269 if (packet->ip_summed == CHECKSUM_PARTIAL)
270 flags = NFQA_SKB_CSUMNOTREADY;
496e4ae7
FW
271 else if (csum_verify)
272 flags = NFQA_SKB_CSUM_NOTVERIFIED;
273
7237190d
FW
274 if (skb_is_gso(packet))
275 flags |= NFQA_SKB_GSO;
276
277 return flags ? nla_put_be32(nlskb, NFQA_SKB_INFO, htonl(flags)) : 0;
278}
279
08c0cad6
VG
280static int nfqnl_put_sk_uidgid(struct sk_buff *skb, struct sock *sk)
281{
282 const struct cred *cred;
283
a8399231 284 if (!sk_fullsock(sk))
08c0cad6
VG
285 return 0;
286
287 read_lock_bh(&sk->sk_callback_lock);
288 if (sk->sk_socket && sk->sk_socket->file) {
289 cred = sk->sk_socket->file->f_cred;
290 if (nla_put_be32(skb, NFQA_UID,
291 htonl(from_kuid_munged(&init_user_ns, cred->fsuid))))
292 goto nla_put_failure;
293 if (nla_put_be32(skb, NFQA_GID,
294 htonl(from_kgid_munged(&init_user_ns, cred->fsgid))))
295 goto nla_put_failure;
296 }
297 read_unlock_bh(&sk->sk_callback_lock);
298 return 0;
299
300nla_put_failure:
301 read_unlock_bh(&sk->sk_callback_lock);
302 return -1;
303}
304
ef493bd9
RK
305static u32 nfqnl_get_sk_secctx(struct sk_buff *skb, char **secdata)
306{
307 u32 seclen = 0;
308#if IS_ENABLED(CONFIG_NETWORK_SECMARK)
309 if (!skb || !sk_fullsock(skb->sk))
310 return 0;
311
312 read_lock_bh(&skb->sk->sk_callback_lock);
313
314 if (skb->secmark)
315 security_secid_to_secctx(skb->secmark, secdata, &seclen);
316
317 read_unlock_bh(&skb->sk->sk_callback_lock);
318#endif
319 return seclen;
320}
321
15824ab2
SB
322static u32 nfqnl_get_bridge_size(struct nf_queue_entry *entry)
323{
324 struct sk_buff *entskb = entry->skb;
325 u32 nlalen = 0;
326
327 if (entry->state.pf != PF_BRIDGE || !skb_mac_header_was_set(entskb))
328 return 0;
329
330 if (skb_vlan_tag_present(entskb))
331 nlalen += nla_total_size(nla_total_size(sizeof(__be16)) +
332 nla_total_size(sizeof(__be16)));
333
334 if (entskb->network_header > entskb->mac_header)
335 nlalen += nla_total_size((entskb->network_header -
336 entskb->mac_header));
337
338 return nlalen;
339}
340
341static int nfqnl_put_bridge(struct nf_queue_entry *entry, struct sk_buff *skb)
342{
343 struct sk_buff *entskb = entry->skb;
344
345 if (entry->state.pf != PF_BRIDGE || !skb_mac_header_was_set(entskb))
346 return 0;
347
348 if (skb_vlan_tag_present(entskb)) {
349 struct nlattr *nest;
350
ae0be8de 351 nest = nla_nest_start(skb, NFQA_VLAN);
15824ab2
SB
352 if (!nest)
353 goto nla_put_failure;
354
355 if (nla_put_be16(skb, NFQA_VLAN_TCI, htons(entskb->vlan_tci)) ||
356 nla_put_be16(skb, NFQA_VLAN_PROTO, entskb->vlan_proto))
357 goto nla_put_failure;
358
359 nla_nest_end(skb, nest);
360 }
361
362 if (entskb->mac_header < entskb->network_header) {
363 int len = (int)(entskb->network_header - entskb->mac_header);
364
365 if (nla_put(skb, NFQA_L2HDR, len, skb_mac_header(entskb)))
366 goto nla_put_failure;
367 }
368
369 return 0;
370
371nla_put_failure:
372 return -1;
373}
374
7af4cc3f 375static struct sk_buff *
74332687 376nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
5863702a
ED
377 struct nf_queue_entry *entry,
378 __be32 **packet_id_ptr)
7af4cc3f 379{
7af4cc3f 380 size_t size;
c5b0db32 381 size_t data_len = 0, cap_len = 0;
af2806f8 382 unsigned int hlen = 0;
7af4cc3f 383 struct sk_buff *skb;
5863702a
ED
384 struct nlattr *nla;
385 struct nfqnl_msg_packet_hdr *pmsg;
7af4cc3f 386 struct nlmsghdr *nlh;
3e4ead4f
JJ
387 struct sk_buff *entskb = entry->skb;
388 struct net_device *indev;
389 struct net_device *outdev;
9cb01766 390 struct nf_conn *ct = NULL;
3f649ab7 391 enum ip_conntrack_info ctinfo;
a4b4766c 392 struct nfnl_ct_hook *nfnl_ct;
496e4ae7 393 bool csum_verify;
ef493bd9
RK
394 char *secdata = NULL;
395 u32 seclen = 0;
7af4cc3f 396
7e59b3fe 397 size = nlmsg_total_size(sizeof(struct nfgenmsg))
df6fb868
PM
398 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
399 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
400 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
1109a90c 401#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
df6fb868
PM
402 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
403 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
fbcd923c 404#endif
df6fb868
PM
405 + nla_total_size(sizeof(u_int32_t)) /* mark */
406 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
7237190d 407 + nla_total_size(sizeof(u_int32_t)) /* skbinfo */
ae08ce00
ED
408 + nla_total_size(sizeof(u_int32_t)); /* cap_len */
409
2456e855 410 if (entskb->tstamp)
ae08ce00 411 size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
7af4cc3f 412
15824ab2
SB
413 size += nfqnl_get_bridge_size(entry);
414
1d1de89b
DM
415 if (entry->state.hook <= NF_INET_FORWARD ||
416 (entry->state.hook == NF_INET_POST_ROUTING && entskb->sk == NULL))
496e4ae7
FW
417 csum_verify = !skb_csum_unnecessary(entskb);
418 else
419 csum_verify = false;
420
1d1de89b 421 outdev = entry->state.out;
3e4ead4f 422
14cd5d4a 423 switch ((enum nfqnl_config_mode)READ_ONCE(queue->copy_mode)) {
7af4cc3f
HW
424 case NFQNL_COPY_META:
425 case NFQNL_COPY_NONE:
7af4cc3f 426 break;
601e68e1 427
7af4cc3f 428 case NFQNL_COPY_PACKET:
00bd1cc2
FW
429 if (!(queue->flags & NFQA_CFG_F_GSO) &&
430 entskb->ip_summed == CHECKSUM_PARTIAL &&
c463ac97 431 skb_checksum_help(entskb))
e7dfb09a 432 return NULL;
c463ac97 433
14cd5d4a 434 data_len = READ_ONCE(queue->copy_range);
9cefbbc9 435 if (data_len > entskb->len)
3e4ead4f 436 data_len = entskb->len;
601e68e1 437
af2806f8
TG
438 hlen = skb_zerocopy_headlen(entskb);
439 hlen = min_t(unsigned int, hlen, data_len);
ae08ce00 440 size += sizeof(struct nlattr) + hlen;
6ee584be 441 cap_len = entskb->len;
7af4cc3f 442 break;
7af4cc3f
HW
443 }
444
8e662164
AB
445 nfnl_ct = rcu_dereference(nfnl_ct_hook);
446
83ace77f 447#if IS_ENABLED(CONFIG_NF_CONNTRACK)
b7bd1809 448 if (queue->flags & NFQA_CFG_F_CONNTRACK) {
a4b4766c 449 if (nfnl_ct != NULL) {
83ace77f 450 ct = nf_ct_get(entskb, &ctinfo);
b7bd1809 451 if (ct != NULL)
a4b4766c 452 size += nfnl_ct->build_size(ct);
b7bd1809
PNA
453 }
454 }
83ace77f 455#endif
7af4cc3f 456
08c0cad6 457 if (queue->flags & NFQA_CFG_F_UID_GID) {
7e59b3fe 458 size += (nla_total_size(sizeof(u_int32_t)) /* uid */
08c0cad6
VG
459 + nla_total_size(sizeof(u_int32_t))); /* gid */
460 }
461
ef493bd9
RK
462 if ((queue->flags & NFQA_CFG_F_SECCTX) && entskb->sk) {
463 seclen = nfqnl_get_sk_secctx(entskb, &secdata);
464 if (seclen)
465 size += nla_total_size(seclen);
466 }
467
c5b0db32 468 skb = alloc_skb(size, GFP_ATOMIC);
36d5fe6a
ZK
469 if (!skb) {
470 skb_tx_error(entskb);
77c1c03c 471 goto nlmsg_failure;
36d5fe6a 472 }
601e68e1 473
19c28b13
PNA
474 nlh = nfnl_msg_put(skb, 0, 0,
475 nfnl_msg_type(NFNL_SUBSYS_QUEUE, NFQNL_MSG_PACKET),
476 0, entry->state.pf, NFNETLINK_V0,
477 htons(queue->queue_num));
3da07c0c 478 if (!nlh) {
36d5fe6a 479 skb_tx_error(entskb);
3da07c0c 480 kfree_skb(skb);
77c1c03c 481 goto nlmsg_failure;
3da07c0c 482 }
7af4cc3f 483
5863702a
ED
484 nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));
485 pmsg = nla_data(nla);
486 pmsg->hw_protocol = entskb->protocol;
1d1de89b 487 pmsg->hook = entry->state.hook;
5863702a 488 *packet_id_ptr = &pmsg->packet_id;
7af4cc3f 489
1d1de89b 490 indev = entry->state.in;
3e4ead4f 491 if (indev) {
1109a90c 492#if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
a447189e
DM
493 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
494 goto nla_put_failure;
fbcd923c 495#else
1d1de89b 496 if (entry->state.pf == PF_BRIDGE) {
fbcd923c 497 /* Case 1: indev is physical input device, we need to
601e68e1 498 * look for bridge group (when called from
fbcd923c 499 * netfilter_bridge) */
a447189e
DM
500 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
501 htonl(indev->ifindex)) ||
fbcd923c 502 /* this is the bridge group "brX" */
f350a0a8 503 /* rcu_read_lock()ed by __nf_queue */
a447189e
DM
504 nla_put_be32(skb, NFQA_IFINDEX_INDEV,
505 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
506 goto nla_put_failure;
fbcd923c 507 } else {
c737b7c4
FW
508 int physinif;
509
fbcd923c
HW
510 /* Case 2: indev is bridge group, we need to look for
511 * physical device (when called from ipv4) */
a447189e
DM
512 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV,
513 htonl(indev->ifindex)))
514 goto nla_put_failure;
c737b7c4
FW
515
516 physinif = nf_bridge_get_physinif(entskb);
517 if (physinif &&
a447189e 518 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
c737b7c4 519 htonl(physinif)))
a447189e 520 goto nla_put_failure;
fbcd923c
HW
521 }
522#endif
7af4cc3f
HW
523 }
524
3e4ead4f 525 if (outdev) {
1109a90c 526#if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
a447189e
DM
527 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
528 goto nla_put_failure;
fbcd923c 529#else
1d1de89b 530 if (entry->state.pf == PF_BRIDGE) {
fbcd923c 531 /* Case 1: outdev is physical output device, we need to
601e68e1 532 * look for bridge group (when called from
fbcd923c 533 * netfilter_bridge) */
a447189e
DM
534 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
535 htonl(outdev->ifindex)) ||
fbcd923c 536 /* this is the bridge group "brX" */
f350a0a8 537 /* rcu_read_lock()ed by __nf_queue */
a447189e
DM
538 nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
539 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
540 goto nla_put_failure;
fbcd923c 541 } else {
c737b7c4
FW
542 int physoutif;
543
fbcd923c
HW
544 /* Case 2: outdev is bridge group, we need to look for
545 * physical output device (when called from ipv4) */
a447189e
DM
546 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
547 htonl(outdev->ifindex)))
548 goto nla_put_failure;
c737b7c4
FW
549
550 physoutif = nf_bridge_get_physoutif(entskb);
551 if (physoutif &&
a447189e 552 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
c737b7c4 553 htonl(physoutif)))
a447189e 554 goto nla_put_failure;
fbcd923c
HW
555 }
556#endif
7af4cc3f
HW
557 }
558
a447189e
DM
559 if (entskb->mark &&
560 nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark)))
561 goto nla_put_failure;
7af4cc3f 562
2c38de4c 563 if (indev && entskb->dev &&
406b7337
IG
564 skb_mac_header_was_set(entskb) &&
565 skb_mac_header_len(entskb) != 0) {
7af4cc3f 566 struct nfqnl_msg_packet_hw phw;
e4d091d7
DC
567 int len;
568
569 memset(&phw, 0, sizeof(phw));
570 len = dev_parse_header(entskb, phw.hw_addr);
b95cce35
SH
571 if (len) {
572 phw.hw_addrlen = htons(len);
a447189e
DM
573 if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
574 goto nla_put_failure;
b95cce35 575 }
7af4cc3f
HW
576 }
577
15824ab2
SB
578 if (nfqnl_put_bridge(entry, skb) < 0)
579 goto nla_put_failure;
580
916f6efa 581 if (entry->state.hook <= NF_INET_FORWARD && entskb->tstamp) {
7af4cc3f 582 struct nfqnl_msg_packet_timestamp ts;
a7f18845 583 struct timespec64 kts = ktime_to_timespec64(entskb->tstamp);
b28b1e82
PNA
584
585 ts.sec = cpu_to_be64(kts.tv_sec);
586 ts.usec = cpu_to_be64(kts.tv_nsec / NSEC_PER_USEC);
7af4cc3f 587
a447189e
DM
588 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts))
589 goto nla_put_failure;
7af4cc3f
HW
590 }
591
08c0cad6
VG
592 if ((queue->flags & NFQA_CFG_F_UID_GID) && entskb->sk &&
593 nfqnl_put_sk_uidgid(skb, entskb->sk) < 0)
594 goto nla_put_failure;
595
ef493bd9
RK
596 if (seclen && nla_put(skb, NFQA_SECCTX, seclen, secdata))
597 goto nla_put_failure;
598
a4b4766c 599 if (ct && nfnl_ct->build(skb, ct, ctinfo, NFQA_CT, NFQA_CT_INFO) < 0)
ae08ce00
ED
600 goto nla_put_failure;
601
7f87712c
FW
602 if (cap_len > data_len &&
603 nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len)))
ae08ce00
ED
604 goto nla_put_failure;
605
496e4ae7 606 if (nfqnl_put_packet_info(skb, entskb, csum_verify))
7237190d
FW
607 goto nla_put_failure;
608
7af4cc3f 609 if (data_len) {
df6fb868 610 struct nlattr *nla;
7af4cc3f 611
ae08ce00
ED
612 if (skb_tailroom(skb) < sizeof(*nla) + hlen)
613 goto nla_put_failure;
7af4cc3f 614
4df864c1 615 nla = skb_put(skb, sizeof(*nla));
df6fb868 616 nla->nla_type = NFQA_PAYLOAD;
ae08ce00 617 nla->nla_len = nla_attr_size(data_len);
7af4cc3f 618
36d5fe6a
ZK
619 if (skb_zerocopy(skb, entskb, data_len, hlen))
620 goto nla_put_failure;
7af4cc3f 621 }
601e68e1 622
ae08ce00 623 nlh->nlmsg_len = skb->len;
77c1c03c
LZ
624 if (seclen)
625 security_release_secctx(secdata, seclen);
7af4cc3f
HW
626 return skb;
627
df6fb868 628nla_put_failure:
36d5fe6a 629 skb_tx_error(entskb);
a6729955 630 kfree_skb(skb);
e87cc472 631 net_err_ratelimited("nf_queue: error creating packet message\n");
77c1c03c
LZ
632nlmsg_failure:
633 if (seclen)
634 security_release_secctx(secdata, seclen);
7af4cc3f
HW
635 return NULL;
636}
637
5da773a3
FW
638static bool nf_ct_drop_unconfirmed(const struct nf_queue_entry *entry)
639{
640#if IS_ENABLED(CONFIG_NF_CONNTRACK)
641 static const unsigned long flags = IPS_CONFIRMED | IPS_DYING;
642 const struct nf_conn *ct = (void *)skb_nfct(entry->skb);
643
644 if (ct && ((ct->status & flags) == IPS_DYING))
645 return true;
646#endif
647 return false;
648}
649
7af4cc3f 650static int
a5fedd43
FW
651__nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,
652 struct nf_queue_entry *entry)
7af4cc3f 653{
7af4cc3f 654 struct sk_buff *nskb;
f1585086 655 int err = -ENOBUFS;
5863702a 656 __be32 *packet_id_ptr;
fdb694a0 657 int failopen = 0;
7af4cc3f 658
74332687 659 nskb = nfqnl_build_packet_message(net, queue, entry, &packet_id_ptr);
f1585086
FW
660 if (nskb == NULL) {
661 err = -ENOMEM;
0ef0f465 662 goto err_out;
f1585086 663 }
7af4cc3f 664 spin_lock_bh(&queue->lock);
601e68e1 665
5da773a3
FW
666 if (nf_ct_drop_unconfirmed(entry))
667 goto err_out_free_nskb;
668
7af4cc3f 669 if (queue->queue_total >= queue->queue_maxlen) {
fdb694a0
KK
670 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
671 failopen = 1;
672 err = 0;
673 } else {
674 queue->queue_dropped++;
675 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
676 queue->queue_total);
677 }
7af4cc3f
HW
678 goto err_out_free_nskb;
679 }
5863702a
ED
680 entry->id = ++queue->id_sequence;
681 *packet_id_ptr = htonl(entry->id);
7af4cc3f
HW
682
683 /* nfnetlink_unicast will either free the nskb or add it to a socket */
ee921183 684 err = nfnetlink_unicast(nskb, net, queue->peer_portid);
0ef0f465 685 if (err < 0) {
93140113
PNA
686 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
687 failopen = 1;
688 err = 0;
689 } else {
690 queue->queue_user_dropped++;
691 }
7af4cc3f
HW
692 goto err_out_unlock;
693 }
694
695 __enqueue_entry(queue, entry);
696
697 spin_unlock_bh(&queue->lock);
0ef0f465 698 return 0;
7af4cc3f
HW
699
700err_out_free_nskb:
601e68e1 701 kfree_skb(nskb);
7af4cc3f
HW
702err_out_unlock:
703 spin_unlock_bh(&queue->lock);
fdb694a0 704 if (failopen)
368982cd 705 nfqnl_reinject(entry, NF_ACCEPT);
0ef0f465 706err_out:
f1585086 707 return err;
7af4cc3f
HW
708}
709
a5fedd43
FW
710static struct nf_queue_entry *
711nf_queue_entry_dup(struct nf_queue_entry *e)
712{
713 struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC);
dd648bd1
FW
714
715 if (!entry)
716 return NULL;
717
718 if (nf_queue_entry_get_refs(entry))
719 return entry;
720
721 kfree(entry);
722 return NULL;
a5fedd43
FW
723}
724
1109a90c 725#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
a5fedd43
FW
726/* When called from bridge netfilter, skb->data must point to MAC header
727 * before calling skb_gso_segment(). Else, original MAC header is lost
728 * and segmented skbs will be sent to wrong destination.
729 */
730static void nf_bridge_adjust_skb_data(struct sk_buff *skb)
731{
c4b0e771 732 if (nf_bridge_info_get(skb))
a5fedd43
FW
733 __skb_push(skb, skb->network_header - skb->mac_header);
734}
735
736static void nf_bridge_adjust_segmented_data(struct sk_buff *skb)
737{
c4b0e771 738 if (nf_bridge_info_get(skb))
a5fedd43
FW
739 __skb_pull(skb, skb->network_header - skb->mac_header);
740}
741#else
742#define nf_bridge_adjust_skb_data(s) do {} while (0)
743#define nf_bridge_adjust_segmented_data(s) do {} while (0)
744#endif
745
a5fedd43
FW
746static int
747__nfqnl_enqueue_packet_gso(struct net *net, struct nfqnl_instance *queue,
748 struct sk_buff *skb, struct nf_queue_entry *entry)
749{
750 int ret = -ENOMEM;
751 struct nf_queue_entry *entry_seg;
752
753 nf_bridge_adjust_segmented_data(skb);
754
755 if (skb->next == NULL) { /* last packet, no need to copy entry */
756 struct sk_buff *gso_skb = entry->skb;
757 entry->skb = skb;
758 ret = __nfqnl_enqueue_packet(net, queue, entry);
759 if (ret)
760 entry->skb = gso_skb;
761 return ret;
762 }
763
a8305bff 764 skb_mark_not_on_list(skb);
a5fedd43
FW
765
766 entry_seg = nf_queue_entry_dup(entry);
767 if (entry_seg) {
768 entry_seg->skb = skb;
769 ret = __nfqnl_enqueue_packet(net, queue, entry_seg);
770 if (ret)
dd3cc111 771 nf_queue_entry_free(entry_seg);
a5fedd43
FW
772 }
773 return ret;
774}
775
776static int
777nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
778{
779 unsigned int queued;
780 struct nfqnl_instance *queue;
2670ee77 781 struct sk_buff *skb, *segs, *nskb;
a5fedd43 782 int err = -ENOBUFS;
9dff2c96 783 struct net *net = entry->state.net;
a5fedd43
FW
784 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
785
e2361cb9 786 /* rcu_read_lock()ed by nf_hook_thresh */
a5fedd43
FW
787 queue = instance_lookup(q, queuenum);
788 if (!queue)
789 return -ESRCH;
790
791 if (queue->copy_mode == NFQNL_COPY_NONE)
792 return -EINVAL;
793
a5fedd43
FW
794 skb = entry->skb;
795
1d1de89b 796 switch (entry->state.pf) {
a5fedd43
FW
797 case NFPROTO_IPV4:
798 skb->protocol = htons(ETH_P_IP);
799 break;
800 case NFPROTO_IPV6:
801 skb->protocol = htons(ETH_P_IPV6);
802 break;
803 }
804
7b8dfe28
PNA
805 if ((queue->flags & NFQA_CFG_F_GSO) || !skb_is_gso(skb))
806 return __nfqnl_enqueue_packet(net, queue, entry);
807
a5fedd43
FW
808 nf_bridge_adjust_skb_data(skb);
809 segs = skb_gso_segment(skb, 0);
810 /* Does not use PTR_ERR to limit the number of error codes that can be
ed78d09d 811 * returned by nf_queue. For instance, callers rely on -ESRCH to
a5fedd43
FW
812 * mean 'ignore this hook'.
813 */
330966e5 814 if (IS_ERR_OR_NULL(segs))
a5fedd43
FW
815 goto out_err;
816 queued = 0;
817 err = 0;
2670ee77 818 skb_list_walk_safe(segs, segs, nskb) {
a5fedd43
FW
819 if (err == 0)
820 err = __nfqnl_enqueue_packet_gso(net, queue,
821 segs, entry);
822 if (err == 0)
823 queued++;
824 else
825 kfree_skb(segs);
2670ee77 826 }
a5fedd43
FW
827
828 if (queued) {
829 if (err) /* some segments are already queued */
dd3cc111 830 nf_queue_entry_free(entry);
a5fedd43
FW
831 kfree_skb(skb);
832 return 0;
833 }
834 out_err:
835 nf_bridge_adjust_segmented_data(skb);
836 return err;
837}
838
7af4cc3f 839static int
8c88f87c 840nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff)
7af4cc3f 841{
e2b58a67 842 struct sk_buff *nskb;
7af4cc3f 843
d8a585d7
PM
844 if (diff < 0) {
845 if (pskb_trim(e->skb, data_len))
846 return -ENOMEM;
847 } else if (diff > 0) {
7af4cc3f
HW
848 if (data_len > 0xFFFF)
849 return -EINVAL;
850 if (diff > skb_tailroom(e->skb)) {
9a732ed6
AE
851 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
852 diff, GFP_ATOMIC);
5191d70f 853 if (!nskb)
e2b58a67 854 return -ENOMEM;
e2b58a67
PM
855 kfree_skb(e->skb);
856 e->skb = nskb;
7af4cc3f
HW
857 }
858 skb_put(e->skb, diff);
859 }
2cf6bffc 860 if (skb_ensure_writable(e->skb, data_len))
7af4cc3f 861 return -ENOMEM;
27d7ff46 862 skb_copy_to_linear_data(e->skb, data, data_len);
e7dfb09a 863 e->skb->ip_summed = CHECKSUM_NONE;
7af4cc3f
HW
864 return 0;
865}
866
7af4cc3f
HW
867static int
868nfqnl_set_mode(struct nfqnl_instance *queue,
869 unsigned char mode, unsigned int range)
870{
c5de0dfd 871 int status = 0;
7af4cc3f
HW
872
873 spin_lock_bh(&queue->lock);
c5de0dfd
PM
874 switch (mode) {
875 case NFQNL_COPY_NONE:
876 case NFQNL_COPY_META:
877 queue->copy_mode = mode;
878 queue->copy_range = 0;
879 break;
880
881 case NFQNL_COPY_PACKET:
882 queue->copy_mode = mode;
9cefbbc9
FW
883 if (range == 0 || range > NFQNL_MAX_COPY_RANGE)
884 queue->copy_range = NFQNL_MAX_COPY_RANGE;
c5de0dfd
PM
885 else
886 queue->copy_range = range;
887 break;
888
889 default:
890 status = -EINVAL;
891
892 }
7af4cc3f
HW
893 spin_unlock_bh(&queue->lock);
894
895 return status;
896}
897
898static int
02f014d8 899dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
7af4cc3f 900{
c4b0e771
FW
901#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
902 int physinif, physoutif;
903
904 physinif = nf_bridge_get_physinif(entry->skb);
905 physoutif = nf_bridge_get_physoutif(entry->skb);
906
907 if (physinif == ifindex || physoutif == ifindex)
908 return 1;
909#endif
1d1de89b
DM
910 if (entry->state.in)
911 if (entry->state.in->ifindex == ifindex)
7af4cc3f 912 return 1;
1d1de89b
DM
913 if (entry->state.out)
914 if (entry->state.out->ifindex == ifindex)
7af4cc3f 915 return 1;
c737b7c4 916
7af4cc3f
HW
917 return 0;
918}
919
920/* drop all packets with either indev or outdev == ifindex from all queue
921 * instances */
922static void
e8179610 923nfqnl_dev_drop(struct net *net, int ifindex)
7af4cc3f
HW
924{
925 int i;
e8179610 926 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
601e68e1 927
9872bec7 928 rcu_read_lock();
7af4cc3f 929
9872bec7 930 for (i = 0; i < INSTANCE_BUCKETS; i++) {
7af4cc3f 931 struct nfqnl_instance *inst;
e8179610 932 struct hlist_head *head = &q->instance_table[i];
7af4cc3f 933
b67bfe0d 934 hlist_for_each_entry_rcu(inst, head, hlist)
b43d8d85 935 nfqnl_flush(inst, dev_cmp, ifindex);
7af4cc3f
HW
936 }
937
9872bec7 938 rcu_read_unlock();
7af4cc3f
HW
939}
940
7af4cc3f
HW
941static int
942nfqnl_rcv_dev_event(struct notifier_block *this,
943 unsigned long event, void *ptr)
944{
351638e7 945 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
7af4cc3f
HW
946
947 /* Drop any packets associated with the downed device */
948 if (event == NETDEV_DOWN)
e8179610 949 nfqnl_dev_drop(dev_net(dev), dev->ifindex);
7af4cc3f
HW
950 return NOTIFY_DONE;
951}
952
953static struct notifier_block nfqnl_dev_notifier = {
954 .notifier_call = nfqnl_rcv_dev_event,
955};
956
26888dfd 957static void nfqnl_nf_hook_drop(struct net *net)
8405a8ff
EB
958{
959 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
960 int i;
961
87029970
FW
962 /* This function is also called on net namespace error unwind,
963 * when pernet_ops->init() failed and ->exit() functions of the
964 * previous pernet_ops gets called.
965 *
966 * This may result in a call to nfqnl_nf_hook_drop() before
967 * struct nfnl_queue_net was allocated.
968 */
969 if (!q)
970 return;
971
8405a8ff
EB
972 for (i = 0; i < INSTANCE_BUCKETS; i++) {
973 struct nfqnl_instance *inst;
974 struct hlist_head *head = &q->instance_table[i];
975
26888dfd 976 hlist_for_each_entry_rcu(inst, head, hlist)
039b40ee 977 nfqnl_flush(inst, NULL, 0);
8405a8ff 978 }
8405a8ff
EB
979}
980
7af4cc3f
HW
981static int
982nfqnl_rcv_nl_event(struct notifier_block *this,
983 unsigned long event, void *ptr)
984{
985 struct netlink_notify *n = ptr;
e8179610 986 struct nfnl_queue_net *q = nfnl_queue_pernet(n->net);
7af4cc3f 987
dee5817e 988 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
7af4cc3f
HW
989 int i;
990
15e47304 991 /* destroy all instances for this portid */
e8179610 992 spin_lock(&q->instances_lock);
9872bec7 993 for (i = 0; i < INSTANCE_BUCKETS; i++) {
b67bfe0d 994 struct hlist_node *t2;
7af4cc3f 995 struct nfqnl_instance *inst;
e8179610 996 struct hlist_head *head = &q->instance_table[i];
7af4cc3f 997
b67bfe0d 998 hlist_for_each_entry_safe(inst, t2, head, hlist) {
e8179610 999 if (n->portid == inst->peer_portid)
7af4cc3f
HW
1000 __instance_destroy(inst);
1001 }
1002 }
e8179610 1003 spin_unlock(&q->instances_lock);
7af4cc3f
HW
1004 }
1005 return NOTIFY_DONE;
1006}
1007
1008static struct notifier_block nfqnl_rtnl_notifier = {
1009 .notifier_call = nfqnl_rcv_nl_event,
1010};
1011
8d45ff22
SB
1012static const struct nla_policy nfqa_vlan_policy[NFQA_VLAN_MAX + 1] = {
1013 [NFQA_VLAN_TCI] = { .type = NLA_U16},
1014 [NFQA_VLAN_PROTO] = { .type = NLA_U16},
1015};
1016
5bf75853
PM
1017static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
1018 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
1019 [NFQA_MARK] = { .type = NLA_U32 },
1020 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
9cb01766 1021 [NFQA_CT] = { .type = NLA_UNSPEC },
bd077937 1022 [NFQA_EXP] = { .type = NLA_UNSPEC },
8d45ff22 1023 [NFQA_VLAN] = { .type = NLA_NESTED },
838ab636
HW
1024};
1025
97d32cf9
FW
1026static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = {
1027 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
1028 [NFQA_MARK] = { .type = NLA_U32 },
1029};
1030
e8179610 1031static struct nfqnl_instance *
cc6bc448 1032verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, u32 nlportid)
97d32cf9
FW
1033{
1034 struct nfqnl_instance *queue;
1035
e8179610 1036 queue = instance_lookup(q, queue_num);
97d32cf9
FW
1037 if (!queue)
1038 return ERR_PTR(-ENODEV);
1039
15e47304 1040 if (queue->peer_portid != nlportid)
97d32cf9
FW
1041 return ERR_PTR(-EPERM);
1042
1043 return queue;
1044}
1045
1046static struct nfqnl_msg_verdict_hdr*
1047verdicthdr_get(const struct nlattr * const nfqa[])
1048{
1049 struct nfqnl_msg_verdict_hdr *vhdr;
1050 unsigned int verdict;
1051
1052 if (!nfqa[NFQA_VERDICT_HDR])
1053 return NULL;
1054
1055 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
c6675233
FW
1056 verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK;
1057 if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN)
97d32cf9
FW
1058 return NULL;
1059 return vhdr;
1060}
1061
1062static int nfq_id_after(unsigned int id, unsigned int max)
1063{
1064 return (int)(id - max) > 0;
1065}
1066
797d4980
PNA
1067static int nfqnl_recv_verdict_batch(struct sk_buff *skb,
1068 const struct nfnl_info *info,
1069 const struct nlattr * const nfqa[])
97d32cf9 1070{
797d4980 1071 struct nfnl_queue_net *q = nfnl_queue_pernet(info->net);
ef4b65e5 1072 u16 queue_num = ntohs(info->nfmsg->res_id);
97d32cf9 1073 struct nf_queue_entry *entry, *tmp;
97d32cf9
FW
1074 struct nfqnl_msg_verdict_hdr *vhdr;
1075 struct nfqnl_instance *queue;
797d4980 1076 unsigned int verdict, maxid;
97d32cf9 1077 LIST_HEAD(batch_list);
e8179610
G
1078
1079 queue = verdict_instance_lookup(q, queue_num,
1080 NETLINK_CB(skb).portid);
97d32cf9
FW
1081 if (IS_ERR(queue))
1082 return PTR_ERR(queue);
1083
1084 vhdr = verdicthdr_get(nfqa);
1085 if (!vhdr)
1086 return -EINVAL;
1087
1088 verdict = ntohl(vhdr->verdict);
1089 maxid = ntohl(vhdr->id);
1090
1091 spin_lock_bh(&queue->lock);
1092
1093 list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) {
1094 if (nfq_id_after(entry->id, maxid))
1095 break;
1096 __dequeue_entry(queue, entry);
1097 list_add_tail(&entry->list, &batch_list);
1098 }
1099
1100 spin_unlock_bh(&queue->lock);
1101
1102 if (list_empty(&batch_list))
1103 return -ENOENT;
1104
1105 list_for_each_entry_safe(entry, tmp, &batch_list, list) {
1106 if (nfqa[NFQA_MARK])
1107 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
368982cd
PNA
1108
1109 nfqnl_reinject(entry, verdict);
97d32cf9
FW
1110 }
1111 return 0;
1112}
1113
a4b4766c 1114static struct nf_conn *nfqnl_ct_parse(struct nfnl_ct_hook *nfnl_ct,
b7bd1809
PNA
1115 const struct nlmsghdr *nlh,
1116 const struct nlattr * const nfqa[],
1117 struct nf_queue_entry *entry,
1118 enum ip_conntrack_info *ctinfo)
1119{
83ace77f 1120#if IS_ENABLED(CONFIG_NF_CONNTRACK)
b7bd1809
PNA
1121 struct nf_conn *ct;
1122
83ace77f 1123 ct = nf_ct_get(entry->skb, ctinfo);
b7bd1809
PNA
1124 if (ct == NULL)
1125 return NULL;
1126
a4b4766c 1127 if (nfnl_ct->parse(nfqa[NFQA_CT], ct) < 0)
b7bd1809
PNA
1128 return NULL;
1129
1130 if (nfqa[NFQA_EXP])
a4b4766c 1131 nfnl_ct->attach_expect(nfqa[NFQA_EXP], ct,
b7bd1809
PNA
1132 NETLINK_CB(entry->skb).portid,
1133 nlmsg_report(nlh));
1134 return ct;
83ace77f
FW
1135#else
1136 return NULL;
1137#endif
b7bd1809
PNA
1138}
1139
8d45ff22
SB
1140static int nfqa_parse_bridge(struct nf_queue_entry *entry,
1141 const struct nlattr * const nfqa[])
1142{
1143 if (nfqa[NFQA_VLAN]) {
1144 struct nlattr *tb[NFQA_VLAN_MAX + 1];
1145 int err;
1146
8cb08174
JB
1147 err = nla_parse_nested_deprecated(tb, NFQA_VLAN_MAX,
1148 nfqa[NFQA_VLAN],
1149 nfqa_vlan_policy, NULL);
8d45ff22
SB
1150 if (err < 0)
1151 return err;
1152
1153 if (!tb[NFQA_VLAN_TCI] || !tb[NFQA_VLAN_PROTO])
1154 return -EINVAL;
1155
82eea4cf
MM
1156 __vlan_hwaccel_put_tag(entry->skb,
1157 nla_get_be16(tb[NFQA_VLAN_PROTO]),
1158 ntohs(nla_get_be16(tb[NFQA_VLAN_TCI])));
8d45ff22
SB
1159 }
1160
1161 if (nfqa[NFQA_L2HDR]) {
1162 int mac_header_len = entry->skb->network_header -
1163 entry->skb->mac_header;
1164
1165 if (mac_header_len != nla_len(nfqa[NFQA_L2HDR]))
1166 return -EINVAL;
1167 else if (mac_header_len > 0)
1168 memcpy(skb_mac_header(entry->skb),
1169 nla_data(nfqa[NFQA_L2HDR]),
1170 mac_header_len);
1171 }
1172
1173 return 0;
1174}
1175
797d4980
PNA
1176static int nfqnl_recv_verdict(struct sk_buff *skb, const struct nfnl_info *info,
1177 const struct nlattr * const nfqa[])
7af4cc3f 1178{
797d4980 1179 struct nfnl_queue_net *q = nfnl_queue_pernet(info->net);
ef4b65e5 1180 u_int16_t queue_num = ntohs(info->nfmsg->res_id);
7af4cc3f 1181 struct nfqnl_msg_verdict_hdr *vhdr;
797d4980 1182 enum ip_conntrack_info ctinfo;
7af4cc3f 1183 struct nfqnl_instance *queue;
02f014d8 1184 struct nf_queue_entry *entry;
a4b4766c 1185 struct nfnl_ct_hook *nfnl_ct;
8c88f87c 1186 struct nf_conn *ct = NULL;
797d4980 1187 unsigned int verdict;
8d45ff22 1188 int err;
7af4cc3f 1189
00a3101f
LZ
1190 queue = verdict_instance_lookup(q, queue_num,
1191 NETLINK_CB(skb).portid);
97d32cf9
FW
1192 if (IS_ERR(queue))
1193 return PTR_ERR(queue);
7af4cc3f 1194
97d32cf9
FW
1195 vhdr = verdicthdr_get(nfqa);
1196 if (!vhdr)
84a797dd 1197 return -EINVAL;
7af4cc3f 1198
7af4cc3f
HW
1199 verdict = ntohl(vhdr->verdict);
1200
b43d8d85 1201 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
84a797dd
ED
1202 if (entry == NULL)
1203 return -ENOENT;
7af4cc3f 1204
8e662164
AB
1205 /* rcu lock already held from nfnl->call_rcu. */
1206 nfnl_ct = rcu_dereference(nfnl_ct_hook);
1207
bd077937 1208 if (nfqa[NFQA_CT]) {
a4b4766c 1209 if (nfnl_ct != NULL)
797d4980
PNA
1210 ct = nfqnl_ct_parse(nfnl_ct, info->nlh, nfqa, entry,
1211 &ctinfo);
bd077937 1212 }
9cb01766 1213
8d45ff22
SB
1214 if (entry->state.pf == PF_BRIDGE) {
1215 err = nfqa_parse_bridge(entry, nfqa);
1216 if (err < 0)
1217 return err;
1218 }
1219
df6fb868 1220 if (nfqa[NFQA_PAYLOAD]) {
8c88f87c
PNA
1221 u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]);
1222 int diff = payload_len - entry->skb->len;
1223
df6fb868 1224 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
8c88f87c 1225 payload_len, entry, diff) < 0)
7af4cc3f 1226 verdict = NF_DROP;
8c88f87c 1227
b7bd1809 1228 if (ct && diff)
a4b4766c 1229 nfnl_ct->seq_adjust(entry->skb, ct, ctinfo, diff);
7af4cc3f
HW
1230 }
1231
df6fb868 1232 if (nfqa[NFQA_MARK])
ea3a66ff 1233 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
601e68e1 1234
368982cd 1235 nfqnl_reinject(entry, verdict);
7af4cc3f
HW
1236 return 0;
1237}
1238
797d4980
PNA
1239static int nfqnl_recv_unsupp(struct sk_buff *skb, const struct nfnl_info *info,
1240 const struct nlattr * const cda[])
7af4cc3f
HW
1241{
1242 return -ENOTSUPP;
1243}
1244
5bf75853
PM
1245static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
1246 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
1247 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
ba062ebb
ED
1248 [NFQA_CFG_QUEUE_MAXLEN] = { .type = NLA_U32 },
1249 [NFQA_CFG_MASK] = { .type = NLA_U32 },
1250 [NFQA_CFG_FLAGS] = { .type = NLA_U32 },
838ab636
HW
1251};
1252
e3ac5298 1253static const struct nf_queue_handler nfqh = {
d4ef3835
AS
1254 .outfn = nfqnl_enqueue_packet,
1255 .nf_hook_drop = nfqnl_nf_hook_drop,
bbd86b9f
HW
1256};
1257
a6555365
PNA
1258static int nfqnl_recv_config(struct sk_buff *skb, const struct nfnl_info *info,
1259 const struct nlattr * const nfqa[])
7af4cc3f 1260{
a6555365 1261 struct nfnl_queue_net *q = nfnl_queue_pernet(info->net);
ef4b65e5 1262 u_int16_t queue_num = ntohs(info->nfmsg->res_id);
9872bec7 1263 struct nfqnl_msg_config_cmd *cmd = NULL;
a6555365 1264 struct nfqnl_instance *queue;
60d2c7f9 1265 __u32 flags = 0, mask = 0;
838ab636 1266 int ret = 0;
7af4cc3f 1267
9872bec7
PM
1268 if (nfqa[NFQA_CFG_CMD]) {
1269 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
1270
0360ae41 1271 /* Obsolete commands without queue context */
9872bec7 1272 switch (cmd->command) {
0360ae41
FW
1273 case NFQNL_CFG_CMD_PF_BIND: return 0;
1274 case NFQNL_CFG_CMD_PF_UNBIND: return 0;
9872bec7 1275 }
9872bec7
PM
1276 }
1277
60d2c7f9
KM
1278 /* Check if we support these flags in first place, dependencies should
1279 * be there too not to break atomicity.
1280 */
1281 if (nfqa[NFQA_CFG_FLAGS]) {
1282 if (!nfqa[NFQA_CFG_MASK]) {
1283 /* A mask is needed to specify which flags are being
1284 * changed.
1285 */
1286 return -EINVAL;
1287 }
1288
1289 flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS]));
1290 mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK]));
1291
1292 if (flags >= NFQA_CFG_F_MAX)
1293 return -EOPNOTSUPP;
1294
1295#if !IS_ENABLED(CONFIG_NETWORK_SECMARK)
1296 if (flags & mask & NFQA_CFG_F_SECCTX)
1297 return -EOPNOTSUPP;
1298#endif
71b2e5f5
KM
1299 if ((flags & mask & NFQA_CFG_F_CONNTRACK) &&
1300 !rcu_access_pointer(nfnl_ct_hook)) {
1301#ifdef CONFIG_MODULES
1302 nfnl_unlock(NFNL_SUBSYS_QUEUE);
1303 request_module("ip_conntrack_netlink");
1304 nfnl_lock(NFNL_SUBSYS_QUEUE);
1305 if (rcu_access_pointer(nfnl_ct_hook))
1306 return -EAGAIN;
1307#endif
1308 return -EOPNOTSUPP;
1309 }
60d2c7f9
KM
1310 }
1311
9872bec7 1312 rcu_read_lock();
e8179610 1313 queue = instance_lookup(q, queue_num);
15e47304 1314 if (queue && queue->peer_portid != NETLINK_CB(skb).portid) {
a3c8e7fd 1315 ret = -EPERM;
9872bec7 1316 goto err_out_unlock;
a3c8e7fd
PM
1317 }
1318
9872bec7 1319 if (cmd != NULL) {
7af4cc3f
HW
1320 switch (cmd->command) {
1321 case NFQNL_CFG_CMD_BIND:
9872bec7
PM
1322 if (queue) {
1323 ret = -EBUSY;
1324 goto err_out_unlock;
1325 }
e8179610
G
1326 queue = instance_create(q, queue_num,
1327 NETLINK_CB(skb).portid);
baab2ce7
PM
1328 if (IS_ERR(queue)) {
1329 ret = PTR_ERR(queue);
9872bec7
PM
1330 goto err_out_unlock;
1331 }
7af4cc3f
HW
1332 break;
1333 case NFQNL_CFG_CMD_UNBIND:
9872bec7
PM
1334 if (!queue) {
1335 ret = -ENODEV;
1336 goto err_out_unlock;
1337 }
e8179610 1338 instance_destroy(q, queue);
17bc6b48 1339 goto err_out_unlock;
7af4cc3f 1340 case NFQNL_CFG_CMD_PF_BIND:
7af4cc3f 1341 case NFQNL_CFG_CMD_PF_UNBIND:
7af4cc3f
HW
1342 break;
1343 default:
cd21f0ac 1344 ret = -ENOTSUPP;
21c3c971 1345 goto err_out_unlock;
7af4cc3f 1346 }
7af4cc3f
HW
1347 }
1348
60d2c7f9
KM
1349 if (!queue) {
1350 ret = -ENODEV;
1351 goto err_out_unlock;
1352 }
1353
df6fb868 1354 if (nfqa[NFQA_CFG_PARAMS]) {
60d2c7f9
KM
1355 struct nfqnl_msg_config_params *params =
1356 nla_data(nfqa[NFQA_CFG_PARAMS]);
7af4cc3f
HW
1357
1358 nfqnl_set_mode(queue, params->copy_mode,
1359 ntohl(params->copy_range));
1360 }
1361
df6fb868 1362 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
60d2c7f9 1363 __be32 *queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
a3c8e7fd 1364
829e17a1
EL
1365 spin_lock_bh(&queue->lock);
1366 queue->queue_maxlen = ntohl(*queue_maxlen);
1367 spin_unlock_bh(&queue->lock);
1368 }
1369
fdb694a0 1370 if (nfqa[NFQA_CFG_FLAGS]) {
fdb694a0
KK
1371 spin_lock_bh(&queue->lock);
1372 queue->flags &= ~mask;
1373 queue->flags |= flags & mask;
1374 spin_unlock_bh(&queue->lock);
1375 }
1376
9872bec7
PM
1377err_out_unlock:
1378 rcu_read_unlock();
838ab636 1379 return ret;
7af4cc3f
HW
1380}
1381
7c8d4cb4 1382static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
50f2db9e
PNA
1383 [NFQNL_MSG_PACKET] = {
1384 .call = nfqnl_recv_unsupp,
1385 .type = NFNL_CB_RCU,
1386 .attr_count = NFQA_MAX,
1387 },
1388 [NFQNL_MSG_VERDICT] = {
1389 .call = nfqnl_recv_verdict,
1390 .type = NFNL_CB_RCU,
1391 .attr_count = NFQA_MAX,
1392 .policy = nfqa_verdict_policy
1393 },
1394 [NFQNL_MSG_CONFIG] = {
1395 .call = nfqnl_recv_config,
1396 .type = NFNL_CB_MUTEX,
1397 .attr_count = NFQA_CFG_MAX,
1398 .policy = nfqa_cfg_policy
1399 },
1400 [NFQNL_MSG_VERDICT_BATCH] = {
1401 .call = nfqnl_recv_verdict_batch,
1402 .type = NFNL_CB_RCU,
1403 .attr_count = NFQA_MAX,
1404 .policy = nfqa_verdict_batch_policy
1405 },
7af4cc3f
HW
1406};
1407
7c8d4cb4 1408static const struct nfnetlink_subsystem nfqnl_subsys = {
7af4cc3f
HW
1409 .name = "nf_queue",
1410 .subsys_id = NFNL_SUBSYS_QUEUE,
1411 .cb_count = NFQNL_MSG_MAX,
7af4cc3f
HW
1412 .cb = nfqnl_cb,
1413};
1414
838ab636
HW
1415#ifdef CONFIG_PROC_FS
1416struct iter_state {
e8179610 1417 struct seq_net_private p;
838ab636
HW
1418 unsigned int bucket;
1419};
1420
1421static struct hlist_node *get_first(struct seq_file *seq)
1422{
1423 struct iter_state *st = seq->private;
e8179610
G
1424 struct net *net;
1425 struct nfnl_queue_net *q;
838ab636
HW
1426
1427 if (!st)
1428 return NULL;
1429
e8179610
G
1430 net = seq_file_net(seq);
1431 q = nfnl_queue_pernet(net);
838ab636 1432 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
e8179610
G
1433 if (!hlist_empty(&q->instance_table[st->bucket]))
1434 return q->instance_table[st->bucket].first;
838ab636
HW
1435 }
1436 return NULL;
1437}
1438
1439static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
1440{
1441 struct iter_state *st = seq->private;
e8179610 1442 struct net *net = seq_file_net(seq);
838ab636
HW
1443
1444 h = h->next;
1445 while (!h) {
e8179610
G
1446 struct nfnl_queue_net *q;
1447
838ab636
HW
1448 if (++st->bucket >= INSTANCE_BUCKETS)
1449 return NULL;
1450
e8179610
G
1451 q = nfnl_queue_pernet(net);
1452 h = q->instance_table[st->bucket].first;
838ab636
HW
1453 }
1454 return h;
1455}
1456
1457static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
1458{
1459 struct hlist_node *head;
1460 head = get_first(seq);
1461
1462 if (head)
1463 while (pos && (head = get_next(seq, head)))
1464 pos--;
1465 return pos ? NULL : head;
1466}
1467
e8179610
G
1468static void *seq_start(struct seq_file *s, loff_t *pos)
1469 __acquires(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
838ab636 1470{
e8179610
G
1471 spin_lock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
1472 return get_idx(s, *pos);
838ab636
HW
1473}
1474
1475static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1476{
1477 (*pos)++;
1478 return get_next(s, v);
1479}
1480
1481static void seq_stop(struct seq_file *s, void *v)
e8179610 1482 __releases(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
838ab636 1483{
e8179610 1484 spin_unlock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
838ab636
HW
1485}
1486
1487static int seq_show(struct seq_file *s, void *v)
1488{
1489 const struct nfqnl_instance *inst = v;
1490
6b46f7b7 1491 seq_printf(s, "%5u %6u %5u %1u %5u %5u %5u %8u %2d\n",
e71456ae
SRRH
1492 inst->queue_num,
1493 inst->peer_portid, inst->queue_total,
1494 inst->copy_mode, inst->copy_range,
1495 inst->queue_dropped, inst->queue_user_dropped,
1496 inst->id_sequence, 1);
861fb107 1497 return 0;
838ab636
HW
1498}
1499
56b3d975 1500static const struct seq_operations nfqnl_seq_ops = {
838ab636
HW
1501 .start = seq_start,
1502 .next = seq_next,
1503 .stop = seq_stop,
1504 .show = seq_show,
1505};
838ab636
HW
1506#endif /* PROC_FS */
1507
e8179610 1508static int __net_init nfnl_queue_net_init(struct net *net)
7af4cc3f 1509{
e8179610
G
1510 unsigned int i;
1511 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
601e68e1 1512
838ab636 1513 for (i = 0; i < INSTANCE_BUCKETS; i++)
e8179610
G
1514 INIT_HLIST_HEAD(&q->instance_table[i]);
1515
1516 spin_lock_init(&q->instances_lock);
1517
1518#ifdef CONFIG_PROC_FS
c3506372
CH
1519 if (!proc_create_net("nfnetlink_queue", 0440, net->nf.proc_netfilter,
1520 &nfqnl_seq_ops, sizeof(struct iter_state)))
e8179610
G
1521 return -ENOMEM;
1522#endif
1523 return 0;
1524}
1525
1526static void __net_exit nfnl_queue_net_exit(struct net *net)
1527{
613d0776
VA
1528 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
1529 unsigned int i;
1530
e778f56e 1531#ifdef CONFIG_PROC_FS
e8179610 1532 remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter);
e778f56e 1533#endif
613d0776
VA
1534 for (i = 0; i < INSTANCE_BUCKETS; i++)
1535 WARN_ON_ONCE(!hlist_empty(&q->instance_table[i]));
e8179610
G
1536}
1537
dc3ee32e
EB
1538static void nfnl_queue_net_exit_batch(struct list_head *net_exit_list)
1539{
1540 synchronize_rcu();
1541}
1542
e8179610 1543static struct pernet_operations nfnl_queue_net_ops = {
dc3ee32e
EB
1544 .init = nfnl_queue_net_init,
1545 .exit = nfnl_queue_net_exit,
1546 .exit_batch = nfnl_queue_net_exit_batch,
1547 .id = &nfnl_queue_net_id,
1548 .size = sizeof(struct nfnl_queue_net),
e8179610
G
1549};
1550
1551static int __init nfnetlink_queue_init(void)
1552{
3bfe0498
FR
1553 int status;
1554
1555 status = register_pernet_subsys(&nfnl_queue_net_ops);
1556 if (status < 0) {
5191d70f 1557 pr_err("failed to register pernet ops\n");
3bfe0498
FR
1558 goto out;
1559 }
838ab636 1560
7af4cc3f
HW
1561 netlink_register_notifier(&nfqnl_rtnl_notifier);
1562 status = nfnetlink_subsys_register(&nfqnl_subsys);
1563 if (status < 0) {
5191d70f 1564 pr_err("failed to create netlink socket\n");
7af4cc3f
HW
1565 goto cleanup_netlink_notifier;
1566 }
1567
4e6577de
GF
1568 status = register_netdevice_notifier(&nfqnl_dev_notifier);
1569 if (status < 0) {
5191d70f 1570 pr_err("failed to register netdevice notifier\n");
4e6577de
GF
1571 goto cleanup_netlink_subsys;
1572 }
1573
87029970
FW
1574 nf_register_queue_handler(&nfqh);
1575
7af4cc3f
HW
1576 return status;
1577
4e6577de
GF
1578cleanup_netlink_subsys:
1579 nfnetlink_subsys_unregister(&nfqnl_subsys);
7af4cc3f
HW
1580cleanup_netlink_notifier:
1581 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
639e077b 1582 unregister_pernet_subsys(&nfnl_queue_net_ops);
3bfe0498 1583out:
7af4cc3f
HW
1584 return status;
1585}
1586
65b4b4e8 1587static void __exit nfnetlink_queue_fini(void)
7af4cc3f 1588{
87029970 1589 nf_unregister_queue_handler();
32292a7f 1590 unregister_netdevice_notifier(&nfqnl_dev_notifier);
32292a7f
PM
1591 nfnetlink_subsys_unregister(&nfqnl_subsys);
1592 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
3bfe0498 1593 unregister_pernet_subsys(&nfnl_queue_net_ops);
67137f3c
JDB
1594
1595 rcu_barrier(); /* Wait for completion of call_rcu()'s */
7af4cc3f
HW
1596}
1597
1598MODULE_DESCRIPTION("netfilter packet queue handler");
1599MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1600MODULE_LICENSE("GPL");
1601MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1602
65b4b4e8
AM
1603module_init(nfnetlink_queue_init);
1604module_exit(nfnetlink_queue_fini);