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