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