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