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