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