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 /*      $FreeBSD: src/sys/netinet6/udp6_output.c,v 1.1.2.6 2003/01/23 21:06:47 sam Exp $        */
    2 /*      $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei 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 "opt_ipsec.h"
   65 #include "opt_inet.h"
   66 #include "opt_inet6.h"
   67 
   68 #include <sys/param.h>
   69 #include <sys/malloc.h>
   70 #include <sys/mbuf.h>
   71 #include <sys/protosw.h>
   72 #include <sys/socket.h>
   73 #include <sys/socketvar.h>
   74 #include <sys/sysctl.h>
   75 #include <sys/errno.h>
   76 #include <sys/stat.h>
   77 #include <sys/systm.h>
   78 #include <sys/proc.h>
   79 #include <sys/priv.h>
   80 #include <sys/syslog.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 
  101 #ifdef IPSEC
  102 #include <netinet6/ipsec.h>
  103 #ifdef INET6
  104 #include <netinet6/ipsec6.h>
  105 #endif
  106 #endif /* IPSEC */
  107 
  108 #include <net/net_osdep.h>
  109 
  110 /*
  111  * UDP protocol inplementation.
  112  * Per RFC 768, August, 1980.
  113  */
  114 
  115 #define in6pcb          inpcb
  116 #define udp6stat        udp_stat
  117 #define udp6s_opackets  udps_opackets
  118 
  119 int
  120 udp6_output(struct in6pcb *in6p, struct mbuf *m, struct sockaddr *addr6,
  121             struct mbuf *control, struct thread *td)
  122 {
  123         u_int32_t ulen = m->m_pkthdr.len;
  124         u_int32_t plen = sizeof(struct udphdr) + ulen;
  125         struct ip6_hdr *ip6;
  126         struct udphdr *udp6;
  127         struct in6_addr *laddr, *faddr;
  128         u_short fport;
  129         int error = 0;
  130         struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
  131         int priv;
  132         int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
  133         int flags;
  134         struct sockaddr_in6 tmp;
  135 
  136         priv = !priv_check(td, PRIV_ROOT);      /* 1 if privileged, 0 if not */
  137         if (control) {
  138                 if ((error = ip6_setpktoptions(control, &opt,
  139                     in6p->in6p_outputopts, 
  140                     IPPROTO_UDP, priv)) != 0)
  141                         goto release;
  142                 in6p->in6p_outputopts = &opt;
  143         }
  144 
  145         if (addr6) {
  146                 /*
  147                  * IPv4 version of udp_output calls in_pcbconnect in this case,
  148                  * which needs splnet and affects performance.
  149                  * Since we saw no essential reason for calling in_pcbconnect,
  150                  * we get rid of such kind of logic, and call in6_selectsrc
  151                  * and in6_pcbsetport in order to fill in the local address
  152                  * and the local port.
  153                  */
  154                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr6;
  155                 if (sin6->sin6_port == 0) {
  156                         error = EADDRNOTAVAIL;
  157                         goto release;
  158                 }
  159 
  160                 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
  161                         /* how about ::ffff:0.0.0.0 case? */
  162                         error = EISCONN;
  163                         goto release;
  164                 }
  165                 if (!prison_remote_ip(td, addr6)) {
  166                         error = EAFNOSUPPORT; /* IPv4 only jail */
  167                         goto release;
  168                 }
  169 
  170                 /* protect *sin6 from overwrites */
  171                 tmp = *sin6;
  172                 sin6 = &tmp;
  173 
  174                 faddr = &sin6->sin6_addr;
  175                 fport = sin6->sin6_port; /* allow 0 port */
  176 
  177                 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
  178                         if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
  179                                 /*
  180                                  * I believe we should explicitly discard the
  181                                  * packet when mapped addresses are disabled,
  182                                  * rather than send the packet as an IPv6 one.
  183                                  * If we chose the latter approach, the packet
  184                                  * might be sent out on the wire based on the
  185                                  * default route, the situation which we'd
  186                                  * probably want to avoid.
  187                                  * (20010421 jinmei@kame.net)
  188                                  */
  189                                 error = EINVAL;
  190                                 goto release;
  191                         } else
  192                                 af = AF_INET;
  193                 }
  194 
  195                 /* KAME hack: embed scopeid */
  196                 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) {
  197                         error = EINVAL;
  198                         goto release;
  199                 }
  200 
  201                 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
  202                         laddr = in6_selectsrc(sin6, in6p->in6p_outputopts,
  203                                               in6p->in6p_moptions,
  204                                               &in6p->in6p_route,
  205                                               &in6p->in6p_laddr, &error, NULL);
  206                 } else
  207                         laddr = &in6p->in6p_laddr;      /* XXX */
  208                 if (laddr == NULL) {
  209                         if (error == 0)
  210                                 error = EADDRNOTAVAIL;
  211                         goto release;
  212                 }
  213                 if (in6p->in6p_lport == 0 &&
  214                     (error = in6_pcbsetport(laddr, in6p, td)) != 0)
  215                         goto release;
  216         } else {
  217                 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
  218                         error = ENOTCONN;
  219                         goto release;
  220                 }
  221                 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
  222                         if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
  223                                 /*
  224                                  * XXX: this case would happen when the
  225                                  * application sets the V6ONLY flag after
  226                                  * connecting the foreign address.
  227                                  * Such applications should be fixed,
  228                                  * so we bark here.
  229                                  */
  230                                 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
  231                                     "option was set for a connected socket\n");
  232                                 error = EINVAL;
  233                                 goto release;
  234                         } else
  235                                 af = AF_INET;
  236                 }
  237                 laddr = &in6p->in6p_laddr;
  238                 faddr = &in6p->in6p_faddr;
  239                 fport = in6p->in6p_fport;
  240         }
  241 
  242         if (af == AF_INET)
  243                 hlen = sizeof(struct ip);
  244 
  245         /*
  246          * Calculate data length and get a mbuf
  247          * for UDP and IP6 headers.
  248          */
  249         M_PREPEND(m, hlen + sizeof(struct udphdr), MB_DONTWAIT);
  250         if (m == NULL) {
  251                 error = ENOBUFS;
  252                 goto release;
  253         }
  254 
  255         /*
  256          * Stuff checksum and output datagram.
  257          */
  258         udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
  259         udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
  260         udp6->uh_dport = fport;
  261         if (plen <= 0xffff)
  262                 udp6->uh_ulen = htons((u_short)plen);
  263         else
  264                 udp6->uh_ulen = 0;
  265         udp6->uh_sum = 0;
  266 
  267         switch (af) {
  268         case AF_INET6:
  269                 ip6 = mtod(m, struct ip6_hdr *);
  270                 ip6->ip6_flow   = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
  271                 ip6->ip6_vfc    &= ~IPV6_VERSION_MASK;
  272                 ip6->ip6_vfc    |= IPV6_VERSION;
  273 #if 0                           /* ip6_plen will be filled in ip6_output. */
  274                 ip6->ip6_plen   = htons((u_short)plen);
  275 #endif
  276                 ip6->ip6_nxt    = IPPROTO_UDP;
  277                 ip6->ip6_hlim   = in6_selecthlim(in6p,
  278                                                  in6p->in6p_route.ro_rt ?
  279                                                  in6p->in6p_route.ro_rt->rt_ifp : NULL);
  280                 ip6->ip6_src    = *laddr;
  281                 ip6->ip6_dst    = *faddr;
  282 
  283                 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
  284                                 sizeof(struct ip6_hdr), plen)) == 0) {
  285                         udp6->uh_sum = 0xffff;
  286                 }
  287 
  288                 flags = 0;
  289 
  290                 udp6stat.udp6s_opackets++;
  291                 error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
  292                     flags, in6p->in6p_moptions, NULL, in6p);
  293                 break;
  294         case AF_INET:
  295                 error = EAFNOSUPPORT;
  296                 goto release;
  297         }
  298         goto releaseopt;
  299 
  300 release:
  301         m_freem(m);
  302 
  303 releaseopt:
  304         if (control) {
  305                 ip6_clearpktopts(in6p->in6p_outputopts, -1);
  306                 in6p->in6p_outputopts = stickyopt;
  307                 m_freem(control);
  308         }
  309         return (error);
  310 }

Cache object: 9d20912520778fd69fa35551094e7b8c


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