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/bsd/netinet6/raw_ip6.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    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  * $FreeBSD: src/sys/netinet6/raw_ip6.c,v 1.7.2.4 2001/07/29 19:32:40 ume Exp $
   30  */
   31 
   32 /*
   33  * Copyright (c) 1982, 1986, 1988, 1993
   34  *      The Regents of the University of California.  All rights reserved.
   35  *
   36  * Redistribution and use in source and binary forms, with or without
   37  * modification, are permitted provided that the following conditions
   38  * are met:
   39  * 1. Redistributions of source code must retain the above copyright
   40  *    notice, this list of conditions and the following disclaimer.
   41  * 2. Redistributions in binary form must reproduce the above copyright
   42  *    notice, this list of conditions and the following disclaimer in the
   43  *    documentation and/or other materials provided with the distribution.
   44  * 3. All advertising materials mentioning features or use of this software
   45  *    must display the following acknowledgement:
   46  *      This product includes software developed by the University of
   47  *      California, Berkeley and its contributors.
   48  * 4. Neither the name of the University nor the names of its contributors
   49  *    may be used to endorse or promote products derived from this software
   50  *    without specific prior written permission.
   51  *
   52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   62  * SUCH DAMAGE.
   63  *
   64  *      @(#)raw_ip.c    8.2 (Berkeley) 1/4/94
   65  */
   66 #include <sys/param.h>
   67 #include <sys/malloc.h>
   68 #include <sys/proc.h>
   69 #include <sys/mbuf.h>
   70 #include <sys/socket.h>
   71 #include <sys/protosw.h>
   72 #include <sys/socketvar.h>
   73 #include <sys/errno.h>
   74 #include <sys/systm.h>
   75 
   76 #include <net/if.h>
   77 #include <net/route.h>
   78 #include <net/if_types.h>
   79 
   80 #include <netinet/in.h>
   81 #include <netinet/in_var.h>
   82 #include <netinet/in_systm.h>
   83 #include <netinet/ip6.h>
   84 #include <netinet6/ip6_var.h>
   85 #include <netinet6/ip6_mroute.h>
   86 #include <netinet/icmp6.h>
   87 #include <netinet/in_pcb.h>
   88 #include <netinet6/in6_pcb.h>
   89 #include <netinet6/nd6.h>
   90 #include <netinet6/ip6protosw.h>
   91 #if ENABLE_DEFAULT_SCOPE
   92 #include <netinet6/scope6_var.h>
   93 #endif
   94 #include <netinet6/raw_ip6.h>
   95 
   96 #if IPSEC
   97 #include <netinet6/ipsec.h>
   98 #include <netinet6/ipsec6.h>
   99 extern int ipsec_bypass;
  100 #endif /*IPSEC*/
  101 
  102 
  103 #include "faith.h"
  104 #if defined(NFAITH) && 0 < NFAITH
  105 #include <net/if_faith.h>
  106 #endif
  107 
  108 #define satosin6(sa)    ((struct sockaddr_in6 *)(sa))
  109 #define ifatoia6(ifa)   ((struct in6_ifaddr *)(ifa))
  110 
  111 /*
  112  * Raw interface to IP6 protocol.
  113  */
  114 
  115 extern struct   inpcbhead ripcb;
  116 extern struct   inpcbinfo ripcbinfo;
  117 extern u_long   rip_sendspace;
  118 extern u_long   rip_recvspace;
  119 
  120 struct rip6stat rip6stat;
  121 
  122 /*
  123  * Setup generic address and protocol structures
  124  * for raw_input routine, then pass them along with
  125  * mbuf chain.
  126  */
  127 int
  128 rip6_input(mp, offp)
  129         struct  mbuf **mp;
  130         int     *offp;
  131 {
  132         struct mbuf *m = *mp;
  133         register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
  134         register struct inpcb *in6p;
  135         struct inpcb *last = 0;
  136         struct mbuf *opts = NULL;
  137         struct sockaddr_in6 rip6src;
  138         int proto = ip6->ip6_nxt;
  139 
  140         rip6stat.rip6s_ipackets++;
  141 
  142 #if defined(NFAITH) && 0 < NFAITH
  143         if (faithprefix(&ip6->ip6_dst)) {
  144                 /* XXX send icmp6 host/port unreach? */
  145                 m_freem(m);
  146                 return IPPROTO_DONE;
  147         }
  148 #endif
  149 
  150         init_sin6(&rip6src, m); /* general init */
  151 
  152         LIST_FOREACH(in6p, &ripcb, inp_list) {
  153                 if ((in6p->in6p_vflag & INP_IPV6) == 0)
  154                         continue;
  155                 if (in6p->in6p_ip6_nxt &&
  156                     in6p->in6p_ip6_nxt != proto)
  157                         continue;
  158                 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
  159                     !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
  160                         continue;
  161                 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
  162                     !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
  163                         continue;
  164                 if (in6p->in6p_cksum != -1) {
  165                         rip6stat.rip6s_isum++;
  166                         if (in6_cksum(m, ip6->ip6_nxt, *offp,
  167                             m->m_pkthdr.len - *offp)) {
  168                                 rip6stat.rip6s_badsum++;
  169                                 continue;
  170                         }
  171                 }
  172                 if (last) {
  173                         struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
  174 
  175 #if IPSEC
  176                         /*
  177                          * Check AH/ESP integrity.
  178                          */
  179                         if (ipsec_bypass == 0 && n && ipsec6_in_reject_so(n, last->inp_socket)) {
  180                                 m_freem(n);
  181                                 ipsec6stat.in_polvio++;
  182                                 /* do not inject data into pcb */
  183                         } else
  184 #endif /*IPSEC*/
  185                         if (n) {
  186                                 if (last->in6p_flags & IN6P_CONTROLOPTS ||
  187                                     last->in6p_socket->so_options & SO_TIMESTAMP)
  188                                         ip6_savecontrol(last, &opts, ip6, n);
  189                                 /* strip intermediate headers */
  190                                 m_adj(n, *offp);
  191                                 if (sbappendaddr(&last->in6p_socket->so_rcv,
  192                                                 (struct sockaddr *)&rip6src,
  193                                                  n, opts) == 0) {
  194                                         m_freem(n);
  195                                         if (opts)
  196                                                 m_freem(opts);
  197                                         rip6stat.rip6s_fullsock++;
  198                                 } else
  199                                         sorwakeup(last->in6p_socket);
  200                                 opts = NULL;
  201                         }
  202                 }
  203                 last = in6p;
  204         }
  205 #if IPSEC
  206         /*
  207          * Check AH/ESP integrity.
  208          */
  209         if (ipsec_bypass == 0 && last && ipsec6_in_reject_so(m, last->inp_socket)) {
  210                 m_freem(m);
  211                 ipsec6stat.in_polvio++;
  212                 ip6stat.ip6s_delivered--;
  213                 /* do not inject data into pcb */
  214         } else
  215 #endif /*IPSEC*/
  216         if (last) {
  217                 if (last->in6p_flags & IN6P_CONTROLOPTS ||
  218                     last->in6p_socket->so_options & SO_TIMESTAMP)
  219                         ip6_savecontrol(last, &opts, ip6, m);
  220                 /* strip intermediate headers */
  221                 m_adj(m, *offp);
  222                 if (sbappendaddr(&last->in6p_socket->so_rcv,
  223                                 (struct sockaddr *)&rip6src, m, opts) == 0) {
  224                         m_freem(m);
  225                         if (opts)
  226                                 m_freem(opts);
  227                         rip6stat.rip6s_fullsock++;
  228                 } else
  229                         sorwakeup(last->in6p_socket);
  230         } else {
  231                 rip6stat.rip6s_nosock++;
  232                 if (m->m_flags & M_MCAST)
  233                         rip6stat.rip6s_nosockmcast++;
  234                 if (proto == IPPROTO_NONE)
  235                         m_freem(m);
  236                 else {
  237                         char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
  238                         icmp6_error(m, ICMP6_PARAM_PROB,
  239                                     ICMP6_PARAMPROB_NEXTHEADER,
  240                                     prvnxtp - mtod(m, char *));
  241                 }
  242                 ip6stat.ip6s_delivered--;
  243         }
  244         return IPPROTO_DONE;
  245 }
  246 
  247 void
  248 rip6_ctlinput(cmd, sa, d)
  249         int cmd;
  250         struct sockaddr *sa;
  251         void *d;
  252 {
  253         struct ip6_hdr *ip6;
  254         struct mbuf *m;
  255         int off = 0;
  256         struct ip6ctlparam *ip6cp = NULL;
  257         const struct sockaddr_in6 *sa6_src = NULL;
  258         void (*notify) __P((struct inpcb *, int)) = in6_rtchange;
  259 
  260         if (sa->sa_family != AF_INET6 ||
  261             sa->sa_len != sizeof(struct sockaddr_in6))
  262                 return;
  263 
  264         if ((unsigned)cmd >= PRC_NCMDS)
  265                 return;
  266         if (PRC_IS_REDIRECT(cmd))
  267                 notify = in6_rtchange, d = NULL;
  268         else if (cmd == PRC_HOSTDEAD)
  269                 d = NULL;
  270         else if (inet6ctlerrmap[cmd] == 0)
  271                 return;
  272 
  273         /* if the parameter is from icmp6, decode it. */
  274         if (d != NULL) {
  275                 ip6cp = (struct ip6ctlparam *)d;
  276                 m = ip6cp->ip6c_m;
  277                 ip6 = ip6cp->ip6c_ip6;
  278                 off = ip6cp->ip6c_off;
  279                 sa6_src = ip6cp->ip6c_src;
  280         } else {
  281                 m = NULL;
  282                 ip6 = NULL;
  283                 sa6_src = &sa6_any;
  284         }
  285 
  286         (void) in6_pcbnotify(&ripcb, sa, 0, (struct sockaddr *)sa6_src,
  287                              0, cmd, notify);
  288 }
  289 
  290 /*
  291  * Generate IPv6 header and pass packet to ip6_output.
  292  * Tack on options user may have setup with control call.
  293  */
  294 int
  295 rip6_output(m, so, dstsock, control)
  296         register struct mbuf *m;
  297         struct socket *so;
  298         struct sockaddr_in6 *dstsock;
  299         struct mbuf *control;
  300 {
  301         struct in6_addr *dst;
  302         struct ip6_hdr *ip6;
  303         struct inpcb *in6p;
  304         u_int   plen = m->m_pkthdr.len;
  305         int error = 0;
  306         struct ip6_pktopts opt, *optp = 0;
  307         struct ifnet *oifp = NULL;
  308         int type = 0, code = 0;         /* for ICMPv6 output statistics only */
  309         int priv = 0;
  310 
  311         in6p = sotoin6pcb(so);
  312 
  313         priv = 0;
  314         if (so->so_uid == 0)
  315                 priv = 1;
  316         dst = &dstsock->sin6_addr;
  317         if (control) {
  318                 if ((error = ip6_setpktoptions(control, &opt, priv, 0)) != 0)
  319                         goto bad;
  320                 optp = &opt;
  321         } else
  322                 optp = in6p->in6p_outputopts;
  323 
  324         /*
  325          * For an ICMPv6 packet, we should know its type and code
  326          * to update statistics.
  327          */
  328         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
  329                 struct icmp6_hdr *icmp6;
  330                 if (m->m_len < sizeof(struct icmp6_hdr) &&
  331                     (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
  332                         error = ENOBUFS;
  333                         goto bad;
  334                 }
  335                 icmp6 = mtod(m, struct icmp6_hdr *);
  336                 type = icmp6->icmp6_type;
  337                 code = icmp6->icmp6_code;
  338         }
  339 
  340         M_PREPEND(m, sizeof(*ip6), M_WAIT);
  341         ip6 = mtod(m, struct ip6_hdr *);
  342 
  343         /*
  344          * Next header might not be ICMP6 but use its pseudo header anyway.
  345          */
  346         ip6->ip6_dst = *dst;
  347 
  348         /*
  349          * If the scope of the destination is link-local, embed the interface
  350          * index in the address.
  351          *
  352          * XXX advanced-api value overrides sin6_scope_id
  353          */
  354         if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
  355                 struct in6_pktinfo *pi;
  356 
  357                 /*
  358                  * XXX Boundary check is assumed to be already done in
  359                  * ip6_setpktoptions().
  360                  */
  361                 if (optp && (pi = optp->ip6po_pktinfo) && pi->ipi6_ifindex) {
  362                         ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
  363                         oifp = ifindex2ifnet[pi->ipi6_ifindex];
  364                 } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
  365                          in6p->in6p_moptions &&
  366                          in6p->in6p_moptions->im6o_multicast_ifp) {
  367                         oifp = in6p->in6p_moptions->im6o_multicast_ifp;
  368                         ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
  369                 } else if (dstsock->sin6_scope_id) {
  370                         /* boundary check */
  371                         if (dstsock->sin6_scope_id < 0
  372                          || if_index < dstsock->sin6_scope_id) {
  373                                 error = ENXIO;  /* XXX EINVAL? */
  374                                 goto bad;
  375                         }
  376                         ip6->ip6_dst.s6_addr16[1]
  377                                 = htons(dstsock->sin6_scope_id & 0xffff);/*XXX*/
  378                 }
  379         }
  380 
  381         /*
  382          * Source address selection.
  383          */
  384         {
  385                 struct in6_addr *in6a;
  386 
  387                 if ((in6a = in6_selectsrc(dstsock, optp,
  388                                           in6p->in6p_moptions,
  389                                           &in6p->in6p_route,
  390                                           &in6p->in6p_laddr,
  391                                           &error)) == 0) {
  392                         if (error == 0)
  393                                 error = EADDRNOTAVAIL;
  394                         goto bad;
  395                 }
  396                 ip6->ip6_src = *in6a;
  397                 if (in6p->in6p_route.ro_rt)
  398                         oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index];
  399         }
  400         ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
  401                 (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
  402         ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
  403                 (IPV6_VERSION & IPV6_VERSION_MASK);
  404         /* ip6_plen will be filled in ip6_output, so not fill it here. */
  405         ip6->ip6_nxt = in6p->in6p_ip6_nxt;
  406         ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
  407 
  408         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
  409             in6p->in6p_cksum != -1) {
  410                 struct mbuf *n;
  411                 int off;
  412                 u_int16_t *p;
  413 
  414                 /* compute checksum */
  415                 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
  416                         off = offsetof(struct icmp6_hdr, icmp6_cksum);
  417                 else
  418                         off = in6p->in6p_cksum;
  419                 if (plen < off + 1) {
  420                         error = EINVAL;
  421                         goto bad;
  422                 }
  423                 off += sizeof(struct ip6_hdr);
  424 
  425                 n = m;
  426                 while (n && n->m_len <= off) {
  427                         off -= n->m_len;
  428                         n = n->m_next;
  429                 }
  430                 if (!n)
  431                         goto bad;
  432                 p = (u_int16_t *)(mtod(n, caddr_t) + off);
  433                 *p = 0;
  434                 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
  435         }
  436 
  437 #if IPSEC
  438         if (ipsec_bypass == 0 && ipsec_setsocket(m, so) != 0) {
  439                 error = ENOBUFS;
  440                 goto bad;
  441         }
  442 #endif /*IPSEC*/
  443 
  444         error = ip6_output(m, optp, &in6p->in6p_route, 0,
  445                            in6p->in6p_moptions, &oifp);
  446         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
  447                 if (oifp)
  448                         icmp6_ifoutstat_inc(oifp, type, code);
  449                 icmp6stat.icp6s_outhist[type]++;
  450         } else
  451                 rip6stat.rip6s_opackets++;
  452 
  453         goto freectl;
  454 
  455  bad:
  456         if (m)
  457                 m_freem(m);
  458 
  459  freectl:
  460         if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt)
  461                 rtfree(optp->ip6po_route.ro_rt);
  462         if (control) {
  463                 if (optp == &opt)
  464                         ip6_clearpktopts(optp, 0, -1);
  465                 m_freem(control);
  466         }
  467         return(error);
  468 }
  469 
  470 /*
  471  * Raw IPv6 socket option processing.
  472  */
  473 int
  474 rip6_ctloutput(so, sopt)
  475         struct socket *so;
  476         struct sockopt *sopt;
  477 {
  478         int error;
  479 
  480         if (sopt->sopt_level == IPPROTO_ICMPV6)
  481                 /*
  482                  * XXX: is it better to call icmp6_ctloutput() directly
  483                  * from protosw?
  484                  */
  485                 return(icmp6_ctloutput(so, sopt));
  486         else if (sopt->sopt_level != IPPROTO_IPV6)
  487                 return (EINVAL);
  488 
  489         error = 0;
  490 
  491         switch (sopt->sopt_dir) {
  492         case SOPT_GET:
  493                 switch (sopt->sopt_name) {
  494                 case MRT6_INIT:
  495                 case MRT6_DONE:
  496                 case MRT6_ADD_MIF:
  497                 case MRT6_DEL_MIF:
  498                 case MRT6_ADD_MFC:
  499                 case MRT6_DEL_MFC:
  500                 case MRT6_PIM:
  501                         error = ip6_mrouter_get(so, sopt);
  502                         break;
  503                 default:
  504                         error = ip6_ctloutput(so, sopt);
  505                         break;
  506                 }
  507                 break;
  508 
  509         case SOPT_SET:
  510                 switch (sopt->sopt_name) {
  511                 case MRT6_INIT:
  512                 case MRT6_DONE:
  513                 case MRT6_ADD_MIF:
  514                 case MRT6_DEL_MIF:
  515                 case MRT6_ADD_MFC:
  516                 case MRT6_DEL_MFC:
  517                 case MRT6_PIM:
  518                         error = ip6_mrouter_set(so, sopt);
  519                         break;
  520                 default:
  521                         error = ip6_ctloutput(so, sopt);
  522                         break;
  523                 }
  524                 break;
  525         }
  526 
  527         return (error);
  528 }
  529 
  530 static int
  531 rip6_attach(struct socket *so, int proto, struct proc *p)
  532 {
  533         struct inpcb *inp;
  534         int error, s;
  535 
  536         inp = sotoinpcb(so);
  537         if (inp)
  538                 panic("rip6_attach");
  539         if (p && (error = suser(p->p_ucred, &p->p_acflag)) != 0)
  540                 return error;
  541 
  542         error = soreserve(so, rip_sendspace, rip_recvspace);
  543         if (error)
  544                 return error;
  545         s = splnet();
  546         error = in_pcballoc(so, &ripcbinfo, p);
  547         splx(s);
  548         if (error)
  549                 return error;
  550         inp = (struct inpcb *)so->so_pcb;
  551         inp->inp_vflag |= INP_IPV6;
  552         inp->in6p_ip6_nxt = (long)proto;
  553         inp->in6p_hops = -1;    /* use kernel default */
  554         inp->in6p_cksum = -1;
  555         MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *,
  556                sizeof(struct icmp6_filter), M_PCB, M_WAITOK);
  557         if (inp->in6p_icmp6filt == NULL)
  558                 return (ENOMEM);
  559         ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
  560         return 0;
  561 }
  562 
  563 static int
  564 rip6_detach(struct socket *so)
  565 {
  566         struct inpcb *inp;
  567 
  568         inp = sotoinpcb(so);
  569         if (inp == 0)
  570                 panic("rip6_detach");
  571         /* xxx: RSVP */
  572         if (so == ip6_mrouter)
  573                 ip6_mrouter_done();
  574         if (inp->in6p_icmp6filt) {
  575                 FREE(inp->in6p_icmp6filt, M_PCB);
  576                 inp->in6p_icmp6filt = NULL;
  577         }
  578         in6_pcbdetach(inp);
  579         return 0;
  580 }
  581 
  582 static int
  583 rip6_abort(struct socket *so)
  584 {
  585         soisdisconnected(so);
  586         return rip6_detach(so);
  587 }
  588 
  589 static int
  590 rip6_disconnect(struct socket *so)
  591 {
  592         struct inpcb *inp = sotoinpcb(so);
  593 
  594         if ((so->so_state & SS_ISCONNECTED) == 0)
  595                 return ENOTCONN;
  596         inp->in6p_faddr = in6addr_any;
  597         return rip6_abort(so);
  598 }
  599 
  600 static int
  601 rip6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
  602 {
  603         struct inpcb *inp = sotoinpcb(so);
  604         struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
  605         struct ifaddr *ia = NULL;
  606 
  607         if (nam->sa_len != sizeof(*addr))
  608                 return EINVAL;
  609 
  610         if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
  611                 return EADDRNOTAVAIL;
  612 #if ENABLE_DEFAULT_SCOPE
  613         if (addr->sin6_scope_id == 0) { /* not change if specified  */
  614                 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
  615         }
  616 #endif
  617         if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
  618             (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
  619                 return EADDRNOTAVAIL;
  620         if (ia &&
  621             ((struct in6_ifaddr *)ia)->ia6_flags &
  622             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
  623              IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
  624                 return(EADDRNOTAVAIL);
  625         }
  626         inp->in6p_laddr = addr->sin6_addr;
  627         return 0;
  628 }
  629 
  630 static int
  631 rip6_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
  632 {
  633         struct inpcb *inp = sotoinpcb(so);
  634         struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
  635         struct in6_addr *in6a = NULL;
  636         int error = 0;
  637 #if ENABLE_DEFAULT_SCOPE
  638         struct sockaddr_in6 tmp;
  639 #endif
  640 
  641         if (nam->sa_len != sizeof(*addr))
  642                 return EINVAL;
  643         if (TAILQ_EMPTY(&ifnet))
  644                 return EADDRNOTAVAIL;
  645         if (addr->sin6_family != AF_INET6)
  646                 return EAFNOSUPPORT;
  647 #if ENABLE_DEFAULT_SCOPE
  648         if (addr->sin6_scope_id == 0) { /* not change if specified  */
  649                 /* avoid overwrites */
  650                 tmp = *addr;
  651                 addr = &tmp;
  652                 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
  653         }
  654 #endif
  655         /* Source address selection. XXX: need pcblookup? */
  656         in6a = in6_selectsrc(addr, inp->in6p_outputopts,
  657                              inp->in6p_moptions, &inp->in6p_route,
  658                              &inp->in6p_laddr, &error);
  659         if (in6a == NULL)
  660                 return (error ? error : EADDRNOTAVAIL);
  661         inp->in6p_laddr = *in6a;
  662         inp->in6p_faddr = addr->sin6_addr;
  663         soisconnected(so);
  664         return 0;
  665 }
  666 
  667 static int
  668 rip6_shutdown(struct socket *so)
  669 {
  670         socantsendmore(so);
  671         return 0;
  672 }
  673 
  674 static int
  675 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
  676          struct mbuf *control, struct proc *p)
  677 {
  678         struct inpcb *inp = sotoinpcb(so);
  679         struct sockaddr_in6 tmp;
  680         struct sockaddr_in6 *dst;
  681 
  682         /* always copy sockaddr to avoid overwrites */
  683         if (so->so_state & SS_ISCONNECTED) {
  684                 if (nam) {
  685                         m_freem(m);
  686                         return EISCONN;
  687                 }
  688                 /* XXX */
  689                 bzero(&tmp, sizeof(tmp));
  690                 tmp.sin6_family = AF_INET6;
  691                 tmp.sin6_len = sizeof(struct sockaddr_in6);
  692                 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
  693                       sizeof(struct in6_addr));
  694                 dst = &tmp;
  695         } else {
  696                 if (nam == NULL) {
  697                         m_freem(m);
  698                         return ENOTCONN;
  699                 }
  700                 tmp = *(struct sockaddr_in6 *)nam;
  701                 dst = &tmp;
  702         }
  703 #if ENABLE_DEFAULT_SCOPE
  704         if (dst->sin6_scope_id == 0) {  /* not change if specified  */
  705                 dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr);
  706         }
  707 #endif
  708         return rip6_output(m, so, dst, control);
  709 }
  710 
  711 struct pr_usrreqs rip6_usrreqs = {
  712         rip6_abort, pru_accept_notsupp, rip6_attach, rip6_bind, rip6_connect,
  713         pru_connect2_notsupp, in6_control, rip6_detach, rip6_disconnect,
  714         pru_listen_notsupp, in6_setpeeraddr, pru_rcvd_notsupp,
  715         pru_rcvoob_notsupp, rip6_send, pru_sense_null, rip6_shutdown,
  716         in6_setsockaddr, sosend, soreceive, sopoll
  717 };

Cache object: 4928062c0887e47088ddf9e231606664


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