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