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

Cache object: 6c96a1f5c1985f4a075299cef5dd1364


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