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