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