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