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/raw_ip6.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 /*-
    2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. Neither the name of the project nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  */
   29 
   30 /*-
   31  * Copyright (c) 1982, 1986, 1988, 1993
   32  *      The Regents of the University of California.
   33  * All rights reserved.
   34  *
   35  * Redistribution and use in source and binary forms, with or without
   36  * modification, are permitted provided that the following conditions
   37  * are met:
   38  * 1. Redistributions of source code must retain the above copyright
   39  *    notice, this list of conditions and the following disclaimer.
   40  * 2. Redistributions in binary form must reproduce the above copyright
   41  *    notice, this list of conditions and the following disclaimer in the
   42  *    documentation and/or other materials provided with the distribution.
   43  * 4. Neither the name of the University nor the names of its contributors
   44  *    may be used to endorse or promote products derived from this software
   45  *    without specific prior written permission.
   46  *
   47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   57  * SUCH DAMAGE.
   58  *
   59  *      @(#)raw_ip.c    8.2 (Berkeley) 1/4/94
   60  */
   61 
   62 #include <sys/cdefs.h>
   63 __FBSDID("$FreeBSD: releng/8.3/sys/netinet6/raw_ip6.c 212056 2010-08-31 15:52:12Z ume $");
   64 
   65 #include "opt_ipsec.h"
   66 #include "opt_inet6.h"
   67 
   68 #include <sys/param.h>
   69 #include <sys/errno.h>
   70 #include <sys/jail.h>
   71 #include <sys/lock.h>
   72 #include <sys/malloc.h>
   73 #include <sys/mbuf.h>
   74 #include <sys/priv.h>
   75 #include <sys/proc.h>
   76 #include <sys/protosw.h>
   77 #include <sys/signalvar.h>
   78 #include <sys/socket.h>
   79 #include <sys/socketvar.h>
   80 #include <sys/sx.h>
   81 #include <sys/syslog.h>
   82 
   83 #include <net/if.h>
   84 #include <net/if_types.h>
   85 #include <net/route.h>
   86 #include <net/vnet.h>
   87 
   88 #include <netinet/in.h>
   89 #include <netinet/in_var.h>
   90 #include <netinet/in_systm.h>
   91 #include <netinet/in_pcb.h>
   92 
   93 #include <netinet/icmp6.h>
   94 #include <netinet/ip6.h>
   95 #include <netinet6/ip6protosw.h>
   96 #include <netinet6/ip6_mroute.h>
   97 #include <netinet6/in6_pcb.h>
   98 #include <netinet6/ip6_var.h>
   99 #include <netinet6/nd6.h>
  100 #include <netinet6/raw_ip6.h>
  101 #include <netinet6/scope6_var.h>
  102 
  103 #ifdef IPSEC
  104 #include <netipsec/ipsec.h>
  105 #include <netipsec/ipsec6.h>
  106 #endif /* IPSEC */
  107 
  108 #include <machine/stdarg.h>
  109 
  110 #define satosin6(sa)    ((struct sockaddr_in6 *)(sa))
  111 #define ifatoia6(ifa)   ((struct in6_ifaddr *)(ifa))
  112 
  113 /*
  114  * Raw interface to IP6 protocol.
  115  */
  116 
  117 VNET_DECLARE(struct inpcbhead, ripcb);
  118 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
  119 #define V_ripcb                         VNET(ripcb)
  120 #define V_ripcbinfo                     VNET(ripcbinfo)
  121 
  122 extern u_long   rip_sendspace;
  123 extern u_long   rip_recvspace;
  124 
  125 VNET_DEFINE(struct rip6stat, rip6stat);
  126 
  127 /*
  128  * Hooks for multicast routing. They all default to NULL, so leave them not
  129  * initialized and rely on BSS being set to 0.
  130  */
  131 
  132 /*
  133  * The socket used to communicate with the multicast routing daemon.
  134  */
  135 VNET_DEFINE(struct socket *, ip6_mrouter);
  136 
  137 /*
  138  * The various mrouter functions.
  139  */
  140 int (*ip6_mrouter_set)(struct socket *, struct sockopt *);
  141 int (*ip6_mrouter_get)(struct socket *, struct sockopt *);
  142 int (*ip6_mrouter_done)(void);
  143 int (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *);
  144 int (*mrt6_ioctl)(u_long, caddr_t);
  145 
  146 /*
  147  * Setup generic address and protocol structures for raw_input routine, then
  148  * pass them along with mbuf chain.
  149  */
  150 int
  151 rip6_input(struct mbuf **mp, int *offp, int proto)
  152 {
  153         struct ifnet *ifp;
  154         struct mbuf *m = *mp;
  155         register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  156         register struct inpcb *in6p;
  157         struct inpcb *last = 0;
  158         struct mbuf *opts = NULL;
  159         struct sockaddr_in6 fromsa;
  160 
  161         V_rip6stat.rip6s_ipackets++;
  162 
  163         if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
  164                 /* XXX Send icmp6 host/port unreach? */
  165                 m_freem(m);
  166                 return (IPPROTO_DONE);
  167         }
  168 
  169         init_sin6(&fromsa, m); /* general init */
  170 
  171         ifp = m->m_pkthdr.rcvif;
  172 
  173         INP_INFO_RLOCK(&V_ripcbinfo);
  174         LIST_FOREACH(in6p, &V_ripcb, inp_list) {
  175                 /* XXX inp locking */
  176                 if ((in6p->inp_vflag & INP_IPV6) == 0)
  177                         continue;
  178                 if (in6p->inp_ip_p &&
  179                     in6p->inp_ip_p != proto)
  180                         continue;
  181                 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
  182                     !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
  183                         continue;
  184                 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
  185                     !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
  186                         continue;
  187                 if (jailed_without_vnet(in6p->inp_cred)) {
  188                         /*
  189                          * Allow raw socket in jail to receive multicast;
  190                          * assume process had PRIV_NETINET_RAW at attach,
  191                          * and fall through into normal filter path if so.
  192                          */
  193                         if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
  194                             prison_check_ip6(in6p->inp_cred,
  195                             &ip6->ip6_dst) != 0)
  196                                 continue;
  197                 }
  198                 if (in6p->in6p_cksum != -1) {
  199                         V_rip6stat.rip6s_isum++;
  200                         if (in6_cksum(m, proto, *offp,
  201                             m->m_pkthdr.len - *offp)) {
  202                                 INP_RUNLOCK(in6p);
  203                                 V_rip6stat.rip6s_badsum++;
  204                                 continue;
  205                         }
  206                 }
  207                 INP_RLOCK(in6p);
  208                 /*
  209                  * If this raw socket has multicast state, and we
  210                  * have received a multicast, check if this socket
  211                  * should receive it, as multicast filtering is now
  212                  * the responsibility of the transport layer.
  213                  */
  214                 if (in6p->in6p_moptions &&
  215                     IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
  216                         /*
  217                          * If the incoming datagram is for MLD, allow it
  218                          * through unconditionally to the raw socket.
  219                          *
  220                          * Use the M_RTALERT_MLD flag to check for MLD
  221                          * traffic without having to inspect the mbuf chain
  222                          * more deeply, as all MLDv1/v2 host messages MUST
  223                          * contain the Router Alert option.
  224                          *
  225                          * In the case of MLDv1, we may not have explicitly
  226                          * joined the group, and may have set IFF_ALLMULTI
  227                          * on the interface. im6o_mc_filter() may discard
  228                          * control traffic we actually need to see.
  229                          *
  230                          * Userland multicast routing daemons should continue
  231                          * filter the control traffic appropriately.
  232                          */
  233                         int blocked;
  234 
  235                         blocked = MCAST_PASS;
  236                         if ((m->m_flags & M_RTALERT_MLD) == 0) {
  237                                 struct sockaddr_in6 mcaddr;
  238 
  239                                 bzero(&mcaddr, sizeof(struct sockaddr_in6));
  240                                 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
  241                                 mcaddr.sin6_family = AF_INET6;
  242                                 mcaddr.sin6_addr = ip6->ip6_dst;
  243 
  244                                 blocked = im6o_mc_filter(in6p->in6p_moptions,
  245                                     ifp,
  246                                     (struct sockaddr *)&mcaddr,
  247                                     (struct sockaddr *)&fromsa);
  248                         }
  249                         if (blocked != MCAST_PASS) {
  250                                 IP6STAT_INC(ip6s_notmember);
  251                                 INP_RUNLOCK(in6p);
  252                                 continue;
  253                         }
  254                 }
  255                 if (last != NULL) {
  256                         struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
  257 
  258 #ifdef IPSEC
  259                         /*
  260                          * Check AH/ESP integrity.
  261                          */
  262                         if (n && ipsec6_in_reject(n, last)) {
  263                                 m_freem(n);
  264                                 V_ipsec6stat.in_polvio++;
  265                                 /* Do not inject data into pcb. */
  266                         } else
  267 #endif /* IPSEC */
  268                         if (n) {
  269                                 if (last->inp_flags & INP_CONTROLOPTS ||
  270                                     last->inp_socket->so_options & SO_TIMESTAMP)
  271                                         ip6_savecontrol(last, n, &opts);
  272                                 /* strip intermediate headers */
  273                                 m_adj(n, *offp);
  274                                 if (sbappendaddr(&last->inp_socket->so_rcv,
  275                                                 (struct sockaddr *)&fromsa,
  276                                                  n, opts) == 0) {
  277                                         m_freem(n);
  278                                         if (opts)
  279                                                 m_freem(opts);
  280                                         V_rip6stat.rip6s_fullsock++;
  281                                 } else
  282                                         sorwakeup(last->inp_socket);
  283                                 opts = NULL;
  284                         }
  285                         INP_RUNLOCK(last);
  286                 }
  287                 last = in6p;
  288         }
  289         INP_INFO_RUNLOCK(&V_ripcbinfo);
  290 #ifdef IPSEC
  291         /*
  292          * Check AH/ESP integrity.
  293          */
  294         if ((last != NULL) && ipsec6_in_reject(m, last)) {
  295                 m_freem(m);
  296                 V_ipsec6stat.in_polvio++;
  297                 V_ip6stat.ip6s_delivered--;
  298                 /* Do not inject data into pcb. */
  299                 INP_RUNLOCK(last);
  300         } else
  301 #endif /* IPSEC */
  302         if (last != NULL) {
  303                 if (last->inp_flags & INP_CONTROLOPTS ||
  304                     last->inp_socket->so_options & SO_TIMESTAMP)
  305                         ip6_savecontrol(last, m, &opts);
  306                 /* Strip intermediate headers. */
  307                 m_adj(m, *offp);
  308                 if (sbappendaddr(&last->inp_socket->so_rcv,
  309                     (struct sockaddr *)&fromsa, m, opts) == 0) {
  310                         m_freem(m);
  311                         if (opts)
  312                                 m_freem(opts);
  313                         V_rip6stat.rip6s_fullsock++;
  314                 } else
  315                         sorwakeup(last->inp_socket);
  316                 INP_RUNLOCK(last);
  317         } else {
  318                 V_rip6stat.rip6s_nosock++;
  319                 if (m->m_flags & M_MCAST)
  320                         V_rip6stat.rip6s_nosockmcast++;
  321                 if (proto == IPPROTO_NONE)
  322                         m_freem(m);
  323                 else {
  324                         char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
  325                         icmp6_error(m, ICMP6_PARAM_PROB,
  326                             ICMP6_PARAMPROB_NEXTHEADER,
  327                             prvnxtp - mtod(m, char *));
  328                 }
  329                 V_ip6stat.ip6s_delivered--;
  330         }
  331         return (IPPROTO_DONE);
  332 }
  333 
  334 void
  335 rip6_ctlinput(int cmd, struct sockaddr *sa, void *d)
  336 {
  337         struct ip6_hdr *ip6;
  338         struct mbuf *m;
  339         int off = 0;
  340         struct ip6ctlparam *ip6cp = NULL;
  341         const struct sockaddr_in6 *sa6_src = NULL;
  342         void *cmdarg;
  343         struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange;
  344 
  345         if (sa->sa_family != AF_INET6 ||
  346             sa->sa_len != sizeof(struct sockaddr_in6))
  347                 return;
  348 
  349         if ((unsigned)cmd >= PRC_NCMDS)
  350                 return;
  351         if (PRC_IS_REDIRECT(cmd))
  352                 notify = in6_rtchange, d = NULL;
  353         else if (cmd == PRC_HOSTDEAD)
  354                 d = NULL;
  355         else if (inet6ctlerrmap[cmd] == 0)
  356                 return;
  357 
  358         /*
  359          * If the parameter is from icmp6, decode it.
  360          */
  361         if (d != NULL) {
  362                 ip6cp = (struct ip6ctlparam *)d;
  363                 m = ip6cp->ip6c_m;
  364                 ip6 = ip6cp->ip6c_ip6;
  365                 off = ip6cp->ip6c_off;
  366                 cmdarg = ip6cp->ip6c_cmdarg;
  367                 sa6_src = ip6cp->ip6c_src;
  368         } else {
  369                 m = NULL;
  370                 ip6 = NULL;
  371                 cmdarg = NULL;
  372                 sa6_src = &sa6_any;
  373         }
  374 
  375         (void) in6_pcbnotify(&V_ripcbinfo, sa, 0,
  376             (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
  377 }
  378 
  379 /*
  380  * Generate IPv6 header and pass packet to ip6_output.  Tack on options user
  381  * may have setup with control call.
  382  */
  383 int
  384 #if __STDC__
  385 rip6_output(struct mbuf *m, ...)
  386 #else
  387 rip6_output(m, va_alist)
  388         struct mbuf *m;
  389         va_dcl
  390 #endif
  391 {
  392         struct mbuf *control;
  393         struct socket *so;
  394         struct sockaddr_in6 *dstsock;
  395         struct in6_addr *dst;
  396         struct ip6_hdr *ip6;
  397         struct inpcb *in6p;
  398         u_int   plen = m->m_pkthdr.len;
  399         int error = 0;
  400         struct ip6_pktopts opt, *optp;
  401         struct ifnet *oifp = NULL;
  402         int type = 0, code = 0;         /* for ICMPv6 output statistics only */
  403         int scope_ambiguous = 0;
  404         int use_defzone = 0;
  405         struct in6_addr in6a;
  406         va_list ap;
  407 
  408         va_start(ap, m);
  409         so = va_arg(ap, struct socket *);
  410         dstsock = va_arg(ap, struct sockaddr_in6 *);
  411         control = va_arg(ap, struct mbuf *);
  412         va_end(ap);
  413 
  414         in6p = sotoinpcb(so);
  415         INP_WLOCK(in6p);
  416 
  417         dst = &dstsock->sin6_addr;
  418         if (control != NULL) {
  419                 if ((error = ip6_setpktopts(control, &opt,
  420                     in6p->in6p_outputopts, so->so_cred,
  421                     so->so_proto->pr_protocol)) != 0) {
  422                         goto bad;
  423                 }
  424                 optp = &opt;
  425         } else
  426                 optp = in6p->in6p_outputopts;
  427 
  428         /*
  429          * Check and convert scope zone ID into internal form.
  430          *
  431          * XXX: we may still need to determine the zone later.
  432          */
  433         if (!(so->so_state & SS_ISCONNECTED)) {
  434                 if (!optp || !optp->ip6po_pktinfo ||
  435                     !optp->ip6po_pktinfo->ipi6_ifindex)
  436                         use_defzone = V_ip6_use_defzone;
  437                 if (dstsock->sin6_scope_id == 0 && !use_defzone)
  438                         scope_ambiguous = 1;
  439                 if ((error = sa6_embedscope(dstsock, use_defzone)) != 0)
  440                         goto bad;
  441         }
  442 
  443         /*
  444          * For an ICMPv6 packet, we should know its type and code to update
  445          * statistics.
  446          */
  447         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
  448                 struct icmp6_hdr *icmp6;
  449                 if (m->m_len < sizeof(struct icmp6_hdr) &&
  450                     (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
  451                         error = ENOBUFS;
  452                         goto bad;
  453                 }
  454                 icmp6 = mtod(m, struct icmp6_hdr *);
  455                 type = icmp6->icmp6_type;
  456                 code = icmp6->icmp6_code;
  457         }
  458 
  459         M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
  460         if (m == NULL) {
  461                 error = ENOBUFS;
  462                 goto bad;
  463         }
  464         ip6 = mtod(m, struct ip6_hdr *);
  465 
  466         /*
  467          * Source address selection.
  468          */
  469         error = in6_selectsrc(dstsock, optp, in6p, NULL, so->so_cred,
  470             &oifp, &in6a);
  471         if (error)
  472                 goto bad;
  473         error = prison_check_ip6(in6p->inp_cred, &in6a);
  474         if (error != 0)
  475                 goto bad;
  476         ip6->ip6_src = in6a;
  477 
  478         if (oifp && scope_ambiguous) {
  479                 /*
  480                  * Application should provide a proper zone ID or the use of
  481                  * default zone IDs should be enabled.  Unfortunately, some
  482                  * applications do not behave as it should, so we need a
  483                  * workaround.  Even if an appropriate ID is not determined
  484                  * (when it's required), if we can determine the outgoing
  485                  * interface. determine the zone ID based on the interface.
  486                  */
  487                 error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
  488                 if (error != 0)
  489                         goto bad;
  490         }
  491         ip6->ip6_dst = dstsock->sin6_addr;
  492 
  493         /*
  494          * Fill in the rest of the IPv6 header fields.
  495          */
  496         ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
  497             (in6p->inp_flow & IPV6_FLOWINFO_MASK);
  498         ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
  499             (IPV6_VERSION & IPV6_VERSION_MASK);
  500 
  501         /*
  502          * ip6_plen will be filled in ip6_output, so not fill it here.
  503          */
  504         ip6->ip6_nxt = in6p->inp_ip_p;
  505         ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
  506 
  507         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
  508             in6p->in6p_cksum != -1) {
  509                 struct mbuf *n;
  510                 int off;
  511                 u_int16_t *p;
  512 
  513                 /* Compute checksum. */
  514                 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
  515                         off = offsetof(struct icmp6_hdr, icmp6_cksum);
  516                 else
  517                         off = in6p->in6p_cksum;
  518                 if (plen < off + 1) {
  519                         error = EINVAL;
  520                         goto bad;
  521                 }
  522                 off += sizeof(struct ip6_hdr);
  523 
  524                 n = m;
  525                 while (n && n->m_len <= off) {
  526                         off -= n->m_len;
  527                         n = n->m_next;
  528                 }
  529                 if (!n)
  530                         goto bad;
  531                 p = (u_int16_t *)(mtod(n, caddr_t) + off);
  532                 *p = 0;
  533                 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
  534         }
  535 
  536         error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
  537         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
  538                 if (oifp)
  539                         icmp6_ifoutstat_inc(oifp, type, code);
  540                 ICMP6STAT_INC(icp6s_outhist[type]);
  541         } else
  542                 V_rip6stat.rip6s_opackets++;
  543 
  544         goto freectl;
  545 
  546  bad:
  547         if (m)
  548                 m_freem(m);
  549 
  550  freectl:
  551         if (control != NULL) {
  552                 ip6_clearpktopts(&opt, -1);
  553                 m_freem(control);
  554         }
  555         INP_WUNLOCK(in6p);
  556         return (error);
  557 }
  558 
  559 /*
  560  * Raw IPv6 socket option processing.
  561  */
  562 int
  563 rip6_ctloutput(struct socket *so, struct sockopt *sopt)
  564 {
  565         int error;
  566 
  567         if (sopt->sopt_level == IPPROTO_ICMPV6)
  568                 /*
  569                  * XXX: is it better to call icmp6_ctloutput() directly
  570                  * from protosw?
  571                  */
  572                 return (icmp6_ctloutput(so, sopt));
  573         else if (sopt->sopt_level != IPPROTO_IPV6)
  574                 return (EINVAL);
  575 
  576         error = 0;
  577 
  578         switch (sopt->sopt_dir) {
  579         case SOPT_GET:
  580                 switch (sopt->sopt_name) {
  581                 case MRT6_INIT:
  582                 case MRT6_DONE:
  583                 case MRT6_ADD_MIF:
  584                 case MRT6_DEL_MIF:
  585                 case MRT6_ADD_MFC:
  586                 case MRT6_DEL_MFC:
  587                 case MRT6_PIM:
  588                         error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
  589                             EOPNOTSUPP;
  590                         break;
  591                 case IPV6_CHECKSUM:
  592                         error = ip6_raw_ctloutput(so, sopt);
  593                         break;
  594                 default:
  595                         error = ip6_ctloutput(so, sopt);
  596                         break;
  597                 }
  598                 break;
  599 
  600         case SOPT_SET:
  601                 switch (sopt->sopt_name) {
  602                 case MRT6_INIT:
  603                 case MRT6_DONE:
  604                 case MRT6_ADD_MIF:
  605                 case MRT6_DEL_MIF:
  606                 case MRT6_ADD_MFC:
  607                 case MRT6_DEL_MFC:
  608                 case MRT6_PIM:
  609                         error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
  610                             EOPNOTSUPP;
  611                         break;
  612                 case IPV6_CHECKSUM:
  613                         error = ip6_raw_ctloutput(so, sopt);
  614                         break;
  615                 default:
  616                         error = ip6_ctloutput(so, sopt);
  617                         break;
  618                 }
  619                 break;
  620         }
  621 
  622         return (error);
  623 }
  624 
  625 static int
  626 rip6_attach(struct socket *so, int proto, struct thread *td)
  627 {
  628         struct inpcb *inp;
  629         struct icmp6_filter *filter;
  630         int error;
  631 
  632         inp = sotoinpcb(so);
  633         KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
  634 
  635         error = priv_check(td, PRIV_NETINET_RAW);
  636         if (error)
  637                 return (error);
  638         error = soreserve(so, rip_sendspace, rip_recvspace);
  639         if (error)
  640                 return (error);
  641         filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
  642         if (filter == NULL)
  643                 return (ENOMEM);
  644         INP_INFO_WLOCK(&V_ripcbinfo);
  645         error = in_pcballoc(so, &V_ripcbinfo);
  646         if (error) {
  647                 INP_INFO_WUNLOCK(&V_ripcbinfo);
  648                 free(filter, M_PCB);
  649                 return (error);
  650         }
  651         inp = (struct inpcb *)so->so_pcb;
  652         INP_INFO_WUNLOCK(&V_ripcbinfo);
  653         inp->inp_vflag |= INP_IPV6;
  654         inp->inp_ip_p = (long)proto;
  655         inp->in6p_hops = -1;    /* use kernel default */
  656         inp->in6p_cksum = -1;
  657         inp->in6p_icmp6filt = filter;
  658         ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
  659         INP_WUNLOCK(inp);
  660         return (0);
  661 }
  662 
  663 static void
  664 rip6_detach(struct socket *so)
  665 {
  666         struct inpcb *inp;
  667 
  668         inp = sotoinpcb(so);
  669         KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
  670 
  671         if (so == V_ip6_mrouter && ip6_mrouter_done)
  672                 ip6_mrouter_done();
  673         /* xxx: RSVP */
  674         INP_INFO_WLOCK(&V_ripcbinfo);
  675         INP_WLOCK(inp);
  676         free(inp->in6p_icmp6filt, M_PCB);
  677         in_pcbdetach(inp);
  678         in_pcbfree(inp);
  679         INP_INFO_WUNLOCK(&V_ripcbinfo);
  680 }
  681 
  682 /* XXXRW: This can't ever be called. */
  683 static void
  684 rip6_abort(struct socket *so)
  685 {
  686         struct inpcb *inp;
  687 
  688         inp = sotoinpcb(so);
  689         KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
  690 
  691         soisdisconnected(so);
  692 }
  693 
  694 static void
  695 rip6_close(struct socket *so)
  696 {
  697         struct inpcb *inp;
  698 
  699         inp = sotoinpcb(so);
  700         KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
  701 
  702         soisdisconnected(so);
  703 }
  704 
  705 static int
  706 rip6_disconnect(struct socket *so)
  707 {
  708         struct inpcb *inp;
  709 
  710         inp = sotoinpcb(so);
  711         KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL"));
  712 
  713         if ((so->so_state & SS_ISCONNECTED) == 0)
  714                 return (ENOTCONN);
  715         inp->in6p_faddr = in6addr_any;
  716         rip6_abort(so);
  717         return (0);
  718 }
  719 
  720 static int
  721 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
  722 {
  723         struct inpcb *inp;
  724         struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
  725         struct ifaddr *ifa = NULL;
  726         int error = 0;
  727 
  728         inp = sotoinpcb(so);
  729         KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
  730 
  731         if (nam->sa_len != sizeof(*addr))
  732                 return (EINVAL);
  733         if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0)
  734                 return (error);
  735         if (TAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6)
  736                 return (EADDRNOTAVAIL);
  737         if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
  738                 return (error);
  739 
  740         if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
  741             (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL)
  742                 return (EADDRNOTAVAIL);
  743         if (ifa != NULL &&
  744             ((struct in6_ifaddr *)ifa)->ia6_flags &
  745             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
  746              IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
  747                 ifa_free(ifa);
  748                 return (EADDRNOTAVAIL);
  749         }
  750         if (ifa != NULL)
  751                 ifa_free(ifa);
  752         INP_INFO_WLOCK(&V_ripcbinfo);
  753         INP_WLOCK(inp);
  754         inp->in6p_laddr = addr->sin6_addr;
  755         INP_WUNLOCK(inp);
  756         INP_INFO_WUNLOCK(&V_ripcbinfo);
  757         return (0);
  758 }
  759 
  760 static int
  761 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
  762 {
  763         struct inpcb *inp;
  764         struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
  765         struct in6_addr in6a;
  766         struct ifnet *ifp = NULL;
  767         int error = 0, scope_ambiguous = 0;
  768 
  769         inp = sotoinpcb(so);
  770         KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
  771 
  772         if (nam->sa_len != sizeof(*addr))
  773                 return (EINVAL);
  774         if (TAILQ_EMPTY(&V_ifnet))
  775                 return (EADDRNOTAVAIL);
  776         if (addr->sin6_family != AF_INET6)
  777                 return (EAFNOSUPPORT);
  778 
  779         /*
  780          * Application should provide a proper zone ID or the use of default
  781          * zone IDs should be enabled.  Unfortunately, some applications do
  782          * not behave as it should, so we need a workaround.  Even if an
  783          * appropriate ID is not determined, we'll see if we can determine
  784          * the outgoing interface.  If we can, determine the zone ID based on
  785          * the interface below.
  786          */
  787         if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone)
  788                 scope_ambiguous = 1;
  789         if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
  790                 return (error);
  791 
  792         INP_INFO_WLOCK(&V_ripcbinfo);
  793         INP_WLOCK(inp);
  794         /* Source address selection. XXX: need pcblookup? */
  795         error = in6_selectsrc(addr, inp->in6p_outputopts,
  796             inp, NULL, so->so_cred, &ifp, &in6a);
  797         if (error) {
  798                 INP_WUNLOCK(inp);
  799                 INP_INFO_WUNLOCK(&V_ripcbinfo);
  800                 return (error);
  801         }
  802 
  803         /* XXX: see above */
  804         if (ifp && scope_ambiguous &&
  805             (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
  806                 INP_WUNLOCK(inp);
  807                 INP_INFO_WUNLOCK(&V_ripcbinfo);
  808                 return (error);
  809         }
  810         inp->in6p_faddr = addr->sin6_addr;
  811         inp->in6p_laddr = in6a;
  812         soisconnected(so);
  813         INP_WUNLOCK(inp);
  814         INP_INFO_WUNLOCK(&V_ripcbinfo);
  815         return (0);
  816 }
  817 
  818 static int
  819 rip6_shutdown(struct socket *so)
  820 {
  821         struct inpcb *inp;
  822 
  823         inp = sotoinpcb(so);
  824         KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL"));
  825 
  826         INP_WLOCK(inp);
  827         socantsendmore(so);
  828         INP_WUNLOCK(inp);
  829         return (0);
  830 }
  831 
  832 static int
  833 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
  834     struct mbuf *control, struct thread *td)
  835 {
  836         struct inpcb *inp;
  837         struct sockaddr_in6 tmp;
  838         struct sockaddr_in6 *dst;
  839         int ret;
  840 
  841         inp = sotoinpcb(so);
  842         KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
  843 
  844         /* Always copy sockaddr to avoid overwrites. */
  845         /* Unlocked read. */
  846         if (so->so_state & SS_ISCONNECTED) {
  847                 if (nam) {
  848                         m_freem(m);
  849                         return (EISCONN);
  850                 }
  851                 /* XXX */
  852                 bzero(&tmp, sizeof(tmp));
  853                 tmp.sin6_family = AF_INET6;
  854                 tmp.sin6_len = sizeof(struct sockaddr_in6);
  855                 INP_RLOCK(inp);
  856                 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
  857                     sizeof(struct in6_addr));
  858                 INP_RUNLOCK(inp);
  859                 dst = &tmp;
  860         } else {
  861                 if (nam == NULL) {
  862                         m_freem(m);
  863                         return (ENOTCONN);
  864                 }
  865                 if (nam->sa_len != sizeof(struct sockaddr_in6)) {
  866                         m_freem(m);
  867                         return (EINVAL);
  868                 }
  869                 tmp = *(struct sockaddr_in6 *)nam;
  870                 dst = &tmp;
  871 
  872                 if (dst->sin6_family == AF_UNSPEC) {
  873                         /*
  874                          * XXX: we allow this case for backward
  875                          * compatibility to buggy applications that
  876                          * rely on old (and wrong) kernel behavior.
  877                          */
  878                         log(LOG_INFO, "rip6 SEND: address family is "
  879                             "unspec. Assume AF_INET6\n");
  880                         dst->sin6_family = AF_INET6;
  881                 } else if (dst->sin6_family != AF_INET6) {
  882                         m_freem(m);
  883                         return(EAFNOSUPPORT);
  884                 }
  885         }
  886         ret = rip6_output(m, so, dst, control);
  887         return (ret);
  888 }
  889 
  890 struct pr_usrreqs rip6_usrreqs = {
  891         .pru_abort =            rip6_abort,
  892         .pru_attach =           rip6_attach,
  893         .pru_bind =             rip6_bind,
  894         .pru_connect =          rip6_connect,
  895         .pru_control =          in6_control,
  896         .pru_detach =           rip6_detach,
  897         .pru_disconnect =       rip6_disconnect,
  898         .pru_peeraddr =         in6_getpeeraddr,
  899         .pru_send =             rip6_send,
  900         .pru_shutdown =         rip6_shutdown,
  901         .pru_sockaddr =         in6_getsockaddr,
  902         .pru_close =            rip6_close,
  903 };

Cache object: c47c9d60ca6c498184ff9fb1978c2e8e


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