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