Home | History | Annotate | Line # | Download | only in netinet
ip_mroute.c revision 1.88
      1 /*	$NetBSD: ip_mroute.c,v 1.88 2005/02/02 21:41:55 perry Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *      The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Stephen Deering of Stanford University.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *      @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
     35  */
     36 
     37 /*
     38  * Copyright (c) 1989 Stephen Deering
     39  *
     40  * This code is derived from software contributed to Berkeley by
     41  * Stephen Deering of Stanford University.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *      This product includes software developed by the University of
     54  *      California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *      @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
     72  */
     73 
     74 /*
     75  * IP multicast forwarding procedures
     76  *
     77  * Written by David Waitzman, BBN Labs, August 1988.
     78  * Modified by Steve Deering, Stanford, February 1989.
     79  * Modified by Mark J. Steiglitz, Stanford, May, 1991
     80  * Modified by Van Jacobson, LBL, January 1993
     81  * Modified by Ajit Thyagarajan, PARC, August 1993
     82  * Modified by Bill Fenner, PARC, April 1994
     83  * Modified by Charles M. Hannum, NetBSD, May 1995.
     84  * Modified by Ahmed Helmy, SGI, June 1996
     85  * Modified by George Edmond Eddy (Rusty), ISI, February 1998
     86  * Modified by Pavlin Radoslavov, USC/ISI, May 1998, August 1999, October 2000
     87  * Modified by Hitoshi Asaeda, WIDE, August 2000
     88  * Modified by Pavlin Radoslavov, ICSI, October 2002
     89  *
     90  * MROUTING Revision: 1.2
     91  * and PIM-SMv2 and PIM-DM support, advanced API support,
     92  * bandwidth metering and signaling
     93  */
     94 
     95 #include <sys/cdefs.h>
     96 __KERNEL_RCSID(0, "$NetBSD: ip_mroute.c,v 1.88 2005/02/02 21:41:55 perry Exp $");
     97 
     98 #include "opt_inet.h"
     99 #include "opt_ipsec.h"
    100 #include "opt_pim.h"
    101 
    102 #ifdef PIM
    103 #define _PIM_VT 1
    104 #endif
    105 
    106 #include <sys/param.h>
    107 #include <sys/systm.h>
    108 #include <sys/callout.h>
    109 #include <sys/mbuf.h>
    110 #include <sys/socket.h>
    111 #include <sys/socketvar.h>
    112 #include <sys/protosw.h>
    113 #include <sys/errno.h>
    114 #include <sys/time.h>
    115 #include <sys/kernel.h>
    116 #include <sys/ioctl.h>
    117 #include <sys/syslog.h>
    118 
    119 #include <net/if.h>
    120 #include <net/route.h>
    121 #include <net/raw_cb.h>
    122 
    123 #include <netinet/in.h>
    124 #include <netinet/in_var.h>
    125 #include <netinet/in_systm.h>
    126 #include <netinet/ip.h>
    127 #include <netinet/ip_var.h>
    128 #include <netinet/in_pcb.h>
    129 #include <netinet/udp.h>
    130 #include <netinet/igmp.h>
    131 #include <netinet/igmp_var.h>
    132 #include <netinet/ip_mroute.h>
    133 #ifdef PIM
    134 #include <netinet/pim.h>
    135 #include <netinet/pim_var.h>
    136 #endif
    137 #include <netinet/ip_encap.h>
    138 
    139 #ifdef IPSEC
    140 #include <netinet6/ipsec.h>
    141 #include <netkey/key.h>
    142 #endif
    143 
    144 #ifdef FAST_IPSEC
    145 #include <netipsec/ipsec.h>
    146 #include <netipsec/key.h>
    147 #endif
    148 
    149 #include <machine/stdarg.h>
    150 
    151 #define IP_MULTICASTOPTS 0
    152 #define	M_PULLUP(m, len)						 \
    153 	do {								 \
    154 		if ((m) && ((m)->m_flags & M_EXT || (m)->m_len < (len))) \
    155 			(m) = m_pullup((m), (len));			 \
    156 	} while (/*CONSTCOND*/ 0)
    157 
    158 /*
    159  * Globals.  All but ip_mrouter and ip_mrtproto could be static,
    160  * except for netstat or debugging purposes.
    161  */
    162 struct socket  *ip_mrouter  = NULL;
    163 int		ip_mrtproto = IGMP_DVMRP;    /* for netstat only */
    164 
    165 #define NO_RTE_FOUND 	0x1
    166 #define RTE_FOUND	0x2
    167 
    168 #define	MFCHASH(a, g)							\
    169 	((((a).s_addr >> 20) ^ ((a).s_addr >> 10) ^ (a).s_addr ^	\
    170 	  ((g).s_addr >> 20) ^ ((g).s_addr >> 10) ^ (g).s_addr) & mfchash)
    171 LIST_HEAD(mfchashhdr, mfc) *mfchashtbl;
    172 u_long	mfchash;
    173 
    174 u_char		nexpire[MFCTBLSIZ];
    175 struct vif	viftable[MAXVIFS];
    176 struct mrtstat	mrtstat;
    177 u_int		mrtdebug = 0;	  /* debug level 	*/
    178 #define		DEBUG_MFC	0x02
    179 #define		DEBUG_FORWARD	0x04
    180 #define		DEBUG_EXPIRE	0x08
    181 #define		DEBUG_XMIT	0x10
    182 #define		DEBUG_PIM	0x20
    183 
    184 #define		VIFI_INVALID	((vifi_t) -1)
    185 
    186 u_int       	tbfdebug = 0;     /* tbf debug level 	*/
    187 #ifdef RSVP_ISI
    188 u_int		rsvpdebug = 0;	  /* rsvp debug level   */
    189 extern struct socket *ip_rsvpd;
    190 extern int rsvp_on;
    191 #endif /* RSVP_ISI */
    192 
    193 /* vif attachment using sys/netinet/ip_encap.c */
    194 static void vif_input(struct mbuf *, ...);
    195 static int vif_encapcheck(const struct mbuf *, int, int, void *);
    196 
    197 static const struct protosw vif_protosw =
    198 { SOCK_RAW,	&inetdomain,	IPPROTO_IPV4,	PR_ATOMIC|PR_ADDR,
    199   vif_input,	rip_output,	0,		rip_ctloutput,
    200   rip_usrreq,
    201   0,            0,              0,              0,
    202 };
    203 
    204 #define		EXPIRE_TIMEOUT	(hz / 4)	/* 4x / second */
    205 #define		UPCALL_EXPIRE	6		/* number of timeouts */
    206 
    207 /*
    208  * Define the token bucket filter structures
    209  */
    210 
    211 #define		TBF_REPROCESS	(hz / 100)	/* 100x / second */
    212 
    213 static int get_sg_cnt(struct sioc_sg_req *);
    214 static int get_vif_cnt(struct sioc_vif_req *);
    215 static int ip_mrouter_init(struct socket *, struct mbuf *);
    216 static int get_version(struct mbuf *);
    217 static int set_assert(struct mbuf *);
    218 static int get_assert(struct mbuf *);
    219 static int add_vif(struct mbuf *);
    220 static int del_vif(struct mbuf *);
    221 static void update_mfc_params(struct mfc *, struct mfcctl2 *);
    222 static void init_mfc_params(struct mfc *, struct mfcctl2 *);
    223 static void expire_mfc(struct mfc *);
    224 static int add_mfc(struct mbuf *);
    225 #ifdef UPCALL_TIMING
    226 static void collate(struct timeval *);
    227 #endif
    228 static int del_mfc(struct mbuf *);
    229 static int set_api_config(struct mbuf *); /* chose API capabilities */
    230 static int get_api_support(struct mbuf *);
    231 static int get_api_config(struct mbuf *);
    232 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *);
    233 static void expire_upcalls(void *);
    234 #ifdef RSVP_ISI
    235 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t);
    236 #else
    237 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *);
    238 #endif
    239 static void phyint_send(struct ip *, struct vif *, struct mbuf *);
    240 static void encap_send(struct ip *, struct vif *, struct mbuf *);
    241 static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_int32_t);
    242 static void tbf_queue(struct vif *, struct mbuf *);
    243 static void tbf_process_q(struct vif *);
    244 static void tbf_reprocess_q(void *);
    245 static int tbf_dq_sel(struct vif *, struct ip *);
    246 static void tbf_send_packet(struct vif *, struct mbuf *);
    247 static void tbf_update_tokens(struct vif *);
    248 static int priority(struct vif *, struct ip *);
    249 
    250 /*
    251  * Bandwidth monitoring
    252  */
    253 static void free_bw_list(struct bw_meter *);
    254 static int add_bw_upcall(struct mbuf *);
    255 static int del_bw_upcall(struct mbuf *);
    256 static void bw_meter_receive_packet(struct bw_meter *, int , struct timeval *);
    257 static void bw_meter_prepare_upcall(struct bw_meter *, struct timeval *);
    258 static void bw_upcalls_send(void);
    259 static void schedule_bw_meter(struct bw_meter *, struct timeval *);
    260 static void unschedule_bw_meter(struct bw_meter *);
    261 static void bw_meter_process(void);
    262 static void expire_bw_upcalls_send(void *);
    263 static void expire_bw_meter_process(void *);
    264 
    265 #ifdef PIM
    266 static int pim_register_send(struct ip *, struct vif *,
    267 		struct mbuf *, struct mfc *);
    268 static int pim_register_send_rp(struct ip *, struct vif *,
    269 		struct mbuf *, struct mfc *);
    270 static int pim_register_send_upcall(struct ip *, struct vif *,
    271 		struct mbuf *, struct mfc *);
    272 static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *);
    273 #endif
    274 
    275 /*
    276  * 'Interfaces' associated with decapsulator (so we can tell
    277  * packets that went through it from ones that get reflected
    278  * by a broken gateway).  These interfaces are never linked into
    279  * the system ifnet list & no routes point to them.  I.e., packets
    280  * can't be sent this way.  They only exist as a placeholder for
    281  * multicast source verification.
    282  */
    283 #if 0
    284 struct ifnet multicast_decap_if[MAXVIFS];
    285 #endif
    286 
    287 #define	ENCAP_TTL	64
    288 #define	ENCAP_PROTO	IPPROTO_IPIP	/* 4 */
    289 
    290 /* prototype IP hdr for encapsulated packets */
    291 struct ip multicast_encap_iphdr = {
    292 #if BYTE_ORDER == LITTLE_ENDIAN
    293 	sizeof(struct ip) >> 2, IPVERSION,
    294 #else
    295 	IPVERSION, sizeof(struct ip) >> 2,
    296 #endif
    297 	0,				/* tos */
    298 	sizeof(struct ip),		/* total length */
    299 	0,				/* id */
    300 	0,				/* frag offset */
    301 	ENCAP_TTL, ENCAP_PROTO,
    302 	0,				/* checksum */
    303 };
    304 
    305 /*
    306  * Bandwidth meter variables and constants
    307  */
    308 
    309 /*
    310  * Pending timeouts are stored in a hash table, the key being the
    311  * expiration time. Periodically, the entries are analysed and processed.
    312  */
    313 #define BW_METER_BUCKETS	1024
    314 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS];
    315 struct callout bw_meter_ch;
    316 #define BW_METER_PERIOD (hz)		/* periodical handling of bw meters */
    317 
    318 /*
    319  * Pending upcalls are stored in a vector which is flushed when
    320  * full, or periodically
    321  */
    322 static struct bw_upcall	bw_upcalls[BW_UPCALLS_MAX];
    323 static u_int	bw_upcalls_n; /* # of pending upcalls */
    324 struct callout	bw_upcalls_ch;
    325 #define BW_UPCALLS_PERIOD (hz)		/* periodical flush of bw upcalls */
    326 
    327 #ifdef PIM
    328 struct pimstat pimstat;
    329 
    330 /*
    331  * Note: the PIM Register encapsulation adds the following in front of a
    332  * data packet:
    333  *
    334  * struct pim_encap_hdr {
    335  *    struct ip ip;
    336  *    struct pim_encap_pimhdr  pim;
    337  * }
    338  *
    339  */
    340 
    341 struct pim_encap_pimhdr {
    342 	struct pim pim;
    343 	uint32_t   flags;
    344 };
    345 
    346 static struct ip pim_encap_iphdr = {
    347 #if BYTE_ORDER == LITTLE_ENDIAN
    348 	sizeof(struct ip) >> 2,
    349 	IPVERSION,
    350 #else
    351 	IPVERSION,
    352 	sizeof(struct ip) >> 2,
    353 #endif
    354 	0,			/* tos */
    355 	sizeof(struct ip),	/* total length */
    356 	0,			/* id */
    357 	0,			/* frag offset */
    358 	ENCAP_TTL,
    359 	IPPROTO_PIM,
    360 	0,			/* checksum */
    361 };
    362 
    363 static struct pim_encap_pimhdr pim_encap_pimhdr = {
    364     {
    365 	PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */
    366 	0,			/* reserved */
    367 	0,			/* checksum */
    368     },
    369     0				/* flags */
    370 };
    371 
    372 static struct ifnet multicast_register_if;
    373 static vifi_t reg_vif_num = VIFI_INVALID;
    374 #endif /* PIM */
    375 
    376 
    377 /*
    378  * Private variables.
    379  */
    380 static vifi_t	   numvifs = 0;
    381 
    382 static struct callout expire_upcalls_ch;
    383 
    384 /*
    385  * one-back cache used by vif_encapcheck to locate a tunnel's vif
    386  * given a datagram's src ip address.
    387  */
    388 static struct in_addr last_encap_src;
    389 static struct vif *last_encap_vif;
    390 
    391 /*
    392  * whether or not special PIM assert processing is enabled.
    393  */
    394 static int pim_assert;
    395 /*
    396  * Rate limit for assert notification messages, in usec
    397  */
    398 #define ASSERT_MSG_TIME		3000000
    399 
    400 /*
    401  * Kernel multicast routing API capabilities and setup.
    402  * If more API capabilities are added to the kernel, they should be
    403  * recorded in `mrt_api_support'.
    404  */
    405 static const u_int32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF |
    406 					  MRT_MFC_FLAGS_BORDER_VIF |
    407 					  MRT_MFC_RP |
    408 					  MRT_MFC_BW_UPCALL);
    409 static u_int32_t mrt_api_config = 0;
    410 
    411 /*
    412  * Find a route for a given origin IP address and Multicast group address
    413  * Type of service parameter to be added in the future!!!
    414  * Statistics are updated by the caller if needed
    415  * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
    416  */
    417 static struct mfc *
    418 mfc_find(struct in_addr *o, struct in_addr *g)
    419 {
    420 	struct mfc *rt;
    421 
    422 	LIST_FOREACH(rt, &mfchashtbl[MFCHASH(*o, *g)], mfc_hash) {
    423 		if (in_hosteq(rt->mfc_origin, *o) &&
    424 		    in_hosteq(rt->mfc_mcastgrp, *g) &&
    425 		    (rt->mfc_stall == NULL))
    426 			break;
    427 	}
    428 
    429 	return (rt);
    430 }
    431 
    432 /*
    433  * Macros to compute elapsed time efficiently
    434  * Borrowed from Van Jacobson's scheduling code
    435  */
    436 #define TV_DELTA(a, b, delta) do {					\
    437 	int xxs;							\
    438 	delta = (a).tv_usec - (b).tv_usec;				\
    439 	xxs = (a).tv_sec - (b).tv_sec;					\
    440 	switch (xxs) {							\
    441 	case 2:								\
    442 		delta += 1000000;					\
    443 		/* fall through */					\
    444 	case 1:								\
    445 		delta += 1000000;					\
    446 		/* fall through */					\
    447 	case 0:								\
    448 		break;							\
    449 	default:							\
    450 		delta += (1000000 * xxs);				\
    451 		break;							\
    452 	}								\
    453 } while (/*CONSTCOND*/ 0)
    454 
    455 #ifdef UPCALL_TIMING
    456 u_int32_t upcall_data[51];
    457 #endif /* UPCALL_TIMING */
    458 
    459 /*
    460  * Handle MRT setsockopt commands to modify the multicast routing tables.
    461  */
    462 int
    463 ip_mrouter_set(so, optname, m)
    464 	struct socket *so;
    465 	int optname;
    466 	struct mbuf **m;
    467 {
    468 	int error;
    469 
    470 	if (optname != MRT_INIT && so != ip_mrouter)
    471 		error = ENOPROTOOPT;
    472 	else
    473 		switch (optname) {
    474 		case MRT_INIT:
    475 			error = ip_mrouter_init(so, *m);
    476 			break;
    477 		case MRT_DONE:
    478 			error = ip_mrouter_done();
    479 			break;
    480 		case MRT_ADD_VIF:
    481 			error = add_vif(*m);
    482 			break;
    483 		case MRT_DEL_VIF:
    484 			error = del_vif(*m);
    485 			break;
    486 		case MRT_ADD_MFC:
    487 			error = add_mfc(*m);
    488 			break;
    489 		case MRT_DEL_MFC:
    490 			error = del_mfc(*m);
    491 			break;
    492 		case MRT_ASSERT:
    493 			error = set_assert(*m);
    494 			break;
    495 		case MRT_API_CONFIG:
    496 			error = set_api_config(*m);
    497 			break;
    498 		case MRT_ADD_BW_UPCALL:
    499 			error = add_bw_upcall(*m);
    500 			break;
    501 		case MRT_DEL_BW_UPCALL:
    502 			error = del_bw_upcall(*m);
    503 			break;
    504 		default:
    505 			error = ENOPROTOOPT;
    506 			break;
    507 		}
    508 
    509 	if (*m)
    510 		m_free(*m);
    511 	return (error);
    512 }
    513 
    514 /*
    515  * Handle MRT getsockopt commands
    516  */
    517 int
    518 ip_mrouter_get(so, optname, m)
    519 	struct socket *so;
    520 	int optname;
    521 	struct mbuf **m;
    522 {
    523 	int error;
    524 
    525 	if (so != ip_mrouter)
    526 		error = ENOPROTOOPT;
    527 	else {
    528 		*m = m_get(M_WAIT, MT_SOOPTS);
    529 		MCLAIM(*m, so->so_mowner);
    530 
    531 		switch (optname) {
    532 		case MRT_VERSION:
    533 			error = get_version(*m);
    534 			break;
    535 		case MRT_ASSERT:
    536 			error = get_assert(*m);
    537 			break;
    538 		case MRT_API_SUPPORT:
    539 			error = get_api_support(*m);
    540 			break;
    541 		case MRT_API_CONFIG:
    542 			error = get_api_config(*m);
    543 			break;
    544 		default:
    545 			error = ENOPROTOOPT;
    546 			break;
    547 		}
    548 
    549 		if (error)
    550 			m_free(*m);
    551 	}
    552 
    553 	return (error);
    554 }
    555 
    556 /*
    557  * Handle ioctl commands to obtain information from the cache
    558  */
    559 int
    560 mrt_ioctl(so, cmd, data)
    561 	struct socket *so;
    562 	u_long cmd;
    563 	caddr_t data;
    564 {
    565 	int error;
    566 
    567 	if (so != ip_mrouter)
    568 		error = EINVAL;
    569 	else
    570 		switch (cmd) {
    571 		case SIOCGETVIFCNT:
    572 			error = get_vif_cnt((struct sioc_vif_req *)data);
    573 			break;
    574 		case SIOCGETSGCNT:
    575 			error = get_sg_cnt((struct sioc_sg_req *)data);
    576 			break;
    577 		default:
    578 			error = EINVAL;
    579 			break;
    580 		}
    581 
    582 	return (error);
    583 }
    584 
    585 /*
    586  * returns the packet, byte, rpf-failure count for the source group provided
    587  */
    588 static int
    589 get_sg_cnt(req)
    590 	struct sioc_sg_req *req;
    591 {
    592 	int s;
    593 	struct mfc *rt;
    594 
    595 	s = splsoftnet();
    596 	rt = mfc_find(&req->src, &req->grp);
    597 	if (rt == NULL) {
    598 		splx(s);
    599 		req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
    600 		return (EADDRNOTAVAIL);
    601 	}
    602 	req->pktcnt = rt->mfc_pkt_cnt;
    603 	req->bytecnt = rt->mfc_byte_cnt;
    604 	req->wrong_if = rt->mfc_wrong_if;
    605 	splx(s);
    606 
    607 	return (0);
    608 }
    609 
    610 /*
    611  * returns the input and output packet and byte counts on the vif provided
    612  */
    613 static int
    614 get_vif_cnt(req)
    615 	struct sioc_vif_req *req;
    616 {
    617 	vifi_t vifi = req->vifi;
    618 
    619 	if (vifi >= numvifs)
    620 		return (EINVAL);
    621 
    622 	req->icount = viftable[vifi].v_pkt_in;
    623 	req->ocount = viftable[vifi].v_pkt_out;
    624 	req->ibytes = viftable[vifi].v_bytes_in;
    625 	req->obytes = viftable[vifi].v_bytes_out;
    626 
    627 	return (0);
    628 }
    629 
    630 /*
    631  * Enable multicast routing
    632  */
    633 static int
    634 ip_mrouter_init(so, m)
    635 	struct socket *so;
    636 	struct mbuf *m;
    637 {
    638 	int *v;
    639 
    640 	if (mrtdebug)
    641 		log(LOG_DEBUG,
    642 		    "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
    643 		    so->so_type, so->so_proto->pr_protocol);
    644 
    645 	if (so->so_type != SOCK_RAW ||
    646 	    so->so_proto->pr_protocol != IPPROTO_IGMP)
    647 		return (EOPNOTSUPP);
    648 
    649 	if (m == NULL || m->m_len < sizeof(int))
    650 		return (EINVAL);
    651 
    652 	v = mtod(m, int *);
    653 	if (*v != 1)
    654 		return (EINVAL);
    655 
    656 	if (ip_mrouter != NULL)
    657 		return (EADDRINUSE);
    658 
    659 	ip_mrouter = so;
    660 
    661 	mfchashtbl =
    662 	    hashinit(MFCTBLSIZ, HASH_LIST, M_MRTABLE, M_WAITOK, &mfchash);
    663 	bzero((caddr_t)nexpire, sizeof(nexpire));
    664 
    665 	pim_assert = 0;
    666 
    667 	callout_init(&expire_upcalls_ch);
    668 	callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
    669 		      expire_upcalls, NULL);
    670 
    671 	callout_init(&bw_upcalls_ch);
    672 	callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
    673 		      expire_bw_upcalls_send, NULL);
    674 
    675 	callout_init(&bw_meter_ch);
    676 	callout_reset(&bw_meter_ch, BW_METER_PERIOD,
    677 		      expire_bw_meter_process, NULL);
    678 
    679 	if (mrtdebug)
    680 		log(LOG_DEBUG, "ip_mrouter_init\n");
    681 
    682 	return (0);
    683 }
    684 
    685 /*
    686  * Disable multicast routing
    687  */
    688 int
    689 ip_mrouter_done()
    690 {
    691 	vifi_t vifi;
    692 	struct vif *vifp;
    693 	int i;
    694 	int s;
    695 
    696 	s = splsoftnet();
    697 
    698 	/* Clear out all the vifs currently in use. */
    699 	for (vifi = 0; vifi < numvifs; vifi++) {
    700 		vifp = &viftable[vifi];
    701 		if (!in_nullhost(vifp->v_lcl_addr))
    702 			reset_vif(vifp);
    703 	}
    704 
    705 	numvifs = 0;
    706 	pim_assert = 0;
    707 	mrt_api_config = 0;
    708 
    709 	callout_stop(&expire_upcalls_ch);
    710 	callout_stop(&bw_upcalls_ch);
    711 	callout_stop(&bw_meter_ch);
    712 
    713 	/*
    714 	 * Free all multicast forwarding cache entries.
    715 	 */
    716 	for (i = 0; i < MFCTBLSIZ; i++) {
    717 		struct mfc *rt, *nrt;
    718 
    719 		for (rt = LIST_FIRST(&mfchashtbl[i]); rt; rt = nrt) {
    720 			nrt = LIST_NEXT(rt, mfc_hash);
    721 
    722 			expire_mfc(rt);
    723 		}
    724 	}
    725 
    726 	bzero((caddr_t)nexpire, sizeof(nexpire));
    727 	free(mfchashtbl, M_MRTABLE);
    728 	mfchashtbl = NULL;
    729 
    730 	bw_upcalls_n = 0;
    731 	bzero(bw_meter_timers, sizeof(bw_meter_timers));
    732 
    733 	/* Reset de-encapsulation cache. */
    734 
    735 	ip_mrouter = NULL;
    736 
    737 	splx(s);
    738 
    739 	if (mrtdebug)
    740 		log(LOG_DEBUG, "ip_mrouter_done\n");
    741 
    742 	return (0);
    743 }
    744 
    745 void
    746 ip_mrouter_detach(ifp)
    747 	struct ifnet *ifp;
    748 {
    749 	int vifi, i;
    750 	struct vif *vifp;
    751 	struct mfc *rt;
    752 	struct rtdetq *rte;
    753 
    754 	/* XXX not sure about side effect to userland routing daemon */
    755 	for (vifi = 0; vifi < numvifs; vifi++) {
    756 		vifp = &viftable[vifi];
    757 		if (vifp->v_ifp == ifp)
    758 			reset_vif(vifp);
    759 	}
    760 	for (i = 0; i < MFCTBLSIZ; i++) {
    761 		if (nexpire[i] == 0)
    762 			continue;
    763 		LIST_FOREACH(rt, &mfchashtbl[i], mfc_hash) {
    764 			for (rte = rt->mfc_stall; rte; rte = rte->next) {
    765 				if (rte->ifp == ifp)
    766 					rte->ifp = NULL;
    767 			}
    768 		}
    769 	}
    770 }
    771 
    772 static int
    773 get_version(m)
    774 	struct mbuf *m;
    775 {
    776 	int *v = mtod(m, int *);
    777 
    778 	*v = 0x0305;	/* XXX !!!! */
    779 	m->m_len = sizeof(int);
    780 	return (0);
    781 }
    782 
    783 /*
    784  * Set PIM assert processing global
    785  */
    786 static int
    787 set_assert(m)
    788 	struct mbuf *m;
    789 {
    790 	int *i;
    791 
    792 	if (m == NULL || m->m_len < sizeof(int))
    793 		return (EINVAL);
    794 
    795 	i = mtod(m, int *);
    796 	pim_assert = !!*i;
    797 	return (0);
    798 }
    799 
    800 /*
    801  * Get PIM assert processing global
    802  */
    803 static int
    804 get_assert(m)
    805 	struct mbuf *m;
    806 {
    807 	int *i = mtod(m, int *);
    808 
    809 	*i = pim_assert;
    810 	m->m_len = sizeof(int);
    811 	return (0);
    812 }
    813 
    814 /*
    815  * Configure API capabilities
    816  */
    817 static int
    818 set_api_config(struct mbuf *m)
    819 {
    820 	int i;
    821 	u_int32_t *apival;
    822 
    823 	if (m == NULL || m->m_len < sizeof(u_int32_t))
    824 		return (EINVAL);
    825 
    826 	apival = mtod(m, u_int32_t *);
    827 
    828 	/*
    829 	 * We can set the API capabilities only if it is the first operation
    830 	 * after MRT_INIT. I.e.:
    831 	 *  - there are no vifs installed
    832 	 *  - pim_assert is not enabled
    833 	 *  - the MFC table is empty
    834 	 */
    835 	if (numvifs > 0) {
    836 		*apival = 0;
    837 		return (EPERM);
    838 	}
    839 	if (pim_assert) {
    840 		*apival = 0;
    841 		return (EPERM);
    842 	}
    843 	for (i = 0; i < MFCTBLSIZ; i++) {
    844 		if (LIST_FIRST(&mfchashtbl[i]) != NULL) {
    845 			*apival = 0;
    846 			return (EPERM);
    847 		}
    848 	}
    849 
    850 	mrt_api_config = *apival & mrt_api_support;
    851 	*apival = mrt_api_config;
    852 
    853 	return (0);
    854 }
    855 
    856 /*
    857  * Get API capabilities
    858  */
    859 static int
    860 get_api_support(struct mbuf *m)
    861 {
    862 	u_int32_t *apival;
    863 
    864 	if (m == NULL || m->m_len < sizeof(u_int32_t))
    865 		return (EINVAL);
    866 
    867 	apival = mtod(m, u_int32_t *);
    868 
    869 	*apival = mrt_api_support;
    870 
    871 	return (0);
    872 }
    873 
    874 /*
    875  * Get API configured capabilities
    876  */
    877 static int
    878 get_api_config(struct mbuf *m)
    879 {
    880 	u_int32_t *apival;
    881 
    882 	if (m == NULL || m->m_len < sizeof(u_int32_t))
    883 		return (EINVAL);
    884 
    885 	apival = mtod(m, u_int32_t *);
    886 
    887 	*apival = mrt_api_config;
    888 
    889 	return (0);
    890 }
    891 
    892 static struct sockaddr_in sin = { sizeof(sin), AF_INET };
    893 
    894 /*
    895  * Add a vif to the vif table
    896  */
    897 static int
    898 add_vif(m)
    899 	struct mbuf *m;
    900 {
    901 	struct vifctl *vifcp;
    902 	struct vif *vifp;
    903 	struct ifaddr *ifa;
    904 	struct ifnet *ifp;
    905 	struct ifreq ifr;
    906 	int error, s;
    907 
    908 	if (m == NULL || m->m_len < sizeof(struct vifctl))
    909 		return (EINVAL);
    910 
    911 	vifcp = mtod(m, struct vifctl *);
    912 	if (vifcp->vifc_vifi >= MAXVIFS)
    913 		return (EINVAL);
    914 	if (in_nullhost(vifcp->vifc_lcl_addr))
    915 		return (EADDRNOTAVAIL);
    916 
    917 	vifp = &viftable[vifcp->vifc_vifi];
    918 	if (!in_nullhost(vifp->v_lcl_addr))
    919 		return (EADDRINUSE);
    920 
    921 	/* Find the interface with an address in AF_INET family. */
    922 #ifdef PIM
    923 	if (vifcp->vifc_flags & VIFF_REGISTER) {
    924 		/*
    925 		 * XXX: Because VIFF_REGISTER does not really need a valid
    926 		 * local interface (e.g. it could be 127.0.0.2), we don't
    927 		 * check its address.
    928 		 */
    929 	    ifp = NULL;
    930 	} else
    931 #endif
    932 	{
    933 		sin.sin_addr = vifcp->vifc_lcl_addr;
    934 		ifa = ifa_ifwithaddr(sintosa(&sin));
    935 		if (ifa == NULL)
    936 			return (EADDRNOTAVAIL);
    937 		ifp = ifa->ifa_ifp;
    938 	}
    939 
    940 	if (vifcp->vifc_flags & VIFF_TUNNEL) {
    941 		if (vifcp->vifc_flags & VIFF_SRCRT) {
    942 			log(LOG_ERR, "source routed tunnels not supported\n");
    943 			return (EOPNOTSUPP);
    944 		}
    945 
    946 		/* attach this vif to decapsulator dispatch table */
    947 		vifp->v_encap_cookie = encap_attach_func(AF_INET, IPPROTO_IPV4,
    948 		    vif_encapcheck, &vif_protosw, vifp);
    949 		if (!vifp->v_encap_cookie)
    950 			return (EINVAL);
    951 
    952 		/* Create a fake encapsulation interface. */
    953 		ifp = (struct ifnet *)malloc(sizeof(*ifp), M_MRTABLE, M_WAITOK);
    954 		bzero(ifp, sizeof(*ifp));
    955 		snprintf(ifp->if_xname, sizeof(ifp->if_xname),
    956 			 "mdecap%d", vifcp->vifc_vifi);
    957 
    958 		/* Prepare cached route entry. */
    959 		bzero(&vifp->v_route, sizeof(vifp->v_route));
    960 #ifdef PIM
    961 	} else if (vifcp->vifc_flags & VIFF_REGISTER) {
    962 		ifp = &multicast_register_if;
    963 		if (mrtdebug)
    964 			log(LOG_DEBUG, "Adding a register vif, ifp: %p\n",
    965 			    (void *)ifp);
    966 		if (reg_vif_num == VIFI_INVALID) {
    967 			bzero(ifp, sizeof(*ifp));
    968 			snprintf(ifp->if_xname, sizeof(ifp->if_xname),
    969 				 "register_vif");
    970 			ifp->if_flags = IFF_LOOPBACK;
    971 			bzero(&vifp->v_route, sizeof(vifp->v_route));
    972 			reg_vif_num = vifcp->vifc_vifi;
    973 		}
    974 #endif
    975 	} else {
    976 		/* Make sure the interface supports multicast. */
    977 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
    978 			return (EOPNOTSUPP);
    979 
    980 		/* Enable promiscuous reception of all IP multicasts. */
    981 		satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
    982 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
    983 		satosin(&ifr.ifr_addr)->sin_addr = zeroin_addr;
    984 		error = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, (caddr_t)&ifr);
    985 		if (error)
    986 			return (error);
    987 	}
    988 
    989 	s = splsoftnet();
    990 
    991 	/* Define parameters for the tbf structure. */
    992 	vifp->tbf_q = NULL;
    993 	vifp->tbf_t = &vifp->tbf_q;
    994 	microtime(&vifp->tbf_last_pkt_t);
    995 	vifp->tbf_n_tok = 0;
    996 	vifp->tbf_q_len = 0;
    997 	vifp->tbf_max_q_len = MAXQSIZE;
    998 
    999 	vifp->v_flags = vifcp->vifc_flags;
   1000 	vifp->v_threshold = vifcp->vifc_threshold;
   1001 	/* scaling up here allows division by 1024 in critical code */
   1002 	vifp->v_rate_limit = vifcp->vifc_rate_limit * 1024 / 1000;
   1003 	vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
   1004 	vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
   1005 	vifp->v_ifp = ifp;
   1006 	/* Initialize per vif pkt counters. */
   1007 	vifp->v_pkt_in = 0;
   1008 	vifp->v_pkt_out = 0;
   1009 	vifp->v_bytes_in = 0;
   1010 	vifp->v_bytes_out = 0;
   1011 
   1012 	callout_init(&vifp->v_repq_ch);
   1013 
   1014 #ifdef RSVP_ISI
   1015 	vifp->v_rsvp_on = 0;
   1016 	vifp->v_rsvpd = NULL;
   1017 #endif /* RSVP_ISI */
   1018 
   1019 	splx(s);
   1020 
   1021 	/* Adjust numvifs up if the vifi is higher than numvifs. */
   1022 	if (numvifs <= vifcp->vifc_vifi)
   1023 		numvifs = vifcp->vifc_vifi + 1;
   1024 
   1025 	if (mrtdebug)
   1026 		log(LOG_DEBUG, "add_vif #%d, lcladdr %x, %s %x, thresh %x, rate %d\n",
   1027 		    vifcp->vifc_vifi,
   1028 		    ntohl(vifcp->vifc_lcl_addr.s_addr),
   1029 		    (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask",
   1030 		    ntohl(vifcp->vifc_rmt_addr.s_addr),
   1031 		    vifcp->vifc_threshold,
   1032 		    vifcp->vifc_rate_limit);
   1033 
   1034 	return (0);
   1035 }
   1036 
   1037 void
   1038 reset_vif(vifp)
   1039 	struct vif *vifp;
   1040 {
   1041 	struct mbuf *m, *n;
   1042 	struct ifnet *ifp;
   1043 	struct ifreq ifr;
   1044 
   1045 	callout_stop(&vifp->v_repq_ch);
   1046 
   1047 	/* detach this vif from decapsulator dispatch table */
   1048 	encap_detach(vifp->v_encap_cookie);
   1049 	vifp->v_encap_cookie = NULL;
   1050 
   1051 	/*
   1052 	 * Free packets queued at the interface
   1053 	 */
   1054 	for (m = vifp->tbf_q; m != NULL; m = n) {
   1055 		n = m->m_nextpkt;
   1056 		m_freem(m);
   1057 	}
   1058 
   1059 	if (vifp->v_flags & VIFF_TUNNEL) {
   1060 		free(vifp->v_ifp, M_MRTABLE);
   1061 		if (vifp == last_encap_vif) {
   1062 			last_encap_vif = NULL;
   1063 			last_encap_src = zeroin_addr;
   1064 		}
   1065 	} else if (vifp->v_flags & VIFF_REGISTER) {
   1066 #ifdef PIM
   1067 		reg_vif_num = VIFI_INVALID;
   1068 #endif
   1069 	} else {
   1070 		satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
   1071 		satosin(&ifr.ifr_addr)->sin_family = AF_INET;
   1072 		satosin(&ifr.ifr_addr)->sin_addr = zeroin_addr;
   1073 		ifp = vifp->v_ifp;
   1074 		(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr);
   1075 	}
   1076 	bzero((caddr_t)vifp, sizeof(*vifp));
   1077 }
   1078 
   1079 /*
   1080  * Delete a vif from the vif table
   1081  */
   1082 static int
   1083 del_vif(m)
   1084 	struct mbuf *m;
   1085 {
   1086 	vifi_t *vifip;
   1087 	struct vif *vifp;
   1088 	vifi_t vifi;
   1089 	int s;
   1090 
   1091 	if (m == NULL || m->m_len < sizeof(vifi_t))
   1092 		return (EINVAL);
   1093 
   1094 	vifip = mtod(m, vifi_t *);
   1095 	if (*vifip >= numvifs)
   1096 		return (EINVAL);
   1097 
   1098 	vifp = &viftable[*vifip];
   1099 	if (in_nullhost(vifp->v_lcl_addr))
   1100 		return (EADDRNOTAVAIL);
   1101 
   1102 	s = splsoftnet();
   1103 
   1104 	reset_vif(vifp);
   1105 
   1106 	/* Adjust numvifs down */
   1107 	for (vifi = numvifs; vifi > 0; vifi--)
   1108 		if (!in_nullhost(viftable[vifi - 1].v_lcl_addr))
   1109 			break;
   1110 	numvifs = vifi;
   1111 
   1112 	splx(s);
   1113 
   1114 	if (mrtdebug)
   1115 		log(LOG_DEBUG, "del_vif %d, numvifs %d\n", *vifip, numvifs);
   1116 
   1117 	return (0);
   1118 }
   1119 
   1120 /*
   1121  * update an mfc entry without resetting counters and S,G addresses.
   1122  */
   1123 static void
   1124 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
   1125 {
   1126 	int i;
   1127 
   1128 	rt->mfc_parent = mfccp->mfcc_parent;
   1129 	for (i = 0; i < numvifs; i++) {
   1130 		rt->mfc_ttls[i] = mfccp->mfcc_ttls[i];
   1131 		rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config &
   1132 			MRT_MFC_FLAGS_ALL;
   1133 	}
   1134 	/* set the RP address */
   1135 	if (mrt_api_config & MRT_MFC_RP)
   1136 		rt->mfc_rp = mfccp->mfcc_rp;
   1137 	else
   1138 		rt->mfc_rp = zeroin_addr;
   1139 }
   1140 
   1141 /*
   1142  * fully initialize an mfc entry from the parameter.
   1143  */
   1144 static void
   1145 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp)
   1146 {
   1147 	rt->mfc_origin     = mfccp->mfcc_origin;
   1148 	rt->mfc_mcastgrp   = mfccp->mfcc_mcastgrp;
   1149 
   1150 	update_mfc_params(rt, mfccp);
   1151 
   1152 	/* initialize pkt counters per src-grp */
   1153 	rt->mfc_pkt_cnt    = 0;
   1154 	rt->mfc_byte_cnt   = 0;
   1155 	rt->mfc_wrong_if   = 0;
   1156 	timerclear(&rt->mfc_last_assert);
   1157 }
   1158 
   1159 static void
   1160 expire_mfc(rt)
   1161 	struct mfc *rt;
   1162 {
   1163 	struct rtdetq *rte, *nrte;
   1164 
   1165 	free_bw_list(rt->mfc_bw_meter);
   1166 
   1167 	for (rte = rt->mfc_stall; rte != NULL; rte = nrte) {
   1168 		nrte = rte->next;
   1169 		m_freem(rte->m);
   1170 		free(rte, M_MRTABLE);
   1171 	}
   1172 
   1173 	LIST_REMOVE(rt, mfc_hash);
   1174 	free(rt, M_MRTABLE);
   1175 }
   1176 
   1177 /*
   1178  * Add an mfc entry
   1179  */
   1180 static int
   1181 add_mfc(m)
   1182 	struct mbuf *m;
   1183 {
   1184 	struct mfcctl2 mfcctl2;
   1185 	struct mfcctl2 *mfccp;
   1186 	struct mfc *rt;
   1187 	u_int32_t hash = 0;
   1188 	struct rtdetq *rte, *nrte;
   1189 	u_short nstl;
   1190 	int s;
   1191 	int mfcctl_size = sizeof(struct mfcctl);
   1192 
   1193 	if (mrt_api_config & MRT_API_FLAGS_ALL)
   1194 		mfcctl_size = sizeof(struct mfcctl2);
   1195 
   1196 	if (m == NULL || m->m_len < mfcctl_size)
   1197 		return (EINVAL);
   1198 
   1199 	/*
   1200 	 * select data size depending on API version.
   1201 	 */
   1202 	if (mrt_api_config & MRT_API_FLAGS_ALL) {
   1203 		struct mfcctl2 *mp2 = mtod(m, struct mfcctl2 *);
   1204 		bcopy(mp2, (caddr_t)&mfcctl2, sizeof(*mp2));
   1205 	} else {
   1206 		struct mfcctl *mp = mtod(m, struct mfcctl *);
   1207 		bcopy(mp, (caddr_t)&mfcctl2, sizeof(*mp));
   1208 		bzero((caddr_t)&mfcctl2 + sizeof(struct mfcctl),
   1209 		      sizeof(mfcctl2) - sizeof(struct mfcctl));
   1210 	}
   1211 	mfccp = &mfcctl2;
   1212 
   1213 	s = splsoftnet();
   1214 	rt = mfc_find(&mfccp->mfcc_origin, &mfccp->mfcc_mcastgrp);
   1215 
   1216 	/* If an entry already exists, just update the fields */
   1217 	if (rt) {
   1218 		if (mrtdebug & DEBUG_MFC)
   1219 			log(LOG_DEBUG, "add_mfc update o %x g %x p %x\n",
   1220 			    ntohl(mfccp->mfcc_origin.s_addr),
   1221 			    ntohl(mfccp->mfcc_mcastgrp.s_addr),
   1222 			    mfccp->mfcc_parent);
   1223 
   1224 		update_mfc_params(rt, mfccp);
   1225 
   1226 		splx(s);
   1227 		return (0);
   1228 	}
   1229 
   1230 	/*
   1231 	 * Find the entry for which the upcall was made and update
   1232 	 */
   1233 	nstl = 0;
   1234 	hash = MFCHASH(mfccp->mfcc_origin, mfccp->mfcc_mcastgrp);
   1235 	LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) {
   1236 		if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) &&
   1237 		    in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp) &&
   1238 		    rt->mfc_stall != NULL) {
   1239 			if (nstl++)
   1240 				log(LOG_ERR, "add_mfc %s o %x g %x p %x dbx %p\n",
   1241 				    "multiple kernel entries",
   1242 				    ntohl(mfccp->mfcc_origin.s_addr),
   1243 				    ntohl(mfccp->mfcc_mcastgrp.s_addr),
   1244 				    mfccp->mfcc_parent, rt->mfc_stall);
   1245 
   1246 			if (mrtdebug & DEBUG_MFC)
   1247 				log(LOG_DEBUG, "add_mfc o %x g %x p %x dbg %p\n",
   1248 				    ntohl(mfccp->mfcc_origin.s_addr),
   1249 				    ntohl(mfccp->mfcc_mcastgrp.s_addr),
   1250 				    mfccp->mfcc_parent, rt->mfc_stall);
   1251 
   1252 			rte = rt->mfc_stall;
   1253 			init_mfc_params(rt, mfccp);
   1254 			rt->mfc_stall = NULL;
   1255 
   1256 			rt->mfc_expire = 0; /* Don't clean this guy up */
   1257 			nexpire[hash]--;
   1258 
   1259 			/* free packets Qed at the end of this entry */
   1260 			for (; rte != NULL; rte = nrte) {
   1261 				nrte = rte->next;
   1262 				if (rte->ifp) {
   1263 #ifdef RSVP_ISI
   1264 					ip_mdq(rte->m, rte->ifp, rt, -1);
   1265 #else
   1266 					ip_mdq(rte->m, rte->ifp, rt);
   1267 #endif /* RSVP_ISI */
   1268 				}
   1269 				m_freem(rte->m);
   1270 #ifdef UPCALL_TIMING
   1271 				collate(&rte->t);
   1272 #endif /* UPCALL_TIMING */
   1273 				free(rte, M_MRTABLE);
   1274 			}
   1275 		}
   1276 	}
   1277 
   1278 	/*
   1279 	 * It is possible that an entry is being inserted without an upcall
   1280 	 */
   1281 	if (nstl == 0) {
   1282 		/*
   1283 		 * No mfc; make a new one
   1284 		 */
   1285 		if (mrtdebug & DEBUG_MFC)
   1286 			log(LOG_DEBUG, "add_mfc no upcall o %x g %x p %x\n",
   1287 			    ntohl(mfccp->mfcc_origin.s_addr),
   1288 			    ntohl(mfccp->mfcc_mcastgrp.s_addr),
   1289 			    mfccp->mfcc_parent);
   1290 
   1291 		LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) {
   1292 			if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) &&
   1293 			    in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp)) {
   1294 				init_mfc_params(rt, mfccp);
   1295 				if (rt->mfc_expire)
   1296 					nexpire[hash]--;
   1297 				rt->mfc_expire = 0;
   1298 				break; /* XXX */
   1299 			}
   1300 		}
   1301 		if (rt == NULL) {	/* no upcall, so make a new entry */
   1302 			rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE,
   1303 						  M_NOWAIT);
   1304 			if (rt == NULL) {
   1305 				splx(s);
   1306 				return (ENOBUFS);
   1307 			}
   1308 
   1309 			init_mfc_params(rt, mfccp);
   1310 			rt->mfc_expire	= 0;
   1311 			rt->mfc_stall	= NULL;
   1312 			rt->mfc_bw_meter = NULL;
   1313 
   1314 			/* insert new entry at head of hash chain */
   1315 			LIST_INSERT_HEAD(&mfchashtbl[hash], rt, mfc_hash);
   1316 		}
   1317 	}
   1318 
   1319 	splx(s);
   1320 	return (0);
   1321 }
   1322 
   1323 #ifdef UPCALL_TIMING
   1324 /*
   1325  * collect delay statistics on the upcalls
   1326  */
   1327 static void
   1328 collate(t)
   1329 	struct timeval *t;
   1330 {
   1331 	u_int32_t d;
   1332 	struct timeval tp;
   1333 	u_int32_t delta;
   1334 
   1335 	microtime(&tp);
   1336 
   1337 	if (timercmp(t, &tp, <)) {
   1338 		TV_DELTA(tp, *t, delta);
   1339 
   1340 		d = delta >> 10;
   1341 		if (d > 50)
   1342 			d = 50;
   1343 
   1344 		++upcall_data[d];
   1345 	}
   1346 }
   1347 #endif /* UPCALL_TIMING */
   1348 
   1349 /*
   1350  * Delete an mfc entry
   1351  */
   1352 static int
   1353 del_mfc(m)
   1354 	struct mbuf *m;
   1355 {
   1356 	struct mfcctl2 mfcctl2;
   1357 	struct mfcctl2 *mfccp;
   1358 	struct mfc *rt;
   1359 	int s;
   1360 	int mfcctl_size = sizeof(struct mfcctl);
   1361 	struct mfcctl *mp = mtod(m, struct mfcctl *);
   1362 
   1363 	/*
   1364 	 * XXX: for deleting MFC entries the information in entries
   1365 	 * of size "struct mfcctl" is sufficient.
   1366 	 */
   1367 
   1368 	if (m == NULL || m->m_len < mfcctl_size)
   1369 		return (EINVAL);
   1370 
   1371 	bcopy(mp, (caddr_t)&mfcctl2, sizeof(*mp));
   1372 	bzero((caddr_t)&mfcctl2 + sizeof(struct mfcctl),
   1373 	      sizeof(mfcctl2) - sizeof(struct mfcctl));
   1374 
   1375 	mfccp = &mfcctl2;
   1376 
   1377 	if (mrtdebug & DEBUG_MFC)
   1378 		log(LOG_DEBUG, "del_mfc origin %x mcastgrp %x\n",
   1379 		    ntohl(mfccp->mfcc_origin.s_addr),
   1380 		    ntohl(mfccp->mfcc_mcastgrp.s_addr));
   1381 
   1382 	s = splsoftnet();
   1383 
   1384 	rt = mfc_find(&mfccp->mfcc_origin, &mfccp->mfcc_mcastgrp);
   1385 	if (rt == NULL) {
   1386 		splx(s);
   1387 		return (EADDRNOTAVAIL);
   1388 	}
   1389 
   1390 	/*
   1391 	 * free the bw_meter entries
   1392 	 */
   1393 	free_bw_list(rt->mfc_bw_meter);
   1394 	rt->mfc_bw_meter = NULL;
   1395 
   1396 	LIST_REMOVE(rt, mfc_hash);
   1397 	free(rt, M_MRTABLE);
   1398 
   1399 	splx(s);
   1400 	return (0);
   1401 }
   1402 
   1403 static int
   1404 socket_send(s, mm, src)
   1405 	struct socket *s;
   1406 	struct mbuf *mm;
   1407 	struct sockaddr_in *src;
   1408 {
   1409 	if (s) {
   1410 		if (sbappendaddr(&s->so_rcv, sintosa(src), mm,
   1411 		    (struct mbuf *)NULL) != 0) {
   1412 			sorwakeup(s);
   1413 			return (0);
   1414 		}
   1415 	}
   1416 	m_freem(mm);
   1417 	return (-1);
   1418 }
   1419 
   1420 /*
   1421  * IP multicast forwarding function. This function assumes that the packet
   1422  * pointed to by "ip" has arrived on (or is about to be sent to) the interface
   1423  * pointed to by "ifp", and the packet is to be relayed to other networks
   1424  * that have members of the packet's destination IP multicast group.
   1425  *
   1426  * The packet is returned unscathed to the caller, unless it is
   1427  * erroneous, in which case a non-zero return value tells the caller to
   1428  * discard it.
   1429  */
   1430 
   1431 #define IP_HDR_LEN  20	/* # bytes of fixed IP header (excluding options) */
   1432 #define TUNNEL_LEN  12  /* # bytes of IP option for tunnel encapsulation  */
   1433 
   1434 int
   1435 #ifdef RSVP_ISI
   1436 ip_mforward(m, ifp, imo)
   1437 #else
   1438 ip_mforward(m, ifp)
   1439 #endif /* RSVP_ISI */
   1440 	struct mbuf *m;
   1441 	struct ifnet *ifp;
   1442 #ifdef RSVP_ISI
   1443 	struct ip_moptions *imo;
   1444 #endif /* RSVP_ISI */
   1445 {
   1446 	struct ip *ip = mtod(m, struct ip *);
   1447 	struct mfc *rt;
   1448 	static int srctun = 0;
   1449 	struct mbuf *mm;
   1450 	int s;
   1451 	vifi_t vifi;
   1452 
   1453 	if (mrtdebug & DEBUG_FORWARD)
   1454 		log(LOG_DEBUG, "ip_mforward: src %x, dst %x, ifp %p\n",
   1455 		    ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr), ifp);
   1456 
   1457 	if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 ||
   1458 	    ((u_char *)(ip + 1))[1] != IPOPT_LSRR) {
   1459 		/*
   1460 		 * Packet arrived via a physical interface or
   1461 		 * an encapsulated tunnel or a register_vif.
   1462 		 */
   1463 	} else {
   1464 		/*
   1465 		 * Packet arrived through a source-route tunnel.
   1466 		 * Source-route tunnels are no longer supported.
   1467 		 */
   1468 		if ((srctun++ % 1000) == 0)
   1469 			log(LOG_ERR,
   1470 			    "ip_mforward: received source-routed packet from %x\n",
   1471 			    ntohl(ip->ip_src.s_addr));
   1472 
   1473 		return (1);
   1474 	}
   1475 
   1476 #ifdef RSVP_ISI
   1477 	if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) {
   1478 		if (ip->ip_ttl < 255)
   1479 			ip->ip_ttl++;	/* compensate for -1 in *_send routines */
   1480 		if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
   1481 			struct vif *vifp = viftable + vifi;
   1482 			printf("Sending IPPROTO_RSVP from %x to %x on vif %d (%s%s)\n",
   1483 			    ntohl(ip->ip_src), ntohl(ip->ip_dst), vifi,
   1484 			    (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "",
   1485 			    vifp->v_ifp->if_xname);
   1486 		}
   1487 		return (ip_mdq(m, ifp, (struct mfc *)NULL, vifi));
   1488 	}
   1489 	if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) {
   1490 		printf("Warning: IPPROTO_RSVP from %x to %x without vif option\n",
   1491 		    ntohl(ip->ip_src), ntohl(ip->ip_dst));
   1492 	}
   1493 #endif /* RSVP_ISI */
   1494 
   1495 	/*
   1496 	 * Don't forward a packet with time-to-live of zero or one,
   1497 	 * or a packet destined to a local-only group.
   1498 	 */
   1499 	if (ip->ip_ttl <= 1 || IN_LOCAL_GROUP(ip->ip_dst.s_addr))
   1500 		return (0);
   1501 
   1502 	/*
   1503 	 * Determine forwarding vifs from the forwarding cache table
   1504 	 */
   1505 	s = splsoftnet();
   1506 	++mrtstat.mrts_mfc_lookups;
   1507 	rt = mfc_find(&ip->ip_src, &ip->ip_dst);
   1508 
   1509 	/* Entry exists, so forward if necessary */
   1510 	if (rt != NULL) {
   1511 		splx(s);
   1512 #ifdef RSVP_ISI
   1513 		return (ip_mdq(m, ifp, rt, -1));
   1514 #else
   1515 		return (ip_mdq(m, ifp, rt));
   1516 #endif /* RSVP_ISI */
   1517 	} else {
   1518 		/*
   1519 		 * If we don't have a route for packet's origin,
   1520 		 * Make a copy of the packet & send message to routing daemon
   1521 		 */
   1522 
   1523 		struct mbuf *mb0;
   1524 		struct rtdetq *rte;
   1525 		u_int32_t hash;
   1526 		int hlen = ip->ip_hl << 2;
   1527 #ifdef UPCALL_TIMING
   1528 		struct timeval tp;
   1529 
   1530 		microtime(&tp);
   1531 #endif /* UPCALL_TIMING */
   1532 
   1533 		++mrtstat.mrts_mfc_misses;
   1534 
   1535 		mrtstat.mrts_no_route++;
   1536 		if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC))
   1537 			log(LOG_DEBUG, "ip_mforward: no rte s %x g %x\n",
   1538 			    ntohl(ip->ip_src.s_addr),
   1539 			    ntohl(ip->ip_dst.s_addr));
   1540 
   1541 		/*
   1542 		 * Allocate mbufs early so that we don't do extra work if we are
   1543 		 * just going to fail anyway.  Make sure to pullup the header so
   1544 		 * that other people can't step on it.
   1545 		 */
   1546 		rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE,
   1547 					      M_NOWAIT);
   1548 		if (rte == NULL) {
   1549 			splx(s);
   1550 			return (ENOBUFS);
   1551 		}
   1552 		mb0 = m_copy(m, 0, M_COPYALL);
   1553 		M_PULLUP(mb0, hlen);
   1554 		if (mb0 == NULL) {
   1555 			free(rte, M_MRTABLE);
   1556 			splx(s);
   1557 			return (ENOBUFS);
   1558 		}
   1559 
   1560 		/* is there an upcall waiting for this flow? */
   1561 		hash = MFCHASH(ip->ip_src, ip->ip_dst);
   1562 		LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) {
   1563 			if (in_hosteq(ip->ip_src, rt->mfc_origin) &&
   1564 			    in_hosteq(ip->ip_dst, rt->mfc_mcastgrp) &&
   1565 			    rt->mfc_stall != NULL)
   1566 				break;
   1567 		}
   1568 
   1569 		if (rt == NULL) {
   1570 			int i;
   1571 			struct igmpmsg *im;
   1572 
   1573 			/*
   1574 			 * Locate the vifi for the incoming interface for
   1575 			 * this packet.
   1576 			 * If none found, drop packet.
   1577 			 */
   1578 			for (vifi = 0; vifi < numvifs &&
   1579 				 viftable[vifi].v_ifp != ifp; vifi++)
   1580 				;
   1581 			if (vifi >= numvifs) /* vif not found, drop packet */
   1582 				goto non_fatal;
   1583 
   1584 			/* no upcall, so make a new entry */
   1585 			rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE,
   1586 						  M_NOWAIT);
   1587 			if (rt == NULL)
   1588 				goto fail;
   1589 
   1590 			/*
   1591 			 * Make a copy of the header to send to the user level
   1592 			 * process
   1593 			 */
   1594 			mm = m_copy(m, 0, hlen);
   1595 			M_PULLUP(mm, hlen);
   1596 			if (mm == NULL)
   1597 				goto fail1;
   1598 
   1599 			/*
   1600 			 * Send message to routing daemon to install
   1601 			 * a route into the kernel table
   1602 			 */
   1603 
   1604 			im = mtod(mm, struct igmpmsg *);
   1605 			im->im_msgtype = IGMPMSG_NOCACHE;
   1606 			im->im_mbz = 0;
   1607 			im->im_vif = vifi;
   1608 
   1609 			mrtstat.mrts_upcalls++;
   1610 
   1611 			sin.sin_addr = ip->ip_src;
   1612 			if (socket_send(ip_mrouter, mm, &sin) < 0) {
   1613 				log(LOG_WARNING,
   1614 				    "ip_mforward: ip_mrouter socket queue full\n");
   1615 				++mrtstat.mrts_upq_sockfull;
   1616 			fail1:
   1617 				free(rt, M_MRTABLE);
   1618 			fail:
   1619 				free(rte, M_MRTABLE);
   1620 				m_freem(mb0);
   1621 				splx(s);
   1622 				return (ENOBUFS);
   1623 			}
   1624 
   1625 			/* insert new entry at head of hash chain */
   1626 			rt->mfc_origin = ip->ip_src;
   1627 			rt->mfc_mcastgrp = ip->ip_dst;
   1628 			rt->mfc_pkt_cnt = 0;
   1629 			rt->mfc_byte_cnt = 0;
   1630 			rt->mfc_wrong_if = 0;
   1631 			rt->mfc_expire = UPCALL_EXPIRE;
   1632 			nexpire[hash]++;
   1633 			for (i = 0; i < numvifs; i++) {
   1634 				rt->mfc_ttls[i] = 0;
   1635 				rt->mfc_flags[i] = 0;
   1636 			}
   1637 			rt->mfc_parent = -1;
   1638 
   1639 			/* clear the RP address */
   1640 			rt->mfc_rp = zeroin_addr;
   1641 
   1642 			rt->mfc_bw_meter = NULL;
   1643 
   1644 			/* link into table */
   1645 			LIST_INSERT_HEAD(&mfchashtbl[hash], rt, mfc_hash);
   1646 			/* Add this entry to the end of the queue */
   1647 			rt->mfc_stall = rte;
   1648 		} else {
   1649 			/* determine if q has overflowed */
   1650 			struct rtdetq **p;
   1651 			int npkts = 0;
   1652 
   1653 			/*
   1654 			 * XXX ouch! we need to append to the list, but we
   1655 			 * only have a pointer to the front, so we have to
   1656 			 * scan the entire list every time.
   1657 			 */
   1658 			for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next)
   1659 				if (++npkts > MAX_UPQ) {
   1660 					mrtstat.mrts_upq_ovflw++;
   1661 				non_fatal:
   1662 					free(rte, M_MRTABLE);
   1663 					m_freem(mb0);
   1664 					splx(s);
   1665 					return (0);
   1666 				}
   1667 
   1668 			/* Add this entry to the end of the queue */
   1669 			*p = rte;
   1670 		}
   1671 
   1672 		rte->next = NULL;
   1673 		rte->m = mb0;
   1674 		rte->ifp = ifp;
   1675 #ifdef UPCALL_TIMING
   1676 		rte->t = tp;
   1677 #endif /* UPCALL_TIMING */
   1678 
   1679 		splx(s);
   1680 
   1681 		return (0);
   1682 	}
   1683 }
   1684 
   1685 
   1686 /*ARGSUSED*/
   1687 static void
   1688 expire_upcalls(v)
   1689 	void *v;
   1690 {
   1691 	int i;
   1692 	int s;
   1693 
   1694 	s = splsoftnet();
   1695 
   1696 	for (i = 0; i < MFCTBLSIZ; i++) {
   1697 		struct mfc *rt, *nrt;
   1698 
   1699 		if (nexpire[i] == 0)
   1700 			continue;
   1701 
   1702 		for (rt = LIST_FIRST(&mfchashtbl[i]); rt; rt = nrt) {
   1703 			nrt = LIST_NEXT(rt, mfc_hash);
   1704 
   1705 			if (rt->mfc_expire == 0 || --rt->mfc_expire > 0)
   1706 				continue;
   1707 			nexpire[i]--;
   1708 
   1709 			/*
   1710 			 * free the bw_meter entries
   1711 			 */
   1712 			while (rt->mfc_bw_meter != NULL) {
   1713 				struct bw_meter *x = rt->mfc_bw_meter;
   1714 
   1715 				rt->mfc_bw_meter = x->bm_mfc_next;
   1716 				free(x, M_BWMETER);
   1717 			}
   1718 
   1719 			++mrtstat.mrts_cache_cleanups;
   1720 			if (mrtdebug & DEBUG_EXPIRE)
   1721 				log(LOG_DEBUG,
   1722 				    "expire_upcalls: expiring (%x %x)\n",
   1723 				    ntohl(rt->mfc_origin.s_addr),
   1724 				    ntohl(rt->mfc_mcastgrp.s_addr));
   1725 
   1726 			expire_mfc(rt);
   1727 		}
   1728 	}
   1729 
   1730 	splx(s);
   1731 	callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
   1732 	    expire_upcalls, NULL);
   1733 }
   1734 
   1735 /*
   1736  * Packet forwarding routine once entry in the cache is made
   1737  */
   1738 static int
   1739 #ifdef RSVP_ISI
   1740 ip_mdq(m, ifp, rt, xmt_vif)
   1741 #else
   1742 ip_mdq(m, ifp, rt)
   1743 #endif /* RSVP_ISI */
   1744 	struct mbuf *m;
   1745 	struct ifnet *ifp;
   1746 	struct mfc *rt;
   1747 #ifdef RSVP_ISI
   1748 	vifi_t xmt_vif;
   1749 #endif /* RSVP_ISI */
   1750 {
   1751 	struct ip  *ip = mtod(m, struct ip *);
   1752 	vifi_t vifi;
   1753 	struct vif *vifp;
   1754 	int plen = ntohs(ip->ip_len) - (ip->ip_hl << 2);
   1755 
   1756 /*
   1757  * Macro to send packet on vif.  Since RSVP packets don't get counted on
   1758  * input, they shouldn't get counted on output, so statistics keeping is
   1759  * separate.
   1760  */
   1761 #define MC_SEND(ip, vifp, m) do {					\
   1762 	if ((vifp)->v_flags & VIFF_TUNNEL)				\
   1763 		encap_send((ip), (vifp), (m));				\
   1764 	else								\
   1765 		phyint_send((ip), (vifp), (m));				\
   1766 } while (/*CONSTCOND*/ 0)
   1767 
   1768 #ifdef RSVP_ISI
   1769 	/*
   1770 	 * If xmt_vif is not -1, send on only the requested vif.
   1771 	 *
   1772 	 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.
   1773 	 */
   1774 	if (xmt_vif < numvifs) {
   1775 #ifdef PIM
   1776 		if (viftable[xmt_vif].v_flags & VIFF_REGISTER)
   1777 			pim_register_send(ip, viftable + xmt_vif, m, rt);
   1778 		else
   1779 #endif
   1780 		MC_SEND(ip, viftable + xmt_vif, m);
   1781 		return (1);
   1782 	}
   1783 #endif /* RSVP_ISI */
   1784 
   1785 	/*
   1786 	 * Don't forward if it didn't arrive from the parent vif for its origin.
   1787 	 */
   1788 	vifi = rt->mfc_parent;
   1789 	if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) {
   1790 		/* came in the wrong interface */
   1791 		if (mrtdebug & DEBUG_FORWARD)
   1792 			log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n",
   1793 			    ifp, vifi,
   1794 			    vifi >= numvifs ? 0 : viftable[vifi].v_ifp);
   1795 		++mrtstat.mrts_wrong_if;
   1796 		++rt->mfc_wrong_if;
   1797 		/*
   1798 		 * If we are doing PIM assert processing, send a message
   1799 		 * to the routing daemon.
   1800 		 *
   1801 		 * XXX: A PIM-SM router needs the WRONGVIF detection so it
   1802 		 * can complete the SPT switch, regardless of the type
   1803 		 * of the iif (broadcast media, GRE tunnel, etc).
   1804 		 */
   1805 		if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) {
   1806 			struct timeval now;
   1807 			u_int32_t delta;
   1808 
   1809 #ifdef PIM
   1810 			if (ifp == &multicast_register_if)
   1811 				pimstat.pims_rcv_registers_wrongiif++;
   1812 #endif
   1813 
   1814 			/* Get vifi for the incoming packet */
   1815 			for (vifi = 0;
   1816 			     vifi < numvifs && viftable[vifi].v_ifp != ifp;
   1817 			     vifi++)
   1818 			    ;
   1819 			if (vifi >= numvifs) {
   1820 				/* The iif is not found: ignore the packet. */
   1821 				return (0);
   1822 			}
   1823 
   1824 			if (rt->mfc_flags[vifi] &
   1825 			    MRT_MFC_FLAGS_DISABLE_WRONGVIF) {
   1826 				/* WRONGVIF disabled: ignore the packet */
   1827 				return (0);
   1828 			}
   1829 
   1830 			microtime(&now);
   1831 
   1832 			TV_DELTA(rt->mfc_last_assert, now, delta);
   1833 
   1834 			if (delta > ASSERT_MSG_TIME) {
   1835 				struct igmpmsg *im;
   1836 				int hlen = ip->ip_hl << 2;
   1837 				struct mbuf *mm = m_copy(m, 0, hlen);
   1838 
   1839 				M_PULLUP(mm, hlen);
   1840 				if (mm == NULL)
   1841 					return (ENOBUFS);
   1842 
   1843 				rt->mfc_last_assert = now;
   1844 
   1845 				im = mtod(mm, struct igmpmsg *);
   1846 				im->im_msgtype	= IGMPMSG_WRONGVIF;
   1847 				im->im_mbz	= 0;
   1848 				im->im_vif	= vifi;
   1849 
   1850 				mrtstat.mrts_upcalls++;
   1851 
   1852 				sin.sin_addr = im->im_src;
   1853 				if (socket_send(ip_mrouter, mm, &sin) < 0) {
   1854 					log(LOG_WARNING,
   1855 					    "ip_mforward: ip_mrouter socket queue full\n");
   1856 					++mrtstat.mrts_upq_sockfull;
   1857 					return (ENOBUFS);
   1858 				}
   1859 			}
   1860 		}
   1861 		return (0);
   1862 	}
   1863 
   1864 	/* If I sourced this packet, it counts as output, else it was input. */
   1865 	if (in_hosteq(ip->ip_src, viftable[vifi].v_lcl_addr)) {
   1866 		viftable[vifi].v_pkt_out++;
   1867 		viftable[vifi].v_bytes_out += plen;
   1868 	} else {
   1869 		viftable[vifi].v_pkt_in++;
   1870 		viftable[vifi].v_bytes_in += plen;
   1871 	}
   1872 	rt->mfc_pkt_cnt++;
   1873 	rt->mfc_byte_cnt += plen;
   1874 
   1875 	/*
   1876 	 * For each vif, decide if a copy of the packet should be forwarded.
   1877 	 * Forward if:
   1878 	 *		- the ttl exceeds the vif's threshold
   1879 	 *		- there are group members downstream on interface
   1880 	 */
   1881 	for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++)
   1882 		if ((rt->mfc_ttls[vifi] > 0) &&
   1883 			(ip->ip_ttl > rt->mfc_ttls[vifi])) {
   1884 			vifp->v_pkt_out++;
   1885 			vifp->v_bytes_out += plen;
   1886 #ifdef PIM
   1887 			if (vifp->v_flags & VIFF_REGISTER)
   1888 				pim_register_send(ip, vifp, m, rt);
   1889 			else
   1890 #endif
   1891 			MC_SEND(ip, vifp, m);
   1892 		}
   1893 
   1894 	/*
   1895 	 * Perform upcall-related bw measuring.
   1896 	 */
   1897 	if (rt->mfc_bw_meter != NULL) {
   1898 		struct bw_meter *x;
   1899 		struct timeval now;
   1900 
   1901 		microtime(&now);
   1902 		for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next)
   1903 			bw_meter_receive_packet(x, plen, &now);
   1904 	}
   1905 
   1906 	return (0);
   1907 }
   1908 
   1909 #ifdef RSVP_ISI
   1910 /*
   1911  * check if a vif number is legal/ok. This is used by ip_output.
   1912  */
   1913 int
   1914 legal_vif_num(vif)
   1915 	int vif;
   1916 {
   1917 	if (vif >= 0 && vif < numvifs)
   1918 		return (1);
   1919 	else
   1920 		return (0);
   1921 }
   1922 #endif /* RSVP_ISI */
   1923 
   1924 static void
   1925 phyint_send(ip, vifp, m)
   1926 	struct ip *ip;
   1927 	struct vif *vifp;
   1928 	struct mbuf *m;
   1929 {
   1930 	struct mbuf *mb_copy;
   1931 	int hlen = ip->ip_hl << 2;
   1932 
   1933 	/*
   1934 	 * Make a new reference to the packet; make sure that
   1935 	 * the IP header is actually copied, not just referenced,
   1936 	 * so that ip_output() only scribbles on the copy.
   1937 	 */
   1938 	mb_copy = m_copy(m, 0, M_COPYALL);
   1939 	M_PULLUP(mb_copy, hlen);
   1940 	if (mb_copy == NULL)
   1941 		return;
   1942 
   1943 	if (vifp->v_rate_limit <= 0)
   1944 		tbf_send_packet(vifp, mb_copy);
   1945 	else
   1946 		tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *),
   1947 		    ntohs(ip->ip_len));
   1948 }
   1949 
   1950 static void
   1951 encap_send(ip, vifp, m)
   1952 	struct ip *ip;
   1953 	struct vif *vifp;
   1954 	struct mbuf *m;
   1955 {
   1956 	struct mbuf *mb_copy;
   1957 	struct ip *ip_copy;
   1958 	int i, len = ntohs(ip->ip_len) + sizeof(multicast_encap_iphdr);
   1959 
   1960 	/* Take care of delayed checksums */
   1961 	if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
   1962 		in_delayed_cksum(m);
   1963 		m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
   1964 	}
   1965 
   1966 	/*
   1967 	 * copy the old packet & pullup it's IP header into the
   1968 	 * new mbuf so we can modify it.  Try to fill the new
   1969 	 * mbuf since if we don't the ethernet driver will.
   1970 	 */
   1971 	MGETHDR(mb_copy, M_DONTWAIT, MT_DATA);
   1972 	if (mb_copy == NULL)
   1973 		return;
   1974 	mb_copy->m_data += max_linkhdr;
   1975 	mb_copy->m_pkthdr.len = len;
   1976 	mb_copy->m_len = sizeof(multicast_encap_iphdr);
   1977 
   1978 	if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
   1979 		m_freem(mb_copy);
   1980 		return;
   1981 	}
   1982 	i = MHLEN - max_linkhdr;
   1983 	if (i > len)
   1984 		i = len;
   1985 	mb_copy = m_pullup(mb_copy, i);
   1986 	if (mb_copy == NULL)
   1987 		return;
   1988 
   1989 	/*
   1990 	 * fill in the encapsulating IP header.
   1991 	 */
   1992 	ip_copy = mtod(mb_copy, struct ip *);
   1993 	*ip_copy = multicast_encap_iphdr;
   1994 	ip_copy->ip_id = ip_newid();
   1995 	ip_copy->ip_len = htons(len);
   1996 	ip_copy->ip_src = vifp->v_lcl_addr;
   1997 	ip_copy->ip_dst = vifp->v_rmt_addr;
   1998 
   1999 	/*
   2000 	 * turn the encapsulated IP header back into a valid one.
   2001 	 */
   2002 	ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
   2003 	--ip->ip_ttl;
   2004 	ip->ip_sum = 0;
   2005 	mb_copy->m_data += sizeof(multicast_encap_iphdr);
   2006 	ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
   2007 	mb_copy->m_data -= sizeof(multicast_encap_iphdr);
   2008 
   2009 	if (vifp->v_rate_limit <= 0)
   2010 		tbf_send_packet(vifp, mb_copy);
   2011 	else
   2012 		tbf_control(vifp, mb_copy, ip, ntohs(ip_copy->ip_len));
   2013 }
   2014 
   2015 /*
   2016  * De-encapsulate a packet and feed it back through ip input.
   2017  */
   2018 static void
   2019 vif_input(struct mbuf *m, ...)
   2020 {
   2021 	int off, proto;
   2022 	va_list ap;
   2023 	struct vif *vifp;
   2024 	int s;
   2025 	struct ifqueue *ifq;
   2026 
   2027 	va_start(ap, m);
   2028 	off = va_arg(ap, int);
   2029 	proto = va_arg(ap, int);
   2030 	va_end(ap);
   2031 
   2032 	vifp = (struct vif *)encap_getarg(m);
   2033 	if (!vifp || proto != AF_INET) {
   2034 		m_freem(m);
   2035 		mrtstat.mrts_bad_tunnel++;
   2036 		return;
   2037 	}
   2038 
   2039 	m_adj(m, off);
   2040 	m->m_pkthdr.rcvif = vifp->v_ifp;
   2041 	ifq = &ipintrq;
   2042 	s = splnet();
   2043 	if (IF_QFULL(ifq)) {
   2044 		IF_DROP(ifq);
   2045 		m_freem(m);
   2046 	} else {
   2047 		IF_ENQUEUE(ifq, m);
   2048 		/*
   2049 		 * normally we would need a "schednetisr(NETISR_IP)"
   2050 		 * here but we were called by ip_input and it is going
   2051 		 * to loop back & try to dequeue the packet we just
   2052 		 * queued as soon as we return so we avoid the
   2053 		 * unnecessary software interrrupt.
   2054 		 */
   2055 	}
   2056 	splx(s);
   2057 }
   2058 
   2059 /*
   2060  * Check if the packet should be grabbed by us.
   2061  */
   2062 static int
   2063 vif_encapcheck(m, off, proto, arg)
   2064 	const struct mbuf *m;
   2065 	int off;
   2066 	int proto;
   2067 	void *arg;
   2068 {
   2069 	struct vif *vifp;
   2070 	struct ip ip;
   2071 
   2072 #ifdef DIAGNOSTIC
   2073 	if (!arg || proto != IPPROTO_IPV4)
   2074 		panic("unexpected arg in vif_encapcheck");
   2075 #endif
   2076 
   2077 	/*
   2078 	 * do not grab the packet if it's not to a multicast destination or if
   2079 	 * we don't have an encapsulating tunnel with the source.
   2080 	 * Note:  This code assumes that the remote site IP address
   2081 	 * uniquely identifies the tunnel (i.e., that this site has
   2082 	 * at most one tunnel with the remote site).
   2083 	 */
   2084 
   2085 	/* LINTED const cast */
   2086 	m_copydata((struct mbuf *)m, off, sizeof(ip), (caddr_t)&ip);
   2087 	if (!IN_MULTICAST(ip.ip_dst.s_addr))
   2088 		return 0;
   2089 
   2090 	/* LINTED const cast */
   2091 	m_copydata((struct mbuf *)m, 0, sizeof(ip), (caddr_t)&ip);
   2092 	if (!in_hosteq(ip.ip_src, last_encap_src)) {
   2093 		vifp = (struct vif *)arg;
   2094 		if (vifp->v_flags & VIFF_TUNNEL &&
   2095 		    in_hosteq(vifp->v_rmt_addr, ip.ip_src))
   2096 			;
   2097 		else
   2098 			return 0;
   2099 		last_encap_vif = vifp;
   2100 		last_encap_src = ip.ip_src;
   2101 	} else
   2102 		vifp = last_encap_vif;
   2103 
   2104 	/* 32bit match, since we have checked ip_src only */
   2105 	return 32;
   2106 }
   2107 
   2108 /*
   2109  * Token bucket filter module
   2110  */
   2111 static void
   2112 tbf_control(vifp, m, ip, len)
   2113 	struct vif *vifp;
   2114 	struct mbuf *m;
   2115 	struct ip *ip;
   2116 	u_int32_t len;
   2117 {
   2118 
   2119 	if (len > MAX_BKT_SIZE) {
   2120 		/* drop if packet is too large */
   2121 		mrtstat.mrts_pkt2large++;
   2122 		m_freem(m);
   2123 		return;
   2124 	}
   2125 
   2126 	tbf_update_tokens(vifp);
   2127 
   2128 	/*
   2129 	 * If there are enough tokens, and the queue is empty, send this packet
   2130 	 * out immediately.  Otherwise, try to insert it on this vif's queue.
   2131 	 */
   2132 	if (vifp->tbf_q_len == 0) {
   2133 		if (len <= vifp->tbf_n_tok) {
   2134 			vifp->tbf_n_tok -= len;
   2135 			tbf_send_packet(vifp, m);
   2136 		} else {
   2137 			/* queue packet and timeout till later */
   2138 			tbf_queue(vifp, m);
   2139 			callout_reset(&vifp->v_repq_ch, TBF_REPROCESS,
   2140 			    tbf_reprocess_q, vifp);
   2141 		}
   2142 	} else {
   2143 		if (vifp->tbf_q_len >= vifp->tbf_max_q_len &&
   2144 		    !tbf_dq_sel(vifp, ip)) {
   2145 			/* queue full, and couldn't make room */
   2146 			mrtstat.mrts_q_overflow++;
   2147 			m_freem(m);
   2148 		} else {
   2149 			/* queue length low enough, or made room */
   2150 			tbf_queue(vifp, m);
   2151 			tbf_process_q(vifp);
   2152 		}
   2153 	}
   2154 }
   2155 
   2156 /*
   2157  * adds a packet to the queue at the interface
   2158  */
   2159 static void
   2160 tbf_queue(vifp, m)
   2161 	struct vif *vifp;
   2162 	struct mbuf *m;
   2163 {
   2164 	int s = splsoftnet();
   2165 
   2166 	/* insert at tail */
   2167 	*vifp->tbf_t = m;
   2168 	vifp->tbf_t = &m->m_nextpkt;
   2169 	vifp->tbf_q_len++;
   2170 
   2171 	splx(s);
   2172 }
   2173 
   2174 
   2175 /*
   2176  * processes the queue at the interface
   2177  */
   2178 static void
   2179 tbf_process_q(vifp)
   2180 	struct vif *vifp;
   2181 {
   2182 	struct mbuf *m;
   2183 	int len;
   2184 	int s = splsoftnet();
   2185 
   2186 	/*
   2187 	 * Loop through the queue at the interface and send as many packets
   2188 	 * as possible.
   2189 	 */
   2190 	for (m = vifp->tbf_q; m != NULL; m = vifp->tbf_q) {
   2191 		len = ntohs(mtod(m, struct ip *)->ip_len);
   2192 
   2193 		/* determine if the packet can be sent */
   2194 		if (len <= vifp->tbf_n_tok) {
   2195 			/* if so,
   2196 			 * reduce no of tokens, dequeue the packet,
   2197 			 * send the packet.
   2198 			 */
   2199 			if ((vifp->tbf_q = m->m_nextpkt) == NULL)
   2200 				vifp->tbf_t = &vifp->tbf_q;
   2201 			--vifp->tbf_q_len;
   2202 
   2203 			m->m_nextpkt = NULL;
   2204 			vifp->tbf_n_tok -= len;
   2205 			tbf_send_packet(vifp, m);
   2206 		} else
   2207 			break;
   2208 	}
   2209 	splx(s);
   2210 }
   2211 
   2212 static void
   2213 tbf_reprocess_q(arg)
   2214 	void *arg;
   2215 {
   2216 	struct vif *vifp = arg;
   2217 
   2218 	if (ip_mrouter == NULL)
   2219 		return;
   2220 
   2221 	tbf_update_tokens(vifp);
   2222 	tbf_process_q(vifp);
   2223 
   2224 	if (vifp->tbf_q_len != 0)
   2225 		callout_reset(&vifp->v_repq_ch, TBF_REPROCESS,
   2226 		    tbf_reprocess_q, vifp);
   2227 }
   2228 
   2229 /* function that will selectively discard a member of the queue
   2230  * based on the precedence value and the priority
   2231  */
   2232 static int
   2233 tbf_dq_sel(vifp, ip)
   2234 	struct vif *vifp;
   2235 	struct ip *ip;
   2236 {
   2237 	u_int p;
   2238 	struct mbuf **mp, *m;
   2239 	int s = splsoftnet();
   2240 
   2241 	p = priority(vifp, ip);
   2242 
   2243 	for (mp = &vifp->tbf_q, m = *mp;
   2244 	    m != NULL;
   2245 	    mp = &m->m_nextpkt, m = *mp) {
   2246 		if (p > priority(vifp, mtod(m, struct ip *))) {
   2247 			if ((*mp = m->m_nextpkt) == NULL)
   2248 				vifp->tbf_t = mp;
   2249 			--vifp->tbf_q_len;
   2250 
   2251 			m_freem(m);
   2252 			mrtstat.mrts_drop_sel++;
   2253 			splx(s);
   2254 			return (1);
   2255 		}
   2256 	}
   2257 	splx(s);
   2258 	return (0);
   2259 }
   2260 
   2261 static void
   2262 tbf_send_packet(vifp, m)
   2263 	struct vif *vifp;
   2264 	struct mbuf *m;
   2265 {
   2266 	int error;
   2267 	int s = splsoftnet();
   2268 
   2269 	if (vifp->v_flags & VIFF_TUNNEL) {
   2270 		/* If tunnel options */
   2271 		ip_output(m, (struct mbuf *)NULL, &vifp->v_route,
   2272 		    IP_FORWARDING, (struct ip_moptions *)NULL,
   2273 		    (struct socket *)NULL);
   2274 	} else {
   2275 		/* if physical interface option, extract the options and then send */
   2276 		struct ip_moptions imo;
   2277 
   2278 		imo.imo_multicast_ifp = vifp->v_ifp;
   2279 		imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1;
   2280 		imo.imo_multicast_loop = 1;
   2281 #ifdef RSVP_ISI
   2282 		imo.imo_multicast_vif = -1;
   2283 #endif
   2284 
   2285 		error = ip_output(m, (struct mbuf *)NULL, (struct route *)NULL,
   2286 		    IP_FORWARDING|IP_MULTICASTOPTS, &imo,
   2287 		    (struct socket *)NULL);
   2288 
   2289 		if (mrtdebug & DEBUG_XMIT)
   2290 			log(LOG_DEBUG, "phyint_send on vif %ld err %d\n",
   2291 			    (long)(vifp - viftable), error);
   2292 	}
   2293 	splx(s);
   2294 }
   2295 
   2296 /* determine the current time and then
   2297  * the elapsed time (between the last time and time now)
   2298  * in milliseconds & update the no. of tokens in the bucket
   2299  */
   2300 static void
   2301 tbf_update_tokens(vifp)
   2302 	struct vif *vifp;
   2303 {
   2304 	struct timeval tp;
   2305 	u_int32_t tm;
   2306 	int s = splsoftnet();
   2307 
   2308 	microtime(&tp);
   2309 
   2310 	TV_DELTA(tp, vifp->tbf_last_pkt_t, tm);
   2311 
   2312 	/*
   2313 	 * This formula is actually
   2314 	 * "time in seconds" * "bytes/second".
   2315 	 *
   2316 	 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
   2317 	 *
   2318 	 * The (1000/1024) was introduced in add_vif to optimize
   2319 	 * this divide into a shift.
   2320 	 */
   2321 	vifp->tbf_n_tok += tm * vifp->v_rate_limit / 8192;
   2322 	vifp->tbf_last_pkt_t = tp;
   2323 
   2324 	if (vifp->tbf_n_tok > MAX_BKT_SIZE)
   2325 		vifp->tbf_n_tok = MAX_BKT_SIZE;
   2326 
   2327 	splx(s);
   2328 }
   2329 
   2330 static int
   2331 priority(vifp, ip)
   2332 	struct vif *vifp;
   2333 	struct ip *ip;
   2334 {
   2335 	int prio = 50;	/* the lowest priority -- default case */
   2336 
   2337 	/* temporary hack; may add general packet classifier some day */
   2338 
   2339 	/*
   2340 	 * The UDP port space is divided up into four priority ranges:
   2341 	 * [0, 16384)     : unclassified - lowest priority
   2342 	 * [16384, 32768) : audio - highest priority
   2343 	 * [32768, 49152) : whiteboard - medium priority
   2344 	 * [49152, 65536) : video - low priority
   2345 	 */
   2346 	if (ip->ip_p == IPPROTO_UDP) {
   2347 		struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2));
   2348 
   2349 		switch (ntohs(udp->uh_dport) & 0xc000) {
   2350 		case 0x4000:
   2351 			prio = 70;
   2352 			break;
   2353 		case 0x8000:
   2354 			prio = 60;
   2355 			break;
   2356 		case 0xc000:
   2357 			prio = 55;
   2358 			break;
   2359 		}
   2360 
   2361 		if (tbfdebug > 1)
   2362 			log(LOG_DEBUG, "port %x prio %d\n",
   2363 			    ntohs(udp->uh_dport), prio);
   2364 	}
   2365 
   2366 	return (prio);
   2367 }
   2368 
   2369 /*
   2370  * End of token bucket filter modifications
   2371  */
   2372 #ifdef RSVP_ISI
   2373 int
   2374 ip_rsvp_vif_init(so, m)
   2375 	struct socket *so;
   2376 	struct mbuf *m;
   2377 {
   2378 	int vifi, s;
   2379 
   2380 	if (rsvpdebug)
   2381 		printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
   2382 		    so->so_type, so->so_proto->pr_protocol);
   2383 
   2384 	if (so->so_type != SOCK_RAW ||
   2385 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
   2386 		return (EOPNOTSUPP);
   2387 
   2388 	/* Check mbuf. */
   2389 	if (m == NULL || m->m_len != sizeof(int)) {
   2390 		return (EINVAL);
   2391 	}
   2392 	vifi = *(mtod(m, int *));
   2393 
   2394 	if (rsvpdebug)
   2395 		printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n",
   2396 		       vifi, rsvp_on);
   2397 
   2398 	s = splsoftnet();
   2399 
   2400 	/* Check vif. */
   2401 	if (!legal_vif_num(vifi)) {
   2402 		splx(s);
   2403 		return (EADDRNOTAVAIL);
   2404 	}
   2405 
   2406 	/* Check if socket is available. */
   2407 	if (viftable[vifi].v_rsvpd != NULL) {
   2408 		splx(s);
   2409 		return (EADDRINUSE);
   2410 	}
   2411 
   2412 	viftable[vifi].v_rsvpd = so;
   2413 	/*
   2414 	 * This may seem silly, but we need to be sure we don't over-increment
   2415 	 * the RSVP counter, in case something slips up.
   2416 	 */
   2417 	if (!viftable[vifi].v_rsvp_on) {
   2418 		viftable[vifi].v_rsvp_on = 1;
   2419 		rsvp_on++;
   2420 	}
   2421 
   2422 	splx(s);
   2423 	return (0);
   2424 }
   2425 
   2426 int
   2427 ip_rsvp_vif_done(so, m)
   2428 	struct socket *so;
   2429 	struct mbuf *m;
   2430 {
   2431 	int vifi, s;
   2432 
   2433 	if (rsvpdebug)
   2434 		printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
   2435 		    so->so_type, so->so_proto->pr_protocol);
   2436 
   2437 	if (so->so_type != SOCK_RAW ||
   2438 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
   2439 		return (EOPNOTSUPP);
   2440 
   2441 	/* Check mbuf. */
   2442 	if (m == NULL || m->m_len != sizeof(int)) {
   2443 		return (EINVAL);
   2444 	}
   2445 	vifi = *(mtod(m, int *));
   2446 
   2447 	s = splsoftnet();
   2448 
   2449 	/* Check vif. */
   2450 	if (!legal_vif_num(vifi)) {
   2451 		splx(s);
   2452 		return (EADDRNOTAVAIL);
   2453 	}
   2454 
   2455 	if (rsvpdebug)
   2456 		printf("ip_rsvp_vif_done: v_rsvpd = %x so = %x\n",
   2457 		    viftable[vifi].v_rsvpd, so);
   2458 
   2459 	viftable[vifi].v_rsvpd = NULL;
   2460 	/*
   2461 	 * This may seem silly, but we need to be sure we don't over-decrement
   2462 	 * the RSVP counter, in case something slips up.
   2463 	 */
   2464 	if (viftable[vifi].v_rsvp_on) {
   2465 		viftable[vifi].v_rsvp_on = 0;
   2466 		rsvp_on--;
   2467 	}
   2468 
   2469 	splx(s);
   2470 	return (0);
   2471 }
   2472 
   2473 void
   2474 ip_rsvp_force_done(so)
   2475 	struct socket *so;
   2476 {
   2477 	int vifi, s;
   2478 
   2479 	/* Don't bother if it is not the right type of socket. */
   2480 	if (so->so_type != SOCK_RAW ||
   2481 	    so->so_proto->pr_protocol != IPPROTO_RSVP)
   2482 		return;
   2483 
   2484 	s = splsoftnet();
   2485 
   2486 	/*
   2487 	 * The socket may be attached to more than one vif...this
   2488 	 * is perfectly legal.
   2489 	 */
   2490 	for (vifi = 0; vifi < numvifs; vifi++) {
   2491 		if (viftable[vifi].v_rsvpd == so) {
   2492 			viftable[vifi].v_rsvpd = NULL;
   2493 			/*
   2494 			 * This may seem silly, but we need to be sure we don't
   2495 			 * over-decrement the RSVP counter, in case something
   2496 			 * slips up.
   2497 			 */
   2498 			if (viftable[vifi].v_rsvp_on) {
   2499 				viftable[vifi].v_rsvp_on = 0;
   2500 				rsvp_on--;
   2501 			}
   2502 		}
   2503 	}
   2504 
   2505 	splx(s);
   2506 	return;
   2507 }
   2508 
   2509 void
   2510 rsvp_input(m, ifp)
   2511 	struct mbuf *m;
   2512 	struct ifnet *ifp;
   2513 {
   2514 	int vifi, s;
   2515 	struct ip *ip = mtod(m, struct ip *);
   2516 	static struct sockaddr_in rsvp_src = { sizeof(sin), AF_INET };
   2517 
   2518 	if (rsvpdebug)
   2519 		printf("rsvp_input: rsvp_on %d\n", rsvp_on);
   2520 
   2521 	/*
   2522 	 * Can still get packets with rsvp_on = 0 if there is a local member
   2523 	 * of the group to which the RSVP packet is addressed.  But in this
   2524 	 * case we want to throw the packet away.
   2525 	 */
   2526 	if (!rsvp_on) {
   2527 		m_freem(m);
   2528 		return;
   2529 	}
   2530 
   2531 	/*
   2532 	 * If the old-style non-vif-associated socket is set, then use
   2533 	 * it and ignore the new ones.
   2534 	 */
   2535 	if (ip_rsvpd != NULL) {
   2536 		if (rsvpdebug)
   2537 			printf("rsvp_input: "
   2538 			    "Sending packet up old-style socket\n");
   2539 		rip_input(m);	/*XXX*/
   2540 		return;
   2541 	}
   2542 
   2543 	s = splsoftnet();
   2544 
   2545 	if (rsvpdebug)
   2546 		printf("rsvp_input: check vifs\n");
   2547 
   2548 	/* Find which vif the packet arrived on. */
   2549 	for (vifi = 0; vifi < numvifs; vifi++) {
   2550 		if (viftable[vifi].v_ifp == ifp)
   2551 			break;
   2552 	}
   2553 
   2554 	if (vifi == numvifs) {
   2555 		/* Can't find vif packet arrived on. Drop packet. */
   2556 		if (rsvpdebug)
   2557 			printf("rsvp_input: "
   2558 			    "Can't find vif for packet...dropping it.\n");
   2559 		m_freem(m);
   2560 		splx(s);
   2561 		return;
   2562 	}
   2563 
   2564 	if (rsvpdebug)
   2565 		printf("rsvp_input: check socket\n");
   2566 
   2567 	if (viftable[vifi].v_rsvpd == NULL) {
   2568 		/*
   2569 		 * drop packet, since there is no specific socket for this
   2570 		 * interface
   2571 		 */
   2572 		if (rsvpdebug)
   2573 			printf("rsvp_input: No socket defined for vif %d\n",
   2574 			    vifi);
   2575 		m_freem(m);
   2576 		splx(s);
   2577 		return;
   2578 	}
   2579 
   2580 	rsvp_src.sin_addr = ip->ip_src;
   2581 
   2582 	if (rsvpdebug && m)
   2583 		printf("rsvp_input: m->m_len = %d, sbspace() = %d\n",
   2584 		    m->m_len, sbspace(&viftable[vifi].v_rsvpd->so_rcv));
   2585 
   2586 	if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0)
   2587 		if (rsvpdebug)
   2588 			printf("rsvp_input: Failed to append to socket\n");
   2589 	else
   2590 		if (rsvpdebug)
   2591 			printf("rsvp_input: send packet up\n");
   2592 
   2593 	splx(s);
   2594 }
   2595 #endif /* RSVP_ISI */
   2596 
   2597 /*
   2598  * Code for bandwidth monitors
   2599  */
   2600 
   2601 /*
   2602  * Define common interface for timeval-related methods
   2603  */
   2604 #define	BW_TIMEVALCMP(tvp, uvp, cmp) timercmp((tvp), (uvp), cmp)
   2605 #define	BW_TIMEVALDECR(vvp, uvp) timersub((vvp), (uvp), (vvp))
   2606 #define	BW_TIMEVALADD(vvp, uvp) timeradd((vvp), (uvp), (vvp))
   2607 
   2608 static uint32_t
   2609 compute_bw_meter_flags(struct bw_upcall *req)
   2610 {
   2611     uint32_t flags = 0;
   2612 
   2613     if (req->bu_flags & BW_UPCALL_UNIT_PACKETS)
   2614 	flags |= BW_METER_UNIT_PACKETS;
   2615     if (req->bu_flags & BW_UPCALL_UNIT_BYTES)
   2616 	flags |= BW_METER_UNIT_BYTES;
   2617     if (req->bu_flags & BW_UPCALL_GEQ)
   2618 	flags |= BW_METER_GEQ;
   2619     if (req->bu_flags & BW_UPCALL_LEQ)
   2620 	flags |= BW_METER_LEQ;
   2621 
   2622     return flags;
   2623 }
   2624 
   2625 /*
   2626  * Add a bw_meter entry
   2627  */
   2628 static int
   2629 add_bw_upcall(struct mbuf *m)
   2630 {
   2631     int s;
   2632     struct mfc *mfc;
   2633     struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC,
   2634 		BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC };
   2635     struct timeval now;
   2636     struct bw_meter *x;
   2637     uint32_t flags;
   2638     struct bw_upcall *req;
   2639 
   2640     if (m == NULL || m->m_len < sizeof(struct bw_upcall))
   2641 	return EINVAL;
   2642 
   2643     req = mtod(m, struct bw_upcall *);
   2644 
   2645     if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
   2646 	return EOPNOTSUPP;
   2647 
   2648     /* Test if the flags are valid */
   2649     if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES)))
   2650 	return EINVAL;
   2651     if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)))
   2652 	return EINVAL;
   2653     if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
   2654 	    == (BW_UPCALL_GEQ | BW_UPCALL_LEQ))
   2655 	return EINVAL;
   2656 
   2657     /* Test if the threshold time interval is valid */
   2658     if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <))
   2659 	return EINVAL;
   2660 
   2661     flags = compute_bw_meter_flags(req);
   2662 
   2663     /*
   2664      * Find if we have already same bw_meter entry
   2665      */
   2666     s = splsoftnet();
   2667     mfc = mfc_find(&req->bu_src, &req->bu_dst);
   2668     if (mfc == NULL) {
   2669 	splx(s);
   2670 	return EADDRNOTAVAIL;
   2671     }
   2672     for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) {
   2673 	if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
   2674 			   &req->bu_threshold.b_time, ==)) &&
   2675 	    (x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
   2676 	    (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
   2677 	    (x->bm_flags & BW_METER_USER_FLAGS) == flags)  {
   2678 	    splx(s);
   2679 	    return 0;		/* XXX Already installed */
   2680 	}
   2681     }
   2682 
   2683     /* Allocate the new bw_meter entry */
   2684     x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT);
   2685     if (x == NULL) {
   2686 	splx(s);
   2687 	return ENOBUFS;
   2688     }
   2689 
   2690     /* Set the new bw_meter entry */
   2691     x->bm_threshold.b_time = req->bu_threshold.b_time;
   2692     microtime(&now);
   2693     x->bm_start_time = now;
   2694     x->bm_threshold.b_packets = req->bu_threshold.b_packets;
   2695     x->bm_threshold.b_bytes = req->bu_threshold.b_bytes;
   2696     x->bm_measured.b_packets = 0;
   2697     x->bm_measured.b_bytes = 0;
   2698     x->bm_flags = flags;
   2699     x->bm_time_next = NULL;
   2700     x->bm_time_hash = BW_METER_BUCKETS;
   2701 
   2702     /* Add the new bw_meter entry to the front of entries for this MFC */
   2703     x->bm_mfc = mfc;
   2704     x->bm_mfc_next = mfc->mfc_bw_meter;
   2705     mfc->mfc_bw_meter = x;
   2706     schedule_bw_meter(x, &now);
   2707     splx(s);
   2708 
   2709     return 0;
   2710 }
   2711 
   2712 static void
   2713 free_bw_list(struct bw_meter *list)
   2714 {
   2715     while (list != NULL) {
   2716 	struct bw_meter *x = list;
   2717 
   2718 	list = list->bm_mfc_next;
   2719 	unschedule_bw_meter(x);
   2720 	free(x, M_BWMETER);
   2721     }
   2722 }
   2723 
   2724 /*
   2725  * Delete one or multiple bw_meter entries
   2726  */
   2727 static int
   2728 del_bw_upcall(struct mbuf *m)
   2729 {
   2730     int s;
   2731     struct mfc *mfc;
   2732     struct bw_meter *x;
   2733     struct bw_upcall *req;
   2734 
   2735     if (m == NULL || m->m_len < sizeof(struct bw_upcall))
   2736 	return EINVAL;
   2737 
   2738     req = mtod(m, struct bw_upcall *);
   2739 
   2740     if (!(mrt_api_config & MRT_MFC_BW_UPCALL))
   2741 	return EOPNOTSUPP;
   2742 
   2743     s = splsoftnet();
   2744     /* Find the corresponding MFC entry */
   2745     mfc = mfc_find(&req->bu_src, &req->bu_dst);
   2746     if (mfc == NULL) {
   2747 	splx(s);
   2748 	return EADDRNOTAVAIL;
   2749     } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) {
   2750 	/*
   2751 	 * Delete all bw_meter entries for this mfc
   2752 	 */
   2753 	struct bw_meter *list;
   2754 
   2755 	list = mfc->mfc_bw_meter;
   2756 	mfc->mfc_bw_meter = NULL;
   2757 	free_bw_list(list);
   2758 	splx(s);
   2759 	return 0;
   2760     } else {			/* Delete a single bw_meter entry */
   2761 	struct bw_meter *prev;
   2762 	uint32_t flags = 0;
   2763 
   2764 	flags = compute_bw_meter_flags(req);
   2765 
   2766 	/* Find the bw_meter entry to delete */
   2767 	for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL;
   2768 	     prev = x, x = x->bm_mfc_next) {
   2769 	    if ((BW_TIMEVALCMP(&x->bm_threshold.b_time,
   2770 			       &req->bu_threshold.b_time, ==)) &&
   2771 		(x->bm_threshold.b_packets == req->bu_threshold.b_packets) &&
   2772 		(x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) &&
   2773 		(x->bm_flags & BW_METER_USER_FLAGS) == flags)
   2774 		break;
   2775 	}
   2776 	if (x != NULL) { /* Delete entry from the list for this MFC */
   2777 	    if (prev != NULL)
   2778 		prev->bm_mfc_next = x->bm_mfc_next;	/* remove from middle*/
   2779 	    else
   2780 		x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */
   2781 
   2782 	    unschedule_bw_meter(x);
   2783 	    splx(s);
   2784 	    /* Free the bw_meter entry */
   2785 	    free(x, M_BWMETER);
   2786 	    return 0;
   2787 	} else {
   2788 	    splx(s);
   2789 	    return EINVAL;
   2790 	}
   2791     }
   2792     /* NOTREACHED */
   2793 }
   2794 
   2795 /*
   2796  * Perform bandwidth measurement processing that may result in an upcall
   2797  */
   2798 static void
   2799 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp)
   2800 {
   2801     struct timeval delta;
   2802 
   2803     delta = *nowp;
   2804     BW_TIMEVALDECR(&delta, &x->bm_start_time);
   2805 
   2806     if (x->bm_flags & BW_METER_GEQ) {
   2807 	/*
   2808 	 * Processing for ">=" type of bw_meter entry
   2809 	 */
   2810 	if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
   2811 	    /* Reset the bw_meter entry */
   2812 	    x->bm_start_time = *nowp;
   2813 	    x->bm_measured.b_packets = 0;
   2814 	    x->bm_measured.b_bytes = 0;
   2815 	    x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
   2816 	}
   2817 
   2818 	/* Record that a packet is received */
   2819 	x->bm_measured.b_packets++;
   2820 	x->bm_measured.b_bytes += plen;
   2821 
   2822 	/*
   2823 	 * Test if we should deliver an upcall
   2824 	 */
   2825 	if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) {
   2826 	    if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
   2827 		 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) ||
   2828 		((x->bm_flags & BW_METER_UNIT_BYTES) &&
   2829 		 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) {
   2830 		/* Prepare an upcall for delivery */
   2831 		bw_meter_prepare_upcall(x, nowp);
   2832 		x->bm_flags |= BW_METER_UPCALL_DELIVERED;
   2833 	    }
   2834 	}
   2835     } else if (x->bm_flags & BW_METER_LEQ) {
   2836 	/*
   2837 	 * Processing for "<=" type of bw_meter entry
   2838 	 */
   2839 	if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) {
   2840 	    /*
   2841 	     * We are behind time with the multicast forwarding table
   2842 	     * scanning for "<=" type of bw_meter entries, so test now
   2843 	     * if we should deliver an upcall.
   2844 	     */
   2845 	    if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
   2846 		 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
   2847 		((x->bm_flags & BW_METER_UNIT_BYTES) &&
   2848 		 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
   2849 		/* Prepare an upcall for delivery */
   2850 		bw_meter_prepare_upcall(x, nowp);
   2851 	    }
   2852 	    /* Reschedule the bw_meter entry */
   2853 	    unschedule_bw_meter(x);
   2854 	    schedule_bw_meter(x, nowp);
   2855 	}
   2856 
   2857 	/* Record that a packet is received */
   2858 	x->bm_measured.b_packets++;
   2859 	x->bm_measured.b_bytes += plen;
   2860 
   2861 	/*
   2862 	 * Test if we should restart the measuring interval
   2863 	 */
   2864 	if ((x->bm_flags & BW_METER_UNIT_PACKETS &&
   2865 	     x->bm_measured.b_packets <= x->bm_threshold.b_packets) ||
   2866 	    (x->bm_flags & BW_METER_UNIT_BYTES &&
   2867 	     x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) {
   2868 	    /* Don't restart the measuring interval */
   2869 	} else {
   2870 	    /* Do restart the measuring interval */
   2871 	    /*
   2872 	     * XXX: note that we don't unschedule and schedule, because this
   2873 	     * might be too much overhead per packet. Instead, when we process
   2874 	     * all entries for a given timer hash bin, we check whether it is
   2875 	     * really a timeout. If not, we reschedule at that time.
   2876 	     */
   2877 	    x->bm_start_time = *nowp;
   2878 	    x->bm_measured.b_packets = 0;
   2879 	    x->bm_measured.b_bytes = 0;
   2880 	    x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
   2881 	}
   2882     }
   2883 }
   2884 
   2885 /*
   2886  * Prepare a bandwidth-related upcall
   2887  */
   2888 static void
   2889 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp)
   2890 {
   2891     struct timeval delta;
   2892     struct bw_upcall *u;
   2893 
   2894     /*
   2895      * Compute the measured time interval
   2896      */
   2897     delta = *nowp;
   2898     BW_TIMEVALDECR(&delta, &x->bm_start_time);
   2899 
   2900     /*
   2901      * If there are too many pending upcalls, deliver them now
   2902      */
   2903     if (bw_upcalls_n >= BW_UPCALLS_MAX)
   2904 	bw_upcalls_send();
   2905 
   2906     /*
   2907      * Set the bw_upcall entry
   2908      */
   2909     u = &bw_upcalls[bw_upcalls_n++];
   2910     u->bu_src = x->bm_mfc->mfc_origin;
   2911     u->bu_dst = x->bm_mfc->mfc_mcastgrp;
   2912     u->bu_threshold.b_time = x->bm_threshold.b_time;
   2913     u->bu_threshold.b_packets = x->bm_threshold.b_packets;
   2914     u->bu_threshold.b_bytes = x->bm_threshold.b_bytes;
   2915     u->bu_measured.b_time = delta;
   2916     u->bu_measured.b_packets = x->bm_measured.b_packets;
   2917     u->bu_measured.b_bytes = x->bm_measured.b_bytes;
   2918     u->bu_flags = 0;
   2919     if (x->bm_flags & BW_METER_UNIT_PACKETS)
   2920 	u->bu_flags |= BW_UPCALL_UNIT_PACKETS;
   2921     if (x->bm_flags & BW_METER_UNIT_BYTES)
   2922 	u->bu_flags |= BW_UPCALL_UNIT_BYTES;
   2923     if (x->bm_flags & BW_METER_GEQ)
   2924 	u->bu_flags |= BW_UPCALL_GEQ;
   2925     if (x->bm_flags & BW_METER_LEQ)
   2926 	u->bu_flags |= BW_UPCALL_LEQ;
   2927 }
   2928 
   2929 /*
   2930  * Send the pending bandwidth-related upcalls
   2931  */
   2932 static void
   2933 bw_upcalls_send(void)
   2934 {
   2935     struct mbuf *m;
   2936     int len = bw_upcalls_n * sizeof(bw_upcalls[0]);
   2937     struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
   2938     static struct igmpmsg igmpmsg = { 0,		/* unused1 */
   2939 				      0,		/* unused2 */
   2940 				      IGMPMSG_BW_UPCALL,/* im_msgtype */
   2941 				      0,		/* im_mbz  */
   2942 				      0,		/* im_vif  */
   2943 				      0,		/* unused3 */
   2944 				      { 0 },		/* im_src  */
   2945 				      { 0 } };		/* im_dst  */
   2946 
   2947     if (bw_upcalls_n == 0)
   2948 	return;			/* No pending upcalls */
   2949 
   2950     bw_upcalls_n = 0;
   2951 
   2952     /*
   2953      * Allocate a new mbuf, initialize it with the header and
   2954      * the payload for the pending calls.
   2955      */
   2956     MGETHDR(m, M_DONTWAIT, MT_HEADER);
   2957     if (m == NULL) {
   2958 	log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n");
   2959 	return;
   2960     }
   2961 
   2962     m->m_len = m->m_pkthdr.len = 0;
   2963     m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg);
   2964     m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]);
   2965 
   2966     /*
   2967      * Send the upcalls
   2968      * XXX do we need to set the address in k_igmpsrc ?
   2969      */
   2970     mrtstat.mrts_upcalls++;
   2971     if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) {
   2972 	log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n");
   2973 	++mrtstat.mrts_upq_sockfull;
   2974     }
   2975 }
   2976 
   2977 /*
   2978  * Compute the timeout hash value for the bw_meter entries
   2979  */
   2980 #define	BW_METER_TIMEHASH(bw_meter, hash)				\
   2981     do {								\
   2982 	struct timeval next_timeval = (bw_meter)->bm_start_time;	\
   2983 									\
   2984 	BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
   2985 	(hash) = next_timeval.tv_sec;					\
   2986 	if (next_timeval.tv_usec)					\
   2987 	    (hash)++; /* XXX: make sure we don't timeout early */	\
   2988 	(hash) %= BW_METER_BUCKETS;					\
   2989     } while (/*CONSTCOND*/ 0)
   2990 
   2991 /*
   2992  * Schedule a timer to process periodically bw_meter entry of type "<="
   2993  * by linking the entry in the proper hash bucket.
   2994  */
   2995 static void
   2996 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp)
   2997 {
   2998     int time_hash;
   2999 
   3000     if (!(x->bm_flags & BW_METER_LEQ))
   3001 	return;		/* XXX: we schedule timers only for "<=" entries */
   3002 
   3003     /*
   3004      * Reset the bw_meter entry
   3005      */
   3006     x->bm_start_time = *nowp;
   3007     x->bm_measured.b_packets = 0;
   3008     x->bm_measured.b_bytes = 0;
   3009     x->bm_flags &= ~BW_METER_UPCALL_DELIVERED;
   3010 
   3011     /*
   3012      * Compute the timeout hash value and insert the entry
   3013      */
   3014     BW_METER_TIMEHASH(x, time_hash);
   3015     x->bm_time_next = bw_meter_timers[time_hash];
   3016     bw_meter_timers[time_hash] = x;
   3017     x->bm_time_hash = time_hash;
   3018 }
   3019 
   3020 /*
   3021  * Unschedule the periodic timer that processes bw_meter entry of type "<="
   3022  * by removing the entry from the proper hash bucket.
   3023  */
   3024 static void
   3025 unschedule_bw_meter(struct bw_meter *x)
   3026 {
   3027     int time_hash;
   3028     struct bw_meter *prev, *tmp;
   3029 
   3030     if (!(x->bm_flags & BW_METER_LEQ))
   3031 	return;		/* XXX: we schedule timers only for "<=" entries */
   3032 
   3033     /*
   3034      * Compute the timeout hash value and delete the entry
   3035      */
   3036     time_hash = x->bm_time_hash;
   3037     if (time_hash >= BW_METER_BUCKETS)
   3038 	return;		/* Entry was not scheduled */
   3039 
   3040     for (prev = NULL, tmp = bw_meter_timers[time_hash];
   3041 	     tmp != NULL; prev = tmp, tmp = tmp->bm_time_next)
   3042 	if (tmp == x)
   3043 	    break;
   3044 
   3045     if (tmp == NULL)
   3046 	panic("unschedule_bw_meter: bw_meter entry not found");
   3047 
   3048     if (prev != NULL)
   3049 	prev->bm_time_next = x->bm_time_next;
   3050     else
   3051 	bw_meter_timers[time_hash] = x->bm_time_next;
   3052 
   3053     x->bm_time_next = NULL;
   3054     x->bm_time_hash = BW_METER_BUCKETS;
   3055 }
   3056 
   3057 /*
   3058  * Process all "<=" type of bw_meter that should be processed now,
   3059  * and for each entry prepare an upcall if necessary. Each processed
   3060  * entry is rescheduled again for the (periodic) processing.
   3061  *
   3062  * This is run periodically (once per second normally). On each round,
   3063  * all the potentially matching entries are in the hash slot that we are
   3064  * looking at.
   3065  */
   3066 static void
   3067 bw_meter_process()
   3068 {
   3069     int s;
   3070     static uint32_t last_tv_sec;	/* last time we processed this */
   3071 
   3072     uint32_t loops;
   3073     int i;
   3074     struct timeval now, process_endtime;
   3075 
   3076     microtime(&now);
   3077     if (last_tv_sec == now.tv_sec)
   3078 	return;		/* nothing to do */
   3079 
   3080     loops = now.tv_sec - last_tv_sec;
   3081     last_tv_sec = now.tv_sec;
   3082     if (loops > BW_METER_BUCKETS)
   3083 	loops = BW_METER_BUCKETS;
   3084 
   3085     s = splsoftnet();
   3086     /*
   3087      * Process all bins of bw_meter entries from the one after the last
   3088      * processed to the current one. On entry, i points to the last bucket
   3089      * visited, so we need to increment i at the beginning of the loop.
   3090      */
   3091     for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) {
   3092 	struct bw_meter *x, *tmp_list;
   3093 
   3094 	if (++i >= BW_METER_BUCKETS)
   3095 	    i = 0;
   3096 
   3097 	/* Disconnect the list of bw_meter entries from the bin */
   3098 	tmp_list = bw_meter_timers[i];
   3099 	bw_meter_timers[i] = NULL;
   3100 
   3101 	/* Process the list of bw_meter entries */
   3102 	while (tmp_list != NULL) {
   3103 	    x = tmp_list;
   3104 	    tmp_list = tmp_list->bm_time_next;
   3105 
   3106 	    /* Test if the time interval is over */
   3107 	    process_endtime = x->bm_start_time;
   3108 	    BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time);
   3109 	    if (BW_TIMEVALCMP(&process_endtime, &now, >)) {
   3110 		/* Not yet: reschedule, but don't reset */
   3111 		int time_hash;
   3112 
   3113 		BW_METER_TIMEHASH(x, time_hash);
   3114 		if (time_hash == i && process_endtime.tv_sec == now.tv_sec) {
   3115 		    /*
   3116 		     * XXX: somehow the bin processing is a bit ahead of time.
   3117 		     * Put the entry in the next bin.
   3118 		     */
   3119 		    if (++time_hash >= BW_METER_BUCKETS)
   3120 			time_hash = 0;
   3121 		}
   3122 		x->bm_time_next = bw_meter_timers[time_hash];
   3123 		bw_meter_timers[time_hash] = x;
   3124 		x->bm_time_hash = time_hash;
   3125 
   3126 		continue;
   3127 	    }
   3128 
   3129 	    /*
   3130 	     * Test if we should deliver an upcall
   3131 	     */
   3132 	    if (((x->bm_flags & BW_METER_UNIT_PACKETS) &&
   3133 		 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) ||
   3134 		((x->bm_flags & BW_METER_UNIT_BYTES) &&
   3135 		 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) {
   3136 		/* Prepare an upcall for delivery */
   3137 		bw_meter_prepare_upcall(x, &now);
   3138 	    }
   3139 
   3140 	    /*
   3141 	     * Reschedule for next processing
   3142 	     */
   3143 	    schedule_bw_meter(x, &now);
   3144 	}
   3145     }
   3146 
   3147     /* Send all upcalls that are pending delivery */
   3148     bw_upcalls_send();
   3149 
   3150     splx(s);
   3151 }
   3152 
   3153 /*
   3154  * A periodic function for sending all upcalls that are pending delivery
   3155  */
   3156 static void
   3157 expire_bw_upcalls_send(void *unused)
   3158 {
   3159     int s;
   3160 
   3161     s = splsoftnet();
   3162     bw_upcalls_send();
   3163     splx(s);
   3164 
   3165     callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD,
   3166 		  expire_bw_upcalls_send, NULL);
   3167 }
   3168 
   3169 /*
   3170  * A periodic function for periodic scanning of the multicast forwarding
   3171  * table for processing all "<=" bw_meter entries.
   3172  */
   3173 static void
   3174 expire_bw_meter_process(void *unused)
   3175 {
   3176     if (mrt_api_config & MRT_MFC_BW_UPCALL)
   3177 	bw_meter_process();
   3178 
   3179     callout_reset(&bw_meter_ch, BW_METER_PERIOD,
   3180 		  expire_bw_meter_process, NULL);
   3181 }
   3182 
   3183 /*
   3184  * End of bandwidth monitoring code
   3185  */
   3186 
   3187 #ifdef PIM
   3188 /*
   3189  * Send the packet up to the user daemon, or eventually do kernel encapsulation
   3190  */
   3191 static int
   3192 pim_register_send(struct ip *ip, struct vif *vifp,
   3193 	struct mbuf *m, struct mfc *rt)
   3194 {
   3195     struct mbuf *mb_copy, *mm;
   3196 
   3197     if (mrtdebug & DEBUG_PIM)
   3198         log(LOG_DEBUG, "pim_register_send: ");
   3199 
   3200     mb_copy = pim_register_prepare(ip, m);
   3201     if (mb_copy == NULL)
   3202 	return ENOBUFS;
   3203 
   3204     /*
   3205      * Send all the fragments. Note that the mbuf for each fragment
   3206      * is freed by the sending machinery.
   3207      */
   3208     for (mm = mb_copy; mm; mm = mb_copy) {
   3209 	mb_copy = mm->m_nextpkt;
   3210 	mm->m_nextpkt = NULL;
   3211 	mm = m_pullup(mm, sizeof(struct ip));
   3212 	if (mm != NULL) {
   3213 	    ip = mtod(mm, struct ip *);
   3214 	    if ((mrt_api_config & MRT_MFC_RP) &&
   3215 		!in_nullhost(rt->mfc_rp)) {
   3216 		pim_register_send_rp(ip, vifp, mm, rt);
   3217 	    } else {
   3218 		pim_register_send_upcall(ip, vifp, mm, rt);
   3219 	    }
   3220 	}
   3221     }
   3222 
   3223     return 0;
   3224 }
   3225 
   3226 /*
   3227  * Return a copy of the data packet that is ready for PIM Register
   3228  * encapsulation.
   3229  * XXX: Note that in the returned copy the IP header is a valid one.
   3230  */
   3231 static struct mbuf *
   3232 pim_register_prepare(struct ip *ip, struct mbuf *m)
   3233 {
   3234     struct mbuf *mb_copy = NULL;
   3235     int mtu;
   3236 
   3237     /* Take care of delayed checksums */
   3238     if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) {
   3239 	in_delayed_cksum(m);
   3240 	m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4);
   3241     }
   3242 
   3243     /*
   3244      * Copy the old packet & pullup its IP header into the
   3245      * new mbuf so we can modify it.
   3246      */
   3247     mb_copy = m_copy(m, 0, M_COPYALL);
   3248     if (mb_copy == NULL)
   3249 	return NULL;
   3250     mb_copy = m_pullup(mb_copy, ip->ip_hl << 2);
   3251     if (mb_copy == NULL)
   3252 	return NULL;
   3253 
   3254     /* take care of the TTL */
   3255     ip = mtod(mb_copy, struct ip *);
   3256     --ip->ip_ttl;
   3257 
   3258     /* Compute the MTU after the PIM Register encapsulation */
   3259     mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr);
   3260 
   3261     if (ntohs(ip->ip_len) <= mtu) {
   3262 	/* Turn the IP header into a valid one */
   3263 	ip->ip_sum = 0;
   3264 	ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
   3265     } else {
   3266 	/* Fragment the packet */
   3267 	if (ip_fragment(mb_copy, NULL, mtu) != 0) {
   3268 	    /* XXX: mb_copy was freed by ip_fragment() */
   3269 	    return NULL;
   3270 	}
   3271     }
   3272     return mb_copy;
   3273 }
   3274 
   3275 /*
   3276  * Send an upcall with the data packet to the user-level process.
   3277  */
   3278 static int
   3279 pim_register_send_upcall(struct ip *ip, struct vif *vifp,
   3280 	struct mbuf *mb_copy, struct mfc *rt)
   3281 {
   3282     struct mbuf *mb_first;
   3283     int len = ntohs(ip->ip_len);
   3284     struct igmpmsg *im;
   3285     struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET };
   3286 
   3287     /*
   3288      * Add a new mbuf with an upcall header
   3289      */
   3290     MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
   3291     if (mb_first == NULL) {
   3292 	m_freem(mb_copy);
   3293 	return ENOBUFS;
   3294     }
   3295     mb_first->m_data += max_linkhdr;
   3296     mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg);
   3297     mb_first->m_len = sizeof(struct igmpmsg);
   3298     mb_first->m_next = mb_copy;
   3299 
   3300     /* Send message to routing daemon */
   3301     im = mtod(mb_first, struct igmpmsg *);
   3302     im->im_msgtype	= IGMPMSG_WHOLEPKT;
   3303     im->im_mbz		= 0;
   3304     im->im_vif		= vifp - viftable;
   3305     im->im_src		= ip->ip_src;
   3306     im->im_dst		= ip->ip_dst;
   3307 
   3308     k_igmpsrc.sin_addr	= ip->ip_src;
   3309 
   3310     mrtstat.mrts_upcalls++;
   3311 
   3312     if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) {
   3313 	if (mrtdebug & DEBUG_PIM)
   3314 	    log(LOG_WARNING,
   3315 		"mcast: pim_register_send_upcall: ip_mrouter socket queue full");
   3316 	++mrtstat.mrts_upq_sockfull;
   3317 	return ENOBUFS;
   3318     }
   3319 
   3320     /* Keep statistics */
   3321     pimstat.pims_snd_registers_msgs++;
   3322     pimstat.pims_snd_registers_bytes += len;
   3323 
   3324     return 0;
   3325 }
   3326 
   3327 /*
   3328  * Encapsulate the data packet in PIM Register message and send it to the RP.
   3329  */
   3330 static int
   3331 pim_register_send_rp(struct ip *ip, struct vif *vifp,
   3332 	struct mbuf *mb_copy, struct mfc *rt)
   3333 {
   3334     struct mbuf *mb_first;
   3335     struct ip *ip_outer;
   3336     struct pim_encap_pimhdr *pimhdr;
   3337     int len = ntohs(ip->ip_len);
   3338     vifi_t vifi = rt->mfc_parent;
   3339 
   3340     if ((vifi >= numvifs) || in_nullhost(viftable[vifi].v_lcl_addr)) {
   3341 	m_freem(mb_copy);
   3342 	return EADDRNOTAVAIL;		/* The iif vif is invalid */
   3343     }
   3344 
   3345     /*
   3346      * Add a new mbuf with the encapsulating header
   3347      */
   3348     MGETHDR(mb_first, M_DONTWAIT, MT_HEADER);
   3349     if (mb_first == NULL) {
   3350 	m_freem(mb_copy);
   3351 	return ENOBUFS;
   3352     }
   3353     mb_first->m_data += max_linkhdr;
   3354     mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr);
   3355     mb_first->m_next = mb_copy;
   3356 
   3357     mb_first->m_pkthdr.len = len + mb_first->m_len;
   3358 
   3359     /*
   3360      * Fill in the encapsulating IP and PIM header
   3361      */
   3362     ip_outer = mtod(mb_first, struct ip *);
   3363     *ip_outer = pim_encap_iphdr;
   3364     ip_outer->ip_id = ip_newid();
   3365     ip_outer->ip_len = htons(len + sizeof(pim_encap_iphdr) +
   3366 			     sizeof(pim_encap_pimhdr));
   3367     ip_outer->ip_src = viftable[vifi].v_lcl_addr;
   3368     ip_outer->ip_dst = rt->mfc_rp;
   3369     /*
   3370      * Copy the inner header TOS to the outer header, and take care of the
   3371      * IP_DF bit.
   3372      */
   3373     ip_outer->ip_tos = ip->ip_tos;
   3374     if (ntohs(ip->ip_off) & IP_DF)
   3375 	ip_outer->ip_off |= IP_DF;
   3376     pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer
   3377 					 + sizeof(pim_encap_iphdr));
   3378     *pimhdr = pim_encap_pimhdr;
   3379     /* If the iif crosses a border, set the Border-bit */
   3380     if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config)
   3381 	pimhdr->flags |= htonl(PIM_BORDER_REGISTER);
   3382 
   3383     mb_first->m_data += sizeof(pim_encap_iphdr);
   3384     pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr));
   3385     mb_first->m_data -= sizeof(pim_encap_iphdr);
   3386 
   3387     if (vifp->v_rate_limit == 0)
   3388 	tbf_send_packet(vifp, mb_first);
   3389     else
   3390 	tbf_control(vifp, mb_first, ip, ntohs(ip_outer->ip_len));
   3391 
   3392     /* Keep statistics */
   3393     pimstat.pims_snd_registers_msgs++;
   3394     pimstat.pims_snd_registers_bytes += len;
   3395 
   3396     return 0;
   3397 }
   3398 
   3399 /*
   3400  * PIM-SMv2 and PIM-DM messages processing.
   3401  * Receives and verifies the PIM control messages, and passes them
   3402  * up to the listening socket, using rip_input().
   3403  * The only message with special processing is the PIM_REGISTER message
   3404  * (used by PIM-SM): the PIM header is stripped off, and the inner packet
   3405  * is passed to if_simloop().
   3406  */
   3407 void
   3408 pim_input(struct mbuf *m, ...)
   3409 {
   3410     struct ip *ip = mtod(m, struct ip *);
   3411     struct pim *pim;
   3412     int minlen;
   3413     int datalen;
   3414     int ip_tos;
   3415     int proto;
   3416     int iphlen;
   3417     va_list ap;
   3418 
   3419     va_start(ap, m);
   3420     iphlen = va_arg(ap, int);
   3421     proto = va_arg(ap, int);
   3422     va_end(ap);
   3423 
   3424     datalen = ntohs(ip->ip_len) - iphlen;
   3425 
   3426     /* Keep statistics */
   3427     pimstat.pims_rcv_total_msgs++;
   3428     pimstat.pims_rcv_total_bytes += datalen;
   3429 
   3430     /*
   3431      * Validate lengths
   3432      */
   3433     if (datalen < PIM_MINLEN) {
   3434 	pimstat.pims_rcv_tooshort++;
   3435 	log(LOG_ERR, "pim_input: packet size too small %d from %lx\n",
   3436 	    datalen, (u_long)ip->ip_src.s_addr);
   3437 	m_freem(m);
   3438 	return;
   3439     }
   3440 
   3441     /*
   3442      * If the packet is at least as big as a REGISTER, go agead
   3443      * and grab the PIM REGISTER header size, to avoid another
   3444      * possible m_pullup() later.
   3445      *
   3446      * PIM_MINLEN       == pimhdr + u_int32_t == 4 + 4 = 8
   3447      * PIM_REG_MINLEN   == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
   3448      */
   3449     minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN);
   3450     /*
   3451      * Get the IP and PIM headers in contiguous memory, and
   3452      * possibly the PIM REGISTER header.
   3453      */
   3454     if ((m->m_flags & M_EXT || m->m_len < minlen) &&
   3455 	(m = m_pullup(m, minlen)) == NULL) {
   3456 	log(LOG_ERR, "pim_input: m_pullup failure\n");
   3457 	return;
   3458     }
   3459     /* m_pullup() may have given us a new mbuf so reset ip. */
   3460     ip = mtod(m, struct ip *);
   3461     ip_tos = ip->ip_tos;
   3462 
   3463     /* adjust mbuf to point to the PIM header */
   3464     m->m_data += iphlen;
   3465     m->m_len  -= iphlen;
   3466     pim = mtod(m, struct pim *);
   3467 
   3468     /*
   3469      * Validate checksum. If PIM REGISTER, exclude the data packet.
   3470      *
   3471      * XXX: some older PIMv2 implementations don't make this distinction,
   3472      * so for compatibility reason perform the checksum over part of the
   3473      * message, and if error, then over the whole message.
   3474      */
   3475     if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) {
   3476 	/* do nothing, checksum okay */
   3477     } else if (in_cksum(m, datalen)) {
   3478 	pimstat.pims_rcv_badsum++;
   3479 	if (mrtdebug & DEBUG_PIM)
   3480 	    log(LOG_DEBUG, "pim_input: invalid checksum");
   3481 	m_freem(m);
   3482 	return;
   3483     }
   3484 
   3485     /* PIM version check */
   3486     if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) {
   3487 	pimstat.pims_rcv_badversion++;
   3488 	log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n",
   3489 	    PIM_VT_V(pim->pim_vt), PIM_VERSION);
   3490 	m_freem(m);
   3491 	return;
   3492     }
   3493 
   3494     /* restore mbuf back to the outer IP */
   3495     m->m_data -= iphlen;
   3496     m->m_len  += iphlen;
   3497 
   3498     if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) {
   3499 	/*
   3500 	 * Since this is a REGISTER, we'll make a copy of the register
   3501 	 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
   3502 	 * routing daemon.
   3503 	 */
   3504 	int s;
   3505 	struct sockaddr_in dst = { sizeof(dst), AF_INET };
   3506 	struct mbuf *mcp;
   3507 	struct ip *encap_ip;
   3508 	u_int32_t *reghdr;
   3509 	struct ifnet *vifp;
   3510 
   3511 	s = splsoftnet();
   3512 	if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) {
   3513 	    splx(s);
   3514 	    if (mrtdebug & DEBUG_PIM)
   3515 		log(LOG_DEBUG,
   3516 		    "pim_input: register vif not set: %d\n", reg_vif_num);
   3517 	    m_freem(m);
   3518 	    return;
   3519 	}
   3520 	/* XXX need refcnt? */
   3521 	vifp = viftable[reg_vif_num].v_ifp;
   3522 	splx(s);
   3523 
   3524 	/*
   3525 	 * Validate length
   3526 	 */
   3527 	if (datalen < PIM_REG_MINLEN) {
   3528 	    pimstat.pims_rcv_tooshort++;
   3529 	    pimstat.pims_rcv_badregisters++;
   3530 	    log(LOG_ERR,
   3531 		"pim_input: register packet size too small %d from %lx\n",
   3532 		datalen, (u_long)ip->ip_src.s_addr);
   3533 	    m_freem(m);
   3534 	    return;
   3535 	}
   3536 
   3537 	reghdr = (u_int32_t *)(pim + 1);
   3538 	encap_ip = (struct ip *)(reghdr + 1);
   3539 
   3540 	if (mrtdebug & DEBUG_PIM) {
   3541 	    log(LOG_DEBUG,
   3542 		"pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
   3543 		(u_long)ntohl(encap_ip->ip_src.s_addr),
   3544 		(u_long)ntohl(encap_ip->ip_dst.s_addr),
   3545 		ntohs(encap_ip->ip_len));
   3546 	}
   3547 
   3548 	/* verify the version number of the inner packet */
   3549 	if (encap_ip->ip_v != IPVERSION) {
   3550 	    pimstat.pims_rcv_badregisters++;
   3551 	    if (mrtdebug & DEBUG_PIM) {
   3552 		log(LOG_DEBUG, "pim_input: invalid IP version (%d) "
   3553 		    "of the inner packet\n", encap_ip->ip_v);
   3554 	    }
   3555 	    m_freem(m);
   3556 	    return;
   3557 	}
   3558 
   3559 	/* verify the inner packet is destined to a mcast group */
   3560 	if (!IN_MULTICAST(encap_ip->ip_dst.s_addr)) {
   3561 	    pimstat.pims_rcv_badregisters++;
   3562 	    if (mrtdebug & DEBUG_PIM)
   3563 		log(LOG_DEBUG,
   3564 		    "pim_input: inner packet of register is not "
   3565 		    "multicast %lx\n",
   3566 		    (u_long)ntohl(encap_ip->ip_dst.s_addr));
   3567 	    m_freem(m);
   3568 	    return;
   3569 	}
   3570 
   3571 	/* If a NULL_REGISTER, pass it to the daemon */
   3572 	if ((ntohl(*reghdr) & PIM_NULL_REGISTER))
   3573 	    goto pim_input_to_daemon;
   3574 
   3575 	/*
   3576 	 * Copy the TOS from the outer IP header to the inner IP header.
   3577 	 */
   3578 	if (encap_ip->ip_tos != ip_tos) {
   3579 	    /* Outer TOS -> inner TOS */
   3580 	    encap_ip->ip_tos = ip_tos;
   3581 	    /* Recompute the inner header checksum. Sigh... */
   3582 
   3583 	    /* adjust mbuf to point to the inner IP header */
   3584 	    m->m_data += (iphlen + PIM_MINLEN);
   3585 	    m->m_len  -= (iphlen + PIM_MINLEN);
   3586 
   3587 	    encap_ip->ip_sum = 0;
   3588 	    encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2);
   3589 
   3590 	    /* restore mbuf to point back to the outer IP header */
   3591 	    m->m_data -= (iphlen + PIM_MINLEN);
   3592 	    m->m_len  += (iphlen + PIM_MINLEN);
   3593 	}
   3594 
   3595 	/*
   3596 	 * Decapsulate the inner IP packet and loopback to forward it
   3597 	 * as a normal multicast packet. Also, make a copy of the
   3598 	 *     outer_iphdr + pimhdr + reghdr + encap_iphdr
   3599 	 * to pass to the daemon later, so it can take the appropriate
   3600 	 * actions (e.g., send back PIM_REGISTER_STOP).
   3601 	 * XXX: here m->m_data points to the outer IP header.
   3602 	 */
   3603 	mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN);
   3604 	if (mcp == NULL) {
   3605 	    log(LOG_ERR,
   3606 		"pim_input: pim register: could not copy register head\n");
   3607 	    m_freem(m);
   3608 	    return;
   3609 	}
   3610 
   3611 	/* Keep statistics */
   3612 	/* XXX: registers_bytes include only the encap. mcast pkt */
   3613 	pimstat.pims_rcv_registers_msgs++;
   3614 	pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len);
   3615 
   3616 	/*
   3617 	 * forward the inner ip packet; point m_data at the inner ip.
   3618 	 */
   3619 	m_adj(m, iphlen + PIM_MINLEN);
   3620 
   3621 	if (mrtdebug & DEBUG_PIM) {
   3622 	    log(LOG_DEBUG,
   3623 		"pim_input: forwarding decapsulated register: "
   3624 		"src %lx, dst %lx, vif %d\n",
   3625 		(u_long)ntohl(encap_ip->ip_src.s_addr),
   3626 		(u_long)ntohl(encap_ip->ip_dst.s_addr),
   3627 		reg_vif_num);
   3628 	}
   3629 	/* NB: vifp was collected above; can it change on us? */
   3630 	looutput(vifp, m, (struct sockaddr *)&dst, (struct rtentry *)NULL);
   3631 
   3632 	/* prepare the register head to send to the mrouting daemon */
   3633 	m = mcp;
   3634     }
   3635 
   3636 pim_input_to_daemon:
   3637     /*
   3638      * Pass the PIM message up to the daemon; if it is a Register message,
   3639      * pass the 'head' only up to the daemon. This includes the
   3640      * outer IP header, PIM header, PIM-Register header and the
   3641      * inner IP header.
   3642      * XXX: the outer IP header pkt size of a Register is not adjust to
   3643      * reflect the fact that the inner multicast data is truncated.
   3644      */
   3645     rip_input(m, iphlen, proto);
   3646 
   3647     return;
   3648 }
   3649 #endif /* PIM */
   3650