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