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