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

Cache object: 8373fe481e05a01d43dd6f4fa993b647


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