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