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