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