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_output.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 /*      $NetBSD: udp6_output.c,v 1.37.4.1 2010/07/16 19:12:53 riz Exp $ */
    2 /*      $KAME: udp6_output.c,v 1.43 2001/10/15 09:19:52 itojun Exp $    */
    3 
    4 /*
    5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. Neither the name of the project nor the names of its contributors
   17  *    may be used to endorse or promote products derived from this software
   18  *    without specific prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   30  * SUCH DAMAGE.
   31  */
   32 
   33 /*
   34  * Copyright (c) 1982, 1986, 1989, 1993
   35  *      The Regents of the University of California.  All rights reserved.
   36  *
   37  * Redistribution and use in source and binary forms, with or without
   38  * modification, are permitted provided that the following conditions
   39  * are met:
   40  * 1. Redistributions of source code must retain the above copyright
   41  *    notice, this list of conditions and the following disclaimer.
   42  * 2. Redistributions in binary form must reproduce the above copyright
   43  *    notice, this list of conditions and the following disclaimer in the
   44  *    documentation and/or other materials provided with the distribution.
   45  * 3. Neither the name of the University nor the names of its contributors
   46  *    may be used to endorse or promote products derived from this software
   47  *    without specific prior written permission.
   48  *
   49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   59  * SUCH DAMAGE.
   60  *
   61  *      @(#)udp_var.h   8.1 (Berkeley) 6/10/93
   62  */
   63 
   64 #include <sys/cdefs.h>
   65 __KERNEL_RCSID(0, "$NetBSD: udp6_output.c,v 1.37.4.1 2010/07/16 19:12:53 riz Exp $");
   66 
   67 #include "opt_inet.h"
   68 
   69 #include <sys/param.h>
   70 #include <sys/malloc.h>
   71 #include <sys/mbuf.h>
   72 #include <sys/protosw.h>
   73 #include <sys/socket.h>
   74 #include <sys/socketvar.h>
   75 #include <sys/errno.h>
   76 #include <sys/stat.h>
   77 #include <sys/systm.h>
   78 #include <sys/proc.h>
   79 #include <sys/syslog.h>
   80 #include <sys/kauth.h>
   81 
   82 #include <net/if.h>
   83 #include <net/route.h>
   84 #include <net/if_types.h>
   85 
   86 #include <netinet/in.h>
   87 #include <netinet/in_var.h>
   88 #include <netinet/in_systm.h>
   89 #include <netinet/ip.h>
   90 #include <netinet/ip_var.h>
   91 #include <netinet/in_pcb.h>
   92 #include <netinet/udp.h>
   93 #include <netinet/udp_var.h>
   94 #include <netinet/udp_private.h>
   95 #include <netinet/ip6.h>
   96 #include <netinet6/ip6_var.h>
   97 #include <netinet6/in6_pcb.h>
   98 #include <netinet6/udp6_var.h>
   99 #include <netinet6/udp6_private.h>
  100 #include <netinet/icmp6.h>
  101 #include <netinet6/ip6protosw.h>
  102 #include <netinet6/scope6_var.h>
  103 
  104 #include "faith.h"
  105 
  106 #include <net/net_osdep.h>
  107 
  108 /*
  109  * UDP protocol inplementation.
  110  * Per RFC 768, August, 1980.
  111  */
  112 
  113 int
  114 udp6_output(struct in6pcb *in6p, struct mbuf *m, struct mbuf *addr6, 
  115         struct mbuf *control, struct lwp *l)
  116 {
  117         struct rtentry *rt;
  118         u_int32_t ulen = m->m_pkthdr.len;
  119         u_int32_t plen = sizeof(struct udphdr) + ulen;
  120         struct ip6_hdr *ip6;
  121         struct udphdr *udp6;
  122         struct in6_addr *laddr, *faddr;
  123         struct in6_addr laddr_mapped; /* XXX ugly */
  124         struct sockaddr_in6 *sin6 = NULL;
  125         struct ifnet *oifp = NULL;
  126         int scope_ambiguous = 0;
  127         u_int16_t fport;
  128         int error = 0;
  129         struct ip6_pktopts *optp = NULL;
  130         struct ip6_pktopts opt;
  131         int priv;
  132         int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
  133 #ifdef INET
  134         struct ip *ip;
  135         struct udpiphdr *ui;
  136         int flags = 0;
  137 #endif
  138         struct sockaddr_in6 tmp;
  139 
  140         priv = 0;
  141         if (l && !kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
  142             NULL))
  143                 priv = 1;
  144 
  145         if (addr6) {
  146                 if (addr6->m_len != sizeof(*sin6)) {
  147                         error = EINVAL;
  148                         goto release;
  149                 }
  150                 sin6 = mtod(addr6, struct sockaddr_in6 *);
  151                 if (sin6->sin6_family != AF_INET6) {
  152                         error = EAFNOSUPPORT;
  153                         goto release;
  154                 }
  155 
  156                 /* protect *sin6 from overwrites */
  157                 tmp = *sin6;
  158                 sin6 = &tmp;
  159 
  160                 /*
  161                  * Application should provide a proper zone ID or the use of
  162                  * default zone IDs should be enabled.  Unfortunately, some
  163                  * applications do not behave as it should, so we need a
  164                  * workaround.  Even if an appropriate ID is not determined,
  165                  * we'll see if we can determine the outgoing interface.  If we
  166                  * can, determine the zone ID based on the interface below.
  167                  */
  168                 if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
  169                         scope_ambiguous = 1;
  170                 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
  171                         goto release;
  172         }
  173 
  174         if (control) {
  175                 if ((error = ip6_setpktopts(control, &opt,
  176                     in6p->in6p_outputopts, priv, IPPROTO_UDP)) != 0)
  177                         goto release;
  178                 optp = &opt;
  179         } else
  180                 optp = in6p->in6p_outputopts;
  181 
  182 
  183         if (sin6) {
  184                 faddr = &sin6->sin6_addr;
  185 
  186                 /*
  187                  * IPv4 version of udp_output calls in_pcbconnect in this case,
  188                  * which needs splnet and affects performance.
  189                  * Since we saw no essential reason for calling in_pcbconnect,
  190                  * we get rid of such kind of logic, and call in6_selectsrc
  191                  * and in6_pcbsetport in order to fill in the local address
  192                  * and the local port.
  193                  */
  194                 if (sin6->sin6_port == 0) {
  195                         error = EADDRNOTAVAIL;
  196                         goto release;
  197                 }
  198 
  199                 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
  200                         /* how about ::ffff:0.0.0.0 case? */
  201                         error = EISCONN;
  202                         goto release;
  203                 }
  204 
  205 
  206                 faddr = &sin6->sin6_addr;
  207                 fport = sin6->sin6_port; /* allow 0 port */
  208 
  209                 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
  210                         if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY))
  211                         {
  212                                 /*
  213                                  * I believe we should explicitly discard the
  214                                  * packet when mapped addresses are disabled,
  215                                  * rather than send the packet as an IPv6 one.
  216                                  * If we chose the latter approach, the packet
  217                                  * might be sent out on the wire based on the
  218                                  * default route, the situation which we'd
  219                                  * probably want to avoid.
  220                                  * (20010421 jinmei@kame.net)
  221                                  */
  222                                 error = EINVAL;
  223                                 goto release;
  224                         }
  225                         if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)
  226                             && !IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
  227                                 /*
  228                                  * when remote addr is an IPv4-mapped address,
  229                                  * local addr should not be an IPv6 address,
  230                                  * since you cannot determine how to map IPv6
  231                                  * source address to IPv4.
  232                                  */
  233                                 error = EINVAL;
  234                                 goto release;
  235                         }
  236 
  237                         af = AF_INET;
  238                 }
  239 
  240                 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
  241                         laddr = in6_selectsrc(sin6, optp,
  242                             in6p->in6p_moptions,
  243                             &in6p->in6p_route,
  244                             &in6p->in6p_laddr, &oifp, &error);
  245                         if (oifp && scope_ambiguous &&
  246                             (error = in6_setscope(&sin6->sin6_addr,
  247                             oifp, NULL))) {
  248                                 goto release;
  249                         }
  250                 } else {
  251                         /*
  252                          * XXX: freebsd[34] does not have in_selectsrc, but
  253                          * we can omit the whole part because freebsd4 calls
  254                          * udp_output() directly in this case, and thus we'll
  255                          * never see this path.
  256                          */
  257                         if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
  258                                 struct sockaddr_in *sinp, sin_dst;
  259                                 struct in_addr ina;
  260 
  261                                 memcpy(&ina, &faddr->s6_addr[12], sizeof(ina));
  262                                 sockaddr_in_init(&sin_dst, &ina, 0);
  263                                 sinp = in_selectsrc(&sin_dst, &in6p->in6p_route,
  264                                     in6p->in6p_socket->so_options, NULL,
  265                                     &error);
  266                                 if (sinp == NULL) {
  267                                         if (error == 0)
  268                                                 error = EADDRNOTAVAIL;
  269                                         goto release;
  270                                 }
  271                                 memset(&laddr_mapped, 0, sizeof(laddr_mapped));
  272                                 laddr_mapped.s6_addr16[5] = 0xffff; /* ugly */
  273                                 memcpy(&laddr_mapped.s6_addr[12],
  274                                       &sinp->sin_addr,
  275                                       sizeof(sinp->sin_addr));
  276                                 laddr = &laddr_mapped;
  277                         } else
  278                         {
  279                                 laddr = &in6p->in6p_laddr;      /* XXX */
  280                         }
  281                 }
  282                 if (laddr == NULL) {
  283                         if (error == 0)
  284                                 error = EADDRNOTAVAIL;
  285                         goto release;
  286                 }
  287                 if (in6p->in6p_lport == 0 &&
  288                     (error = in6_pcbsetport(laddr, in6p, l)) != 0)
  289                         goto release;
  290         } else {
  291                 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
  292                         error = ENOTCONN;
  293                         goto release;
  294                 }
  295                 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
  296                         if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY))
  297                         {
  298                                 /*
  299                                  * XXX: this case would happen when the
  300                                  * application sets the V6ONLY flag after
  301                                  * connecting the foreign address.
  302                                  * Such applications should be fixed,
  303                                  * so we bark here.
  304                                  */
  305                                 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
  306                                     "option was set for a connected socket\n");
  307                                 error = EINVAL;
  308                                 goto release;
  309                         } else
  310                                 af = AF_INET;
  311                 }
  312                 laddr = &in6p->in6p_laddr;
  313                 faddr = &in6p->in6p_faddr;
  314                 fport = in6p->in6p_fport;
  315         }
  316 
  317         if (af == AF_INET)
  318                 hlen = sizeof(struct ip);
  319 
  320         /*
  321          * Calculate data length and get a mbuf
  322          * for UDP and IP6 headers.
  323          */
  324         M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
  325         if (m == 0) {
  326                 error = ENOBUFS;
  327                 goto release;
  328         }
  329 
  330         /*
  331          * Stuff checksum and output datagram.
  332          */
  333         udp6 = (struct udphdr *)(mtod(m, char *) + hlen);
  334         udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
  335         udp6->uh_dport = fport;
  336         if (plen <= 0xffff)
  337                 udp6->uh_ulen = htons((u_int16_t)plen);
  338         else
  339                 udp6->uh_ulen = 0;
  340         udp6->uh_sum = 0;
  341 
  342         switch (af) {
  343         case AF_INET6:
  344                 ip6 = mtod(m, struct ip6_hdr *);
  345                 ip6->ip6_flow   = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
  346                 ip6->ip6_vfc    &= ~IPV6_VERSION_MASK;
  347                 ip6->ip6_vfc    |= IPV6_VERSION;
  348 #if 0                           /* ip6_plen will be filled in ip6_output. */
  349                 ip6->ip6_plen   = htons((u_int16_t)plen);
  350 #endif
  351                 ip6->ip6_nxt    = IPPROTO_UDP;
  352                 ip6->ip6_hlim   = in6_selecthlim(in6p,
  353                     (rt = rtcache_validate(&in6p->in6p_route)) != NULL
  354                         ? rt->rt_ifp : NULL);
  355                 ip6->ip6_src    = *laddr;
  356                 ip6->ip6_dst    = *faddr;
  357 
  358                 udp6->uh_sum = in6_cksum_phdr(laddr, faddr,
  359                     htonl(plen), htonl(IPPROTO_UDP));
  360                 m->m_pkthdr.csum_flags = M_CSUM_UDPv6;
  361                 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
  362 
  363                 UDP6_STATINC(UDP6_STAT_OPACKETS);
  364                 error = ip6_output(m, optp, &in6p->in6p_route, 0,
  365                     in6p->in6p_moptions, in6p->in6p_socket, NULL);
  366                 break;
  367         case AF_INET:
  368 #ifdef INET
  369                 /* can't transmit jumbogram over IPv4 */
  370                 if (plen > 0xffff) {
  371                         error = EMSGSIZE;
  372                         goto release;
  373                 }
  374 
  375                 ip = mtod(m, struct ip *);
  376                 ui = (struct udpiphdr *)ip;
  377                 memset(ui->ui_x1, 0, sizeof(ui->ui_x1));
  378                 ui->ui_pr = IPPROTO_UDP;
  379                 ui->ui_len = htons(plen);
  380                 memcpy(&ui->ui_src, &laddr->s6_addr[12], sizeof(ui->ui_src));
  381                 ui->ui_ulen = ui->ui_len;
  382 
  383                 flags = (in6p->in6p_socket->so_options &
  384                          (SO_DONTROUTE | SO_BROADCAST));
  385                 memcpy(&ui->ui_dst, &faddr->s6_addr[12], sizeof(ui->ui_dst));
  386 
  387                 udp6->uh_sum = in_cksum(m, hlen + plen);
  388                 if (udp6->uh_sum == 0)
  389                         udp6->uh_sum = 0xffff;
  390 
  391                 ip->ip_len = htons(hlen + plen);
  392                 ip->ip_ttl = in6_selecthlim(in6p, NULL); /* XXX */
  393                 ip->ip_tos = 0; /* XXX */
  394 
  395                 UDP_STATINC(UDP_STAT_OPACKETS);
  396                 error = ip_output(m, NULL, &in6p->in6p_route, flags /* XXX */,
  397                     (struct ip_moptions *)NULL,
  398                     (struct socket *)in6p->in6p_socket);
  399                 break;
  400 #else
  401                 error = EAFNOSUPPORT;
  402                 goto release;
  403 #endif
  404         }
  405         goto releaseopt;
  406 
  407 release:
  408         m_freem(m);
  409 
  410 releaseopt:
  411         if (control) {
  412                 if (optp == &opt)
  413                         ip6_clearpktopts(&opt, -1);
  414                 m_freem(control);
  415         }
  416         return (error);
  417 }

Cache object: af88285a4309f108d24349a935bd99b1


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