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.26.8.1 2010/07/16 19:38:11 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.26.8.1 2010/07/16 19:38:11 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/ip6.h>
   95 #include <netinet6/ip6_var.h>
   96 #include <netinet6/in6_pcb.h>
   97 #include <netinet6/udp6_var.h>
   98 #include <netinet/icmp6.h>
   99 #include <netinet6/ip6protosw.h>
  100 #include <netinet6/scope6_var.h>
  101 
  102 #include "faith.h"
  103 
  104 #include <net/net_osdep.h>
  105 
  106 /*
  107  * UDP protocol inplementation.
  108  * Per RFC 768, August, 1980.
  109  */
  110 
  111 int
  112 udp6_output(in6p, m, addr6, control, l)
  113         struct in6pcb *in6p;
  114         struct mbuf *m;
  115         struct mbuf *addr6, *control;
  116         struct lwp *l;
  117 {
  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             &l->l_acflag))
  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, &in6p->in6p_route,
  243                             &in6p->in6p_laddr, &oifp, &error);
  244                         if (oifp && scope_ambiguous &&
  245                             (error = in6_setscope(&sin6->sin6_addr,
  246                             oifp, NULL))) {
  247                                 goto release;
  248                         }
  249                 } else {
  250                         /*
  251                          * XXX: freebsd[34] does not have in_selectsrc, but
  252                          * we can omit the whole part because freebsd4 calls
  253                          * udp_output() directly in this case, and thus we'll
  254                          * never see this path.
  255                          */
  256                         if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
  257                                 struct sockaddr_in *sinp, sin_dst;
  258 
  259                                 bzero(&sin_dst, sizeof(sin_dst));
  260                                 sin_dst.sin_family = AF_INET;
  261                                 sin_dst.sin_len = sizeof(sin_dst);
  262                                 bcopy(&faddr->s6_addr[12], &sin_dst.sin_addr,
  263                                       sizeof(sin_dst.sin_addr));
  264                                 sinp = in_selectsrc(&sin_dst,
  265                                                     (struct route *)&in6p->in6p_route,
  266                                                     in6p->in6p_socket->so_options,
  267                                                     NULL, &error);
  268                                 if (sinp == NULL) {
  269                                         if (error == 0)
  270                                                 error = EADDRNOTAVAIL;
  271                                         goto release;
  272                                 }
  273                                 bzero(&laddr_mapped, sizeof(laddr_mapped));
  274                                 laddr_mapped.s6_addr16[5] = 0xffff; /* ugly */
  275                                 bcopy(&sinp->sin_addr,
  276                                       &laddr_mapped.s6_addr[12],
  277                                       sizeof(sinp->sin_addr));
  278                                 laddr = &laddr_mapped;
  279                         } else
  280                         {
  281                                 laddr = &in6p->in6p_laddr;      /* XXX */
  282                         }
  283                 }
  284                 if (laddr == NULL) {
  285                         if (error == 0)
  286                                 error = EADDRNOTAVAIL;
  287                         goto release;
  288                 }
  289                 if (in6p->in6p_lport == 0 &&
  290                     (error = in6_pcbsetport(laddr, in6p, l)) != 0)
  291                         goto release;
  292         } else {
  293                 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
  294                         error = ENOTCONN;
  295                         goto release;
  296                 }
  297                 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
  298                         if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY))
  299                         {
  300                                 /*
  301                                  * XXX: this case would happen when the
  302                                  * application sets the V6ONLY flag after
  303                                  * connecting the foreign address.
  304                                  * Such applications should be fixed,
  305                                  * so we bark here.
  306                                  */
  307                                 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
  308                                     "option was set for a connected socket\n");
  309                                 error = EINVAL;
  310                                 goto release;
  311                         } else
  312                                 af = AF_INET;
  313                 }
  314                 laddr = &in6p->in6p_laddr;
  315                 faddr = &in6p->in6p_faddr;
  316                 fport = in6p->in6p_fport;
  317         }
  318 
  319         if (af == AF_INET)
  320                 hlen = sizeof(struct ip);
  321 
  322         /*
  323          * Calculate data length and get a mbuf
  324          * for UDP and IP6 headers.
  325          */
  326         M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
  327         if (m == 0) {
  328                 error = ENOBUFS;
  329                 goto release;
  330         }
  331 
  332         /*
  333          * Stuff checksum and output datagram.
  334          */
  335         udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
  336         udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
  337         udp6->uh_dport = fport;
  338         if (plen <= 0xffff)
  339                 udp6->uh_ulen = htons((u_int16_t)plen);
  340         else
  341                 udp6->uh_ulen = 0;
  342         udp6->uh_sum = 0;
  343 
  344         switch (af) {
  345         case AF_INET6:
  346                 ip6 = mtod(m, struct ip6_hdr *);
  347                 ip6->ip6_flow   = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
  348                 ip6->ip6_vfc    &= ~IPV6_VERSION_MASK;
  349                 ip6->ip6_vfc    |= IPV6_VERSION;
  350 #if 0                           /* ip6_plen will be filled in ip6_output. */
  351                 ip6->ip6_plen   = htons((u_int16_t)plen);
  352 #endif
  353                 ip6->ip6_nxt    = IPPROTO_UDP;
  354                 ip6->ip6_hlim   = in6_selecthlim(in6p,
  355                                                  in6p->in6p_route.ro_rt ?
  356                                                  in6p->in6p_route.ro_rt->rt_ifp : NULL);
  357                 ip6->ip6_src    = *laddr;
  358                 ip6->ip6_dst    = *faddr;
  359 
  360                 udp6->uh_sum = in6_cksum_phdr(laddr, faddr,
  361                     htonl(plen), htonl(IPPROTO_UDP));
  362                 m->m_pkthdr.csum_flags = M_CSUM_UDPv6;
  363                 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
  364 
  365                 udp6stat.udp6s_opackets++;
  366                 error = ip6_output(m, optp, &in6p->in6p_route, 0,
  367                     in6p->in6p_moptions, in6p->in6p_socket, NULL);
  368                 break;
  369         case AF_INET:
  370 #ifdef INET
  371                 /* can't transmit jumbogram over IPv4 */
  372                 if (plen > 0xffff) {
  373                         error = EMSGSIZE;
  374                         goto release;
  375                 }
  376 
  377                 ip = mtod(m, struct ip *);
  378                 ui = (struct udpiphdr *)ip;
  379                 bzero(ui->ui_x1, sizeof ui->ui_x1);
  380                 ui->ui_pr = IPPROTO_UDP;
  381                 ui->ui_len = htons(plen);
  382                 bcopy(&laddr->s6_addr[12], &ui->ui_src, sizeof(ui->ui_src));
  383                 ui->ui_ulen = ui->ui_len;
  384 
  385                 flags = (in6p->in6p_socket->so_options &
  386                          (SO_DONTROUTE | SO_BROADCAST));
  387                 bcopy(&faddr->s6_addr[12], &ui->ui_dst, sizeof(ui->ui_dst));
  388 
  389                 udp6->uh_sum = in_cksum(m, hlen + plen);
  390                 if (udp6->uh_sum == 0)
  391                         udp6->uh_sum = 0xffff;
  392 
  393                 ip->ip_len = htons(hlen + plen);
  394                 ip->ip_ttl = in6_selecthlim(in6p, NULL); /* XXX */
  395                 ip->ip_tos = 0; /* XXX */
  396 
  397                 udpstat.udps_opackets++;
  398                 error = ip_output(m, NULL, &in6p->in6p_route, flags /* XXX */,
  399                     (struct ip_moptions *)NULL,
  400                     (struct socket *)in6p->in6p_socket);
  401                 break;
  402 #else
  403                 error = EAFNOSUPPORT;
  404                 goto release;
  405 #endif
  406         }
  407         goto releaseopt;
  408 
  409 release:
  410         m_freem(m);
  411 
  412 releaseopt:
  413         if (control) {
  414                 if (optp == &opt)
  415                         ip6_clearpktopts(&opt, -1);
  416                 m_freem(control);
  417         }
  418         return (error);
  419 }

Cache object: d2a86e0f4ac15dcb12c5b7972584c398


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