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