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