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