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