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