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