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 *
66 * @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
67 * BSDI ip_mroute.c,v 2.10 1996/11/14 00:29:52 jch Exp
68 */
69
70 /*
71 * IP multicast forwarding procedures
72 *
73 * Written by David Waitzman, BBN Labs, August 1988.
74 * Modified by Steve Deering, Stanford, February 1989.
75 * Modified by Mark J. Steiglitz, Stanford, May, 1991
76 * Modified by Van Jacobson, LBL, January 1993
77 * Modified by Ajit Thyagarajan, PARC, August 1993
78 * Modified by Bill Fenner, PARC, April 1994
79 *
80 * MROUTING Revision: 3.5.1.2 + PIM-SMv2 (pimd) Support
81 */
82
83 #include <sys/cdefs.h>
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/sdt.h>
97 #include <sys/signalvar.h>
98 #include <sys/socket.h>
99 #include <sys/socketvar.h>
100 #include <sys/sockio.h>
101 #include <sys/sx.h>
102 #include <sys/sysctl.h>
103 #include <sys/syslog.h>
104 #include <sys/systm.h>
105 #include <sys/time.h>
106
107 #include <net/if.h>
108 #include <net/if_var.h>
109 #include <net/if_private.h>
110 #include <net/if_types.h>
111 #include <net/vnet.h>
112
113 #include <netinet/in.h>
114 #include <netinet/in_var.h>
115 #include <netinet/icmp6.h>
116 #include <netinet/ip_encap.h>
117
118 #include <netinet/ip6.h>
119 #include <netinet/in_kdtrace.h>
120 #include <netinet6/ip6_var.h>
121 #include <netinet6/scope6_var.h>
122 #include <netinet6/nd6.h>
123 #include <netinet6/ip6_mroute.h>
124 #include <netinet6/pim6.h>
125 #include <netinet6/pim6_var.h>
126
127 static MALLOC_DEFINE(M_MRTABLE6, "mf6c", "multicast forwarding cache entry");
128
129 static int ip6_mdq(struct mbuf *, struct ifnet *, struct mf6c *);
130 static void phyint_send(struct ip6_hdr *, struct mif6 *, struct mbuf *);
131 static int register_send(struct ip6_hdr *, struct mif6 *, struct mbuf *);
132 static int set_pim6(int *);
133 static int socket_send(struct socket *, struct mbuf *,
134 struct sockaddr_in6 *);
135
136 extern int in6_mcast_loop;
137 extern struct domain inet6domain;
138
139 static const struct encaptab *pim6_encap_cookie;
140 static int pim6_encapcheck(const struct mbuf *, int, int, void *);
141 static int pim6_input(struct mbuf *, int, int, void *);
142
143 static const struct encap_config ipv6_encap_cfg = {
144 .proto = IPPROTO_PIM,
145 .min_length = sizeof(struct ip6_hdr) + PIM_MINLEN,
146 .exact_match = 8,
147 .check = pim6_encapcheck,
148 .input = pim6_input
149 };
150
151 VNET_DEFINE_STATIC(int, ip6_mrouter_ver) = 0;
152 #define V_ip6_mrouter_ver VNET(ip6_mrouter_ver)
153
154 SYSCTL_DECL(_net_inet6);
155 SYSCTL_DECL(_net_inet6_ip6);
156 static SYSCTL_NODE(_net_inet6, IPPROTO_PIM, pim,
157 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
158 "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 sx mrouter6_mtx;
170 #define MROUTER6_LOCKPTR() (&mrouter6_mtx)
171 #define MROUTER6_LOCK() sx_xlock(MROUTER6_LOCKPTR())
172 #define MROUTER6_UNLOCK() sx_xunlock(MROUTER6_LOCKPTR())
173 #define MROUTER6_LOCK_ASSERT() sx_assert(MROUTER6_LOCKPTR(), SA_XLOCKED
174 #define MROUTER6_LOCK_INIT() sx_init(MROUTER6_LOCKPTR(), "mrouter6")
175 #define MROUTER6_LOCK_DESTROY() sx_destroy(MROUTER6_LOCKPTR())
176
177 static struct mf6c *mf6ctable[MF6CTBLSIZ];
178 SYSCTL_OPAQUE(_net_inet6_ip6, OID_AUTO, mf6ctable, CTLFLAG_RD,
179 &mf6ctable, sizeof(mf6ctable), "S,*mf6ctable[MF6CTBLSIZ]",
180 "IPv6 Multicast Forwarding Table (struct *mf6ctable[MF6CTBLSIZ], "
181 "netinet6/ip6_mroute.h)");
182
183 static struct mtx mfc6_mtx;
184 #define MFC6_LOCKPTR() (&mfc6_mtx)
185 #define MFC6_LOCK() mtx_lock(MFC6_LOCKPTR())
186 #define MFC6_UNLOCK() mtx_unlock(MFC6_LOCKPTR())
187 #define MFC6_LOCK_ASSERT() mtx_assert(MFC6_LOCKPTR(), MA_OWNED)
188 #define MFC6_LOCK_INIT() mtx_init(MFC6_LOCKPTR(), \
189 "IPv6 multicast forwarding cache", \
190 NULL, MTX_DEF)
191 #define MFC6_LOCK_DESTROY() mtx_destroy(MFC6_LOCKPTR())
192
193 static u_char n6expire[MF6CTBLSIZ];
194
195 static struct mif6 mif6table[MAXMIFS];
196 static int
sysctl_mif6table(SYSCTL_HANDLER_ARGS)197 sysctl_mif6table(SYSCTL_HANDLER_ARGS)
198 {
199 struct mif6_sctl *out;
200 int error;
201
202 out = malloc(sizeof(struct mif6_sctl) * MAXMIFS, M_TEMP,
203 M_WAITOK | M_ZERO);
204 for (int i = 0; i < MAXMIFS; i++) {
205 out[i].m6_flags = mif6table[i].m6_flags;
206 out[i].m6_rate_limit = mif6table[i].m6_rate_limit;
207 out[i].m6_lcl_addr = mif6table[i].m6_lcl_addr;
208 if (mif6table[i].m6_ifp != NULL)
209 out[i].m6_ifp = mif6table[i].m6_ifp->if_index;
210 else
211 out[i].m6_ifp = 0;
212 out[i].m6_pkt_in = mif6table[i].m6_pkt_in;
213 out[i].m6_pkt_out = mif6table[i].m6_pkt_out;
214 out[i].m6_bytes_in = mif6table[i].m6_bytes_in;
215 out[i].m6_bytes_out = mif6table[i].m6_bytes_out;
216 }
217 error = SYSCTL_OUT(req, out, sizeof(struct mif6_sctl) * MAXMIFS);
218 free(out, M_TEMP);
219 return (error);
220 }
221 SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, mif6table,
222 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
223 NULL, 0, sysctl_mif6table, "S,mif6_sctl[MAXMIFS]",
224 "IPv6 Multicast Interfaces (struct mif6_sctl[MAXMIFS], "
225 "netinet6/ip6_mroute.h)");
226
227 static struct mtx mif6_mtx;
228 #define MIF6_LOCKPTR() (&mif6_mtx)
229 #define MIF6_LOCK() mtx_lock(MIF6_LOCKPTR())
230 #define MIF6_UNLOCK() mtx_unlock(MIF6_LOCKPTR())
231 #define MIF6_LOCK_ASSERT() mtx_assert(MIF6_LOCKPTR(), MA_OWNED)
232 #define MIF6_LOCK_INIT() \
233 mtx_init(MIF6_LOCKPTR(), "IPv6 multicast interfaces", NULL, MTX_DEF)
234 #define MIF6_LOCK_DESTROY() mtx_destroy(MIF6_LOCKPTR())
235
236 #ifdef MRT6DEBUG
237 VNET_DEFINE_STATIC(u_int, mrt6debug) = 0; /* debug level */
238 #define V_mrt6debug VNET(mrt6debug)
239 #define DEBUG_MFC 0x02
240 #define DEBUG_FORWARD 0x04
241 #define DEBUG_EXPIRE 0x08
242 #define DEBUG_XMIT 0x10
243 #define DEBUG_REG 0x20
244 #define DEBUG_PIM 0x40
245 #define DEBUG_ERR 0x80
246 #define DEBUG_ANY 0x7f
247 #define MRT6_DLOG(m, fmt, ...) \
248 if (V_mrt6debug & (m)) \
249 log(((m) & DEBUG_ERR) ? LOG_ERR: LOG_DEBUG, \
250 "%s: " fmt "\n", __func__, ##__VA_ARGS__)
251 #else
252 #define MRT6_DLOG(m, fmt, ...)
253 #endif
254
255 static void expire_upcalls(void *);
256 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
257 #define UPCALL_EXPIRE 6 /* number of timeouts */
258
259 /*
260 * XXX TODO: maintain a count to if_allmulti() calls in struct ifnet.
261 */
262
263 /*
264 * 'Interfaces' associated with decapsulator (so we can tell
265 * packets that went through it from ones that get reflected
266 * by a broken gateway). Different from IPv4 register_if,
267 * these interfaces are linked into the system ifnet list,
268 * because per-interface IPv6 statistics are maintained in
269 * ifp->if_afdata. But it does not have any routes point
270 * to them. I.e., packets can't be sent this way. They
271 * only exist as a placeholder for multicast source
272 * verification.
273 */
274 static struct ifnet *multicast_register_if6;
275
276 #define ENCAP_HOPS 64
277
278 /*
279 * Private variables.
280 */
281 static mifi_t nummifs = 0;
282 static mifi_t reg_mif_num = (mifi_t)-1;
283
284 static struct pim6stat pim6stat;
285 SYSCTL_STRUCT(_net_inet6_pim, PIM6CTL_STATS, stats, CTLFLAG_RW,
286 &pim6stat, pim6stat,
287 "PIM Statistics (struct pim6stat, netinet6/pim6_var.h)");
288
289 #define PIM6STAT_INC(name) pim6stat.name += 1
290 VNET_DEFINE_STATIC(int, pim6);
291 #define V_pim6 VNET(pim6)
292
293 /*
294 * Hash function for a source, group entry
295 */
296 #define MF6CHASH(a, g) MF6CHASHMOD((a).s6_addr32[0] ^ (a).s6_addr32[1] ^ \
297 (a).s6_addr32[2] ^ (a).s6_addr32[3] ^ \
298 (g).s6_addr32[0] ^ (g).s6_addr32[1] ^ \
299 (g).s6_addr32[2] ^ (g).s6_addr32[3])
300
301 /*
302 * Find a route for a given origin IPv6 address and Multicast group address.
303 */
304 #define MF6CFIND(o, g, rt) do { \
305 struct mf6c *_rt = mf6ctable[MF6CHASH(o,g)]; \
306 rt = NULL; \
307 while (_rt) { \
308 if (IN6_ARE_ADDR_EQUAL(&_rt->mf6c_origin.sin6_addr, &(o)) && \
309 IN6_ARE_ADDR_EQUAL(&_rt->mf6c_mcastgrp.sin6_addr, &(g)) && \
310 (_rt->mf6c_stall == NULL)) { \
311 rt = _rt; \
312 break; \
313 } \
314 _rt = _rt->mf6c_next; \
315 } \
316 if (rt == NULL) { \
317 MRT6STAT_INC(mrt6s_mfc_misses); \
318 } \
319 } while (/*CONSTCOND*/ 0)
320
321 /*
322 * Macros to compute elapsed time efficiently
323 * Borrowed from Van Jacobson's scheduling code
324 * XXX: replace with timersub() ?
325 */
326 #define TV_DELTA(a, b, delta) do { \
327 int xxs; \
328 \
329 delta = (a).tv_usec - (b).tv_usec; \
330 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
331 switch (xxs) { \
332 case 2: \
333 delta += 1000000; \
334 /* FALLTHROUGH */ \
335 case 1: \
336 delta += 1000000; \
337 break; \
338 default: \
339 delta += (1000000 * xxs); \
340 } \
341 } \
342 } while (/*CONSTCOND*/ 0)
343
344 /* XXX: replace with timercmp(a, b, <) ? */
345 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
346 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
347
348 #ifdef UPCALL_TIMING
349 #define UPCALL_MAX 50
350 static u_long upcall_data[UPCALL_MAX + 1];
351 static void collate();
352 #endif /* UPCALL_TIMING */
353
354 static int ip6_mrouter_init(struct socket *, int, int);
355 static int add_m6fc(struct mf6cctl *);
356 static int add_m6if(struct mif6ctl *);
357 static int del_m6fc(struct mf6cctl *);
358 static int del_m6if(mifi_t *);
359 static int del_m6if_locked(mifi_t *);
360 static int get_mif6_cnt(struct sioc_mif_req6 *);
361 static int get_sg_cnt(struct sioc_sg_req6 *);
362
363 static struct callout expire_upcalls_ch;
364
365 int X_ip6_mforward(struct ip6_hdr *, struct ifnet *, struct mbuf *);
366 int X_ip6_mrouter_done(void);
367 int X_ip6_mrouter_set(struct socket *, struct sockopt *);
368 int X_ip6_mrouter_get(struct socket *, struct sockopt *);
369 int X_mrt6_ioctl(u_long, caddr_t);
370
371 /*
372 * Handle MRT setsockopt commands to modify the multicast routing tables.
373 */
374 int
X_ip6_mrouter_set(struct socket * so,struct sockopt * sopt)375 X_ip6_mrouter_set(struct socket *so, struct sockopt *sopt)
376 {
377 int error = 0;
378 int optval;
379 struct mif6ctl mifc;
380 struct mf6cctl mfcc;
381 mifi_t mifi;
382
383 if (so != V_ip6_mrouter && sopt->sopt_name != MRT6_INIT)
384 return (EPERM);
385
386 switch (sopt->sopt_name) {
387 case MRT6_INIT:
388 #ifdef MRT6_OINIT
389 case MRT6_OINIT:
390 #endif
391 error = sooptcopyin(sopt, &optval, sizeof(optval),
392 sizeof(optval));
393 if (error)
394 break;
395 error = ip6_mrouter_init(so, optval, sopt->sopt_name);
396 break;
397 case MRT6_DONE:
398 error = X_ip6_mrouter_done();
399 break;
400 case MRT6_ADD_MIF:
401 error = sooptcopyin(sopt, &mifc, sizeof(mifc), sizeof(mifc));
402 if (error)
403 break;
404 error = add_m6if(&mifc);
405 break;
406 case MRT6_ADD_MFC:
407 error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
408 if (error)
409 break;
410 error = add_m6fc(&mfcc);
411 break;
412 case MRT6_DEL_MFC:
413 error = sooptcopyin(sopt, &mfcc, sizeof(mfcc), sizeof(mfcc));
414 if (error)
415 break;
416 error = del_m6fc(&mfcc);
417 break;
418 case MRT6_DEL_MIF:
419 error = sooptcopyin(sopt, &mifi, sizeof(mifi), sizeof(mifi));
420 if (error)
421 break;
422 error = del_m6if(&mifi);
423 break;
424 case MRT6_PIM:
425 error = sooptcopyin(sopt, &optval, sizeof(optval),
426 sizeof(optval));
427 if (error)
428 break;
429 error = set_pim6(&optval);
430 break;
431 default:
432 error = EOPNOTSUPP;
433 break;
434 }
435
436 return (error);
437 }
438
439 /*
440 * Handle MRT getsockopt commands
441 */
442 int
X_ip6_mrouter_get(struct socket * so,struct sockopt * sopt)443 X_ip6_mrouter_get(struct socket *so, struct sockopt *sopt)
444 {
445 int error = 0;
446
447 if (so != V_ip6_mrouter)
448 return (EACCES);
449
450 switch (sopt->sopt_name) {
451 case MRT6_PIM:
452 error = sooptcopyout(sopt, &V_pim6, sizeof(V_pim6));
453 break;
454 }
455 return (error);
456 }
457
458 /*
459 * Handle ioctl commands to obtain information from the cache
460 */
461 int
X_mrt6_ioctl(u_long cmd,caddr_t data)462 X_mrt6_ioctl(u_long cmd, caddr_t data)
463 {
464 int ret;
465
466 ret = EINVAL;
467
468 switch (cmd) {
469 case SIOCGETSGCNT_IN6:
470 ret = get_sg_cnt((struct sioc_sg_req6 *)data);
471 break;
472
473 case SIOCGETMIFCNT_IN6:
474 ret = get_mif6_cnt((struct sioc_mif_req6 *)data);
475 break;
476
477 default:
478 break;
479 }
480
481 return (ret);
482 }
483
484 /*
485 * returns the packet, byte, rpf-failure count for the source group provided
486 */
487 static int
get_sg_cnt(struct sioc_sg_req6 * req)488 get_sg_cnt(struct sioc_sg_req6 *req)
489 {
490 struct mf6c *rt;
491 int ret;
492
493 ret = 0;
494
495 MFC6_LOCK();
496
497 MF6CFIND(req->src.sin6_addr, req->grp.sin6_addr, rt);
498 if (rt == NULL) {
499 ret = ESRCH;
500 } else {
501 req->pktcnt = rt->mf6c_pkt_cnt;
502 req->bytecnt = rt->mf6c_byte_cnt;
503 req->wrong_if = rt->mf6c_wrong_if;
504 }
505
506 MFC6_UNLOCK();
507
508 return (ret);
509 }
510
511 /*
512 * returns the input and output packet and byte counts on the mif provided
513 */
514 static int
get_mif6_cnt(struct sioc_mif_req6 * req)515 get_mif6_cnt(struct sioc_mif_req6 *req)
516 {
517 mifi_t mifi;
518 int ret;
519
520 ret = 0;
521 mifi = req->mifi;
522
523 MIF6_LOCK();
524
525 if (mifi >= nummifs) {
526 ret = EINVAL;
527 } else {
528 req->icount = mif6table[mifi].m6_pkt_in;
529 req->ocount = mif6table[mifi].m6_pkt_out;
530 req->ibytes = mif6table[mifi].m6_bytes_in;
531 req->obytes = mif6table[mifi].m6_bytes_out;
532 }
533
534 MIF6_UNLOCK();
535
536 return (ret);
537 }
538
539 static int
set_pim6(int * i)540 set_pim6(int *i)
541 {
542 if ((*i != 1) && (*i != 0))
543 return (EINVAL);
544
545 V_pim6 = *i;
546
547 return (0);
548 }
549
550 /*
551 * Enable multicast routing
552 */
553 static int
ip6_mrouter_init(struct socket * so,int v,int cmd)554 ip6_mrouter_init(struct socket *so, int v, int cmd)
555 {
556
557 MRT6_DLOG(DEBUG_ANY, "%s: socket %p", __func__, so);
558
559 if (v != 1)
560 return (ENOPROTOOPT);
561
562 MROUTER6_LOCK();
563
564 if (V_ip6_mrouter != NULL) {
565 MROUTER6_UNLOCK();
566 return (EADDRINUSE);
567 }
568
569 V_ip6_mrouter = so;
570 V_ip6_mrouter_ver = cmd;
571
572 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
573 bzero((caddr_t)n6expire, sizeof(n6expire));
574
575 V_pim6 = 0;/* used for stubbing out/in pim stuff */
576
577 callout_init_mtx(&expire_upcalls_ch, MFC6_LOCKPTR(), 0);
578 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
579 expire_upcalls, NULL);
580
581 MROUTER6_UNLOCK();
582
583 MRT6_DLOG(DEBUG_ANY, "finished");
584
585 return (0);
586 }
587
588 /*
589 * Disable IPv6 multicast forwarding.
590 */
591 int
X_ip6_mrouter_done(void)592 X_ip6_mrouter_done(void)
593 {
594 mifi_t mifi;
595 u_long i;
596 struct mf6c *rt;
597 struct rtdetq *rte;
598
599 MROUTER6_LOCK();
600
601 if (V_ip6_mrouter == NULL) {
602 MROUTER6_UNLOCK();
603 return (EINVAL);
604 }
605
606 /*
607 * For each phyint in use, disable promiscuous reception of all IPv6
608 * multicasts.
609 */
610 for (mifi = 0; mifi < nummifs; mifi++) {
611 if (mif6table[mifi].m6_ifp &&
612 !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
613 if_allmulti(mif6table[mifi].m6_ifp, 0);
614 }
615 }
616 bzero((caddr_t)mif6table, sizeof(mif6table));
617 nummifs = 0;
618
619 V_pim6 = 0; /* used to stub out/in pim specific code */
620
621 /*
622 * Free all multicast forwarding cache entries.
623 */
624 MFC6_LOCK();
625 for (i = 0; i < MF6CTBLSIZ; i++) {
626 rt = mf6ctable[i];
627 while (rt) {
628 struct mf6c *frt;
629
630 for (rte = rt->mf6c_stall; rte != NULL; ) {
631 struct rtdetq *n = rte->next;
632
633 m_freem(rte->m);
634 free(rte, M_MRTABLE6);
635 rte = n;
636 }
637 frt = rt;
638 rt = rt->mf6c_next;
639 free(frt, M_MRTABLE6);
640 }
641 }
642 bzero((caddr_t)mf6ctable, sizeof(mf6ctable));
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 V_ip6_mrouter = NULL;
658 V_ip6_mrouter_ver = 0;
659
660 MROUTER6_UNLOCK();
661 MRT6_DLOG(DEBUG_ANY, "finished");
662
663 return (0);
664 }
665
666 static struct sockaddr_in6 sin6 = { sizeof(sin6), AF_INET6 };
667
668 /*
669 * Add a mif to the mif table
670 */
671 static int
add_m6if(struct mif6ctl * mifcp)672 add_m6if(struct mif6ctl *mifcp)
673 {
674 struct epoch_tracker et;
675 struct mif6 *mifp;
676 struct ifnet *ifp;
677 int error;
678
679 MIF6_LOCK();
680
681 if (mifcp->mif6c_mifi >= MAXMIFS) {
682 MIF6_UNLOCK();
683 return (EINVAL);
684 }
685 mifp = mif6table + mifcp->mif6c_mifi;
686 if (mifp->m6_ifp != NULL) {
687 MIF6_UNLOCK();
688 return (EADDRINUSE); /* XXX: is it appropriate? */
689 }
690
691 NET_EPOCH_ENTER(et);
692 if ((ifp = ifnet_byindex(mifcp->mif6c_pifi)) == NULL) {
693 NET_EPOCH_EXIT(et);
694 MIF6_UNLOCK();
695 return (ENXIO);
696 }
697 NET_EPOCH_EXIT(et); /* XXXGL: unsafe ifp */
698
699 if (mifcp->mif6c_flags & MIFF_REGISTER) {
700 if (reg_mif_num == (mifi_t)-1) {
701 ifp = if_alloc(IFT_OTHER);
702
703 if_initname(ifp, "register_mif", 0);
704 ifp->if_flags |= IFF_LOOPBACK;
705 if_attach(ifp);
706 multicast_register_if6 = ifp;
707 reg_mif_num = mifcp->mif6c_mifi;
708 /*
709 * it is impossible to guess the ifindex of the
710 * register interface. So mif6c_pifi is automatically
711 * calculated.
712 */
713 mifcp->mif6c_pifi = ifp->if_index;
714 } else {
715 ifp = multicast_register_if6;
716 }
717 } else {
718 /* Make sure the interface supports multicast */
719 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
720 MIF6_UNLOCK();
721 return (EOPNOTSUPP);
722 }
723
724 error = if_allmulti(ifp, 1);
725 if (error) {
726 MIF6_UNLOCK();
727 return (error);
728 }
729 }
730
731 mifp->m6_flags = mifcp->mif6c_flags;
732 mifp->m6_ifp = ifp;
733
734 /* initialize per mif pkt counters */
735 mifp->m6_pkt_in = 0;
736 mifp->m6_pkt_out = 0;
737 mifp->m6_bytes_in = 0;
738 mifp->m6_bytes_out = 0;
739
740 /* Adjust nummifs up if the mifi is higher than nummifs */
741 if (nummifs <= mifcp->mif6c_mifi)
742 nummifs = mifcp->mif6c_mifi + 1;
743
744 MIF6_UNLOCK();
745 MRT6_DLOG(DEBUG_ANY, "mif #%d, phyint %s", mifcp->mif6c_mifi,
746 if_name(ifp));
747
748 return (0);
749 }
750
751 /*
752 * Delete a mif from the mif table
753 */
754 static int
del_m6if_locked(mifi_t * mifip)755 del_m6if_locked(mifi_t *mifip)
756 {
757 struct mif6 *mifp = mif6table + *mifip;
758 mifi_t mifi;
759 struct ifnet *ifp;
760
761 MIF6_LOCK_ASSERT();
762
763 if (*mifip >= nummifs)
764 return (EINVAL);
765 if (mifp->m6_ifp == NULL)
766 return (EINVAL);
767
768 if (!(mifp->m6_flags & MIFF_REGISTER)) {
769 /* XXX: TODO: Maintain an ALLMULTI refcount in struct ifnet. */
770 ifp = mifp->m6_ifp;
771 if_allmulti(ifp, 0);
772 } else {
773 if (reg_mif_num != (mifi_t)-1 &&
774 multicast_register_if6 != NULL) {
775 if_detach(multicast_register_if6);
776 if_free(multicast_register_if6);
777 reg_mif_num = (mifi_t)-1;
778 multicast_register_if6 = NULL;
779 }
780 }
781
782 bzero((caddr_t)mifp, sizeof(*mifp));
783
784 /* Adjust nummifs down */
785 for (mifi = nummifs; mifi > 0; mifi--)
786 if (mif6table[mifi - 1].m6_ifp)
787 break;
788 nummifs = mifi;
789 MRT6_DLOG(DEBUG_ANY, "mif %d, nummifs %d", *mifip, nummifs);
790
791 return (0);
792 }
793
794 static int
del_m6if(mifi_t * mifip)795 del_m6if(mifi_t *mifip)
796 {
797 int cc;
798
799 MIF6_LOCK();
800 cc = del_m6if_locked(mifip);
801 MIF6_UNLOCK();
802
803 return (cc);
804 }
805
806 /*
807 * Add an mfc entry
808 */
809 static int
add_m6fc(struct mf6cctl * mfccp)810 add_m6fc(struct mf6cctl *mfccp)
811 {
812 struct mf6c *rt;
813 u_long hash;
814 struct rtdetq *rte;
815 u_short nstl;
816 char ip6bufo[INET6_ADDRSTRLEN], ip6bufg[INET6_ADDRSTRLEN];
817
818 MFC6_LOCK();
819
820 MF6CFIND(mfccp->mf6cc_origin.sin6_addr,
821 mfccp->mf6cc_mcastgrp.sin6_addr, rt);
822
823 /* If an entry already exists, just update the fields */
824 if (rt) {
825 MRT6_DLOG(DEBUG_MFC, "no upcall o %s g %s p %x",
826 ip6_sprintf(ip6bufo, &mfccp->mf6cc_origin.sin6_addr),
827 ip6_sprintf(ip6bufg, &mfccp->mf6cc_mcastgrp.sin6_addr),
828 mfccp->mf6cc_parent);
829
830 rt->mf6c_parent = mfccp->mf6cc_parent;
831 rt->mf6c_ifset = mfccp->mf6cc_ifset;
832
833 MFC6_UNLOCK();
834 return (0);
835 }
836
837 /*
838 * Find the entry for which the upcall was made and update
839 */
840 hash = MF6CHASH(mfccp->mf6cc_origin.sin6_addr,
841 mfccp->mf6cc_mcastgrp.sin6_addr);
842 for (rt = mf6ctable[hash], nstl = 0; rt; rt = rt->mf6c_next) {
843 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
844 &mfccp->mf6cc_origin.sin6_addr) &&
845 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
846 &mfccp->mf6cc_mcastgrp.sin6_addr) &&
847 (rt->mf6c_stall != NULL)) {
848 if (nstl++)
849 log(LOG_ERR,
850 "add_m6fc: %s o %s g %s p %x dbx %p\n",
851 "multiple kernel entries",
852 ip6_sprintf(ip6bufo,
853 &mfccp->mf6cc_origin.sin6_addr),
854 ip6_sprintf(ip6bufg,
855 &mfccp->mf6cc_mcastgrp.sin6_addr),
856 mfccp->mf6cc_parent, rt->mf6c_stall);
857
858 MRT6_DLOG(DEBUG_MFC, "o %s g %s p %x dbg %p",
859 ip6_sprintf(ip6bufo,
860 &mfccp->mf6cc_origin.sin6_addr),
861 ip6_sprintf(ip6bufg,
862 &mfccp->mf6cc_mcastgrp.sin6_addr),
863 mfccp->mf6cc_parent, rt->mf6c_stall);
864
865 rt->mf6c_origin = mfccp->mf6cc_origin;
866 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
867 rt->mf6c_parent = mfccp->mf6cc_parent;
868 rt->mf6c_ifset = mfccp->mf6cc_ifset;
869 /* initialize pkt counters per src-grp */
870 rt->mf6c_pkt_cnt = 0;
871 rt->mf6c_byte_cnt = 0;
872 rt->mf6c_wrong_if = 0;
873
874 rt->mf6c_expire = 0; /* Don't clean this guy up */
875 n6expire[hash]--;
876
877 /* free packets Qed at the end of this entry */
878 for (rte = rt->mf6c_stall; rte != NULL; ) {
879 struct rtdetq *n = rte->next;
880 ip6_mdq(rte->m, rte->ifp, rt);
881 m_freem(rte->m);
882 #ifdef UPCALL_TIMING
883 collate(&(rte->t));
884 #endif /* UPCALL_TIMING */
885 free(rte, M_MRTABLE6);
886 rte = n;
887 }
888 rt->mf6c_stall = NULL;
889 }
890 }
891
892 /*
893 * It is possible that an entry is being inserted without an upcall
894 */
895 if (nstl == 0) {
896 MRT6_DLOG(DEBUG_MFC, "no upcall h %lu o %s g %s p %x", hash,
897 ip6_sprintf(ip6bufo, &mfccp->mf6cc_origin.sin6_addr),
898 ip6_sprintf(ip6bufg, &mfccp->mf6cc_mcastgrp.sin6_addr),
899 mfccp->mf6cc_parent);
900
901 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
902 if (IN6_ARE_ADDR_EQUAL(&rt->mf6c_origin.sin6_addr,
903 &mfccp->mf6cc_origin.sin6_addr)&&
904 IN6_ARE_ADDR_EQUAL(&rt->mf6c_mcastgrp.sin6_addr,
905 &mfccp->mf6cc_mcastgrp.sin6_addr)) {
906 rt->mf6c_origin = mfccp->mf6cc_origin;
907 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
908 rt->mf6c_parent = mfccp->mf6cc_parent;
909 rt->mf6c_ifset = mfccp->mf6cc_ifset;
910 /* initialize pkt counters per src-grp */
911 rt->mf6c_pkt_cnt = 0;
912 rt->mf6c_byte_cnt = 0;
913 rt->mf6c_wrong_if = 0;
914
915 if (rt->mf6c_expire)
916 n6expire[hash]--;
917 rt->mf6c_expire = 0;
918 }
919 }
920 if (rt == NULL) {
921 /* no upcall, so make a new entry */
922 rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6,
923 M_NOWAIT);
924 if (rt == NULL) {
925 MFC6_UNLOCK();
926 return (ENOBUFS);
927 }
928
929 /* insert new entry at head of hash chain */
930 rt->mf6c_origin = mfccp->mf6cc_origin;
931 rt->mf6c_mcastgrp = mfccp->mf6cc_mcastgrp;
932 rt->mf6c_parent = mfccp->mf6cc_parent;
933 rt->mf6c_ifset = mfccp->mf6cc_ifset;
934 /* initialize pkt counters per src-grp */
935 rt->mf6c_pkt_cnt = 0;
936 rt->mf6c_byte_cnt = 0;
937 rt->mf6c_wrong_if = 0;
938 rt->mf6c_expire = 0;
939 rt->mf6c_stall = NULL;
940
941 /* link into table */
942 rt->mf6c_next = mf6ctable[hash];
943 mf6ctable[hash] = rt;
944 }
945 }
946
947 MFC6_UNLOCK();
948 return (0);
949 }
950
951 #ifdef UPCALL_TIMING
952 /*
953 * collect delay statistics on the upcalls
954 */
955 static void
collate(struct timeval * t)956 collate(struct timeval *t)
957 {
958 u_long d;
959 struct timeval tp;
960 u_long delta;
961
962 GET_TIME(tp);
963
964 if (TV_LT(*t, tp))
965 {
966 TV_DELTA(tp, *t, delta);
967
968 d = delta >> 10;
969 if (d > UPCALL_MAX)
970 d = UPCALL_MAX;
971
972 ++upcall_data[d];
973 }
974 }
975 #endif /* UPCALL_TIMING */
976
977 /*
978 * Delete an mfc entry
979 */
980 static int
del_m6fc(struct mf6cctl * mfccp)981 del_m6fc(struct mf6cctl *mfccp)
982 {
983 #ifdef MRT6DEBUG
984 char ip6bufo[INET6_ADDRSTRLEN], ip6bufg[INET6_ADDRSTRLEN];
985 #endif
986 struct sockaddr_in6 origin;
987 struct sockaddr_in6 mcastgrp;
988 struct mf6c *rt;
989 struct mf6c **nptr;
990 u_long hash;
991
992 origin = mfccp->mf6cc_origin;
993 mcastgrp = mfccp->mf6cc_mcastgrp;
994 hash = MF6CHASH(origin.sin6_addr, mcastgrp.sin6_addr);
995
996 MRT6_DLOG(DEBUG_MFC, "orig %s mcastgrp %s",
997 ip6_sprintf(ip6bufo, &origin.sin6_addr),
998 ip6_sprintf(ip6bufg, &mcastgrp.sin6_addr));
999
1000 MFC6_LOCK();
1001
1002 nptr = &mf6ctable[hash];
1003 while ((rt = *nptr) != NULL) {
1004 if (IN6_ARE_ADDR_EQUAL(&origin.sin6_addr,
1005 &rt->mf6c_origin.sin6_addr) &&
1006 IN6_ARE_ADDR_EQUAL(&mcastgrp.sin6_addr,
1007 &rt->mf6c_mcastgrp.sin6_addr) &&
1008 rt->mf6c_stall == NULL)
1009 break;
1010
1011 nptr = &rt->mf6c_next;
1012 }
1013 if (rt == NULL) {
1014 MFC6_UNLOCK();
1015 return (EADDRNOTAVAIL);
1016 }
1017
1018 *nptr = rt->mf6c_next;
1019 free(rt, M_MRTABLE6);
1020
1021 MFC6_UNLOCK();
1022
1023 return (0);
1024 }
1025
1026 static int
socket_send(struct socket * s,struct mbuf * mm,struct sockaddr_in6 * src)1027 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in6 *src)
1028 {
1029
1030 if (s) {
1031 if (sbappendaddr(&s->so_rcv,
1032 (struct sockaddr *)src,
1033 mm, (struct mbuf *)0) != 0) {
1034 sorwakeup(s);
1035 return (0);
1036 } else
1037 soroverflow(s);
1038 }
1039 m_freem(mm);
1040 return (-1);
1041 }
1042
1043 /*
1044 * IPv6 multicast forwarding function. This function assumes that the packet
1045 * pointed to by "ip6" has arrived on (or is about to be sent to) the interface
1046 * pointed to by "ifp", and the packet is to be relayed to other networks
1047 * that have members of the packet's destination IPv6 multicast group.
1048 *
1049 * The packet is returned unscathed to the caller, unless it is
1050 * erroneous, in which case a non-zero return value tells the caller to
1051 * discard it.
1052 *
1053 * NOTE: this implementation assumes that m->m_pkthdr.rcvif is NULL iff
1054 * this function is called in the originating context (i.e., not when
1055 * forwarding a packet from other node). ip6_output(), which is currently the
1056 * only function that calls this function is called in the originating context,
1057 * explicitly ensures this condition. It is caller's responsibility to ensure
1058 * that if this function is called from somewhere else in the originating
1059 * context in the future.
1060 */
1061 int
X_ip6_mforward(struct ip6_hdr * ip6,struct ifnet * ifp,struct mbuf * m)1062 X_ip6_mforward(struct ip6_hdr *ip6, struct ifnet *ifp, struct mbuf *m)
1063 {
1064 struct rtdetq *rte;
1065 struct mbuf *mb0;
1066 struct mf6c *rt;
1067 struct mif6 *mifp;
1068 struct mbuf *mm;
1069 u_long hash;
1070 mifi_t mifi;
1071 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1072 #ifdef UPCALL_TIMING
1073 struct timeval tp;
1074
1075 GET_TIME(tp);
1076 #endif /* UPCALL_TIMING */
1077
1078 MRT6_DLOG(DEBUG_FORWARD, "src %s, dst %s, ifindex %d",
1079 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1080 ip6_sprintf(ip6bufd, &ip6->ip6_dst), ifp->if_index);
1081
1082 /*
1083 * Don't forward a packet with Hop limit of zero or one,
1084 * or a packet destined to a local-only group.
1085 */
1086 if (ip6->ip6_hlim <= 1 || IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst) ||
1087 IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst))
1088 return (0);
1089 ip6->ip6_hlim--;
1090
1091 /*
1092 * Source address check: do not forward packets with unspecified
1093 * source. It was discussed in July 2000, on ipngwg mailing list.
1094 * This is rather more serious than unicast cases, because some
1095 * MLD packets can be sent with the unspecified source address
1096 * (although such packets must normally set 1 to the hop limit field).
1097 */
1098 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
1099 IP6STAT_INC(ip6s_cantforward);
1100 if (V_ip6_log_cannot_forward && ip6_log_ratelimit()) {
1101 log(LOG_DEBUG,
1102 "cannot forward "
1103 "from %s to %s nxt %d received on %s\n",
1104 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1105 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
1106 ip6->ip6_nxt,
1107 if_name(m->m_pkthdr.rcvif));
1108 }
1109 return (0);
1110 }
1111
1112 MFC6_LOCK();
1113
1114 /*
1115 * Determine forwarding mifs from the forwarding cache table
1116 */
1117 MF6CFIND(ip6->ip6_src, ip6->ip6_dst, rt);
1118 MRT6STAT_INC(mrt6s_mfc_lookups);
1119
1120 /* Entry exists, so forward if necessary */
1121 if (rt) {
1122 MFC6_UNLOCK();
1123 return (ip6_mdq(m, ifp, rt));
1124 }
1125
1126 /*
1127 * If we don't have a route for packet's origin,
1128 * Make a copy of the packet & send message to routing daemon.
1129 */
1130 MRT6STAT_INC(mrt6s_no_route);
1131 MRT6_DLOG(DEBUG_FORWARD | DEBUG_MFC, "no rte s %s g %s",
1132 ip6_sprintf(ip6bufs, &ip6->ip6_src),
1133 ip6_sprintf(ip6bufd, &ip6->ip6_dst));
1134
1135 /*
1136 * Allocate mbufs early so that we don't do extra work if we
1137 * are just going to fail anyway.
1138 */
1139 rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE6, M_NOWAIT);
1140 if (rte == NULL) {
1141 MFC6_UNLOCK();
1142 return (ENOBUFS);
1143 }
1144 mb0 = m_copym(m, 0, M_COPYALL, M_NOWAIT);
1145 /*
1146 * Pullup packet header if needed before storing it,
1147 * as other references may modify it in the meantime.
1148 */
1149 if (mb0 && (!M_WRITABLE(mb0) || mb0->m_len < sizeof(struct ip6_hdr)))
1150 mb0 = m_pullup(mb0, sizeof(struct ip6_hdr));
1151 if (mb0 == NULL) {
1152 free(rte, M_MRTABLE6);
1153 MFC6_UNLOCK();
1154 return (ENOBUFS);
1155 }
1156
1157 /* is there an upcall waiting for this packet? */
1158 hash = MF6CHASH(ip6->ip6_src, ip6->ip6_dst);
1159 for (rt = mf6ctable[hash]; rt; rt = rt->mf6c_next) {
1160 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
1161 &rt->mf6c_origin.sin6_addr) &&
1162 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1163 &rt->mf6c_mcastgrp.sin6_addr) && (rt->mf6c_stall != NULL))
1164 break;
1165 }
1166
1167 if (rt == NULL) {
1168 struct mrt6msg *im;
1169 #ifdef MRT6_OINIT
1170 struct omrt6msg *oim;
1171 #endif
1172 /* no upcall, so make a new entry */
1173 rt = (struct mf6c *)malloc(sizeof(*rt), M_MRTABLE6, M_NOWAIT);
1174 if (rt == NULL) {
1175 free(rte, M_MRTABLE6);
1176 m_freem(mb0);
1177 MFC6_UNLOCK();
1178 return (ENOBUFS);
1179 }
1180 /*
1181 * Make a copy of the header to send to the user
1182 * level process
1183 */
1184 mm = m_copym(mb0, 0, sizeof(struct ip6_hdr), M_NOWAIT);
1185 if (mm == NULL) {
1186 free(rte, M_MRTABLE6);
1187 m_freem(mb0);
1188 free(rt, M_MRTABLE6);
1189 MFC6_UNLOCK();
1190 return (ENOBUFS);
1191 }
1192
1193 /*
1194 * Send message to routing daemon
1195 */
1196 sin6.sin6_addr = ip6->ip6_src;
1197 im = NULL;
1198 #ifdef MRT6_OINIT
1199 oim = NULL;
1200 #endif
1201 switch (V_ip6_mrouter_ver) {
1202 #ifdef MRT6_OINIT
1203 case MRT6_OINIT:
1204 oim = mtod(mm, struct omrt6msg *);
1205 oim->im6_msgtype = MRT6MSG_NOCACHE;
1206 oim->im6_mbz = 0;
1207 break;
1208 #endif
1209 case MRT6_INIT:
1210 im = mtod(mm, struct mrt6msg *);
1211 im->im6_msgtype = MRT6MSG_NOCACHE;
1212 im->im6_mbz = 0;
1213 break;
1214 default:
1215 free(rte, M_MRTABLE6);
1216 m_freem(mb0);
1217 free(rt, M_MRTABLE6);
1218 MFC6_UNLOCK();
1219 return (EINVAL);
1220 }
1221
1222 MRT6_DLOG(DEBUG_FORWARD, "getting the iif info in the kernel");
1223 for (mifp = mif6table, mifi = 0;
1224 mifi < nummifs && mifp->m6_ifp != ifp; mifp++, mifi++)
1225 ;
1226
1227 switch (V_ip6_mrouter_ver) {
1228 #ifdef MRT6_OINIT
1229 case MRT6_OINIT:
1230 oim->im6_mif = mifi;
1231 break;
1232 #endif
1233 case MRT6_INIT:
1234 im->im6_mif = mifi;
1235 break;
1236 }
1237
1238 if (socket_send(V_ip6_mrouter, mm, &sin6) < 0) {
1239 log(LOG_WARNING, "ip6_mforward: ip6_mrouter "
1240 "socket queue full\n");
1241 MRT6STAT_INC(mrt6s_upq_sockfull);
1242 free(rte, M_MRTABLE6);
1243 m_freem(mb0);
1244 free(rt, M_MRTABLE6);
1245 MFC6_UNLOCK();
1246 return (ENOBUFS);
1247 }
1248
1249 MRT6STAT_INC(mrt6s_upcalls);
1250
1251 /* insert new entry at head of hash chain */
1252 bzero(rt, sizeof(*rt));
1253 rt->mf6c_origin.sin6_family = AF_INET6;
1254 rt->mf6c_origin.sin6_len = sizeof(struct sockaddr_in6);
1255 rt->mf6c_origin.sin6_addr = ip6->ip6_src;
1256 rt->mf6c_mcastgrp.sin6_family = AF_INET6;
1257 rt->mf6c_mcastgrp.sin6_len = sizeof(struct sockaddr_in6);
1258 rt->mf6c_mcastgrp.sin6_addr = ip6->ip6_dst;
1259 rt->mf6c_expire = UPCALL_EXPIRE;
1260 n6expire[hash]++;
1261 rt->mf6c_parent = MF6C_INCOMPLETE_PARENT;
1262
1263 /* link into table */
1264 rt->mf6c_next = mf6ctable[hash];
1265 mf6ctable[hash] = rt;
1266 /* Add this entry to the end of the queue */
1267 rt->mf6c_stall = rte;
1268 } else {
1269 /* determine if q has overflowed */
1270 struct rtdetq **p;
1271 int npkts = 0;
1272
1273 for (p = &rt->mf6c_stall; *p != NULL; p = &(*p)->next)
1274 if (++npkts > MAX_UPQ6) {
1275 MRT6STAT_INC(mrt6s_upq_ovflw);
1276 free(rte, M_MRTABLE6);
1277 m_freem(mb0);
1278 MFC6_UNLOCK();
1279 return (0);
1280 }
1281
1282 /* Add this entry to the end of the queue */
1283 *p = rte;
1284 }
1285
1286 rte->next = NULL;
1287 rte->m = mb0;
1288 rte->ifp = ifp;
1289 #ifdef UPCALL_TIMING
1290 rte->t = tp;
1291 #endif /* UPCALL_TIMING */
1292
1293 MFC6_UNLOCK();
1294
1295 return (0);
1296 }
1297
1298 /*
1299 * Clean up cache entries if upcalls are not serviced
1300 * Call from the Slow Timeout mechanism, every half second.
1301 */
1302 static void
expire_upcalls(void * unused)1303 expire_upcalls(void *unused)
1304 {
1305 #ifdef MRT6DEBUG
1306 char ip6bufo[INET6_ADDRSTRLEN], ip6bufg[INET6_ADDRSTRLEN];
1307 #endif
1308 struct rtdetq *rte;
1309 struct mf6c *mfc, **nptr;
1310 u_long i;
1311
1312 MFC6_LOCK_ASSERT();
1313
1314 for (i = 0; i < MF6CTBLSIZ; i++) {
1315 if (n6expire[i] == 0)
1316 continue;
1317 nptr = &mf6ctable[i];
1318 while ((mfc = *nptr) != NULL) {
1319 rte = mfc->mf6c_stall;
1320 /*
1321 * Skip real cache entries
1322 * Make sure it wasn't marked to not expire (shouldn't happen)
1323 * If it expires now
1324 */
1325 if (rte != NULL &&
1326 mfc->mf6c_expire != 0 &&
1327 --mfc->mf6c_expire == 0) {
1328 MRT6_DLOG(DEBUG_EXPIRE, "expiring (%s %s)",
1329 ip6_sprintf(ip6bufo, &mfc->mf6c_origin.sin6_addr),
1330 ip6_sprintf(ip6bufg, &mfc->mf6c_mcastgrp.sin6_addr));
1331 /*
1332 * drop all the packets
1333 * free the mbuf with the pkt, if, timing info
1334 */
1335 do {
1336 struct rtdetq *n = rte->next;
1337 m_freem(rte->m);
1338 free(rte, M_MRTABLE6);
1339 rte = n;
1340 } while (rte != NULL);
1341 MRT6STAT_INC(mrt6s_cache_cleanups);
1342 n6expire[i]--;
1343
1344 *nptr = mfc->mf6c_next;
1345 free(mfc, M_MRTABLE6);
1346 } else {
1347 nptr = &mfc->mf6c_next;
1348 }
1349 }
1350 }
1351 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT,
1352 expire_upcalls, NULL);
1353 }
1354
1355 /*
1356 * Packet forwarding routine once entry in the cache is made
1357 */
1358 static int
ip6_mdq(struct mbuf * m,struct ifnet * ifp,struct mf6c * rt)1359 ip6_mdq(struct mbuf *m, struct ifnet *ifp, struct mf6c *rt)
1360 {
1361 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1362 mifi_t mifi, iif;
1363 struct mif6 *mifp;
1364 int plen = m->m_pkthdr.len;
1365 struct in6_addr src0, dst0; /* copies for local work */
1366 u_int32_t iszone, idzone, oszone, odzone;
1367 int error = 0;
1368
1369 /*
1370 * Don't forward if it didn't arrive from the parent mif
1371 * for its origin.
1372 */
1373 mifi = rt->mf6c_parent;
1374 if ((mifi >= nummifs) || (mif6table[mifi].m6_ifp != ifp)) {
1375 /* came in the wrong interface */
1376 MRT6_DLOG(DEBUG_FORWARD,
1377 "wrong if: ifid %d mifi %d mififid %x", ifp->if_index,
1378 mifi, mif6table[mifi].m6_ifp->if_index);
1379 MRT6STAT_INC(mrt6s_wrong_if);
1380 rt->mf6c_wrong_if++;
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 (mifi < nummifs && mif6table[mifi].m6_ifp)
1388 if (V_pim6 && (m->m_flags & M_LOOP) == 0) {
1389 /*
1390 * Check the M_LOOP flag to avoid an
1391 * unnecessary PIM assert.
1392 * XXX: M_LOOP is an ad-hoc hack...
1393 */
1394 static struct sockaddr_in6 sin6 =
1395 { sizeof(sin6), AF_INET6 };
1396
1397 struct mbuf *mm;
1398 struct mrt6msg *im;
1399 #ifdef MRT6_OINIT
1400 struct omrt6msg *oim;
1401 #endif
1402
1403 mm = m_copym(m, 0, sizeof(struct ip6_hdr),
1404 M_NOWAIT);
1405 if (mm &&
1406 (!M_WRITABLE(mm) ||
1407 mm->m_len < sizeof(struct ip6_hdr)))
1408 mm = m_pullup(mm, sizeof(struct ip6_hdr));
1409 if (mm == NULL)
1410 return (ENOBUFS);
1411
1412 #ifdef MRT6_OINIT
1413 oim = NULL;
1414 #endif
1415 im = NULL;
1416 switch (V_ip6_mrouter_ver) {
1417 #ifdef MRT6_OINIT
1418 case MRT6_OINIT:
1419 oim = mtod(mm, struct omrt6msg *);
1420 oim->im6_msgtype = MRT6MSG_WRONGMIF;
1421 oim->im6_mbz = 0;
1422 break;
1423 #endif
1424 case MRT6_INIT:
1425 im = mtod(mm, struct mrt6msg *);
1426 im->im6_msgtype = MRT6MSG_WRONGMIF;
1427 im->im6_mbz = 0;
1428 break;
1429 default:
1430 m_freem(mm);
1431 return (EINVAL);
1432 }
1433
1434 for (mifp = mif6table, iif = 0;
1435 iif < nummifs && mifp &&
1436 mifp->m6_ifp != ifp;
1437 mifp++, iif++)
1438 ;
1439
1440 switch (V_ip6_mrouter_ver) {
1441 #ifdef MRT6_OINIT
1442 case MRT6_OINIT:
1443 oim->im6_mif = iif;
1444 sin6.sin6_addr = oim->im6_src;
1445 break;
1446 #endif
1447 case MRT6_INIT:
1448 im->im6_mif = iif;
1449 sin6.sin6_addr = im->im6_src;
1450 break;
1451 }
1452
1453 MRT6STAT_INC(mrt6s_upcalls);
1454
1455 if (socket_send(V_ip6_mrouter, mm, &sin6) < 0) {
1456 MRT6_DLOG(DEBUG_ANY,
1457 "ip6_mrouter socket queue full");
1458 MRT6STAT_INC(mrt6s_upq_sockfull);
1459 return (ENOBUFS);
1460 } /* if socket Q full */
1461 } /* if PIM */
1462 return (0);
1463 } /* if wrong iif */
1464
1465 /* If I sourced this packet, it counts as output, else it was input. */
1466 if (m->m_pkthdr.rcvif == NULL) {
1467 /* XXX: is rcvif really NULL when output?? */
1468 mif6table[mifi].m6_pkt_out++;
1469 mif6table[mifi].m6_bytes_out += plen;
1470 } else {
1471 mif6table[mifi].m6_pkt_in++;
1472 mif6table[mifi].m6_bytes_in += plen;
1473 }
1474 rt->mf6c_pkt_cnt++;
1475 rt->mf6c_byte_cnt += plen;
1476
1477 /*
1478 * For each mif, forward a copy of the packet if there are group
1479 * members downstream on the interface.
1480 */
1481 src0 = ip6->ip6_src;
1482 dst0 = ip6->ip6_dst;
1483 if ((error = in6_setscope(&src0, ifp, &iszone)) != 0 ||
1484 (error = in6_setscope(&dst0, ifp, &idzone)) != 0) {
1485 IP6STAT_INC(ip6s_badscope);
1486 return (error);
1487 }
1488 for (mifp = mif6table, mifi = 0; mifi < nummifs; mifp++, mifi++) {
1489 if (IF_ISSET(mifi, &rt->mf6c_ifset)) {
1490 /*
1491 * check if the outgoing packet is going to break
1492 * a scope boundary.
1493 * XXX For packets through PIM register tunnel
1494 * interface, we believe a routing daemon.
1495 */
1496 if (!(mif6table[rt->mf6c_parent].m6_flags &
1497 MIFF_REGISTER) &&
1498 !(mif6table[mifi].m6_flags & MIFF_REGISTER)) {
1499 if (in6_setscope(&src0, mif6table[mifi].m6_ifp,
1500 &oszone) ||
1501 in6_setscope(&dst0, mif6table[mifi].m6_ifp,
1502 &odzone) ||
1503 iszone != oszone ||
1504 idzone != odzone) {
1505 IP6STAT_INC(ip6s_badscope);
1506 continue;
1507 }
1508 }
1509
1510 mifp->m6_pkt_out++;
1511 mifp->m6_bytes_out += plen;
1512 if (mifp->m6_flags & MIFF_REGISTER)
1513 register_send(ip6, mifp, m);
1514 else
1515 phyint_send(ip6, mifp, m);
1516 }
1517 }
1518 return (0);
1519 }
1520
1521 static void
phyint_send(struct ip6_hdr * ip6,struct mif6 * mifp,struct mbuf * m)1522 phyint_send(struct ip6_hdr *ip6, struct mif6 *mifp, struct mbuf *m)
1523 {
1524 #ifdef MRT6DEBUG
1525 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN];
1526 #endif
1527 struct mbuf *mb_copy;
1528 struct ifnet *ifp = mifp->m6_ifp;
1529 int error __unused = 0;
1530 u_long linkmtu;
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_LINKMTU(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_mrouter != NULL)
1912 return EINVAL;
1913
1914 if (pim6_encap_cookie) {
1915 ip6_encap_detach(pim6_encap_cookie);
1916 pim6_encap_cookie = NULL;
1917 }
1918 X_ip6_mrouter_done();
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