GNU Linux-libre 4.19.286-gnu1
[releases.git] / net / ipv6 / udp.c
1 /*
2  *      UDP over IPv6
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>
7  *
8  *      Based on linux/ipv4/udp.c
9  *
10  *      Fixes:
11  *      Hideaki YOSHIFUJI       :       sin6_scope_id support
12  *      YOSHIFUJI Hideaki @USAGI and:   Support IPV6_V6ONLY socket option, which
13  *      Alexey Kuznetsov                allow both IPv4 and IPv6 sockets to bind
14  *                                      a single port at the same time.
15  *      Kazunori MIYAZAWA @USAGI:       change process style to use ip6_append_data
16  *      YOSHIFUJI Hideaki @USAGI:       convert /proc/net/udp6 to seq_file.
17  *
18  *      This program is free software; you can redistribute it and/or
19  *      modify it under the terms of the GNU General Public License
20  *      as published by the Free Software Foundation; either version
21  *      2 of the License, or (at your option) any later version.
22  */
23
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/in6.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_arp.h>
32 #include <linux/ipv6.h>
33 #include <linux/icmpv6.h>
34 #include <linux/init.h>
35 #include <linux/module.h>
36 #include <linux/skbuff.h>
37 #include <linux/slab.h>
38 #include <linux/uaccess.h>
39
40 #include <net/addrconf.h>
41 #include <net/ndisc.h>
42 #include <net/protocol.h>
43 #include <net/transp_v6.h>
44 #include <net/ip6_route.h>
45 #include <net/raw.h>
46 #include <net/tcp_states.h>
47 #include <net/ip6_checksum.h>
48 #include <net/xfrm.h>
49 #include <net/inet_hashtables.h>
50 #include <net/inet6_hashtables.h>
51 #include <net/busy_poll.h>
52 #include <net/sock_reuseport.h>
53
54 #include <linux/proc_fs.h>
55 #include <linux/seq_file.h>
56 #include <trace/events/skb.h>
57 #include "udp_impl.h"
58
59 static bool udp6_lib_exact_dif_match(struct net *net, struct sk_buff *skb)
60 {
61 #if defined(CONFIG_NET_L3_MASTER_DEV)
62         if (!net->ipv4.sysctl_udp_l3mdev_accept &&
63             skb && ipv6_l3mdev_skb(IP6CB(skb)->flags))
64                 return true;
65 #endif
66         return false;
67 }
68
69 static void udpv6_destruct_sock(struct sock *sk)
70 {
71         udp_destruct_common(sk);
72         inet6_sock_destruct(sk);
73 }
74
75 int udpv6_init_sock(struct sock *sk)
76 {
77         skb_queue_head_init(&udp_sk(sk)->reader_queue);
78         sk->sk_destruct = udpv6_destruct_sock;
79         return 0;
80 }
81
82 static u32 udp6_ehashfn(const struct net *net,
83                         const struct in6_addr *laddr,
84                         const u16 lport,
85                         const struct in6_addr *faddr,
86                         const __be16 fport)
87 {
88         static u32 udp6_ehash_secret __read_mostly;
89         static u32 udp_ipv6_hash_secret __read_mostly;
90
91         u32 lhash, fhash;
92
93         net_get_random_once(&udp6_ehash_secret,
94                             sizeof(udp6_ehash_secret));
95         net_get_random_once(&udp_ipv6_hash_secret,
96                             sizeof(udp_ipv6_hash_secret));
97
98         lhash = (__force u32)laddr->s6_addr32[3];
99         fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
100
101         return __inet6_ehashfn(lhash, lport, fhash, fport,
102                                udp_ipv6_hash_secret + net_hash_mix(net));
103 }
104
105 int udp_v6_get_port(struct sock *sk, unsigned short snum)
106 {
107         unsigned int hash2_nulladdr =
108                 ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
109         unsigned int hash2_partial =
110                 ipv6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
111
112         /* precompute partial secondary hash */
113         udp_sk(sk)->udp_portaddr_hash = hash2_partial;
114         return udp_lib_get_port(sk, snum, hash2_nulladdr);
115 }
116
117 static void udp_v6_rehash(struct sock *sk)
118 {
119         u16 new_hash = ipv6_portaddr_hash(sock_net(sk),
120                                           &sk->sk_v6_rcv_saddr,
121                                           inet_sk(sk)->inet_num);
122
123         udp_lib_rehash(sk, new_hash);
124 }
125
126 static int compute_score(struct sock *sk, struct net *net,
127                          const struct in6_addr *saddr, __be16 sport,
128                          const struct in6_addr *daddr, unsigned short hnum,
129                          int dif, int sdif, bool exact_dif)
130 {
131         int score;
132         struct inet_sock *inet;
133
134         if (!net_eq(sock_net(sk), net) ||
135             udp_sk(sk)->udp_port_hash != hnum ||
136             sk->sk_family != PF_INET6)
137                 return -1;
138
139         score = 0;
140         inet = inet_sk(sk);
141
142         if (inet->inet_dport) {
143                 if (inet->inet_dport != sport)
144                         return -1;
145                 score++;
146         }
147
148         if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
149                 if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
150                         return -1;
151                 score++;
152         }
153
154         if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
155                 if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
156                         return -1;
157                 score++;
158         }
159
160         if (sk->sk_bound_dev_if || exact_dif) {
161                 bool dev_match = (sk->sk_bound_dev_if == dif ||
162                                   sk->sk_bound_dev_if == sdif);
163
164                 if (!dev_match)
165                         return -1;
166                 if (sk->sk_bound_dev_if)
167                         score++;
168         }
169
170         if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
171                 score++;
172
173         return score;
174 }
175
176 /* called with rcu_read_lock() */
177 static struct sock *udp6_lib_lookup2(struct net *net,
178                 const struct in6_addr *saddr, __be16 sport,
179                 const struct in6_addr *daddr, unsigned int hnum,
180                 int dif, int sdif, bool exact_dif,
181                 struct udp_hslot *hslot2, struct sk_buff *skb)
182 {
183         struct sock *sk, *result, *reuseport_result;
184         int score, badness;
185         u32 hash = 0;
186
187         result = NULL;
188         badness = -1;
189         udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
190                 score = compute_score(sk, net, saddr, sport,
191                                       daddr, hnum, dif, sdif, exact_dif);
192                 if (score > badness) {
193                         reuseport_result = NULL;
194
195                         if (sk->sk_reuseport &&
196                             sk->sk_state != TCP_ESTABLISHED) {
197                                 hash = udp6_ehashfn(net, daddr, hnum,
198                                                     saddr, sport);
199
200                                 reuseport_result = reuseport_select_sock(sk, hash, skb,
201                                                                          sizeof(struct udphdr));
202                                 if (reuseport_result && !reuseport_has_conns(sk, false))
203                                         return reuseport_result;
204                         }
205
206                         result = reuseport_result ? : sk;
207                         badness = score;
208                 }
209         }
210         return result;
211 }
212
213 /* rcu_read_lock() must be held */
214 struct sock *__udp6_lib_lookup(struct net *net,
215                                const struct in6_addr *saddr, __be16 sport,
216                                const struct in6_addr *daddr, __be16 dport,
217                                int dif, int sdif, struct udp_table *udptable,
218                                struct sk_buff *skb)
219 {
220         struct sock *sk, *result;
221         unsigned short hnum = ntohs(dport);
222         unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
223         struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
224         bool exact_dif = udp6_lib_exact_dif_match(net, skb);
225         int score, badness;
226         u32 hash = 0;
227
228         if (hslot->count > 10) {
229                 hash2 = ipv6_portaddr_hash(net, daddr, hnum);
230                 slot2 = hash2 & udptable->mask;
231                 hslot2 = &udptable->hash2[slot2];
232                 if (hslot->count < hslot2->count)
233                         goto begin;
234
235                 result = udp6_lib_lookup2(net, saddr, sport,
236                                           daddr, hnum, dif, sdif, exact_dif,
237                                           hslot2, skb);
238                 if (!result) {
239                         unsigned int old_slot2 = slot2;
240                         hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
241                         slot2 = hash2 & udptable->mask;
242                         /* avoid searching the same slot again. */
243                         if (unlikely(slot2 == old_slot2))
244                                 return result;
245
246                         hslot2 = &udptable->hash2[slot2];
247                         if (hslot->count < hslot2->count)
248                                 goto begin;
249
250                         result = udp6_lib_lookup2(net, saddr, sport,
251                                                   daddr, hnum, dif, sdif,
252                                                   exact_dif, hslot2,
253                                                   skb);
254                 }
255                 if (unlikely(IS_ERR(result)))
256                         return NULL;
257                 return result;
258         }
259 begin:
260         result = NULL;
261         badness = -1;
262         sk_for_each_rcu(sk, &hslot->head) {
263                 score = compute_score(sk, net, saddr, sport, daddr, hnum, dif,
264                                       sdif, exact_dif);
265                 if (score > badness) {
266                         if (sk->sk_reuseport) {
267                                 hash = udp6_ehashfn(net, daddr, hnum,
268                                                     saddr, sport);
269                                 result = reuseport_select_sock(sk, hash, skb,
270                                                         sizeof(struct udphdr));
271                                 if (unlikely(IS_ERR(result)))
272                                         return NULL;
273                                 if (result)
274                                         return result;
275                         }
276                         result = sk;
277                         badness = score;
278                 }
279         }
280         return result;
281 }
282 EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
283
284 static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
285                                           __be16 sport, __be16 dport,
286                                           struct udp_table *udptable)
287 {
288         const struct ipv6hdr *iph = ipv6_hdr(skb);
289
290         return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
291                                  &iph->daddr, dport, inet6_iif(skb),
292                                  inet6_sdif(skb), udptable, skb);
293 }
294
295 struct sock *udp6_lib_lookup_skb(struct sk_buff *skb,
296                                  __be16 sport, __be16 dport)
297 {
298         const struct ipv6hdr *iph = ipv6_hdr(skb);
299
300         return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
301                                  &iph->daddr, dport, inet6_iif(skb),
302                                  inet6_sdif(skb), &udp_table, NULL);
303 }
304 EXPORT_SYMBOL_GPL(udp6_lib_lookup_skb);
305
306 /* Must be called under rcu_read_lock().
307  * Does increment socket refcount.
308  */
309 #if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6)
310 struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
311                              const struct in6_addr *daddr, __be16 dport, int dif)
312 {
313         struct sock *sk;
314
315         sk =  __udp6_lib_lookup(net, saddr, sport, daddr, dport,
316                                 dif, 0, &udp_table, NULL);
317         if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
318                 sk = NULL;
319         return sk;
320 }
321 EXPORT_SYMBOL_GPL(udp6_lib_lookup);
322 #endif
323
324 /* do not use the scratch area len for jumbogram: their length execeeds the
325  * scratch area space; note that the IP6CB flags is still in the first
326  * cacheline, so checking for jumbograms is cheap
327  */
328 static int udp6_skb_len(struct sk_buff *skb)
329 {
330         return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb);
331 }
332
333 /*
334  *      This should be easy, if there is something there we
335  *      return it, otherwise we block.
336  */
337
338 int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
339                   int noblock, int flags, int *addr_len)
340 {
341         struct ipv6_pinfo *np = inet6_sk(sk);
342         struct inet_sock *inet = inet_sk(sk);
343         struct sk_buff *skb;
344         unsigned int ulen, copied;
345         int peeked, peeking, off;
346         int err;
347         int is_udplite = IS_UDPLITE(sk);
348         bool checksum_valid = false;
349         int is_udp4;
350
351         if (flags & MSG_ERRQUEUE)
352                 return ipv6_recv_error(sk, msg, len, addr_len);
353
354         if (np->rxpmtu && np->rxopt.bits.rxpmtu)
355                 return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
356
357 try_again:
358         peeking = flags & MSG_PEEK;
359         off = sk_peek_offset(sk, flags);
360         skb = __skb_recv_udp(sk, flags, noblock, &peeked, &off, &err);
361         if (!skb)
362                 return err;
363
364         ulen = udp6_skb_len(skb);
365         copied = len;
366         if (copied > ulen - off)
367                 copied = ulen - off;
368         else if (copied < ulen)
369                 msg->msg_flags |= MSG_TRUNC;
370
371         is_udp4 = (skb->protocol == htons(ETH_P_IP));
372
373         /*
374          * If checksum is needed at all, try to do it while copying the
375          * data.  If the data is truncated, or if we only want a partial
376          * coverage checksum (UDP-Lite), do it before the copy.
377          */
378
379         if (copied < ulen || peeking ||
380             (is_udplite && UDP_SKB_CB(skb)->partial_cov)) {
381                 checksum_valid = udp_skb_csum_unnecessary(skb) ||
382                                 !__udp_lib_checksum_complete(skb);
383                 if (!checksum_valid)
384                         goto csum_copy_err;
385         }
386
387         if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
388                 if (udp_skb_is_linear(skb))
389                         err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
390                 else
391                         err = skb_copy_datagram_msg(skb, off, msg, copied);
392         } else {
393                 err = skb_copy_and_csum_datagram_msg(skb, off, msg);
394                 if (err == -EINVAL)
395                         goto csum_copy_err;
396         }
397         if (unlikely(err)) {
398                 if (!peeked) {
399                         atomic_inc(&sk->sk_drops);
400                         if (is_udp4)
401                                 UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
402                                               is_udplite);
403                         else
404                                 UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
405                                                is_udplite);
406                 }
407                 kfree_skb(skb);
408                 return err;
409         }
410         if (!peeked) {
411                 if (is_udp4)
412                         UDP_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS,
413                                       is_udplite);
414                 else
415                         UDP6_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS,
416                                        is_udplite);
417         }
418
419         sock_recv_ts_and_drops(msg, sk, skb);
420
421         /* Copy the address. */
422         if (msg->msg_name) {
423                 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
424                 sin6->sin6_family = AF_INET6;
425                 sin6->sin6_port = udp_hdr(skb)->source;
426                 sin6->sin6_flowinfo = 0;
427
428                 if (is_udp4) {
429                         ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
430                                                &sin6->sin6_addr);
431                         sin6->sin6_scope_id = 0;
432                 } else {
433                         sin6->sin6_addr = ipv6_hdr(skb)->saddr;
434                         sin6->sin6_scope_id =
435                                 ipv6_iface_scope_id(&sin6->sin6_addr,
436                                                     inet6_iif(skb));
437                 }
438                 *addr_len = sizeof(*sin6);
439
440                 if (cgroup_bpf_enabled)
441                         BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk,
442                                                 (struct sockaddr *)sin6);
443         }
444
445         if (np->rxopt.all)
446                 ip6_datagram_recv_common_ctl(sk, msg, skb);
447
448         if (is_udp4) {
449                 if (inet->cmsg_flags)
450                         ip_cmsg_recv_offset(msg, sk, skb,
451                                             sizeof(struct udphdr), off);
452         } else {
453                 if (np->rxopt.all)
454                         ip6_datagram_recv_specific_ctl(sk, msg, skb);
455         }
456
457         err = copied;
458         if (flags & MSG_TRUNC)
459                 err = ulen;
460
461         skb_consume_udp(sk, skb, peeking ? -err : err);
462         return err;
463
464 csum_copy_err:
465         if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
466                                  udp_skb_destructor)) {
467                 if (is_udp4) {
468                         UDP_INC_STATS(sock_net(sk),
469                                       UDP_MIB_CSUMERRORS, is_udplite);
470                         UDP_INC_STATS(sock_net(sk),
471                                       UDP_MIB_INERRORS, is_udplite);
472                 } else {
473                         UDP6_INC_STATS(sock_net(sk),
474                                        UDP_MIB_CSUMERRORS, is_udplite);
475                         UDP6_INC_STATS(sock_net(sk),
476                                        UDP_MIB_INERRORS, is_udplite);
477                 }
478         }
479         kfree_skb(skb);
480
481         /* starting over for a new packet, but check if we need to yield */
482         cond_resched();
483         msg->msg_flags &= ~MSG_TRUNC;
484         goto try_again;
485 }
486
487 void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
488                     u8 type, u8 code, int offset, __be32 info,
489                     struct udp_table *udptable)
490 {
491         struct ipv6_pinfo *np;
492         const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
493         const struct in6_addr *saddr = &hdr->saddr;
494         const struct in6_addr *daddr = &hdr->daddr;
495         struct udphdr *uh = (struct udphdr *)(skb->data+offset);
496         struct sock *sk;
497         int harderr;
498         int err;
499         struct net *net = dev_net(skb->dev);
500
501         sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
502                                inet6_iif(skb), 0, udptable, NULL);
503         if (!sk) {
504                 __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
505                                   ICMP6_MIB_INERRORS);
506                 return;
507         }
508
509         harderr = icmpv6_err_convert(type, code, &err);
510         np = inet6_sk(sk);
511
512         if (type == ICMPV6_PKT_TOOBIG) {
513                 if (!ip6_sk_accept_pmtu(sk))
514                         goto out;
515                 ip6_sk_update_pmtu(skb, sk, info);
516                 if (np->pmtudisc != IPV6_PMTUDISC_DONT)
517                         harderr = 1;
518         }
519         if (type == NDISC_REDIRECT) {
520                 ip6_sk_redirect(skb, sk);
521                 goto out;
522         }
523
524         if (!np->recverr) {
525                 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
526                         goto out;
527         } else {
528                 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
529         }
530
531         sk->sk_err = err;
532         sk->sk_error_report(sk);
533 out:
534         return;
535 }
536
537 static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
538 {
539         int rc;
540
541         if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
542                 sock_rps_save_rxhash(sk, skb);
543                 sk_mark_napi_id(sk, skb);
544                 sk_incoming_cpu_update(sk);
545         } else {
546                 sk_mark_napi_id_once(sk, skb);
547         }
548
549         rc = __udp_enqueue_schedule_skb(sk, skb);
550         if (rc < 0) {
551                 int is_udplite = IS_UDPLITE(sk);
552
553                 /* Note that an ENOMEM error is charged twice */
554                 if (rc == -ENOMEM)
555                         UDP6_INC_STATS(sock_net(sk),
556                                          UDP_MIB_RCVBUFERRORS, is_udplite);
557                 UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
558                 kfree_skb(skb);
559                 return -1;
560         }
561
562         return 0;
563 }
564
565 static __inline__ void udpv6_err(struct sk_buff *skb,
566                                  struct inet6_skb_parm *opt, u8 type,
567                                  u8 code, int offset, __be32 info)
568 {
569         __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
570 }
571
572 static DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
573 void udpv6_encap_enable(void)
574 {
575         static_branch_enable(&udpv6_encap_needed_key);
576 }
577 EXPORT_SYMBOL(udpv6_encap_enable);
578
579 static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
580 {
581         struct udp_sock *up = udp_sk(sk);
582         int is_udplite = IS_UDPLITE(sk);
583
584         if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
585                 goto drop;
586
587         if (static_branch_unlikely(&udpv6_encap_needed_key) && up->encap_type) {
588                 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
589
590                 /*
591                  * This is an encapsulation socket so pass the skb to
592                  * the socket's udp_encap_rcv() hook. Otherwise, just
593                  * fall through and pass this up the UDP socket.
594                  * up->encap_rcv() returns the following value:
595                  * =0 if skb was successfully passed to the encap
596                  *    handler or was discarded by it.
597                  * >0 if skb should be passed on to UDP.
598                  * <0 if skb should be resubmitted as proto -N
599                  */
600
601                 /* if we're overly short, let UDP handle it */
602                 encap_rcv = READ_ONCE(up->encap_rcv);
603                 if (encap_rcv) {
604                         int ret;
605
606                         /* Verify checksum before giving to encap */
607                         if (udp_lib_checksum_complete(skb))
608                                 goto csum_error;
609
610                         ret = encap_rcv(sk, skb);
611                         if (ret <= 0) {
612                                 __UDP_INC_STATS(sock_net(sk),
613                                                 UDP_MIB_INDATAGRAMS,
614                                                 is_udplite);
615                                 return -ret;
616                         }
617                 }
618
619                 /* FALLTHROUGH -- it's a UDP Packet */
620         }
621
622         /*
623          * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
624          */
625         if ((up->pcflag & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
626
627                 if (up->pcrlen == 0) {          /* full coverage was set  */
628                         net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
629                                             UDP_SKB_CB(skb)->cscov, skb->len);
630                         goto drop;
631                 }
632                 if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
633                         net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
634                                             UDP_SKB_CB(skb)->cscov, up->pcrlen);
635                         goto drop;
636                 }
637         }
638
639         prefetch(&sk->sk_rmem_alloc);
640         if (rcu_access_pointer(sk->sk_filter) &&
641             udp_lib_checksum_complete(skb))
642                 goto csum_error;
643
644         if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
645                 goto drop;
646
647         udp_csum_pull_header(skb);
648
649         skb_dst_drop(skb);
650
651         return __udpv6_queue_rcv_skb(sk, skb);
652
653 csum_error:
654         __UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
655 drop:
656         __UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
657         atomic_inc(&sk->sk_drops);
658         kfree_skb(skb);
659         return -1;
660 }
661
662 static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
663                                    __be16 loc_port, const struct in6_addr *loc_addr,
664                                    __be16 rmt_port, const struct in6_addr *rmt_addr,
665                                    int dif, unsigned short hnum)
666 {
667         struct inet_sock *inet = inet_sk(sk);
668
669         if (!net_eq(sock_net(sk), net))
670                 return false;
671
672         if (udp_sk(sk)->udp_port_hash != hnum ||
673             sk->sk_family != PF_INET6 ||
674             (inet->inet_dport && inet->inet_dport != rmt_port) ||
675             (!ipv6_addr_any(&sk->sk_v6_daddr) &&
676                     !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
677             (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) ||
678             (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
679                     !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
680                 return false;
681         if (!inet6_mc_check(sk, loc_addr, rmt_addr))
682                 return false;
683         return true;
684 }
685
686 static void udp6_csum_zero_error(struct sk_buff *skb)
687 {
688         /* RFC 2460 section 8.1 says that we SHOULD log
689          * this error. Well, it is reasonable.
690          */
691         net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
692                             &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
693                             &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
694 }
695
696 /*
697  * Note: called only from the BH handler context,
698  * so we don't need to lock the hashes.
699  */
700 static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
701                 const struct in6_addr *saddr, const struct in6_addr *daddr,
702                 struct udp_table *udptable, int proto)
703 {
704         struct sock *sk, *first = NULL;
705         const struct udphdr *uh = udp_hdr(skb);
706         unsigned short hnum = ntohs(uh->dest);
707         struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
708         unsigned int offset = offsetof(typeof(*sk), sk_node);
709         unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
710         int dif = inet6_iif(skb);
711         struct hlist_node *node;
712         struct sk_buff *nskb;
713
714         if (use_hash2) {
715                 hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) &
716                             udptable->mask;
717                 hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask;
718 start_lookup:
719                 hslot = &udptable->hash2[hash2];
720                 offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
721         }
722
723         sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
724                 if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
725                                             uh->source, saddr, dif, hnum))
726                         continue;
727                 /* If zero checksum and no_check is not on for
728                  * the socket then skip it.
729                  */
730                 if (!uh->check && !udp_sk(sk)->no_check6_rx)
731                         continue;
732                 if (!first) {
733                         first = sk;
734                         continue;
735                 }
736                 nskb = skb_clone(skb, GFP_ATOMIC);
737                 if (unlikely(!nskb)) {
738                         atomic_inc(&sk->sk_drops);
739                         __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
740                                          IS_UDPLITE(sk));
741                         __UDP6_INC_STATS(net, UDP_MIB_INERRORS,
742                                          IS_UDPLITE(sk));
743                         continue;
744                 }
745
746                 if (udpv6_queue_rcv_skb(sk, nskb) > 0)
747                         consume_skb(nskb);
748         }
749
750         /* Also lookup *:port if we are using hash2 and haven't done so yet. */
751         if (use_hash2 && hash2 != hash2_any) {
752                 hash2 = hash2_any;
753                 goto start_lookup;
754         }
755
756         if (first) {
757                 if (udpv6_queue_rcv_skb(first, skb) > 0)
758                         consume_skb(skb);
759         } else {
760                 kfree_skb(skb);
761                 __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
762                                  proto == IPPROTO_UDPLITE);
763         }
764         return 0;
765 }
766
767 static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
768 {
769         if (udp_sk_rx_dst_set(sk, dst)) {
770                 const struct rt6_info *rt = (const struct rt6_info *)dst;
771
772                 inet6_sk(sk)->rx_dst_cookie = rt6_get_cookie(rt);
773         }
774 }
775
776 /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and
777  * return code conversion for ip layer consumption
778  */
779 static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,
780                                 struct udphdr *uh)
781 {
782         int ret;
783
784         if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
785                 skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
786                                          ip6_compute_pseudo);
787
788         ret = udpv6_queue_rcv_skb(sk, skb);
789
790         /* a return value > 0 means to resubmit the input */
791         if (ret > 0)
792                 return ret;
793         return 0;
794 }
795
796 int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
797                    int proto)
798 {
799         const struct in6_addr *saddr, *daddr;
800         struct net *net = dev_net(skb->dev);
801         struct udphdr *uh;
802         struct sock *sk;
803         u32 ulen = 0;
804
805         if (!pskb_may_pull(skb, sizeof(struct udphdr)))
806                 goto discard;
807
808         saddr = &ipv6_hdr(skb)->saddr;
809         daddr = &ipv6_hdr(skb)->daddr;
810         uh = udp_hdr(skb);
811
812         ulen = ntohs(uh->len);
813         if (ulen > skb->len)
814                 goto short_packet;
815
816         if (proto == IPPROTO_UDP) {
817                 /* UDP validates ulen. */
818
819                 /* Check for jumbo payload */
820                 if (ulen == 0)
821                         ulen = skb->len;
822
823                 if (ulen < sizeof(*uh))
824                         goto short_packet;
825
826                 if (ulen < skb->len) {
827                         if (pskb_trim_rcsum(skb, ulen))
828                                 goto short_packet;
829                         saddr = &ipv6_hdr(skb)->saddr;
830                         daddr = &ipv6_hdr(skb)->daddr;
831                         uh = udp_hdr(skb);
832                 }
833         }
834
835         if (udp6_csum_init(skb, uh, proto))
836                 goto csum_error;
837
838         /* Check if the socket is already available, e.g. due to early demux */
839         sk = skb_steal_sock(skb);
840         if (sk) {
841                 struct dst_entry *dst = skb_dst(skb);
842                 int ret;
843
844                 if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst))
845                         udp6_sk_rx_dst_set(sk, dst);
846
847                 if (!uh->check && !udp_sk(sk)->no_check6_rx) {
848                         sock_put(sk);
849                         goto report_csum_error;
850                 }
851
852                 ret = udp6_unicast_rcv_skb(sk, skb, uh);
853                 sock_put(sk);
854                 return ret;
855         }
856
857         /*
858          *      Multicast receive code
859          */
860         if (ipv6_addr_is_multicast(daddr))
861                 return __udp6_lib_mcast_deliver(net, skb,
862                                 saddr, daddr, udptable, proto);
863
864         /* Unicast */
865         sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
866         if (sk) {
867                 if (!uh->check && !udp_sk(sk)->no_check6_rx)
868                         goto report_csum_error;
869                 return udp6_unicast_rcv_skb(sk, skb, uh);
870         }
871
872         if (!uh->check)
873                 goto report_csum_error;
874
875         if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
876                 goto discard;
877
878         if (udp_lib_checksum_complete(skb))
879                 goto csum_error;
880
881         __UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
882         icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
883
884         kfree_skb(skb);
885         return 0;
886
887 short_packet:
888         net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
889                             proto == IPPROTO_UDPLITE ? "-Lite" : "",
890                             saddr, ntohs(uh->source),
891                             ulen, skb->len,
892                             daddr, ntohs(uh->dest));
893         goto discard;
894
895 report_csum_error:
896         udp6_csum_zero_error(skb);
897 csum_error:
898         __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
899 discard:
900         __UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
901         kfree_skb(skb);
902         return 0;
903 }
904
905
906 static struct sock *__udp6_lib_demux_lookup(struct net *net,
907                         __be16 loc_port, const struct in6_addr *loc_addr,
908                         __be16 rmt_port, const struct in6_addr *rmt_addr,
909                         int dif, int sdif)
910 {
911         unsigned short hnum = ntohs(loc_port);
912         unsigned int hash2 = ipv6_portaddr_hash(net, loc_addr, hnum);
913         unsigned int slot2 = hash2 & udp_table.mask;
914         struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
915         const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
916         struct sock *sk;
917
918         udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
919                 if (sk->sk_state == TCP_ESTABLISHED &&
920                     INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif))
921                         return sk;
922                 /* Only check first socket in chain */
923                 break;
924         }
925         return NULL;
926 }
927
928 void udp_v6_early_demux(struct sk_buff *skb)
929 {
930         struct net *net = dev_net(skb->dev);
931         const struct udphdr *uh;
932         struct sock *sk;
933         struct dst_entry *dst;
934         int dif = skb->dev->ifindex;
935         int sdif = inet6_sdif(skb);
936
937         if (!pskb_may_pull(skb, skb_transport_offset(skb) +
938             sizeof(struct udphdr)))
939                 return;
940
941         uh = udp_hdr(skb);
942
943         if (skb->pkt_type == PACKET_HOST)
944                 sk = __udp6_lib_demux_lookup(net, uh->dest,
945                                              &ipv6_hdr(skb)->daddr,
946                                              uh->source, &ipv6_hdr(skb)->saddr,
947                                              dif, sdif);
948         else
949                 return;
950
951         if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
952                 return;
953
954         skb->sk = sk;
955         skb->destructor = sock_efree;
956         dst = rcu_dereference(sk->sk_rx_dst);
957
958         if (dst)
959                 dst = dst_check(dst, inet6_sk(sk)->rx_dst_cookie);
960         if (dst) {
961                 /* set noref for now.
962                  * any place which wants to hold dst has to call
963                  * dst_hold_safe()
964                  */
965                 skb_dst_set_noref(skb, dst);
966         }
967 }
968
969 static __inline__ int udpv6_rcv(struct sk_buff *skb)
970 {
971         return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
972 }
973
974 /*
975  * Throw away all pending data and cancel the corking. Socket is locked.
976  */
977 static void udp_v6_flush_pending_frames(struct sock *sk)
978 {
979         struct udp_sock *up = udp_sk(sk);
980
981         if (up->pending == AF_INET)
982                 udp_flush_pending_frames(sk);
983         else if (up->pending) {
984                 up->len = 0;
985                 up->pending = 0;
986                 ip6_flush_pending_frames(sk);
987         }
988 }
989
990 static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr,
991                              int addr_len)
992 {
993         /* The following checks are replicated from __ip6_datagram_connect()
994          * and intended to prevent BPF program called below from accessing
995          * bytes that are out of the bound specified by user in addr_len.
996          */
997         if (uaddr->sa_family == AF_INET) {
998                 if (__ipv6_only_sock(sk))
999                         return -EAFNOSUPPORT;
1000                 return udp_pre_connect(sk, uaddr, addr_len);
1001         }
1002
1003         if (addr_len < SIN6_LEN_RFC2133)
1004                 return -EINVAL;
1005
1006         return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr);
1007 }
1008
1009 /**
1010  *      udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
1011  *      @sk:    socket we are sending on
1012  *      @skb:   sk_buff containing the filled-in UDP header
1013  *              (checksum field must be zeroed out)
1014  */
1015 static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
1016                                  const struct in6_addr *saddr,
1017                                  const struct in6_addr *daddr, int len)
1018 {
1019         unsigned int offset;
1020         struct udphdr *uh = udp_hdr(skb);
1021         struct sk_buff *frags = skb_shinfo(skb)->frag_list;
1022         __wsum csum = 0;
1023
1024         if (!frags) {
1025                 /* Only one fragment on the socket.  */
1026                 skb->csum_start = skb_transport_header(skb) - skb->head;
1027                 skb->csum_offset = offsetof(struct udphdr, check);
1028                 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
1029         } else {
1030                 /*
1031                  * HW-checksum won't work as there are two or more
1032                  * fragments on the socket so that all csums of sk_buffs
1033                  * should be together
1034                  */
1035                 offset = skb_transport_offset(skb);
1036                 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
1037                 csum = skb->csum;
1038
1039                 skb->ip_summed = CHECKSUM_NONE;
1040
1041                 do {
1042                         csum = csum_add(csum, frags->csum);
1043                 } while ((frags = frags->next));
1044
1045                 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
1046                                             csum);
1047                 if (uh->check == 0)
1048                         uh->check = CSUM_MANGLED_0;
1049         }
1050 }
1051
1052 /*
1053  *      Sending
1054  */
1055
1056 static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6,
1057                            struct inet_cork *cork)
1058 {
1059         struct sock *sk = skb->sk;
1060         struct udphdr *uh;
1061         int err = 0;
1062         int is_udplite = IS_UDPLITE(sk);
1063         __wsum csum = 0;
1064         int offset = skb_transport_offset(skb);
1065         int len = skb->len - offset;
1066         int datalen = len - sizeof(*uh);
1067
1068         /*
1069          * Create a UDP header
1070          */
1071         uh = udp_hdr(skb);
1072         uh->source = fl6->fl6_sport;
1073         uh->dest = fl6->fl6_dport;
1074         uh->len = htons(len);
1075         uh->check = 0;
1076
1077         if (cork->gso_size) {
1078                 const int hlen = skb_network_header_len(skb) +
1079                                  sizeof(struct udphdr);
1080
1081                 if (hlen + cork->gso_size > cork->fragsize) {
1082                         kfree_skb(skb);
1083                         return -EINVAL;
1084                 }
1085                 if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) {
1086                         kfree_skb(skb);
1087                         return -EINVAL;
1088                 }
1089                 if (udp_sk(sk)->no_check6_tx) {
1090                         kfree_skb(skb);
1091                         return -EINVAL;
1092                 }
1093                 if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite ||
1094                     dst_xfrm(skb_dst(skb))) {
1095                         kfree_skb(skb);
1096                         return -EIO;
1097                 }
1098
1099                 if (datalen > cork->gso_size) {
1100                         skb_shinfo(skb)->gso_size = cork->gso_size;
1101                         skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
1102                         skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen,
1103                                                                  cork->gso_size);
1104                 }
1105                 goto csum_partial;
1106         }
1107
1108         if (is_udplite)
1109                 csum = udplite_csum(skb);
1110         else if (udp_sk(sk)->no_check6_tx) {   /* UDP csum disabled */
1111                 skb->ip_summed = CHECKSUM_NONE;
1112                 goto send;
1113         } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
1114 csum_partial:
1115                 udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
1116                 goto send;
1117         } else
1118                 csum = udp_csum(skb);
1119
1120         /* add protocol-dependent pseudo-header */
1121         uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
1122                                     len, fl6->flowi6_proto, csum);
1123         if (uh->check == 0)
1124                 uh->check = CSUM_MANGLED_0;
1125
1126 send:
1127         err = ip6_send_skb(skb);
1128         if (err) {
1129                 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
1130                         UDP6_INC_STATS(sock_net(sk),
1131                                        UDP_MIB_SNDBUFERRORS, is_udplite);
1132                         err = 0;
1133                 }
1134         } else {
1135                 UDP6_INC_STATS(sock_net(sk),
1136                                UDP_MIB_OUTDATAGRAMS, is_udplite);
1137         }
1138         return err;
1139 }
1140
1141 static int udp_v6_push_pending_frames(struct sock *sk)
1142 {
1143         struct sk_buff *skb;
1144         struct udp_sock  *up = udp_sk(sk);
1145         struct flowi6 fl6;
1146         int err = 0;
1147
1148         if (up->pending == AF_INET)
1149                 return udp_push_pending_frames(sk);
1150
1151         /* ip6_finish_skb will release the cork, so make a copy of
1152          * fl6 here.
1153          */
1154         fl6 = inet_sk(sk)->cork.fl.u.ip6;
1155
1156         skb = ip6_finish_skb(sk);
1157         if (!skb)
1158                 goto out;
1159
1160         err = udp_v6_send_skb(skb, &fl6, &inet_sk(sk)->cork.base);
1161
1162 out:
1163         up->len = 0;
1164         up->pending = 0;
1165         return err;
1166 }
1167
1168 int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1169 {
1170         struct ipv6_txoptions opt_space;
1171         struct udp_sock *up = udp_sk(sk);
1172         struct inet_sock *inet = inet_sk(sk);
1173         struct ipv6_pinfo *np = inet6_sk(sk);
1174         DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
1175         struct in6_addr *daddr, *final_p, final;
1176         struct ipv6_txoptions *opt = NULL;
1177         struct ipv6_txoptions *opt_to_free = NULL;
1178         struct ip6_flowlabel *flowlabel = NULL;
1179         struct flowi6 fl6;
1180         struct dst_entry *dst;
1181         struct ipcm6_cookie ipc6;
1182         int addr_len = msg->msg_namelen;
1183         bool connected = false;
1184         int ulen = len;
1185         int corkreq = READ_ONCE(up->corkflag) || msg->msg_flags&MSG_MORE;
1186         int err;
1187         int is_udplite = IS_UDPLITE(sk);
1188         int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1189
1190         ipcm6_init(&ipc6);
1191         ipc6.gso_size = READ_ONCE(up->gso_size);
1192         ipc6.sockc.tsflags = sk->sk_tsflags;
1193
1194         /* destination address check */
1195         if (sin6) {
1196                 if (addr_len < offsetof(struct sockaddr, sa_data))
1197                         return -EINVAL;
1198
1199                 switch (sin6->sin6_family) {
1200                 case AF_INET6:
1201                         if (addr_len < SIN6_LEN_RFC2133)
1202                                 return -EINVAL;
1203                         daddr = &sin6->sin6_addr;
1204                         if (ipv6_addr_any(daddr) &&
1205                             ipv6_addr_v4mapped(&np->saddr))
1206                                 ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
1207                                                        daddr);
1208                         break;
1209                 case AF_INET:
1210                         goto do_udp_sendmsg;
1211                 case AF_UNSPEC:
1212                         msg->msg_name = sin6 = NULL;
1213                         msg->msg_namelen = addr_len = 0;
1214                         daddr = NULL;
1215                         break;
1216                 default:
1217                         return -EINVAL;
1218                 }
1219         } else if (!up->pending) {
1220                 if (sk->sk_state != TCP_ESTABLISHED)
1221                         return -EDESTADDRREQ;
1222                 daddr = &sk->sk_v6_daddr;
1223         } else
1224                 daddr = NULL;
1225
1226         if (daddr) {
1227                 if (ipv6_addr_v4mapped(daddr)) {
1228                         struct sockaddr_in sin;
1229                         sin.sin_family = AF_INET;
1230                         sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1231                         sin.sin_addr.s_addr = daddr->s6_addr32[3];
1232                         msg->msg_name = &sin;
1233                         msg->msg_namelen = sizeof(sin);
1234 do_udp_sendmsg:
1235                         err = __ipv6_only_sock(sk) ?
1236                                 -ENETUNREACH : udp_sendmsg(sk, msg, len);
1237                         msg->msg_name = sin6;
1238                         msg->msg_namelen = addr_len;
1239                         return err;
1240                 }
1241         }
1242
1243         if (up->pending == AF_INET)
1244                 return udp_sendmsg(sk, msg, len);
1245
1246         /* Rough check on arithmetic overflow,
1247            better check is made in ip6_append_data().
1248            */
1249         if (len > INT_MAX - sizeof(struct udphdr))
1250                 return -EMSGSIZE;
1251
1252         getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1253         if (up->pending) {
1254                 /*
1255                  * There are pending frames.
1256                  * The socket lock must be held while it's corked.
1257                  */
1258                 lock_sock(sk);
1259                 if (likely(up->pending)) {
1260                         if (unlikely(up->pending != AF_INET6)) {
1261                                 release_sock(sk);
1262                                 return -EAFNOSUPPORT;
1263                         }
1264                         dst = NULL;
1265                         goto do_append_data;
1266                 }
1267                 release_sock(sk);
1268         }
1269         ulen += sizeof(struct udphdr);
1270
1271         memset(&fl6, 0, sizeof(fl6));
1272
1273         if (sin6) {
1274                 if (sin6->sin6_port == 0)
1275                         return -EINVAL;
1276
1277                 fl6.fl6_dport = sin6->sin6_port;
1278                 daddr = &sin6->sin6_addr;
1279
1280                 if (np->sndflow) {
1281                         fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
1282                         if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
1283                                 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1284                                 if (!flowlabel)
1285                                         return -EINVAL;
1286                         }
1287                 }
1288
1289                 /*
1290                  * Otherwise it will be difficult to maintain
1291                  * sk->sk_dst_cache.
1292                  */
1293                 if (sk->sk_state == TCP_ESTABLISHED &&
1294                     ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1295                         daddr = &sk->sk_v6_daddr;
1296
1297                 if (addr_len >= sizeof(struct sockaddr_in6) &&
1298                     sin6->sin6_scope_id &&
1299                     __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
1300                         fl6.flowi6_oif = sin6->sin6_scope_id;
1301         } else {
1302                 if (sk->sk_state != TCP_ESTABLISHED)
1303                         return -EDESTADDRREQ;
1304
1305                 fl6.fl6_dport = inet->inet_dport;
1306                 daddr = &sk->sk_v6_daddr;
1307                 fl6.flowlabel = np->flow_label;
1308                 connected = true;
1309         }
1310
1311         if (!fl6.flowi6_oif)
1312                 fl6.flowi6_oif = sk->sk_bound_dev_if;
1313
1314         if (!fl6.flowi6_oif)
1315                 fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
1316
1317         fl6.flowi6_mark = sk->sk_mark;
1318         fl6.flowi6_uid = sk->sk_uid;
1319
1320         if (msg->msg_controllen) {
1321                 opt = &opt_space;
1322                 memset(opt, 0, sizeof(struct ipv6_txoptions));
1323                 opt->tot_len = sizeof(*opt);
1324                 ipc6.opt = opt;
1325
1326                 err = udp_cmsg_send(sk, msg, &ipc6.gso_size);
1327                 if (err > 0)
1328                         err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6,
1329                                                     &ipc6);
1330                 if (err < 0) {
1331                         fl6_sock_release(flowlabel);
1332                         return err;
1333                 }
1334                 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
1335                         flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1336                         if (!flowlabel)
1337                                 return -EINVAL;
1338                 }
1339                 if (!(opt->opt_nflen|opt->opt_flen))
1340                         opt = NULL;
1341                 connected = false;
1342         }
1343         if (!opt) {
1344                 opt = txopt_get(np);
1345                 opt_to_free = opt;
1346         }
1347         if (flowlabel)
1348                 opt = fl6_merge_options(&opt_space, flowlabel, opt);
1349         opt = ipv6_fixup_options(&opt_space, opt);
1350         ipc6.opt = opt;
1351
1352         fl6.flowi6_proto = sk->sk_protocol;
1353         fl6.daddr = *daddr;
1354         if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
1355                 fl6.saddr = np->saddr;
1356         fl6.fl6_sport = inet->inet_sport;
1357
1358         if (cgroup_bpf_enabled && !connected) {
1359                 err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk,
1360                                            (struct sockaddr *)sin6, &fl6.saddr);
1361                 if (err)
1362                         goto out_no_dst;
1363                 if (sin6) {
1364                         if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
1365                                 /* BPF program rewrote IPv6-only by IPv4-mapped
1366                                  * IPv6. It's currently unsupported.
1367                                  */
1368                                 err = -ENOTSUPP;
1369                                 goto out_no_dst;
1370                         }
1371                         if (sin6->sin6_port == 0) {
1372                                 /* BPF program set invalid port. Reject it. */
1373                                 err = -EINVAL;
1374                                 goto out_no_dst;
1375                         }
1376                         fl6.fl6_dport = sin6->sin6_port;
1377                         fl6.daddr = sin6->sin6_addr;
1378                 }
1379         }
1380
1381         if (ipv6_addr_any(&fl6.daddr))
1382                 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1383
1384         final_p = fl6_update_dst(&fl6, opt, &final);
1385         if (final_p)
1386                 connected = false;
1387
1388         if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
1389                 fl6.flowi6_oif = np->mcast_oif;
1390                 connected = false;
1391         } else if (!fl6.flowi6_oif)
1392                 fl6.flowi6_oif = np->ucast_oif;
1393
1394         security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1395
1396         if (ipc6.tclass < 0)
1397                 ipc6.tclass = np->tclass;
1398
1399         fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
1400
1401         dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, connected);
1402         if (IS_ERR(dst)) {
1403                 err = PTR_ERR(dst);
1404                 dst = NULL;
1405                 goto out;
1406         }
1407
1408         if (ipc6.hlimit < 0)
1409                 ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
1410
1411         if (msg->msg_flags&MSG_CONFIRM)
1412                 goto do_confirm;
1413 back_from_confirm:
1414
1415         /* Lockless fast path for the non-corking case */
1416         if (!corkreq) {
1417                 struct inet_cork_full cork;
1418                 struct sk_buff *skb;
1419
1420                 skb = ip6_make_skb(sk, getfrag, msg, ulen,
1421                                    sizeof(struct udphdr), &ipc6,
1422                                    &fl6, (struct rt6_info *)dst,
1423                                    msg->msg_flags, &cork);
1424                 err = PTR_ERR(skb);
1425                 if (!IS_ERR_OR_NULL(skb))
1426                         err = udp_v6_send_skb(skb, &fl6, &cork.base);
1427                 goto out;
1428         }
1429
1430         lock_sock(sk);
1431         if (unlikely(up->pending)) {
1432                 /* The socket is already corked while preparing it. */
1433                 /* ... which is an evident application bug. --ANK */
1434                 release_sock(sk);
1435
1436                 net_dbg_ratelimited("udp cork app bug 2\n");
1437                 err = -EINVAL;
1438                 goto out;
1439         }
1440
1441         up->pending = AF_INET6;
1442
1443 do_append_data:
1444         if (ipc6.dontfrag < 0)
1445                 ipc6.dontfrag = np->dontfrag;
1446         up->len += ulen;
1447         err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr),
1448                               &ipc6, &fl6, (struct rt6_info *)dst,
1449                               corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1450         if (err)
1451                 udp_v6_flush_pending_frames(sk);
1452         else if (!corkreq)
1453                 err = udp_v6_push_pending_frames(sk);
1454         else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1455                 up->pending = 0;
1456
1457         if (err > 0)
1458                 err = np->recverr ? net_xmit_errno(err) : 0;
1459         release_sock(sk);
1460
1461 out:
1462         dst_release(dst);
1463 out_no_dst:
1464         fl6_sock_release(flowlabel);
1465         txopt_put(opt_to_free);
1466         if (!err)
1467                 return len;
1468         /*
1469          * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1470          * ENOBUFS might not be good (it's not tunable per se), but otherwise
1471          * we don't have a good statistic (IpOutDiscards but it can be too many
1472          * things).  We could add another new stat but at least for now that
1473          * seems like overkill.
1474          */
1475         if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1476                 UDP6_INC_STATS(sock_net(sk),
1477                                UDP_MIB_SNDBUFERRORS, is_udplite);
1478         }
1479         return err;
1480
1481 do_confirm:
1482         if (msg->msg_flags & MSG_PROBE)
1483                 dst_confirm_neigh(dst, &fl6.daddr);
1484         if (!(msg->msg_flags&MSG_PROBE) || len)
1485                 goto back_from_confirm;
1486         err = 0;
1487         goto out;
1488 }
1489
1490 void udpv6_destroy_sock(struct sock *sk)
1491 {
1492         struct udp_sock *up = udp_sk(sk);
1493         lock_sock(sk);
1494
1495         /* protects from races with udp_abort() */
1496         sock_set_flag(sk, SOCK_DEAD);
1497         udp_v6_flush_pending_frames(sk);
1498         release_sock(sk);
1499
1500         if (static_branch_unlikely(&udpv6_encap_needed_key) && up->encap_type) {
1501                 void (*encap_destroy)(struct sock *sk);
1502                 encap_destroy = READ_ONCE(up->encap_destroy);
1503                 if (encap_destroy)
1504                         encap_destroy(sk);
1505         }
1506 }
1507
1508 /*
1509  *      Socket option code for UDP
1510  */
1511 int udpv6_setsockopt(struct sock *sk, int level, int optname,
1512                      char __user *optval, unsigned int optlen)
1513 {
1514         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1515                 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1516                                           udp_v6_push_pending_frames);
1517         return ipv6_setsockopt(sk, level, optname, optval, optlen);
1518 }
1519
1520 #ifdef CONFIG_COMPAT
1521 int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1522                             char __user *optval, unsigned int optlen)
1523 {
1524         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1525                 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1526                                           udp_v6_push_pending_frames);
1527         return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1528 }
1529 #endif
1530
1531 int udpv6_getsockopt(struct sock *sk, int level, int optname,
1532                      char __user *optval, int __user *optlen)
1533 {
1534         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1535                 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1536         return ipv6_getsockopt(sk, level, optname, optval, optlen);
1537 }
1538
1539 #ifdef CONFIG_COMPAT
1540 int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1541                             char __user *optval, int __user *optlen)
1542 {
1543         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1544                 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1545         return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1546 }
1547 #endif
1548
1549 static const struct inet6_protocol udpv6_protocol = {
1550         .handler        =       udpv6_rcv,
1551         .err_handler    =       udpv6_err,
1552         .flags          =       INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1553 };
1554
1555 /* ------------------------------------------------------------------------ */
1556 #ifdef CONFIG_PROC_FS
1557 int udp6_seq_show(struct seq_file *seq, void *v)
1558 {
1559         if (v == SEQ_START_TOKEN) {
1560                 seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
1561         } else {
1562                 int bucket = ((struct udp_iter_state *)seq->private)->bucket;
1563                 struct inet_sock *inet = inet_sk(v);
1564                 __u16 srcp = ntohs(inet->inet_sport);
1565                 __u16 destp = ntohs(inet->inet_dport);
1566                 __ip6_dgram_sock_seq_show(seq, v, srcp, destp,
1567                                           udp_rqueue_get(v), bucket);
1568         }
1569         return 0;
1570 }
1571
1572 const struct seq_operations udp6_seq_ops = {
1573         .start          = udp_seq_start,
1574         .next           = udp_seq_next,
1575         .stop           = udp_seq_stop,
1576         .show           = udp6_seq_show,
1577 };
1578 EXPORT_SYMBOL(udp6_seq_ops);
1579
1580 static struct udp_seq_afinfo udp6_seq_afinfo = {
1581         .family         = AF_INET6,
1582         .udp_table      = &udp_table,
1583 };
1584
1585 int __net_init udp6_proc_init(struct net *net)
1586 {
1587         if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops,
1588                         sizeof(struct udp_iter_state), &udp6_seq_afinfo))
1589                 return -ENOMEM;
1590         return 0;
1591 }
1592
1593 void udp6_proc_exit(struct net *net)
1594 {
1595         remove_proc_entry("udp6", net->proc_net);
1596 }
1597 #endif /* CONFIG_PROC_FS */
1598
1599 /* ------------------------------------------------------------------------ */
1600
1601 struct proto udpv6_prot = {
1602         .name                   = "UDPv6",
1603         .owner                  = THIS_MODULE,
1604         .close                  = udp_lib_close,
1605         .pre_connect            = udpv6_pre_connect,
1606         .connect                = ip6_datagram_connect,
1607         .disconnect             = udp_disconnect,
1608         .ioctl                  = udp_ioctl,
1609         .init                   = udpv6_init_sock,
1610         .destroy                = udpv6_destroy_sock,
1611         .setsockopt             = udpv6_setsockopt,
1612         .getsockopt             = udpv6_getsockopt,
1613         .sendmsg                = udpv6_sendmsg,
1614         .recvmsg                = udpv6_recvmsg,
1615         .release_cb             = ip6_datagram_release_cb,
1616         .hash                   = udp_lib_hash,
1617         .unhash                 = udp_lib_unhash,
1618         .rehash                 = udp_v6_rehash,
1619         .get_port               = udp_v6_get_port,
1620         .memory_allocated       = &udp_memory_allocated,
1621         .sysctl_mem             = sysctl_udp_mem,
1622         .sysctl_wmem_offset     = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
1623         .sysctl_rmem_offset     = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
1624         .obj_size               = sizeof(struct udp6_sock),
1625         .h.udp_table            = &udp_table,
1626 #ifdef CONFIG_COMPAT
1627         .compat_setsockopt      = compat_udpv6_setsockopt,
1628         .compat_getsockopt      = compat_udpv6_getsockopt,
1629 #endif
1630         .diag_destroy           = udp_abort,
1631 };
1632
1633 static struct inet_protosw udpv6_protosw = {
1634         .type =      SOCK_DGRAM,
1635         .protocol =  IPPROTO_UDP,
1636         .prot =      &udpv6_prot,
1637         .ops =       &inet6_dgram_ops,
1638         .flags =     INET_PROTOSW_PERMANENT,
1639 };
1640
1641 int __init udpv6_init(void)
1642 {
1643         int ret;
1644
1645         ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1646         if (ret)
1647                 goto out;
1648
1649         ret = inet6_register_protosw(&udpv6_protosw);
1650         if (ret)
1651                 goto out_udpv6_protocol;
1652 out:
1653         return ret;
1654
1655 out_udpv6_protocol:
1656         inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1657         goto out;
1658 }
1659
1660 void udpv6_exit(void)
1661 {
1662         inet6_unregister_protosw(&udpv6_protosw);
1663         inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1664 }