forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
kernel/net/ipv4/udp.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * INET An implementation of the TCP/IP protocol suite for the LINUX
34 * operating system. INET is implemented using the BSD Socket
....@@ -69,19 +70,13 @@
6970 * a single port at the same time.
7071 * Derek Atkins <derek@ihtfp.com>: Add Encapulation Support
7172 * James Chapman : Add L2TP encapsulation type.
72
- *
73
- *
74
- * This program is free software; you can redistribute it and/or
75
- * modify it under the terms of the GNU General Public License
76
- * as published by the Free Software Foundation; either version
77
- * 2 of the License, or (at your option) any later version.
7873 */
7974
8075 #define pr_fmt(fmt) "UDP: " fmt
8176
8277 #include <linux/uaccess.h>
8378 #include <asm/ioctls.h>
84
-#include <linux/bootmem.h>
79
+#include <linux/memblock.h>
8580 #include <linux/highmem.h>
8681 #include <linux/swap.h>
8782 #include <linux/types.h>
....@@ -105,16 +100,23 @@
105100 #include <net/net_namespace.h>
106101 #include <net/icmp.h>
107102 #include <net/inet_hashtables.h>
103
+#include <net/ip_tunnels.h>
108104 #include <net/route.h>
109105 #include <net/checksum.h>
110106 #include <net/xfrm.h>
111107 #include <trace/events/udp.h>
112108 #include <linux/static_key.h>
109
+#include <linux/btf_ids.h>
113110 #include <trace/events/skb.h>
114111 #include <net/busy_poll.h>
115112 #include "udp_impl.h"
116113 #include <net/sock_reuseport.h>
117114 #include <net/addrconf.h>
115
+#include <net/udp_tunnel.h>
116
+#if IS_ENABLED(CONFIG_IPV6)
117
+#include <net/ipv6_stubs.h>
118
+#endif
119
+#include <trace/hooks/ipv4.h>
118120
119121 struct udp_table udp_table __read_mostly;
120122 EXPORT_SYMBOL(udp_table);
....@@ -127,17 +129,6 @@
127129
128130 #define MAX_UDP_PORTS 65536
129131 #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN)
130
-
131
-/* IPCB reference means this can not be used from early demux */
132
-static bool udp_lib_exact_dif_match(struct net *net, struct sk_buff *skb)
133
-{
134
-#if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
135
- if (!net->ipv4.sysctl_udp_l3mdev_accept &&
136
- skb && ipv4_l3mdev_skb(IPCB(skb)->flags))
137
- return true;
138
-#endif
139
- return false;
140
-}
141132
142133 static int udp_lib_lport_inuse(struct net *net, __u16 num,
143134 const struct udp_hslot *hslot,
....@@ -367,25 +358,23 @@
367358 static int compute_score(struct sock *sk, struct net *net,
368359 __be32 saddr, __be16 sport,
369360 __be32 daddr, unsigned short hnum,
370
- int dif, int sdif, bool exact_dif)
361
+ int dif, int sdif)
371362 {
372363 int score;
373364 struct inet_sock *inet;
365
+ bool dev_match;
374366
375367 if (!net_eq(sock_net(sk), net) ||
376368 udp_sk(sk)->udp_port_hash != hnum ||
377369 ipv6_only_sock(sk))
378370 return -1;
379371
372
+ if (sk->sk_rcv_saddr != daddr)
373
+ return -1;
374
+
380375 score = (sk->sk_family == PF_INET) ? 2 : 1;
376
+
381377 inet = inet_sk(sk);
382
-
383
- if (inet->inet_rcv_saddr) {
384
- if (inet->inet_rcv_saddr != daddr)
385
- return -1;
386
- score += 4;
387
- }
388
-
389378 if (inet->inet_daddr) {
390379 if (inet->inet_daddr != saddr)
391380 return -1;
....@@ -398,15 +387,12 @@
398387 score += 4;
399388 }
400389
401
- if (sk->sk_bound_dev_if || exact_dif) {
402
- bool dev_match = (sk->sk_bound_dev_if == dif ||
403
- sk->sk_bound_dev_if == sdif);
404
-
405
- if (!dev_match)
406
- return -1;
407
- if (sk->sk_bound_dev_if)
408
- score += 4;
409
- }
390
+ dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if,
391
+ dif, sdif);
392
+ if (!dev_match)
393
+ return -1;
394
+ if (sk->sk_bound_dev_if)
395
+ score += 4;
410396
411397 if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
412398 score++;
....@@ -425,41 +411,73 @@
425411 udp_ehash_secret + net_hash_mix(net));
426412 }
427413
414
+static struct sock *lookup_reuseport(struct net *net, struct sock *sk,
415
+ struct sk_buff *skb,
416
+ __be32 saddr, __be16 sport,
417
+ __be32 daddr, unsigned short hnum)
418
+{
419
+ struct sock *reuse_sk = NULL;
420
+ u32 hash;
421
+
422
+ if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) {
423
+ hash = udp_ehashfn(net, daddr, hnum, saddr, sport);
424
+ reuse_sk = reuseport_select_sock(sk, hash, skb,
425
+ sizeof(struct udphdr));
426
+ }
427
+ return reuse_sk;
428
+}
429
+
428430 /* called with rcu_read_lock() */
429431 static struct sock *udp4_lib_lookup2(struct net *net,
430432 __be32 saddr, __be16 sport,
431433 __be32 daddr, unsigned int hnum,
432
- int dif, int sdif, bool exact_dif,
434
+ int dif, int sdif,
433435 struct udp_hslot *hslot2,
434436 struct sk_buff *skb)
435437 {
436
- struct sock *sk, *result, *reuseport_result;
438
+ struct sock *sk, *result;
437439 int score, badness;
438
- u32 hash = 0;
439440
440441 result = NULL;
441442 badness = 0;
442443 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
443444 score = compute_score(sk, net, saddr, sport,
444
- daddr, hnum, dif, sdif, exact_dif);
445
+ daddr, hnum, dif, sdif);
445446 if (score > badness) {
446
- reuseport_result = NULL;
447
+ result = lookup_reuseport(net, sk, skb,
448
+ saddr, sport, daddr, hnum);
449
+ /* Fall back to scoring if group has connections */
450
+ if (result && !reuseport_has_conns(sk))
451
+ return result;
447452
448
- if (sk->sk_reuseport &&
449
- sk->sk_state != TCP_ESTABLISHED) {
450
- hash = udp_ehashfn(net, daddr, hnum,
451
- saddr, sport);
452
- reuseport_result = reuseport_select_sock(sk, hash, skb,
453
- sizeof(struct udphdr));
454
- if (reuseport_result && !reuseport_has_conns(sk, false))
455
- return reuseport_result;
456
- }
457
-
458
- result = reuseport_result ? : sk;
453
+ result = result ? : sk;
459454 badness = score;
460455 }
461456 }
462457 return result;
458
+}
459
+
460
+static struct sock *udp4_lookup_run_bpf(struct net *net,
461
+ struct udp_table *udptable,
462
+ struct sk_buff *skb,
463
+ __be32 saddr, __be16 sport,
464
+ __be32 daddr, u16 hnum)
465
+{
466
+ struct sock *sk, *reuse_sk;
467
+ bool no_reuseport;
468
+
469
+ if (udptable != &udp_table)
470
+ return NULL; /* only UDP is supported */
471
+
472
+ no_reuseport = bpf_sk_lookup_run_v4(net, IPPROTO_UDP,
473
+ saddr, sport, daddr, hnum, &sk);
474
+ if (no_reuseport || IS_ERR_OR_NULL(sk))
475
+ return sk;
476
+
477
+ reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum);
478
+ if (reuse_sk)
479
+ sk = reuse_sk;
480
+ return sk;
463481 }
464482
465483 /* UDP is nearly always wildcards out the wazoo, it makes no sense to try
....@@ -469,65 +487,47 @@
469487 __be16 sport, __be32 daddr, __be16 dport, int dif,
470488 int sdif, struct udp_table *udptable, struct sk_buff *skb)
471489 {
472
- struct sock *sk, *result;
473490 unsigned short hnum = ntohs(dport);
474
- unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
475
- struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
476
- bool exact_dif = udp_lib_exact_dif_match(net, skb);
477
- int score, badness;
478
- u32 hash = 0;
491
+ unsigned int hash2, slot2;
492
+ struct udp_hslot *hslot2;
493
+ struct sock *result, *sk;
479494
480
- if (hslot->count > 10) {
481
- hash2 = ipv4_portaddr_hash(net, daddr, hnum);
482
- slot2 = hash2 & udptable->mask;
483
- hslot2 = &udptable->hash2[slot2];
484
- if (hslot->count < hslot2->count)
485
- goto begin;
495
+ hash2 = ipv4_portaddr_hash(net, daddr, hnum);
496
+ slot2 = hash2 & udptable->mask;
497
+ hslot2 = &udptable->hash2[slot2];
486498
487
- result = udp4_lib_lookup2(net, saddr, sport,
488
- daddr, hnum, dif, sdif,
489
- exact_dif, hslot2, skb);
490
- if (!result) {
491
- unsigned int old_slot2 = slot2;
492
- hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
493
- slot2 = hash2 & udptable->mask;
494
- /* avoid searching the same slot again. */
495
- if (unlikely(slot2 == old_slot2))
496
- return result;
499
+ /* Lookup connected or non-wildcard socket */
500
+ result = udp4_lib_lookup2(net, saddr, sport,
501
+ daddr, hnum, dif, sdif,
502
+ hslot2, skb);
503
+ if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED)
504
+ goto done;
497505
498
- hslot2 = &udptable->hash2[slot2];
499
- if (hslot->count < hslot2->count)
500
- goto begin;
501
-
502
- result = udp4_lib_lookup2(net, saddr, sport,
503
- daddr, hnum, dif, sdif,
504
- exact_dif, hslot2, skb);
505
- }
506
- if (unlikely(IS_ERR(result)))
507
- return NULL;
508
- return result;
509
- }
510
-begin:
511
- result = NULL;
512
- badness = 0;
513
- sk_for_each_rcu(sk, &hslot->head) {
514
- score = compute_score(sk, net, saddr, sport,
515
- daddr, hnum, dif, sdif, exact_dif);
516
- if (score > badness) {
517
- if (sk->sk_reuseport) {
518
- hash = udp_ehashfn(net, daddr, hnum,
519
- saddr, sport);
520
- result = reuseport_select_sock(sk, hash, skb,
521
- sizeof(struct udphdr));
522
- if (unlikely(IS_ERR(result)))
523
- return NULL;
524
- if (result)
525
- return result;
526
- }
506
+ /* Lookup redirect from BPF */
507
+ if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
508
+ sk = udp4_lookup_run_bpf(net, udptable, skb,
509
+ saddr, sport, daddr, hnum);
510
+ if (sk) {
527511 result = sk;
528
- badness = score;
512
+ goto done;
529513 }
530514 }
515
+
516
+ /* Got non-wildcard socket or error on first lookup */
517
+ if (result)
518
+ goto done;
519
+
520
+ /* Lookup wildcard sockets */
521
+ hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
522
+ slot2 = hash2 & udptable->mask;
523
+ hslot2 = &udptable->hash2[slot2];
524
+
525
+ result = udp4_lib_lookup2(net, saddr, sport,
526
+ htonl(INADDR_ANY), hnum, dif, sdif,
527
+ hslot2, skb);
528
+done:
529
+ if (IS_ERR(result))
530
+ return NULL;
531531 return result;
532532 }
533533 EXPORT_SYMBOL_GPL(__udp4_lib_lookup);
....@@ -585,12 +585,102 @@
585585 (inet->inet_dport != rmt_port && inet->inet_dport) ||
586586 (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) ||
587587 ipv6_only_sock(sk) ||
588
- (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif &&
589
- sk->sk_bound_dev_if != sdif))
588
+ !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
590589 return false;
591590 if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif, sdif))
592591 return false;
593592 return true;
593
+}
594
+
595
+DEFINE_STATIC_KEY_FALSE(udp_encap_needed_key);
596
+void udp_encap_enable(void)
597
+{
598
+ static_branch_inc(&udp_encap_needed_key);
599
+}
600
+EXPORT_SYMBOL(udp_encap_enable);
601
+
602
+void udp_encap_disable(void)
603
+{
604
+ static_branch_dec(&udp_encap_needed_key);
605
+}
606
+EXPORT_SYMBOL(udp_encap_disable);
607
+
608
+/* Handler for tunnels with arbitrary destination ports: no socket lookup, go
609
+ * through error handlers in encapsulations looking for a match.
610
+ */
611
+static int __udp4_lib_err_encap_no_sk(struct sk_buff *skb, u32 info)
612
+{
613
+ int i;
614
+
615
+ for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) {
616
+ int (*handler)(struct sk_buff *skb, u32 info);
617
+ const struct ip_tunnel_encap_ops *encap;
618
+
619
+ encap = rcu_dereference(iptun_encaps[i]);
620
+ if (!encap)
621
+ continue;
622
+ handler = encap->err_handler;
623
+ if (handler && !handler(skb, info))
624
+ return 0;
625
+ }
626
+
627
+ return -ENOENT;
628
+}
629
+
630
+/* Try to match ICMP errors to UDP tunnels by looking up a socket without
631
+ * reversing source and destination port: this will match tunnels that force the
632
+ * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that
633
+ * lwtunnels might actually break this assumption by being configured with
634
+ * different destination ports on endpoints, in this case we won't be able to
635
+ * trace ICMP messages back to them.
636
+ *
637
+ * If this doesn't match any socket, probe tunnels with arbitrary destination
638
+ * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port
639
+ * we've sent packets to won't necessarily match the local destination port.
640
+ *
641
+ * Then ask the tunnel implementation to match the error against a valid
642
+ * association.
643
+ *
644
+ * Return an error if we can't find a match, the socket if we need further
645
+ * processing, zero otherwise.
646
+ */
647
+static struct sock *__udp4_lib_err_encap(struct net *net,
648
+ const struct iphdr *iph,
649
+ struct udphdr *uh,
650
+ struct udp_table *udptable,
651
+ struct sk_buff *skb, u32 info)
652
+{
653
+ int network_offset, transport_offset;
654
+ struct sock *sk;
655
+
656
+ network_offset = skb_network_offset(skb);
657
+ transport_offset = skb_transport_offset(skb);
658
+
659
+ /* Network header needs to point to the outer IPv4 header inside ICMP */
660
+ skb_reset_network_header(skb);
661
+
662
+ /* Transport header needs to point to the UDP header */
663
+ skb_set_transport_header(skb, iph->ihl << 2);
664
+
665
+ sk = __udp4_lib_lookup(net, iph->daddr, uh->source,
666
+ iph->saddr, uh->dest, skb->dev->ifindex, 0,
667
+ udptable, NULL);
668
+ if (sk) {
669
+ int (*lookup)(struct sock *sk, struct sk_buff *skb);
670
+ struct udp_sock *up = udp_sk(sk);
671
+
672
+ lookup = READ_ONCE(up->encap_err_lookup);
673
+ if (!lookup || lookup(sk, skb))
674
+ sk = NULL;
675
+ }
676
+
677
+ if (!sk)
678
+ sk = ERR_PTR(__udp4_lib_err_encap_no_sk(skb, info));
679
+
680
+ skb_set_transport_header(skb, transport_offset);
681
+ skb_set_network_header(skb, network_offset);
682
+
683
+ return sk;
594684 }
595685
596686 /*
....@@ -604,24 +694,38 @@
604694 * to find the appropriate port.
605695 */
606696
607
-void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
697
+int __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
608698 {
609699 struct inet_sock *inet;
610700 const struct iphdr *iph = (const struct iphdr *)skb->data;
611701 struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2));
612702 const int type = icmp_hdr(skb)->type;
613703 const int code = icmp_hdr(skb)->code;
704
+ bool tunnel = false;
614705 struct sock *sk;
615706 int harderr;
616707 int err;
617708 struct net *net = dev_net(skb->dev);
618709
619710 sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
620
- iph->saddr, uh->source, skb->dev->ifindex, 0,
621
- udptable, NULL);
711
+ iph->saddr, uh->source, skb->dev->ifindex,
712
+ inet_sdif(skb), udptable, NULL);
622713 if (!sk) {
623
- __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
624
- return; /* No socket for error */
714
+ /* No socket for error: try tunnels before discarding */
715
+ sk = ERR_PTR(-ENOENT);
716
+ if (static_branch_unlikely(&udp_encap_needed_key)) {
717
+ sk = __udp4_lib_err_encap(net, iph, uh, udptable, skb,
718
+ info);
719
+ if (!sk)
720
+ return 0;
721
+ }
722
+
723
+ if (IS_ERR(sk)) {
724
+ __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
725
+ return PTR_ERR(sk);
726
+ }
727
+
728
+ tunnel = true;
625729 }
626730
627731 err = 0;
....@@ -664,6 +768,10 @@
664768 * RFC1122: OK. Passes ICMP errors back to application, as per
665769 * 4.1.3.3.
666770 */
771
+ if (tunnel) {
772
+ /* ...not for tunnels though: we don't have a sending socket */
773
+ goto out;
774
+ }
667775 if (!inet->recverr) {
668776 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
669777 goto out;
....@@ -673,12 +781,12 @@
673781 sk->sk_err = err;
674782 sk->sk_error_report(sk);
675783 out:
676
- return;
784
+ return 0;
677785 }
678786
679
-void udp_err(struct sk_buff *skb, u32 info)
787
+int udp_err(struct sk_buff *skb, u32 info)
680788 {
681
- __udp4_lib_err(skb, info, &udp_table);
789
+ return __udp4_lib_err(skb, info, &udp_table);
682790 }
683791
684792 /*
....@@ -949,6 +1057,7 @@
9491057
9501058 if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message compatibility */
9511059 return -EOPNOTSUPP;
1060
+ trace_android_rvh_udp_sendmsg(sk);
9521061
9531062 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
9541063
....@@ -1061,7 +1170,7 @@
10611170 }
10621171
10631172 if (ipv4_is_multicast(daddr)) {
1064
- if (!ipc.oif)
1173
+ if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif))
10651174 ipc.oif = inet->mc_index;
10661175 if (!saddr)
10671176 saddr = inet->mc_addr;
....@@ -1070,7 +1179,7 @@
10701179 ipc.oif = inet->uc_index;
10711180 } else if (ipv4_is_lbcast(daddr) && inet->uc_index) {
10721181 /* oif is set, packet is to local broadcast and
1073
- * and uc_index is set. oif is most likely set
1182
+ * uc_index is set. oif is most likely set
10741183 * by sk_bound_dev_if. If uc_index != oif check if the
10751184 * oif is an L3 master and uc_index is an L3 slave.
10761185 * If so, we want to allow the send using the uc_index.
....@@ -1091,13 +1200,13 @@
10911200
10921201 fl4 = &fl4_stack;
10931202
1094
- flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos,
1203
+ flowi4_init_output(fl4, ipc.oif, ipc.sockc.mark, tos,
10951204 RT_SCOPE_UNIVERSE, sk->sk_protocol,
10961205 flow_flags,
10971206 faddr, saddr, dport, inet->inet_sport,
10981207 sk->sk_uid);
10991208
1100
- security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
1209
+ security_sk_classify_flow(sk, flowi4_to_flowi_common(fl4));
11011210 rt = ip_route_output_flow(net, fl4, sk);
11021211 if (IS_ERR(rt)) {
11031212 err = PTR_ERR(rt);
....@@ -1254,6 +1363,27 @@
12541363
12551364 #define UDP_SKB_IS_STATELESS 0x80000000
12561365
1366
+/* all head states (dst, sk, nf conntrack) except skb extensions are
1367
+ * cleared by udp_rcv().
1368
+ *
1369
+ * We need to preserve secpath, if present, to eventually process
1370
+ * IP_CMSG_PASSSEC at recvmsg() time.
1371
+ *
1372
+ * Other extensions can be cleared.
1373
+ */
1374
+static bool udp_try_make_stateless(struct sk_buff *skb)
1375
+{
1376
+ if (!skb_has_extensions(skb))
1377
+ return true;
1378
+
1379
+ if (!secpath_exists(skb)) {
1380
+ skb_ext_reset(skb);
1381
+ return true;
1382
+ }
1383
+
1384
+ return false;
1385
+}
1386
+
12571387 static void udp_set_dev_scratch(struct sk_buff *skb)
12581388 {
12591389 struct udp_dev_scratch *scratch = udp_skb_scratch(skb);
....@@ -1265,11 +1395,7 @@
12651395 scratch->csum_unnecessary = !!skb_csum_unnecessary(skb);
12661396 scratch->is_linear = !skb_is_nonlinear(skb);
12671397 #endif
1268
- /* all head states execept sp (dst, sk, nf) are always cleared by
1269
- * udp_rcv() and we need to preserve secpath, if present, to eventually
1270
- * process IP_CMSG_PASSSEC at recvmsg() time
1271
- */
1272
- if (likely(!skb_sec_path(skb)))
1398
+ if (udp_try_make_stateless(skb))
12731399 scratch->_tsize_state |= UDP_SKB_IS_STATELESS;
12741400 }
12751401
....@@ -1590,7 +1716,7 @@
15901716 EXPORT_SYMBOL(udp_ioctl);
15911717
15921718 struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags,
1593
- int noblock, int *peeked, int *off, int *err)
1719
+ int noblock, int *off, int *err)
15941720 {
15951721 struct sk_buff_head *sk_queue = &sk->sk_receive_queue;
15961722 struct sk_buff_head *queue;
....@@ -1609,14 +1735,13 @@
16091735 break;
16101736
16111737 error = -EAGAIN;
1612
- *peeked = 0;
16131738 do {
16141739 spin_lock_bh(&queue->lock);
1615
- skb = __skb_try_recv_from_queue(sk, queue, flags,
1616
- udp_skb_destructor,
1617
- peeked, off, err,
1618
- &last);
1740
+ skb = __skb_try_recv_from_queue(sk, queue, flags, off,
1741
+ err, &last);
16191742 if (skb) {
1743
+ if (!(flags & MSG_PEEK))
1744
+ udp_skb_destructor(sk, skb);
16201745 spin_unlock_bh(&queue->lock);
16211746 return skb;
16221747 }
....@@ -1634,10 +1759,10 @@
16341759 spin_lock(&sk_queue->lock);
16351760 skb_queue_splice_tail_init(sk_queue, queue);
16361761
1637
- skb = __skb_try_recv_from_queue(sk, queue, flags,
1638
- udp_skb_dtor_locked,
1639
- peeked, off, err,
1640
- &last);
1762
+ skb = __skb_try_recv_from_queue(sk, queue, flags, off,
1763
+ err, &last);
1764
+ if (skb && !(flags & MSG_PEEK))
1765
+ udp_skb_dtor_locked(sk, skb);
16411766 spin_unlock(&sk_queue->lock);
16421767 spin_unlock_bh(&queue->lock);
16431768 if (skb)
....@@ -1652,7 +1777,8 @@
16521777
16531778 /* sk_queue is empty, reader_queue may contain peeked packets */
16541779 } while (timeo &&
1655
- !__skb_wait_for_more_packets(sk, &error, &timeo,
1780
+ !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue,
1781
+ &error, &timeo,
16561782 (struct sk_buff *)sk_queue));
16571783
16581784 *err = error;
....@@ -1672,8 +1798,7 @@
16721798 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
16731799 struct sk_buff *skb;
16741800 unsigned int ulen, copied;
1675
- int peeked, peeking, off;
1676
- int err;
1801
+ int off, err, peeking = flags & MSG_PEEK;
16771802 int is_udplite = IS_UDPLITE(sk);
16781803 bool checksum_valid = false;
16791804
....@@ -1681,11 +1806,11 @@
16811806 return ip_recv_error(sk, msg, len, addr_len);
16821807
16831808 try_again:
1684
- peeking = flags & MSG_PEEK;
16851809 off = sk_peek_offset(sk, flags);
1686
- skb = __skb_recv_udp(sk, flags, noblock, &peeked, &off, &err);
1810
+ skb = __skb_recv_udp(sk, flags, noblock, &off, &err);
16871811 if (!skb)
16881812 return err;
1813
+ trace_android_rvh_udp_recvmsg(sk);
16891814
16901815 ulen = udp_skb_len(skb);
16911816 copied = len;
....@@ -1721,7 +1846,7 @@
17211846 }
17221847
17231848 if (unlikely(err)) {
1724
- if (!peeked) {
1849
+ if (!peeking) {
17251850 atomic_inc(&sk->sk_drops);
17261851 UDP_INC_STATS(sock_net(sk),
17271852 UDP_MIB_INERRORS, is_udplite);
....@@ -1730,7 +1855,7 @@
17301855 return err;
17311856 }
17321857
1733
- if (!peeked)
1858
+ if (!peeking)
17341859 UDP_INC_STATS(sock_net(sk),
17351860 UDP_MIB_INDATAGRAMS, is_udplite);
17361861
....@@ -1748,6 +1873,10 @@
17481873 BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk,
17491874 (struct sockaddr *)sin);
17501875 }
1876
+
1877
+ if (udp_sk(sk)->gro_enabled)
1878
+ udp_cmsg_recv(msg, sk, skb);
1879
+
17511880 if (inet->cmsg_flags)
17521881 ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off);
17531882
....@@ -1797,8 +1926,12 @@
17971926 inet->inet_dport = 0;
17981927 sock_rps_reset_rxhash(sk);
17991928 sk->sk_bound_dev_if = 0;
1800
- if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK))
1929
+ if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) {
18011930 inet_reset_saddr(sk);
1931
+ if (sk->sk_prot->rehash &&
1932
+ (sk->sk_userlocks & SOCK_BINDPORT_LOCK))
1933
+ sk->sk_prot->rehash(sk);
1934
+ }
18021935
18031936 if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
18041937 sk->sk_prot->unhash(sk);
....@@ -1887,7 +2020,7 @@
18872020 }
18882021 EXPORT_SYMBOL(udp_lib_rehash);
18892022
1890
-static void udp_v4_rehash(struct sock *sk)
2023
+void udp_v4_rehash(struct sock *sk)
18912024 {
18922025 u16 new_hash = ipv4_portaddr_hash(sock_net(sk),
18932026 inet_sk(sk)->inet_rcv_saddr,
....@@ -1924,13 +2057,6 @@
19242057 return 0;
19252058 }
19262059
1927
-static DEFINE_STATIC_KEY_FALSE(udp_encap_needed_key);
1928
-void udp_encap_enable(void)
1929
-{
1930
- static_branch_enable(&udp_encap_needed_key);
1931
-}
1932
-EXPORT_SYMBOL(udp_encap_enable);
1933
-
19342060 /* returns:
19352061 * -1: error
19362062 * 0: success
....@@ -1939,7 +2065,7 @@
19392065 * Note that in the success and error cases, the skb is assumed to
19402066 * have either been requeued or freed.
19412067 */
1942
-static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
2068
+static int udp_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
19432069 {
19442070 struct udp_sock *up = udp_sk(sk);
19452071 int is_udplite = IS_UDPLITE(sk);
....@@ -1949,7 +2075,7 @@
19492075 */
19502076 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
19512077 goto drop;
1952
- nf_reset(skb);
2078
+ nf_reset_ct(skb);
19532079
19542080 if (static_branch_unlikely(&udp_encap_needed_key) && up->encap_type) {
19552081 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
....@@ -2042,6 +2168,26 @@
20422168 return -1;
20432169 }
20442170
2171
+static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
2172
+{
2173
+ struct sk_buff *next, *segs;
2174
+ int ret;
2175
+
2176
+ if (likely(!udp_unexpected_gso(sk, skb)))
2177
+ return udp_queue_rcv_one_skb(sk, skb);
2178
+
2179
+ BUILD_BUG_ON(sizeof(struct udp_skb_cb) > SKB_GSO_CB_OFFSET);
2180
+ __skb_push(skb, -skb_mac_offset(skb));
2181
+ segs = udp_rcv_segment(sk, skb, true);
2182
+ skb_list_walk_safe(segs, skb, next) {
2183
+ __skb_pull(skb, skb_transport_offset(skb));
2184
+ ret = udp_queue_rcv_one_skb(sk, skb);
2185
+ if (ret > 0)
2186
+ ip_protocol_deliver_rcu(dev_net(skb->dev), skb, ret);
2187
+ }
2188
+ return 0;
2189
+}
2190
+
20452191 /* For TCP sockets, sk_rx_dst is protected by socket lock
20462192 * For UDP, we use xchg() to guard against concurrent changes.
20472193 */
....@@ -2050,7 +2196,7 @@
20502196 struct dst_entry *old;
20512197
20522198 if (dst_hold_safe(dst)) {
2053
- old = xchg(&sk->sk_rx_dst, dst);
2199
+ old = xchg((__force struct dst_entry **)&sk->sk_rx_dst, dst);
20542200 dst_release(old);
20552201 return old != dst;
20562202 }
....@@ -2130,7 +2276,7 @@
21302276
21312277 /* Initialize UDP checksum. If exited with zero value (success),
21322278 * CHECKSUM_UNNECESSARY means, that no more checks are required.
2133
- * Otherwise, csum completion requires chacksumming packet body,
2279
+ * Otherwise, csum completion requires checksumming packet body,
21342280 * including udp header and folding it to skb->csum.
21352281 */
21362282 static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
....@@ -2184,8 +2330,7 @@
21842330 int ret;
21852331
21862332 if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
2187
- skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
2188
- inet_compute_pseudo);
2333
+ skb_checksum_try_convert(skb, IPPROTO_UDP, inet_compute_pseudo);
21892334
21902335 ret = udp_queue_rcv_skb(sk, skb);
21912336
....@@ -2210,6 +2355,7 @@
22102355 struct rtable *rt = skb_rtable(skb);
22112356 __be32 saddr, daddr;
22122357 struct net *net = dev_net(skb->dev);
2358
+ bool refcounted;
22132359
22142360 /*
22152361 * Validate the packet.
....@@ -2235,16 +2381,17 @@
22352381 if (udp4_csum_init(skb, uh, proto))
22362382 goto csum_error;
22372383
2238
- sk = skb_steal_sock(skb);
2384
+ sk = skb_steal_sock(skb, &refcounted);
22392385 if (sk) {
22402386 struct dst_entry *dst = skb_dst(skb);
22412387 int ret;
22422388
2243
- if (unlikely(sk->sk_rx_dst != dst))
2389
+ if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst))
22442390 udp_sk_rx_dst_set(sk, dst);
22452391
22462392 ret = udp_unicast_rcv_skb(sk, skb, uh);
2247
- sock_put(sk);
2393
+ if (refcounted)
2394
+ sock_put(sk);
22482395 return ret;
22492396 }
22502397
....@@ -2258,7 +2405,7 @@
22582405
22592406 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
22602407 goto drop;
2261
- nf_reset(skb);
2408
+ nf_reset_ct(skb);
22622409
22632410 /* No socket. Drop packet silently, if checksum is wrong */
22642411 if (udp_lib_checksum_complete(skb))
....@@ -2346,8 +2493,7 @@
23462493 struct sock *sk;
23472494
23482495 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
2349
- if (INET_MATCH(sk, net, acookie, rmt_addr,
2350
- loc_addr, ports, dif, sdif))
2496
+ if (INET_MATCH(net, sk, acookie, ports, dif, sdif))
23512497 return sk;
23522498 /* Only check first socket in chain */
23532499 break;
....@@ -2398,7 +2544,7 @@
23982544
23992545 skb->sk = sk;
24002546 skb->destructor = sock_efree;
2401
- dst = READ_ONCE(sk->sk_rx_dst);
2547
+ dst = rcu_dereference(sk->sk_rx_dst);
24022548
24032549 if (dst)
24042550 dst = dst_check(dst, 0);
....@@ -2437,11 +2583,15 @@
24372583 sock_set_flag(sk, SOCK_DEAD);
24382584 udp_flush_pending_frames(sk);
24392585 unlock_sock_fast(sk, slow);
2440
- if (static_branch_unlikely(&udp_encap_needed_key) && up->encap_type) {
2441
- void (*encap_destroy)(struct sock *sk);
2442
- encap_destroy = READ_ONCE(up->encap_destroy);
2443
- if (encap_destroy)
2444
- encap_destroy(sk);
2586
+ if (static_branch_unlikely(&udp_encap_needed_key)) {
2587
+ if (up->encap_type) {
2588
+ void (*encap_destroy)(struct sock *sk);
2589
+ encap_destroy = READ_ONCE(up->encap_destroy);
2590
+ if (encap_destroy)
2591
+ encap_destroy(sk);
2592
+ }
2593
+ if (up->encap_enabled)
2594
+ static_branch_dec(&udp_encap_needed_key);
24452595 }
24462596 }
24472597
....@@ -2449,7 +2599,7 @@
24492599 * Socket option code for UDP
24502600 */
24512601 int udp_lib_setsockopt(struct sock *sk, int level, int optname,
2452
- char __user *optval, unsigned int optlen,
2602
+ sockptr_t optval, unsigned int optlen,
24532603 int (*push_pending_frames)(struct sock *))
24542604 {
24552605 struct udp_sock *up = udp_sk(sk);
....@@ -2460,7 +2610,7 @@
24602610 if (optlen < sizeof(int))
24612611 return -EINVAL;
24622612
2463
- if (get_user(val, (int __user *)optval))
2613
+ if (copy_from_sockptr(&val, optval, sizeof(val)))
24642614 return -EFAULT;
24652615
24662616 valbool = val ? 1 : 0;
....@@ -2480,13 +2630,22 @@
24802630 case UDP_ENCAP:
24812631 switch (val) {
24822632 case 0:
2633
+#ifdef CONFIG_XFRM
24832634 case UDP_ENCAP_ESPINUDP:
24842635 case UDP_ENCAP_ESPINUDP_NON_IKE:
2485
- up->encap_rcv = xfrm4_udp_encap_rcv;
2486
- /* FALLTHROUGH */
2636
+#if IS_ENABLED(CONFIG_IPV6)
2637
+ if (sk->sk_family == AF_INET6)
2638
+ up->encap_rcv = ipv6_stub->xfrm6_udp_encap_rcv;
2639
+ else
2640
+#endif
2641
+ up->encap_rcv = xfrm4_udp_encap_rcv;
2642
+#endif
2643
+ fallthrough;
24872644 case UDP_ENCAP_L2TPINUDP:
24882645 up->encap_type = val;
2489
- udp_encap_enable();
2646
+ lock_sock(sk);
2647
+ udp_tunnel_encap_enable(sk->sk_socket);
2648
+ release_sock(sk);
24902649 break;
24912650 default:
24922651 err = -ENOPROTOOPT;
....@@ -2506,6 +2665,17 @@
25062665 if (val < 0 || val > USHRT_MAX)
25072666 return -EINVAL;
25082667 WRITE_ONCE(up->gso_size, val);
2668
+ break;
2669
+
2670
+ case UDP_GRO:
2671
+ lock_sock(sk);
2672
+
2673
+ /* when enabling GRO, accept the related GSO packet type */
2674
+ if (valbool)
2675
+ udp_tunnel_encap_enable(sk->sk_socket);
2676
+ up->gro_enabled = valbool;
2677
+ up->accept_udp_l4 = valbool;
2678
+ release_sock(sk);
25092679 break;
25102680
25112681 /*
....@@ -2547,25 +2717,15 @@
25472717 }
25482718 EXPORT_SYMBOL(udp_lib_setsockopt);
25492719
2550
-int udp_setsockopt(struct sock *sk, int level, int optname,
2551
- char __user *optval, unsigned int optlen)
2720
+int udp_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
2721
+ unsigned int optlen)
25522722 {
25532723 if (level == SOL_UDP || level == SOL_UDPLITE)
2554
- return udp_lib_setsockopt(sk, level, optname, optval, optlen,
2724
+ return udp_lib_setsockopt(sk, level, optname,
2725
+ optval, optlen,
25552726 udp_push_pending_frames);
25562727 return ip_setsockopt(sk, level, optname, optval, optlen);
25572728 }
2558
-
2559
-#ifdef CONFIG_COMPAT
2560
-int compat_udp_setsockopt(struct sock *sk, int level, int optname,
2561
- char __user *optval, unsigned int optlen)
2562
-{
2563
- if (level == SOL_UDP || level == SOL_UDPLITE)
2564
- return udp_lib_setsockopt(sk, level, optname, optval, optlen,
2565
- udp_push_pending_frames);
2566
- return compat_ip_setsockopt(sk, level, optname, optval, optlen);
2567
-}
2568
-#endif
25692729
25702730 int udp_lib_getsockopt(struct sock *sk, int level, int optname,
25712731 char __user *optval, int __user *optlen)
....@@ -2602,6 +2762,10 @@
26022762 val = READ_ONCE(up->gso_size);
26032763 break;
26042764
2765
+ case UDP_GRO:
2766
+ val = up->gro_enabled;
2767
+ break;
2768
+
26052769 /* The following two cannot be changed on UDP sockets, the return is
26062770 * always 0 (which corresponds to the full checksum coverage of UDP). */
26072771 case UDPLITE_SEND_CSCOV:
....@@ -2632,20 +2796,11 @@
26322796 return ip_getsockopt(sk, level, optname, optval, optlen);
26332797 }
26342798
2635
-#ifdef CONFIG_COMPAT
2636
-int compat_udp_getsockopt(struct sock *sk, int level, int optname,
2637
- char __user *optval, int __user *optlen)
2638
-{
2639
- if (level == SOL_UDP || level == SOL_UDPLITE)
2640
- return udp_lib_getsockopt(sk, level, optname, optval, optlen);
2641
- return compat_ip_getsockopt(sk, level, optname, optval, optlen);
2642
-}
2643
-#endif
26442799 /**
26452800 * udp_poll - wait for a UDP event.
2646
- * @file - file struct
2647
- * @sock - socket
2648
- * @wait - poll table
2801
+ * @file: - file struct
2802
+ * @sock: - socket
2803
+ * @wait: - poll table
26492804 *
26502805 * This is same as datagram poll, except for the special case of
26512806 * blocking sockets. If application is using a blocking fd
....@@ -2719,10 +2874,6 @@
27192874 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
27202875 .obj_size = sizeof(struct udp_sock),
27212876 .h.udp_table = &udp_table,
2722
-#ifdef CONFIG_COMPAT
2723
- .compat_setsockopt = compat_udp_setsockopt,
2724
- .compat_getsockopt = compat_udp_getsockopt,
2725
-#endif
27262877 .diag_destroy = udp_abort,
27272878 };
27282879 EXPORT_SYMBOL(udp_prot);
....@@ -2733,9 +2884,14 @@
27332884 static struct sock *udp_get_first(struct seq_file *seq, int start)
27342885 {
27352886 struct sock *sk;
2736
- struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file));
2887
+ struct udp_seq_afinfo *afinfo;
27372888 struct udp_iter_state *state = seq->private;
27382889 struct net *net = seq_file_net(seq);
2890
+
2891
+ if (state->bpf_seq_afinfo)
2892
+ afinfo = state->bpf_seq_afinfo;
2893
+ else
2894
+ afinfo = PDE_DATA(file_inode(seq->file));
27392895
27402896 for (state->bucket = start; state->bucket <= afinfo->udp_table->mask;
27412897 ++state->bucket) {
....@@ -2748,7 +2904,8 @@
27482904 sk_for_each(sk, &hslot->head) {
27492905 if (!net_eq(sock_net(sk), net))
27502906 continue;
2751
- if (sk->sk_family == afinfo->family)
2907
+ if (afinfo->family == AF_UNSPEC ||
2908
+ sk->sk_family == afinfo->family)
27522909 goto found;
27532910 }
27542911 spin_unlock_bh(&hslot->lock);
....@@ -2760,13 +2917,20 @@
27602917
27612918 static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
27622919 {
2763
- struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file));
2920
+ struct udp_seq_afinfo *afinfo;
27642921 struct udp_iter_state *state = seq->private;
27652922 struct net *net = seq_file_net(seq);
27662923
2924
+ if (state->bpf_seq_afinfo)
2925
+ afinfo = state->bpf_seq_afinfo;
2926
+ else
2927
+ afinfo = PDE_DATA(file_inode(seq->file));
2928
+
27672929 do {
27682930 sk = sk_next(sk);
2769
- } while (sk && (!net_eq(sock_net(sk), net) || sk->sk_family != afinfo->family));
2931
+ } while (sk && (!net_eq(sock_net(sk), net) ||
2932
+ (afinfo->family != AF_UNSPEC &&
2933
+ sk->sk_family != afinfo->family)));
27702934
27712935 if (!sk) {
27722936 if (state->bucket <= afinfo->udp_table->mask)
....@@ -2811,8 +2975,13 @@
28112975
28122976 void udp_seq_stop(struct seq_file *seq, void *v)
28132977 {
2814
- struct udp_seq_afinfo *afinfo = PDE_DATA(file_inode(seq->file));
2978
+ struct udp_seq_afinfo *afinfo;
28152979 struct udp_iter_state *state = seq->private;
2980
+
2981
+ if (state->bpf_seq_afinfo)
2982
+ afinfo = state->bpf_seq_afinfo;
2983
+ else
2984
+ afinfo = PDE_DATA(file_inode(seq->file));
28162985
28172986 if (state->bucket <= afinfo->udp_table->mask)
28182987 spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock);
....@@ -2830,7 +2999,7 @@
28302999 __u16 srcp = ntohs(inet->inet_sport);
28313000
28323001 seq_printf(f, "%5d: %08X:%04X %08X:%04X"
2833
- " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
3002
+ " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u",
28343003 bucket, src, srcp, dest, destp, sp->sk_state,
28353004 sk_wmem_alloc_get(sp),
28363005 udp_rqueue_get(sp),
....@@ -2856,6 +3025,67 @@
28563025 seq_pad(seq, '\n');
28573026 return 0;
28583027 }
3028
+
3029
+#ifdef CONFIG_BPF_SYSCALL
3030
+struct bpf_iter__udp {
3031
+ __bpf_md_ptr(struct bpf_iter_meta *, meta);
3032
+ __bpf_md_ptr(struct udp_sock *, udp_sk);
3033
+ uid_t uid __aligned(8);
3034
+ int bucket __aligned(8);
3035
+};
3036
+
3037
+static int udp_prog_seq_show(struct bpf_prog *prog, struct bpf_iter_meta *meta,
3038
+ struct udp_sock *udp_sk, uid_t uid, int bucket)
3039
+{
3040
+ struct bpf_iter__udp ctx;
3041
+
3042
+ meta->seq_num--; /* skip SEQ_START_TOKEN */
3043
+ ctx.meta = meta;
3044
+ ctx.udp_sk = udp_sk;
3045
+ ctx.uid = uid;
3046
+ ctx.bucket = bucket;
3047
+ return bpf_iter_run_prog(prog, &ctx);
3048
+}
3049
+
3050
+static int bpf_iter_udp_seq_show(struct seq_file *seq, void *v)
3051
+{
3052
+ struct udp_iter_state *state = seq->private;
3053
+ struct bpf_iter_meta meta;
3054
+ struct bpf_prog *prog;
3055
+ struct sock *sk = v;
3056
+ uid_t uid;
3057
+
3058
+ if (v == SEQ_START_TOKEN)
3059
+ return 0;
3060
+
3061
+ uid = from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk));
3062
+ meta.seq = seq;
3063
+ prog = bpf_iter_get_info(&meta, false);
3064
+ return udp_prog_seq_show(prog, &meta, v, uid, state->bucket);
3065
+}
3066
+
3067
+static void bpf_iter_udp_seq_stop(struct seq_file *seq, void *v)
3068
+{
3069
+ struct bpf_iter_meta meta;
3070
+ struct bpf_prog *prog;
3071
+
3072
+ if (!v) {
3073
+ meta.seq = seq;
3074
+ prog = bpf_iter_get_info(&meta, true);
3075
+ if (prog)
3076
+ (void)udp_prog_seq_show(prog, &meta, v, 0, 0);
3077
+ }
3078
+
3079
+ udp_seq_stop(seq, v);
3080
+}
3081
+
3082
+static const struct seq_operations bpf_iter_udp_seq_ops = {
3083
+ .start = udp_seq_start,
3084
+ .next = udp_seq_next,
3085
+ .stop = bpf_iter_udp_seq_stop,
3086
+ .show = bpf_iter_udp_seq_show,
3087
+};
3088
+#endif
28593089
28603090 const struct seq_operations udp_seq_ops = {
28613091 .start = udp_seq_start,
....@@ -2974,6 +3204,62 @@
29743204 .init = udp_sysctl_init,
29753205 };
29763206
3207
+#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
3208
+DEFINE_BPF_ITER_FUNC(udp, struct bpf_iter_meta *meta,
3209
+ struct udp_sock *udp_sk, uid_t uid, int bucket)
3210
+
3211
+static int bpf_iter_init_udp(void *priv_data, struct bpf_iter_aux_info *aux)
3212
+{
3213
+ struct udp_iter_state *st = priv_data;
3214
+ struct udp_seq_afinfo *afinfo;
3215
+ int ret;
3216
+
3217
+ afinfo = kmalloc(sizeof(*afinfo), GFP_USER | __GFP_NOWARN);
3218
+ if (!afinfo)
3219
+ return -ENOMEM;
3220
+
3221
+ afinfo->family = AF_UNSPEC;
3222
+ afinfo->udp_table = &udp_table;
3223
+ st->bpf_seq_afinfo = afinfo;
3224
+ ret = bpf_iter_init_seq_net(priv_data, aux);
3225
+ if (ret)
3226
+ kfree(afinfo);
3227
+ return ret;
3228
+}
3229
+
3230
+static void bpf_iter_fini_udp(void *priv_data)
3231
+{
3232
+ struct udp_iter_state *st = priv_data;
3233
+
3234
+ kfree(st->bpf_seq_afinfo);
3235
+ bpf_iter_fini_seq_net(priv_data);
3236
+}
3237
+
3238
+static const struct bpf_iter_seq_info udp_seq_info = {
3239
+ .seq_ops = &bpf_iter_udp_seq_ops,
3240
+ .init_seq_private = bpf_iter_init_udp,
3241
+ .fini_seq_private = bpf_iter_fini_udp,
3242
+ .seq_priv_size = sizeof(struct udp_iter_state),
3243
+};
3244
+
3245
+static struct bpf_iter_reg udp_reg_info = {
3246
+ .target = "udp",
3247
+ .ctx_arg_info_size = 1,
3248
+ .ctx_arg_info = {
3249
+ { offsetof(struct bpf_iter__udp, udp_sk),
3250
+ PTR_TO_BTF_ID_OR_NULL },
3251
+ },
3252
+ .seq_info = &udp_seq_info,
3253
+};
3254
+
3255
+static void __init bpf_iter_register(void)
3256
+{
3257
+ udp_reg_info.ctx_arg_info[0].btf_id = btf_sock_ids[BTF_SOCK_TYPE_UDP];
3258
+ if (bpf_iter_reg_target(&udp_reg_info))
3259
+ pr_warn("Warning: could not register bpf iterator udp\n");
3260
+}
3261
+#endif
3262
+
29773263 void __init udp_init(void)
29783264 {
29793265 unsigned long limit;
....@@ -2999,4 +3285,8 @@
29993285
30003286 if (register_pernet_subsys(&udp_sysctl_ops))
30013287 panic("UDP: failed to init sysctl parameters.\n");
3288
+
3289
+#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
3290
+ bpf_iter_register();
3291
+#endif
30023292 }