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