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