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