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