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/ip6_mroute.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) 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  *      $KAME: ip6_mroute.c,v 1.58 2001/12/18 02:36:31 itojun Exp $
   30  */
   31 
   32 /*-
   33  * Copyright (c) 1989 Stephen Deering
   34  * Copyright (c) 1992, 1993
   35  *      The Regents of the University of California.  All rights reserved.
   36  *
   37  * This code is derived from software contributed to Berkeley by
   38  * Stephen Deering of Stanford University.
   39  *
   40  * Redistribution and use in source and binary forms, with or without
   41  * modification, are permitted provided that the following conditions
   42  * are met:
   43  * 1. Redistributions of source code must retain the above copyright
   44  *    notice, this list of conditions and the following disclaimer.
   45  * 2. Redistributions in binary form must reproduce the above copyright
   46  *    notice, this list of conditions and the following disclaimer in the
   47  *    documentation and/or other materials provided with the distribution.
   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  *      @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
   65  *      BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp
   66  */
   67 
   68 /*
   69  * IP multicast forwarding procedures
   70  *
   71  * Written by David Waitzman, BBN Labs, August 1988.
   72  * Modified by Steve Deering, Stanford, February 1989.
   73  * Modified by Mark J. Steiglitz, Stanford, May, 1991
   74  * Modified by Van Jacobson, LBL, January 1993
   75  * Modified by Ajit Thyagarajan, PARC, August 1993
   76  * Modified by Bill Fenner, PARC, April 1994
   77  *
   78  * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support
   79  */
   80 
   81 #include <sys/cdefs.h>
   82 __FBSDID("$FreeBSD$");
   83 
   84 #include "opt_inet.h"
   85 #include "opt_inet6.h"
   86 
   87 #include <sys/param.h>
   88 #include <sys/callout.h>
   89 #include <sys/errno.h>
   90 #include <sys/kernel.h>
   91 #include <sys/lock.h>
   92 #include <sys/malloc.h>
   93 #include <sys/mbuf.h>
   94 #include <sys/protosw.h>
   95 #include <sys/signalvar.h>
   96 #include <sys/socket.h>
   97 #include <sys/socketvar.h>
   98 #include <sys/sockio.h>
   99 #include <sys/sx.h>
  100 #include <sys/syslog.h>
  101 #include <sys/systm.h>
  102 #include <sys/time.h>
  103 
  104 #include <net/if.h>
  105 #include <net/if_types.h>
  106 #include <net/raw_cb.h>
  107 #include <net/route.h>
  108 
  109 #include <netinet/in.h>
  110 #include <netinet/in_var.h>
  111 #include <netinet/icmp6.h>
  112 
  113 #include <netinet/ip6.h>
  114 #include <netinet6/ip6_var.h>
  115 #include <netinet6/scope6_var.h>
  116 #include <netinet6/nd6.h>
  117 #include <netinet6/ip6_mroute.h>
  118 #include <netinet6/pim6.h>
  119 #include <netinet6/pim6_var.h>
  120 
  121 #include <net/net_osdep.h>
  122 
  123 static MALLOC_DEFINE(M_MRTABLE6, "mf6c", "multicast forwarding cache entry");
  124 
  125 #define M_HASCL(m) ((m)->m_flags & M_EXT)
  126 
  127 static int ip6_mdq(struct mbuf *, struct ifnet *, struct mf6c *);
  128 static void phyint_send(struct ip6_hdr *, struct mif6 *, struct mbuf *);
  129 
  130 static int set_pim6(int *);
  131 static int socket_send __P((struct socket *, struct mbuf *,
  132             struct sockaddr_in6 *));
  133 static int register_send __P((struct ip6_hdr *, struct mif6 *,
  134             struct mbuf *));
  135 
  136 /*
  137  * Globals.  All but ip6_mrouter, ip6_mrtproto and mrt6stat could be static,
  138  * except for netstat or debugging purposes.
  139  */
  140 struct socket  *ip6_mrouter = NULL;
  141 int             ip6_mrouter_ver = 0;
  142 int             ip6_mrtproto = IPPROTO_PIM;    /* for netstat only */
  143 struct mrt6stat mrt6stat;
  144 
  145 #define NO_RTE_FOUND    0x1
  146 #define RTE_FOUND       0x2
  147 
  148 struct mf6c     *mf6ctable[MF6CTBLSIZ];
  149 u_char          n6expire[MF6CTBLSIZ];
  150 static struct mif6 mif6table[MAXMIFS];
  151 #ifdef MRT6DEBUG
  152 u_int           mrt6debug = 0;    /* debug level        */
  153 #define DEBUG_MFC       0x02
  154 #define DEBUG_FORWARD   0x04
  155 #define DEBUG_EXPIRE    0x08
  156 #define DEBUG_XMIT      0x10
  157 #define DEBUG_REG       0x20
  158 #define DEBUG_PIM       0x40
  159 #endif
  160 
  161 static void     expire_upcalls(void *);
  162 #define EXPIRE_TIMEOUT  (hz / 4)        /* 4x / second */
  163 #define UPCALL_EXPIRE   6               /* number of timeouts */
  164 
  165 #ifdef INET
  166 #ifdef MROUTING
  167 extern struct socket *ip_mrouter;
  168 #endif
  169 #endif
  170 
  171 /*
  172  * 'Interfaces' associated with decapsulator (so we can tell
  173  * packets that went through it from ones that get reflected
  174  * by a broken gateway).  Different from IPv4 register_if,
  175  * these interfaces are linked into the system ifnet list,
  176  * because per-interface IPv6 statistics are maintained in
  177  * ifp->if_afdata.  But it does not have any routes point 
  178  * to them.  I.e., packets can't be sent this way.  They
  179  * only exist as a placeholder for multicast source 
  180  * verification.
  181  */
  182 static struct ifnet *multicast_register_if6;
  183 
  184 #define ENCAP_HOPS 64
  185 
  186 /*
  187  * Private variables.
  188  */
  189 static mifi_t nummifs = 0;
  190 static mifi_t reg_mif_num = (mifi_t)-1;
  191 
  192 static struct pim6stat pim6stat;
  193 static int pim6;
  194 
  195 /*
  196  * Hash function for a source, group entry
  197  */
  198 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
  199                                    (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
  200                                    (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
  201                                    (g).s6_addr32[2] ^ (g).s6_addr32[3])
  202 
  203 /*
  204  * Find a route for a given origin IPv6 address and Multicast group address.
  205  * Quality of service parameter to be added in the future!!!
  206  */
  207 
  208 #define MF6CFIND(o, g, rt) do { \
  209         struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
  210         rt = NULL; \
  211         mrt6stat.mrt6s_mfc_lookups++; \
  212         while (_rt) { \
  213                 if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
  214                     IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
  215                     (_rt->mf6c_stall == NULL)) { \
  216                         rt = _rt; \
  217                         break; \
  218                 } \
  219                 _rt = _rt->mf6c_next; \
  220         } \
  221         if (rt == NULL) { \
  222                 mrt6stat.mrt6s_mfc_misses++; \
  223         } \
  224 } while (/*CONSTCOND*/ 0)
  225 
  226 /*
  227  * Macros to compute elapsed time efficiently
  228  * Borrowed from Van Jacobson's scheduling code
  229  */
  230 #define TV_DELTA(a, b, delta) do { \
  231             int xxs; \
  232                 \
  233             delta = (a).tv_usec - (b).tv_usec; \
  234             if ((xxs = (a).tv_sec - (b).tv_sec)) { \
  235                switch (xxs) { \
  236                       case 2: \
  237                           delta += 1000000; \
  238                               /* FALLTHROUGH */ \
  239                       case 1: \
  240                           delta += 1000000; \
  241                           break; \
  242                       default: \
  243                           delta += (1000000 * xxs); \
  244                } \
  245             } \
  246 } while (/*CONSTCOND*/ 0)
  247 
  248 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
  249               (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
  250 
  251 #ifdef UPCALL_TIMING
  252 #define UPCALL_MAX      50
  253 u_long upcall_data[UPCALL_MAX + 1];
  254 static void collate();
  255 #endif /* UPCALL_TIMING */
  256 
  257 static int get_sg_cnt(struct sioc_sg_req6 *);
  258 static int get_mif6_cnt(struct sioc_mif_req6 *);
  259 static int ip6_mrouter_init(struct socket *, int, int);
  260 static int add_m6if(struct mif6ctl *);
  261 static int del_m6if(mifi_t *);
  262 static int add_m6fc(struct mf6cctl *);
  263 static int del_m6fc(struct mf6cctl *);
  264 
  265 static struct callout expire_upcalls_ch;
  266 
  267 /*
  268  * Handle MRT setsockopt commands to modify the multicast routing tables.
  269  */
  270 int
  271 ip6_mrouter_set(so, sopt)
  272         struct socket *so;
  273         struct sockopt *sopt;
  274 {
  275         int error = 0;
  276         int optval;
  277         struct mif6ctl mifc;
  278         struct mf6cctl mfcc;
  279         mifi_t mifi;
  280 
  281         if (so != ip6_mrouter && sopt->sopt_name != MRT6_INIT)
  282                 return (EACCES);
  283 
  284         switch (sopt->sopt_name) {
  285         case MRT6_INIT:
  286 #ifdef MRT6_OINIT
  287         case MRT6_OINIT:
  288 #endif
  289                 error = sooptcopyin(sopt, &optval, sizeof(optval),
  290                     sizeof(optval));
  291                 if (error)
  292                         break;
  293                 error = ip6_mrouter_init(so, optval, sopt->sopt_name);
  294                 break;
  295         case MRT6_DONE:
  296                 error = ip6_mrouter_done();
  297                 break;
  298         case MRT6_ADD_MIF:
  299                 error = sooptcopyin(sopt, &mifc, sizeof(mifc), sizeof(mifc));
  300                 if (error)
  301                         break;
  302                 error = add_m6if(&mifc);
  303                 break;
  304         case MRT6_ADD_MFC:
  305                 error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
  306                 if (error)
  307                         break;
  308                 error = add_m6fc(&mfcc);
  309                 break;
  310         case MRT6_DEL_MFC:
  311                 error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
  312                 if (error)
  313                         break;
  314                 error = del_m6fc(&mfcc);
  315                 break;
  316         case MRT6_DEL_MIF:
  317                 error = sooptcopyin(sopt, &mifi, sizeof(mifi), sizeof(mifi));
  318                 if (error)
  319                         break;
  320                 error = del_m6if(&mifi);
  321                 break;
  322         case MRT6_PIM:
  323                 error = sooptcopyin(sopt, &optval, sizeof(optval),
  324                     sizeof(optval));
  325                 if (error)
  326                         break;
  327                 error = set_pim6(&optval);
  328                 break;
  329         default:
  330                 error = EOPNOTSUPP;
  331                 break;
  332         }
  333 
  334         return (error);
  335 }
  336 
  337 /*
  338  * Handle MRT getsockopt commands
  339  */
  340 int
  341 ip6_mrouter_get(so, sopt)
  342         struct socket *so;
  343         struct sockopt *sopt;
  344 {
  345         int error = 0;
  346 
  347         if (so != ip6_mrouter)
  348                 return (EACCES);
  349 
  350         switch (sopt->sopt_name) {
  351                 case MRT6_PIM:
  352                         error = sooptcopyout(sopt, &pim6, sizeof(pim6));
  353                         break;
  354         }
  355         return (error);
  356 }
  357 
  358 /*
  359  * Handle ioctl commands to obtain information from the cache
  360  */
  361 int
  362 mrt6_ioctl(cmd, data)
  363         int cmd;
  364         caddr_t data;
  365 {
  366         switch (cmd) {
  367         case SIOCGETSGCNT_IN6:
  368                 return (get_sg_cnt((struct sioc_sg_req6 *)data));
  369         case SIOCGETMIFCNT_IN6:
  370                 return (get_mif6_cnt((struct sioc_mif_req6 *)data));
  371         default:
  372                 return (EINVAL);
  373         }
  374 }
  375 
  376 /*
  377  * returns the packet, byte, rpf-failure count for the source group provided
  378  */
  379 static int
  380 get_sg_cnt(req)
  381         struct sioc_sg_req6 *req;
  382 {
  383         struct mf6c *rt;
  384         int s;
  385 
  386         s = splnet();
  387         MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt);
  388         splx(s);
  389         if (rt != NULL) {
  390                 req->pktcnt = rt->mf6c_pkt_cnt;
  391                 req->bytecnt = rt->mf6c_byte_cnt;
  392                 req->wrong_if = rt->mf6c_wrong_if;
  393         } else
  394                 return (ESRCH);
  395 #if 0
  396                 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
  397 #endif
  398 
  399         return (0);
  400 }
  401 
  402 /*
  403  * returns the input and output packet and byte counts on the mif provided
  404  */
  405 static int
  406 get_mif6_cnt(req)
  407         struct sioc_mif_req6 *req;
  408 {
  409         mifi_t mifi = req->mifi;
  410 
  411         if (mifi >= nummifs)
  412                 return (EINVAL);
  413 
  414         req->icount = mif6table[mifi].m6_pkt_in;
  415         req->ocount = mif6table[mifi].m6_pkt_out;
  416         req->ibytes = mif6table[mifi].m6_bytes_in;
  417         req->obytes = mif6table[mifi].m6_bytes_out;
  418 
  419         return (0);
  420 }
  421 
  422 static int
  423 set_pim6(i)
  424         int *i;
  425 {
  426         if ((*i != 1) && (*i != 0))
  427                 return (EINVAL);
  428 
  429         pim6 = *i;
  430 
  431         return (0);
  432 }
  433 
  434 /*
  435  * Enable multicast routing
  436  */
  437 static int
  438 ip6_mrouter_init(so, v, cmd)
  439         struct socket *so;
  440         int v;
  441         int cmd;
  442 {
  443 #ifdef MRT6DEBUG
  444         if (mrt6debug)
  445                 log(LOG_DEBUG,
  446                     "ip6_mrouter_init: so_type = %d, pr_protocol = %d\n",
  447                     so->so_type, so->so_proto->pr_protocol);
  448 #endif
  449 
  450         if (so->so_type != SOCK_RAW ||
  451             so->so_proto->pr_protocol != IPPROTO_ICMPV6)
  452                 return (EOPNOTSUPP);
  453 
  454         if (v != 1)
  455                 return (ENOPROTOOPT);
  456 
  457         if (ip6_mrouter != NULL)
  458                 return (EADDRINUSE);
  459 
  460         ip6_mrouter = so;
  461         ip6_mrouter_ver = cmd;
  462 
  463         bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
  464         bzero((caddr_t)n6expire, sizeof(n6expire));
  465 
  466         pim6 = 0;/* used for stubbing out/in pim stuff */
  467 
  468         callout_init(&expire_upcalls_ch, 0);
  469         callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
  470             expire_upcalls, NULL);
  471 
  472 #ifdef MRT6DEBUG
  473         if (mrt6debug)
  474                 log(LOG_DEBUG, "ip6_mrouter_init\n");
  475 #endif
  476 
  477         return (0);
  478 }
  479 
  480 /*
  481  * Disable multicast routing
  482  */
  483 int
  484 ip6_mrouter_done()
  485 {
  486         mifi_t mifi;
  487         int i;
  488         struct mf6c *rt;
  489         struct rtdetq *rte;
  490         int s;
  491 
  492         s = splnet();
  493 
  494         /*
  495          * For each phyint in use, disable promiscuous reception of all IPv6
  496          * multicasts.
  497          */
  498 #ifdef INET
  499 #ifdef MROUTING
  500         /*
  501          * If there is still IPv4 multicast routing daemon,
  502          * we remain interfaces to receive all muliticasted packets.
  503          * XXX: there may be an interface in which the IPv4 multicast
  504          * daemon is not interested...
  505          */
  506         if (!ip_mrouter)
  507 #endif
  508 #endif
  509         {
  510                 for (mifi = 0; mifi < nummifs; mifi++) {
  511                         if (mif6table[mifi].m6_ifp &&
  512                             !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
  513                                 if_allmulti(mif6table[mifi].m6_ifp, 0);
  514                         }
  515                 }
  516         }
  517 #ifdef notyet
  518         bzero((caddr_t)qtable, sizeof(qtable));
  519         bzero((caddr_t)tbftable, sizeof(tbftable));
  520 #endif
  521         bzero((caddr_t)mif6table, sizeof(mif6table));
  522         nummifs = 0;
  523 
  524         pim6 = 0; /* used to stub out/in pim specific code */
  525 
  526         callout_stop(&expire_upcalls_ch);
  527 
  528         /*
  529          * Free all multicast forwarding cache entries.
  530          */
  531         for (i = 0; i < MF6CTBLSIZ; i++) {
  532                 rt = mf6ctable[i];
  533                 while (rt) {
  534                         struct mf6c *frt;
  535 
  536                         for (rte = rt->mf6c_stall; rte != NULL; ) {
  537                                 struct rtdetq *n = rte->next;
  538 
  539                                 m_free(rte->m);
  540                                 free(rte, M_MRTABLE6);
  541                                 rte = n;
  542                         }
  543                         frt = rt;
  544                         rt = rt->mf6c_next;
  545                         free(frt, M_MRTABLE6);
  546                 }
  547         }
  548 
  549         bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
  550 
  551         /*
  552          * Reset register interface
  553          */
  554         if (reg_mif_num != (mifi_t)-1 && multicast_register_if6 != NULL) {
  555                 if_detach(multicast_register_if6);
  556                 if_free(multicast_register_if6);
  557                 reg_mif_num = (mifi_t)-1;
  558                 multicast_register_if6 = NULL;
  559         }
  560 
  561         ip6_mrouter = NULL;
  562         ip6_mrouter_ver = 0;
  563 
  564         splx(s);
  565 
  566 #ifdef MRT6DEBUG
  567         if (mrt6debug)
  568                 log(LOG_DEBUG, "ip6_mrouter_done\n");
  569 #endif
  570 
  571         return (0);
  572 }
  573 
  574 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
  575 
  576 /*
  577  * Add a mif to the mif table
  578  */
  579 static int
  580 add_m6if(mifcp)
  581         struct mif6ctl *mifcp;
  582 {
  583         struct mif6 *mifp;
  584         struct ifnet *ifp;
  585         int error, s;
  586 #ifdef notyet
  587         struct tbf *m_tbf = tbftable + mifcp->mif6c_mifi;
  588 #endif
  589 
  590         if (mifcp->mif6c_mifi >= MAXMIFS)
  591                 return (EINVAL);
  592         mifp = mif6table + mifcp->mif6c_mifi;
  593         if (mifp->m6_ifp)
  594                 return (EADDRINUSE); /* XXX: is it appropriate? */
  595         if (mifcp->mif6c_pifi == 0 || mifcp->mif6c_pifi > if_index)
  596                 return (ENXIO);
  597         ifp = ifnet_byindex(mifcp->mif6c_pifi);
  598 
  599         if (mifcp->mif6c_flags & MIFF_REGISTER) {
  600                 if (reg_mif_num == (mifi_t)-1) {
  601                         ifp = if_alloc(IFT_OTHER);
  602 
  603                         if_initname(ifp, "register_mif", 0);
  604                         ifp->if_flags |= IFF_LOOPBACK;
  605                         if_attach(ifp);
  606                         multicast_register_if6 = ifp;
  607                         reg_mif_num = mifcp->mif6c_mifi;
  608                         /* 
  609                          * it is impossible to guess the ifindex of the 
  610                          * register interface.  So mif6c_pifi is automatically
  611                          * calculated.
  612                          */
  613                         mifcp->mif6c_pifi = ifp->if_index;
  614                 } else {
  615                         ifp = multicast_register_if6;
  616                 }
  617 
  618         } /* if REGISTER */
  619         else {
  620                 /* Make sure the interface supports multicast */
  621                 if ((ifp->if_flags & IFF_MULTICAST) == 0)
  622                         return (EOPNOTSUPP);
  623 
  624                 s = splnet();
  625                 error = if_allmulti(ifp, 1);
  626                 splx(s);
  627                 if (error)
  628                         return (error);
  629         }
  630 
  631         s = splnet();
  632         mifp->m6_flags     = mifcp->mif6c_flags;
  633         mifp->m6_ifp       = ifp;
  634 #ifdef notyet
  635         /* scaling up here allows division by 1024 in critical code */
  636         mifp->m6_rate_limit = mifcp->mif6c_rate_limit * 1024 / 1000;
  637 #endif
  638         /* initialize per mif pkt counters */
  639         mifp->m6_pkt_in    = 0;
  640         mifp->m6_pkt_out   = 0;
  641         mifp->m6_bytes_in  = 0;
  642         mifp->m6_bytes_out = 0;
  643         splx(s);
  644 
  645         /* Adjust nummifs up if the mifi is higher than nummifs */
  646         if (nummifs <= mifcp->mif6c_mifi)
  647                 nummifs = mifcp->mif6c_mifi + 1;
  648 
  649 #ifdef MRT6DEBUG
  650         if (mrt6debug)
  651                 log(LOG_DEBUG,
  652                     "add_mif #%d, phyint %s\n",
  653                     mifcp->mif6c_mifi,
  654                     ifp->if_xname);
  655 #endif
  656 
  657         return (0);
  658 }
  659 
  660 /*
  661  * Delete a mif from the mif table
  662  */
  663 static int
  664 del_m6if(mifip)
  665         mifi_t *mifip;
  666 {
  667         struct mif6 *mifp = mif6table + *mifip;
  668         mifi_t mifi;
  669         struct ifnet *ifp;
  670         int s;
  671 
  672         if (*mifip >= nummifs)
  673                 return (EINVAL);
  674         if (mifp->m6_ifp == NULL)
  675                 return (EINVAL);
  676 
  677         s = splnet();
  678 
  679         if (!(mifp->m6_flags & MIFF_REGISTER)) {
  680                 /*
  681                  * XXX: what if there is yet IPv4 multicast daemon
  682                  *      using the interface?
  683                  */
  684                 ifp = mifp->m6_ifp;
  685 
  686                 if_allmulti(ifp, 0);
  687         } else {
  688                 if (reg_mif_num != (mifi_t)-1 &&
  689                     multicast_register_if6 != NULL) {
  690                         if_detach(multicast_register_if6);
  691                         if_free(multicast_register_if6);
  692                         reg_mif_num = (mifi_t)-1;
  693                         multicast_register_if6 = NULL;
  694                 }
  695         }
  696 
  697 #ifdef notyet
  698         bzero((caddr_t)qtable[*mifip], sizeof(qtable[*mifip]));
  699         bzero((caddr_t)mifp->m6_tbf, sizeof(*(mifp->m6_tbf)));
  700 #endif
  701         bzero((caddr_t)mifp, sizeof(*mifp));
  702 
  703         /* Adjust nummifs down */
  704         for (mifi = nummifs; mifi > 0; mifi--)
  705                 if (mif6table[mifi - 1].m6_ifp)
  706                         break;
  707         nummifs = mifi;
  708 
  709         splx(s);
  710 
  711 #ifdef MRT6DEBUG
  712         if (mrt6debug)
  713                 log(LOG_DEBUG, "del_m6if %d, nummifs %d\n", *mifip, nummifs);
  714 #endif
  715 
  716         return (0);
  717 }
  718 
  719 /*
  720  * Add an mfc entry
  721  */
  722 static int
  723 add_m6fc(mfccp)
  724         struct mf6cctl *mfccp;
  725 {
  726         struct mf6c *rt;
  727         u_long hash;
  728         struct rtdetq *rte;
  729         u_short nstl;
  730         int s;
  731 
  732         MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
  733                  mfccp->mf6cc_mcastgrp.sin6_addr, rt);
  734 
  735         /* If an entry already exists, just update the fields */
  736         if (rt) {
  737 #ifdef MRT6DEBUG
  738                 if (mrt6debug & DEBUG_MFC)
  739                         log(LOG_DEBUG,
  740                             "add_m6fc no upcall h %d o %s g %s p %x\n",
  741                             ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
  742                             ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
  743                             mfccp->mf6cc_parent);
  744 #endif
  745 
  746                 s = splnet();
  747                 rt->mf6c_parent = mfccp->mf6cc_parent;
  748                 rt->mf6c_ifset = mfccp->mf6cc_ifset;
  749                 splx(s);
  750                 return (0);
  751         }
  752 
  753         /*
  754          * Find the entry for which the upcall was made and update
  755          */
  756         s = splnet();
  757         hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
  758                         mfccp->mf6cc_mcastgrp.sin6_addr);
  759         for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
  760                 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
  761                                        &mfccp->mf6cc_origin.sin6_addr) &&
  762                     IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
  763                                        &mfccp->mf6cc_mcastgrp.sin6_addr) &&
  764                     (rt->mf6c_stall != NULL)) {
  765 
  766                         if (nstl++)
  767                                 log(LOG_ERR,
  768                                     "add_m6fc: %s o %s g %s p %x dbx %p\n",
  769                                     "multiple kernel entries",
  770                                     ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
  771                                     ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
  772                                     mfccp->mf6cc_parent, rt->mf6c_stall);
  773 
  774 #ifdef MRT6DEBUG
  775                         if (mrt6debug & DEBUG_MFC)
  776                                 log(LOG_DEBUG,
  777                                     "add_m6fc o %s g %s p %x dbg %x\n",
  778                                     ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
  779                                     ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
  780                                     mfccp->mf6cc_parent, rt->mf6c_stall);
  781 #endif
  782 
  783                         rt->mf6c_origin     = mfccp->mf6cc_origin;
  784                         rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
  785                         rt->mf6c_parent     = mfccp->mf6cc_parent;
  786                         rt->mf6c_ifset      = mfccp->mf6cc_ifset;
  787                         /* initialize pkt counters per src-grp */
  788                         rt->mf6c_pkt_cnt    = 0;
  789                         rt->mf6c_byte_cnt   = 0;
  790                         rt->mf6c_wrong_if   = 0;
  791 
  792                         rt->mf6c_expire = 0;    /* Don't clean this guy up */
  793                         n6expire[hash]--;
  794 
  795                         /* free packets Qed at the end of this entry */
  796                         for (rte = rt->mf6c_stall; rte != NULL; ) {
  797                                 struct rtdetq *n = rte->next;
  798                                 ip6_mdq(rte->m, rte->ifp, rt);
  799                                 m_freem(rte->m);
  800 #ifdef UPCALL_TIMING
  801                                 collate(&(rte->t));
  802 #endif /* UPCALL_TIMING */
  803                                 free(rte, M_MRTABLE6);
  804                                 rte = n;
  805                         }
  806                         rt->mf6c_stall = NULL;
  807                 }
  808         }
  809 
  810         /*
  811          * It is possible that an entry is being inserted without an upcall
  812          */
  813         if (nstl == 0) {
  814 #ifdef MRT6DEBUG
  815                 if (mrt6debug & DEBUG_MFC)
  816                         log(LOG_DEBUG,
  817                             "add_mfc no upcall h %d o %s g %s p %x\n",
  818                             hash,
  819                             ip6_sprintf(&mfccp->mf6cc_origin.sin6_addr),
  820                             ip6_sprintf(&mfccp->mf6cc_mcastgrp.sin6_addr),
  821                             mfccp->mf6cc_parent);
  822 #endif
  823 
  824                 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
  825 
  826                         if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
  827                                                &mfccp->mf6cc_origin.sin6_addr)&&
  828                             IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
  829                                                &mfccp->mf6cc_mcastgrp.sin6_addr)) {
  830 
  831                                 rt->mf6c_origin     = mfccp->mf6cc_origin;
  832                                 rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
  833                                 rt->mf6c_parent     = mfccp->mf6cc_parent;
  834                                 rt->mf6c_ifset      = mfccp->mf6cc_ifset;
  835                                 /* initialize pkt counters per src-grp */
  836                                 rt->mf6c_pkt_cnt    = 0;
  837                                 rt->mf6c_byte_cnt   = 0;
  838                                 rt->mf6c_wrong_if   = 0;
  839 
  840                                 if (rt->mf6c_expire)
  841                                         n6expire[hash]--;
  842                                 rt->mf6c_expire    = 0;
  843                         }
  844                 }
  845                 if (rt == NULL) {
  846                         /* no upcall, so make a new entry */
  847                         rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6,
  848                                                   M_NOWAIT);
  849                         if (rt == NULL) {
  850                                 splx(s);
  851                                 return (ENOBUFS);
  852                         }
  853 
  854                         /* insert new entry at head of hash chain */
  855                         rt->mf6c_origin     = mfccp->mf6cc_origin;
  856                         rt->mf6c_mcastgrp   = mfccp->mf6cc_mcastgrp;
  857                         rt->mf6c_parent     = mfccp->mf6cc_parent;
  858                         rt->mf6c_ifset      = mfccp->mf6cc_ifset;
  859                         /* initialize pkt counters per src-grp */
  860                         rt->mf6c_pkt_cnt    = 0;
  861                         rt->mf6c_byte_cnt   = 0;
  862                         rt->mf6c_wrong_if   = 0;
  863                         rt->mf6c_expire     = 0;
  864                         rt->mf6c_stall = NULL;
  865 
  866                         /* link into table */
  867                         rt->mf6c_next  = mf6ctable[hash];
  868                         mf6ctable[hash] = rt;
  869                 }
  870         }
  871         splx(s);
  872         return (0);
  873 }
  874 
  875 #ifdef UPCALL_TIMING
  876 /*
  877  * collect delay statistics on the upcalls
  878  */
  879 static void
  880 collate(t)
  881         struct timeval *t;
  882 {
  883         u_long d;
  884         struct timeval tp;
  885         u_long delta;
  886 
  887         GET_TIME(tp);
  888 
  889         if (TV_LT(*t, tp))
  890         {
  891                 TV_DELTA(tp, *t, delta);
  892 
  893                 d = delta >> 10;
  894                 if (d > UPCALL_MAX)
  895                         d = UPCALL_MAX;
  896 
  897                 ++upcall_data[d];
  898         }
  899 }
  900 #endif /* UPCALL_TIMING */
  901 
  902 /*
  903  * Delete an mfc entry
  904  */
  905 static int
  906 del_m6fc(mfccp)
  907         struct mf6cctl *mfccp;
  908 {
  909         struct sockaddr_in6     origin;
  910         struct sockaddr_in6     mcastgrp;
  911         struct mf6c             *rt;
  912         struct mf6c             **nptr;
  913         u_long          hash;
  914         int s;
  915 
  916         origin = mfccp->mf6cc_origin;
  917         mcastgrp = mfccp->mf6cc_mcastgrp;
  918         hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
  919 
  920 #ifdef MRT6DEBUG
  921         if (mrt6debug & DEBUG_MFC)
  922                 log(LOG_DEBUG,"del_m6fc orig %s mcastgrp %s\n",
  923                     ip6_sprintf(&origin.sin6_addr),
  924                     ip6_sprintf(&mcastgrp.sin6_addr));
  925 #endif
  926 
  927         s = splnet();
  928 
  929         nptr = &mf6ctable[hash];
  930         while ((rt = *nptr) != NULL) {
  931                 if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
  932                                        &rt->mf6c_origin.sin6_addr) &&
  933                     IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
  934                                        &rt->mf6c_mcastgrp.sin6_addr) &&
  935                     rt->mf6c_stall == NULL)
  936                         break;
  937 
  938                 nptr = &rt->mf6c_next;
  939         }
  940         if (rt == NULL) {
  941                 splx(s);
  942                 return (EADDRNOTAVAIL);
  943         }
  944 
  945         *nptr = rt->mf6c_next;
  946         free(rt, M_MRTABLE6);
  947 
  948         splx(s);
  949 
  950         return (0);
  951 }
  952 
  953 static int
  954 socket_send(s, mm, src)
  955         struct socket *s;
  956         struct mbuf *mm;
  957         struct sockaddr_in6 *src;
  958 {
  959         if (s) {
  960                 if (sbappendaddr(&s->so_rcv,
  961                                  (struct sockaddr *)src,
  962                                  mm, (struct mbuf *)0) != 0) {
  963                         sorwakeup(s);
  964                         return (0);
  965                 }
  966         }
  967         m_freem(mm);
  968         return (-1);
  969 }
  970 
  971 /*
  972  * IPv6 multicast forwarding function. This function assumes that the packet
  973  * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
  974  * pointed to by "ifp", and the packet is to be relayed to other networks
  975  * that have members of the packet's destination IPv6 multicast group.
  976  *
  977  * The packet is returned unscathed to the caller, unless it is
  978  * erroneous, in which case a non-zero return value tells the caller to
  979  * discard it.
  980  *
  981  * NOTE: this implementation assumes that m->m_pkthdr.rcvif is NULL iff
  982  * this function is called in the originating context (i.e., not when
  983  * forwarding a packet from other node).  ip6_output(), which is currently the
  984  * only function that calls this function is called in the originating context,
  985  * explicitly ensures this condition.  It is caller's responsibility to ensure
  986  * that if this function is called from somewhere else in the originating
  987  * context in the future.
  988  */
  989 
  990 int
  991 ip6_mforward(ip6, ifp, m)
  992         struct ip6_hdr *ip6;
  993         struct ifnet *ifp;
  994         struct mbuf *m;
  995 {
  996         struct mf6c *rt;
  997         struct mif6 *mifp;
  998         struct mbuf *mm;
  999         int s;
 1000         mifi_t mifi;
 1001 
 1002 #ifdef MRT6DEBUG
 1003         if (mrt6debug & DEBUG_FORWARD)
 1004                 log(LOG_DEBUG, "ip6_mforward: src %s, dst %s, ifindex %d\n",
 1005                     ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst),
 1006                     ifp->if_index);
 1007 #endif
 1008 
 1009         /*
 1010          * Don't forward a packet with Hop limit of zero or one,
 1011          * or a packet destined to a local-only group.
 1012          */
 1013         if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) ||
 1014             IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
 1015                 return (0);
 1016         ip6->ip6_hlim--;
 1017 
 1018         /*
 1019          * Source address check: do not forward packets with unspecified
 1020          * source. It was discussed in July 2000, on ipngwg mailing list.
 1021          * This is rather more serious than unicast cases, because some
 1022          * MLD packets can be sent with the unspecified source address
 1023          * (although such packets must normally set 1 to the hop limit field).
 1024          */
 1025         if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
 1026                 ip6stat.ip6s_cantforward++;
 1027                 if (ip6_log_time + ip6_log_interval < time_second) {
 1028                         ip6_log_time = time_second;
 1029                         log(LOG_DEBUG,
 1030                             "cannot forward "
 1031                             "from %s to %s nxt %d received on %s\n",
 1032                             ip6_sprintf(&ip6->ip6_src),
 1033                             ip6_sprintf(&ip6->ip6_dst),
 1034                             ip6->ip6_nxt,
 1035                             if_name(m->m_pkthdr.rcvif));
 1036                 }
 1037                 return (0);
 1038         }
 1039 
 1040         /*
 1041          * Determine forwarding mifs from the forwarding cache table
 1042          */
 1043         s = splnet();
 1044         MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
 1045 
 1046         /* Entry exists, so forward if necessary */
 1047         if (rt) {
 1048                 splx(s);
 1049                 return (ip6_mdq(m, ifp, rt));
 1050         } else {
 1051                 /*
 1052                  * If we don't have a route for packet's origin,
 1053                  * Make a copy of the packet &
 1054                  * send message to routing daemon
 1055                  */
 1056 
 1057                 struct mbuf *mb0;
 1058                 struct rtdetq *rte;
 1059                 u_long hash;
 1060 /*              int i, npkts;*/
 1061 #ifdef UPCALL_TIMING
 1062                 struct timeval tp;
 1063 
 1064                 GET_TIME(tp);
 1065 #endif /* UPCALL_TIMING */
 1066 
 1067                 mrt6stat.mrt6s_no_route++;
 1068 #ifdef MRT6DEBUG
 1069                 if (mrt6debug & (DEBUG_FORWARD | DEBUG_MFC))
 1070                         log(LOG_DEBUG, "ip6_mforward: no rte s %s g %s\n",
 1071                             ip6_sprintf(&ip6->ip6_src),
 1072                             ip6_sprintf(&ip6->ip6_dst));
 1073 #endif
 1074 
 1075                 /*
 1076                  * Allocate mbufs early so that we don't do extra work if we
 1077                  * are just going to fail anyway.
 1078                  */
 1079                 rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE6,
 1080                                               M_NOWAIT);
 1081                 if (rte == NULL) {
 1082                         splx(s);
 1083                         return (ENOBUFS);
 1084                 }
 1085                 mb0 = m_copy(m, 0, M_COPYALL);
 1086                 /*
 1087                  * Pullup packet header if needed before storing it,
 1088                  * as other references may modify it in the meantime.
 1089                  */
 1090                 if (mb0 &&
 1091                     (M_HASCL(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
 1092                         mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
 1093                 if (mb0 == NULL) {
 1094                         free(rte, M_MRTABLE6);
 1095                         splx(s);
 1096                         return (ENOBUFS);
 1097                 }
 1098 
 1099                 /* is there an upcall waiting for this packet? */
 1100                 hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
 1101                 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
 1102                         if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
 1103                                                &rt->mf6c_origin.sin6_addr) &&
 1104                             IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
 1105                                                &rt->mf6c_mcastgrp.sin6_addr) &&
 1106                             (rt->mf6c_stall != NULL))
 1107                                 break;
 1108                 }
 1109 
 1110                 if (rt == NULL) {
 1111                         struct mrt6msg *im;
 1112 #ifdef MRT6_OINIT
 1113                         struct omrt6msg *oim;
 1114 #endif
 1115 
 1116                         /* no upcall, so make a new entry */
 1117                         rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6,
 1118                                                   M_NOWAIT);
 1119                         if (rt == NULL) {
 1120                                 free(rte, M_MRTABLE6);
 1121                                 m_freem(mb0);
 1122                                 splx(s);
 1123                                 return (ENOBUFS);
 1124                         }
 1125                         /*
 1126                          * Make a copy of the header to send to the user
 1127                          * level process
 1128                          */
 1129                         mm = m_copy(mb0, 0, sizeof(struct ip6_hdr));
 1130 
 1131                         if (mm == NULL) {
 1132                                 free(rte, M_MRTABLE6);
 1133                                 m_freem(mb0);
 1134                                 free(rt, M_MRTABLE6);
 1135                                 splx(s);
 1136                                 return (ENOBUFS);
 1137                         }
 1138 
 1139                         /*
 1140                          * Send message to routing daemon
 1141                          */
 1142                         sin6.sin6_addr = ip6->ip6_src;
 1143 
 1144                         im = NULL;
 1145 #ifdef MRT6_OINIT
 1146                         oim = NULL;
 1147 #endif
 1148                         switch (ip6_mrouter_ver) {
 1149 #ifdef MRT6_OINIT
 1150                         case MRT6_OINIT:
 1151                                 oim = mtod(mm, struct omrt6msg *);
 1152                                 oim->im6_msgtype = MRT6MSG_NOCACHE;
 1153                                 oim->im6_mbz = 0;
 1154                                 break;
 1155 #endif
 1156                         case MRT6_INIT:
 1157                                 im = mtod(mm, struct mrt6msg *);
 1158                                 im->im6_msgtype = MRT6MSG_NOCACHE;
 1159                                 im->im6_mbz = 0;
 1160                                 break;
 1161                         default:
 1162                                 free(rte, M_MRTABLE6);
 1163                                 m_freem(mb0);
 1164                                 free(rt, M_MRTABLE6);
 1165                                 splx(s);
 1166                                 return (EINVAL);
 1167                         }
 1168 
 1169 #ifdef MRT6DEBUG
 1170                         if (mrt6debug & DEBUG_FORWARD)
 1171                                 log(LOG_DEBUG,
 1172                                     "getting the iif info in the kernel\n");
 1173 #endif
 1174 
 1175                         for (mifp = mif6table, mifi = 0;
 1176                              mifi < nummifs && mifp->m6_ifp != ifp;
 1177                              mifp++, mifi++)
 1178                                 ;
 1179 
 1180                         switch (ip6_mrouter_ver) {
 1181 #ifdef MRT6_OINIT
 1182                         case MRT6_OINIT:
 1183                                 oim->im6_mif = mifi;
 1184                                 break;
 1185 #endif
 1186                         case MRT6_INIT:
 1187                                 im->im6_mif = mifi;
 1188                                 break;
 1189                         }
 1190 
 1191                         if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
 1192                                 log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
 1193                                     "socket queue full\n");
 1194                                 mrt6stat.mrt6s_upq_sockfull++;
 1195                                 free(rte, M_MRTABLE6);
 1196                                 m_freem(mb0);
 1197                                 free(rt, M_MRTABLE6);
 1198                                 splx(s);
 1199                                 return (ENOBUFS);
 1200                         }
 1201 
 1202                         mrt6stat.mrt6s_upcalls++;
 1203 
 1204                         /* insert new entry at head of hash chain */
 1205                         bzero(rt, sizeof(*rt));
 1206                         rt->mf6c_origin.sin6_family = AF_INET6;
 1207                         rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6);
 1208                         rt->mf6c_origin.sin6_addr = ip6->ip6_src;
 1209                         rt->mf6c_mcastgrp.sin6_family = AF_INET6;
 1210                         rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6);
 1211                         rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst;
 1212                         rt->mf6c_expire = UPCALL_EXPIRE;
 1213                         n6expire[hash]++;
 1214                         rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
 1215 
 1216                         /* link into table */
 1217                         rt->mf6c_next  = mf6ctable[hash];
 1218                         mf6ctable[hash] = rt;
 1219                         /* Add this entry to the end of the queue */
 1220                         rt->mf6c_stall = rte;
 1221                 } else {
 1222                         /* determine if q has overflowed */
 1223                         struct rtdetq **p;
 1224                         int npkts = 0;
 1225 
 1226                         for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
 1227                                 if (++npkts > MAX_UPQ6) {
 1228                                         mrt6stat.mrt6s_upq_ovflw++;
 1229                                         free(rte, M_MRTABLE6);
 1230                                         m_freem(mb0);
 1231                                         splx(s);
 1232                                         return (0);
 1233                                 }
 1234 
 1235                         /* Add this entry to the end of the queue */
 1236                         *p = rte;
 1237                 }
 1238 
 1239                 rte->next = NULL;
 1240                 rte->m = mb0;
 1241                 rte->ifp = ifp;
 1242 #ifdef UPCALL_TIMING
 1243                 rte->t = tp;
 1244 #endif /* UPCALL_TIMING */
 1245 
 1246                 splx(s);
 1247 
 1248                 return (0);
 1249         }
 1250 }
 1251 
 1252 /*
 1253  * Clean up cache entries if upcalls are not serviced
 1254  * Call from the Slow Timeout mechanism, every half second.
 1255  */
 1256 static void
 1257 expire_upcalls(unused)
 1258         void *unused;
 1259 {
 1260         struct rtdetq *rte;
 1261         struct mf6c *mfc, **nptr;
 1262         int i;
 1263         int s;
 1264 
 1265         s = splnet();
 1266         for (i = 0; i < MF6CTBLSIZ; i++) {
 1267                 if (n6expire[i] == 0)
 1268                         continue;
 1269                 nptr = &mf6ctable[i];
 1270                 while ((mfc = *nptr) != NULL) {
 1271                         rte = mfc->mf6c_stall;
 1272                         /*
 1273                          * Skip real cache entries
 1274                          * Make sure it wasn't marked to not expire (shouldn't happen)
 1275                          * If it expires now
 1276                          */
 1277                         if (rte != NULL &&
 1278                             mfc->mf6c_expire != 0 &&
 1279                             --mfc->mf6c_expire == 0) {
 1280 #ifdef MRT6DEBUG
 1281                                 if (mrt6debug & DEBUG_EXPIRE)
 1282                                         log(LOG_DEBUG, "expire_upcalls: expiring (%s %s)\n",
 1283                                             ip6_sprintf(&mfc->mf6c_origin.sin6_addr),
 1284                                             ip6_sprintf(&mfc->mf6c_mcastgrp.sin6_addr));
 1285 #endif
 1286                                 /*
 1287                                  * drop all the packets
 1288                                  * free the mbuf with the pkt, if, timing info
 1289                                  */
 1290                                 do {
 1291                                         struct rtdetq *n = rte->next;
 1292                                         m_freem(rte->m);
 1293                                         free(rte, M_MRTABLE6);
 1294                                         rte = n;
 1295                                 } while (rte != NULL);
 1296                                 mrt6stat.mrt6s_cache_cleanups++;
 1297                                 n6expire[i]--;
 1298 
 1299                                 *nptr = mfc->mf6c_next;
 1300                                 free(mfc, M_MRTABLE6);
 1301                         } else {
 1302                                 nptr = &mfc->mf6c_next;
 1303                         }
 1304                 }
 1305         }
 1306         splx(s);
 1307         callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
 1308             expire_upcalls, NULL);
 1309 }
 1310 
 1311 /*
 1312  * Packet forwarding routine once entry in the cache is made
 1313  */
 1314 static int
 1315 ip6_mdq(m, ifp, rt)
 1316         struct mbuf *m;
 1317         struct ifnet *ifp;
 1318         struct mf6c *rt;
 1319 {
 1320         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
 1321         mifi_t mifi, iif;
 1322         struct mif6 *mifp;
 1323         int plen = m->m_pkthdr.len;
 1324         struct in6_addr src0, dst0; /* copies for local work */
 1325         u_int32_t iszone, idzone, oszone, odzone;
 1326         int error = 0;
 1327 
 1328 /*
 1329  * Macro to send packet on mif.  Since RSVP packets don't get counted on
 1330  * input, they shouldn't get counted on output, so statistics keeping is
 1331  * separate.
 1332  */
 1333 
 1334 #define MC6_SEND(ip6, mifp, m) do {                             \
 1335         if ((mifp)->m6_flags & MIFF_REGISTER)                   \
 1336                 register_send((ip6), (mifp), (m));              \
 1337         else                                                    \
 1338                 phyint_send((ip6), (mifp), (m));                \
 1339 } while (/*CONSTCOND*/ 0)
 1340 
 1341         /*
 1342          * Don't forward if it didn't arrive from the parent mif
 1343          * for its origin.
 1344          */
 1345         mifi = rt->mf6c_parent;
 1346         if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
 1347                 /* came in the wrong interface */
 1348 #ifdef MRT6DEBUG
 1349                 if (mrt6debug & DEBUG_FORWARD)
 1350                         log(LOG_DEBUG,
 1351                             "wrong if: ifid %d mifi %d mififid %x\n",
 1352                             ifp->if_index, mifi,
 1353                             mif6table[mifi].m6_ifp->if_index);
 1354 #endif
 1355                 mrt6stat.mrt6s_wrong_if++;
 1356                 rt->mf6c_wrong_if++;
 1357                 /*
 1358                  * If we are doing PIM processing, and we are forwarding
 1359                  * packets on this interface, send a message to the
 1360                  * routing daemon.
 1361                  */
 1362                 /* have to make sure this is a valid mif */
 1363                 if (mifi < nummifs && mif6table[mifi].m6_ifp)
 1364                         if (pim6 && (m->m_flags & M_LOOP) == 0) {
 1365                                 /*
 1366                                  * Check the M_LOOP flag to avoid an
 1367                                  * unnecessary PIM assert.
 1368                                  * XXX: M_LOOP is an ad-hoc hack...
 1369                                  */
 1370                                 static struct sockaddr_in6 sin6 =
 1371                                 { sizeof(sin6), AF_INET6 };
 1372 
 1373                                 struct mbuf *mm;
 1374                                 struct mrt6msg *im;
 1375 #ifdef MRT6_OINIT
 1376                                 struct omrt6msg *oim;
 1377 #endif
 1378 
 1379                                 mm = m_copy(m, 0, sizeof(struct ip6_hdr));
 1380                                 if (mm &&
 1381                                     (M_HASCL(mm) ||
 1382                                      mm->m_len < sizeof(struct ip6_hdr)))
 1383                                         mm = m_pullup(mm, sizeof(struct ip6_hdr));
 1384                                 if (mm == NULL)
 1385                                         return (ENOBUFS);
 1386 
 1387 #ifdef MRT6_OINIT
 1388                                 oim = NULL;
 1389 #endif
 1390                                 im = NULL;
 1391                                 switch (ip6_mrouter_ver) {
 1392 #ifdef MRT6_OINIT
 1393                                 case MRT6_OINIT:
 1394                                         oim = mtod(mm, struct omrt6msg *);
 1395                                         oim->im6_msgtype = MRT6MSG_WRONGMIF;
 1396                                         oim->im6_mbz = 0;
 1397                                         break;
 1398 #endif
 1399                                 case MRT6_INIT:
 1400                                         im = mtod(mm, struct mrt6msg *);
 1401                                         im->im6_msgtype = MRT6MSG_WRONGMIF;
 1402                                         im->im6_mbz = 0;
 1403                                         break;
 1404                                 default:
 1405                                         m_freem(mm);
 1406                                         return (EINVAL);
 1407                                 }
 1408 
 1409                                 for (mifp = mif6table, iif = 0;
 1410                                      iif < nummifs && mifp &&
 1411                                              mifp->m6_ifp != ifp;
 1412                                      mifp++, iif++)
 1413                                         ;
 1414 
 1415                                 switch (ip6_mrouter_ver) {
 1416 #ifdef MRT6_OINIT
 1417                                 case MRT6_OINIT:
 1418                                         oim->im6_mif = iif;
 1419                                         sin6.sin6_addr = oim->im6_src;
 1420                                         break;
 1421 #endif
 1422                                 case MRT6_INIT:
 1423                                         im->im6_mif = iif;
 1424                                         sin6.sin6_addr = im->im6_src;
 1425                                         break;
 1426                                 }
 1427 
 1428                                 mrt6stat.mrt6s_upcalls++;
 1429 
 1430                                 if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
 1431 #ifdef MRT6DEBUG
 1432                                         if (mrt6debug)
 1433                                                 log(LOG_WARNING, "mdq, ip6_mrouter socket queue full\n");
 1434 #endif
 1435                                         ++mrt6stat.mrt6s_upq_sockfull;
 1436                                         return (ENOBUFS);
 1437                                 }       /* if socket Q full */
 1438                         }               /* if PIM */
 1439                 return (0);
 1440         }                       /* if wrong iif */
 1441 
 1442         /* If I sourced this packet, it counts as output, else it was input. */
 1443         if (m->m_pkthdr.rcvif == NULL) {
 1444                 /* XXX: is rcvif really NULL when output?? */
 1445                 mif6table[mifi].m6_pkt_out++;
 1446                 mif6table[mifi].m6_bytes_out += plen;
 1447         } else {
 1448                 mif6table[mifi].m6_pkt_in++;
 1449                 mif6table[mifi].m6_bytes_in += plen;
 1450         }
 1451         rt->mf6c_pkt_cnt++;
 1452         rt->mf6c_byte_cnt += plen;
 1453 
 1454         /*
 1455          * For each mif, forward a copy of the packet if there are group
 1456          * members downstream on the interface.
 1457          */
 1458         src0 = ip6->ip6_src;
 1459         dst0 = ip6->ip6_dst;
 1460         if ((error = in6_setscope(&src0, ifp, &iszone)) != 0 ||
 1461             (error = in6_setscope(&dst0, ifp, &idzone)) != 0) {
 1462                 ip6stat.ip6s_badscope++;
 1463                 return (error);
 1464         }
 1465         for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++) {
 1466                 if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
 1467                         /*
 1468                          * check if the outgoing packet is going to break
 1469                          * a scope boundary.
 1470                          * XXX For packets through PIM register tunnel
 1471                          * interface, we believe a routing daemon.
 1472                          */
 1473                         if (!(mif6table[rt->mf6c_parent].m6_flags &
 1474                               MIFF_REGISTER) &&
 1475                             !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
 1476                                 if (in6_setscope(&src0, mif6table[mifi].m6_ifp,
 1477                                     &oszone) ||
 1478                                     in6_setscope(&dst0, mif6table[mifi].m6_ifp,
 1479                                     &odzone) ||
 1480                                     iszone != oszone ||
 1481                                     idzone != odzone) {
 1482                                         ip6stat.ip6s_badscope++;
 1483                                         continue;
 1484                                 }
 1485                         }
 1486 
 1487                         mifp->m6_pkt_out++;
 1488                         mifp->m6_bytes_out += plen;
 1489                         MC6_SEND(ip6, mifp, m);
 1490                 }
 1491         }
 1492         return (0);
 1493 }
 1494 
 1495 static void
 1496 phyint_send(ip6, mifp, m)
 1497     struct ip6_hdr *ip6;
 1498     struct mif6 *mifp;
 1499     struct mbuf *m;
 1500 {
 1501         struct mbuf *mb_copy;
 1502         struct ifnet *ifp = mifp->m6_ifp;
 1503         int error = 0;
 1504         int s = splnet();       /* needs to protect static "ro" below. */
 1505         static struct route_in6 ro;
 1506         struct  in6_multi *in6m;
 1507         struct sockaddr_in6 *dst6;
 1508         u_long linkmtu;
 1509 
 1510         /*
 1511          * Make a new reference to the packet; make sure that
 1512          * the IPv6 header is actually copied, not just referenced,
 1513          * so that ip6_output() only scribbles on the copy.
 1514          */
 1515         mb_copy = m_copy(m, 0, M_COPYALL);
 1516         if (mb_copy &&
 1517             (M_HASCL(mb_copy) || mb_copy->m_len < sizeof(struct ip6_hdr)))
 1518                 mb_copy = m_pullup(mb_copy, sizeof(struct ip6_hdr));
 1519         if (mb_copy == NULL) {
 1520                 splx(s);
 1521                 return;
 1522         }
 1523         /* set MCAST flag to the outgoing packet */
 1524         mb_copy->m_flags |= M_MCAST;
 1525 
 1526         /*
 1527          * If we sourced the packet, call ip6_output since we may devide
 1528          * the packet into fragments when the packet is too big for the
 1529          * outgoing interface.
 1530          * Otherwise, we can simply send the packet to the interface
 1531          * sending queue.
 1532          */
 1533         if (m->m_pkthdr.rcvif == NULL) {
 1534                 struct ip6_moptions im6o;
 1535 
 1536                 im6o.im6o_multicast_ifp = ifp;
 1537                 /* XXX: ip6_output will override ip6->ip6_hlim */
 1538                 im6o.im6o_multicast_hlim = ip6->ip6_hlim;
 1539                 im6o.im6o_multicast_loop = 1;
 1540                 error = ip6_output(mb_copy, NULL, &ro,
 1541                                    IPV6_FORWARDING, &im6o, NULL, NULL);
 1542 
 1543 #ifdef MRT6DEBUG
 1544                 if (mrt6debug & DEBUG_XMIT)
 1545                         log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
 1546                             mifp - mif6table, error);
 1547 #endif
 1548                 splx(s);
 1549                 return;
 1550         }
 1551 
 1552         /*
 1553          * If we belong to the destination multicast group
 1554          * on the outgoing interface, loop back a copy.
 1555          */
 1556         dst6 = (struct sockaddr_in6 *)&ro.ro_dst;
 1557         IN6_LOOKUP_MULTI(ip6->ip6_dst, ifp, in6m);
 1558         if (in6m != NULL) {
 1559                 dst6->sin6_len = sizeof(struct sockaddr_in6);
 1560                 dst6->sin6_family = AF_INET6;
 1561                 dst6->sin6_addr = ip6->ip6_dst;
 1562                 ip6_mloopback(ifp, m, (struct sockaddr_in6 *)&ro.ro_dst);
 1563         }
 1564         /*
 1565          * Put the packet into the sending queue of the outgoing interface
 1566          * if it would fit in the MTU of the interface.
 1567          */
 1568         linkmtu = IN6_LINKMTU(ifp);
 1569         if (mb_copy->m_pkthdr.len <= linkmtu || linkmtu < IPV6_MMTU) {
 1570                 dst6->sin6_len = sizeof(struct sockaddr_in6);
 1571                 dst6->sin6_family = AF_INET6;
 1572                 dst6->sin6_addr = ip6->ip6_dst;
 1573                 /*
 1574                  * We just call if_output instead of nd6_output here, since
 1575                  * we need no ND for a multicast forwarded packet...right?
 1576                  */
 1577                 error = (*ifp->if_output)(ifp, mb_copy,
 1578                     (struct sockaddr *)&ro.ro_dst, NULL);
 1579 #ifdef MRT6DEBUG
 1580                 if (mrt6debug & DEBUG_XMIT)
 1581                         log(LOG_DEBUG, "phyint_send on mif %d err %d\n",
 1582                             mifp - mif6table, error);
 1583 #endif
 1584         } else {
 1585                 /*
 1586                  * pMTU discovery is intentionally disabled by default, since
 1587                  * various router may notify pMTU in multicast, which can be
 1588                  * a DDoS to a router
 1589                  */
 1590                 if (ip6_mcast_pmtu)
 1591                         icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0, linkmtu);
 1592                 else {
 1593 #ifdef MRT6DEBUG
 1594                         if (mrt6debug & DEBUG_XMIT)
 1595                                 log(LOG_DEBUG,
 1596                                     "phyint_send: packet too big on %s o %s "
 1597                                     "g %s size %d(discarded)\n",
 1598                                     if_name(ifp),
 1599                                     ip6_sprintf(&ip6->ip6_src),
 1600                                     ip6_sprintf(&ip6->ip6_dst),
 1601                                     mb_copy->m_pkthdr.len);
 1602 #endif /* MRT6DEBUG */
 1603                         m_freem(mb_copy); /* simply discard the packet */
 1604                 }
 1605         }
 1606 
 1607         splx(s);
 1608 }
 1609 
 1610 static int
 1611 register_send(ip6, mif, m)
 1612         struct ip6_hdr *ip6;
 1613         struct mif6 *mif;
 1614         struct mbuf *m;
 1615 {
 1616         struct mbuf *mm;
 1617         int i, len = m->m_pkthdr.len;
 1618         static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
 1619         struct mrt6msg *im6;
 1620 
 1621 #ifdef MRT6DEBUG
 1622         if (mrt6debug)
 1623                 log(LOG_DEBUG, "** IPv6 register_send **\n src %s dst %s\n",
 1624                     ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&ip6->ip6_dst));
 1625 #endif
 1626         ++pim6stat.pim6s_snd_registers;
 1627 
 1628         /* Make a copy of the packet to send to the user level process */
 1629         MGETHDR(mm, M_DONTWAIT, MT_HEADER);
 1630         if (mm == NULL)
 1631                 return (ENOBUFS);
 1632         mm->m_pkthdr.rcvif = NULL;
 1633         mm->m_data += max_linkhdr;
 1634         mm->m_len = sizeof(struct ip6_hdr);
 1635 
 1636         if ((mm->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
 1637                 m_freem(mm);
 1638                 return (ENOBUFS);
 1639         }
 1640         i = MHLEN - M_LEADINGSPACE(mm);
 1641         if (i > len)
 1642                 i = len;
 1643         mm = m_pullup(mm, i);
 1644         if (mm == NULL)
 1645                 return (ENOBUFS);
 1646 /* TODO: check it! */
 1647         mm->m_pkthdr.len = len + sizeof(struct ip6_hdr);
 1648 
 1649         /*
 1650          * Send message to routing daemon
 1651          */
 1652         sin6.sin6_addr = ip6->ip6_src;
 1653 
 1654         im6 = mtod(mm, struct mrt6msg *);
 1655         im6->im6_msgtype      = MRT6MSG_WHOLEPKT;
 1656         im6->im6_mbz          = 0;
 1657 
 1658         im6->im6_mif = mif - mif6table;
 1659 
 1660         /* iif info is not given for reg. encap.n */
 1661         mrt6stat.mrt6s_upcalls++;
 1662 
 1663         if (socket_send(ip6_mrouter, mm, &sin6) < 0) {
 1664 #ifdef MRT6DEBUG
 1665                 if (mrt6debug)
 1666                         log(LOG_WARNING,
 1667                             "register_send: ip6_mrouter socket queue full\n");
 1668 #endif
 1669                 ++mrt6stat.mrt6s_upq_sockfull;
 1670                 return (ENOBUFS);
 1671         }
 1672         return (0);
 1673 }
 1674 
 1675 /*
 1676  * PIM sparse mode hook
 1677  * Receives the pim control messages, and passes them up to the listening
 1678  * socket, using rip6_input.
 1679  * The only message processed is the REGISTER pim message; the pim header
 1680  * is stripped off, and the inner packet is passed to register_mforward.
 1681  */
 1682 int
 1683 pim6_input(mp, offp, proto)
 1684         struct mbuf **mp;
 1685         int *offp, proto;
 1686 {
 1687         struct pim *pim; /* pointer to a pim struct */
 1688         struct ip6_hdr *ip6;
 1689         int pimlen;
 1690         struct mbuf *m = *mp;
 1691         int minlen;
 1692         int off = *offp;
 1693 
 1694         ++pim6stat.pim6s_rcv_total;
 1695 
 1696         ip6 = mtod(m, struct ip6_hdr *);
 1697         pimlen = m->m_pkthdr.len - *offp;
 1698 
 1699         /*
 1700          * Validate lengths
 1701          */
 1702         if (pimlen < PIM_MINLEN) {
 1703                 ++pim6stat.pim6s_rcv_tooshort;
 1704 #ifdef MRT6DEBUG
 1705                 if (mrt6debug & DEBUG_PIM)
 1706                         log(LOG_DEBUG,"pim6_input: PIM packet too short\n");
 1707 #endif
 1708                 m_freem(m);
 1709                 return (IPPROTO_DONE);
 1710         }
 1711 
 1712         /*
 1713          * if the packet is at least as big as a REGISTER, go ahead
 1714          * and grab the PIM REGISTER header size, to avoid another
 1715          * possible m_pullup() later.
 1716          *
 1717          * PIM_MINLEN       == pimhdr + u_int32 == 8
 1718          * PIM6_REG_MINLEN   == pimhdr + reghdr + eip6hdr == 4 + 4 + 40
 1719          */
 1720         minlen = (pimlen >= PIM6_REG_MINLEN) ? PIM6_REG_MINLEN : PIM_MINLEN;
 1721 
 1722         /*
 1723          * Make sure that the IP6 and PIM headers in contiguous memory, and
 1724          * possibly the PIM REGISTER header
 1725          */
 1726 #ifndef PULLDOWN_TEST
 1727         IP6_EXTHDR_CHECK(m, off, minlen, IPPROTO_DONE);
 1728         /* adjust pointer */
 1729         ip6 = mtod(m, struct ip6_hdr *);
 1730 
 1731         /* adjust mbuf to point to the PIM header */
 1732         pim = (struct pim *)((caddr_t)ip6 + off);
 1733 #else
 1734         IP6_EXTHDR_GET(pim, struct pim *, m, off, minlen);
 1735         if (pim == NULL) {
 1736                 pim6stat.pim6s_rcv_tooshort++;
 1737                 return (IPPROTO_DONE);
 1738         }
 1739 #endif
 1740 
 1741 #define PIM6_CHECKSUM
 1742 #ifdef PIM6_CHECKSUM
 1743         {
 1744                 int cksumlen;
 1745 
 1746                 /*
 1747                  * Validate checksum.
 1748                  * If PIM REGISTER, exclude the data packet
 1749                  */
 1750                 if (pim->pim_type == PIM_REGISTER)
 1751                         cksumlen = PIM_MINLEN;
 1752                 else
 1753                         cksumlen = pimlen;
 1754 
 1755                 if (in6_cksum(m, IPPROTO_PIM, off, cksumlen)) {
 1756                         ++pim6stat.pim6s_rcv_badsum;
 1757 #ifdef MRT6DEBUG
 1758                         if (mrt6debug & DEBUG_PIM)
 1759                                 log(LOG_DEBUG,
 1760                                     "pim6_input: invalid checksum\n");
 1761 #endif
 1762                         m_freem(m);
 1763                         return (IPPROTO_DONE);
 1764                 }
 1765         }
 1766 #endif /* PIM_CHECKSUM */
 1767 
 1768         /* PIM version check */
 1769         if (pim->pim_ver != PIM_VERSION) {
 1770                 ++pim6stat.pim6s_rcv_badversion;
 1771 #ifdef MRT6DEBUG
 1772                 log(LOG_ERR,
 1773                     "pim6_input: incorrect version %d, expecting %d\n",
 1774                     pim->pim_ver, PIM_VERSION);
 1775 #endif
 1776                 m_freem(m);
 1777                 return (IPPROTO_DONE);
 1778         }
 1779 
 1780         if (pim->pim_type == PIM_REGISTER) {
 1781                 /*
 1782                  * since this is a REGISTER, we'll make a copy of the register
 1783                  * headers ip6+pim+u_int32_t+encap_ip6, to be passed up to the
 1784                  * routing daemon.
 1785                  */
 1786                 static struct sockaddr_in6 dst = { sizeof(dst), AF_INET6 };
 1787 
 1788                 struct mbuf *mcp;
 1789                 struct ip6_hdr *eip6;
 1790                 u_int32_t *reghdr;
 1791                 int rc;
 1792 
 1793                 ++pim6stat.pim6s_rcv_registers;
 1794 
 1795                 if ((reg_mif_num >= nummifs) || (reg_mif_num == (mifi_t) -1)) {
 1796 #ifdef MRT6DEBUG
 1797                         if (mrt6debug & DEBUG_PIM)
 1798                                 log(LOG_DEBUG,
 1799                                     "pim6_input: register mif not set: %d\n",
 1800                                     reg_mif_num);
 1801 #endif
 1802                         m_freem(m);
 1803                         return (IPPROTO_DONE);
 1804                 }
 1805 
 1806                 reghdr = (u_int32_t *)(pim + 1);
 1807 
 1808                 if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
 1809                         goto pim6_input_to_daemon;
 1810 
 1811                 /*
 1812                  * Validate length
 1813                  */
 1814                 if (pimlen < PIM6_REG_MINLEN) {
 1815                         ++pim6stat.pim6s_rcv_tooshort;
 1816                         ++pim6stat.pim6s_rcv_badregisters;
 1817 #ifdef MRT6DEBUG
 1818                         log(LOG_ERR,
 1819                             "pim6_input: register packet size too "
 1820                             "small %d from %s\n",
 1821                             pimlen, ip6_sprintf(&ip6->ip6_src));
 1822 #endif
 1823                         m_freem(m);
 1824                         return (IPPROTO_DONE);
 1825                 }
 1826 
 1827                 eip6 = (struct ip6_hdr *) (reghdr + 1);
 1828 #ifdef MRT6DEBUG
 1829                 if (mrt6debug & DEBUG_PIM)
 1830                         log(LOG_DEBUG,
 1831                             "pim6_input[register], eip6: %s -> %s, "
 1832                             "eip6 plen %d\n",
 1833                             ip6_sprintf(&eip6->ip6_src),
 1834                             ip6_sprintf(&eip6->ip6_dst),
 1835                             ntohs(eip6->ip6_plen));
 1836 #endif
 1837 
 1838                 /* verify the version number of the inner packet */
 1839                 if ((eip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) {
 1840                         ++pim6stat.pim6s_rcv_badregisters;
 1841 #ifdef MRT6DEBUG
 1842                         log(LOG_DEBUG, "pim6_input: invalid IP version (%d) "
 1843                             "of the inner packet\n",
 1844                             (eip6->ip6_vfc & IPV6_VERSION));
 1845 #endif
 1846                         m_freem(m);
 1847                         return (IPPROTO_NONE);
 1848                 }
 1849 
 1850                 /* verify the inner packet is destined to a mcast group */
 1851                 if (!IN6_IS_ADDR_MULTICAST(&eip6->ip6_dst)) {
 1852                         ++pim6stat.pim6s_rcv_badregisters;
 1853 #ifdef MRT6DEBUG
 1854                         if (mrt6debug & DEBUG_PIM)
 1855                                 log(LOG_DEBUG,
 1856                                     "pim6_input: inner packet of register "
 1857                                     "is not multicast %s\n",
 1858                                     ip6_sprintf(&eip6->ip6_dst));
 1859 #endif
 1860                         m_freem(m);
 1861                         return (IPPROTO_DONE);
 1862                 }
 1863 
 1864                 /*
 1865                  * make a copy of the whole header to pass to the daemon later.
 1866                  */
 1867                 mcp = m_copy(m, 0, off + PIM6_REG_MINLEN);
 1868                 if (mcp == NULL) {
 1869 #ifdef MRT6DEBUG
 1870                         log(LOG_ERR,
 1871                             "pim6_input: pim register: "
 1872                             "could not copy register head\n");
 1873 #endif
 1874                         m_freem(m);
 1875                         return (IPPROTO_DONE);
 1876                 }
 1877 
 1878                 /*
 1879                  * forward the inner ip6 packet; point m_data at the inner ip6.
 1880                  */
 1881                 m_adj(m, off + PIM_MINLEN);
 1882 #ifdef MRT6DEBUG
 1883                 if (mrt6debug & DEBUG_PIM) {
 1884                         log(LOG_DEBUG,
 1885                             "pim6_input: forwarding decapsulated register: "
 1886                             "src %s, dst %s, mif %d\n",
 1887                             ip6_sprintf(&eip6->ip6_src),
 1888                             ip6_sprintf(&eip6->ip6_dst),
 1889                             reg_mif_num);
 1890                 }
 1891 #endif
 1892 
 1893                 rc = if_simloop(mif6table[reg_mif_num].m6_ifp, m,
 1894                                 dst.sin6_family, 0);
 1895 
 1896                 /* prepare the register head to send to the mrouting daemon */
 1897                 m = mcp;
 1898         }
 1899 
 1900         /*
 1901          * Pass the PIM message up to the daemon; if it is a register message
 1902          * pass the 'head' only up to the daemon. This includes the
 1903          * encapsulator ip6 header, pim header, register header and the
 1904          * encapsulated ip6 header.
 1905          */
 1906   pim6_input_to_daemon:
 1907         rip6_input(&m, offp, proto);
 1908         return (IPPROTO_DONE);
 1909 }

Cache object: d38280caf92119dc6d85a855f5490d53


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