xref: /openbsd-src/sys/netinet6/ip6_mroute.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: ip6_mroute.c,v 1.122 2019/09/04 16:13:49 bluhm Exp $	*/
2 /*	$NetBSD: ip6_mroute.c,v 1.59 2003/12/10 09:28:38 itojun Exp $	*/
3 /*	$KAME: ip6_mroute.c,v 1.45 2001/03/25 08:38:51 itojun Exp $	*/
4 
5 /*
6  * Copyright (C) 1998 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*	BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp	*/
35 
36 /*
37  * Copyright (c) 1989 Stephen Deering
38  * Copyright (c) 1992, 1993
39  *      The Regents of the University of California.  All rights reserved.
40  *
41  * This code is derived from software contributed to Berkeley by
42  * Stephen Deering of Stanford University.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. Neither the name of the University nor the names of its contributors
53  *    may be used to endorse or promote products derived from this software
54  *    without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66  * SUCH DAMAGE.
67  *
68  *      @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
69  */
70 
71 /*
72  * IP multicast forwarding procedures
73  *
74  * Written by David Waitzman, BBN Labs, August 1988.
75  * Modified by Steve Deering, Stanford, February 1989.
76  * Modified by Mark J. Steiglitz, Stanford, May, 1991
77  * Modified by Van Jacobson, LBL, January 1993
78  * Modified by Ajit Thyagarajan, PARC, August 1993
79  * Modified by Bill Fenner, PARC, April 1994
80  *
81  * MROUTING Revision: 3.5.1.2
82  */
83 
84 #include <sys/param.h>
85 #include <sys/malloc.h>
86 #include <sys/systm.h>
87 #include <sys/timeout.h>
88 #include <sys/mbuf.h>
89 #include <sys/socket.h>
90 #include <sys/socketvar.h>
91 #include <sys/protosw.h>
92 #include <sys/kernel.h>
93 #include <sys/ioctl.h>
94 #include <sys/syslog.h>
95 #include <sys/sysctl.h>
96 
97 #include <net/if.h>
98 #include <net/if_var.h>
99 #include <net/route.h>
100 
101 #include <netinet/in.h>
102 #include <netinet6/in6_var.h>
103 #include <netinet/ip.h>
104 #include <netinet/ip6.h>
105 #include <netinet/icmp6.h>
106 #include <netinet6/ip6_var.h>
107 #include <netinet6/ip6_mroute.h>
108 #include <netinet/in_pcb.h>
109 
110 /* #define MCAST_DEBUG */
111 
112 #ifdef MCAST_DEBUG
113 int mcast6_debug = 1;
114 #define DPRINTF(fmt, args...)						\
115 	do {								\
116 		if (mcast6_debug)					\
117 			printf("%s:%d " fmt "\n",			\
118 			    __func__, __LINE__, ## args);		\
119 	} while (0)
120 #else
121 #define DPRINTF(fmt, args...)			\
122 	do { } while (0)
123 #endif
124 
125 int ip6_mdq(struct mbuf *, struct ifnet *, struct rtentry *);
126 void phyint_send6(struct ifnet *, struct ip6_hdr *, struct mbuf *);
127 
128 /*
129  * Globals.  All but ip6_mrouter, ip6_mrtproto and mrt6stat could be static,
130  * except for netstat or debugging purposes.
131  */
132 struct socket  *ip6_mrouter[RT_TABLEID_MAX + 1];
133 struct rttimer_queue *mrouter6q[RT_TABLEID_MAX + 1];
134 int		ip6_mrouter_ver = 0;
135 int		ip6_mrtproto;    /* for netstat only */
136 struct mrt6stat	mrt6stat;
137 
138 #define NO_RTE_FOUND	0x1
139 #define RTE_FOUND	0x2
140 
141 #define		MCAST_EXPIRE_TIMEOUT 30		/* seconds */
142 
143 /*
144  * Macros to compute elapsed time efficiently
145  * Borrowed from Van Jacobson's scheduling code
146  */
147 #define TV_DELTA(a, b, delta) do { \
148 	    int xxs; \
149 		\
150 	    delta = (a).tv_usec - (b).tv_usec; \
151 	    if ((xxs = (a).tv_sec - (b).tv_sec)) { \
152 	       switch (xxs) { \
153 		      case 2: \
154 			  delta += 1000000; \
155 			      /* FALLTHROUGH */ \
156 		      case 1: \
157 			  delta += 1000000; \
158 			  break; \
159 		      default: \
160 			  delta += (1000000 * xxs); \
161 	       } \
162 	    } \
163 } while (0)
164 
165 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
166 	      (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
167 
168 int get_sg6_cnt(struct sioc_sg_req6 *, unsigned int);
169 int get_mif6_cnt(struct sioc_mif_req6 *, unsigned int);
170 int ip6_mrouter_init(struct socket *, int, int);
171 int add_m6if(struct socket *, struct mif6ctl *);
172 int del_m6if(struct socket *, mifi_t *);
173 int add_m6fc(struct socket *, struct mf6cctl *);
174 int del_m6fc(struct socket *, struct mf6cctl *);
175 struct ifnet *mrt6_iflookupbymif(mifi_t, unsigned int);
176 struct rtentry *mf6c_find(struct ifnet *, struct in6_addr *,
177     struct in6_addr *, unsigned int);
178 struct rtentry *mrt6_mcast_add(struct ifnet *, struct sockaddr *,
179     struct sockaddr *);
180 void mrt6_mcast_del(struct rtentry *, unsigned int);
181 void mf6c_expire_route(struct rtentry *, struct rttimer *);
182 
183 /*
184  * Handle MRT setsockopt commands to modify the multicast routing tables.
185  */
186 int
187 ip6_mrouter_set(int cmd, struct socket *so, struct mbuf *m)
188 {
189 	struct inpcb	*inp = sotoinpcb(so);
190 
191 	if (cmd != MRT6_INIT && so != ip6_mrouter[inp->inp_rtableid])
192 		return (EPERM);
193 
194 	switch (cmd) {
195 	case MRT6_INIT:
196 		if (m == NULL || m->m_len < sizeof(int))
197 			return (EINVAL);
198 		return (ip6_mrouter_init(so, *mtod(m, int *), cmd));
199 	case MRT6_DONE:
200 		return (ip6_mrouter_done(so));
201 	case MRT6_ADD_MIF:
202 		if (m == NULL || m->m_len < sizeof(struct mif6ctl))
203 			return (EINVAL);
204 		return (add_m6if(so, mtod(m, struct mif6ctl *)));
205 	case MRT6_DEL_MIF:
206 		if (m == NULL || m->m_len < sizeof(mifi_t))
207 			return (EINVAL);
208 		return (del_m6if(so, mtod(m, mifi_t *)));
209 	case MRT6_ADD_MFC:
210 		if (m == NULL || m->m_len < sizeof(struct mf6cctl))
211 			return (EINVAL);
212 		return (add_m6fc(so, mtod(m, struct mf6cctl *)));
213 	case MRT6_DEL_MFC:
214 		if (m == NULL || m->m_len < sizeof(struct mf6cctl))
215 			return (EINVAL);
216 		return (del_m6fc(so, mtod(m,  struct mf6cctl *)));
217 	default:
218 		return (EOPNOTSUPP);
219 	}
220 }
221 
222 /*
223  * Handle MRT getsockopt commands
224  */
225 int
226 ip6_mrouter_get(int cmd, struct socket *so, struct mbuf *m)
227 {
228 	struct inpcb	*inp = sotoinpcb(so);
229 
230 	if (so != ip6_mrouter[inp->inp_rtableid])
231 		return (EPERM);
232 
233 	switch (cmd) {
234 	default:
235 		return EOPNOTSUPP;
236 	}
237 }
238 
239 /*
240  * Handle ioctl commands to obtain information from the cache
241  */
242 int
243 mrt6_ioctl(struct socket *so, u_long cmd, caddr_t data)
244 {
245 	struct inpcb *inp = sotoinpcb(so);
246 	int error;
247 
248 	if (inp == NULL)
249 		return (ENOTCONN);
250 
251 	switch (cmd) {
252 	case SIOCGETSGCNT_IN6:
253 		NET_RLOCK();
254 		error = get_sg6_cnt((struct sioc_sg_req6 *)data,
255 		    inp->inp_rtableid);
256 		NET_RUNLOCK();
257 		break;
258 	case SIOCGETMIFCNT_IN6:
259 		NET_RLOCK();
260 		error = get_mif6_cnt((struct sioc_mif_req6 *)data,
261 		    inp->inp_rtableid);
262 		NET_RUNLOCK();
263 		break;
264 	default:
265 		error = ENOTTY;
266 		break;
267 	}
268 	return error;
269 }
270 
271 /*
272  * returns the packet, byte, rpf-failure count for the source group provided
273  */
274 int
275 get_sg6_cnt(struct sioc_sg_req6 *req, unsigned int rtableid)
276 {
277 	struct rtentry *rt;
278 	struct mf6c *mf6c;
279 
280 	rt = mf6c_find(NULL, &req->src.sin6_addr, &req->grp.sin6_addr,
281 	    rtableid);
282 	if (rt == NULL) {
283 		req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff;
284 		return EADDRNOTAVAIL;
285 	}
286 
287 	req->pktcnt = req->bytecnt = req->wrong_if = 0;
288 	do {
289 		mf6c = (struct mf6c *)rt->rt_llinfo;
290 		if (mf6c == NULL)
291 			continue;
292 
293 		req->pktcnt += mf6c->mf6c_pkt_cnt;
294 		req->bytecnt += mf6c->mf6c_byte_cnt;
295 		req->wrong_if += mf6c->mf6c_wrong_if;
296 	} while ((rt = rtable_iterate(rt)) != NULL);
297 
298 	return 0;
299 }
300 
301 /*
302  * returns the input and output packet and byte counts on the mif provided
303  */
304 int
305 get_mif6_cnt(struct sioc_mif_req6 *req, unsigned int rtableid)
306 {
307 	struct ifnet *ifp;
308 	struct mif6 *m6;
309 
310 	if ((ifp = mrt6_iflookupbymif(req->mifi, rtableid)) == NULL)
311 		return EINVAL;
312 
313 	m6 = (struct mif6 *)ifp->if_mcast6;
314 	req->icount = m6->m6_pkt_in;
315 	req->ocount = m6->m6_pkt_out;
316 	req->ibytes = m6->m6_bytes_in;
317 	req->obytes = m6->m6_bytes_out;
318 
319 	return 0;
320 }
321 
322 int
323 mrt6_sysctl_mif(void *oldp, size_t *oldlenp)
324 {
325 	struct ifnet *ifp;
326 	caddr_t where = oldp;
327 	size_t needed, given;
328 	struct mif6 *mifp;
329 	struct mif6info minfo;
330 
331 	given = *oldlenp;
332 	needed = 0;
333 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
334 		if ((mifp = (struct mif6 *)ifp->if_mcast6) == NULL)
335 			continue;
336 
337 		minfo.m6_mifi = mifp->m6_mifi;
338 		minfo.m6_flags = mifp->m6_flags;
339 		minfo.m6_lcl_addr = mifp->m6_lcl_addr;
340 		minfo.m6_ifindex = ifp->if_index;
341 		minfo.m6_pkt_in = mifp->m6_pkt_in;
342 		minfo.m6_pkt_out = mifp->m6_pkt_out;
343 		minfo.m6_bytes_in = mifp->m6_bytes_in;
344 		minfo.m6_bytes_out = mifp->m6_bytes_out;
345 		minfo.m6_rate_limit = mifp->m6_rate_limit;
346 
347 		needed += sizeof(minfo);
348 		if (where && needed <= given) {
349 			int error;
350 
351 			error = copyout(&minfo, where, sizeof(minfo));
352 			if (error)
353 				return (error);
354 			where += sizeof(minfo);
355 		}
356 	}
357 	if (where) {
358 		*oldlenp = needed;
359 		if (given < needed)
360 			return (ENOMEM);
361 	} else
362 		*oldlenp = (11 * needed) / 10;
363 
364 	return (0);
365 }
366 
367 struct mf6csysctlarg {
368 	struct mf6cinfo	*ms6a_minfos;
369 	size_t		 ms6a_len;
370 	size_t		 ms6a_needed;
371 };
372 
373 int
374 mrt6_rtwalk_mf6csysctl(struct rtentry *rt, void *arg, unsigned int rtableid)
375 {
376 	struct mf6c		*mf6c = (struct mf6c *)rt->rt_llinfo;
377 	struct mf6csysctlarg	*msa = arg;
378 	struct ifnet		*ifp;
379 	struct mif6		*m6;
380 	struct mf6cinfo		*minfo;
381 	int			 new = 0;
382 
383 	/* Skip entries being removed. */
384 	if (mf6c == NULL)
385 		return 0;
386 
387 	/* Skip non-multicast routes. */
388 	if (ISSET(rt->rt_flags, RTF_HOST | RTF_MULTICAST) !=
389 	    (RTF_HOST | RTF_MULTICAST))
390 		return 0;
391 
392 	/* User just asked for the output size. */
393 	if (msa->ms6a_minfos == NULL) {
394 		msa->ms6a_needed += sizeof(*minfo);
395 		return 0;
396 	}
397 
398 	/* Skip route with invalid interfaces. */
399 	if ((ifp = if_get(rt->rt_ifidx)) == NULL)
400 		return 0;
401 	if ((m6 = (struct mif6 *)ifp->if_mcast6) == NULL) {
402 		if_put(ifp);
403 		return 0;
404 	}
405 
406 	for (minfo = msa->ms6a_minfos;
407 	     (uint8_t *)minfo < ((uint8_t *)msa->ms6a_minfos + msa->ms6a_len);
408 	     minfo++) {
409 		/* Find a new entry or update old entry. */
410 		if (!IN6_ARE_ADDR_EQUAL(&minfo->mf6c_origin.sin6_addr,
411 		    &satosin6(rt->rt_gateway)->sin6_addr) ||
412 		    !IN6_ARE_ADDR_EQUAL(&minfo->mf6c_mcastgrp.sin6_addr,
413 		    &satosin6(rt_key(rt))->sin6_addr)) {
414 			if (!IN6_IS_ADDR_UNSPECIFIED(
415 			    &minfo->mf6c_origin.sin6_addr) ||
416 			    !IN6_IS_ADDR_UNSPECIFIED(
417 			    &minfo->mf6c_mcastgrp.sin6_addr))
418 				continue;
419 
420 			new = 1;
421 		}
422 
423 		minfo->mf6c_origin = *satosin6(rt->rt_gateway);
424 		minfo->mf6c_mcastgrp = *satosin6(rt_key(rt));
425 		minfo->mf6c_parent = mf6c->mf6c_parent;
426 		minfo->mf6c_pkt_cnt += mf6c->mf6c_pkt_cnt;
427 		minfo->mf6c_byte_cnt += mf6c->mf6c_byte_cnt;
428 		IF_SET(m6->m6_mifi, &minfo->mf6c_ifset);
429 		break;
430 	}
431 
432 	if (new != 0)
433 		msa->ms6a_needed += sizeof(*minfo);
434 
435 	if_put(ifp);
436 
437 	return 0;
438 }
439 
440 int
441 mrt6_sysctl_mfc(void *oldp, size_t *oldlenp)
442 {
443 	unsigned int		 rtableid;
444 	int			 error;
445 	struct mf6csysctlarg	 msa;
446 
447 	if (oldp != NULL && *oldlenp > MAXPHYS)
448 		return EINVAL;
449 
450 	if (oldp != NULL)
451 		msa.ms6a_minfos = malloc(*oldlenp, M_TEMP, M_WAITOK | M_ZERO);
452 	else
453 		msa.ms6a_minfos = NULL;
454 
455 	msa.ms6a_len = *oldlenp;
456 	msa.ms6a_needed = 0;
457 
458 	for (rtableid = 0; rtableid <= RT_TABLEID_MAX; rtableid++) {
459 		rtable_walk(rtableid, AF_INET6, NULL, mrt6_rtwalk_mf6csysctl,
460 		    &msa);
461 	}
462 
463 	if (msa.ms6a_minfos != NULL && msa.ms6a_needed > 0 &&
464 	    (error = copyout(msa.ms6a_minfos, oldp, msa.ms6a_needed)) != 0) {
465 		free(msa.ms6a_minfos, M_TEMP, *oldlenp);
466 		return error;
467 	}
468 
469 	free(msa.ms6a_minfos, M_TEMP, *oldlenp);
470 	*oldlenp = msa.ms6a_needed;
471 
472 	return 0;
473 }
474 
475 /*
476  * Enable multicast routing
477  */
478 int
479 ip6_mrouter_init(struct socket *so, int v, int cmd)
480 {
481 	struct inpcb *inp = sotoinpcb(so);
482 	unsigned int rtableid = inp->inp_rtableid;
483 
484 	if (so->so_type != SOCK_RAW ||
485 	    so->so_proto->pr_protocol != IPPROTO_ICMPV6)
486 		return (EOPNOTSUPP);
487 
488 	if (v != 1)
489 		return (ENOPROTOOPT);
490 
491 	if (ip6_mrouter[rtableid] != NULL)
492 		return (EADDRINUSE);
493 
494 	ip6_mrouter[rtableid] = so;
495 	ip6_mrouter_ver = cmd;
496 	mrouter6q[rtableid] = rt_timer_queue_create(MCAST_EXPIRE_TIMEOUT);
497 
498 	return (0);
499 }
500 
501 int
502 mrouter6_rtwalk_delete(struct rtentry *rt, void *arg, unsigned int rtableid)
503 {
504 	/* Skip non-multicast routes. */
505 	if (ISSET(rt->rt_flags, RTF_HOST | RTF_MULTICAST) !=
506 	    (RTF_HOST | RTF_MULTICAST))
507 		return 0;
508 
509 	return EEXIST;
510 }
511 
512 /*
513  * Disable multicast routing
514  */
515 int
516 ip6_mrouter_done(struct socket *so)
517 {
518 	struct inpcb *inp = sotoinpcb(so);
519 	struct ifnet *ifp;
520 	unsigned int rtableid = inp->inp_rtableid;
521 	int error;
522 
523 	NET_ASSERT_LOCKED();
524 
525 	/* Delete all remaining installed multicast routes. */
526 	do {
527 		struct rtentry *rt = NULL;
528 
529 		error = rtable_walk(rtableid, AF_INET6, &rt,
530 		    mrouter6_rtwalk_delete, NULL);
531 		if (rt != NULL && error == EEXIST) {
532 			mrt6_mcast_del(rt, rtableid);
533 			error = EAGAIN;
534 		}
535 		rtfree(rt);
536 	} while (error == EAGAIN);
537 
538 	/* Unregister all interfaces in the domain. */
539 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
540 		if (ifp->if_rdomain != rtableid)
541 			continue;
542 
543 		ip6_mrouter_detach(ifp);
544 	}
545 
546 	rt_timer_queue_destroy(mrouter6q[rtableid]);
547 	ip6_mrouter[inp->inp_rtableid] = NULL;
548 	ip6_mrouter_ver = 0;
549 	mrouter6q[rtableid] = NULL;
550 
551 	return 0;
552 }
553 
554 void
555 ip6_mrouter_detach(struct ifnet *ifp)
556 {
557 	struct mif6 *m6 = (struct mif6 *)ifp->if_mcast6;
558 	struct in6_ifreq ifr;
559 
560 	if (m6 == NULL)
561 		return;
562 
563 	ifp->if_mcast6 = NULL;
564 
565 	memset(&ifr, 0, sizeof(ifr));
566 	ifr.ifr_addr.sin6_family = AF_INET6;
567 	ifr.ifr_addr.sin6_addr = in6addr_any;
568 	(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr);
569 
570 	free(m6, M_MRTABLE, sizeof(*m6));
571 }
572 
573 /*
574  * Add a mif to the mif table
575  */
576 int
577 add_m6if(struct socket *so, struct mif6ctl *mifcp)
578 {
579 	struct inpcb *inp = sotoinpcb(so);
580 	struct mif6 *mifp;
581 	struct ifnet *ifp;
582 	struct in6_ifreq ifr;
583 	int error;
584 	unsigned int rtableid = inp->inp_rtableid;
585 
586 	NET_ASSERT_LOCKED();
587 
588 	if (mifcp->mif6c_mifi >= MAXMIFS)
589 		return EINVAL;
590 
591 	if (mrt6_iflookupbymif(mifcp->mif6c_mifi, rtableid) != NULL)
592 		return EADDRINUSE; /* XXX: is it appropriate? */
593 
594 	{
595 		ifp = if_get(mifcp->mif6c_pifi);
596 		if (ifp == NULL)
597 			return ENXIO;
598 
599 		/* Make sure the interface supports multicast */
600 		if ((ifp->if_flags & IFF_MULTICAST) == 0) {
601 			if_put(ifp);
602 			return EOPNOTSUPP;
603 		}
604 
605 		/*
606 		 * Enable promiscuous reception of all IPv6 multicasts
607 		 * from the interface.
608 		 */
609 		memset(&ifr, 0, sizeof(ifr));
610 		ifr.ifr_addr.sin6_family = AF_INET6;
611 		ifr.ifr_addr.sin6_addr = in6addr_any;
612 		error = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, (caddr_t)&ifr);
613 
614 		if (error) {
615 			if_put(ifp);
616 			return error;
617 		}
618 	}
619 
620 	mifp = malloc(sizeof(*mifp), M_MRTABLE, M_WAITOK | M_ZERO);
621 	ifp->if_mcast6	   = (caddr_t)mifp;
622 	mifp->m6_mifi	   = mifcp->mif6c_mifi;
623 	mifp->m6_flags     = mifcp->mif6c_flags;
624 #ifdef notyet
625 	/* scaling up here allows division by 1024 in critical code */
626 	mifp->m6_rate_limit = mifcp->mif6c_rate_limit * 1024 / 1000;
627 #endif
628 
629 	if_put(ifp);
630 
631 	return 0;
632 }
633 
634 /*
635  * Delete a mif from the mif table
636  */
637 int
638 del_m6if(struct socket *so, mifi_t *mifip)
639 {
640 	struct inpcb *inp = sotoinpcb(so);
641 	struct ifnet *ifp;
642 
643 	NET_ASSERT_LOCKED();
644 
645 	if (*mifip >= MAXMIFS)
646 		return EINVAL;
647 	if ((ifp = mrt6_iflookupbymif(*mifip, inp->inp_rtableid)) == NULL)
648 		return EINVAL;
649 
650 	ip6_mrouter_detach(ifp);
651 
652 	return 0;
653 }
654 
655 int
656 mf6c_add_route(struct ifnet *ifp, struct sockaddr *origin,
657     struct sockaddr *group, struct mf6cctl *mf6cc, int wait)
658 {
659 	struct rtentry *rt;
660 	struct mf6c *mf6c;
661 	unsigned int rtableid = ifp->if_rdomain;
662 #ifdef MCAST_DEBUG
663 	char bsrc[INET6_ADDRSTRLEN], bdst[INET6_ADDRSTRLEN];
664 #endif /* MCAST_DEBUG */
665 
666 	rt = mrt6_mcast_add(ifp, origin, group);
667 	if (rt == NULL)
668 		return ENOENT;
669 
670 	mf6c = malloc(sizeof(*mf6c), M_MRTABLE, wait | M_ZERO);
671 	if (mf6c == NULL) {
672 		DPRINTF("origin %s group %s parent %d (%s) malloc failed",
673 		    inet_ntop(AF_INET6, origin, bsrc, sizeof(bsrc)),
674 		    inet_ntop(AF_INET6, group, bdst, sizeof(bdst)),
675 		    mf6cc->mf6cc_parent, ifp->if_xname);
676 		mrt6_mcast_del(rt, rtableid);
677 		rtfree(rt);
678 		return ENOMEM;
679 	}
680 
681 	rt->rt_llinfo = (caddr_t)mf6c;
682 	rt_timer_add(rt, mf6c_expire_route, mrouter6q[rtableid], rtableid);
683 	mf6c->mf6c_parent = mf6cc->mf6cc_parent;
684 	rtfree(rt);
685 
686 	return 0;
687 }
688 
689 void
690 mf6c_update(struct mf6cctl *mf6cc, int wait, unsigned int rtableid)
691 {
692 	struct rtentry *rt;
693 	struct mf6c *mf6c;
694 	struct ifnet *ifp;
695 	struct sockaddr_in6 osin6, gsin6;
696 	mifi_t mifi;
697 #ifdef MCAST_DEBUG
698 	char bdst[INET6_ADDRSTRLEN];
699 #endif /* MCAST_DEBUG */
700 
701 	memset(&osin6, 0, sizeof(osin6));
702 	osin6.sin6_family = AF_INET6;
703 	osin6.sin6_len = sizeof(osin6);
704 	osin6.sin6_addr = mf6cc->mf6cc_origin.sin6_addr;
705 
706 	memset(&gsin6, 0, sizeof(gsin6));
707 	gsin6.sin6_family = AF_INET6;
708 	gsin6.sin6_len = sizeof(gsin6);
709 	gsin6.sin6_addr = mf6cc->mf6cc_mcastgrp.sin6_addr;
710 
711 	for (mifi = 0; mifi < MAXMIFS; mifi++) {
712 		if (mifi == mf6cc->mf6cc_parent)
713 			continue;
714 
715 		/* Test for mif existence and then update the entry. */
716 		if ((ifp = mrt6_iflookupbymif(mifi, rtableid)) == NULL)
717 			continue;
718 
719 		rt = mf6c_find(ifp, &mf6cc->mf6cc_origin.sin6_addr,
720 		    &mf6cc->mf6cc_mcastgrp.sin6_addr, rtableid);
721 
722 		/* mif not configured or removed. */
723 		if (!IF_ISSET(mifi, &mf6cc->mf6cc_ifset)) {
724 			/* Route doesn't exist, nothing to do. */
725 			if (rt == NULL)
726 				continue;
727 
728 			DPRINTF("del route (group %s) for mif %d (%s)",
729 			    inet_ntop(AF_INET6,
730 			    &mf6cc->mf6cc_mcastgrp.sin6_addr, bdst,
731 			    sizeof(bdst)), mifi, ifp->if_xname);
732 			mrt6_mcast_del(rt, rtableid);
733 			rtfree(rt);
734 			continue;
735 		}
736 
737 		/* Route exists, look for changes. */
738 		if (rt != NULL) {
739 			mf6c = (struct mf6c *)rt->rt_llinfo;
740 			/* Skip route being deleted. */
741 			if (mf6c == NULL) {
742 				rtfree(rt);
743 				continue;
744 			}
745 
746 			/* No new changes to apply. */
747 			if (mf6cc->mf6cc_parent == mf6c->mf6c_parent) {
748 				rtfree(rt);
749 				continue;
750 			}
751 
752 			DPRINTF("update route (group %s) for mif %d (%s)",
753 			    inet_ntop(AF_INET6,
754 			    &mf6cc->mf6cc_mcastgrp.sin6_addr, bdst,
755 			    sizeof(bdst)), mifi, ifp->if_xname);
756 
757 			mf6c->mf6c_parent = mf6cc->mf6cc_parent;
758 			rtfree(rt);
759 			continue;
760 		}
761 
762 		DPRINTF("add route (group %s) for mif %d (%s)",
763 		    inet_ntop(AF_INET6, &mf6cc->mf6cc_mcastgrp.sin6_addr,
764 		    bdst, sizeof(bdst)), mifi, ifp->if_xname);
765 
766 		mf6c_add_route(ifp, sin6tosa(&osin6), sin6tosa(&gsin6),
767 		    mf6cc, wait);
768 	}
769 
770 	/* Create route for the parent interface. */
771 	if ((ifp = mrt6_iflookupbymif(mf6cc->mf6cc_parent,
772 	    rtableid)) == NULL) {
773 		DPRINTF("failed to find upstream interface %d",
774 		    mf6cc->mf6cc_parent);
775 		return;
776 	}
777 
778 	/* We already have a route, nothing to do here. */
779 	if ((rt = mf6c_find(ifp, &mf6cc->mf6cc_origin.sin6_addr,
780 	    &mf6cc->mf6cc_mcastgrp.sin6_addr, rtableid)) != NULL) {
781 		rtfree(rt);
782 		return;
783 	}
784 
785 	DPRINTF("add upstream route (group %s) for if %s",
786 	    inet_ntop(AF_INET6, &mf6cc->mf6cc_mcastgrp.sin6_addr,
787 	    bdst, sizeof(bdst)), ifp->if_xname);
788 	mf6c_add_route(ifp, sin6tosa(&osin6), sin6tosa(&gsin6), mf6cc, wait);
789 }
790 
791 int
792 mf6c_add(struct mf6cctl *mfccp, struct in6_addr *origin,
793     struct in6_addr *group, int vidx, unsigned int rtableid, int wait)
794 {
795 	struct ifnet *ifp;
796 	struct mif6 *m6;
797 	struct mf6cctl mf6cc;
798 
799 	ifp = mrt6_iflookupbymif(vidx, rtableid);
800 	if (ifp == NULL ||
801 	    (m6 = (struct mif6 *)ifp->if_mcast6) == NULL)
802 		return ENOENT;
803 
804 	memset(&mf6cc, 0, sizeof(mf6cc));
805 	if (mfccp == NULL) {
806 		mf6cc.mf6cc_origin.sin6_family = AF_INET6;
807 		mf6cc.mf6cc_origin.sin6_len = sizeof(mf6cc.mf6cc_origin);
808 		mf6cc.mf6cc_origin.sin6_addr = *origin;
809 		mf6cc.mf6cc_mcastgrp.sin6_family = AF_INET6;
810 		mf6cc.mf6cc_mcastgrp.sin6_len = sizeof(mf6cc.mf6cc_mcastgrp);
811 		mf6cc.mf6cc_mcastgrp.sin6_addr = *group;
812 		mf6cc.mf6cc_parent = vidx;
813 	} else
814 		memcpy(&mf6cc, mfccp, sizeof(mf6cc));
815 
816 	mf6c_update(&mf6cc, wait, rtableid);
817 
818 	return 0;
819 }
820 
821 int
822 add_m6fc(struct socket *so, struct mf6cctl *mfccp)
823 {
824 	struct inpcb *inp = sotoinpcb(so);
825 	unsigned int rtableid = inp->inp_rtableid;
826 
827 	NET_ASSERT_LOCKED();
828 
829 	return mf6c_add(mfccp, &mfccp->mf6cc_origin.sin6_addr,
830 	    &mfccp->mf6cc_mcastgrp.sin6_addr, mfccp->mf6cc_parent,
831 	    rtableid, M_WAITOK);
832 }
833 
834 int
835 del_m6fc(struct socket *so, struct mf6cctl *mfccp)
836 {
837 	struct inpcb *inp = sotoinpcb(so);
838 	struct rtentry *rt;
839 	unsigned int rtableid = inp->inp_rtableid;
840 
841 	NET_ASSERT_LOCKED();
842 
843 	while ((rt = mf6c_find(NULL, &mfccp->mf6cc_origin.sin6_addr,
844 	    &mfccp->mf6cc_mcastgrp.sin6_addr, rtableid)) != NULL) {
845 		mrt6_mcast_del(rt, rtableid);
846 		rtfree(rt);
847 	}
848 
849 	return 0;
850 }
851 
852 int
853 socket6_send(struct socket *s, struct mbuf *mm, struct sockaddr_in6 *src)
854 {
855 	if (s) {
856 		if (sbappendaddr(s, &s->so_rcv, sin6tosa(src), mm, NULL) != 0) {
857 			sorwakeup(s);
858 			return 0;
859 		}
860 	}
861 	m_freem(mm);
862 	return -1;
863 }
864 
865 /*
866  * IPv6 multicast forwarding function. This function assumes that the packet
867  * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
868  * pointed to by "ifp", and the packet is to be relayed to other networks
869  * that have members of the packet's destination IPv6 multicast group.
870  *
871  * The packet is returned unscathed to the caller, unless it is
872  * erroneous, in which case a non-zero return value tells the caller to
873  * discard it.
874  */
875 int
876 ip6_mforward(struct ip6_hdr *ip6, struct ifnet *ifp, struct mbuf *m)
877 {
878 	struct rtentry *rt;
879 	struct mif6 *mifp;
880 	struct mbuf *mm;
881 	struct sockaddr_in6 sin6;
882 	unsigned int rtableid = ifp->if_rdomain;
883 
884 	NET_ASSERT_LOCKED();
885 
886 	/*
887 	 * Don't forward a packet with Hop limit of zero or one,
888 	 * or a packet destined to a local-only group.
889 	 */
890 	if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) ||
891 	    IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
892 		return 0;
893 	ip6->ip6_hlim--;
894 
895 	/*
896 	 * Source address check: do not forward packets with unspecified
897 	 * source. It was discussed in July 2000, on ipngwg mailing list.
898 	 * This is rather more serious than unicast cases, because some
899 	 * MLD packets can be sent with the unspecified source address
900 	 * (although such packets must normally set 1 to the hop limit field).
901 	 */
902 	if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
903 		ip6stat_inc(ip6s_cantforward);
904 		if (ip6_log_time + ip6_log_interval < time_uptime) {
905 			char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];
906 
907 			ip6_log_time = time_uptime;
908 
909 			inet_ntop(AF_INET6, &ip6->ip6_src, src, sizeof(src));
910 			inet_ntop(AF_INET6, &ip6->ip6_dst, dst, sizeof(dst));
911 			log(LOG_DEBUG, "cannot forward "
912 			    "from %s to %s nxt %d received on interface %u\n",
913 			    src, dst, ip6->ip6_nxt, m->m_pkthdr.ph_ifidx);
914 		}
915 		return 0;
916 	}
917 
918 	/*
919 	 * Determine forwarding mifs from the forwarding cache table
920 	 */
921 	rt = mf6c_find(NULL, &ip6->ip6_src, &ip6->ip6_dst, rtableid);
922 
923 	/* Entry exists, so forward if necessary */
924 	if (rt) {
925 		return (ip6_mdq(m, ifp, rt));
926 	} else {
927 		/*
928 		 * If we don't have a route for packet's origin,
929 		 * Make a copy of the packet &
930 		 * send message to routing daemon
931 		 */
932 
933 		mrt6stat.mrt6s_no_route++;
934 
935 		{
936 			struct mrt6msg *im;
937 
938 			if ((mifp = (struct mif6 *)ifp->if_mcast6) == NULL)
939 				return EHOSTUNREACH;
940 
941 			/*
942 			 * Make a copy of the header to send to the user
943 			 * level process
944 			 */
945 			mm = m_copym(m, 0, sizeof(struct ip6_hdr), M_NOWAIT);
946 			if (mm == NULL)
947 				return ENOBUFS;
948 
949 			/*
950 			 * Send message to routing daemon
951 			 */
952 			(void)memset(&sin6, 0, sizeof(sin6));
953 			sin6.sin6_len = sizeof(sin6);
954 			sin6.sin6_family = AF_INET6;
955 			sin6.sin6_addr = ip6->ip6_src;
956 
957 			im = NULL;
958 			switch (ip6_mrouter_ver) {
959 			case MRT6_INIT:
960 				im = mtod(mm, struct mrt6msg *);
961 				im->im6_msgtype = MRT6MSG_NOCACHE;
962 				im->im6_mbz = 0;
963 				im->im6_mif = mifp->m6_mifi;
964 				break;
965 			default:
966 				m_freem(mm);
967 				return EINVAL;
968 			}
969 
970 			if (socket6_send(ip6_mrouter[rtableid], mm,
971 			    &sin6) < 0) {
972 				log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
973 				    "socket queue full\n");
974 				mrt6stat.mrt6s_upq_sockfull++;
975 				return ENOBUFS;
976 			}
977 
978 			mrt6stat.mrt6s_upcalls++;
979 
980 			mf6c_add(NULL, &ip6->ip6_src, &ip6->ip6_dst,
981 			    mifp->m6_mifi, rtableid, M_NOWAIT);
982 		}
983 
984 		return 0;
985 	}
986 }
987 
988 void
989 mf6c_expire_route(struct rtentry *rt, struct rttimer *rtt)
990 {
991 	struct mf6c *mf6c = (struct mf6c *)rt->rt_llinfo;
992 	unsigned int rtableid = rtt->rtt_tableid;
993 #ifdef MCAST_DEBUG
994 	char bsrc[INET6_ADDRSTRLEN], bdst[INET6_ADDRSTRLEN];
995 #endif /* MCAST_DEBUG */
996 
997 	/* Skip entry being deleted. */
998 	if (mf6c == NULL)
999 		return;
1000 
1001 	DPRINTF("origin %s group %s interface %d expire %s",
1002 	    inet_ntop(AF_INET6, &satosin6(rt->rt_gateway)->sin6_addr,
1003 	    bsrc, sizeof(bsrc)),
1004 	    inet_ntop(AF_INET6, &satosin6(rt_key(rt))->sin6_addr,
1005 	    bdst, sizeof(bdst)), rt->rt_ifidx,
1006 	    mf6c->mf6c_expire ? "yes" : "no");
1007 
1008 	if (mf6c->mf6c_expire == 0) {
1009 		mf6c->mf6c_expire = 1;
1010 		rt_timer_add(rt, mf6c_expire_route, mrouter6q[rtableid],
1011 		    rtableid);
1012 		return;
1013 	}
1014 
1015 	mrt6_mcast_del(rt, rtableid);
1016 }
1017 
1018 /*
1019  * Packet forwarding routine once entry in the cache is made
1020  */
1021 int
1022 ip6_mdq(struct mbuf *m, struct ifnet *ifp, struct rtentry *rt)
1023 {
1024 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1025 	struct mif6 *m6, *mifp = (struct mif6 *)ifp->if_mcast6;
1026 	struct mf6c *mf6c = (struct mf6c *)rt->rt_llinfo;
1027 	struct ifnet *ifn;
1028 	int plen = m->m_pkthdr.len;
1029 
1030 	if (mifp == NULL || mf6c == NULL) {
1031 		rtfree(rt);
1032 		return EHOSTUNREACH;
1033 	}
1034 
1035 	/*
1036 	 * Don't forward if it didn't arrive from the parent mif
1037 	 * for its origin.
1038 	 */
1039 	if (mifp->m6_mifi != mf6c->mf6c_parent) {
1040 		/* came in the wrong interface */
1041 		mrt6stat.mrt6s_wrong_if++;
1042 		mf6c->mf6c_wrong_if++;
1043 		rtfree(rt);
1044 		return 0;
1045 	}			/* if wrong iif */
1046 
1047 	/* If I sourced this packet, it counts as output, else it was input. */
1048 	if (m->m_pkthdr.ph_ifidx == 0) {
1049 		/* XXX: is ph_ifidx really 0 when output?? */
1050 		mifp->m6_pkt_out++;
1051 		mifp->m6_bytes_out += plen;
1052 	} else {
1053 		mifp->m6_pkt_in++;
1054 		mifp->m6_bytes_in += plen;
1055 	}
1056 
1057 	/*
1058 	 * For each mif, forward a copy of the packet if there are group
1059 	 * members downstream on the interface.
1060 	 */
1061 	do {
1062 		/* Don't consider non multicast routes. */
1063 		if (ISSET(rt->rt_flags, RTF_HOST | RTF_MULTICAST) !=
1064 		    (RTF_HOST | RTF_MULTICAST))
1065 			continue;
1066 
1067 		mf6c = (struct mf6c *)rt->rt_llinfo;
1068 		if (mf6c == NULL)
1069 			continue;
1070 
1071 		mf6c->mf6c_pkt_cnt++;
1072 		mf6c->mf6c_byte_cnt += m->m_pkthdr.len;
1073 
1074 		/* Don't let this route expire. */
1075 		mf6c->mf6c_expire = 0;
1076 
1077 		if ((ifn = if_get(rt->rt_ifidx)) == NULL)
1078 			continue;
1079 
1080 		/* Sanity check: did we configure this? */
1081 		if ((m6 = (struct mif6 *)ifn->if_mcast6) == NULL) {
1082 			if_put(ifn);
1083 			continue;
1084 		}
1085 
1086 		/* Don't send in the upstream interface. */
1087 		if (mf6c->mf6c_parent == m6->m6_mifi) {
1088 			if_put(ifn);
1089 			continue;
1090 		}
1091 
1092 		/*
1093 		 * check if the outgoing packet is going to break
1094 		 * a scope boundary.
1095 		 */
1096 		if ((mifp->m6_flags & MIFF_REGISTER) == 0 &&
1097 		    (m6->m6_flags & MIFF_REGISTER) == 0 &&
1098 		    (in6_addr2scopeid(ifp->if_index, &ip6->ip6_dst) !=
1099 		    in6_addr2scopeid(ifn->if_index, &ip6->ip6_dst) ||
1100 		    in6_addr2scopeid(ifp->if_index, &ip6->ip6_src) !=
1101 		    in6_addr2scopeid(ifn->if_index, &ip6->ip6_src))) {
1102 			if_put(ifn);
1103 			ip6stat_inc(ip6s_badscope);
1104 			continue;
1105 		}
1106 
1107 		m6->m6_pkt_out++;
1108 		m6->m6_bytes_out += plen;
1109 
1110 		phyint_send6(ifn, ip6, m);
1111 		if_put(ifn);
1112 	} while ((rt = rtable_iterate(rt)) != NULL);
1113 
1114 	return 0;
1115 }
1116 
1117 void
1118 phyint_send6(struct ifnet *ifp, struct ip6_hdr *ip6, struct mbuf *m)
1119 {
1120 	struct mbuf *mb_copy;
1121 	struct sockaddr_in6 *dst6, sin6;
1122 	int error = 0;
1123 
1124 	NET_ASSERT_LOCKED();
1125 
1126 	/*
1127 	 * Make a new reference to the packet; make sure that
1128 	 * the IPv6 header is actually copied, not just referenced,
1129 	 * so that ip6_output() only scribbles on the copy.
1130 	 */
1131 	mb_copy = m_dup_pkt(m, max_linkhdr, M_NOWAIT);
1132 	if (mb_copy == NULL)
1133 		return;
1134 	/* set MCAST flag to the outgoing packet */
1135 	mb_copy->m_flags |= M_MCAST;
1136 
1137 	/*
1138 	 * If we sourced the packet, call ip6_output since we may devide
1139 	 * the packet into fragments when the packet is too big for the
1140 	 * outgoing interface.
1141 	 * Otherwise, we can simply send the packet to the interface
1142 	 * sending queue.
1143 	 */
1144 	if (m->m_pkthdr.ph_ifidx == 0) {
1145 		struct ip6_moptions im6o;
1146 
1147 		im6o.im6o_ifidx = ifp->if_index;
1148 		/* XXX: ip6_output will override ip6->ip6_hlim */
1149 		im6o.im6o_hlim = ip6->ip6_hlim;
1150 		im6o.im6o_loop = 1;
1151 		error = ip6_output(mb_copy, NULL, NULL, IPV6_FORWARDING, &im6o,
1152 		    NULL);
1153 		return;
1154 	}
1155 
1156 	/*
1157 	 * If we belong to the destination multicast group
1158 	 * on the outgoing interface, loop back a copy.
1159 	 */
1160 	dst6 = &sin6;
1161 	memset(&sin6, 0, sizeof(sin6));
1162 	if (in6_hasmulti(&ip6->ip6_dst, ifp)) {
1163 		dst6->sin6_len = sizeof(struct sockaddr_in6);
1164 		dst6->sin6_family = AF_INET6;
1165 		dst6->sin6_addr = ip6->ip6_dst;
1166 		ip6_mloopback(ifp, m, dst6);
1167 	}
1168 	/*
1169 	 * Put the packet into the sending queue of the outgoing interface
1170 	 * if it would fit in the MTU of the interface.
1171 	 */
1172 	if (mb_copy->m_pkthdr.len <= ifp->if_mtu || ifp->if_mtu < IPV6_MMTU) {
1173 		dst6->sin6_len = sizeof(struct sockaddr_in6);
1174 		dst6->sin6_family = AF_INET6;
1175 		dst6->sin6_addr = ip6->ip6_dst;
1176 		error = ifp->if_output(ifp, mb_copy, sin6tosa(dst6), NULL);
1177 	} else {
1178 		if (ip6_mcast_pmtu)
1179 			icmp6_error(mb_copy, ICMP6_PACKET_TOO_BIG, 0,
1180 			    ifp->if_mtu);
1181 		else {
1182 			m_freem(mb_copy); /* simply discard the packet */
1183 		}
1184 	}
1185 }
1186 
1187 struct ifnet *
1188 mrt6_iflookupbymif(mifi_t mifi, unsigned int rtableid)
1189 {
1190 	struct mif6	*m6;
1191 	struct ifnet	*ifp;
1192 
1193 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1194 		if (ifp->if_rdomain != rtableid)
1195 			continue;
1196 		if ((m6 = (struct mif6 *)ifp->if_mcast6) == NULL)
1197 			continue;
1198 		if (m6->m6_mifi != mifi)
1199 			continue;
1200 
1201 		return ifp;
1202 	}
1203 
1204 	return NULL;
1205 }
1206 
1207 struct rtentry *
1208 mf6c_find(struct ifnet *ifp, struct in6_addr *origin, struct in6_addr *group,
1209     unsigned int rtableid)
1210 {
1211 	struct rtentry *rt;
1212 	struct sockaddr_in6 msin6;
1213 
1214 	memset(&msin6, 0, sizeof(msin6));
1215 	msin6.sin6_family = AF_INET6;
1216 	msin6.sin6_len = sizeof(msin6);
1217 	msin6.sin6_addr = *group;
1218 
1219 	rt = rtalloc(sin6tosa(&msin6), 0, rtableid);
1220 	do {
1221 		if (!rtisvalid(rt)) {
1222 			rtfree(rt);
1223 			return NULL;
1224 		}
1225 		if (ISSET(rt->rt_flags, RTF_HOST | RTF_MULTICAST) !=
1226 		    (RTF_HOST | RTF_MULTICAST))
1227 			continue;
1228 		/* Return first occurrence if interface is not specified. */
1229 		if (ifp == NULL)
1230 			return rt;
1231 		if (rt->rt_ifidx == ifp->if_index)
1232 			return rt;
1233 	} while ((rt = rtable_iterate(rt)) != NULL);
1234 
1235 	return NULL;
1236 }
1237 
1238 struct rtentry *
1239 mrt6_mcast_add(struct ifnet *ifp, struct sockaddr *origin,
1240     struct sockaddr *group)
1241 {
1242 	struct ifaddr *ifa;
1243 	int rv;
1244 	unsigned int rtableid = ifp->if_rdomain;
1245 
1246 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1247 		if (ifa->ifa_addr->sa_family == AF_INET6)
1248 			break;
1249 	}
1250 	if (ifa == NULL) {
1251 		DPRINTF("ifa == NULL");
1252 		return NULL;
1253 	}
1254 
1255 	rv = rt_ifa_add(ifa, RTF_HOST | RTF_MULTICAST | RTF_MPATH, group,
1256 	    ifp->if_rdomain);
1257 	if (rv != 0) {
1258 		DPRINTF("rt_ifa_add failed %d", rv);
1259 		return NULL;
1260 	}
1261 
1262 	return mf6c_find(ifp, NULL, &satosin6(group)->sin6_addr, rtableid);
1263 }
1264 
1265 void
1266 mrt6_mcast_del(struct rtentry *rt, unsigned int rtableid)
1267 {
1268 	struct ifnet *ifp;
1269 	int error;
1270 
1271 	/* Remove all timers related to this route. */
1272 	rt_timer_remove_all(rt);
1273 
1274 	free(rt->rt_llinfo, M_MRTABLE, sizeof(struct mf6c));
1275 	rt->rt_llinfo = NULL;
1276 
1277 	ifp = if_get(rt->rt_ifidx);
1278 	if (ifp == NULL)
1279 		return;
1280 	error = rtdeletemsg(rt, ifp, rtableid);
1281 	if_put(ifp);
1282 
1283 	if (error)
1284 		DPRINTF("delete route error %d\n", error);
1285 }
1286