1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80#define pr_fmt(fmt) "UDP: " fmt
81
82#include <linux/uaccess.h>
83#include <asm/ioctls.h>
84#include <linux/memblock.h>
85#include <linux/highmem.h>
86#include <linux/swap.h>
87#include <linux/types.h>
88#include <linux/fcntl.h>
89#include <linux/module.h>
90#include <linux/socket.h>
91#include <linux/sockios.h>
92#include <linux/igmp.h>
93#include <linux/inetdevice.h>
94#include <linux/in.h>
95#include <linux/errno.h>
96#include <linux/timer.h>
97#include <linux/mm.h>
98#include <linux/inet.h>
99#include <linux/netdevice.h>
100#include <linux/slab.h>
101#include <net/tcp_states.h>
102#include <linux/skbuff.h>
103#include <linux/proc_fs.h>
104#include <linux/seq_file.h>
105#include <net/net_namespace.h>
106#include <net/icmp.h>
107#include <net/inet_hashtables.h>
108#include <net/ip_tunnels.h>
109#include <net/route.h>
110#include <net/checksum.h>
111#include <net/xfrm.h>
112#include <trace/events/udp.h>
113#include <linux/static_key.h>
114#include <linux/btf_ids.h>
115#include <trace/events/skb.h>
116#include <net/busy_poll.h>
117#include "udp_impl.h"
118#include <net/sock_reuseport.h>
119#include <net/addrconf.h>
120#include <net/udp_tunnel.h>
121#if IS_ENABLED(CONFIG_IPV6)
122#include <net/ipv6_stubs.h>
123#endif
124
125struct udp_table udp_table __read_mostly;
126EXPORT_SYMBOL(udp_table);
127
128long sysctl_udp_mem[3] __read_mostly;
129EXPORT_SYMBOL(sysctl_udp_mem);
130
131atomic_long_t udp_memory_allocated;
132EXPORT_SYMBOL(udp_memory_allocated);
133
134#define MAX_UDP_PORTS 65536
135#define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN)
136
137static int udp_lib_lport_inuse(struct net *net, __u16 num,
138 const struct udp_hslot *hslot,
139 unsigned long *bitmap,
140 struct sock *sk, unsigned int log)
141{
142 struct sock *sk2;
143 kuid_t uid = sock_i_uid(sk);
144
145 sk_for_each(sk2, &hslot->head) {
146 if (net_eq(sock_net(sk2), net) &&
147 sk2 != sk &&
148 (bitmap || udp_sk(sk2)->udp_port_hash == num) &&
149 (!sk2->sk_reuse || !sk->sk_reuse) &&
150 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
151 sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
152 inet_rcv_saddr_equal(sk, sk2, true)) {
153 if (sk2->sk_reuseport && sk->sk_reuseport &&
154 !rcu_access_pointer(sk->sk_reuseport_cb) &&
155 uid_eq(uid, sock_i_uid(sk2))) {
156 if (!bitmap)
157 return 0;
158 } else {
159 if (!bitmap)
160 return 1;
161 __set_bit(udp_sk(sk2)->udp_port_hash >> log,
162 bitmap);
163 }
164 }
165 }
166 return 0;
167}
168
169
170
171
172
173static int udp_lib_lport_inuse2(struct net *net, __u16 num,
174 struct udp_hslot *hslot2,
175 struct sock *sk)
176{
177 struct sock *sk2;
178 kuid_t uid = sock_i_uid(sk);
179 int res = 0;
180
181 spin_lock(&hslot2->lock);
182 udp_portaddr_for_each_entry(sk2, &hslot2->head) {
183 if (net_eq(sock_net(sk2), net) &&
184 sk2 != sk &&
185 (udp_sk(sk2)->udp_port_hash == num) &&
186 (!sk2->sk_reuse || !sk->sk_reuse) &&
187 (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
188 sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
189 inet_rcv_saddr_equal(sk, sk2, true)) {
190 if (sk2->sk_reuseport && sk->sk_reuseport &&
191 !rcu_access_pointer(sk->sk_reuseport_cb) &&
192 uid_eq(uid, sock_i_uid(sk2))) {
193 res = 0;
194 } else {
195 res = 1;
196 }
197 break;
198 }
199 }
200 spin_unlock(&hslot2->lock);
201 return res;
202}
203
204static int udp_reuseport_add_sock(struct sock *sk, struct udp_hslot *hslot)
205{
206 struct net *net = sock_net(sk);
207 kuid_t uid = sock_i_uid(sk);
208 struct sock *sk2;
209
210 sk_for_each(sk2, &hslot->head) {
211 if (net_eq(sock_net(sk2), net) &&
212 sk2 != sk &&
213 sk2->sk_family == sk->sk_family &&
214 ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
215 (udp_sk(sk2)->udp_port_hash == udp_sk(sk)->udp_port_hash) &&
216 (sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
217 sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) &&
218 inet_rcv_saddr_equal(sk, sk2, false)) {
219 return reuseport_add_sock(sk, sk2,
220 inet_rcv_saddr_any(sk));
221 }
222 }
223
224 return reuseport_alloc(sk, inet_rcv_saddr_any(sk));
225}
226
227
228
229
230
231
232
233
234
235int udp_lib_get_port(struct sock *sk, unsigned short snum,
236 unsigned int hash2_nulladdr)
237{
238 struct udp_hslot *hslot, *hslot2;
239 struct udp_table *udptable = sk->sk_prot->h.udp_table;
240 int error = 1;
241 struct net *net = sock_net(sk);
242
243 if (!snum) {
244 int low, high, remaining;
245 unsigned int rand;
246 unsigned short first, last;
247 DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
248
249 inet_get_local_port_range(net, &low, &high);
250 remaining = (high - low) + 1;
251
252 rand = prandom_u32();
253 first = reciprocal_scale(rand, remaining) + low;
254
255
256
257 rand = (rand | 1) * (udptable->mask + 1);
258 last = first + udptable->mask + 1;
259 do {
260 hslot = udp_hashslot(udptable, net, first);
261 bitmap_zero(bitmap, PORTS_PER_CHAIN);
262 spin_lock_bh(&hslot->lock);
263 udp_lib_lport_inuse(net, snum, hslot, bitmap, sk,
264 udptable->log);
265
266 snum = first;
267
268
269
270
271
272 do {
273 if (low <= snum && snum <= high &&
274 !test_bit(snum >> udptable->log, bitmap) &&
275 !inet_is_local_reserved_port(net, snum))
276 goto found;
277 snum += rand;
278 } while (snum != first);
279 spin_unlock_bh(&hslot->lock);
280 cond_resched();
281 } while (++first != last);
282 goto fail;
283 } else {
284 hslot = udp_hashslot(udptable, net, snum);
285 spin_lock_bh(&hslot->lock);
286 if (hslot->count > 10) {
287 int exist;
288 unsigned int slot2 = udp_sk(sk)->udp_portaddr_hash ^ snum;
289
290 slot2 &= udptable->mask;
291 hash2_nulladdr &= udptable->mask;
292
293 hslot2 = udp_hashslot2(udptable, slot2);
294 if (hslot->count < hslot2->count)
295 goto scan_primary_hash;
296
297 exist = udp_lib_lport_inuse2(net, snum, hslot2, sk);
298 if (!exist && (hash2_nulladdr != slot2)) {
299 hslot2 = udp_hashslot2(udptable, hash2_nulladdr);
300 exist = udp_lib_lport_inuse2(net, snum, hslot2,
301 sk);
302 }
303 if (exist)
304 goto fail_unlock;
305 else
306 goto found;
307 }
308scan_primary_hash:
309 if (udp_lib_lport_inuse(net, snum, hslot, NULL, sk, 0))
310 goto fail_unlock;
311 }
312found:
313 inet_sk(sk)->inet_num = snum;
314 udp_sk(sk)->udp_port_hash = snum;
315 udp_sk(sk)->udp_portaddr_hash ^= snum;
316 if (sk_unhashed(sk)) {
317 if (sk->sk_reuseport &&
318 udp_reuseport_add_sock(sk, hslot)) {
319 inet_sk(sk)->inet_num = 0;
320 udp_sk(sk)->udp_port_hash = 0;
321 udp_sk(sk)->udp_portaddr_hash ^= snum;
322 goto fail_unlock;
323 }
324
325 sk_add_node_rcu(sk, &hslot->head);
326 hslot->count++;
327 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
328
329 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
330 spin_lock(&hslot2->lock);
331 if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
332 sk->sk_family == AF_INET6)
333 hlist_add_tail_rcu(&udp_sk(sk)->udp_portaddr_node,
334 &hslot2->head);
335 else
336 hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
337 &hslot2->head);
338 hslot2->count++;
339 spin_unlock(&hslot2->lock);
340 }
341 sock_set_flag(sk, SOCK_RCU_FREE);
342 error = 0;
343fail_unlock:
344 spin_unlock_bh(&hslot->lock);
345fail:
346 return error;
347}
348EXPORT_SYMBOL(udp_lib_get_port);
349
350int udp_v4_get_port(struct sock *sk, unsigned short snum)
351{
352 unsigned int hash2_nulladdr =
353 ipv4_portaddr_hash(sock_net(sk), htonl(INADDR_ANY), snum);
354 unsigned int hash2_partial =
355 ipv4_portaddr_hash(sock_net(sk), inet_sk(sk)->inet_rcv_saddr, 0);
356
357
358 udp_sk(sk)->udp_portaddr_hash = hash2_partial;
359 return udp_lib_get_port(sk, snum, hash2_nulladdr);
360}
361
362static int compute_score(struct sock *sk, struct net *net,
363 __be32 saddr, __be16 sport,
364 __be32 daddr, unsigned short hnum,
365 int dif, int sdif)
366{
367 int score;
368 struct inet_sock *inet;
369 bool dev_match;
370
371 if (!net_eq(sock_net(sk), net) ||
372 udp_sk(sk)->udp_port_hash != hnum ||
373 ipv6_only_sock(sk))
374 return -1;
375
376 if (sk->sk_rcv_saddr != daddr)
377 return -1;
378
379 score = (sk->sk_family == PF_INET) ? 2 : 1;
380
381 inet = inet_sk(sk);
382 if (inet->inet_daddr) {
383 if (inet->inet_daddr != saddr)
384 return -1;
385 score += 4;
386 }
387
388 if (inet->inet_dport) {
389 if (inet->inet_dport != sport)
390 return -1;
391 score += 4;
392 }
393
394 dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if,
395 dif, sdif);
396 if (!dev_match)
397 return -1;
398 score += 4;
399
400 if (sk->sk_incoming_cpu == raw_smp_processor_id())
401 score++;
402 return score;
403}
404
405static u32 udp_ehashfn(const struct net *net, const __be32 laddr,
406 const __u16 lport, const __be32 faddr,
407 const __be16 fport)
408{
409 static u32 udp_ehash_secret __read_mostly;
410
411 net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret));
412
413 return __inet_ehashfn(laddr, lport, faddr, fport,
414 udp_ehash_secret + net_hash_mix(net));
415}
416
417static struct sock *lookup_reuseport(struct net *net, struct sock *sk,
418 struct sk_buff *skb,
419 __be32 saddr, __be16 sport,
420 __be32 daddr, unsigned short hnum)
421{
422 struct sock *reuse_sk = NULL;
423 u32 hash;
424
425 if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) {
426 hash = udp_ehashfn(net, daddr, hnum, saddr, sport);
427 reuse_sk = reuseport_select_sock(sk, hash, skb,
428 sizeof(struct udphdr));
429 }
430 return reuse_sk;
431}
432
433
434static struct sock *udp4_lib_lookup2(struct net *net,
435 __be32 saddr, __be16 sport,
436 __be32 daddr, unsigned int hnum,
437 int dif, int sdif,
438 struct udp_hslot *hslot2,
439 struct sk_buff *skb)
440{
441 struct sock *sk, *result;
442 int score, badness;
443
444 result = NULL;
445 badness = 0;
446 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
447 score = compute_score(sk, net, saddr, sport,
448 daddr, hnum, dif, sdif);
449 if (score > badness) {
450 result = lookup_reuseport(net, sk, skb,
451 saddr, sport, daddr, hnum);
452
453 if (result && !reuseport_has_conns(sk, false))
454 return result;
455
456 result = result ? : sk;
457 badness = score;
458 }
459 }
460 return result;
461}
462
463static struct sock *udp4_lookup_run_bpf(struct net *net,
464 struct udp_table *udptable,
465 struct sk_buff *skb,
466 __be32 saddr, __be16 sport,
467 __be32 daddr, u16 hnum)
468{
469 struct sock *sk, *reuse_sk;
470 bool no_reuseport;
471
472 if (udptable != &udp_table)
473 return NULL;
474
475 no_reuseport = bpf_sk_lookup_run_v4(net, IPPROTO_UDP,
476 saddr, sport, daddr, hnum, &sk);
477 if (no_reuseport || IS_ERR_OR_NULL(sk))
478 return sk;
479
480 reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum);
481 if (reuse_sk)
482 sk = reuse_sk;
483 return sk;
484}
485
486
487
488
489struct sock *__udp4_lib_lookup(struct net *net, __be32 saddr,
490 __be16 sport, __be32 daddr, __be16 dport, int dif,
491 int sdif, struct udp_table *udptable, struct sk_buff *skb)
492{
493 unsigned short hnum = ntohs(dport);
494 unsigned int hash2, slot2;
495 struct udp_hslot *hslot2;
496 struct sock *result, *sk;
497
498 hash2 = ipv4_portaddr_hash(net, daddr, hnum);
499 slot2 = hash2 & udptable->mask;
500 hslot2 = &udptable->hash2[slot2];
501
502
503 result = udp4_lib_lookup2(net, saddr, sport,
504 daddr, hnum, dif, sdif,
505 hslot2, skb);
506 if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED)
507 goto done;
508
509
510 if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
511 sk = udp4_lookup_run_bpf(net, udptable, skb,
512 saddr, sport, daddr, hnum);
513 if (sk) {
514 result = sk;
515 goto done;
516 }
517 }
518
519
520 if (result)
521 goto done;
522
523
524 hash2 = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum);
525 slot2 = hash2 & udptable->mask;
526 hslot2 = &udptable->hash2[slot2];
527
528 result = udp4_lib_lookup2(net, saddr, sport,
529 htonl(INADDR_ANY), hnum, dif, sdif,
530 hslot2, skb);
531done:
532 if (IS_ERR(result))
533 return NULL;
534 return result;
535}
536EXPORT_SYMBOL_GPL(__udp4_lib_lookup);
537
538static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
539 __be16 sport, __be16 dport,
540 struct udp_table *udptable)
541{
542 const struct iphdr *iph = ip_hdr(skb);
543
544 return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport,
545 iph->daddr, dport, inet_iif(skb),
546 inet_sdif(skb), udptable, skb);
547}
548
549struct sock *udp4_lib_lookup_skb(const struct sk_buff *skb,
550 __be16 sport, __be16 dport)
551{
552 const struct iphdr *iph = ip_hdr(skb);
553
554 return __udp4_lib_lookup(dev_net(skb->dev), iph->saddr, sport,
555 iph->daddr, dport, inet_iif(skb),
556 inet_sdif(skb), &udp_table, NULL);
557}
558EXPORT_SYMBOL_GPL(udp4_lib_lookup_skb);
559
560
561
562
563#if IS_ENABLED(CONFIG_NF_TPROXY_IPV4) || IS_ENABLED(CONFIG_NF_SOCKET_IPV4)
564struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
565 __be32 daddr, __be16 dport, int dif)
566{
567 struct sock *sk;
568
569 sk = __udp4_lib_lookup(net, saddr, sport, daddr, dport,
570 dif, 0, &udp_table, NULL);
571 if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
572 sk = NULL;
573 return sk;
574}
575EXPORT_SYMBOL_GPL(udp4_lib_lookup);
576#endif
577
578static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk,
579 __be16 loc_port, __be32 loc_addr,
580 __be16 rmt_port, __be32 rmt_addr,
581 int dif, int sdif, unsigned short hnum)
582{
583 struct inet_sock *inet = inet_sk(sk);
584
585 if (!net_eq(sock_net(sk), net) ||
586 udp_sk(sk)->udp_port_hash != hnum ||
587 (inet->inet_daddr && inet->inet_daddr != rmt_addr) ||
588 (inet->inet_dport != rmt_port && inet->inet_dport) ||
589 (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) ||
590 ipv6_only_sock(sk) ||
591 !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
592 return false;
593 if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif, sdif))
594 return false;
595 return true;
596}
597
598DEFINE_STATIC_KEY_FALSE(udp_encap_needed_key);
599void udp_encap_enable(void)
600{
601 static_branch_inc(&udp_encap_needed_key);
602}
603EXPORT_SYMBOL(udp_encap_enable);
604
605void udp_encap_disable(void)
606{
607 static_branch_dec(&udp_encap_needed_key);
608}
609EXPORT_SYMBOL(udp_encap_disable);
610
611
612
613
614static int __udp4_lib_err_encap_no_sk(struct sk_buff *skb, u32 info)
615{
616 int i;
617
618 for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) {
619 int (*handler)(struct sk_buff *skb, u32 info);
620 const struct ip_tunnel_encap_ops *encap;
621
622 encap = rcu_dereference(iptun_encaps[i]);
623 if (!encap)
624 continue;
625 handler = encap->err_handler;
626 if (handler && !handler(skb, info))
627 return 0;
628 }
629
630 return -ENOENT;
631}
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650static struct sock *__udp4_lib_err_encap(struct net *net,
651 const struct iphdr *iph,
652 struct udphdr *uh,
653 struct udp_table *udptable,
654 struct sock *sk,
655 struct sk_buff *skb, u32 info)
656{
657 int (*lookup)(struct sock *sk, struct sk_buff *skb);
658 int network_offset, transport_offset;
659 struct udp_sock *up;
660
661 network_offset = skb_network_offset(skb);
662 transport_offset = skb_transport_offset(skb);
663
664
665 skb_reset_network_header(skb);
666
667
668 skb_set_transport_header(skb, iph->ihl << 2);
669
670 if (sk) {
671 up = udp_sk(sk);
672
673 lookup = READ_ONCE(up->encap_err_lookup);
674 if (lookup && lookup(sk, skb))
675 sk = NULL;
676
677 goto out;
678 }
679
680 sk = __udp4_lib_lookup(net, iph->daddr, uh->source,
681 iph->saddr, uh->dest, skb->dev->ifindex, 0,
682 udptable, NULL);
683 if (sk) {
684 up = udp_sk(sk);
685
686 lookup = READ_ONCE(up->encap_err_lookup);
687 if (!lookup || lookup(sk, skb))
688 sk = NULL;
689 }
690
691out:
692 if (!sk)
693 sk = ERR_PTR(__udp4_lib_err_encap_no_sk(skb, info));
694
695 skb_set_transport_header(skb, transport_offset);
696 skb_set_network_header(skb, network_offset);
697
698 return sk;
699}
700
701
702
703
704
705
706
707
708
709
710
711
712int __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
713{
714 struct inet_sock *inet;
715 const struct iphdr *iph = (const struct iphdr *)skb->data;
716 struct udphdr *uh = (struct udphdr *)(skb->data+(iph->ihl<<2));
717 const int type = icmp_hdr(skb)->type;
718 const int code = icmp_hdr(skb)->code;
719 bool tunnel = false;
720 struct sock *sk;
721 int harderr;
722 int err;
723 struct net *net = dev_net(skb->dev);
724
725 sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
726 iph->saddr, uh->source, skb->dev->ifindex,
727 inet_sdif(skb), udptable, NULL);
728
729 if (!sk || udp_sk(sk)->encap_type) {
730
731 if (static_branch_unlikely(&udp_encap_needed_key)) {
732 sk = __udp4_lib_err_encap(net, iph, uh, udptable, sk, skb,
733 info);
734 if (!sk)
735 return 0;
736 } else
737 sk = ERR_PTR(-ENOENT);
738
739 if (IS_ERR(sk)) {
740 __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
741 return PTR_ERR(sk);
742 }
743
744 tunnel = true;
745 }
746
747 err = 0;
748 harderr = 0;
749 inet = inet_sk(sk);
750
751 switch (type) {
752 default:
753 case ICMP_TIME_EXCEEDED:
754 err = EHOSTUNREACH;
755 break;
756 case ICMP_SOURCE_QUENCH:
757 goto out;
758 case ICMP_PARAMETERPROB:
759 err = EPROTO;
760 harderr = 1;
761 break;
762 case ICMP_DEST_UNREACH:
763 if (code == ICMP_FRAG_NEEDED) {
764 ipv4_sk_update_pmtu(skb, sk, info);
765 if (inet->pmtudisc != IP_PMTUDISC_DONT) {
766 err = EMSGSIZE;
767 harderr = 1;
768 break;
769 }
770 goto out;
771 }
772 err = EHOSTUNREACH;
773 if (code <= NR_ICMP_UNREACH) {
774 harderr = icmp_err_convert[code].fatal;
775 err = icmp_err_convert[code].errno;
776 }
777 break;
778 case ICMP_REDIRECT:
779 ipv4_sk_redirect(skb, sk);
780 goto out;
781 }
782
783
784
785
786
787 if (tunnel) {
788
789 goto out;
790 }
791 if (!inet->recverr) {
792 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
793 goto out;
794 } else
795 ip_icmp_error(sk, skb, err, uh->dest, info, (u8 *)(uh+1));
796
797 sk->sk_err = err;
798 sk_error_report(sk);
799out:
800 return 0;
801}
802
803int udp_err(struct sk_buff *skb, u32 info)
804{
805 return __udp4_lib_err(skb, info, &udp_table);
806}
807
808
809
810
811void udp_flush_pending_frames(struct sock *sk)
812{
813 struct udp_sock *up = udp_sk(sk);
814
815 if (up->pending) {
816 up->len = 0;
817 up->pending = 0;
818 ip_flush_pending_frames(sk);
819 }
820}
821EXPORT_SYMBOL(udp_flush_pending_frames);
822
823
824
825
826
827
828
829
830void udp4_hwcsum(struct sk_buff *skb, __be32 src, __be32 dst)
831{
832 struct udphdr *uh = udp_hdr(skb);
833 int offset = skb_transport_offset(skb);
834 int len = skb->len - offset;
835 int hlen = len;
836 __wsum csum = 0;
837
838 if (!skb_has_frag_list(skb)) {
839
840
841
842 skb->csum_start = skb_transport_header(skb) - skb->head;
843 skb->csum_offset = offsetof(struct udphdr, check);
844 uh->check = ~csum_tcpudp_magic(src, dst, len,
845 IPPROTO_UDP, 0);
846 } else {
847 struct sk_buff *frags;
848
849
850
851
852
853
854 skb_walk_frags(skb, frags) {
855 csum = csum_add(csum, frags->csum);
856 hlen -= frags->len;
857 }
858
859 csum = skb_checksum(skb, offset, hlen, csum);
860 skb->ip_summed = CHECKSUM_NONE;
861
862 uh->check = csum_tcpudp_magic(src, dst, len, IPPROTO_UDP, csum);
863 if (uh->check == 0)
864 uh->check = CSUM_MANGLED_0;
865 }
866}
867EXPORT_SYMBOL_GPL(udp4_hwcsum);
868
869
870
871
872void udp_set_csum(bool nocheck, struct sk_buff *skb,
873 __be32 saddr, __be32 daddr, int len)
874{
875 struct udphdr *uh = udp_hdr(skb);
876
877 if (nocheck) {
878 uh->check = 0;
879 } else if (skb_is_gso(skb)) {
880 uh->check = ~udp_v4_check(len, saddr, daddr, 0);
881 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
882 uh->check = 0;
883 uh->check = udp_v4_check(len, saddr, daddr, lco_csum(skb));
884 if (uh->check == 0)
885 uh->check = CSUM_MANGLED_0;
886 } else {
887 skb->ip_summed = CHECKSUM_PARTIAL;
888 skb->csum_start = skb_transport_header(skb) - skb->head;
889 skb->csum_offset = offsetof(struct udphdr, check);
890 uh->check = ~udp_v4_check(len, saddr, daddr, 0);
891 }
892}
893EXPORT_SYMBOL(udp_set_csum);
894
895static int udp_send_skb(struct sk_buff *skb, struct flowi4 *fl4,
896 struct inet_cork *cork)
897{
898 struct sock *sk = skb->sk;
899 struct inet_sock *inet = inet_sk(sk);
900 struct udphdr *uh;
901 int err = 0;
902 int is_udplite = IS_UDPLITE(sk);
903 int offset = skb_transport_offset(skb);
904 int len = skb->len - offset;
905 int datalen = len - sizeof(*uh);
906 __wsum csum = 0;
907
908
909
910
911 uh = udp_hdr(skb);
912 uh->source = inet->inet_sport;
913 uh->dest = fl4->fl4_dport;
914 uh->len = htons(len);
915 uh->check = 0;
916
917 if (cork->gso_size) {
918 const int hlen = skb_network_header_len(skb) +
919 sizeof(struct udphdr);
920
921 if (hlen + cork->gso_size > cork->fragsize) {
922 kfree_skb(skb);
923 return -EINVAL;
924 }
925 if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) {
926 kfree_skb(skb);
927 return -EINVAL;
928 }
929 if (sk->sk_no_check_tx) {
930 kfree_skb(skb);
931 return -EINVAL;
932 }
933 if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite ||
934 dst_xfrm(skb_dst(skb))) {
935 kfree_skb(skb);
936 return -EIO;
937 }
938
939 if (datalen > cork->gso_size) {
940 skb_shinfo(skb)->gso_size = cork->gso_size;
941 skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
942 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen,
943 cork->gso_size);
944 }
945 goto csum_partial;
946 }
947
948 if (is_udplite)
949 csum = udplite_csum(skb);
950
951 else if (sk->sk_no_check_tx) {
952
953 skb->ip_summed = CHECKSUM_NONE;
954 goto send;
955
956 } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
957csum_partial:
958
959 udp4_hwcsum(skb, fl4->saddr, fl4->daddr);
960 goto send;
961
962 } else
963 csum = udp_csum(skb);
964
965
966 uh->check = csum_tcpudp_magic(fl4->saddr, fl4->daddr, len,
967 sk->sk_protocol, csum);
968 if (uh->check == 0)
969 uh->check = CSUM_MANGLED_0;
970
971send:
972 err = ip_send_skb(sock_net(sk), skb);
973 if (err) {
974 if (err == -ENOBUFS && !inet->recverr) {
975 UDP_INC_STATS(sock_net(sk),
976 UDP_MIB_SNDBUFERRORS, is_udplite);
977 err = 0;
978 }
979 } else
980 UDP_INC_STATS(sock_net(sk),
981 UDP_MIB_OUTDATAGRAMS, is_udplite);
982 return err;
983}
984
985
986
987
988int udp_push_pending_frames(struct sock *sk)
989{
990 struct udp_sock *up = udp_sk(sk);
991 struct inet_sock *inet = inet_sk(sk);
992 struct flowi4 *fl4 = &inet->cork.fl.u.ip4;
993 struct sk_buff *skb;
994 int err = 0;
995
996 skb = ip_finish_skb(sk, fl4);
997 if (!skb)
998 goto out;
999
1000 err = udp_send_skb(skb, fl4, &inet->cork.base);
1001
1002out:
1003 up->len = 0;
1004 up->pending = 0;
1005 return err;
1006}
1007EXPORT_SYMBOL(udp_push_pending_frames);
1008
1009static int __udp_cmsg_send(struct cmsghdr *cmsg, u16 *gso_size)
1010{
1011 switch (cmsg->cmsg_type) {
1012 case UDP_SEGMENT:
1013 if (cmsg->cmsg_len != CMSG_LEN(sizeof(__u16)))
1014 return -EINVAL;
1015 *gso_size = *(__u16 *)CMSG_DATA(cmsg);
1016 return 0;
1017 default:
1018 return -EINVAL;
1019 }
1020}
1021
1022int udp_cmsg_send(struct sock *sk, struct msghdr *msg, u16 *gso_size)
1023{
1024 struct cmsghdr *cmsg;
1025 bool need_ip = false;
1026 int err;
1027
1028 for_each_cmsghdr(cmsg, msg) {
1029 if (!CMSG_OK(msg, cmsg))
1030 return -EINVAL;
1031
1032 if (cmsg->cmsg_level != SOL_UDP) {
1033 need_ip = true;
1034 continue;
1035 }
1036
1037 err = __udp_cmsg_send(cmsg, gso_size);
1038 if (err)
1039 return err;
1040 }
1041
1042 return need_ip;
1043}
1044EXPORT_SYMBOL_GPL(udp_cmsg_send);
1045
1046int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1047{
1048 struct inet_sock *inet = inet_sk(sk);
1049 struct udp_sock *up = udp_sk(sk);
1050 DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
1051 struct flowi4 fl4_stack;
1052 struct flowi4 *fl4;
1053 int ulen = len;
1054 struct ipcm_cookie ipc;
1055 struct rtable *rt = NULL;
1056 int free = 0;
1057 int connected = 0;
1058 __be32 daddr, faddr, saddr;
1059 __be16 dport;
1060 u8 tos;
1061 int err, is_udplite = IS_UDPLITE(sk);
1062 int corkreq = READ_ONCE(up->corkflag) || msg->msg_flags&MSG_MORE;
1063 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1064 struct sk_buff *skb;
1065 struct ip_options_data opt_copy;
1066
1067 if (len > 0xFFFF)
1068 return -EMSGSIZE;
1069
1070
1071
1072
1073
1074 if (msg->msg_flags & MSG_OOB)
1075 return -EOPNOTSUPP;
1076
1077 ipc.opt = NULL;
1078 ipc.tx_flags = 0;
1079 ipc.ttl = 0;
1080 ipc.tos = -1;
1081 ipc.sockc.transmit_time = 0;
1082
1083 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
1084
1085 fl4 = &inet->cork.fl.u.ip4;
1086 if (up->pending) {
1087
1088
1089
1090
1091 lock_sock(sk);
1092 if (likely(up->pending)) {
1093 if (unlikely(up->pending != AF_INET)) {
1094 release_sock(sk);
1095 return -EINVAL;
1096 }
1097 goto do_append_data;
1098 }
1099 release_sock(sk);
1100 }
1101 ulen += sizeof(struct udphdr);
1102
1103
1104
1105
1106 if (usin) {
1107 if (msg->msg_namelen < sizeof(*usin))
1108 return -EINVAL;
1109 if (usin->sin_family != AF_INET) {
1110 if (usin->sin_family != AF_UNSPEC)
1111 return -EAFNOSUPPORT;
1112 }
1113
1114 daddr = usin->sin_addr.s_addr;
1115 dport = usin->sin_port;
1116 if (dport == 0)
1117 return -EINVAL;
1118 } else {
1119 if (sk->sk_state != TCP_ESTABLISHED)
1120 return -EDESTADDRREQ;
1121 daddr = inet->inet_daddr;
1122 dport = inet->inet_dport;
1123
1124
1125
1126 connected = 1;
1127 }
1128
1129 ipc.sockc.tsflags = sk->sk_tsflags;
1130 ipc.addr = inet->inet_saddr;
1131 ipc.oif = sk->sk_bound_dev_if;
1132 ipc.gso_size = READ_ONCE(up->gso_size);
1133
1134 if (msg->msg_controllen) {
1135 err = udp_cmsg_send(sk, msg, &ipc.gso_size);
1136 if (err > 0)
1137 err = ip_cmsg_send(sk, msg, &ipc,
1138 sk->sk_family == AF_INET6);
1139 if (unlikely(err < 0)) {
1140 kfree(ipc.opt);
1141 return err;
1142 }
1143 if (ipc.opt)
1144 free = 1;
1145 connected = 0;
1146 }
1147 if (!ipc.opt) {
1148 struct ip_options_rcu *inet_opt;
1149
1150 rcu_read_lock();
1151 inet_opt = rcu_dereference(inet->inet_opt);
1152 if (inet_opt) {
1153 memcpy(&opt_copy, inet_opt,
1154 sizeof(*inet_opt) + inet_opt->opt.optlen);
1155 ipc.opt = &opt_copy.opt;
1156 }
1157 rcu_read_unlock();
1158 }
1159
1160 if (cgroup_bpf_enabled(BPF_CGROUP_UDP4_SENDMSG) && !connected) {
1161 err = BPF_CGROUP_RUN_PROG_UDP4_SENDMSG_LOCK(sk,
1162 (struct sockaddr *)usin, &ipc.addr);
1163 if (err)
1164 goto out_free;
1165 if (usin) {
1166 if (usin->sin_port == 0) {
1167
1168 err = -EINVAL;
1169 goto out_free;
1170 }
1171 daddr = usin->sin_addr.s_addr;
1172 dport = usin->sin_port;
1173 }
1174 }
1175
1176 saddr = ipc.addr;
1177 ipc.addr = faddr = daddr;
1178
1179 sock_tx_timestamp(sk, ipc.sockc.tsflags, &ipc.tx_flags);
1180
1181 if (ipc.opt && ipc.opt->opt.srr) {
1182 if (!daddr) {
1183 err = -EINVAL;
1184 goto out_free;
1185 }
1186 faddr = ipc.opt->opt.faddr;
1187 connected = 0;
1188 }
1189 tos = get_rttos(&ipc, inet);
1190 if (sock_flag(sk, SOCK_LOCALROUTE) ||
1191 (msg->msg_flags & MSG_DONTROUTE) ||
1192 (ipc.opt && ipc.opt->opt.is_strictroute)) {
1193 tos |= RTO_ONLINK;
1194 connected = 0;
1195 }
1196
1197 if (ipv4_is_multicast(daddr)) {
1198 if (!ipc.oif || netif_index_is_l3_master(sock_net(sk), ipc.oif))
1199 ipc.oif = inet->mc_index;
1200 if (!saddr)
1201 saddr = inet->mc_addr;
1202 connected = 0;
1203 } else if (!ipc.oif) {
1204 ipc.oif = inet->uc_index;
1205 } else if (ipv4_is_lbcast(daddr) && inet->uc_index) {
1206
1207
1208
1209
1210
1211
1212 if (ipc.oif != inet->uc_index &&
1213 ipc.oif == l3mdev_master_ifindex_by_index(sock_net(sk),
1214 inet->uc_index)) {
1215 ipc.oif = inet->uc_index;
1216 }
1217 }
1218
1219 if (connected)
1220 rt = (struct rtable *)sk_dst_check(sk, 0);
1221
1222 if (!rt) {
1223 struct net *net = sock_net(sk);
1224 __u8 flow_flags = inet_sk_flowi_flags(sk);
1225
1226 fl4 = &fl4_stack;
1227
1228 flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos,
1229 RT_SCOPE_UNIVERSE, sk->sk_protocol,
1230 flow_flags,
1231 faddr, saddr, dport, inet->inet_sport,
1232 sk->sk_uid);
1233
1234 security_sk_classify_flow(sk, flowi4_to_flowi(fl4));
1235 rt = ip_route_output_flow(net, fl4, sk);
1236 if (IS_ERR(rt)) {
1237 err = PTR_ERR(rt);
1238 rt = NULL;
1239 if (err == -ENETUNREACH)
1240 IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
1241 goto out;
1242 }
1243
1244 err = -EACCES;
1245 if ((rt->rt_flags & RTCF_BROADCAST) &&
1246 !sock_flag(sk, SOCK_BROADCAST))
1247 goto out;
1248 if (connected)
1249 sk_dst_set(sk, dst_clone(&rt->dst));
1250 }
1251
1252 if (msg->msg_flags&MSG_CONFIRM)
1253 goto do_confirm;
1254back_from_confirm:
1255
1256 saddr = fl4->saddr;
1257 if (!ipc.addr)
1258 daddr = ipc.addr = fl4->daddr;
1259
1260
1261 if (!corkreq) {
1262 struct inet_cork cork;
1263
1264 skb = ip_make_skb(sk, fl4, getfrag, msg, ulen,
1265 sizeof(struct udphdr), &ipc, &rt,
1266 &cork, msg->msg_flags);
1267 err = PTR_ERR(skb);
1268 if (!IS_ERR_OR_NULL(skb))
1269 err = udp_send_skb(skb, fl4, &cork);
1270 goto out;
1271 }
1272
1273 lock_sock(sk);
1274 if (unlikely(up->pending)) {
1275
1276
1277 release_sock(sk);
1278
1279 net_dbg_ratelimited("socket already corked\n");
1280 err = -EINVAL;
1281 goto out;
1282 }
1283
1284
1285
1286 fl4 = &inet->cork.fl.u.ip4;
1287 fl4->daddr = daddr;
1288 fl4->saddr = saddr;
1289 fl4->fl4_dport = dport;
1290 fl4->fl4_sport = inet->inet_sport;
1291 up->pending = AF_INET;
1292
1293do_append_data:
1294 up->len += ulen;
1295 err = ip_append_data(sk, fl4, getfrag, msg, ulen,
1296 sizeof(struct udphdr), &ipc, &rt,
1297 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1298 if (err)
1299 udp_flush_pending_frames(sk);
1300 else if (!corkreq)
1301 err = udp_push_pending_frames(sk);
1302 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1303 up->pending = 0;
1304 release_sock(sk);
1305
1306out:
1307 ip_rt_put(rt);
1308out_free:
1309 if (free)
1310 kfree(ipc.opt);
1311 if (!err)
1312 return len;
1313
1314
1315
1316
1317
1318
1319
1320 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1321 UDP_INC_STATS(sock_net(sk),
1322 UDP_MIB_SNDBUFERRORS, is_udplite);
1323 }
1324 return err;
1325
1326do_confirm:
1327 if (msg->msg_flags & MSG_PROBE)
1328 dst_confirm_neigh(&rt->dst, &fl4->daddr);
1329 if (!(msg->msg_flags&MSG_PROBE) || len)
1330 goto back_from_confirm;
1331 err = 0;
1332 goto out;
1333}
1334EXPORT_SYMBOL(udp_sendmsg);
1335
1336int udp_sendpage(struct sock *sk, struct page *page, int offset,
1337 size_t size, int flags)
1338{
1339 struct inet_sock *inet = inet_sk(sk);
1340 struct udp_sock *up = udp_sk(sk);
1341 int ret;
1342
1343 if (flags & MSG_SENDPAGE_NOTLAST)
1344 flags |= MSG_MORE;
1345
1346 if (!up->pending) {
1347 struct msghdr msg = { .msg_flags = flags|MSG_MORE };
1348
1349
1350
1351
1352
1353 ret = udp_sendmsg(sk, &msg, 0);
1354 if (ret < 0)
1355 return ret;
1356 }
1357
1358 lock_sock(sk);
1359
1360 if (unlikely(!up->pending)) {
1361 release_sock(sk);
1362
1363 net_dbg_ratelimited("cork failed\n");
1364 return -EINVAL;
1365 }
1366
1367 ret = ip_append_page(sk, &inet->cork.fl.u.ip4,
1368 page, offset, size, flags);
1369 if (ret == -EOPNOTSUPP) {
1370 release_sock(sk);
1371 return sock_no_sendpage(sk->sk_socket, page, offset,
1372 size, flags);
1373 }
1374 if (ret < 0) {
1375 udp_flush_pending_frames(sk);
1376 goto out;
1377 }
1378
1379 up->len += size;
1380 if (!(READ_ONCE(up->corkflag) || (flags&MSG_MORE)))
1381 ret = udp_push_pending_frames(sk);
1382 if (!ret)
1383 ret = size;
1384out:
1385 release_sock(sk);
1386 return ret;
1387}
1388
1389#define UDP_SKB_IS_STATELESS 0x80000000
1390
1391static void udp_set_dev_scratch(struct sk_buff *skb)
1392{
1393 struct udp_dev_scratch *scratch = udp_skb_scratch(skb);
1394
1395 BUILD_BUG_ON(sizeof(struct udp_dev_scratch) > sizeof(long));
1396 scratch->_tsize_state = skb->truesize;
1397#if BITS_PER_LONG == 64
1398 scratch->len = skb->len;
1399 scratch->csum_unnecessary = !!skb_csum_unnecessary(skb);
1400 scratch->is_linear = !skb_is_nonlinear(skb);
1401#endif
1402
1403
1404
1405
1406 if (likely(!skb_sec_path(skb)))
1407 scratch->_tsize_state |= UDP_SKB_IS_STATELESS;
1408}
1409
1410static void udp_skb_csum_unnecessary_set(struct sk_buff *skb)
1411{
1412
1413
1414
1415
1416
1417
1418#if BITS_PER_LONG == 64
1419 if (!skb_shared(skb))
1420 udp_skb_scratch(skb)->csum_unnecessary = true;
1421#endif
1422}
1423
1424static int udp_skb_truesize(struct sk_buff *skb)
1425{
1426 return udp_skb_scratch(skb)->_tsize_state & ~UDP_SKB_IS_STATELESS;
1427}
1428
1429static bool udp_skb_has_head_state(struct sk_buff *skb)
1430{
1431 return !(udp_skb_scratch(skb)->_tsize_state & UDP_SKB_IS_STATELESS);
1432}
1433
1434
1435static void udp_rmem_release(struct sock *sk, int size, int partial,
1436 bool rx_queue_lock_held)
1437{
1438 struct udp_sock *up = udp_sk(sk);
1439 struct sk_buff_head *sk_queue;
1440 int amt;
1441
1442 if (likely(partial)) {
1443 up->forward_deficit += size;
1444 size = up->forward_deficit;
1445 if (size < (sk->sk_rcvbuf >> 2) &&
1446 !skb_queue_empty(&up->reader_queue))
1447 return;
1448 } else {
1449 size += up->forward_deficit;
1450 }
1451 up->forward_deficit = 0;
1452
1453
1454
1455
1456 sk_queue = &sk->sk_receive_queue;
1457 if (!rx_queue_lock_held)
1458 spin_lock(&sk_queue->lock);
1459
1460
1461 sk->sk_forward_alloc += size;
1462 amt = (sk->sk_forward_alloc - partial) & ~(SK_MEM_QUANTUM - 1);
1463 sk->sk_forward_alloc -= amt;
1464
1465 if (amt)
1466 __sk_mem_reduce_allocated(sk, amt >> SK_MEM_QUANTUM_SHIFT);
1467
1468 atomic_sub(size, &sk->sk_rmem_alloc);
1469
1470
1471 skb_queue_splice_tail_init(sk_queue, &up->reader_queue);
1472
1473 if (!rx_queue_lock_held)
1474 spin_unlock(&sk_queue->lock);
1475}
1476
1477
1478
1479
1480
1481
1482void udp_skb_destructor(struct sock *sk, struct sk_buff *skb)
1483{
1484 prefetch(&skb->data);
1485 udp_rmem_release(sk, udp_skb_truesize(skb), 1, false);
1486}
1487EXPORT_SYMBOL(udp_skb_destructor);
1488
1489
1490static void udp_skb_dtor_locked(struct sock *sk, struct sk_buff *skb)
1491{
1492 prefetch(&skb->data);
1493 udp_rmem_release(sk, udp_skb_truesize(skb), 1, true);
1494}
1495
1496
1497
1498
1499
1500
1501
1502
1503static int udp_busylocks_log __read_mostly;
1504static spinlock_t *udp_busylocks __read_mostly;
1505
1506static spinlock_t *busylock_acquire(void *ptr)
1507{
1508 spinlock_t *busy;
1509
1510 busy = udp_busylocks + hash_ptr(ptr, udp_busylocks_log);
1511 spin_lock(busy);
1512 return busy;
1513}
1514
1515static void busylock_release(spinlock_t *busy)
1516{
1517 if (busy)
1518 spin_unlock(busy);
1519}
1520
1521int __udp_enqueue_schedule_skb(struct sock *sk, struct sk_buff *skb)
1522{
1523 struct sk_buff_head *list = &sk->sk_receive_queue;
1524 int rmem, delta, amt, err = -ENOMEM;
1525 spinlock_t *busy = NULL;
1526 int size;
1527
1528
1529
1530
1531 rmem = atomic_read(&sk->sk_rmem_alloc);
1532 if (rmem > sk->sk_rcvbuf)
1533 goto drop;
1534
1535
1536
1537
1538
1539
1540
1541 if (rmem > (sk->sk_rcvbuf >> 1)) {
1542 skb_condense(skb);
1543
1544 busy = busylock_acquire(sk);
1545 }
1546 size = skb->truesize;
1547 udp_set_dev_scratch(skb);
1548
1549
1550
1551
1552 rmem = atomic_add_return(size, &sk->sk_rmem_alloc);
1553 if (rmem > (size + (unsigned int)sk->sk_rcvbuf))
1554 goto uncharge_drop;
1555
1556 spin_lock(&list->lock);
1557 if (size >= sk->sk_forward_alloc) {
1558 amt = sk_mem_pages(size);
1559 delta = amt << SK_MEM_QUANTUM_SHIFT;
1560 if (!__sk_mem_raise_allocated(sk, delta, amt, SK_MEM_RECV)) {
1561 err = -ENOBUFS;
1562 spin_unlock(&list->lock);
1563 goto uncharge_drop;
1564 }
1565
1566 sk->sk_forward_alloc += delta;
1567 }
1568
1569 sk->sk_forward_alloc -= size;
1570
1571
1572
1573
1574 sock_skb_set_dropcount(sk, skb);
1575
1576 __skb_queue_tail(list, skb);
1577 spin_unlock(&list->lock);
1578
1579 if (!sock_flag(sk, SOCK_DEAD))
1580 sk->sk_data_ready(sk);
1581
1582 busylock_release(busy);
1583 return 0;
1584
1585uncharge_drop:
1586 atomic_sub(skb->truesize, &sk->sk_rmem_alloc);
1587
1588drop:
1589 atomic_inc(&sk->sk_drops);
1590 busylock_release(busy);
1591 return err;
1592}
1593EXPORT_SYMBOL_GPL(__udp_enqueue_schedule_skb);
1594
1595void udp_destruct_sock(struct sock *sk)
1596{
1597
1598 struct udp_sock *up = udp_sk(sk);
1599 unsigned int total = 0;
1600 struct sk_buff *skb;
1601
1602 skb_queue_splice_tail_init(&sk->sk_receive_queue, &up->reader_queue);
1603 while ((skb = __skb_dequeue(&up->reader_queue)) != NULL) {
1604 total += skb->truesize;
1605 kfree_skb(skb);
1606 }
1607 udp_rmem_release(sk, total, 0, true);
1608
1609 inet_sock_destruct(sk);
1610}
1611EXPORT_SYMBOL_GPL(udp_destruct_sock);
1612
1613int udp_init_sock(struct sock *sk)
1614{
1615 skb_queue_head_init(&udp_sk(sk)->reader_queue);
1616 sk->sk_destruct = udp_destruct_sock;
1617 return 0;
1618}
1619EXPORT_SYMBOL_GPL(udp_init_sock);
1620
1621void skb_consume_udp(struct sock *sk, struct sk_buff *skb, int len)
1622{
1623 if (unlikely(READ_ONCE(sk->sk_peek_off) >= 0)) {
1624 bool slow = lock_sock_fast(sk);
1625
1626 sk_peek_offset_bwd(sk, len);
1627 unlock_sock_fast(sk, slow);
1628 }
1629
1630 if (!skb_unref(skb))
1631 return;
1632
1633
1634
1635
1636 if (unlikely(udp_skb_has_head_state(skb)))
1637 skb_release_head_state(skb);
1638 __consume_stateless_skb(skb);
1639}
1640EXPORT_SYMBOL_GPL(skb_consume_udp);
1641
1642static struct sk_buff *__first_packet_length(struct sock *sk,
1643 struct sk_buff_head *rcvq,
1644 int *total)
1645{
1646 struct sk_buff *skb;
1647
1648 while ((skb = skb_peek(rcvq)) != NULL) {
1649 if (udp_lib_checksum_complete(skb)) {
1650 __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS,
1651 IS_UDPLITE(sk));
1652 __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
1653 IS_UDPLITE(sk));
1654 atomic_inc(&sk->sk_drops);
1655 __skb_unlink(skb, rcvq);
1656 *total += skb->truesize;
1657 kfree_skb(skb);
1658 } else {
1659 udp_skb_csum_unnecessary_set(skb);
1660 break;
1661 }
1662 }
1663 return skb;
1664}
1665
1666
1667
1668
1669
1670
1671
1672
1673static int first_packet_length(struct sock *sk)
1674{
1675 struct sk_buff_head *rcvq = &udp_sk(sk)->reader_queue;
1676 struct sk_buff_head *sk_queue = &sk->sk_receive_queue;
1677 struct sk_buff *skb;
1678 int total = 0;
1679 int res;
1680
1681 spin_lock_bh(&rcvq->lock);
1682 skb = __first_packet_length(sk, rcvq, &total);
1683 if (!skb && !skb_queue_empty(sk_queue)) {
1684 spin_lock(&sk_queue->lock);
1685 skb_queue_splice_tail_init(sk_queue, rcvq);
1686 spin_unlock(&sk_queue->lock);
1687
1688 skb = __first_packet_length(sk, rcvq, &total);
1689 }
1690 res = skb ? skb->len : -1;
1691 if (total)
1692 udp_rmem_release(sk, total, 1, false);
1693 spin_unlock_bh(&rcvq->lock);
1694 return res;
1695}
1696
1697
1698
1699
1700
1701int udp_ioctl(struct sock *sk, int cmd, unsigned long arg)
1702{
1703 switch (cmd) {
1704 case SIOCOUTQ:
1705 {
1706 int amount = sk_wmem_alloc_get(sk);
1707
1708 return put_user(amount, (int __user *)arg);
1709 }
1710
1711 case SIOCINQ:
1712 {
1713 int amount = max_t(int, 0, first_packet_length(sk));
1714
1715 return put_user(amount, (int __user *)arg);
1716 }
1717
1718 default:
1719 return -ENOIOCTLCMD;
1720 }
1721
1722 return 0;
1723}
1724EXPORT_SYMBOL(udp_ioctl);
1725
1726struct sk_buff *__skb_recv_udp(struct sock *sk, unsigned int flags,
1727 int noblock, int *off, int *err)
1728{
1729 struct sk_buff_head *sk_queue = &sk->sk_receive_queue;
1730 struct sk_buff_head *queue;
1731 struct sk_buff *last;
1732 long timeo;
1733 int error;
1734
1735 queue = &udp_sk(sk)->reader_queue;
1736 flags |= noblock ? MSG_DONTWAIT : 0;
1737 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1738 do {
1739 struct sk_buff *skb;
1740
1741 error = sock_error(sk);
1742 if (error)
1743 break;
1744
1745 error = -EAGAIN;
1746 do {
1747 spin_lock_bh(&queue->lock);
1748 skb = __skb_try_recv_from_queue(sk, queue, flags,
1749 udp_skb_destructor,
1750 off, err, &last);
1751 if (skb) {
1752 spin_unlock_bh(&queue->lock);
1753 return skb;
1754 }
1755
1756 if (skb_queue_empty(sk_queue)) {
1757 spin_unlock_bh(&queue->lock);
1758 goto busy_check;
1759 }
1760
1761
1762
1763
1764
1765
1766 spin_lock(&sk_queue->lock);
1767 skb_queue_splice_tail_init(sk_queue, queue);
1768
1769 skb = __skb_try_recv_from_queue(sk, queue, flags,
1770 udp_skb_dtor_locked,
1771 off, err, &last);
1772 spin_unlock(&sk_queue->lock);
1773 spin_unlock_bh(&queue->lock);
1774 if (skb)
1775 return skb;
1776
1777busy_check:
1778 if (!sk_can_busy_loop(sk))
1779 break;
1780
1781 sk_busy_loop(sk, flags & MSG_DONTWAIT);
1782 } while (!skb_queue_empty(sk_queue));
1783
1784
1785 } while (timeo &&
1786 !__skb_wait_for_more_packets(sk, &sk->sk_receive_queue,
1787 &error, &timeo,
1788 (struct sk_buff *)sk_queue));
1789
1790 *err = error;
1791 return NULL;
1792}
1793EXPORT_SYMBOL(__skb_recv_udp);
1794
1795int udp_read_sock(struct sock *sk, read_descriptor_t *desc,
1796 sk_read_actor_t recv_actor)
1797{
1798 int copied = 0;
1799
1800 while (1) {
1801 struct sk_buff *skb;
1802 int err, used;
1803
1804 skb = skb_recv_udp(sk, 0, 1, &err);
1805 if (!skb)
1806 return err;
1807
1808 if (udp_lib_checksum_complete(skb)) {
1809 __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS,
1810 IS_UDPLITE(sk));
1811 __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
1812 IS_UDPLITE(sk));
1813 atomic_inc(&sk->sk_drops);
1814 kfree_skb(skb);
1815 continue;
1816 }
1817
1818 used = recv_actor(desc, skb, 0, skb->len);
1819 if (used <= 0) {
1820 if (!copied)
1821 copied = used;
1822 kfree_skb(skb);
1823 break;
1824 } else if (used <= skb->len) {
1825 copied += used;
1826 }
1827
1828 kfree_skb(skb);
1829 if (!desc->count)
1830 break;
1831 }
1832
1833 return copied;
1834}
1835EXPORT_SYMBOL(udp_read_sock);
1836
1837
1838
1839
1840
1841
1842int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
1843 int flags, int *addr_len)
1844{
1845 struct inet_sock *inet = inet_sk(sk);
1846 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
1847 struct sk_buff *skb;
1848 unsigned int ulen, copied;
1849 int off, err, peeking = flags & MSG_PEEK;
1850 int is_udplite = IS_UDPLITE(sk);
1851 bool checksum_valid = false;
1852
1853 if (flags & MSG_ERRQUEUE)
1854 return ip_recv_error(sk, msg, len, addr_len);
1855
1856try_again:
1857 off = sk_peek_offset(sk, flags);
1858 skb = __skb_recv_udp(sk, flags, noblock, &off, &err);
1859 if (!skb)
1860 return err;
1861
1862 ulen = udp_skb_len(skb);
1863 copied = len;
1864 if (copied > ulen - off)
1865 copied = ulen - off;
1866 else if (copied < ulen)
1867 msg->msg_flags |= MSG_TRUNC;
1868
1869
1870
1871
1872
1873
1874
1875 if (copied < ulen || peeking ||
1876 (is_udplite && UDP_SKB_CB(skb)->partial_cov)) {
1877 checksum_valid = udp_skb_csum_unnecessary(skb) ||
1878 !__udp_lib_checksum_complete(skb);
1879 if (!checksum_valid)
1880 goto csum_copy_err;
1881 }
1882
1883 if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
1884 if (udp_skb_is_linear(skb))
1885 err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
1886 else
1887 err = skb_copy_datagram_msg(skb, off, msg, copied);
1888 } else {
1889 err = skb_copy_and_csum_datagram_msg(skb, off, msg);
1890
1891 if (err == -EINVAL)
1892 goto csum_copy_err;
1893 }
1894
1895 if (unlikely(err)) {
1896 if (!peeking) {
1897 atomic_inc(&sk->sk_drops);
1898 UDP_INC_STATS(sock_net(sk),
1899 UDP_MIB_INERRORS, is_udplite);
1900 }
1901 kfree_skb(skb);
1902 return err;
1903 }
1904
1905 if (!peeking)
1906 UDP_INC_STATS(sock_net(sk),
1907 UDP_MIB_INDATAGRAMS, is_udplite);
1908
1909 sock_recv_ts_and_drops(msg, sk, skb);
1910
1911
1912 if (sin) {
1913 sin->sin_family = AF_INET;
1914 sin->sin_port = udp_hdr(skb)->source;
1915 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
1916 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1917 *addr_len = sizeof(*sin);
1918
1919 BPF_CGROUP_RUN_PROG_UDP4_RECVMSG_LOCK(sk,
1920 (struct sockaddr *)sin);
1921 }
1922
1923 if (udp_sk(sk)->gro_enabled)
1924 udp_cmsg_recv(msg, sk, skb);
1925
1926 if (inet->cmsg_flags)
1927 ip_cmsg_recv_offset(msg, sk, skb, sizeof(struct udphdr), off);
1928
1929 err = copied;
1930 if (flags & MSG_TRUNC)
1931 err = ulen;
1932
1933 skb_consume_udp(sk, skb, peeking ? -err : err);
1934 return err;
1935
1936csum_copy_err:
1937 if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
1938 udp_skb_destructor)) {
1939 UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
1940 UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
1941 }
1942 kfree_skb(skb);
1943
1944
1945 cond_resched();
1946 msg->msg_flags &= ~MSG_TRUNC;
1947 goto try_again;
1948}
1949
1950int udp_pre_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
1951{
1952
1953
1954
1955
1956 if (addr_len < sizeof(struct sockaddr_in))
1957 return -EINVAL;
1958
1959 return BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr);
1960}
1961EXPORT_SYMBOL(udp_pre_connect);
1962
1963int __udp_disconnect(struct sock *sk, int flags)
1964{
1965 struct inet_sock *inet = inet_sk(sk);
1966
1967
1968
1969
1970 sk->sk_state = TCP_CLOSE;
1971 inet->inet_daddr = 0;
1972 inet->inet_dport = 0;
1973 sock_rps_reset_rxhash(sk);
1974 sk->sk_bound_dev_if = 0;
1975 if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) {
1976 inet_reset_saddr(sk);
1977 if (sk->sk_prot->rehash &&
1978 (sk->sk_userlocks & SOCK_BINDPORT_LOCK))
1979 sk->sk_prot->rehash(sk);
1980 }
1981
1982 if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
1983 sk->sk_prot->unhash(sk);
1984 inet->inet_sport = 0;
1985 }
1986 sk_dst_reset(sk);
1987 return 0;
1988}
1989EXPORT_SYMBOL(__udp_disconnect);
1990
1991int udp_disconnect(struct sock *sk, int flags)
1992{
1993 lock_sock(sk);
1994 __udp_disconnect(sk, flags);
1995 release_sock(sk);
1996 return 0;
1997}
1998EXPORT_SYMBOL(udp_disconnect);
1999
2000void udp_lib_unhash(struct sock *sk)
2001{
2002 if (sk_hashed(sk)) {
2003 struct udp_table *udptable = sk->sk_prot->h.udp_table;
2004 struct udp_hslot *hslot, *hslot2;
2005
2006 hslot = udp_hashslot(udptable, sock_net(sk),
2007 udp_sk(sk)->udp_port_hash);
2008 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
2009
2010 spin_lock_bh(&hslot->lock);
2011 if (rcu_access_pointer(sk->sk_reuseport_cb))
2012 reuseport_detach_sock(sk);
2013 if (sk_del_node_init_rcu(sk)) {
2014 hslot->count--;
2015 inet_sk(sk)->inet_num = 0;
2016 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
2017
2018 spin_lock(&hslot2->lock);
2019 hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
2020 hslot2->count--;
2021 spin_unlock(&hslot2->lock);
2022 }
2023 spin_unlock_bh(&hslot->lock);
2024 }
2025}
2026EXPORT_SYMBOL(udp_lib_unhash);
2027
2028
2029
2030
2031void udp_lib_rehash(struct sock *sk, u16 newhash)
2032{
2033 if (sk_hashed(sk)) {
2034 struct udp_table *udptable = sk->sk_prot->h.udp_table;
2035 struct udp_hslot *hslot, *hslot2, *nhslot2;
2036
2037 hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
2038 nhslot2 = udp_hashslot2(udptable, newhash);
2039 udp_sk(sk)->udp_portaddr_hash = newhash;
2040
2041 if (hslot2 != nhslot2 ||
2042 rcu_access_pointer(sk->sk_reuseport_cb)) {
2043 hslot = udp_hashslot(udptable, sock_net(sk),
2044 udp_sk(sk)->udp_port_hash);
2045
2046 spin_lock_bh(&hslot->lock);
2047 if (rcu_access_pointer(sk->sk_reuseport_cb))
2048 reuseport_detach_sock(sk);
2049
2050 if (hslot2 != nhslot2) {
2051 spin_lock(&hslot2->lock);
2052 hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
2053 hslot2->count--;
2054 spin_unlock(&hslot2->lock);
2055
2056 spin_lock(&nhslot2->lock);
2057 hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
2058 &nhslot2->head);
2059 nhslot2->count++;
2060 spin_unlock(&nhslot2->lock);
2061 }
2062
2063 spin_unlock_bh(&hslot->lock);
2064 }
2065 }
2066}
2067EXPORT_SYMBOL(udp_lib_rehash);
2068
2069void udp_v4_rehash(struct sock *sk)
2070{
2071 u16 new_hash = ipv4_portaddr_hash(sock_net(sk),
2072 inet_sk(sk)->inet_rcv_saddr,
2073 inet_sk(sk)->inet_num);
2074 udp_lib_rehash(sk, new_hash);
2075}
2076
2077static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
2078{
2079 int rc;
2080
2081 if (inet_sk(sk)->inet_daddr) {
2082 sock_rps_save_rxhash(sk, skb);
2083 sk_mark_napi_id(sk, skb);
2084 sk_incoming_cpu_update(sk);
2085 } else {
2086 sk_mark_napi_id_once(sk, skb);
2087 }
2088
2089 rc = __udp_enqueue_schedule_skb(sk, skb);
2090 if (rc < 0) {
2091 int is_udplite = IS_UDPLITE(sk);
2092
2093
2094 if (rc == -ENOMEM)
2095 UDP_INC_STATS(sock_net(sk), UDP_MIB_RCVBUFERRORS,
2096 is_udplite);
2097 UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
2098 kfree_skb(skb);
2099 trace_udp_fail_queue_rcv_skb(rc, sk);
2100 return -1;
2101 }
2102
2103 return 0;
2104}
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114static int udp_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
2115{
2116 struct udp_sock *up = udp_sk(sk);
2117 int is_udplite = IS_UDPLITE(sk);
2118
2119
2120
2121
2122 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
2123 goto drop;
2124 nf_reset(skb);
2125
2126 if (static_branch_unlikely(&udp_encap_needed_key) && up->encap_type) {
2127 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141 encap_rcv = READ_ONCE(up->encap_rcv);
2142 if (encap_rcv) {
2143 int ret;
2144
2145
2146 if (udp_lib_checksum_complete(skb))
2147 goto csum_error;
2148
2149 ret = encap_rcv(sk, skb);
2150 if (ret <= 0) {
2151 __UDP_INC_STATS(sock_net(sk),
2152 UDP_MIB_INDATAGRAMS,
2153 is_udplite);
2154 return -ret;
2155 }
2156 }
2157
2158
2159 }
2160
2161
2162
2163
2164 if ((up->pcflag & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177 if (up->pcrlen == 0) {
2178 net_dbg_ratelimited("UDPLite: partial coverage %d while full coverage %d requested\n",
2179 UDP_SKB_CB(skb)->cscov, skb->len);
2180 goto drop;
2181 }
2182
2183
2184
2185
2186
2187
2188 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
2189 net_dbg_ratelimited("UDPLite: coverage %d too small, need min %d\n",
2190 UDP_SKB_CB(skb)->cscov, up->pcrlen);
2191 goto drop;
2192 }
2193 }
2194
2195 prefetch(&sk->sk_rmem_alloc);
2196 if (rcu_access_pointer(sk->sk_filter) &&
2197 udp_lib_checksum_complete(skb))
2198 goto csum_error;
2199
2200 if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
2201 goto drop;
2202
2203 udp_csum_pull_header(skb);
2204
2205 ipv4_pktinfo_prepare(sk, skb);
2206 return __udp_queue_rcv_skb(sk, skb);
2207
2208csum_error:
2209 __UDP_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
2210drop:
2211 __UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
2212 atomic_inc(&sk->sk_drops);
2213 kfree_skb(skb);
2214 return -1;
2215}
2216
2217static int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
2218{
2219 struct sk_buff *next, *segs;
2220 int ret;
2221
2222 if (likely(!udp_unexpected_gso(sk, skb)))
2223 return udp_queue_rcv_one_skb(sk, skb);
2224
2225 BUILD_BUG_ON(sizeof(struct udp_skb_cb) > SKB_GSO_CB_OFFSET);
2226 __skb_push(skb, -skb_mac_offset(skb));
2227 segs = udp_rcv_segment(sk, skb, true);
2228 for (skb = segs; skb; skb = next) {
2229 next = skb->next;
2230 __skb_pull(skb, skb_transport_offset(skb));
2231
2232 udp_post_segment_fix_csum(skb);
2233 ret = udp_queue_rcv_one_skb(sk, skb);
2234 if (ret > 0)
2235 ip_protocol_deliver_rcu(dev_net(skb->dev), skb, ret);
2236 }
2237 return 0;
2238}
2239
2240
2241
2242
2243bool udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
2244{
2245 struct dst_entry *old;
2246
2247 if (dst_hold_safe(dst)) {
2248 old = xchg(&sk->sk_rx_dst, dst);
2249 dst_release(old);
2250 return old != dst;
2251 }
2252 return false;
2253}
2254EXPORT_SYMBOL(udp_sk_rx_dst_set);
2255
2256
2257
2258
2259
2260
2261static int __udp4_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
2262 struct udphdr *uh,
2263 __be32 saddr, __be32 daddr,
2264 struct udp_table *udptable,
2265 int proto)
2266{
2267 struct sock *sk, *first = NULL;
2268 unsigned short hnum = ntohs(uh->dest);
2269 struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
2270 unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
2271 unsigned int offset = offsetof(typeof(*sk), sk_node);
2272 int dif = skb->dev->ifindex;
2273 int sdif = inet_sdif(skb);
2274 struct hlist_node *node;
2275 struct sk_buff *nskb;
2276
2277 if (use_hash2) {
2278 hash2_any = ipv4_portaddr_hash(net, htonl(INADDR_ANY), hnum) &
2279 udptable->mask;
2280 hash2 = ipv4_portaddr_hash(net, daddr, hnum) & udptable->mask;
2281start_lookup:
2282 hslot = &udptable->hash2[hash2];
2283 offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
2284 }
2285
2286 sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
2287 if (!__udp_is_mcast_sock(net, sk, uh->dest, daddr,
2288 uh->source, saddr, dif, sdif, hnum))
2289 continue;
2290
2291 if (!first) {
2292 first = sk;
2293 continue;
2294 }
2295 nskb = skb_clone(skb, GFP_ATOMIC);
2296
2297 if (unlikely(!nskb)) {
2298 atomic_inc(&sk->sk_drops);
2299 __UDP_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
2300 IS_UDPLITE(sk));
2301 __UDP_INC_STATS(net, UDP_MIB_INERRORS,
2302 IS_UDPLITE(sk));
2303 continue;
2304 }
2305 if (udp_queue_rcv_skb(sk, nskb) > 0)
2306 consume_skb(nskb);
2307 }
2308
2309
2310 if (use_hash2 && hash2 != hash2_any) {
2311 hash2 = hash2_any;
2312 goto start_lookup;
2313 }
2314
2315 if (first) {
2316 if (udp_queue_rcv_skb(first, skb) > 0)
2317 consume_skb(skb);
2318 } else {
2319 kfree_skb(skb);
2320 __UDP_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
2321 proto == IPPROTO_UDPLITE);
2322 }
2323 return 0;
2324}
2325
2326
2327
2328
2329
2330
2331static inline int udp4_csum_init(struct sk_buff *skb, struct udphdr *uh,
2332 int proto)
2333{
2334 int err;
2335
2336 UDP_SKB_CB(skb)->partial_cov = 0;
2337 UDP_SKB_CB(skb)->cscov = skb->len;
2338
2339 if (proto == IPPROTO_UDPLITE) {
2340 err = udplite_checksum_init(skb, uh);
2341 if (err)
2342 return err;
2343
2344 if (UDP_SKB_CB(skb)->partial_cov) {
2345 skb->csum = inet_compute_pseudo(skb, proto);
2346 return 0;
2347 }
2348 }
2349
2350
2351
2352
2353 err = (__force int)skb_checksum_init_zero_check(skb, proto, uh->check,
2354 inet_compute_pseudo);
2355 if (err)
2356 return err;
2357
2358 if (skb->ip_summed == CHECKSUM_COMPLETE && !skb->csum_valid) {
2359
2360 if (skb->csum_complete_sw)
2361 return 1;
2362
2363
2364
2365
2366
2367 skb_checksum_complete_unset(skb);
2368 }
2369
2370 return 0;
2371}
2372
2373
2374
2375
2376static int udp_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,
2377 struct udphdr *uh)
2378{
2379 int ret;
2380
2381 if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
2382 skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
2383 inet_compute_pseudo);
2384
2385 ret = udp_queue_rcv_skb(sk, skb);
2386
2387
2388
2389
2390 if (ret > 0)
2391 return -ret;
2392 return 0;
2393}
2394
2395
2396
2397
2398
2399int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
2400 int proto)
2401{
2402 struct sock *sk;
2403 struct udphdr *uh;
2404 unsigned short ulen;
2405 struct rtable *rt = skb_rtable(skb);
2406 __be32 saddr, daddr;
2407 struct net *net = dev_net(skb->dev);
2408 bool refcounted;
2409
2410
2411
2412
2413 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
2414 goto drop;
2415
2416 uh = udp_hdr(skb);
2417 ulen = ntohs(uh->len);
2418 saddr = ip_hdr(skb)->saddr;
2419 daddr = ip_hdr(skb)->daddr;
2420
2421 if (ulen > skb->len)
2422 goto short_packet;
2423
2424 if (proto == IPPROTO_UDP) {
2425
2426 if (ulen < sizeof(*uh) || pskb_trim_rcsum(skb, ulen))
2427 goto short_packet;
2428 uh = udp_hdr(skb);
2429 }
2430
2431 if (udp4_csum_init(skb, uh, proto))
2432 goto csum_error;
2433
2434 sk = skb_steal_sock(skb, &refcounted);
2435 if (sk) {
2436 struct dst_entry *dst = skb_dst(skb);
2437 int ret;
2438
2439 if (unlikely(sk->sk_rx_dst != dst))
2440 udp_sk_rx_dst_set(sk, dst);
2441
2442 ret = udp_unicast_rcv_skb(sk, skb, uh);
2443 if (refcounted)
2444 sock_put(sk);
2445 return ret;
2446 }
2447
2448 if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
2449 return __udp4_lib_mcast_deliver(net, skb, uh,
2450 saddr, daddr, udptable, proto);
2451
2452 sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
2453 if (sk)
2454 return udp_unicast_rcv_skb(sk, skb, uh);
2455
2456 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
2457 goto drop;
2458 nf_reset(skb);
2459
2460
2461 if (udp_lib_checksum_complete(skb))
2462 goto csum_error;
2463
2464 __UDP_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
2465 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_PORT_UNREACH, 0);
2466
2467
2468
2469
2470
2471 kfree_skb(skb);
2472 return 0;
2473
2474short_packet:
2475 net_dbg_ratelimited("UDP%s: short packet: From %pI4:%u %d/%d to %pI4:%u\n",
2476 proto == IPPROTO_UDPLITE ? "Lite" : "",
2477 &saddr, ntohs(uh->source),
2478 ulen, skb->len,
2479 &daddr, ntohs(uh->dest));
2480 goto drop;
2481
2482csum_error:
2483
2484
2485
2486
2487 net_dbg_ratelimited("UDP%s: bad checksum. From %pI4:%u to %pI4:%u ulen %d\n",
2488 proto == IPPROTO_UDPLITE ? "Lite" : "",
2489 &saddr, ntohs(uh->source), &daddr, ntohs(uh->dest),
2490 ulen);
2491 __UDP_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
2492drop:
2493 __UDP_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
2494 kfree_skb(skb);
2495 return 0;
2496}
2497
2498
2499
2500
2501static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net,
2502 __be16 loc_port, __be32 loc_addr,
2503 __be16 rmt_port, __be32 rmt_addr,
2504 int dif, int sdif)
2505{
2506 struct sock *sk, *result;
2507 unsigned short hnum = ntohs(loc_port);
2508 unsigned int slot = udp_hashfn(net, hnum, udp_table.mask);
2509 struct udp_hslot *hslot = &udp_table.hash[slot];
2510
2511
2512 if (hslot->count > 10)
2513 return NULL;
2514
2515 result = NULL;
2516 sk_for_each_rcu(sk, &hslot->head) {
2517 if (__udp_is_mcast_sock(net, sk, loc_port, loc_addr,
2518 rmt_port, rmt_addr, dif, sdif, hnum)) {
2519 if (result)
2520 return NULL;
2521 result = sk;
2522 }
2523 }
2524
2525 return result;
2526}
2527
2528
2529
2530
2531
2532static struct sock *__udp4_lib_demux_lookup(struct net *net,
2533 __be16 loc_port, __be32 loc_addr,
2534 __be16 rmt_port, __be32 rmt_addr,
2535 int dif, int sdif)
2536{
2537 unsigned short hnum = ntohs(loc_port);
2538 unsigned int hash2 = ipv4_portaddr_hash(net, loc_addr, hnum);
2539 unsigned int slot2 = hash2 & udp_table.mask;
2540 struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
2541 INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr);
2542 const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
2543 struct sock *sk;
2544
2545 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
2546 if (INET_MATCH(sk, net, acookie, rmt_addr,
2547 loc_addr, ports, dif, sdif))
2548 return sk;
2549
2550 break;
2551 }
2552 return NULL;
2553}
2554
2555int udp_v4_early_demux(struct sk_buff *skb)
2556{
2557 struct net *net = dev_net(skb->dev);
2558 struct in_device *in_dev = NULL;
2559 const struct iphdr *iph;
2560 const struct udphdr *uh;
2561 struct sock *sk = NULL;
2562 struct dst_entry *dst;
2563 int dif = skb->dev->ifindex;
2564 int sdif = inet_sdif(skb);
2565 int ours;
2566
2567
2568 if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr)))
2569 return 0;
2570
2571 iph = ip_hdr(skb);
2572 uh = udp_hdr(skb);
2573
2574 if (skb->pkt_type == PACKET_MULTICAST) {
2575 in_dev = __in_dev_get_rcu(skb->dev);
2576
2577 if (!in_dev)
2578 return 0;
2579
2580 ours = ip_check_mc_rcu(in_dev, iph->daddr, iph->saddr,
2581 iph->protocol);
2582 if (!ours)
2583 return 0;
2584
2585 sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr,
2586 uh->source, iph->saddr,
2587 dif, sdif);
2588 } else if (skb->pkt_type == PACKET_HOST) {
2589 sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr,
2590 uh->source, iph->saddr, dif, sdif);
2591 }
2592
2593 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
2594 return 0;
2595
2596 skb->sk = sk;
2597 skb->destructor = sock_efree;
2598 dst = READ_ONCE(sk->sk_rx_dst);
2599
2600 if (dst)
2601 dst = dst_check(dst, 0);
2602 if (dst) {
2603 u32 itag = 0;
2604
2605
2606
2607
2608
2609 skb_dst_set_noref(skb, dst);
2610
2611
2612
2613
2614 if (!inet_sk(sk)->inet_daddr && in_dev)
2615 return ip_mc_validate_source(skb, iph->daddr,
2616 iph->saddr,
2617 iph->tos & IPTOS_RT_MASK,
2618 skb->dev, in_dev, &itag);
2619 }
2620 return 0;
2621}
2622
2623int udp_rcv(struct sk_buff *skb)
2624{
2625 return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
2626}
2627
2628void udp_destroy_sock(struct sock *sk)
2629{
2630 struct udp_sock *up = udp_sk(sk);
2631 bool slow = lock_sock_fast(sk);
2632
2633
2634 sock_set_flag(sk, SOCK_DEAD);
2635 udp_flush_pending_frames(sk);
2636 unlock_sock_fast(sk, slow);
2637 if (static_branch_unlikely(&udp_encap_needed_key)) {
2638 if (up->encap_type) {
2639 void (*encap_destroy)(struct sock *sk);
2640 encap_destroy = READ_ONCE(up->encap_destroy);
2641 if (encap_destroy)
2642 encap_destroy(sk);
2643 }
2644 if (up->encap_enabled)
2645 static_branch_dec(&udp_encap_needed_key);
2646 }
2647}
2648
2649
2650
2651
2652int udp_lib_setsockopt(struct sock *sk, int level, int optname,
2653 char __user *optval, unsigned int optlen,
2654 int (*push_pending_frames)(struct sock *))
2655{
2656 struct udp_sock *up = udp_sk(sk);
2657 int val, valbool;
2658 int err = 0;
2659 int is_udplite = IS_UDPLITE(sk);
2660
2661 if (optlen < sizeof(int))
2662 return -EINVAL;
2663
2664 if (get_user(val, (int __user *)optval))
2665 return -EFAULT;
2666
2667 valbool = val ? 1 : 0;
2668
2669 switch (optname) {
2670 case UDP_CORK:
2671 if (val != 0) {
2672 WRITE_ONCE(up->corkflag, 1);
2673 } else {
2674 WRITE_ONCE(up->corkflag, 0);
2675 lock_sock(sk);
2676 push_pending_frames(sk);
2677 release_sock(sk);
2678 }
2679 break;
2680
2681 case UDP_ENCAP:
2682 switch (val) {
2683 case 0:
2684#ifdef CONFIG_XFRM
2685 case UDP_ENCAP_ESPINUDP:
2686 case UDP_ENCAP_ESPINUDP_NON_IKE:
2687#if IS_ENABLED(CONFIG_IPV6)
2688 if (sk->sk_family == AF_INET6)
2689 up->encap_rcv = ipv6_stub->xfrm6_udp_encap_rcv;
2690 else
2691#endif
2692 up->encap_rcv = xfrm4_udp_encap_rcv;
2693#endif
2694
2695 case UDP_ENCAP_L2TPINUDP:
2696 up->encap_type = val;
2697 lock_sock(sk);
2698 udp_tunnel_encap_enable(sk->sk_socket);
2699 release_sock(sk);
2700 break;
2701 default:
2702 err = -ENOPROTOOPT;
2703 break;
2704 }
2705 break;
2706
2707 case UDP_NO_CHECK6_TX:
2708 up->no_check6_tx = valbool;
2709 break;
2710
2711 case UDP_NO_CHECK6_RX:
2712 up->no_check6_rx = valbool;
2713 break;
2714
2715 case UDP_SEGMENT:
2716 if (val < 0 || val > USHRT_MAX)
2717 return -EINVAL;
2718 WRITE_ONCE(up->gso_size, val);
2719 break;
2720
2721 case UDP_GRO:
2722 lock_sock(sk);
2723
2724
2725 if (valbool)
2726 udp_tunnel_encap_enable(sk->sk_socket);
2727 up->gro_enabled = valbool;
2728 up->accept_udp_l4 = valbool;
2729 release_sock(sk);
2730 break;
2731
2732
2733
2734
2735
2736
2737 case UDPLITE_SEND_CSCOV:
2738 if (!is_udplite)
2739 return -ENOPROTOOPT;
2740 if (val != 0 && val < 8)
2741 val = 8;
2742 else if (val > USHRT_MAX)
2743 val = USHRT_MAX;
2744 up->pcslen = val;
2745 up->pcflag |= UDPLITE_SEND_CC;
2746 break;
2747
2748
2749
2750
2751 case UDPLITE_RECV_CSCOV:
2752 if (!is_udplite)
2753 return -ENOPROTOOPT;
2754 if (val != 0 && val < 8)
2755 val = 8;
2756 else if (val > USHRT_MAX)
2757 val = USHRT_MAX;
2758 up->pcrlen = val;
2759 up->pcflag |= UDPLITE_RECV_CC;
2760 break;
2761
2762 default:
2763 err = -ENOPROTOOPT;
2764 break;
2765 }
2766
2767 return err;
2768}
2769EXPORT_SYMBOL(udp_lib_setsockopt);
2770
2771int udp_setsockopt(struct sock *sk, int level, int optname,
2772 char __user *optval, unsigned int optlen)
2773{
2774 if (level == SOL_UDP || level == SOL_UDPLITE)
2775 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
2776 udp_push_pending_frames);
2777 return ip_setsockopt(sk, level, optname, optval, optlen);
2778}
2779
2780#ifdef CONFIG_COMPAT
2781int compat_udp_setsockopt(struct sock *sk, int level, int optname,
2782 char __user *optval, unsigned int optlen)
2783{
2784 if (level == SOL_UDP || level == SOL_UDPLITE)
2785 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
2786 udp_push_pending_frames);
2787 return compat_ip_setsockopt(sk, level, optname, optval, optlen);
2788}
2789#endif
2790
2791int udp_lib_getsockopt(struct sock *sk, int level, int optname,
2792 char __user *optval, int __user *optlen)
2793{
2794 struct udp_sock *up = udp_sk(sk);
2795 int val, len;
2796
2797 if (get_user(len, optlen))
2798 return -EFAULT;
2799
2800 len = min_t(unsigned int, len, sizeof(int));
2801
2802 if (len < 0)
2803 return -EINVAL;
2804
2805 switch (optname) {
2806 case UDP_CORK:
2807 val = READ_ONCE(up->corkflag);
2808 break;
2809
2810 case UDP_ENCAP:
2811 val = up->encap_type;
2812 break;
2813
2814 case UDP_NO_CHECK6_TX:
2815 val = up->no_check6_tx;
2816 break;
2817
2818 case UDP_NO_CHECK6_RX:
2819 val = up->no_check6_rx;
2820 break;
2821
2822 case UDP_SEGMENT:
2823 val = READ_ONCE(up->gso_size);
2824 break;
2825
2826 case UDP_GRO:
2827 val = up->gro_enabled;
2828 break;
2829
2830
2831
2832 case UDPLITE_SEND_CSCOV:
2833 val = up->pcslen;
2834 break;
2835
2836 case UDPLITE_RECV_CSCOV:
2837 val = up->pcrlen;
2838 break;
2839
2840 default:
2841 return -ENOPROTOOPT;
2842 }
2843
2844 if (put_user(len, optlen))
2845 return -EFAULT;
2846 if (copy_to_user(optval, &val, len))
2847 return -EFAULT;
2848 return 0;
2849}
2850EXPORT_SYMBOL(udp_lib_getsockopt);
2851
2852int udp_getsockopt(struct sock *sk, int level, int optname,
2853 char __user *optval, int __user *optlen)
2854{
2855 if (level == SOL_UDP || level == SOL_UDPLITE)
2856 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
2857 return ip_getsockopt(sk, level, optname, optval, optlen);
2858}
2859
2860#ifdef CONFIG_COMPAT
2861int compat_udp_getsockopt(struct sock *sk, int level, int optname,
2862 char __user *optval, int __user *optlen)
2863{
2864 if (level == SOL_UDP || level == SOL_UDPLITE)
2865 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
2866 return compat_ip_getsockopt(sk, level, optname, optval, optlen);
2867}
2868#endif
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882__poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait)
2883{
2884 __poll_t mask = datagram_poll(file, sock, wait);
2885 struct sock *sk = sock->sk;
2886
2887 if (!skb_queue_empty_lockless(&udp_sk(sk)->reader_queue))
2888 mask |= EPOLLIN | EPOLLRDNORM;
2889
2890
2891 if ((mask & EPOLLRDNORM) && !(file->f_flags & O_NONBLOCK) &&
2892 !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1)
2893 mask &= ~(EPOLLIN | EPOLLRDNORM);
2894
2895 return mask;
2896
2897}
2898EXPORT_SYMBOL(udp_poll);
2899
2900int udp_abort(struct sock *sk, int err)
2901{
2902 lock_sock(sk);
2903
2904
2905
2906
2907 if (sock_flag(sk, SOCK_DEAD))
2908 goto out;
2909
2910 sk->sk_err = err;
2911 sk_error_report(sk);
2912 __udp_disconnect(sk, 0);
2913
2914out:
2915 release_sock(sk);
2916
2917 return 0;
2918}
2919EXPORT_SYMBOL_GPL(udp_abort);
2920
2921struct proto udp_prot = {
2922 .name = "UDP",
2923 .owner = THIS_MODULE,
2924 .close = udp_lib_close,
2925 .pre_connect = udp_pre_connect,
2926 .connect = ip4_datagram_connect,
2927 .disconnect = udp_disconnect,
2928 .ioctl = udp_ioctl,
2929 .init = udp_init_sock,
2930 .destroy = udp_destroy_sock,
2931 .setsockopt = udp_setsockopt,
2932 .getsockopt = udp_getsockopt,
2933 .sendmsg = udp_sendmsg,
2934 .recvmsg = udp_recvmsg,
2935 .sendpage = udp_sendpage,
2936 .release_cb = ip4_datagram_release_cb,
2937 .hash = udp_lib_hash,
2938 .unhash = udp_lib_unhash,
2939 .rehash = udp_v4_rehash,
2940 .get_port = udp_v4_get_port,
2941#ifdef CONFIG_BPF_SYSCALL
2942 .psock_update_sk_prot = udp_bpf_update_proto,
2943#endif
2944 .memory_allocated = &udp_memory_allocated,
2945 .sysctl_mem = sysctl_udp_mem,
2946 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
2947 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
2948 .obj_size = sizeof(struct udp_sock),
2949 .h.udp_table = &udp_table,
2950#ifdef CONFIG_COMPAT
2951 .compat_setsockopt = compat_udp_setsockopt,
2952 .compat_getsockopt = compat_udp_getsockopt,
2953#endif
2954 .diag_destroy = udp_abort,
2955};
2956EXPORT_SYMBOL(udp_prot);
2957
2958
2959#ifdef CONFIG_PROC_FS
2960
2961static struct sock *udp_get_first(struct seq_file *seq, int start)
2962{
2963 struct sock *sk;
2964 struct udp_seq_afinfo *afinfo;
2965 struct udp_iter_state *state = seq->private;
2966 struct net *net = seq_file_net(seq);
2967
2968 if (state->bpf_seq_afinfo)
2969 afinfo = state->bpf_seq_afinfo;
2970 else
2971 afinfo = PDE_DATA(file_inode(seq->file));
2972
2973 for (state->bucket = start; state->bucket <= afinfo->udp_table->mask;
2974 ++state->bucket) {
2975 struct udp_hslot *hslot = &afinfo->udp_table->hash[state->bucket];
2976
2977 if (hlist_empty(&hslot->head))
2978 continue;
2979
2980 spin_lock_bh(&hslot->lock);
2981 sk_for_each(sk, &hslot->head) {
2982 if (!net_eq(sock_net(sk), net))
2983 continue;
2984 if (afinfo->family == AF_UNSPEC ||
2985 sk->sk_family == afinfo->family)
2986 goto found;
2987 }
2988 spin_unlock_bh(&hslot->lock);
2989 }
2990 sk = NULL;
2991found:
2992 return sk;
2993}
2994
2995static struct sock *udp_get_next(struct seq_file *seq, struct sock *sk)
2996{
2997 struct udp_seq_afinfo *afinfo;
2998 struct udp_iter_state *state = seq->private;
2999 struct net *net = seq_file_net(seq);
3000
3001 if (state->bpf_seq_afinfo)
3002 afinfo = state->bpf_seq_afinfo;
3003 else
3004 afinfo = PDE_DATA(file_inode(seq->file));
3005
3006 do {
3007 sk = sk_next(sk);
3008 } while (sk && (!net_eq(sock_net(sk), net) ||
3009 (afinfo->family != AF_UNSPEC &&
3010 sk->sk_family != afinfo->family)));
3011
3012 if (!sk) {
3013 if (state->bucket <= afinfo->udp_table->mask)
3014 spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock);
3015 return udp_get_first(seq, state->bucket + 1);
3016 }
3017 return sk;
3018}
3019
3020static struct sock *udp_get_idx(struct seq_file *seq, loff_t pos)
3021{
3022 struct sock *sk = udp_get_first(seq, 0);
3023
3024 if (sk)
3025 while (pos && (sk = udp_get_next(seq, sk)) != NULL)
3026 --pos;
3027 return pos ? NULL : sk;
3028}
3029
3030void *udp_seq_start(struct seq_file *seq, loff_t *pos)
3031{
3032 struct udp_iter_state *state = seq->private;
3033 state->bucket = MAX_UDP_PORTS;
3034
3035 return *pos ? udp_get_idx(seq, *pos-1) : SEQ_START_TOKEN;
3036}
3037EXPORT_SYMBOL(udp_seq_start);
3038
3039void *udp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3040{
3041 struct sock *sk;
3042
3043 if (v == SEQ_START_TOKEN)
3044 sk = udp_get_idx(seq, 0);
3045 else
3046 sk = udp_get_next(seq, v);
3047
3048 ++*pos;
3049 return sk;
3050}
3051EXPORT_SYMBOL(udp_seq_next);
3052
3053void udp_seq_stop(struct seq_file *seq, void *v)
3054{
3055 struct udp_seq_afinfo *afinfo;
3056 struct udp_iter_state *state = seq->private;
3057
3058 if (state->bpf_seq_afinfo)
3059 afinfo = state->bpf_seq_afinfo;
3060 else
3061 afinfo = PDE_DATA(file_inode(seq->file));
3062
3063 if (state->bucket <= afinfo->udp_table->mask)
3064 spin_unlock_bh(&afinfo->udp_table->hash[state->bucket].lock);
3065}
3066EXPORT_SYMBOL(udp_seq_stop);
3067
3068
3069static void udp4_format_sock(struct sock *sp, struct seq_file *f,
3070 int bucket)
3071{
3072 struct inet_sock *inet = inet_sk(sp);
3073 __be32 dest = inet->inet_daddr;
3074 __be32 src = inet->inet_rcv_saddr;
3075 __u16 destp = ntohs(inet->inet_dport);
3076 __u16 srcp = ntohs(inet->inet_sport);
3077
3078 seq_printf(f, "%5d: %08X:%04X %08X:%04X"
3079 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %u",
3080 bucket, src, srcp, dest, destp, sp->sk_state,
3081 sk_wmem_alloc_get(sp),
3082 udp_rqueue_get(sp),
3083 0, 0L, 0,
3084 from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
3085 0, sock_i_ino(sp),
3086 refcount_read(&sp->sk_refcnt), sp,
3087 atomic_read(&sp->sk_drops));
3088}
3089
3090int udp4_seq_show(struct seq_file *seq, void *v)
3091{
3092 seq_setwidth(seq, 127);
3093 if (v == SEQ_START_TOKEN)
3094 seq_puts(seq, " sl local_address rem_address st tx_queue "
3095 "rx_queue tr tm->when retrnsmt uid timeout "
3096 "inode ref pointer drops");
3097 else {
3098 struct udp_iter_state *state = seq->private;
3099
3100 udp4_format_sock(v, seq, state->bucket);
3101 }
3102 seq_pad(seq, '\n');
3103 return 0;
3104}
3105
3106#ifdef CONFIG_BPF_SYSCALL
3107struct bpf_iter__udp {
3108 __bpf_md_ptr(struct bpf_iter_meta *, meta);
3109 __bpf_md_ptr(struct udp_sock *, udp_sk);
3110 uid_t uid __aligned(8);
3111 int bucket __aligned(8);
3112};
3113
3114static int udp_prog_seq_show(struct bpf_prog *prog, struct bpf_iter_meta *meta,
3115 struct udp_sock *udp_sk, uid_t uid, int bucket)
3116{
3117 struct bpf_iter__udp ctx;
3118
3119 meta->seq_num--;
3120 ctx.meta = meta;
3121 ctx.udp_sk = udp_sk;
3122 ctx.uid = uid;
3123 ctx.bucket = bucket;
3124 return bpf_iter_run_prog(prog, &ctx);
3125}
3126
3127static int bpf_iter_udp_seq_show(struct seq_file *seq, void *v)
3128{
3129 struct udp_iter_state *state = seq->private;
3130 struct bpf_iter_meta meta;
3131 struct bpf_prog *prog;
3132 struct sock *sk = v;
3133 uid_t uid;
3134
3135 if (v == SEQ_START_TOKEN)
3136 return 0;
3137
3138 uid = from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk));
3139 meta.seq = seq;
3140 prog = bpf_iter_get_info(&meta, false);
3141 return udp_prog_seq_show(prog, &meta, v, uid, state->bucket);
3142}
3143
3144static void bpf_iter_udp_seq_stop(struct seq_file *seq, void *v)
3145{
3146 struct bpf_iter_meta meta;
3147 struct bpf_prog *prog;
3148
3149 if (!v) {
3150 meta.seq = seq;
3151 prog = bpf_iter_get_info(&meta, true);
3152 if (prog)
3153 (void)udp_prog_seq_show(prog, &meta, v, 0, 0);
3154 }
3155
3156 udp_seq_stop(seq, v);
3157}
3158
3159static const struct seq_operations bpf_iter_udp_seq_ops = {
3160 .start = udp_seq_start,
3161 .next = udp_seq_next,
3162 .stop = bpf_iter_udp_seq_stop,
3163 .show = bpf_iter_udp_seq_show,
3164};
3165#endif
3166
3167const struct seq_operations udp_seq_ops = {
3168 .start = udp_seq_start,
3169 .next = udp_seq_next,
3170 .stop = udp_seq_stop,
3171 .show = udp4_seq_show,
3172};
3173EXPORT_SYMBOL(udp_seq_ops);
3174
3175static struct udp_seq_afinfo udp4_seq_afinfo = {
3176 .family = AF_INET,
3177 .udp_table = &udp_table,
3178};
3179
3180static int __net_init udp4_proc_init_net(struct net *net)
3181{
3182 if (!proc_create_net_data("udp", 0444, net->proc_net, &udp_seq_ops,
3183 sizeof(struct udp_iter_state), &udp4_seq_afinfo))
3184 return -ENOMEM;
3185 return 0;
3186}
3187
3188static void __net_exit udp4_proc_exit_net(struct net *net)
3189{
3190 remove_proc_entry("udp", net->proc_net);
3191}
3192
3193static struct pernet_operations udp4_net_ops = {
3194 .init = udp4_proc_init_net,
3195 .exit = udp4_proc_exit_net,
3196};
3197
3198int __init udp4_proc_init(void)
3199{
3200 return register_pernet_subsys(&udp4_net_ops);
3201}
3202
3203void udp4_proc_exit(void)
3204{
3205 unregister_pernet_subsys(&udp4_net_ops);
3206}
3207#endif
3208
3209static __initdata unsigned long uhash_entries;
3210static int __init set_uhash_entries(char *str)
3211{
3212 ssize_t ret;
3213
3214 if (!str)
3215 return 0;
3216
3217 ret = kstrtoul(str, 0, &uhash_entries);
3218 if (ret)
3219 return 0;
3220
3221 if (uhash_entries && uhash_entries < UDP_HTABLE_SIZE_MIN)
3222 uhash_entries = UDP_HTABLE_SIZE_MIN;
3223 return 1;
3224}
3225__setup("uhash_entries=", set_uhash_entries);
3226
3227void __init udp_table_init(struct udp_table *table, const char *name)
3228{
3229 unsigned int i;
3230
3231 table->hash = alloc_large_system_hash(name,
3232 2 * sizeof(struct udp_hslot),
3233 uhash_entries,
3234 21,
3235 0,
3236 &table->log,
3237 &table->mask,
3238 UDP_HTABLE_SIZE_MIN,
3239 64 * 1024);
3240
3241 table->hash2 = table->hash + (table->mask + 1);
3242 for (i = 0; i <= table->mask; i++) {
3243 INIT_HLIST_HEAD(&table->hash[i].head);
3244 table->hash[i].count = 0;
3245 spin_lock_init(&table->hash[i].lock);
3246 }
3247 for (i = 0; i <= table->mask; i++) {
3248 INIT_HLIST_HEAD(&table->hash2[i].head);
3249 table->hash2[i].count = 0;
3250 spin_lock_init(&table->hash2[i].lock);
3251 }
3252}
3253
3254u32 udp_flow_hashrnd(void)
3255{
3256 static u32 hashrnd __read_mostly;
3257
3258 net_get_random_once(&hashrnd, sizeof(hashrnd));
3259
3260 return hashrnd;
3261}
3262EXPORT_SYMBOL(udp_flow_hashrnd);
3263
3264static void __udp_sysctl_init(struct net *net)
3265{
3266 net->ipv4.sysctl_udp_rmem_min = SK_MEM_QUANTUM;
3267 net->ipv4.sysctl_udp_wmem_min = SK_MEM_QUANTUM;
3268
3269#ifdef CONFIG_NET_L3_MASTER_DEV
3270 net->ipv4.sysctl_udp_l3mdev_accept = 0;
3271#endif
3272}
3273
3274static int __net_init udp_sysctl_init(struct net *net)
3275{
3276 __udp_sysctl_init(net);
3277 return 0;
3278}
3279
3280static struct pernet_operations __net_initdata udp_sysctl_ops = {
3281 .init = udp_sysctl_init,
3282};
3283
3284#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
3285DEFINE_BPF_ITER_FUNC(udp, struct bpf_iter_meta *meta,
3286 struct udp_sock *udp_sk, uid_t uid, int bucket)
3287
3288static int bpf_iter_init_udp(void *priv_data, struct bpf_iter_aux_info *aux)
3289{
3290 struct udp_iter_state *st = priv_data;
3291 struct udp_seq_afinfo *afinfo;
3292 int ret;
3293
3294 afinfo = kmalloc(sizeof(*afinfo), GFP_USER | __GFP_NOWARN);
3295 if (!afinfo)
3296 return -ENOMEM;
3297
3298 afinfo->family = AF_UNSPEC;
3299 afinfo->udp_table = &udp_table;
3300 st->bpf_seq_afinfo = afinfo;
3301 ret = bpf_iter_init_seq_net(priv_data, aux);
3302 if (ret)
3303 kfree(afinfo);
3304 return ret;
3305}
3306
3307static void bpf_iter_fini_udp(void *priv_data)
3308{
3309 struct udp_iter_state *st = priv_data;
3310
3311 kfree(st->bpf_seq_afinfo);
3312 bpf_iter_fini_seq_net(priv_data);
3313}
3314
3315static const struct bpf_iter_seq_info udp_seq_info = {
3316 .seq_ops = &bpf_iter_udp_seq_ops,
3317 .init_seq_private = bpf_iter_init_udp,
3318 .fini_seq_private = bpf_iter_fini_udp,
3319 .seq_priv_size = sizeof(struct udp_iter_state),
3320};
3321
3322static struct bpf_iter_reg udp_reg_info = {
3323 .target = "udp",
3324 .ctx_arg_info_size = 1,
3325 .ctx_arg_info = {
3326 { offsetof(struct bpf_iter__udp, udp_sk),
3327 PTR_TO_BTF_ID_OR_NULL },
3328 },
3329 .seq_info = &udp_seq_info,
3330};
3331
3332static void __init bpf_iter_register(void)
3333{
3334 udp_reg_info.ctx_arg_info[0].btf_id = btf_sock_ids[BTF_SOCK_TYPE_UDP];
3335 if (bpf_iter_reg_target(&udp_reg_info))
3336 pr_warn("Warning: could not register bpf iterator udp\n");
3337}
3338#endif
3339
3340void __init udp_init(void)
3341{
3342 unsigned long limit;
3343 unsigned int i;
3344
3345 udp_table_init(&udp_table, "UDP");
3346 limit = nr_free_buffer_pages() / 8;
3347 limit = max(limit, 128UL);
3348 sysctl_udp_mem[0] = limit / 4 * 3;
3349 sysctl_udp_mem[1] = limit;
3350 sysctl_udp_mem[2] = sysctl_udp_mem[0] * 2;
3351
3352 __udp_sysctl_init(&init_net);
3353
3354
3355 udp_busylocks_log = ilog2(nr_cpu_ids) + 4;
3356 udp_busylocks = kmalloc(sizeof(spinlock_t) << udp_busylocks_log,
3357 GFP_KERNEL);
3358 if (!udp_busylocks)
3359 panic("UDP: failed to alloc udp_busylocks\n");
3360 for (i = 0; i < (1U << udp_busylocks_log); i++)
3361 spin_lock_init(udp_busylocks + i);
3362
3363 if (register_pernet_subsys(&udp_sysctl_ops))
3364 panic("UDP: failed to init sysctl parameters.\n");
3365
3366#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_PROC_FS)
3367 bpf_iter_register();
3368#endif
3369}
3370