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