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