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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $FreeBSD: src/sys/netinet6/udp6_usrreq.c,v 1.51.2.3 2006/02/09 02:33:14 ume Exp $       */
    2 /*      $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 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  * 4. 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_inet.h"
   65 #include "opt_inet6.h"
   66 #include "opt_ipsec.h"
   67 
   68 #include <sys/param.h>
   69 #include <sys/errno.h>
   70 #include <sys/kernel.h>
   71 #include <sys/lock.h>
   72 #include <sys/mbuf.h>
   73 #include <sys/proc.h>
   74 #include <sys/protosw.h>
   75 #include <sys/signalvar.h>
   76 #include <sys/socket.h>
   77 #include <sys/socketvar.h>
   78 #include <sys/stat.h>
   79 #include <sys/sx.h>
   80 #include <sys/sysctl.h>
   81 #include <sys/syslog.h>
   82 #include <sys/systm.h>
   83 
   84 #include <net/if.h>
   85 #include <net/if_types.h>
   86 #include <net/route.h>
   87 
   88 #include <netinet/in.h>
   89 #include <netinet/in_pcb.h>
   90 #include <netinet/in_systm.h>
   91 #include <netinet/in_var.h>
   92 #include <netinet/ip.h>
   93 #include <netinet/ip6.h>
   94 #include <netinet/icmp6.h>
   95 #include <netinet/ip_var.h>
   96 #include <netinet/udp.h>
   97 #include <netinet/udp_var.h>
   98 #include <netinet6/ip6protosw.h>
   99 #include <netinet6/ip6_var.h>
  100 #include <netinet6/in6_pcb.h>
  101 #include <netinet6/udp6_var.h>
  102 
  103 #ifdef IPSEC
  104 #include <netinet6/ipsec.h>
  105 #include <netinet6/ipsec6.h>
  106 #endif /* IPSEC */
  107 
  108 #ifdef FAST_IPSEC
  109 #include <netipsec/ipsec.h>
  110 #include <netipsec/ipsec6.h>
  111 #endif /* FAST_IPSEC */
  112 
  113 /*
  114  * UDP protocol inplementation.
  115  * Per RFC 768, August, 1980.
  116  */
  117 
  118 extern  struct protosw inetsw[];
  119 static  int udp6_detach __P((struct socket *so));
  120 
  121 int
  122 udp6_input(mp, offp, proto)
  123         struct mbuf **mp;
  124         int *offp, proto;
  125 {
  126         struct mbuf *m = *mp, *opts;
  127         register struct ip6_hdr *ip6;
  128         register struct udphdr *uh;
  129         register struct inpcb *in6p;
  130         int off = *offp;
  131         int plen, ulen;
  132         struct sockaddr_in6 fromsa;
  133 
  134         opts = NULL;
  135 
  136         ip6 = mtod(m, struct ip6_hdr *);
  137 
  138         if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
  139                 /* XXX send icmp6 host/port unreach? */
  140                 m_freem(m);
  141                 return IPPROTO_DONE;
  142         }
  143 
  144 #ifndef PULLDOWN_TEST
  145         IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
  146         ip6 = mtod(m, struct ip6_hdr *);
  147         uh = (struct udphdr *)((caddr_t)ip6 + off);
  148 #else
  149         IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
  150         if (!uh)
  151                 return IPPROTO_DONE;
  152 #endif
  153 
  154         udpstat.udps_ipackets++;
  155 
  156         plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
  157         ulen = ntohs((u_short)uh->uh_ulen);
  158 
  159         if (plen != ulen) {
  160                 udpstat.udps_badlen++;
  161                 goto bad;
  162         }
  163 
  164         /*
  165          * Checksum extended UDP header and data.
  166          */
  167         if (uh->uh_sum == 0) {
  168                 udpstat.udps_nosum++;
  169                 goto bad;
  170         }
  171         if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
  172                 udpstat.udps_badsum++;
  173                 goto bad;
  174         }
  175 
  176         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  177                 struct  inpcb *last;
  178 
  179                 /*
  180                  * Deliver a multicast datagram to all sockets
  181                  * for which the local and remote addresses and ports match
  182                  * those of the incoming datagram.  This allows more than
  183                  * one process to receive multicasts on the same port.
  184                  * (This really ought to be done for unicast datagrams as
  185                  * well, but that would cause problems with existing
  186                  * applications that open both address-specific sockets and
  187                  * a wildcard socket listening to the same port -- they would
  188                  * end up receiving duplicates of every unicast datagram.
  189                  * Those applications open the multiple sockets to overcome an
  190                  * inadequacy of the UDP socket interface, but for backwards
  191                  * compatibility we avoid the problem here rather than
  192                  * fixing the interface.  Maybe 4.5BSD will remedy this?)
  193                  */
  194 
  195                 /*
  196                  * In a case that laddr should be set to the link-local
  197                  * address (this happens in RIPng), the multicast address
  198                  * specified in the received packet does not match with
  199                  * laddr. To cure this situation, the matching is relaxed
  200                  * if the receiving interface is the same as one specified
  201                  * in the socket and if the destination multicast address
  202                  * matches one of the multicast groups specified in the socket.
  203                  */
  204 
  205                 /*
  206                  * Construct sockaddr format source address.
  207                  */
  208                 init_sin6(&fromsa, m);
  209                 fromsa.sin6_port = uh->uh_sport;
  210                 /*
  211                  * KAME note: traditionally we dropped udpiphdr from mbuf here.
  212                  * We need udphdr for IPsec processing so we do that later.
  213                  */
  214 
  215                 /*
  216                  * Locate pcb(s) for datagram.
  217                  * (Algorithm copied from raw_intr().)
  218                  */
  219                 last = NULL;
  220                 LIST_FOREACH(in6p, &udb, inp_list) {
  221                         if ((in6p->inp_vflag & INP_IPV6) == 0)
  222                                 continue;
  223                         if (in6p->in6p_lport != uh->uh_dport)
  224                                 continue;
  225                         if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
  226                                 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
  227                                                         &ip6->ip6_dst))
  228                                         continue;
  229                         }
  230                         if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
  231                                 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
  232                                                         &ip6->ip6_src) ||
  233                                     in6p->in6p_fport != uh->uh_sport)
  234                                         continue;
  235                         }
  236 
  237                         if (last != NULL) {
  238                                 struct mbuf *n;
  239 
  240 #if defined(IPSEC) || defined(FAST_IPSEC)
  241                                 /*
  242                                  * Check AH/ESP integrity.
  243                                  */
  244                                 if (ipsec6_in_reject(m, last)) {
  245 #ifdef IPSEC
  246                                         ipsec6stat.in_polvio++;
  247 #endif /* IPSEC */
  248                                         /* do not inject data into pcb */
  249                                 } else
  250 #endif /*IPSEC || FAST_IPSEC*/
  251                                 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
  252                                         /*
  253                                          * KAME NOTE: do not
  254                                          * m_copy(m, offset, ...) above.
  255                                          * sbappendaddr() expects M_PKTHDR,
  256                                          * and m_copy() will copy M_PKTHDR
  257                                          * only if offset is 0.
  258                                          */
  259                                         if (last->in6p_flags & IN6P_CONTROLOPTS
  260                                             || last->in6p_socket->so_options & SO_TIMESTAMP)
  261                                                 ip6_savecontrol(last, n, &opts);
  262 
  263                                         m_adj(n, off + sizeof(struct udphdr));
  264                                         if (sbappendaddr(&last->in6p_socket->so_rcv,
  265                                                         (struct sockaddr *)&fromsa,
  266                                                         n, opts) == 0) {
  267                                                 m_freem(n);
  268                                                 if (opts)
  269                                                         m_freem(opts);
  270                                                 udpstat.udps_fullsock++;
  271                                         } else
  272                                                 sorwakeup(last->in6p_socket);
  273                                         opts = NULL;
  274                                 }
  275                         }
  276                         last = in6p;
  277                         /*
  278                          * Don't look for additional matches if this one does
  279                          * not have either the SO_REUSEPORT or SO_REUSEADDR
  280                          * socket options set.  This heuristic avoids searching
  281                          * through all pcbs in the common case of a non-shared
  282                          * port.  It assumes that an application will never
  283                          * clear these options after setting them.
  284                          */
  285                         if ((last->in6p_socket->so_options &
  286                              (SO_REUSEPORT|SO_REUSEADDR)) == 0)
  287                                 break;
  288                 }
  289 
  290                 if (last == NULL) {
  291                         /*
  292                          * No matching pcb found; discard datagram.
  293                          * (No need to send an ICMP Port Unreachable
  294                          * for a broadcast or multicast datgram.)
  295                          */
  296                         udpstat.udps_noport++;
  297                         udpstat.udps_noportmcast++;
  298                         goto bad;
  299                 }
  300 #if defined(IPSEC) || defined(FAST_IPSEC)
  301                 /*
  302                  * Check AH/ESP integrity.
  303                  */
  304                 if (ipsec6_in_reject(m, last)) {
  305 #ifdef IPSEC
  306                         ipsec6stat.in_polvio++;
  307 #endif /* IPSEC */
  308                         goto bad;
  309                 }
  310 #endif /*IPSEC || FAST_IPSEC*/
  311                 if (last->in6p_flags & IN6P_CONTROLOPTS
  312                     || last->in6p_socket->so_options & SO_TIMESTAMP)
  313                         ip6_savecontrol(last, m, &opts);
  314 
  315                 m_adj(m, off + sizeof(struct udphdr));
  316                 if (sbappendaddr(&last->in6p_socket->so_rcv,
  317                                 (struct sockaddr *)&fromsa,
  318                                 m, opts) == 0) {
  319                         udpstat.udps_fullsock++;
  320                         goto bad;
  321                 }
  322                 sorwakeup(last->in6p_socket);
  323                 return IPPROTO_DONE;
  324         }
  325         /*
  326          * Locate pcb for datagram.
  327          */
  328         in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
  329                                   &ip6->ip6_dst, uh->uh_dport, 1,
  330                                   m->m_pkthdr.rcvif);
  331         if (in6p == 0) {
  332                 if (log_in_vain) {
  333                         char buf[INET6_ADDRSTRLEN];
  334 
  335                         strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
  336                         log(LOG_INFO,
  337                             "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
  338                             buf, ntohs(uh->uh_dport),
  339                             ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
  340                 }
  341                 udpstat.udps_noport++;
  342                 if (m->m_flags & M_MCAST) {
  343                         printf("UDP6: M_MCAST is set in a unicast packet.\n");
  344                         udpstat.udps_noportmcast++;
  345                         goto bad;
  346                 }
  347                 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
  348                 return IPPROTO_DONE;
  349         }
  350 #if defined(IPSEC) || defined(FAST_IPSEC)
  351         /*
  352          * Check AH/ESP integrity.
  353          */
  354         if (ipsec6_in_reject(m, in6p)) {
  355 #ifdef IPSEC
  356                 ipsec6stat.in_polvio++;
  357 #endif /* IPSEC */
  358                 goto bad;
  359         }
  360 #endif /*IPSEC || FAST_IPSEC*/
  361 
  362         /*
  363          * Construct sockaddr format source address.
  364          * Stuff source address and datagram in user buffer.
  365          */
  366         init_sin6(&fromsa, m);
  367         fromsa.sin6_port = uh->uh_sport;
  368         if (in6p->in6p_flags & IN6P_CONTROLOPTS
  369             || in6p->in6p_socket->so_options & SO_TIMESTAMP)
  370                 ip6_savecontrol(in6p, m, &opts);
  371         m_adj(m, off + sizeof(struct udphdr));
  372         if (sbappendaddr(&in6p->in6p_socket->so_rcv,
  373                         (struct sockaddr *)&fromsa, m, opts) == 0) {
  374                 udpstat.udps_fullsock++;
  375                 goto bad;
  376         }
  377         sorwakeup(in6p->in6p_socket);
  378         return IPPROTO_DONE;
  379 bad:
  380         if (m)
  381                 m_freem(m);
  382         if (opts)
  383                 m_freem(opts);
  384         return IPPROTO_DONE;
  385 }
  386 
  387 void
  388 udp6_ctlinput(cmd, sa, d)
  389         int cmd;
  390         struct sockaddr *sa;
  391         void *d;
  392 {
  393         struct udphdr uh;
  394         struct ip6_hdr *ip6;
  395         struct mbuf *m;
  396         int off = 0;
  397         struct ip6ctlparam *ip6cp = NULL;
  398         const struct sockaddr_in6 *sa6_src = NULL;
  399         void *cmdarg;
  400         struct inpcb *(*notify) __P((struct inpcb *, int)) = udp_notify;
  401         struct udp_portonly {
  402                 u_int16_t uh_sport;
  403                 u_int16_t uh_dport;
  404         } *uhp;
  405 
  406         if (sa->sa_family != AF_INET6 ||
  407             sa->sa_len != sizeof(struct sockaddr_in6))
  408                 return;
  409 
  410         if ((unsigned)cmd >= PRC_NCMDS)
  411                 return;
  412         if (PRC_IS_REDIRECT(cmd))
  413                 notify = in6_rtchange, d = NULL;
  414         else if (cmd == PRC_HOSTDEAD)
  415                 d = NULL;
  416         else if (inet6ctlerrmap[cmd] == 0)
  417                 return;
  418 
  419         /* if the parameter is from icmp6, decode it. */
  420         if (d != NULL) {
  421                 ip6cp = (struct ip6ctlparam *)d;
  422                 m = ip6cp->ip6c_m;
  423                 ip6 = ip6cp->ip6c_ip6;
  424                 off = ip6cp->ip6c_off;
  425                 cmdarg = ip6cp->ip6c_cmdarg;
  426                 sa6_src = ip6cp->ip6c_src;
  427         } else {
  428                 m = NULL;
  429                 ip6 = NULL;
  430                 cmdarg = NULL;
  431                 sa6_src = &sa6_any;
  432         }
  433 
  434         if (ip6) {
  435                 /*
  436                  * XXX: We assume that when IPV6 is non NULL,
  437                  * M and OFF are valid.
  438                  */
  439 
  440                 /* check if we can safely examine src and dst ports */
  441                 if (m->m_pkthdr.len < off + sizeof(*uhp))
  442                         return;
  443 
  444                 bzero(&uh, sizeof(uh));
  445                 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
  446 
  447                 (void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport,
  448                                      (struct sockaddr *)ip6cp->ip6c_src,
  449                                      uh.uh_sport, cmd, cmdarg, notify);
  450         } else
  451                 (void) in6_pcbnotify(&udbinfo, sa, 0,
  452                                      (const struct sockaddr *)sa6_src,
  453                                      0, cmd, cmdarg, notify);
  454 }
  455 
  456 static int
  457 udp6_getcred(SYSCTL_HANDLER_ARGS)
  458 {
  459         struct xucred xuc;
  460         struct sockaddr_in6 addrs[2];
  461         struct inpcb *inp;
  462         int error, s;
  463 
  464         error = suser(req->td);
  465         if (error)
  466                 return (error);
  467 
  468         if (req->newlen != sizeof(addrs))
  469                 return (EINVAL);
  470         if (req->oldlen != sizeof(struct xucred))
  471                 return (EINVAL);
  472         error = SYSCTL_IN(req, addrs, sizeof(addrs));
  473         if (error)
  474                 return (error);
  475         s = splnet();
  476         inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
  477                                  addrs[1].sin6_port,
  478                                  &addrs[0].sin6_addr, addrs[0].sin6_port,
  479                                  1, NULL);
  480         if (!inp || !inp->inp_socket) {
  481                 error = ENOENT;
  482                 goto out;
  483         }
  484         cru2x(inp->inp_socket->so_cred, &xuc);
  485         error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
  486 out:
  487         splx(s);
  488         return (error);
  489 }
  490 
  491 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
  492             0, 0,
  493             udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
  494 
  495 static int
  496 udp6_abort(struct socket *so)
  497 {
  498         struct inpcb *inp;
  499         int s;
  500 
  501         INP_INFO_WLOCK(&udbinfo);
  502         inp = sotoinpcb(so);
  503         if (inp == 0) {
  504                 INP_INFO_WUNLOCK(&udbinfo);
  505                 return EINVAL;  /* ??? possible? panic instead? */
  506         }
  507         soisdisconnected(so);
  508         s = splnet();
  509         INP_LOCK(inp);
  510         in6_pcbdetach(inp);
  511         INP_INFO_WUNLOCK(&udbinfo);
  512         splx(s);
  513         return 0;
  514 }
  515 
  516 static int
  517 udp6_attach(struct socket *so, int proto, struct thread *td)
  518 {
  519         struct inpcb *inp;
  520         int s, error;
  521 
  522         INP_INFO_WLOCK(&udbinfo);
  523         inp = sotoinpcb(so);
  524         if (inp != 0) {
  525                 INP_INFO_WUNLOCK(&udbinfo);
  526                 return EINVAL;
  527         }
  528 
  529         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
  530                 error = soreserve(so, udp_sendspace, udp_recvspace);
  531                 if (error) {
  532                         INP_INFO_WUNLOCK(&udbinfo);
  533                         return error;
  534                 }
  535         }
  536         s = splnet();
  537         error = in_pcballoc(so, &udbinfo, "udp6inp");
  538         splx(s);
  539         if (error) {
  540                 INP_INFO_WUNLOCK(&udbinfo);
  541                 return error;
  542         }
  543         inp = (struct inpcb *)so->so_pcb;
  544         INP_LOCK(inp);
  545         INP_INFO_WUNLOCK(&udbinfo);
  546         inp->inp_vflag |= INP_IPV6;
  547         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
  548                 inp->inp_vflag |= INP_IPV4;
  549         inp->in6p_hops = -1;    /* use kernel default */
  550         inp->in6p_cksum = -1;   /* just to be sure */
  551         /*
  552          * XXX: ugly!!
  553          * IPv4 TTL initialization is necessary for an IPv6 socket as well,
  554          * because the socket may be bound to an IPv6 wildcard address,
  555          * which may match an IPv4-mapped IPv6 address.
  556          */
  557         inp->inp_ip_ttl = ip_defttl;
  558         INP_UNLOCK(inp);
  559         return 0;
  560 }
  561 
  562 static int
  563 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
  564 {
  565         struct inpcb *inp;
  566         int s, error;
  567 
  568         INP_INFO_WLOCK(&udbinfo);
  569         inp = sotoinpcb(so);
  570         if (inp == 0) {
  571                 INP_INFO_WUNLOCK(&udbinfo);
  572                 return EINVAL;
  573         }
  574         INP_LOCK(inp);
  575 
  576         inp->inp_vflag &= ~INP_IPV4;
  577         inp->inp_vflag |= INP_IPV6;
  578         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
  579                 struct sockaddr_in6 *sin6_p;
  580 
  581                 sin6_p = (struct sockaddr_in6 *)nam;
  582 
  583                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
  584                         inp->inp_vflag |= INP_IPV4;
  585                 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
  586                         struct sockaddr_in sin;
  587 
  588                         in6_sin6_2_sin(&sin, sin6_p);
  589                         inp->inp_vflag |= INP_IPV4;
  590                         inp->inp_vflag &= ~INP_IPV6;
  591                         s = splnet();
  592                         error = in_pcbbind(inp, (struct sockaddr *)&sin,
  593                             td->td_ucred);
  594                         goto out;
  595                 }
  596         }
  597 
  598         s = splnet();
  599         error = in6_pcbbind(inp, nam, td->td_ucred);
  600 out:
  601         INP_UNLOCK(inp);
  602         INP_INFO_WUNLOCK(&udbinfo);
  603         splx(s);
  604         return error;
  605 }
  606 
  607 static int
  608 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
  609 {
  610         struct inpcb *inp;
  611         int s, error;
  612 
  613         INP_INFO_WLOCK(&udbinfo);
  614         inp = sotoinpcb(so);
  615         if (inp == 0) {
  616                 INP_INFO_WUNLOCK(&udbinfo);
  617                 return EINVAL;
  618         }
  619         INP_LOCK(inp);
  620 
  621         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
  622                 struct sockaddr_in6 *sin6_p;
  623 
  624                 sin6_p = (struct sockaddr_in6 *)nam;
  625                 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
  626                         struct sockaddr_in sin;
  627 
  628                         if (inp->inp_faddr.s_addr != INADDR_ANY)
  629                                 return EISCONN;
  630                         in6_sin6_2_sin(&sin, sin6_p);
  631                         s = splnet();
  632                         error = in_pcbconnect(inp, (struct sockaddr *)&sin,
  633                             td->td_ucred);
  634                         splx(s);
  635                         if (error == 0) {
  636                                 inp->inp_vflag |= INP_IPV4;
  637                                 inp->inp_vflag &= ~INP_IPV6;
  638                                 soisconnected(so);
  639                         }
  640                         goto out;
  641                 }
  642         }
  643         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  644                 error = EISCONN;
  645                 goto out;
  646         }
  647         s = splnet();
  648         error = in6_pcbconnect(inp, nam, td->td_ucred);
  649         splx(s);
  650         if (error == 0) {
  651                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
  652                         /* should be non mapped addr */
  653                         inp->inp_vflag &= ~INP_IPV4;
  654                         inp->inp_vflag |= INP_IPV6;
  655                 }
  656                 soisconnected(so);
  657         }
  658 out:
  659         INP_UNLOCK(inp);
  660         INP_INFO_WUNLOCK(&udbinfo);
  661         return error;
  662 }
  663 
  664 static int
  665 udp6_detach(struct socket *so)
  666 {
  667         struct inpcb *inp;
  668         int s;
  669 
  670         INP_INFO_WLOCK(&udbinfo);
  671         inp = sotoinpcb(so);
  672         if (inp == 0) {
  673                 INP_INFO_WUNLOCK(&udbinfo);
  674                 return EINVAL;
  675         }
  676         INP_LOCK(inp);
  677         s = splnet();
  678         in6_pcbdetach(inp);
  679         splx(s);
  680         INP_INFO_WUNLOCK(&udbinfo);
  681         return 0;
  682 }
  683 
  684 static int
  685 udp6_disconnect(struct socket *so)
  686 {
  687         struct inpcb *inp;
  688         int error, s;
  689 
  690         INP_INFO_WLOCK(&udbinfo);
  691         inp = sotoinpcb(so);
  692         if (inp == 0) {
  693                 INP_INFO_WUNLOCK(&udbinfo);
  694                 return EINVAL;
  695         }
  696         INP_LOCK(inp);
  697 
  698 #ifdef INET
  699         if (inp->inp_vflag & INP_IPV4) {
  700                 struct pr_usrreqs *pru;
  701 
  702                 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
  703                 error = (*pru->pru_disconnect)(so);
  704                 goto out;
  705         }
  706 #endif
  707 
  708         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
  709                 error = ENOTCONN;
  710                 goto out;
  711         }
  712 
  713         s = splnet();
  714         in6_pcbdisconnect(inp);
  715         inp->in6p_laddr = in6addr_any;
  716         splx(s);
  717         /* XXXRW: so_state locking? */
  718         so->so_state &= ~SS_ISCONNECTED;                /* XXX */
  719 out:
  720         INP_UNLOCK(inp);
  721         INP_INFO_WUNLOCK(&udbinfo);
  722         return 0;
  723 }
  724 
  725 static int
  726 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
  727           struct mbuf *control, struct thread *td)
  728 {
  729         struct inpcb *inp;
  730         int error = 0;
  731 
  732         INP_INFO_WLOCK(&udbinfo);
  733         inp = sotoinpcb(so);
  734         if (inp == 0) {
  735                 INP_INFO_WUNLOCK(&udbinfo);
  736                 m_freem(m);
  737                 return EINVAL;
  738         }
  739         INP_LOCK(inp);
  740 
  741         if (addr) {
  742                 if (addr->sa_len != sizeof(struct sockaddr_in6)) { 
  743                         error = EINVAL;
  744                         goto bad;
  745                 }
  746                 if (addr->sa_family != AF_INET6) {
  747                         error = EAFNOSUPPORT;
  748                         goto bad;
  749                 }
  750         }
  751 
  752 #ifdef INET
  753         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
  754                 int hasv4addr;
  755                 struct sockaddr_in6 *sin6 = 0;
  756 
  757                 if (addr == 0)
  758                         hasv4addr = (inp->inp_vflag & INP_IPV4);
  759                 else {
  760                         sin6 = (struct sockaddr_in6 *)addr;
  761                         hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
  762                                 ? 1 : 0;
  763                 }
  764                 if (hasv4addr) {
  765                         struct pr_usrreqs *pru;
  766 
  767                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
  768                             !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
  769                                 /*
  770                                  * when remote addr is IPv4-mapped
  771                                  * address, local addr should not be
  772                                  * an IPv6 address; since you cannot
  773                                  * determine how to map IPv6 source
  774                                  * address to IPv4.
  775                                  */
  776                                 error = EINVAL;
  777                                 goto out;
  778                         }
  779                         if (sin6)
  780                                 in6_sin6_2_sin_in_sock(addr);
  781                         pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
  782                         error = ((*pru->pru_send)(so, flags, m, addr, control,
  783                                                   td));
  784                         /* addr will just be freed in sendit(). */
  785                         goto out;
  786                 }
  787         }
  788 #endif
  789 
  790         error = udp6_output(inp, m, addr, control, td);
  791 out:
  792         INP_UNLOCK(inp);
  793         INP_INFO_WUNLOCK(&udbinfo);
  794         return error;
  795 
  796   bad:
  797         INP_UNLOCK(inp);
  798         INP_INFO_WUNLOCK(&udbinfo);
  799         m_freem(m);
  800         return (error);
  801 }
  802 
  803 struct pr_usrreqs udp6_usrreqs = {
  804         udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect,
  805         pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect,
  806         pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp,
  807         pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown,
  808         in6_mapped_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel
  809 };

Cache object: 1ff5998f1f7714be7df15451a56072b7


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