The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/udp6_usrreq.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    3  * Copyright (c) 2010-2011 Juniper Networks, Inc.
    4  * Copyright (c) 2014 Kevin Lo
    5  * All rights reserved.
    6  *
    7  * Portions of this software were developed by Robert N. M. Watson under
    8  * contract to Juniper Networks, Inc.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 3. Neither the name of the project nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  *      $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
   35  *      $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
   36  */
   37 
   38 /*-
   39  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
   40  *      The Regents of the University of California.
   41  * All rights reserved.
   42  *
   43  * Redistribution and use in source and binary forms, with or without
   44  * modification, are permitted provided that the following conditions
   45  * are met:
   46  * 1. Redistributions of source code must retain the above copyright
   47  *    notice, this list of conditions and the following disclaimer.
   48  * 2. Redistributions in binary form must reproduce the above copyright
   49  *    notice, this list of conditions and the following disclaimer in the
   50  *    documentation and/or other materials provided with the distribution.
   51  * 4. Neither the name of the University nor the names of its contributors
   52  *    may be used to endorse or promote products derived from this software
   53  *    without specific prior written permission.
   54  *
   55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   65  * SUCH DAMAGE.
   66  *
   67  *      @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
   68  */
   69 
   70 #include <sys/cdefs.h>
   71 __FBSDID("$FreeBSD: releng/11.2/sys/netinet6/udp6_usrreq.c 338980 2018-09-27 18:34:42Z gordon $");
   72 
   73 #include "opt_inet.h"
   74 #include "opt_inet6.h"
   75 #include "opt_ipsec.h"
   76 #include "opt_rss.h"
   77 
   78 #include <sys/param.h>
   79 #include <sys/jail.h>
   80 #include <sys/kernel.h>
   81 #include <sys/lock.h>
   82 #include <sys/mbuf.h>
   83 #include <sys/priv.h>
   84 #include <sys/proc.h>
   85 #include <sys/protosw.h>
   86 #include <sys/sdt.h>
   87 #include <sys/signalvar.h>
   88 #include <sys/socket.h>
   89 #include <sys/socketvar.h>
   90 #include <sys/sx.h>
   91 #include <sys/sysctl.h>
   92 #include <sys/syslog.h>
   93 #include <sys/systm.h>
   94 
   95 #include <net/if.h>
   96 #include <net/if_var.h>
   97 #include <net/if_types.h>
   98 #include <net/route.h>
   99 #include <net/rss_config.h>
  100 
  101 #include <netinet/in.h>
  102 #include <netinet/in_kdtrace.h>
  103 #include <netinet/in_pcb.h>
  104 #include <netinet/in_systm.h>
  105 #include <netinet/in_var.h>
  106 #include <netinet/ip.h>
  107 #include <netinet/ip6.h>
  108 #include <netinet/icmp6.h>
  109 #include <netinet/ip_var.h>
  110 #include <netinet/udp.h>
  111 #include <netinet/udp_var.h>
  112 #include <netinet/udplite.h>
  113 
  114 #include <netinet6/ip6protosw.h>
  115 #include <netinet6/ip6_var.h>
  116 #include <netinet6/in6_pcb.h>
  117 #include <netinet6/in6_rss.h>
  118 #include <netinet6/udp6_var.h>
  119 #include <netinet6/scope6_var.h>
  120 
  121 #include <netipsec/ipsec_support.h>
  122 
  123 #include <security/mac/mac_framework.h>
  124 
  125 /*
  126  * UDP protocol implementation.
  127  * Per RFC 768, August, 1980.
  128  */
  129 
  130 extern struct protosw   inetsw[];
  131 static void             udp6_detach(struct socket *so);
  132 
  133 static int
  134 udp6_append(struct inpcb *inp, struct mbuf *n, int off,
  135     struct sockaddr_in6 *fromsa)
  136 {
  137         struct socket *so;
  138         struct mbuf *opts;
  139         struct udpcb *up;
  140 
  141         INP_LOCK_ASSERT(inp);
  142 
  143         /*
  144          * Engage the tunneling protocol.
  145          */
  146         up = intoudpcb(inp);
  147         if (up->u_tun_func != NULL) {
  148                 in_pcbref(inp);
  149                 INP_RUNLOCK(inp);
  150                 (*up->u_tun_func)(n, off, inp, (struct sockaddr *)fromsa,
  151                     up->u_tun_ctx);
  152                 INP_RLOCK(inp);
  153                 return (in_pcbrele_rlocked(inp));
  154         }
  155 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
  156         /* Check AH/ESP integrity. */
  157         if (IPSEC_ENABLED(ipv6)) {
  158                 if (IPSEC_CHECK_POLICY(ipv6, n, inp) != 0) {
  159                         m_freem(n);
  160                         return (0);
  161                 }
  162         }
  163 #endif /* IPSEC */
  164 #ifdef MAC
  165         if (mac_inpcb_check_deliver(inp, n) != 0) {
  166                 m_freem(n);
  167                 return (0);
  168         }
  169 #endif
  170         opts = NULL;
  171         if (inp->inp_flags & INP_CONTROLOPTS ||
  172             inp->inp_socket->so_options & SO_TIMESTAMP)
  173                 ip6_savecontrol(inp, n, &opts);
  174         m_adj(n, off + sizeof(struct udphdr));
  175 
  176         so = inp->inp_socket;
  177         SOCKBUF_LOCK(&so->so_rcv);
  178         if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
  179             opts) == 0) {
  180                 SOCKBUF_UNLOCK(&so->so_rcv);
  181                 m_freem(n);
  182                 if (opts)
  183                         m_freem(opts);
  184                 UDPSTAT_INC(udps_fullsock);
  185         } else
  186                 sorwakeup_locked(so);
  187         return (0);
  188 }
  189 
  190 int
  191 udp6_input(struct mbuf **mp, int *offp, int proto)
  192 {
  193         struct mbuf *m = *mp;
  194         struct ifnet *ifp;
  195         struct ip6_hdr *ip6;
  196         struct udphdr *uh;
  197         struct inpcb *inp;
  198         struct inpcbinfo *pcbinfo;
  199         struct udpcb *up;
  200         int off = *offp;
  201         int cscov_partial;
  202         int plen, ulen;
  203         struct sockaddr_in6 fromsa;
  204         struct m_tag *fwd_tag;
  205         uint16_t uh_sum;
  206         uint8_t nxt;
  207 
  208         ifp = m->m_pkthdr.rcvif;
  209 
  210 #ifndef PULLDOWN_TEST
  211         IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
  212         ip6 = mtod(m, struct ip6_hdr *);
  213         uh = (struct udphdr *)((caddr_t)ip6 + off);
  214 #else
  215         IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
  216         if (!uh)
  217                 return (IPPROTO_DONE);
  218         ip6 = mtod(m, struct ip6_hdr *);
  219 #endif
  220 
  221         UDPSTAT_INC(udps_ipackets);
  222 
  223         /*
  224          * Destination port of 0 is illegal, based on RFC768.
  225          */
  226         if (uh->uh_dport == 0)
  227                 goto badunlocked;
  228 
  229         plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
  230         ulen = ntohs((u_short)uh->uh_ulen);
  231 
  232         nxt = proto;
  233         cscov_partial = (nxt == IPPROTO_UDPLITE) ? 1 : 0;
  234         if (nxt == IPPROTO_UDPLITE) {
  235                 /* Zero means checksum over the complete packet. */
  236                 if (ulen == 0)
  237                         ulen = plen;
  238                 if (ulen == plen)
  239                         cscov_partial = 0;
  240                 if ((ulen < sizeof(struct udphdr)) || (ulen > plen)) {
  241                         /* XXX: What is the right UDPLite MIB counter? */
  242                         goto badunlocked;
  243                 }
  244                 if (uh->uh_sum == 0) {
  245                         /* XXX: What is the right UDPLite MIB counter? */
  246                         goto badunlocked;
  247                 }
  248         } else {
  249                 if ((ulen < sizeof(struct udphdr)) || (plen != ulen)) {
  250                         UDPSTAT_INC(udps_badlen);
  251                         goto badunlocked;
  252                 }
  253                 if (uh->uh_sum == 0) {
  254                         UDPSTAT_INC(udps_nosum);
  255                         goto badunlocked;
  256                 }
  257         }
  258 
  259         if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) &&
  260             !cscov_partial) {
  261                 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
  262                         uh_sum = m->m_pkthdr.csum_data;
  263                 else
  264                         uh_sum = in6_cksum_pseudo(ip6, ulen, nxt,
  265                             m->m_pkthdr.csum_data);
  266                 uh_sum ^= 0xffff;
  267         } else
  268                 uh_sum = in6_cksum_partial(m, nxt, off, plen, ulen);
  269 
  270         if (uh_sum != 0) {
  271                 UDPSTAT_INC(udps_badsum);
  272                 goto badunlocked;
  273         }
  274 
  275         /*
  276          * Construct sockaddr format source address.
  277          */
  278         init_sin6(&fromsa, m);
  279         fromsa.sin6_port = uh->uh_sport;
  280 
  281         pcbinfo = udp_get_inpcbinfo(nxt);
  282         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  283                 struct inpcb *last;
  284                 struct inpcbhead *pcblist;
  285                 struct ip6_moptions *imo;
  286 
  287                 INP_INFO_RLOCK(pcbinfo);
  288                 /*
  289                  * In the event that laddr should be set to the link-local
  290                  * address (this happens in RIPng), the multicast address
  291                  * specified in the received packet will not match laddr.  To
  292                  * handle this situation, matching is relaxed if the
  293                  * receiving interface is the same as one specified in the
  294                  * socket and if the destination multicast address matches
  295                  * one of the multicast groups specified in the socket.
  296                  */
  297 
  298                 /*
  299                  * KAME note: traditionally we dropped udpiphdr from mbuf
  300                  * here.  We need udphdr for IPsec processing so we do that
  301                  * later.
  302                  */
  303                 pcblist = udp_get_pcblist(nxt);
  304                 last = NULL;
  305                 LIST_FOREACH(inp, pcblist, inp_list) {
  306                         if ((inp->inp_vflag & INP_IPV6) == 0)
  307                                 continue;
  308                         if (inp->inp_lport != uh->uh_dport)
  309                                 continue;
  310                         if (inp->inp_fport != 0 &&
  311                             inp->inp_fport != uh->uh_sport)
  312                                 continue;
  313                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
  314                                 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
  315                                                         &ip6->ip6_dst))
  316                                         continue;
  317                         }
  318                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  319                                 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
  320                                                         &ip6->ip6_src) ||
  321                                     inp->inp_fport != uh->uh_sport)
  322                                         continue;
  323                         }
  324 
  325                         /*
  326                          * XXXRW: Because we weren't holding either the inpcb
  327                          * or the hash lock when we checked for a match 
  328                          * before, we should probably recheck now that the 
  329                          * inpcb lock is (supposed to be) held.
  330                          */
  331 
  332                         /*
  333                          * Handle socket delivery policy for any-source
  334                          * and source-specific multicast. [RFC3678]
  335                          */
  336                         imo = inp->in6p_moptions;
  337                         if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  338                                 struct sockaddr_in6      mcaddr;
  339                                 int                      blocked;
  340 
  341                                 INP_RLOCK(inp);
  342 
  343                                 bzero(&mcaddr, sizeof(struct sockaddr_in6));
  344                                 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
  345                                 mcaddr.sin6_family = AF_INET6;
  346                                 mcaddr.sin6_addr = ip6->ip6_dst;
  347 
  348                                 blocked = im6o_mc_filter(imo, ifp,
  349                                         (struct sockaddr *)&mcaddr,
  350                                         (struct sockaddr *)&fromsa);
  351                                 if (blocked != MCAST_PASS) {
  352                                         if (blocked == MCAST_NOTGMEMBER)
  353                                                 IP6STAT_INC(ip6s_notmember);
  354                                         if (blocked == MCAST_NOTSMEMBER ||
  355                                             blocked == MCAST_MUTED)
  356                                                 UDPSTAT_INC(udps_filtermcast);
  357                                         INP_RUNLOCK(inp); /* XXX */
  358                                         continue;
  359                                 }
  360 
  361                                 INP_RUNLOCK(inp);
  362                         }
  363                         if (last != NULL) {
  364                                 struct mbuf *n;
  365 
  366                                 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
  367                                         INP_RLOCK(last);
  368                                         UDP_PROBE(receive, NULL, last, ip6,
  369                                             last, uh);
  370                                         if (udp6_append(last, n, off, &fromsa))
  371                                                 goto inp_lost;
  372                                         INP_RUNLOCK(last);
  373                                 }
  374                         }
  375                         last = inp;
  376                         /*
  377                          * Don't look for additional matches if this one does
  378                          * not have either the SO_REUSEPORT or SO_REUSEADDR
  379                          * socket options set.  This heuristic avoids
  380                          * searching through all pcbs in the common case of a
  381                          * non-shared port.  It assumes that an application
  382                          * will never clear these options after setting them.
  383                          */
  384                         if ((last->inp_socket->so_options &
  385                              (SO_REUSEPORT|SO_REUSEADDR)) == 0)
  386                                 break;
  387                 }
  388 
  389                 if (last == NULL) {
  390                         /*
  391                          * No matching pcb found; discard datagram.  (No need
  392                          * to send an ICMP Port Unreachable for a broadcast
  393                          * or multicast datgram.)
  394                          */
  395                         UDPSTAT_INC(udps_noport);
  396                         UDPSTAT_INC(udps_noportmcast);
  397                         goto badheadlocked;
  398                 }
  399                 INP_RLOCK(last);
  400                 INP_INFO_RUNLOCK(pcbinfo);
  401                 UDP_PROBE(receive, NULL, last, ip6, last, uh);
  402                 if (udp6_append(last, m, off, &fromsa) == 0) 
  403                         INP_RUNLOCK(last);
  404         inp_lost:
  405                 return (IPPROTO_DONE);
  406         }
  407         /*
  408          * Locate pcb for datagram.
  409          */
  410 
  411         /*
  412          * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
  413          */
  414         if ((m->m_flags & M_IP6_NEXTHOP) &&
  415             (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
  416                 struct sockaddr_in6 *next_hop6;
  417 
  418                 next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
  419 
  420                 /*
  421                  * Transparently forwarded. Pretend to be the destination.
  422                  * Already got one like this?
  423                  */
  424                 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
  425                     uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
  426                     INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif, m);
  427                 if (!inp) {
  428                         /*
  429                          * It's new.  Try to find the ambushing socket.
  430                          * Because we've rewritten the destination address,
  431                          * any hardware-generated hash is ignored.
  432                          */
  433                         inp = in6_pcblookup(pcbinfo, &ip6->ip6_src,
  434                             uh->uh_sport, &next_hop6->sin6_addr,
  435                             next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
  436                             uh->uh_dport, INPLOOKUP_WILDCARD |
  437                             INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif);
  438                 }
  439                 /* Remove the tag from the packet. We don't need it anymore. */
  440                 m_tag_delete(m, fwd_tag);
  441                 m->m_flags &= ~M_IP6_NEXTHOP;
  442         } else
  443                 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
  444                     uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
  445                     INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
  446                     m->m_pkthdr.rcvif, m);
  447         if (inp == NULL) {
  448                 if (udp_log_in_vain) {
  449                         char ip6bufs[INET6_ADDRSTRLEN];
  450                         char ip6bufd[INET6_ADDRSTRLEN];
  451 
  452                         log(LOG_INFO,
  453                             "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
  454                             ip6_sprintf(ip6bufd, &ip6->ip6_dst),
  455                             ntohs(uh->uh_dport),
  456                             ip6_sprintf(ip6bufs, &ip6->ip6_src),
  457                             ntohs(uh->uh_sport));
  458                 }
  459                 UDPSTAT_INC(udps_noport);
  460                 if (m->m_flags & M_MCAST) {
  461                         printf("UDP6: M_MCAST is set in a unicast packet.\n");
  462                         UDPSTAT_INC(udps_noportmcast);
  463                         goto badunlocked;
  464                 }
  465                 if (V_udp_blackhole)
  466                         goto badunlocked;
  467                 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
  468                 return (IPPROTO_DONE);
  469         }
  470         INP_RLOCK_ASSERT(inp);
  471         up = intoudpcb(inp);
  472         if (cscov_partial) {
  473                 if (up->u_rxcslen == 0 || up->u_rxcslen > ulen) {
  474                         INP_RUNLOCK(inp);
  475                         m_freem(m);
  476                         return (IPPROTO_DONE);
  477                 }
  478         }
  479         UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
  480         if (udp6_append(inp, m, off, &fromsa) == 0)
  481                 INP_RUNLOCK(inp);
  482         return (IPPROTO_DONE);
  483 
  484 badheadlocked:
  485         INP_INFO_RUNLOCK(pcbinfo);
  486 badunlocked:
  487         if (m)
  488                 m_freem(m);
  489         return (IPPROTO_DONE);
  490 }
  491 
  492 static void
  493 udp6_common_ctlinput(int cmd, struct sockaddr *sa, void *d,
  494     struct inpcbinfo *pcbinfo)
  495 {
  496         struct udphdr uh;
  497         struct ip6_hdr *ip6;
  498         struct mbuf *m;
  499         int off = 0;
  500         struct ip6ctlparam *ip6cp = NULL;
  501         const struct sockaddr_in6 *sa6_src = NULL;
  502         void *cmdarg;
  503         struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
  504         struct udp_portonly {
  505                 u_int16_t uh_sport;
  506                 u_int16_t uh_dport;
  507         } *uhp;
  508 
  509         if (sa->sa_family != AF_INET6 ||
  510             sa->sa_len != sizeof(struct sockaddr_in6))
  511                 return;
  512 
  513         if ((unsigned)cmd >= PRC_NCMDS)
  514                 return;
  515         if (PRC_IS_REDIRECT(cmd))
  516                 notify = in6_rtchange, d = NULL;
  517         else if (cmd == PRC_HOSTDEAD)
  518                 d = NULL;
  519         else if (inet6ctlerrmap[cmd] == 0)
  520                 return;
  521 
  522         /* if the parameter is from icmp6, decode it. */
  523         if (d != NULL) {
  524                 ip6cp = (struct ip6ctlparam *)d;
  525                 m = ip6cp->ip6c_m;
  526                 ip6 = ip6cp->ip6c_ip6;
  527                 off = ip6cp->ip6c_off;
  528                 cmdarg = ip6cp->ip6c_cmdarg;
  529                 sa6_src = ip6cp->ip6c_src;
  530         } else {
  531                 m = NULL;
  532                 ip6 = NULL;
  533                 cmdarg = NULL;
  534                 sa6_src = &sa6_any;
  535         }
  536 
  537         if (ip6) {
  538                 /*
  539                  * XXX: We assume that when IPV6 is non NULL,
  540                  * M and OFF are valid.
  541                  */
  542 
  543                 /* Check if we can safely examine src and dst ports. */
  544                 if (m->m_pkthdr.len < off + sizeof(*uhp))
  545                         return;
  546 
  547                 bzero(&uh, sizeof(uh));
  548                 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
  549 
  550                 if (!PRC_IS_REDIRECT(cmd)) {
  551                         /* Check to see if its tunneled */
  552                         struct inpcb *inp;
  553                         inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_dst,
  554                             uh.uh_dport, &ip6->ip6_src, uh.uh_sport,
  555                             INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
  556                             m->m_pkthdr.rcvif, m);
  557                         if (inp != NULL) {
  558                                 struct udpcb *up;
  559                                 
  560                                 up = intoudpcb(inp);
  561                                 if (up->u_icmp_func) {
  562                                         /* Yes it is. */
  563                                         INP_RUNLOCK(inp);
  564                                         (*up->u_icmp_func)(cmd, (struct sockaddr *)ip6cp->ip6c_src,
  565                                               d, up->u_tun_ctx);
  566                                         return;
  567                                 } else {
  568                                         /* Can't find it. */
  569                                         INP_RUNLOCK(inp);
  570                                 }
  571                         }
  572                 }
  573                 (void)in6_pcbnotify(pcbinfo, sa, uh.uh_dport,
  574                     (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
  575                     cmdarg, notify);
  576         } else
  577                 (void)in6_pcbnotify(pcbinfo, sa, 0,
  578                     (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
  579 }
  580 
  581 void
  582 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
  583 {
  584 
  585         return (udp6_common_ctlinput(cmd, sa, d, &V_udbinfo));
  586 }
  587 
  588 void
  589 udplite6_ctlinput(int cmd, struct sockaddr *sa, void *d)
  590 {
  591 
  592         return (udp6_common_ctlinput(cmd, sa, d, &V_ulitecbinfo));
  593 }
  594 
  595 static int
  596 udp6_getcred(SYSCTL_HANDLER_ARGS)
  597 {
  598         struct xucred xuc;
  599         struct sockaddr_in6 addrs[2];
  600         struct inpcb *inp;
  601         int error;
  602 
  603         error = priv_check(req->td, PRIV_NETINET_GETCRED);
  604         if (error)
  605                 return (error);
  606 
  607         if (req->newlen != sizeof(addrs))
  608                 return (EINVAL);
  609         if (req->oldlen != sizeof(struct xucred))
  610                 return (EINVAL);
  611         error = SYSCTL_IN(req, addrs, sizeof(addrs));
  612         if (error)
  613                 return (error);
  614         if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
  615             (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
  616                 return (error);
  617         }
  618         inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
  619             addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
  620             INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
  621         if (inp != NULL) {
  622                 INP_RLOCK_ASSERT(inp);
  623                 if (inp->inp_socket == NULL)
  624                         error = ENOENT;
  625                 if (error == 0)
  626                         error = cr_canseesocket(req->td->td_ucred,
  627                             inp->inp_socket);
  628                 if (error == 0)
  629                         cru2x(inp->inp_cred, &xuc);
  630                 INP_RUNLOCK(inp);
  631         } else
  632                 error = ENOENT;
  633         if (error == 0)
  634                 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
  635         return (error);
  636 }
  637 
  638 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
  639     0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
  640 
  641 static int
  642 udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
  643     struct mbuf *control, struct thread *td)
  644 {
  645         u_int32_t ulen = m->m_pkthdr.len;
  646         u_int32_t plen = sizeof(struct udphdr) + ulen;
  647         struct ip6_hdr *ip6;
  648         struct udphdr *udp6;
  649         struct in6_addr *laddr, *faddr, in6a;
  650         struct sockaddr_in6 *sin6 = NULL;
  651         int cscov_partial = 0;
  652         int scope_ambiguous = 0;
  653         u_short fport;
  654         int error = 0;
  655         uint8_t nxt;
  656         uint16_t cscov = 0;
  657         struct ip6_pktopts *optp, opt;
  658         int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
  659         int flags;
  660         struct sockaddr_in6 tmp;
  661 
  662         INP_WLOCK_ASSERT(inp);
  663         INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
  664 
  665         if (addr6) {
  666                 /* addr6 has been validated in udp6_send(). */
  667                 sin6 = (struct sockaddr_in6 *)addr6;
  668 
  669                 /* protect *sin6 from overwrites */
  670                 tmp = *sin6;
  671                 sin6 = &tmp;
  672 
  673                 /*
  674                  * Application should provide a proper zone ID or the use of
  675                  * default zone IDs should be enabled.  Unfortunately, some
  676                  * applications do not behave as it should, so we need a
  677                  * workaround.  Even if an appropriate ID is not determined,
  678                  * we'll see if we can determine the outgoing interface.  If we
  679                  * can, determine the zone ID based on the interface below.
  680                  */
  681                 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
  682                         scope_ambiguous = 1;
  683                 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
  684                         return (error);
  685         }
  686 
  687         nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
  688             IPPROTO_UDP : IPPROTO_UDPLITE;
  689         if (control) {
  690                 if ((error = ip6_setpktopts(control, &opt,
  691                     inp->in6p_outputopts, td->td_ucred, nxt)) != 0)
  692                         goto release;
  693                 optp = &opt;
  694         } else
  695                 optp = inp->in6p_outputopts;
  696 
  697         if (sin6) {
  698                 faddr = &sin6->sin6_addr;
  699 
  700                 /*
  701                  * Since we saw no essential reason for calling in_pcbconnect,
  702                  * we get rid of such kind of logic, and call in6_selectsrc
  703                  * and in6_pcbsetport in order to fill in the local address
  704                  * and the local port.
  705                  */
  706                 if (sin6->sin6_port == 0) {
  707                         error = EADDRNOTAVAIL;
  708                         goto release;
  709                 }
  710 
  711                 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  712                         /* how about ::ffff:0.0.0.0 case? */
  713                         error = EISCONN;
  714                         goto release;
  715                 }
  716 
  717                 fport = sin6->sin6_port; /* allow 0 port */
  718 
  719                 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
  720                         if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
  721                                 /*
  722                                  * I believe we should explicitly discard the
  723                                  * packet when mapped addresses are disabled,
  724                                  * rather than send the packet as an IPv6 one.
  725                                  * If we chose the latter approach, the packet
  726                                  * might be sent out on the wire based on the
  727                                  * default route, the situation which we'd
  728                                  * probably want to avoid.
  729                                  * (20010421 jinmei@kame.net)
  730                                  */
  731                                 error = EINVAL;
  732                                 goto release;
  733                         }
  734                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
  735                             !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
  736                                 /*
  737                                  * when remote addr is an IPv4-mapped address,
  738                                  * local addr should not be an IPv6 address,
  739                                  * since you cannot determine how to map IPv6
  740                                  * source address to IPv4.
  741                                  */
  742                                 error = EINVAL;
  743                                 goto release;
  744                         }
  745 
  746                         af = AF_INET;
  747                 }
  748 
  749                 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
  750                         error = in6_selectsrc_socket(sin6, optp, inp,
  751                             td->td_ucred, scope_ambiguous, &in6a, NULL);
  752                         if (error)
  753                                 goto release;
  754                         laddr = &in6a;
  755                 } else
  756                         laddr = &inp->in6p_laddr;       /* XXX */
  757                 if (laddr == NULL) {
  758                         if (error == 0)
  759                                 error = EADDRNOTAVAIL;
  760                         goto release;
  761                 }
  762                 if (inp->inp_lport == 0 &&
  763                     (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0) {
  764                         /* Undo an address bind that may have occurred. */
  765                         inp->in6p_laddr = in6addr_any;
  766                         goto release;
  767                 }
  768         } else {
  769                 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  770                         error = ENOTCONN;
  771                         goto release;
  772                 }
  773                 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
  774                         if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
  775                                 /*
  776                                  * XXX: this case would happen when the
  777                                  * application sets the V6ONLY flag after
  778                                  * connecting the foreign address.
  779                                  * Such applications should be fixed,
  780                                  * so we bark here.
  781                                  */
  782                                 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
  783                                     "option was set for a connected socket\n");
  784                                 error = EINVAL;
  785                                 goto release;
  786                         } else
  787                                 af = AF_INET;
  788                 }
  789                 laddr = &inp->in6p_laddr;
  790                 faddr = &inp->in6p_faddr;
  791                 fport = inp->inp_fport;
  792         }
  793 
  794         if (af == AF_INET)
  795                 hlen = sizeof(struct ip);
  796 
  797         /*
  798          * Calculate data length and get a mbuf
  799          * for UDP and IP6 headers.
  800          */
  801         M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT);
  802         if (m == NULL) {
  803                 error = ENOBUFS;
  804                 goto release;
  805         }
  806 
  807         /*
  808          * Stuff checksum and output datagram.
  809          */
  810         udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
  811         udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
  812         udp6->uh_dport = fport;
  813         if (nxt == IPPROTO_UDPLITE) {
  814                 struct udpcb *up;
  815 
  816                 up = intoudpcb(inp);
  817                 cscov = up->u_txcslen;
  818                 if (cscov >= plen)
  819                         cscov = 0;
  820                 udp6->uh_ulen = htons(cscov);
  821                 /*
  822                  * For UDP-Lite, checksum coverage length of zero means
  823                  * the entire UDPLite packet is covered by the checksum.
  824                  */
  825                 cscov_partial = (cscov == 0) ? 0 : 1;
  826         } else if (plen <= 0xffff)
  827                 udp6->uh_ulen = htons((u_short)plen);
  828         else
  829                 udp6->uh_ulen = 0;
  830         udp6->uh_sum = 0;
  831 
  832         switch (af) {
  833         case AF_INET6:
  834                 ip6 = mtod(m, struct ip6_hdr *);
  835                 ip6->ip6_flow   = inp->inp_flow & IPV6_FLOWINFO_MASK;
  836                 ip6->ip6_vfc    &= ~IPV6_VERSION_MASK;
  837                 ip6->ip6_vfc    |= IPV6_VERSION;
  838                 ip6->ip6_plen   = htons((u_short)plen);
  839                 ip6->ip6_nxt    = nxt;
  840                 ip6->ip6_hlim   = in6_selecthlim(inp, NULL);
  841                 ip6->ip6_src    = *laddr;
  842                 ip6->ip6_dst    = *faddr;
  843 
  844                 if (cscov_partial) {
  845                         if ((udp6->uh_sum = in6_cksum_partial(m, nxt,
  846                             sizeof(struct ip6_hdr), plen, cscov)) == 0)
  847                                 udp6->uh_sum = 0xffff;
  848                 } else {
  849                         udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
  850                         m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
  851                         m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
  852                 }
  853 
  854 #ifdef  RSS
  855                 {
  856                         uint32_t hash_val, hash_type;
  857                         uint8_t pr;
  858 
  859                         pr = inp->inp_socket->so_proto->pr_protocol;
  860                         /*
  861                          * Calculate an appropriate RSS hash for UDP and
  862                          * UDP Lite.
  863                          *
  864                          * The called function will take care of figuring out
  865                          * whether a 2-tuple or 4-tuple hash is required based
  866                          * on the currently configured scheme.
  867                          *
  868                          * Later later on connected socket values should be
  869                          * cached in the inpcb and reused, rather than constantly
  870                          * re-calculating it.
  871                          *
  872                          * UDP Lite is a different protocol number and will
  873                          * likely end up being hashed as a 2-tuple until
  874                          * RSS / NICs grow UDP Lite protocol awareness.
  875                          */
  876                         if (rss_proto_software_hash_v6(faddr, laddr, fport,
  877                             inp->inp_lport, pr, &hash_val, &hash_type) == 0) {
  878                                 m->m_pkthdr.flowid = hash_val;
  879                                 M_HASHTYPE_SET(m, hash_type);
  880                         }
  881                 }
  882 #endif
  883                 flags = 0;
  884 #ifdef  RSS
  885                 /*
  886                  * Don't override with the inp cached flowid.
  887                  *
  888                  * Until the whole UDP path is vetted, it may actually
  889                  * be incorrect.
  890                  */
  891                 flags |= IP_NODEFAULTFLOWID;
  892 #endif
  893 
  894                 UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
  895                 UDPSTAT_INC(udps_opackets);
  896                 error = ip6_output(m, optp, &inp->inp_route6, flags,
  897                     inp->in6p_moptions, NULL, inp);
  898                 break;
  899         case AF_INET:
  900                 error = EAFNOSUPPORT;
  901                 goto release;
  902         }
  903         goto releaseopt;
  904 
  905 release:
  906         m_freem(m);
  907 
  908 releaseopt:
  909         if (control) {
  910                 ip6_clearpktopts(&opt, -1);
  911                 m_freem(control);
  912         }
  913         return (error);
  914 }
  915 
  916 static void
  917 udp6_abort(struct socket *so)
  918 {
  919         struct inpcb *inp;
  920         struct inpcbinfo *pcbinfo;
  921 
  922         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
  923         inp = sotoinpcb(so);
  924         KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
  925 
  926         INP_WLOCK(inp);
  927 #ifdef INET
  928         if (inp->inp_vflag & INP_IPV4) {
  929                 struct pr_usrreqs *pru;
  930                 uint8_t nxt;
  931 
  932                 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
  933                     IPPROTO_UDP : IPPROTO_UDPLITE;
  934                 INP_WUNLOCK(inp);
  935                 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
  936                 (*pru->pru_abort)(so);
  937                 return;
  938         }
  939 #endif
  940 
  941         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  942                 INP_HASH_WLOCK(pcbinfo);
  943                 in6_pcbdisconnect(inp);
  944                 inp->in6p_laddr = in6addr_any;
  945                 INP_HASH_WUNLOCK(pcbinfo);
  946                 soisdisconnected(so);
  947         }
  948         INP_WUNLOCK(inp);
  949 }
  950 
  951 static int
  952 udp6_attach(struct socket *so, int proto, struct thread *td)
  953 {
  954         struct inpcb *inp;
  955         struct inpcbinfo *pcbinfo;
  956         int error;
  957 
  958         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
  959         inp = sotoinpcb(so);
  960         KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
  961 
  962         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
  963                 error = soreserve(so, udp_sendspace, udp_recvspace);
  964                 if (error)
  965                         return (error);
  966         }
  967         INP_INFO_WLOCK(pcbinfo);
  968         error = in_pcballoc(so, pcbinfo);
  969         if (error) {
  970                 INP_INFO_WUNLOCK(pcbinfo);
  971                 return (error);
  972         }
  973         inp = (struct inpcb *)so->so_pcb;
  974         inp->inp_vflag |= INP_IPV6;
  975         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
  976                 inp->inp_vflag |= INP_IPV4;
  977         inp->in6p_hops = -1;    /* use kernel default */
  978         inp->in6p_cksum = -1;   /* just to be sure */
  979         /*
  980          * XXX: ugly!!
  981          * IPv4 TTL initialization is necessary for an IPv6 socket as well,
  982          * because the socket may be bound to an IPv6 wildcard address,
  983          * which may match an IPv4-mapped IPv6 address.
  984          */
  985         inp->inp_ip_ttl = V_ip_defttl;
  986 
  987         error = udp_newudpcb(inp);
  988         if (error) {
  989                 in_pcbdetach(inp);
  990                 in_pcbfree(inp);
  991                 INP_INFO_WUNLOCK(pcbinfo);
  992                 return (error);
  993         }
  994         INP_WUNLOCK(inp);
  995         INP_INFO_WUNLOCK(pcbinfo);
  996         return (0);
  997 }
  998 
  999 static int
 1000 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
 1001 {
 1002         struct inpcb *inp;
 1003         struct inpcbinfo *pcbinfo;
 1004         int error;
 1005         u_char vflagsav;
 1006 
 1007         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
 1008         inp = sotoinpcb(so);
 1009         KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
 1010 
 1011         INP_WLOCK(inp);
 1012         INP_HASH_WLOCK(pcbinfo);
 1013         vflagsav = inp->inp_vflag;
 1014         inp->inp_vflag &= ~INP_IPV4;
 1015         inp->inp_vflag |= INP_IPV6;
 1016         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
 1017                 struct sockaddr_in6 *sin6_p;
 1018 
 1019                 sin6_p = (struct sockaddr_in6 *)nam;
 1020 
 1021                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
 1022                         inp->inp_vflag |= INP_IPV4;
 1023 #ifdef INET
 1024                 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
 1025                         struct sockaddr_in sin;
 1026 
 1027                         in6_sin6_2_sin(&sin, sin6_p);
 1028                         inp->inp_vflag |= INP_IPV4;
 1029                         inp->inp_vflag &= ~INP_IPV6;
 1030                         error = in_pcbbind(inp, (struct sockaddr *)&sin,
 1031                             td->td_ucred);
 1032                         goto out;
 1033                 }
 1034 #endif
 1035         }
 1036 
 1037         error = in6_pcbbind(inp, nam, td->td_ucred);
 1038 #ifdef INET
 1039 out:
 1040 #endif
 1041         if (error != 0)
 1042                 inp->inp_vflag = vflagsav;
 1043         INP_HASH_WUNLOCK(pcbinfo);
 1044         INP_WUNLOCK(inp);
 1045         return (error);
 1046 }
 1047 
 1048 static void
 1049 udp6_close(struct socket *so)
 1050 {
 1051         struct inpcb *inp;
 1052         struct inpcbinfo *pcbinfo;
 1053 
 1054         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
 1055         inp = sotoinpcb(so);
 1056         KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
 1057 
 1058         INP_WLOCK(inp);
 1059 #ifdef INET
 1060         if (inp->inp_vflag & INP_IPV4) {
 1061                 struct pr_usrreqs *pru;
 1062                 uint8_t nxt;
 1063 
 1064                 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
 1065                     IPPROTO_UDP : IPPROTO_UDPLITE;
 1066                 INP_WUNLOCK(inp);
 1067                 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
 1068                 (*pru->pru_disconnect)(so);
 1069                 return;
 1070         }
 1071 #endif
 1072         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
 1073                 INP_HASH_WLOCK(pcbinfo);
 1074                 in6_pcbdisconnect(inp);
 1075                 inp->in6p_laddr = in6addr_any;
 1076                 INP_HASH_WUNLOCK(pcbinfo);
 1077                 soisdisconnected(so);
 1078         }
 1079         INP_WUNLOCK(inp);
 1080 }
 1081 
 1082 static int
 1083 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
 1084 {
 1085         struct inpcb *inp;
 1086         struct inpcbinfo *pcbinfo;
 1087         struct sockaddr_in6 *sin6;
 1088         int error;
 1089         u_char vflagsav;
 1090 
 1091         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
 1092         inp = sotoinpcb(so);
 1093         sin6 = (struct sockaddr_in6 *)nam;
 1094         KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
 1095 
 1096         /*
 1097          * XXXRW: Need to clarify locking of v4/v6 flags.
 1098          */
 1099         INP_WLOCK(inp);
 1100 #ifdef INET
 1101         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
 1102                 struct sockaddr_in sin;
 1103 
 1104                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
 1105                         error = EINVAL;
 1106                         goto out;
 1107                 }
 1108                 if ((inp->inp_vflag & INP_IPV4) == 0) {
 1109                         error = EAFNOSUPPORT;
 1110                         goto out;
 1111                 }
 1112                 if (inp->inp_faddr.s_addr != INADDR_ANY) {
 1113                         error = EISCONN;
 1114                         goto out;
 1115                 }
 1116                 in6_sin6_2_sin(&sin, sin6);
 1117                 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
 1118                 if (error != 0)
 1119                         goto out;
 1120                 vflagsav = inp->inp_vflag;
 1121                 inp->inp_vflag |= INP_IPV4;
 1122                 inp->inp_vflag &= ~INP_IPV6;
 1123                 INP_HASH_WLOCK(pcbinfo);
 1124                 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
 1125                     td->td_ucred);
 1126                 INP_HASH_WUNLOCK(pcbinfo);
 1127                 /*
 1128                  * If connect succeeds, mark socket as connected. If
 1129                  * connect fails and socket is unbound, reset inp_vflag
 1130                  * field.
 1131                  */
 1132                 if (error == 0)
 1133                         soisconnected(so);
 1134                 else if (inp->inp_laddr.s_addr == INADDR_ANY &&
 1135                     inp->inp_lport == 0)
 1136                         inp->inp_vflag = vflagsav;
 1137                 goto out;
 1138         } else {
 1139                 if ((inp->inp_vflag & INP_IPV6) == 0) {
 1140                         error = EAFNOSUPPORT;
 1141                         goto out;
 1142                 }
 1143         }
 1144 #endif
 1145         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
 1146                 error = EISCONN;
 1147                 goto out;
 1148         }
 1149         error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
 1150         if (error != 0)
 1151                 goto out;
 1152         vflagsav = inp->inp_vflag;
 1153         inp->inp_vflag &= ~INP_IPV4;
 1154         inp->inp_vflag |= INP_IPV6;
 1155         INP_HASH_WLOCK(pcbinfo);
 1156         error = in6_pcbconnect(inp, nam, td->td_ucred);
 1157         INP_HASH_WUNLOCK(pcbinfo);
 1158         /*
 1159          * If connect succeeds, mark socket as connected. If
 1160          * connect fails and socket is unbound, reset inp_vflag
 1161          * field.
 1162          */
 1163         if (error == 0)
 1164                 soisconnected(so);
 1165         else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
 1166             inp->inp_lport == 0)
 1167                 inp->inp_vflag = vflagsav;
 1168 out:
 1169         INP_WUNLOCK(inp);
 1170         return (error);
 1171 }
 1172 
 1173 static void
 1174 udp6_detach(struct socket *so)
 1175 {
 1176         struct inpcb *inp;
 1177         struct inpcbinfo *pcbinfo;
 1178         struct udpcb *up;
 1179 
 1180         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
 1181         inp = sotoinpcb(so);
 1182         KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
 1183 
 1184         INP_INFO_WLOCK(pcbinfo);
 1185         INP_WLOCK(inp);
 1186         up = intoudpcb(inp);
 1187         KASSERT(up != NULL, ("%s: up == NULL", __func__));
 1188         in_pcbdetach(inp);
 1189         in_pcbfree(inp);
 1190         INP_INFO_WUNLOCK(pcbinfo);
 1191         udp_discardcb(up);
 1192 }
 1193 
 1194 static int
 1195 udp6_disconnect(struct socket *so)
 1196 {
 1197         struct inpcb *inp;
 1198         struct inpcbinfo *pcbinfo;
 1199         int error;
 1200 
 1201         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
 1202         inp = sotoinpcb(so);
 1203         KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
 1204 
 1205         INP_WLOCK(inp);
 1206 #ifdef INET
 1207         if (inp->inp_vflag & INP_IPV4) {
 1208                 struct pr_usrreqs *pru;
 1209                 uint8_t nxt;
 1210 
 1211                 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
 1212                     IPPROTO_UDP : IPPROTO_UDPLITE;
 1213                 INP_WUNLOCK(inp);
 1214                 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
 1215                 (void)(*pru->pru_disconnect)(so);
 1216                 return (0);
 1217         }
 1218 #endif
 1219 
 1220         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
 1221                 error = ENOTCONN;
 1222                 goto out;
 1223         }
 1224 
 1225         INP_HASH_WLOCK(pcbinfo);
 1226         in6_pcbdisconnect(inp);
 1227         inp->in6p_laddr = in6addr_any;
 1228         INP_HASH_WUNLOCK(pcbinfo);
 1229         SOCK_LOCK(so);
 1230         so->so_state &= ~SS_ISCONNECTED;                /* XXX */
 1231         SOCK_UNLOCK(so);
 1232 out:
 1233         INP_WUNLOCK(inp);
 1234         return (0);
 1235 }
 1236 
 1237 static int
 1238 udp6_send(struct socket *so, int flags, struct mbuf *m,
 1239     struct sockaddr *addr, struct mbuf *control, struct thread *td)
 1240 {
 1241         struct inpcb *inp;
 1242         struct inpcbinfo *pcbinfo;
 1243         int error = 0;
 1244 
 1245         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
 1246         inp = sotoinpcb(so);
 1247         KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
 1248 
 1249         INP_WLOCK(inp);
 1250         if (addr) {
 1251                 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
 1252                         error = EINVAL;
 1253                         goto bad;
 1254                 }
 1255                 if (addr->sa_family != AF_INET6) {
 1256                         error = EAFNOSUPPORT;
 1257                         goto bad;
 1258                 }
 1259         }
 1260 
 1261 #ifdef INET
 1262         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
 1263                 int hasv4addr;
 1264                 struct sockaddr_in6 *sin6 = NULL;
 1265 
 1266                 if (addr == NULL)
 1267                         hasv4addr = (inp->inp_vflag & INP_IPV4);
 1268                 else {
 1269                         sin6 = (struct sockaddr_in6 *)addr;
 1270                         hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
 1271                             ? 1 : 0;
 1272                 }
 1273                 if (hasv4addr) {
 1274                         struct pr_usrreqs *pru;
 1275                         uint8_t nxt;
 1276 
 1277                         nxt = (inp->inp_socket->so_proto->pr_protocol ==
 1278                             IPPROTO_UDP) ? IPPROTO_UDP : IPPROTO_UDPLITE;
 1279                         /*
 1280                          * XXXRW: We release UDP-layer locks before calling
 1281                          * udp_send() in order to avoid recursion.  However,
 1282                          * this does mean there is a short window where inp's
 1283                          * fields are unstable.  Could this lead to a
 1284                          * potential race in which the factors causing us to
 1285                          * select the UDPv4 output routine are invalidated?
 1286                          */
 1287                         INP_WUNLOCK(inp);
 1288                         if (sin6)
 1289                                 in6_sin6_2_sin_in_sock(addr);
 1290                         pru = inetsw[ip_protox[nxt]].pr_usrreqs;
 1291                         /* addr will just be freed in sendit(). */
 1292                         return ((*pru->pru_send)(so, flags, m, addr, control,
 1293                             td));
 1294                 }
 1295         }
 1296 #endif
 1297 #ifdef MAC
 1298         mac_inpcb_create_mbuf(inp, m);
 1299 #endif
 1300         INP_HASH_WLOCK(pcbinfo);
 1301         error = udp6_output(inp, m, addr, control, td);
 1302         INP_HASH_WUNLOCK(pcbinfo);
 1303         INP_WUNLOCK(inp);
 1304         return (error);
 1305 
 1306 bad:
 1307         INP_WUNLOCK(inp);
 1308         m_freem(m);
 1309         return (error);
 1310 }
 1311 
 1312 struct pr_usrreqs udp6_usrreqs = {
 1313         .pru_abort =            udp6_abort,
 1314         .pru_attach =           udp6_attach,
 1315         .pru_bind =             udp6_bind,
 1316         .pru_connect =          udp6_connect,
 1317         .pru_control =          in6_control,
 1318         .pru_detach =           udp6_detach,
 1319         .pru_disconnect =       udp6_disconnect,
 1320         .pru_peeraddr =         in6_mapped_peeraddr,
 1321         .pru_send =             udp6_send,
 1322         .pru_shutdown =         udp_shutdown,
 1323         .pru_sockaddr =         in6_mapped_sockaddr,
 1324         .pru_soreceive =        soreceive_dgram,
 1325         .pru_sosend =           sosend_dgram,
 1326         .pru_sosetlabel =       in_pcbsosetlabel,
 1327         .pru_close =            udp6_close
 1328 };

Cache object: 52066060129daf796aa41932b8fdc667


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.