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