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  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. Neither the name of the project nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
   30  *      $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
   31  */
   32 
   33 /*-
   34  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
   35  *      The Regents of the University of California.
   36  * All rights reserved.
   37  *
   38  * Redistribution and use in source and binary forms, with or without
   39  * modification, are permitted provided that the following conditions
   40  * are met:
   41  * 1. Redistributions of source code must retain the above copyright
   42  *    notice, this list of conditions and the following disclaimer.
   43  * 2. Redistributions in binary form must reproduce the above copyright
   44  *    notice, this list of conditions and the following disclaimer in the
   45  *    documentation and/or other materials provided with the distribution.
   46  * 4. Neither the name of the University nor the names of its contributors
   47  *    may be used to endorse or promote products derived from this software
   48  *    without specific prior written permission.
   49  *
   50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   60  * SUCH DAMAGE.
   61  *
   62  *      @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
   63  */
   64 
   65 #include <sys/cdefs.h>
   66 __FBSDID("$FreeBSD: releng/7.3/sys/netinet6/udp6_usrreq.c 202914 2010-01-24 10:08:54Z bz $");
   67 
   68 #include "opt_inet.h"
   69 #include "opt_inet6.h"
   70 #include "opt_ipsec.h"
   71 #include "opt_mac.h"
   72 
   73 #include <sys/param.h>
   74 #include <sys/jail.h>
   75 #include <sys/kernel.h>
   76 #include <sys/lock.h>
   77 #include <sys/mbuf.h>
   78 #include <sys/priv.h>
   79 #include <sys/proc.h>
   80 #include <sys/protosw.h>
   81 #include <sys/signalvar.h>
   82 #include <sys/socket.h>
   83 #include <sys/socketvar.h>
   84 #include <sys/sx.h>
   85 #include <sys/sysctl.h>
   86 #include <sys/syslog.h>
   87 #include <sys/systm.h>
   88 
   89 #include <net/if.h>
   90 #include <net/if_types.h>
   91 #include <net/route.h>
   92 
   93 #include <netinet/in.h>
   94 #include <netinet/in_pcb.h>
   95 #include <netinet/in_systm.h>
   96 #include <netinet/in_var.h>
   97 #include <netinet/ip.h>
   98 #include <netinet/ip_icmp.h>
   99 #include <netinet/ip6.h>
  100 #include <netinet/icmp_var.h>
  101 #include <netinet/icmp6.h>
  102 #include <netinet/ip_var.h>
  103 #include <netinet/udp.h>
  104 #include <netinet/udp_var.h>
  105 #include <netinet6/ip6protosw.h>
  106 #include <netinet6/ip6_var.h>
  107 #include <netinet6/in6_pcb.h>
  108 #include <netinet6/udp6_var.h>
  109 #include <netinet6/scope6_var.h>
  110 
  111 #ifdef IPSEC
  112 #include <netipsec/ipsec.h>
  113 #include <netipsec/ipsec6.h>
  114 #endif /* IPSEC */
  115 
  116 #include <security/mac/mac_framework.h>
  117 
  118 /*
  119  * UDP protocol implementation.
  120  * Per RFC 768, August, 1980.
  121  */
  122 
  123 extern struct protosw   inetsw[];
  124 static void             udp6_detach(struct socket *so);
  125 
  126 static void
  127 udp6_append(struct inpcb *inp, struct mbuf *n, int off,
  128     struct sockaddr_in6 *fromsa)
  129 {
  130         struct socket *so;
  131         struct mbuf *opts;
  132 
  133         INP_LOCK_ASSERT(inp);
  134 
  135 #ifdef IPSEC
  136         /* Check AH/ESP integrity. */
  137         if (ipsec6_in_reject(n, inp)) {
  138                 m_freem(n);
  139                 ipsec6stat.in_polvio++;
  140                 return;
  141         }
  142 #endif /* IPSEC */
  143 #ifdef MAC
  144         if (mac_check_inpcb_deliver(inp, n) != 0) {
  145                 m_freem(n);
  146                 return;
  147         }
  148 #endif
  149         opts = NULL;
  150         if (inp->inp_flags & INP_CONTROLOPTS ||
  151             inp->inp_socket->so_options & SO_TIMESTAMP)
  152                 ip6_savecontrol(inp, n, &opts);
  153         m_adj(n, off + sizeof(struct udphdr));
  154 
  155         so = inp->inp_socket;
  156         SOCKBUF_LOCK(&so->so_rcv);
  157         if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
  158             opts) == 0) {
  159                 SOCKBUF_UNLOCK(&so->so_rcv);
  160                 m_freem(n);
  161                 if (opts)
  162                         m_freem(opts);
  163                 udpstat.udps_fullsock++;
  164         } else
  165                 sorwakeup_locked(so);
  166 }
  167 
  168 int
  169 udp6_input(struct mbuf **mp, int *offp, int proto)
  170 {
  171         struct mbuf *m = *mp;
  172         struct ip6_hdr *ip6;
  173         struct udphdr *uh;
  174         struct inpcb *inp;
  175         int off = *offp;
  176         int plen, ulen;
  177         struct sockaddr_in6 fromsa;
  178 
  179         ip6 = mtod(m, struct ip6_hdr *);
  180 
  181         if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
  182                 /* XXX send icmp6 host/port unreach? */
  183                 m_freem(m);
  184                 return (IPPROTO_DONE);
  185         }
  186 
  187 #ifndef PULLDOWN_TEST
  188         IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
  189         ip6 = mtod(m, struct ip6_hdr *);
  190         uh = (struct udphdr *)((caddr_t)ip6 + off);
  191 #else
  192         IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
  193         if (!uh)
  194                 return (IPPROTO_DONE);
  195 #endif
  196 
  197         udpstat.udps_ipackets++;
  198 
  199         /*
  200          * Destination port of 0 is illegal, based on RFC768.
  201          */
  202         if (uh->uh_dport == 0)
  203                 goto badunlocked;
  204 
  205         plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
  206         ulen = ntohs((u_short)uh->uh_ulen);
  207 
  208         if (plen != ulen) {
  209                 udpstat.udps_badlen++;
  210                 goto badunlocked;
  211         }
  212 
  213         /*
  214          * Checksum extended UDP header and data.
  215          */
  216         if (uh->uh_sum == 0) {
  217                 udpstat.udps_nosum++;
  218                 goto badunlocked;
  219         }
  220         if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
  221                 udpstat.udps_badsum++;
  222                 goto badunlocked;
  223         }
  224 
  225         /*
  226          * Construct sockaddr format source address.
  227          */
  228         init_sin6(&fromsa, m);
  229         fromsa.sin6_port = uh->uh_sport;
  230 
  231         INP_INFO_RLOCK(&udbinfo);
  232         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  233                 struct inpcb *last;
  234 
  235                 /*
  236                  * In the event that laddr should be set to the link-local
  237                  * address (this happens in RIPng), the multicast address
  238                  * specified in the received packet will not match laddr.  To
  239                  * handle this situation, matching is relaxed if the
  240                  * receiving interface is the same as one specified in the
  241                  * socket and if the destination multicast address matches
  242                  * one of the multicast groups specified in the socket.
  243                  */
  244 
  245                 /*
  246                  * KAME note: traditionally we dropped udpiphdr from mbuf
  247                  * here.  We need udphdr for IPsec processing so we do that
  248                  * later.
  249                  */
  250                 last = NULL;
  251                 LIST_FOREACH(inp, &udb, inp_list) {
  252                         if ((inp->inp_vflag & INP_IPV6) == 0)
  253                                 continue;
  254                         if (inp->inp_lport != uh->uh_dport)
  255                                 continue;
  256                         /*
  257                          * XXX: Do not check source port of incoming datagram
  258                          * unless inp_connect() has been called to bind the
  259                          * fport part of the 4-tuple; the source could be
  260                          * trying to talk to us with an ephemeral port.
  261                          */
  262                         if (inp->inp_fport != 0 &&
  263                             inp->inp_fport != uh->uh_sport)
  264                                 continue;
  265                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
  266                                 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
  267                                                         &ip6->ip6_dst))
  268                                         continue;
  269                         }
  270                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  271                                 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
  272                                                         &ip6->ip6_src) ||
  273                                     inp->inp_fport != uh->uh_sport)
  274                                         continue;
  275                         }
  276 
  277                         if (last != NULL) {
  278                                 struct mbuf *n;
  279 
  280                                 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
  281                                         INP_RLOCK(last);
  282                                         udp6_append(last, n, off, &fromsa);
  283                                         INP_RUNLOCK(last);
  284                                 }
  285                         }
  286                         last = inp;
  287                         /*
  288                          * Don't look for additional matches if this one does
  289                          * not have either the SO_REUSEPORT or SO_REUSEADDR
  290                          * socket options set.  This heuristic avoids
  291                          * searching through all pcbs in the common case of a
  292                          * non-shared port.  It assumes that an application
  293                          * will never clear these options after setting them.
  294                          */
  295                         if ((last->inp_socket->so_options &
  296                              (SO_REUSEPORT|SO_REUSEADDR)) == 0)
  297                                 break;
  298                 }
  299 
  300                 if (last == NULL) {
  301                         /*
  302                          * No matching pcb found; discard datagram.  (No need
  303                          * to send an ICMP Port Unreachable for a broadcast
  304                          * or multicast datgram.)
  305                          */
  306                         udpstat.udps_noport++;
  307                         udpstat.udps_noportmcast++;
  308                         goto badheadlocked;
  309                 }
  310                 INP_RLOCK(last);
  311                 INP_INFO_RUNLOCK(&udbinfo);
  312                 udp6_append(last, m, off, &fromsa);
  313                 INP_RUNLOCK(last);
  314                 return (IPPROTO_DONE);
  315         }
  316         /*
  317          * Locate pcb for datagram.
  318          */
  319         inp = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
  320             &ip6->ip6_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
  321         if (inp == NULL) {
  322                 if (udp_log_in_vain) {
  323                         char ip6bufs[INET6_ADDRSTRLEN];
  324                         char ip6bufd[INET6_ADDRSTRLEN];
  325 
  326                         log(LOG_INFO,
  327                             "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
  328                             ip6_sprintf(ip6bufd, &ip6->ip6_dst),
  329                             ntohs(uh->uh_dport),
  330                             ip6_sprintf(ip6bufs, &ip6->ip6_src),
  331                             ntohs(uh->uh_sport));
  332                 }
  333                 udpstat.udps_noport++;
  334                 if (m->m_flags & M_MCAST) {
  335                         printf("UDP6: M_MCAST is set in a unicast packet.\n");
  336                         udpstat.udps_noportmcast++;
  337                         goto badheadlocked;
  338                 }
  339                 INP_INFO_RUNLOCK(&udbinfo);
  340                 if (udp_blackhole)
  341                         goto badunlocked;
  342                 if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
  343                         goto badunlocked;
  344                 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
  345                 return (IPPROTO_DONE);
  346         }
  347         INP_RLOCK(inp);
  348         INP_INFO_RUNLOCK(&udbinfo);
  349         udp6_append(inp, m, off, &fromsa);
  350         INP_RUNLOCK(inp);
  351         return (IPPROTO_DONE);
  352 
  353 badheadlocked:
  354         INP_INFO_RUNLOCK(&udbinfo);
  355 badunlocked:
  356         if (m)
  357                 m_freem(m);
  358         return (IPPROTO_DONE);
  359 }
  360 
  361 void
  362 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
  363 {
  364         struct udphdr uh;
  365         struct ip6_hdr *ip6;
  366         struct mbuf *m;
  367         int off = 0;
  368         struct ip6ctlparam *ip6cp = NULL;
  369         const struct sockaddr_in6 *sa6_src = NULL;
  370         void *cmdarg;
  371         struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
  372         struct udp_portonly {
  373                 u_int16_t uh_sport;
  374                 u_int16_t uh_dport;
  375         } *uhp;
  376 
  377         if (sa->sa_family != AF_INET6 ||
  378             sa->sa_len != sizeof(struct sockaddr_in6))
  379                 return;
  380 
  381         if ((unsigned)cmd >= PRC_NCMDS)
  382                 return;
  383         if (PRC_IS_REDIRECT(cmd))
  384                 notify = in6_rtchange, d = NULL;
  385         else if (cmd == PRC_HOSTDEAD)
  386                 d = NULL;
  387         else if (inet6ctlerrmap[cmd] == 0)
  388                 return;
  389 
  390         /* if the parameter is from icmp6, decode it. */
  391         if (d != NULL) {
  392                 ip6cp = (struct ip6ctlparam *)d;
  393                 m = ip6cp->ip6c_m;
  394                 ip6 = ip6cp->ip6c_ip6;
  395                 off = ip6cp->ip6c_off;
  396                 cmdarg = ip6cp->ip6c_cmdarg;
  397                 sa6_src = ip6cp->ip6c_src;
  398         } else {
  399                 m = NULL;
  400                 ip6 = NULL;
  401                 cmdarg = NULL;
  402                 sa6_src = &sa6_any;
  403         }
  404 
  405         if (ip6) {
  406                 /*
  407                  * XXX: We assume that when IPV6 is non NULL,
  408                  * M and OFF are valid.
  409                  */
  410 
  411                 /* Check if we can safely examine src and dst ports. */
  412                 if (m->m_pkthdr.len < off + sizeof(*uhp))
  413                         return;
  414 
  415                 bzero(&uh, sizeof(uh));
  416                 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
  417 
  418                 (void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport,
  419                     (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
  420                     cmdarg, notify);
  421         } else
  422                 (void) in6_pcbnotify(&udbinfo, sa, 0,
  423                     (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
  424 }
  425 
  426 static int
  427 udp6_getcred(SYSCTL_HANDLER_ARGS)
  428 {
  429         struct xucred xuc;
  430         struct sockaddr_in6 addrs[2];
  431         struct inpcb *inp;
  432         int error;
  433 
  434         error = priv_check(req->td, PRIV_NETINET_GETCRED);
  435         if (error)
  436                 return (error);
  437 
  438         if (req->newlen != sizeof(addrs))
  439                 return (EINVAL);
  440         if (req->oldlen != sizeof(struct xucred))
  441                 return (EINVAL);
  442         error = SYSCTL_IN(req, addrs, sizeof(addrs));
  443         if (error)
  444                 return (error);
  445         if ((error = sa6_embedscope(&addrs[0], ip6_use_defzone)) != 0 ||
  446             (error = sa6_embedscope(&addrs[1], ip6_use_defzone)) != 0) {
  447                 return (error);
  448         }
  449         INP_INFO_RLOCK(&udbinfo);
  450         inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
  451             addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port, 1,
  452             NULL);
  453         if (inp != NULL) {
  454                 INP_RLOCK(inp);
  455                 INP_INFO_RUNLOCK(&udbinfo);
  456                 if (inp->inp_socket == NULL)
  457                         error = ENOENT;
  458                 if (error == 0)
  459                         error = cr_canseesocket(req->td->td_ucred,
  460                             inp->inp_socket);
  461                 if (error == 0)
  462                         cru2x(inp->inp_cred, &xuc);
  463                 INP_RUNLOCK(inp);
  464         } else {
  465                 INP_INFO_RUNLOCK(&udbinfo);
  466                 error = ENOENT;
  467         }
  468         if (error == 0)
  469                 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
  470         return (error);
  471 }
  472 
  473 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
  474     0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
  475 
  476 static int
  477 udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
  478     struct mbuf *control, struct thread *td)
  479 {
  480         u_int32_t ulen = m->m_pkthdr.len;
  481         u_int32_t plen = sizeof(struct udphdr) + ulen;
  482         struct ip6_hdr *ip6;
  483         struct udphdr *udp6;
  484         struct in6_addr *laddr, *faddr, in6a;
  485         struct sockaddr_in6 *sin6 = NULL;
  486         struct ifnet *oifp = NULL;
  487         int scope_ambiguous = 0;
  488         u_short fport;
  489         int error = 0;
  490         struct ip6_pktopts *optp, opt;
  491         int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
  492         int flags;
  493         struct sockaddr_in6 tmp;
  494 
  495         INP_WLOCK_ASSERT(inp);
  496 
  497         if (addr6) {
  498                 /* addr6 has been validated in udp6_send(). */
  499                 sin6 = (struct sockaddr_in6 *)addr6;
  500 
  501                 /* protect *sin6 from overwrites */
  502                 tmp = *sin6;
  503                 sin6 = &tmp;
  504 
  505                 /*
  506                  * Application should provide a proper zone ID or the use of
  507                  * default zone IDs should be enabled.  Unfortunately, some
  508                  * applications do not behave as it should, so we need a
  509                  * workaround.  Even if an appropriate ID is not determined,
  510                  * we'll see if we can determine the outgoing interface.  If we
  511                  * can, determine the zone ID based on the interface below.
  512                  */
  513                 if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
  514                         scope_ambiguous = 1;
  515                 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
  516                         return (error);
  517         }
  518 
  519         if (control) {
  520                 if ((error = ip6_setpktopts(control, &opt,
  521                     inp->in6p_outputopts, td->td_ucred, IPPROTO_UDP)) != 0)
  522                         goto release;
  523                 optp = &opt;
  524         } else
  525                 optp = inp->in6p_outputopts;
  526 
  527         if (sin6) {
  528                 faddr = &sin6->sin6_addr;
  529 
  530                 /*
  531                  * IPv4 version of udp_output calls in_pcbconnect in this case,
  532                  * which needs splnet and affects performance.
  533                  * Since we saw no essential reason for calling in_pcbconnect,
  534                  * we get rid of such kind of logic, and call in6_selectsrc
  535                  * and in6_pcbsetport in order to fill in the local address
  536                  * and the local port.
  537                  */
  538                 if (sin6->sin6_port == 0) {
  539                         error = EADDRNOTAVAIL;
  540                         goto release;
  541                 }
  542 
  543                 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  544                         /* how about ::ffff:0.0.0.0 case? */
  545                         error = EISCONN;
  546                         goto release;
  547                 }
  548 
  549                 fport = sin6->sin6_port; /* allow 0 port */
  550 
  551                 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
  552                         if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
  553                                 /*
  554                                  * I believe we should explicitly discard the
  555                                  * packet when mapped addresses are disabled,
  556                                  * rather than send the packet as an IPv6 one.
  557                                  * If we chose the latter approach, the packet
  558                                  * might be sent out on the wire based on the
  559                                  * default route, the situation which we'd
  560                                  * probably want to avoid.
  561                                  * (20010421 jinmei@kame.net)
  562                                  */
  563                                 error = EINVAL;
  564                                 goto release;
  565                         }
  566                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
  567                             !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
  568                                 /*
  569                                  * when remote addr is an IPv4-mapped address,
  570                                  * local addr should not be an IPv6 address,
  571                                  * since you cannot determine how to map IPv6
  572                                  * source address to IPv4.
  573                                  */
  574                                 error = EINVAL;
  575                                 goto release;
  576                         }
  577 
  578                         af = AF_INET;
  579                 }
  580 
  581                 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
  582                         error = in6_selectsrc(sin6, optp, inp, NULL,
  583                             td->td_ucred, &oifp, &in6a);
  584                         if (error)
  585                                 goto release;
  586                         if (oifp && scope_ambiguous &&
  587                             (error = in6_setscope(&sin6->sin6_addr,
  588                             oifp, NULL))) {
  589                                 goto release;
  590                         }
  591                         laddr = &in6a;
  592                 } else
  593                         laddr = &inp->in6p_laddr;       /* XXX */
  594                 if (laddr == NULL) {
  595                         if (error == 0)
  596                                 error = EADDRNOTAVAIL;
  597                         goto release;
  598                 }
  599                 if (inp->inp_lport == 0 &&
  600                     (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0)
  601                         goto release;
  602         } else {
  603                 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  604                         error = ENOTCONN;
  605                         goto release;
  606                 }
  607                 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
  608                         if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
  609                                 /*
  610                                  * XXX: this case would happen when the
  611                                  * application sets the V6ONLY flag after
  612                                  * connecting the foreign address.
  613                                  * Such applications should be fixed,
  614                                  * so we bark here.
  615                                  */
  616                                 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
  617                                     "option was set for a connected socket\n");
  618                                 error = EINVAL;
  619                                 goto release;
  620                         } else
  621                                 af = AF_INET;
  622                 }
  623                 laddr = &inp->in6p_laddr;
  624                 faddr = &inp->in6p_faddr;
  625                 fport = inp->inp_fport;
  626         }
  627 
  628         if (af == AF_INET)
  629                 hlen = sizeof(struct ip);
  630 
  631         /*
  632          * Calculate data length and get a mbuf
  633          * for UDP and IP6 headers.
  634          */
  635         M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
  636         if (m == 0) {
  637                 error = ENOBUFS;
  638                 goto release;
  639         }
  640 
  641         /*
  642          * Stuff checksum and output datagram.
  643          */
  644         udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
  645         udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
  646         udp6->uh_dport = fport;
  647         if (plen <= 0xffff)
  648                 udp6->uh_ulen = htons((u_short)plen);
  649         else
  650                 udp6->uh_ulen = 0;
  651         udp6->uh_sum = 0;
  652 
  653         switch (af) {
  654         case AF_INET6:
  655                 ip6 = mtod(m, struct ip6_hdr *);
  656                 ip6->ip6_flow   = inp->inp_flow & IPV6_FLOWINFO_MASK;
  657                 ip6->ip6_vfc    &= ~IPV6_VERSION_MASK;
  658                 ip6->ip6_vfc    |= IPV6_VERSION;
  659 #if 0                           /* ip6_plen will be filled in ip6_output. */
  660                 ip6->ip6_plen   = htons((u_short)plen);
  661 #endif
  662                 ip6->ip6_nxt    = IPPROTO_UDP;
  663                 ip6->ip6_hlim   = in6_selecthlim(inp, NULL);
  664                 ip6->ip6_src    = *laddr;
  665                 ip6->ip6_dst    = *faddr;
  666 
  667                 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
  668                                 sizeof(struct ip6_hdr), plen)) == 0) {
  669                         udp6->uh_sum = 0xffff;
  670                 }
  671 
  672                 flags = 0;
  673 
  674                 udpstat.udps_opackets++;
  675                 error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
  676                     NULL, inp);
  677                 break;
  678         case AF_INET:
  679                 error = EAFNOSUPPORT;
  680                 goto release;
  681         }
  682         goto releaseopt;
  683 
  684 release:
  685         m_freem(m);
  686 
  687 releaseopt:
  688         if (control) {
  689                 ip6_clearpktopts(&opt, -1);
  690                 m_freem(control);
  691         }
  692         return (error);
  693 }
  694 
  695 static void
  696 udp6_abort(struct socket *so)
  697 {
  698         struct inpcb *inp;
  699 
  700         inp = sotoinpcb(so);
  701         KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
  702 
  703 #ifdef INET
  704         if (inp->inp_vflag & INP_IPV4) {
  705                 struct pr_usrreqs *pru;
  706 
  707                 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
  708                 (*pru->pru_abort)(so);
  709                 return;
  710         }
  711 #endif
  712 
  713         INP_INFO_WLOCK(&udbinfo);
  714         INP_WLOCK(inp);
  715         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  716                 in6_pcbdisconnect(inp);
  717                 inp->in6p_laddr = in6addr_any;
  718                 soisdisconnected(so);
  719         }
  720         INP_WUNLOCK(inp);
  721         INP_INFO_WUNLOCK(&udbinfo);
  722 }
  723 
  724 static int
  725 udp6_attach(struct socket *so, int proto, struct thread *td)
  726 {
  727         struct inpcb *inp;
  728         int error;
  729 
  730         inp = sotoinpcb(so);
  731         KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
  732 
  733         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
  734                 error = soreserve(so, udp_sendspace, udp_recvspace);
  735                 if (error)
  736                         return (error);
  737         }
  738         INP_INFO_WLOCK(&udbinfo);
  739         error = in_pcballoc(so, &udbinfo);
  740         if (error) {
  741                 INP_INFO_WUNLOCK(&udbinfo);
  742                 return (error);
  743         }
  744         inp = (struct inpcb *)so->so_pcb;
  745         inp->inp_vflag |= INP_IPV6;
  746         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
  747                 inp->inp_vflag |= INP_IPV4;
  748         inp->in6p_hops = -1;    /* use kernel default */
  749         inp->in6p_cksum = -1;   /* just to be sure */
  750         /*
  751          * XXX: ugly!!
  752          * IPv4 TTL initialization is necessary for an IPv6 socket as well,
  753          * because the socket may be bound to an IPv6 wildcard address,
  754          * which may match an IPv4-mapped IPv6 address.
  755          */
  756         inp->inp_ip_ttl = ip_defttl;
  757 
  758         error = udp_newudpcb(inp);
  759         if (error) {
  760                 in_pcbdetach(inp);
  761                 in_pcbfree(inp);
  762                 INP_INFO_WUNLOCK(&udbinfo);
  763                 return (error);
  764         }
  765         INP_WUNLOCK(inp);
  766         INP_INFO_WUNLOCK(&udbinfo);
  767         return (0);
  768 }
  769 
  770 static int
  771 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
  772 {
  773         struct inpcb *inp;
  774         int error;
  775 
  776         inp = sotoinpcb(so);
  777         KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
  778 
  779         INP_INFO_WLOCK(&udbinfo);
  780         INP_WLOCK(inp);
  781         inp->inp_vflag &= ~INP_IPV4;
  782         inp->inp_vflag |= INP_IPV6;
  783         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
  784                 struct sockaddr_in6 *sin6_p;
  785 
  786                 sin6_p = (struct sockaddr_in6 *)nam;
  787 
  788                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
  789                         inp->inp_vflag |= INP_IPV4;
  790                 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
  791                         struct sockaddr_in sin;
  792 
  793                         in6_sin6_2_sin(&sin, sin6_p);
  794                         inp->inp_vflag |= INP_IPV4;
  795                         inp->inp_vflag &= ~INP_IPV6;
  796                         error = in_pcbbind(inp, (struct sockaddr *)&sin,
  797                             td->td_ucred);
  798                         goto out;
  799                 }
  800         }
  801 
  802         error = in6_pcbbind(inp, nam, td->td_ucred);
  803 out:
  804         INP_WUNLOCK(inp);
  805         INP_INFO_WUNLOCK(&udbinfo);
  806         return (error);
  807 }
  808 
  809 static void
  810 udp6_close(struct socket *so)
  811 {
  812         struct inpcb *inp;
  813 
  814         inp = sotoinpcb(so);
  815         KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
  816 
  817 #ifdef INET
  818         if (inp->inp_vflag & INP_IPV4) {
  819                 struct pr_usrreqs *pru;
  820 
  821                 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
  822                 (*pru->pru_disconnect)(so);
  823                 return;
  824         }
  825 #endif
  826         INP_INFO_WLOCK(&udbinfo);
  827         INP_WLOCK(inp);
  828         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  829                 in6_pcbdisconnect(inp);
  830                 inp->in6p_laddr = in6addr_any;
  831                 soisdisconnected(so);
  832         }
  833         INP_WUNLOCK(inp);
  834         INP_INFO_WUNLOCK(&udbinfo);
  835 }
  836 
  837 static int
  838 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
  839 {
  840         struct inpcb *inp;
  841         struct sockaddr_in6 *sin6;
  842         int error;
  843 
  844         inp = sotoinpcb(so);
  845         sin6 = (struct sockaddr_in6 *)nam;
  846         KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
  847 
  848         INP_INFO_WLOCK(&udbinfo);
  849         INP_WLOCK(inp);
  850         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
  851             IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
  852                 struct sockaddr_in sin;
  853 
  854                 if (inp->inp_faddr.s_addr != INADDR_ANY) {
  855                         error = EISCONN;
  856                         goto out;
  857                 }
  858                 in6_sin6_2_sin(&sin, sin6);
  859                 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
  860                 if (error != 0)
  861                         goto out;
  862                 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
  863                     td->td_ucred);
  864                 if (error == 0) {
  865                         inp->inp_vflag |= INP_IPV4;
  866                         inp->inp_vflag &= ~INP_IPV6;
  867                         soisconnected(so);
  868                 }
  869                 goto out;
  870         }
  871         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  872                 error = EISCONN;
  873                 goto out;
  874         }
  875         error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
  876         if (error != 0)
  877                 goto out;
  878         error = in6_pcbconnect(inp, nam, td->td_ucred);
  879         if (error == 0) {
  880                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
  881                         /* should be non mapped addr */
  882                         inp->inp_vflag &= ~INP_IPV4;
  883                         inp->inp_vflag |= INP_IPV6;
  884                 }
  885                 soisconnected(so);
  886         }
  887 out:
  888         INP_WUNLOCK(inp);
  889         INP_INFO_WUNLOCK(&udbinfo);
  890         return (error);
  891 }
  892 
  893 static void
  894 udp6_detach(struct socket *so)
  895 {
  896         struct inpcb *inp;
  897         struct udpcb *up;
  898 
  899         inp = sotoinpcb(so);
  900         KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
  901 
  902         INP_INFO_WLOCK(&udbinfo);
  903         INP_WLOCK(inp);
  904         up = intoudpcb(inp);
  905         KASSERT(up != NULL, ("%s: up == NULL", __func__));
  906         in_pcbdetach(inp);
  907         in_pcbfree(inp);
  908         INP_INFO_WUNLOCK(&udbinfo);
  909         udp_discardcb(up);
  910 }
  911 
  912 static int
  913 udp6_disconnect(struct socket *so)
  914 {
  915         struct inpcb *inp;
  916         int error;
  917 
  918         inp = sotoinpcb(so);
  919         KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
  920 
  921         INP_INFO_WLOCK(&udbinfo);
  922         INP_WLOCK(inp);
  923 
  924 #ifdef INET
  925         if (inp->inp_vflag & INP_IPV4) {
  926                 struct pr_usrreqs *pru;
  927 
  928                 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
  929                 error = (*pru->pru_disconnect)(so);
  930                 goto out;
  931         }
  932 #endif
  933 
  934         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  935                 error = ENOTCONN;
  936                 goto out;
  937         }
  938 
  939         in6_pcbdisconnect(inp);
  940         inp->in6p_laddr = in6addr_any;
  941         SOCK_LOCK(so);
  942         so->so_state &= ~SS_ISCONNECTED;                /* XXX */
  943         SOCK_UNLOCK(so);
  944 out:
  945         INP_WUNLOCK(inp);
  946         INP_INFO_WUNLOCK(&udbinfo);
  947         return (0);
  948 }
  949 
  950 static int
  951 udp6_send(struct socket *so, int flags, struct mbuf *m,
  952     struct sockaddr *addr, struct mbuf *control, struct thread *td)
  953 {
  954         struct inpcb *inp;
  955         int error = 0;
  956 
  957         inp = sotoinpcb(so);
  958         KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
  959 
  960         INP_INFO_WLOCK(&udbinfo);
  961         INP_WLOCK(inp);
  962         if (addr) {
  963                 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
  964                         error = EINVAL;
  965                         goto bad;
  966                 }
  967                 if (addr->sa_family != AF_INET6) {
  968                         error = EAFNOSUPPORT;
  969                         goto bad;
  970                 }
  971         }
  972 
  973 #ifdef INET
  974         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
  975                 int hasv4addr;
  976                 struct sockaddr_in6 *sin6 = 0;
  977 
  978                 if (addr == 0)
  979                         hasv4addr = (inp->inp_vflag & INP_IPV4);
  980                 else {
  981                         sin6 = (struct sockaddr_in6 *)addr;
  982                         hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
  983                             ? 1 : 0;
  984                 }
  985                 if (hasv4addr) {
  986                         struct pr_usrreqs *pru;
  987 
  988                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
  989                             !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
  990                                 /*
  991                                  * When remote addr is IPv4-mapped address,
  992                                  * local addr should not be an IPv6 address;
  993                                  * since you cannot determine how to map IPv6
  994                                  * source address to IPv4.
  995                                  */
  996                                 error = EINVAL;
  997                                 goto out;
  998                         }
  999 
 1000                         /*
 1001                          * XXXRW: We release UDP-layer locks before calling
 1002                          * udp_send() in order to avoid recursion.  However,
 1003                          * this does mean there is a short window where inp's
 1004                          * fields are unstable.  Could this lead to a
 1005                          * potential race in which the factors causing us to
 1006                          * select the UDPv4 output routine are invalidated?
 1007                          */
 1008                         INP_WUNLOCK(inp);
 1009                         INP_INFO_WUNLOCK(&udbinfo);
 1010                         if (sin6)
 1011                                 in6_sin6_2_sin_in_sock(addr);
 1012                         pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
 1013                         /* addr will just be freed in sendit(). */
 1014                         return ((*pru->pru_send)(so, flags, m, addr, control,
 1015                             td));
 1016                 }
 1017         }
 1018 #endif
 1019 #ifdef MAC
 1020         mac_create_mbuf_from_inpcb(inp, m);
 1021 #endif
 1022         error = udp6_output(inp, m, addr, control, td);
 1023 out:
 1024         INP_WUNLOCK(inp);
 1025         INP_INFO_WUNLOCK(&udbinfo);
 1026         return (error);
 1027 
 1028 bad:
 1029         INP_WUNLOCK(inp);
 1030         INP_INFO_WUNLOCK(&udbinfo);
 1031         m_freem(m);
 1032         return (error);
 1033 }
 1034 
 1035 struct pr_usrreqs udp6_usrreqs = {
 1036         .pru_abort =            udp6_abort,
 1037         .pru_attach =           udp6_attach,
 1038         .pru_bind =             udp6_bind,
 1039         .pru_connect =          udp6_connect,
 1040         .pru_control =          in6_control,
 1041         .pru_detach =           udp6_detach,
 1042         .pru_disconnect =       udp6_disconnect,
 1043         .pru_peeraddr =         in6_mapped_peeraddr,
 1044         .pru_send =             udp6_send,
 1045         .pru_shutdown =         udp_shutdown,
 1046         .pru_sockaddr =         in6_mapped_sockaddr,
 1047         .pru_sosend =           sosend_dgram,
 1048         .pru_sosetlabel =       in_pcbsosetlabel,
 1049         .pru_close =            udp6_close
 1050 };

Cache object: 6ba54ecf7e3c681b33dc6c5d73ca5120


[ 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.