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