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