1 /* $OpenBSD: ip_mroute.c,v 1.56 2009/08/01 09:08:21 blambert Exp $ */ 2 /* $NetBSD: ip_mroute.c,v 1.85 2004/04/26 01:31:57 matt Exp $ */ 3 4 /* 5 * Copyright (c) 1989 Stephen Deering 6 * Copyright (c) 1992, 1993 7 * The Regents of the University of California. All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * Stephen Deering of Stanford University. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93 37 */ 38 39 /* 40 * IP multicast forwarding procedures 41 * 42 * Written by David Waitzman, BBN Labs, August 1988. 43 * Modified by Steve Deering, Stanford, February 1989. 44 * Modified by Mark J. Steiglitz, Stanford, May, 1991 45 * Modified by Van Jacobson, LBL, January 1993 46 * Modified by Ajit Thyagarajan, PARC, August 1993 47 * Modified by Bill Fenner, PARC, April 1994 48 * Modified by Charles M. Hannum, NetBSD, May 1995. 49 * Modified by Ahmed Helmy, SGI, June 1996 50 * Modified by George Edmond Eddy (Rusty), ISI, February 1998 51 * Modified by Pavlin Radoslavov, USC/ISI, May 1998, August 1999, October 2000 52 * Modified by Hitoshi Asaeda, WIDE, August 2000 53 * Modified by Pavlin Radoslavov, ICSI, October 2002 54 * 55 * MROUTING Revision: 1.2 56 * and PIM-SMv2 and PIM-DM support, advanced API support, 57 * bandwidth metering and signaling 58 */ 59 60 #ifdef PIM 61 #define _PIM_VT 1 62 #endif 63 64 #include <sys/param.h> 65 #include <sys/systm.h> 66 #include <sys/mbuf.h> 67 #include <sys/socket.h> 68 #include <sys/socketvar.h> 69 #include <sys/protosw.h> 70 #include <sys/errno.h> 71 #include <sys/time.h> 72 #include <sys/kernel.h> 73 #include <sys/ioctl.h> 74 #include <sys/syslog.h> 75 #include <sys/sysctl.h> 76 #include <sys/timeout.h> 77 78 #include <net/if.h> 79 #include <net/route.h> 80 #include <net/raw_cb.h> 81 82 #include <netinet/in.h> 83 #include <netinet/in_var.h> 84 #include <netinet/in_systm.h> 85 #include <netinet/ip.h> 86 #include <netinet/ip_var.h> 87 #include <netinet/in_pcb.h> 88 #include <netinet/udp.h> 89 #include <netinet/igmp.h> 90 #include <netinet/igmp_var.h> 91 #include <netinet/ip_mroute.h> 92 #ifdef PIM 93 #include <netinet/pim.h> 94 #include <netinet/pim_var.h> 95 #endif 96 97 #include <sys/stdarg.h> 98 99 #define IP_MULTICASTOPTS 0 100 #define M_PULLUP(m, len) \ 101 do { \ 102 if ((m) && ((m)->m_flags & M_EXT || (m)->m_len < (len))) \ 103 (m) = m_pullup((m), (len)); \ 104 } while (/*CONSTCOND*/ 0) 105 106 /* 107 * Globals. All but ip_mrouter and ip_mrtproto could be static, 108 * except for netstat or debugging purposes. 109 */ 110 struct socket *ip_mrouter = NULL; 111 int ip_mrtproto = IGMP_DVMRP; /* for netstat only */ 112 113 #define NO_RTE_FOUND 0x1 114 #define RTE_FOUND 0x2 115 116 #define MFCHASH(a, g) \ 117 ((((a).s_addr >> 20) ^ ((a).s_addr >> 10) ^ (a).s_addr ^ \ 118 ((g).s_addr >> 20) ^ ((g).s_addr >> 10) ^ (g).s_addr) & mfchash) 119 LIST_HEAD(mfchashhdr, mfc) *mfchashtbl; 120 u_long mfchash; 121 122 u_char nexpire[MFCTBLSIZ]; 123 struct vif viftable[MAXVIFS]; 124 struct mrtstat mrtstat; 125 u_int mrtdebug = 0; /* debug level */ 126 #define DEBUG_MFC 0x02 127 #define DEBUG_FORWARD 0x04 128 #define DEBUG_EXPIRE 0x08 129 #define DEBUG_XMIT 0x10 130 #define DEBUG_PIM 0x20 131 132 #define VIFI_INVALID ((vifi_t) -1) 133 134 #ifdef RSVP_ISI 135 u_int rsvpdebug = 0; /* rsvp debug level */ 136 extern struct socket *ip_rsvpd; 137 extern int rsvp_on; 138 #endif /* RSVP_ISI */ 139 140 #define EXPIRE_TIMEOUT 250 /* 4x / second */ 141 #define UPCALL_EXPIRE 6 /* number of timeouts */ 142 struct timeout expire_upcalls_ch; 143 144 static int get_sg_cnt(struct sioc_sg_req *); 145 static int get_vif_cnt(struct sioc_vif_req *); 146 static int ip_mrouter_init(struct socket *, struct mbuf *); 147 static int get_version(struct mbuf *); 148 static int set_assert(struct mbuf *); 149 static int get_assert(struct mbuf *); 150 static int add_vif(struct mbuf *); 151 static int del_vif(struct mbuf *); 152 static void update_mfc_params(struct mfc *, struct mfcctl2 *); 153 static void init_mfc_params(struct mfc *, struct mfcctl2 *); 154 static void expire_mfc(struct mfc *); 155 static int add_mfc(struct mbuf *); 156 #ifdef UPCALL_TIMING 157 static void collate(struct timeval *); 158 #endif 159 static int del_mfc(struct mbuf *); 160 static int set_api_config(struct mbuf *); /* chose API capabilities */ 161 static int get_api_support(struct mbuf *); 162 static int get_api_config(struct mbuf *); 163 static int socket_send(struct socket *, struct mbuf *, 164 struct sockaddr_in *); 165 static void expire_upcalls(void *); 166 #ifdef RSVP_ISI 167 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t); 168 #else 169 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *); 170 #endif 171 static void phyint_send(struct ip *, struct vif *, struct mbuf *); 172 static void encap_send(struct ip *, struct vif *, struct mbuf *); 173 static void send_packet(struct vif *, struct mbuf *); 174 175 /* 176 * Bandwidth monitoring 177 */ 178 static void free_bw_list(struct bw_meter *); 179 static int add_bw_upcall(struct mbuf *); 180 static int del_bw_upcall(struct mbuf *); 181 static void bw_meter_receive_packet(struct bw_meter *, int , struct timeval *); 182 static void bw_meter_prepare_upcall(struct bw_meter *, struct timeval *); 183 static void bw_upcalls_send(void); 184 static void schedule_bw_meter(struct bw_meter *, struct timeval *); 185 static void unschedule_bw_meter(struct bw_meter *); 186 static void bw_meter_process(void); 187 static void expire_bw_upcalls_send(void *); 188 static void expire_bw_meter_process(void *); 189 190 #ifdef PIM 191 static int pim_register_send(struct ip *, struct vif *, 192 struct mbuf *, struct mfc *); 193 static int pim_register_send_rp(struct ip *, struct vif *, 194 struct mbuf *, struct mfc *); 195 static int pim_register_send_upcall(struct ip *, struct vif *, 196 struct mbuf *, struct mfc *); 197 static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *); 198 #endif 199 200 /* 201 * 'Interfaces' associated with decapsulator (so we can tell 202 * packets that went through it from ones that get reflected 203 * by a broken gateway). These interfaces are never linked into 204 * the system ifnet list & no routes point to them. I.e., packets 205 * can't be sent this way. They only exist as a placeholder for 206 * multicast source verification. 207 */ 208 #if 0 209 struct ifnet multicast_decap_if[MAXVIFS]; 210 #endif 211 212 #define ENCAP_TTL 64 213 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */ 214 215 /* prototype IP hdr for encapsulated packets */ 216 struct ip multicast_encap_iphdr = { 217 #if BYTE_ORDER == LITTLE_ENDIAN 218 sizeof(struct ip) >> 2, IPVERSION, 219 #else 220 IPVERSION, sizeof(struct ip) >> 2, 221 #endif 222 0, /* tos */ 223 sizeof(struct ip), /* total length */ 224 0, /* id */ 225 0, /* frag offset */ 226 ENCAP_TTL, ENCAP_PROTO, 227 0, /* checksum */ 228 }; 229 230 /* 231 * Bandwidth meter variables and constants 232 */ 233 234 /* 235 * Pending timeouts are stored in a hash table, the key being the 236 * expiration time. Periodically, the entries are analysed and processed. 237 */ 238 #define BW_METER_BUCKETS 1024 239 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS]; 240 struct timeout bw_meter_ch; 241 #define BW_METER_PERIOD 1000 /* periodical handling of bw meters (in ms) */ 242 243 /* 244 * Pending upcalls are stored in a vector which is flushed when 245 * full, or periodically 246 */ 247 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX]; 248 static u_int bw_upcalls_n; /* # of pending upcalls */ 249 struct timeout bw_upcalls_ch; 250 #define BW_UPCALLS_PERIOD 1000 /* periodical flush of bw upcalls (in ms) */ 251 252 #ifdef PIM 253 struct pimstat pimstat; 254 255 /* 256 * Note: the PIM Register encapsulation adds the following in front of a 257 * data packet: 258 * 259 * struct pim_encap_hdr { 260 * struct ip ip; 261 * struct pim_encap_pimhdr pim; 262 * } 263 * 264 */ 265 266 struct pim_encap_pimhdr { 267 struct pim pim; 268 uint32_t flags; 269 }; 270 271 static struct ip pim_encap_iphdr = { 272 #if BYTE_ORDER == LITTLE_ENDIAN 273 sizeof(struct ip) >> 2, 274 IPVERSION, 275 #else 276 IPVERSION, 277 sizeof(struct ip) >> 2, 278 #endif 279 0, /* tos */ 280 sizeof(struct ip), /* total length */ 281 0, /* id */ 282 0, /* frag offset */ 283 ENCAP_TTL, 284 IPPROTO_PIM, 285 0, /* checksum */ 286 }; 287 288 static struct pim_encap_pimhdr pim_encap_pimhdr = { 289 { 290 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */ 291 0, /* reserved */ 292 0, /* checksum */ 293 }, 294 0 /* flags */ 295 }; 296 297 static struct ifnet multicast_register_if; 298 static vifi_t reg_vif_num = VIFI_INVALID; 299 #endif /* PIM */ 300 301 302 /* 303 * Private variables. 304 */ 305 static vifi_t numvifs = 0; 306 static int have_encap_tunnel = 0; 307 308 /* 309 * whether or not special PIM assert processing is enabled. 310 */ 311 static int pim_assert; 312 /* 313 * Rate limit for assert notification messages, in usec 314 */ 315 #define ASSERT_MSG_TIME 3000000 316 317 /* 318 * Kernel multicast routing API capabilities and setup. 319 * If more API capabilities are added to the kernel, they should be 320 * recorded in `mrt_api_support'. 321 */ 322 static const u_int32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF | 323 MRT_MFC_FLAGS_BORDER_VIF | 324 MRT_MFC_RP | 325 MRT_MFC_BW_UPCALL); 326 static u_int32_t mrt_api_config = 0; 327 328 /* 329 * Find a route for a given origin IP address and Multicast group address 330 * Type of service parameter to be added in the future!!! 331 * Statistics are updated by the caller if needed 332 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses) 333 */ 334 static struct mfc * 335 mfc_find(struct in_addr *o, struct in_addr *g) 336 { 337 struct mfc *rt; 338 339 LIST_FOREACH(rt, &mfchashtbl[MFCHASH(*o, *g)], mfc_hash) { 340 if (in_hosteq(rt->mfc_origin, *o) && 341 in_hosteq(rt->mfc_mcastgrp, *g) && 342 (rt->mfc_stall == NULL)) 343 break; 344 } 345 346 return (rt); 347 } 348 349 /* 350 * Macros to compute elapsed time efficiently 351 * Borrowed from Van Jacobson's scheduling code 352 */ 353 #define TV_DELTA(a, b, delta) do { \ 354 int xxs; \ 355 delta = (a).tv_usec - (b).tv_usec; \ 356 xxs = (a).tv_sec - (b).tv_sec; \ 357 switch (xxs) { \ 358 case 2: \ 359 delta += 1000000; \ 360 /* FALLTHROUGH */ \ 361 case 1: \ 362 delta += 1000000; \ 363 /* FALLTHROUGH */ \ 364 case 0: \ 365 break; \ 366 default: \ 367 delta += (1000000 * xxs); \ 368 break; \ 369 } \ 370 } while (/*CONSTCOND*/ 0) 371 372 #ifdef UPCALL_TIMING 373 u_int32_t upcall_data[51]; 374 #endif /* UPCALL_TIMING */ 375 376 /* 377 * Handle MRT setsockopt commands to modify the multicast routing tables. 378 */ 379 int 380 ip_mrouter_set(struct socket *so, int optname, struct mbuf **m) 381 { 382 int error; 383 384 if (optname != MRT_INIT && so != ip_mrouter) 385 error = ENOPROTOOPT; 386 else 387 switch (optname) { 388 case MRT_INIT: 389 error = ip_mrouter_init(so, *m); 390 break; 391 case MRT_DONE: 392 error = ip_mrouter_done(); 393 break; 394 case MRT_ADD_VIF: 395 error = add_vif(*m); 396 break; 397 case MRT_DEL_VIF: 398 error = del_vif(*m); 399 break; 400 case MRT_ADD_MFC: 401 error = add_mfc(*m); 402 break; 403 case MRT_DEL_MFC: 404 error = del_mfc(*m); 405 break; 406 case MRT_ASSERT: 407 error = set_assert(*m); 408 break; 409 case MRT_API_CONFIG: 410 error = set_api_config(*m); 411 break; 412 case MRT_ADD_BW_UPCALL: 413 error = add_bw_upcall(*m); 414 break; 415 case MRT_DEL_BW_UPCALL: 416 error = del_bw_upcall(*m); 417 break; 418 default: 419 error = ENOPROTOOPT; 420 break; 421 } 422 423 if (*m) 424 m_free(*m); 425 return (error); 426 } 427 428 /* 429 * Handle MRT getsockopt commands 430 */ 431 int 432 ip_mrouter_get(struct socket *so, int optname, struct mbuf **m) 433 { 434 int error; 435 436 if (so != ip_mrouter) 437 error = ENOPROTOOPT; 438 else { 439 *m = m_get(M_WAIT, MT_SOOPTS); 440 441 switch (optname) { 442 case MRT_VERSION: 443 error = get_version(*m); 444 break; 445 case MRT_ASSERT: 446 error = get_assert(*m); 447 break; 448 case MRT_API_SUPPORT: 449 error = get_api_support(*m); 450 break; 451 case MRT_API_CONFIG: 452 error = get_api_config(*m); 453 break; 454 default: 455 error = ENOPROTOOPT; 456 break; 457 } 458 459 if (error) 460 m_free(*m); 461 } 462 463 return (error); 464 } 465 466 /* 467 * Handle ioctl commands to obtain information from the cache 468 */ 469 int 470 mrt_ioctl(struct socket *so, u_long cmd, caddr_t data) 471 { 472 int error; 473 474 if (so != ip_mrouter) 475 error = EINVAL; 476 else 477 switch (cmd) { 478 case SIOCGETVIFCNT: 479 error = get_vif_cnt((struct sioc_vif_req *)data); 480 break; 481 case SIOCGETSGCNT: 482 error = get_sg_cnt((struct sioc_sg_req *)data); 483 break; 484 default: 485 error = ENOTTY; 486 break; 487 } 488 489 return (error); 490 } 491 492 /* 493 * returns the packet, byte, rpf-failure count for the source group provided 494 */ 495 static int 496 get_sg_cnt(struct sioc_sg_req *req) 497 { 498 int s; 499 struct mfc *rt; 500 501 s = splsoftnet(); 502 rt = mfc_find(&req->src, &req->grp); 503 if (rt == NULL) { 504 splx(s); 505 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff; 506 return (EADDRNOTAVAIL); 507 } 508 req->pktcnt = rt->mfc_pkt_cnt; 509 req->bytecnt = rt->mfc_byte_cnt; 510 req->wrong_if = rt->mfc_wrong_if; 511 splx(s); 512 513 return (0); 514 } 515 516 /* 517 * returns the input and output packet and byte counts on the vif provided 518 */ 519 static int 520 get_vif_cnt(struct sioc_vif_req *req) 521 { 522 vifi_t vifi = req->vifi; 523 524 if (vifi >= numvifs) 525 return (EINVAL); 526 527 req->icount = viftable[vifi].v_pkt_in; 528 req->ocount = viftable[vifi].v_pkt_out; 529 req->ibytes = viftable[vifi].v_bytes_in; 530 req->obytes = viftable[vifi].v_bytes_out; 531 532 return (0); 533 } 534 535 /* 536 * Enable multicast routing 537 */ 538 static int 539 ip_mrouter_init(struct socket *so, struct mbuf *m) 540 { 541 int *v; 542 543 if (mrtdebug) 544 log(LOG_DEBUG, 545 "ip_mrouter_init: so_type = %d, pr_protocol = %d\n", 546 so->so_type, so->so_proto->pr_protocol); 547 548 if (so->so_type != SOCK_RAW || 549 so->so_proto->pr_protocol != IPPROTO_IGMP) 550 return (EOPNOTSUPP); 551 552 if (m == NULL || m->m_len < sizeof(int)) 553 return (EINVAL); 554 555 v = mtod(m, int *); 556 if (*v != 1) 557 return (EINVAL); 558 559 if (ip_mrouter != NULL) 560 return (EADDRINUSE); 561 562 ip_mrouter = so; 563 564 mfchashtbl = hashinit(MFCTBLSIZ, M_MRTABLE, M_WAITOK, &mfchash); 565 bzero((caddr_t)nexpire, sizeof(nexpire)); 566 567 pim_assert = 0; 568 569 timeout_set(&expire_upcalls_ch, expire_upcalls, NULL); 570 timeout_add_msec(&expire_upcalls_ch, EXPIRE_TIMEOUT); 571 572 timeout_set(&bw_upcalls_ch, expire_bw_upcalls_send, NULL); 573 timeout_add_msec(&bw_upcalls_ch, BW_UPCALLS_PERIOD); 574 575 timeout_set(&bw_meter_ch, expire_bw_meter_process, NULL); 576 timeout_add_msec(&bw_meter_ch, BW_METER_PERIOD); 577 578 if (mrtdebug) 579 log(LOG_DEBUG, "ip_mrouter_init\n"); 580 581 return (0); 582 } 583 584 /* 585 * Disable multicast routing 586 */ 587 int 588 ip_mrouter_done() 589 { 590 vifi_t vifi; 591 struct vif *vifp; 592 int i; 593 int s; 594 595 s = splsoftnet(); 596 597 /* Clear out all the vifs currently in use. */ 598 for (vifi = 0; vifi < numvifs; vifi++) { 599 vifp = &viftable[vifi]; 600 if (!in_nullhost(vifp->v_lcl_addr)) 601 reset_vif(vifp); 602 } 603 604 numvifs = 0; 605 pim_assert = 0; 606 mrt_api_config = 0; 607 608 timeout_del(&expire_upcalls_ch); 609 timeout_del(&bw_upcalls_ch); 610 timeout_del(&bw_meter_ch); 611 612 /* 613 * Free all multicast forwarding cache entries. 614 */ 615 for (i = 0; i < MFCTBLSIZ; i++) { 616 struct mfc *rt, *nrt; 617 618 for (rt = LIST_FIRST(&mfchashtbl[i]); rt; rt = nrt) { 619 nrt = LIST_NEXT(rt, mfc_hash); 620 621 expire_mfc(rt); 622 } 623 } 624 625 bzero((caddr_t)nexpire, sizeof(nexpire)); 626 free(mfchashtbl, M_MRTABLE); 627 mfchashtbl = NULL; 628 629 bw_upcalls_n = 0; 630 bzero(bw_meter_timers, sizeof(bw_meter_timers)); 631 632 /* Reset de-encapsulation cache. */ 633 have_encap_tunnel = 0; 634 635 ip_mrouter = NULL; 636 637 splx(s); 638 639 if (mrtdebug) 640 log(LOG_DEBUG, "ip_mrouter_done\n"); 641 642 return (0); 643 } 644 645 void 646 ip_mrouter_detach(struct ifnet *ifp) 647 { 648 int vifi, i; 649 struct vif *vifp; 650 struct mfc *rt; 651 struct rtdetq *rte; 652 653 /* XXX not sure about side effect to userland routing daemon */ 654 for (vifi = 0; vifi < numvifs; vifi++) { 655 vifp = &viftable[vifi]; 656 if (vifp->v_ifp == ifp) 657 reset_vif(vifp); 658 } 659 for (i = 0; i < MFCTBLSIZ; i++) { 660 if (nexpire[i] == 0) 661 continue; 662 LIST_FOREACH(rt, &mfchashtbl[i], mfc_hash) { 663 for (rte = rt->mfc_stall; rte; rte = rte->next) { 664 if (rte->ifp == ifp) 665 rte->ifp = NULL; 666 } 667 } 668 } 669 } 670 671 static int 672 get_version(struct mbuf *m) 673 { 674 int *v = mtod(m, int *); 675 676 *v = 0x0305; /* XXX !!!! */ 677 m->m_len = sizeof(int); 678 return (0); 679 } 680 681 /* 682 * Set PIM assert processing global 683 */ 684 static int 685 set_assert(struct mbuf *m) 686 { 687 int *i; 688 689 if (m == NULL || m->m_len < sizeof(int)) 690 return (EINVAL); 691 692 i = mtod(m, int *); 693 pim_assert = !!*i; 694 return (0); 695 } 696 697 /* 698 * Get PIM assert processing global 699 */ 700 static int 701 get_assert(struct mbuf *m) 702 { 703 int *i = mtod(m, int *); 704 705 *i = pim_assert; 706 m->m_len = sizeof(int); 707 return (0); 708 } 709 710 /* 711 * Configure API capabilities 712 */ 713 static int 714 set_api_config(struct mbuf *m) 715 { 716 int i; 717 u_int32_t *apival; 718 719 if (m == NULL || m->m_len < sizeof(u_int32_t)) 720 return (EINVAL); 721 722 apival = mtod(m, u_int32_t *); 723 724 /* 725 * We can set the API capabilities only if it is the first operation 726 * after MRT_INIT. I.e.: 727 * - there are no vifs installed 728 * - pim_assert is not enabled 729 * - the MFC table is empty 730 */ 731 if (numvifs > 0) { 732 *apival = 0; 733 return (EPERM); 734 } 735 if (pim_assert) { 736 *apival = 0; 737 return (EPERM); 738 } 739 for (i = 0; i < MFCTBLSIZ; i++) { 740 if (LIST_FIRST(&mfchashtbl[i]) != NULL) { 741 *apival = 0; 742 return (EPERM); 743 } 744 } 745 746 mrt_api_config = *apival & mrt_api_support; 747 *apival = mrt_api_config; 748 749 return (0); 750 } 751 752 /* 753 * Get API capabilities 754 */ 755 static int 756 get_api_support(struct mbuf *m) 757 { 758 u_int32_t *apival; 759 760 if (m == NULL || m->m_len < sizeof(u_int32_t)) 761 return (EINVAL); 762 763 apival = mtod(m, u_int32_t *); 764 765 *apival = mrt_api_support; 766 767 return (0); 768 } 769 770 /* 771 * Get API configured capabilities 772 */ 773 static int 774 get_api_config(struct mbuf *m) 775 { 776 u_int32_t *apival; 777 778 if (m == NULL || m->m_len < sizeof(u_int32_t)) 779 return (EINVAL); 780 781 apival = mtod(m, u_int32_t *); 782 783 *apival = mrt_api_config; 784 785 return (0); 786 } 787 788 static struct sockaddr_in sin = { sizeof(sin), AF_INET }; 789 790 /* 791 * Add a vif to the vif table 792 */ 793 static int 794 add_vif(struct mbuf *m) 795 { 796 struct vifctl *vifcp; 797 struct vif *vifp; 798 struct ifaddr *ifa; 799 struct ifnet *ifp; 800 struct ifreq ifr; 801 int error, s; 802 803 if (m == NULL || m->m_len < sizeof(struct vifctl)) 804 return (EINVAL); 805 806 vifcp = mtod(m, struct vifctl *); 807 if (vifcp->vifc_vifi >= MAXVIFS) 808 return (EINVAL); 809 if (in_nullhost(vifcp->vifc_lcl_addr)) 810 return (EADDRNOTAVAIL); 811 812 vifp = &viftable[vifcp->vifc_vifi]; 813 if (!in_nullhost(vifp->v_lcl_addr)) 814 return (EADDRINUSE); 815 816 /* Find the interface with an address in AF_INET family. */ 817 #ifdef PIM 818 if (vifcp->vifc_flags & VIFF_REGISTER) { 819 /* 820 * XXX: Because VIFF_REGISTER does not really need a valid 821 * local interface (e.g. it could be 127.0.0.2), we don't 822 * check its address. 823 */ 824 } else 825 #endif 826 { 827 sin.sin_addr = vifcp->vifc_lcl_addr; 828 ifa = ifa_ifwithaddr(sintosa(&sin), /* XXX */ 0); 829 if (ifa == NULL) 830 return (EADDRNOTAVAIL); 831 } 832 833 if (vifcp->vifc_flags & VIFF_TUNNEL) { 834 /* tunnels are no longer supported use gif(4) instead */ 835 return (EOPNOTSUPP); 836 #ifdef PIM 837 } else if (vifcp->vifc_flags & VIFF_REGISTER) { 838 ifp = &multicast_register_if; 839 if (mrtdebug) 840 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n", 841 (void *)ifp); 842 if (reg_vif_num == VIFI_INVALID) { 843 bzero(ifp, sizeof(*ifp)); 844 snprintf(ifp->if_xname, sizeof ifp->if_xname, 845 "register_vif"); 846 ifp->if_flags = IFF_LOOPBACK; 847 bzero(&vifp->v_route, sizeof(vifp->v_route)); 848 reg_vif_num = vifcp->vifc_vifi; 849 } 850 #endif 851 } else { 852 /* Use the physical interface associated with the address. */ 853 ifp = ifa->ifa_ifp; 854 855 /* Make sure the interface supports multicast. */ 856 if ((ifp->if_flags & IFF_MULTICAST) == 0) 857 return (EOPNOTSUPP); 858 859 /* Enable promiscuous reception of all IP multicasts. */ 860 satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in); 861 satosin(&ifr.ifr_addr)->sin_family = AF_INET; 862 satosin(&ifr.ifr_addr)->sin_addr = zeroin_addr; 863 error = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, (caddr_t)&ifr); 864 if (error) 865 return (error); 866 } 867 868 s = splsoftnet(); 869 870 vifp->v_flags = vifcp->vifc_flags; 871 vifp->v_threshold = vifcp->vifc_threshold; 872 vifp->v_lcl_addr = vifcp->vifc_lcl_addr; 873 vifp->v_rmt_addr = vifcp->vifc_rmt_addr; 874 vifp->v_ifp = ifp; 875 /* Initialize per vif pkt counters. */ 876 vifp->v_pkt_in = 0; 877 vifp->v_pkt_out = 0; 878 vifp->v_bytes_in = 0; 879 vifp->v_bytes_out = 0; 880 881 timeout_del(&vifp->v_repq_ch); 882 883 #ifdef RSVP_ISI 884 vifp->v_rsvp_on = 0; 885 vifp->v_rsvpd = NULL; 886 #endif /* RSVP_ISI */ 887 888 splx(s); 889 890 /* Adjust numvifs up if the vifi is higher than numvifs. */ 891 if (numvifs <= vifcp->vifc_vifi) 892 numvifs = vifcp->vifc_vifi + 1; 893 894 if (mrtdebug) 895 log(LOG_DEBUG, "add_vif #%d, lcladdr %x, %s %x, " 896 "thresh %x\n", 897 vifcp->vifc_vifi, 898 ntohl(vifcp->vifc_lcl_addr.s_addr), 899 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask", 900 ntohl(vifcp->vifc_rmt_addr.s_addr), 901 vifcp->vifc_threshold); 902 903 return (0); 904 } 905 906 void 907 reset_vif(struct vif *vifp) 908 { 909 struct ifnet *ifp; 910 struct ifreq ifr; 911 912 if (vifp->v_flags & VIFF_TUNNEL) { 913 /* empty */ 914 } else if (vifp->v_flags & VIFF_REGISTER) { 915 #ifdef PIM 916 reg_vif_num = VIFI_INVALID; 917 #endif 918 } else { 919 satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in); 920 satosin(&ifr.ifr_addr)->sin_family = AF_INET; 921 satosin(&ifr.ifr_addr)->sin_addr = zeroin_addr; 922 ifp = vifp->v_ifp; 923 (*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr); 924 } 925 bzero((caddr_t)vifp, sizeof(*vifp)); 926 } 927 928 /* 929 * Delete a vif from the vif table 930 */ 931 static int 932 del_vif(struct mbuf *m) 933 { 934 vifi_t *vifip; 935 struct vif *vifp; 936 vifi_t vifi; 937 int s; 938 939 if (m == NULL || m->m_len < sizeof(vifi_t)) 940 return (EINVAL); 941 942 vifip = mtod(m, vifi_t *); 943 if (*vifip >= numvifs) 944 return (EINVAL); 945 946 vifp = &viftable[*vifip]; 947 if (in_nullhost(vifp->v_lcl_addr)) 948 return (EADDRNOTAVAIL); 949 950 s = splsoftnet(); 951 952 reset_vif(vifp); 953 954 /* Adjust numvifs down */ 955 for (vifi = numvifs; vifi > 0; vifi--) 956 if (!in_nullhost(viftable[vifi - 1].v_lcl_addr)) 957 break; 958 numvifs = vifi; 959 960 splx(s); 961 962 if (mrtdebug) 963 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", *vifip, numvifs); 964 965 return (0); 966 } 967 968 void 969 vif_delete(struct ifnet *ifp) 970 { 971 int i; 972 struct vif *vifp; 973 struct mfc *rt; 974 struct rtdetq *rte; 975 976 for (i = 0; i < numvifs; i++) { 977 vifp = &viftable[i]; 978 if (vifp->v_ifp == ifp) 979 bzero((caddr_t)vifp, sizeof *vifp); 980 } 981 982 for (i = numvifs; i > 0; i--) 983 if (!in_nullhost(viftable[i - 1].v_lcl_addr)) 984 break; 985 numvifs = i; 986 987 for (i = 0; i < MFCTBLSIZ; i++) { 988 if (nexpire[i] == 0) 989 continue; 990 LIST_FOREACH(rt, &mfchashtbl[i], mfc_hash) { 991 for (rte = rt->mfc_stall; rte; rte = rte->next) { 992 if (rte->ifp == ifp) 993 rte->ifp = NULL; 994 } 995 } 996 } 997 } 998 999 /* 1000 * update an mfc entry without resetting counters and S,G addresses. 1001 */ 1002 static void 1003 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp) 1004 { 1005 int i; 1006 1007 rt->mfc_parent = mfccp->mfcc_parent; 1008 for (i = 0; i < numvifs; i++) { 1009 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i]; 1010 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config & 1011 MRT_MFC_FLAGS_ALL; 1012 } 1013 /* set the RP address */ 1014 if (mrt_api_config & MRT_MFC_RP) 1015 rt->mfc_rp = mfccp->mfcc_rp; 1016 else 1017 rt->mfc_rp = zeroin_addr; 1018 } 1019 1020 /* 1021 * fully initialize an mfc entry from the parameter. 1022 */ 1023 static void 1024 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp) 1025 { 1026 rt->mfc_origin = mfccp->mfcc_origin; 1027 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp; 1028 1029 update_mfc_params(rt, mfccp); 1030 1031 /* initialize pkt counters per src-grp */ 1032 rt->mfc_pkt_cnt = 0; 1033 rt->mfc_byte_cnt = 0; 1034 rt->mfc_wrong_if = 0; 1035 timerclear(&rt->mfc_last_assert); 1036 } 1037 1038 static void 1039 expire_mfc(struct mfc *rt) 1040 { 1041 struct rtdetq *rte, *nrte; 1042 1043 free_bw_list(rt->mfc_bw_meter); 1044 1045 for (rte = rt->mfc_stall; rte != NULL; rte = nrte) { 1046 nrte = rte->next; 1047 m_freem(rte->m); 1048 free(rte, M_MRTABLE); 1049 } 1050 1051 LIST_REMOVE(rt, mfc_hash); 1052 free(rt, M_MRTABLE); 1053 } 1054 1055 /* 1056 * Add an mfc entry 1057 */ 1058 static int 1059 add_mfc(struct mbuf *m) 1060 { 1061 struct mfcctl2 mfcctl2; 1062 struct mfcctl2 *mfccp; 1063 struct mfc *rt; 1064 u_int32_t hash = 0; 1065 struct rtdetq *rte, *nrte; 1066 u_short nstl; 1067 int s; 1068 int mfcctl_size = sizeof(struct mfcctl); 1069 1070 if (mrt_api_config & MRT_API_FLAGS_ALL) 1071 mfcctl_size = sizeof(struct mfcctl2); 1072 1073 if (m == NULL || m->m_len < mfcctl_size) 1074 return (EINVAL); 1075 1076 /* 1077 * select data size depending on API version. 1078 */ 1079 if (mrt_api_config & MRT_API_FLAGS_ALL) { 1080 struct mfcctl2 *mp2 = mtod(m, struct mfcctl2 *); 1081 bcopy(mp2, (caddr_t)&mfcctl2, sizeof(*mp2)); 1082 } else { 1083 struct mfcctl *mp = mtod(m, struct mfcctl *); 1084 bcopy(mp, (caddr_t)&mfcctl2, sizeof(*mp)); 1085 bzero((caddr_t)&mfcctl2 + sizeof(struct mfcctl), 1086 sizeof(mfcctl2) - sizeof(struct mfcctl)); 1087 } 1088 mfccp = &mfcctl2; 1089 1090 s = splsoftnet(); 1091 rt = mfc_find(&mfccp->mfcc_origin, &mfccp->mfcc_mcastgrp); 1092 1093 /* If an entry already exists, just update the fields */ 1094 if (rt) { 1095 if (mrtdebug & DEBUG_MFC) 1096 log(LOG_DEBUG, "add_mfc update o %x g %x p %x\n", 1097 ntohl(mfccp->mfcc_origin.s_addr), 1098 ntohl(mfccp->mfcc_mcastgrp.s_addr), 1099 mfccp->mfcc_parent); 1100 1101 update_mfc_params(rt, mfccp); 1102 1103 splx(s); 1104 return (0); 1105 } 1106 1107 /* 1108 * Find the entry for which the upcall was made and update 1109 */ 1110 nstl = 0; 1111 hash = MFCHASH(mfccp->mfcc_origin, mfccp->mfcc_mcastgrp); 1112 LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) { 1113 if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) && 1114 in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp) && 1115 rt->mfc_stall != NULL) { 1116 if (nstl++) 1117 log(LOG_ERR, "add_mfc %s o %x g %x " 1118 "p %x dbx %p\n", 1119 "multiple kernel entries", 1120 ntohl(mfccp->mfcc_origin.s_addr), 1121 ntohl(mfccp->mfcc_mcastgrp.s_addr), 1122 mfccp->mfcc_parent, rt->mfc_stall); 1123 1124 if (mrtdebug & DEBUG_MFC) 1125 log(LOG_DEBUG, "add_mfc o %x g %x " 1126 "p %x dbg %p\n", 1127 ntohl(mfccp->mfcc_origin.s_addr), 1128 ntohl(mfccp->mfcc_mcastgrp.s_addr), 1129 mfccp->mfcc_parent, rt->mfc_stall); 1130 1131 rte = rt->mfc_stall; 1132 init_mfc_params(rt, mfccp); 1133 rt->mfc_stall = NULL; 1134 1135 rt->mfc_expire = 0; /* Don't clean this guy up */ 1136 nexpire[hash]--; 1137 1138 /* free packets Qed at the end of this entry */ 1139 for (; rte != NULL; rte = nrte) { 1140 nrte = rte->next; 1141 if (rte->ifp) { 1142 #ifdef RSVP_ISI 1143 ip_mdq(rte->m, rte->ifp, rt, -1); 1144 #else 1145 ip_mdq(rte->m, rte->ifp, rt); 1146 #endif /* RSVP_ISI */ 1147 } 1148 m_freem(rte->m); 1149 #ifdef UPCALL_TIMING 1150 collate(&rte->t); 1151 #endif /* UPCALL_TIMING */ 1152 free(rte, M_MRTABLE); 1153 } 1154 } 1155 } 1156 1157 /* 1158 * It is possible that an entry is being inserted without an upcall 1159 */ 1160 if (nstl == 0) { 1161 /* 1162 * No mfc; make a new one 1163 */ 1164 if (mrtdebug & DEBUG_MFC) 1165 log(LOG_DEBUG, "add_mfc no upcall o %x g %x p %x\n", 1166 ntohl(mfccp->mfcc_origin.s_addr), 1167 ntohl(mfccp->mfcc_mcastgrp.s_addr), 1168 mfccp->mfcc_parent); 1169 1170 LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) { 1171 if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) && 1172 in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp)) { 1173 init_mfc_params(rt, mfccp); 1174 if (rt->mfc_expire) 1175 nexpire[hash]--; 1176 rt->mfc_expire = 0; 1177 break; /* XXX */ 1178 } 1179 } 1180 if (rt == NULL) { /* no upcall, so make a new entry */ 1181 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, 1182 M_NOWAIT); 1183 if (rt == NULL) { 1184 splx(s); 1185 return (ENOBUFS); 1186 } 1187 1188 init_mfc_params(rt, mfccp); 1189 rt->mfc_expire = 0; 1190 rt->mfc_stall = NULL; 1191 rt->mfc_bw_meter = NULL; 1192 1193 /* insert new entry at head of hash chain */ 1194 LIST_INSERT_HEAD(&mfchashtbl[hash], rt, mfc_hash); 1195 } 1196 } 1197 1198 splx(s); 1199 return (0); 1200 } 1201 1202 #ifdef UPCALL_TIMING 1203 /* 1204 * collect delay statistics on the upcalls 1205 */ 1206 static void 1207 collate(struct timeval *t) 1208 { 1209 u_int32_t d; 1210 struct timeval tp; 1211 u_int32_t delta; 1212 1213 microtime(&tp); 1214 1215 if (timercmp(t, &tp, <)) { 1216 TV_DELTA(tp, *t, delta); 1217 1218 d = delta >> 10; 1219 if (d > 50) 1220 d = 50; 1221 1222 ++upcall_data[d]; 1223 } 1224 } 1225 #endif /* UPCALL_TIMING */ 1226 1227 /* 1228 * Delete an mfc entry 1229 */ 1230 static int 1231 del_mfc(struct mbuf *m) 1232 { 1233 struct mfcctl2 mfcctl2; 1234 struct mfcctl2 *mfccp; 1235 struct mfc *rt; 1236 int s; 1237 int mfcctl_size = sizeof(struct mfcctl); 1238 struct mfcctl *mp = mtod(m, struct mfcctl *); 1239 1240 /* 1241 * XXX: for deleting MFC entries the information in entries 1242 * of size "struct mfcctl" is sufficient. 1243 */ 1244 1245 if (m == NULL || m->m_len < mfcctl_size) 1246 return (EINVAL); 1247 1248 bcopy(mp, (caddr_t)&mfcctl2, sizeof(*mp)); 1249 bzero((caddr_t)&mfcctl2 + sizeof(struct mfcctl), 1250 sizeof(mfcctl2) - sizeof(struct mfcctl)); 1251 1252 mfccp = &mfcctl2; 1253 1254 if (mrtdebug & DEBUG_MFC) 1255 log(LOG_DEBUG, "del_mfc origin %x mcastgrp %x\n", 1256 ntohl(mfccp->mfcc_origin.s_addr), 1257 ntohl(mfccp->mfcc_mcastgrp.s_addr)); 1258 1259 s = splsoftnet(); 1260 1261 rt = mfc_find(&mfccp->mfcc_origin, &mfccp->mfcc_mcastgrp); 1262 if (rt == NULL) { 1263 splx(s); 1264 return (EADDRNOTAVAIL); 1265 } 1266 1267 /* 1268 * free the bw_meter entries 1269 */ 1270 free_bw_list(rt->mfc_bw_meter); 1271 rt->mfc_bw_meter = NULL; 1272 1273 LIST_REMOVE(rt, mfc_hash); 1274 free(rt, M_MRTABLE); 1275 1276 splx(s); 1277 return (0); 1278 } 1279 1280 static int 1281 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src) 1282 { 1283 if (s != NULL) { 1284 if (sbappendaddr(&s->so_rcv, sintosa(src), mm, 1285 (struct mbuf *)NULL) != 0) { 1286 sorwakeup(s); 1287 return (0); 1288 } 1289 } 1290 m_freem(mm); 1291 return (-1); 1292 } 1293 1294 /* 1295 * IP multicast forwarding function. This function assumes that the packet 1296 * pointed to by "ip" has arrived on (or is about to be sent to) the interface 1297 * pointed to by "ifp", and the packet is to be relayed to other networks 1298 * that have members of the packet's destination IP multicast group. 1299 * 1300 * The packet is returned unscathed to the caller, unless it is 1301 * erroneous, in which case a non-zero return value tells the caller to 1302 * discard it. 1303 */ 1304 1305 #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */ 1306 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */ 1307 1308 int 1309 #ifdef RSVP_ISI 1310 ip_mforward(struct mbuf *m, struct ifnet *ifp, struct ip_moptions *imo) 1311 #else 1312 ip_mforward(struct mbuf *m, struct ifnet *ifp) 1313 #endif /* RSVP_ISI */ 1314 { 1315 struct ip *ip = mtod(m, struct ip *); 1316 struct mfc *rt; 1317 static int srctun = 0; 1318 struct mbuf *mm; 1319 int s; 1320 vifi_t vifi; 1321 1322 if (mrtdebug & DEBUG_FORWARD) 1323 log(LOG_DEBUG, "ip_mforward: src %x, dst %x, ifp %p\n", 1324 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr), ifp); 1325 1326 if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 || 1327 ((u_char *)(ip + 1))[1] != IPOPT_LSRR) { 1328 /* 1329 * Packet arrived via a physical interface or 1330 * an encapsulated tunnel or a register_vif. 1331 */ 1332 } else { 1333 /* 1334 * Packet arrived through a source-route tunnel. 1335 * Source-route tunnels are no longer supported. 1336 */ 1337 if ((srctun++ % 1000) == 0) 1338 log(LOG_ERR, "ip_mforward: received source-routed " 1339 "packet from %x\n", ntohl(ip->ip_src.s_addr)); 1340 1341 return (1); 1342 } 1343 1344 #ifdef RSVP_ISI 1345 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) { 1346 if (ip->ip_ttl < MAXTTL) { 1347 /* compensate for -1 in *_send routines */ 1348 ip->ip_ttl++; 1349 } 1350 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) { 1351 struct vif *vifp = viftable + vifi; 1352 printf("Sending IPPROTO_RSVP from %x to %x on " 1353 "vif %d (%s%s)\n", 1354 ntohl(ip->ip_src), ntohl(ip->ip_dst), vifi, 1355 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "", 1356 vifp->v_ifp->if_xname); 1357 } 1358 return (ip_mdq(m, ifp, (struct mfc *)NULL, vifi)); 1359 } 1360 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) { 1361 printf("Warning: IPPROTO_RSVP from %x to %x without " 1362 "vif option\n", ntohl(ip->ip_src), ntohl(ip->ip_dst)); 1363 } 1364 #endif /* RSVP_ISI */ 1365 1366 /* 1367 * Don't forward a packet with time-to-live of zero or one, 1368 * or a packet destined to a local-only group. 1369 */ 1370 if (ip->ip_ttl <= 1 || IN_LOCAL_GROUP(ip->ip_dst.s_addr)) 1371 return (0); 1372 1373 /* 1374 * Determine forwarding vifs from the forwarding cache table 1375 */ 1376 s = splsoftnet(); 1377 ++mrtstat.mrts_mfc_lookups; 1378 rt = mfc_find(&ip->ip_src, &ip->ip_dst); 1379 1380 /* Entry exists, so forward if necessary */ 1381 if (rt != NULL) { 1382 splx(s); 1383 #ifdef RSVP_ISI 1384 return (ip_mdq(m, ifp, rt, -1)); 1385 #else 1386 return (ip_mdq(m, ifp, rt)); 1387 #endif /* RSVP_ISI */ 1388 } else { 1389 /* 1390 * If we don't have a route for packet's origin, 1391 * Make a copy of the packet & send message to routing daemon 1392 */ 1393 1394 struct mbuf *mb0; 1395 struct rtdetq *rte; 1396 u_int32_t hash; 1397 int hlen = ip->ip_hl << 2; 1398 #ifdef UPCALL_TIMING 1399 struct timeval tp; 1400 1401 microtime(&tp); 1402 #endif /* UPCALL_TIMING */ 1403 1404 ++mrtstat.mrts_mfc_misses; 1405 1406 mrtstat.mrts_no_route++; 1407 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC)) 1408 log(LOG_DEBUG, "ip_mforward: no rte s %x g %x\n", 1409 ntohl(ip->ip_src.s_addr), 1410 ntohl(ip->ip_dst.s_addr)); 1411 1412 /* 1413 * Allocate mbufs early so that we don't do extra work if we are 1414 * just going to fail anyway. Make sure to pullup the header so 1415 * that other people can't step on it. 1416 */ 1417 rte = (struct rtdetq *)malloc(sizeof(*rte), 1418 M_MRTABLE, M_NOWAIT); 1419 if (rte == NULL) { 1420 splx(s); 1421 return (ENOBUFS); 1422 } 1423 mb0 = m_copy(m, 0, M_COPYALL); 1424 M_PULLUP(mb0, hlen); 1425 if (mb0 == NULL) { 1426 free(rte, M_MRTABLE); 1427 splx(s); 1428 return (ENOBUFS); 1429 } 1430 1431 /* is there an upcall waiting for this flow? */ 1432 hash = MFCHASH(ip->ip_src, ip->ip_dst); 1433 LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) { 1434 if (in_hosteq(ip->ip_src, rt->mfc_origin) && 1435 in_hosteq(ip->ip_dst, rt->mfc_mcastgrp) && 1436 rt->mfc_stall != NULL) 1437 break; 1438 } 1439 1440 if (rt == NULL) { 1441 int i; 1442 struct igmpmsg *im; 1443 1444 /* 1445 * Locate the vifi for the incoming interface for 1446 * this packet. 1447 * If none found, drop packet. 1448 */ 1449 for (vifi = 0; vifi < numvifs && 1450 viftable[vifi].v_ifp != ifp; vifi++) 1451 ; 1452 if (vifi >= numvifs) /* vif not found, drop packet */ 1453 goto non_fatal; 1454 1455 /* no upcall, so make a new entry */ 1456 rt = (struct mfc *)malloc(sizeof(*rt), 1457 M_MRTABLE, M_NOWAIT); 1458 if (rt == NULL) 1459 goto fail; 1460 /* 1461 * Make a copy of the header to send to the user level 1462 * process 1463 */ 1464 mm = m_copy(m, 0, hlen); 1465 M_PULLUP(mm, hlen); 1466 if (mm == NULL) 1467 goto fail1; 1468 1469 /* 1470 * Send message to routing daemon to install 1471 * a route into the kernel table 1472 */ 1473 1474 im = mtod(mm, struct igmpmsg *); 1475 im->im_msgtype = IGMPMSG_NOCACHE; 1476 im->im_mbz = 0; 1477 im->im_vif = vifi; 1478 1479 mrtstat.mrts_upcalls++; 1480 1481 sin.sin_addr = ip->ip_src; 1482 if (socket_send(ip_mrouter, mm, &sin) < 0) { 1483 log(LOG_WARNING, "ip_mforward: ip_mrouter " 1484 "socket queue full\n"); 1485 ++mrtstat.mrts_upq_sockfull; 1486 fail1: 1487 free(rt, M_MRTABLE); 1488 fail: 1489 free(rte, M_MRTABLE); 1490 m_freem(mb0); 1491 splx(s); 1492 return (ENOBUFS); 1493 } 1494 1495 /* insert new entry at head of hash chain */ 1496 rt->mfc_origin = ip->ip_src; 1497 rt->mfc_mcastgrp = ip->ip_dst; 1498 rt->mfc_pkt_cnt = 0; 1499 rt->mfc_byte_cnt = 0; 1500 rt->mfc_wrong_if = 0; 1501 rt->mfc_expire = UPCALL_EXPIRE; 1502 nexpire[hash]++; 1503 for (i = 0; i < numvifs; i++) { 1504 rt->mfc_ttls[i] = 0; 1505 rt->mfc_flags[i] = 0; 1506 } 1507 rt->mfc_parent = -1; 1508 1509 /* clear the RP address */ 1510 rt->mfc_rp = zeroin_addr; 1511 1512 rt->mfc_bw_meter = NULL; 1513 1514 /* link into table */ 1515 LIST_INSERT_HEAD(&mfchashtbl[hash], rt, mfc_hash); 1516 /* Add this entry to the end of the queue */ 1517 rt->mfc_stall = rte; 1518 } else { 1519 /* determine if q has overflowed */ 1520 struct rtdetq **p; 1521 int npkts = 0; 1522 1523 /* 1524 * XXX ouch! we need to append to the list, but we 1525 * only have a pointer to the front, so we have to 1526 * scan the entire list every time. 1527 */ 1528 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next) 1529 if (++npkts > MAX_UPQ) { 1530 mrtstat.mrts_upq_ovflw++; 1531 non_fatal: 1532 free(rte, M_MRTABLE); 1533 m_freem(mb0); 1534 splx(s); 1535 return (0); 1536 } 1537 1538 /* Add this entry to the end of the queue */ 1539 *p = rte; 1540 } 1541 1542 rte->next = NULL; 1543 rte->m = mb0; 1544 rte->ifp = ifp; 1545 #ifdef UPCALL_TIMING 1546 rte->t = tp; 1547 #endif /* UPCALL_TIMING */ 1548 1549 splx(s); 1550 1551 return (0); 1552 } 1553 } 1554 1555 1556 /*ARGSUSED*/ 1557 static void 1558 expire_upcalls(void *v) 1559 { 1560 int i; 1561 int s; 1562 1563 s = splsoftnet(); 1564 1565 for (i = 0; i < MFCTBLSIZ; i++) { 1566 struct mfc *rt, *nrt; 1567 1568 if (nexpire[i] == 0) 1569 continue; 1570 1571 for (rt = LIST_FIRST(&mfchashtbl[i]); rt; rt = nrt) { 1572 nrt = LIST_NEXT(rt, mfc_hash); 1573 1574 if (rt->mfc_expire == 0 || --rt->mfc_expire > 0) 1575 continue; 1576 nexpire[i]--; 1577 1578 /* 1579 * free the bw_meter entries 1580 */ 1581 while (rt->mfc_bw_meter != NULL) { 1582 struct bw_meter *x = rt->mfc_bw_meter; 1583 1584 rt->mfc_bw_meter = x->bm_mfc_next; 1585 free(x, M_BWMETER); 1586 } 1587 1588 ++mrtstat.mrts_cache_cleanups; 1589 if (mrtdebug & DEBUG_EXPIRE) 1590 log(LOG_DEBUG, 1591 "expire_upcalls: expiring (%x %x)\n", 1592 ntohl(rt->mfc_origin.s_addr), 1593 ntohl(rt->mfc_mcastgrp.s_addr)); 1594 1595 expire_mfc(rt); 1596 } 1597 } 1598 1599 splx(s); 1600 timeout_add_msec(&expire_upcalls_ch, EXPIRE_TIMEOUT); 1601 } 1602 1603 /* 1604 * Packet forwarding routine once entry in the cache is made 1605 */ 1606 static int 1607 #ifdef RSVP_ISI 1608 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif) 1609 #else 1610 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt) 1611 #endif /* RSVP_ISI */ 1612 { 1613 struct ip *ip = mtod(m, struct ip *); 1614 vifi_t vifi; 1615 struct vif *vifp; 1616 int plen = ntohs(ip->ip_len) - (ip->ip_hl << 2); 1617 1618 /* 1619 * Macro to send packet on vif. Since RSVP packets don't get counted on 1620 * input, they shouldn't get counted on output, so statistics keeping is 1621 * separate. 1622 */ 1623 #define MC_SEND(ip, vifp, m) do { \ 1624 if ((vifp)->v_flags & VIFF_TUNNEL) \ 1625 encap_send((ip), (vifp), (m)); \ 1626 else \ 1627 phyint_send((ip), (vifp), (m)); \ 1628 } while (/*CONSTCOND*/ 0) 1629 1630 #ifdef RSVP_ISI 1631 /* 1632 * If xmt_vif is not -1, send on only the requested vif. 1633 * 1634 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs. 1635 */ 1636 if (xmt_vif < numvifs) { 1637 #ifdef PIM 1638 if (viftable[xmt_vif].v_flags & VIFF_REGISTER) 1639 pim_register_send(ip, viftable + xmt_vif, m, rt); 1640 else 1641 #endif 1642 MC_SEND(ip, viftable + xmt_vif, m); 1643 return (1); 1644 } 1645 #endif /* RSVP_ISI */ 1646 1647 /* 1648 * Don't forward if it didn't arrive from the parent vif for its origin. 1649 */ 1650 vifi = rt->mfc_parent; 1651 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) { 1652 /* came in the wrong interface */ 1653 if (mrtdebug & DEBUG_FORWARD) 1654 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n", 1655 ifp, vifi, 1656 vifi >= numvifs ? 0 : viftable[vifi].v_ifp); 1657 ++mrtstat.mrts_wrong_if; 1658 ++rt->mfc_wrong_if; 1659 /* 1660 * If we are doing PIM assert processing, send a message 1661 * to the routing daemon. 1662 * 1663 * XXX: A PIM-SM router needs the WRONGVIF detection so it 1664 * can complete the SPT switch, regardless of the type 1665 * of interface (broadcast media, GRE tunnel, etc). 1666 */ 1667 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) { 1668 struct timeval now; 1669 u_int32_t delta; 1670 1671 #ifdef PIM 1672 if (ifp == &multicast_register_if) 1673 pimstat.pims_rcv_registers_wrongiif++; 1674 #endif 1675 1676 /* Get vifi for the incoming packet */ 1677 for (vifi = 0; 1678 vifi < numvifs && viftable[vifi].v_ifp != ifp; 1679 vifi++) 1680 ; 1681 if (vifi >= numvifs) { 1682 /* The iif is not found: ignore the packet. */ 1683 return (0); 1684 } 1685 1686 if (rt->mfc_flags[vifi] & 1687 MRT_MFC_FLAGS_DISABLE_WRONGVIF) { 1688 /* WRONGVIF disabled: ignore the packet */ 1689 return (0); 1690 } 1691 1692 microtime(&now); 1693 1694 TV_DELTA(rt->mfc_last_assert, now, delta); 1695 1696 if (delta > ASSERT_MSG_TIME) { 1697 struct igmpmsg *im; 1698 int hlen = ip->ip_hl << 2; 1699 struct mbuf *mm = m_copy(m, 0, hlen); 1700 1701 M_PULLUP(mm, hlen); 1702 if (mm == NULL) 1703 return (ENOBUFS); 1704 1705 rt->mfc_last_assert = now; 1706 1707 im = mtod(mm, struct igmpmsg *); 1708 im->im_msgtype = IGMPMSG_WRONGVIF; 1709 im->im_mbz = 0; 1710 im->im_vif = vifi; 1711 1712 mrtstat.mrts_upcalls++; 1713 1714 sin.sin_addr = im->im_src; 1715 if (socket_send(ip_mrouter, mm, &sin) < 0) { 1716 log(LOG_WARNING, "ip_mforward: " 1717 "ip_mrouter socket queue full\n"); 1718 ++mrtstat.mrts_upq_sockfull; 1719 return (ENOBUFS); 1720 } 1721 } 1722 } 1723 return (0); 1724 } 1725 1726 /* If I sourced this packet, it counts as output, else it was input. */ 1727 if (in_hosteq(ip->ip_src, viftable[vifi].v_lcl_addr)) { 1728 viftable[vifi].v_pkt_out++; 1729 viftable[vifi].v_bytes_out += plen; 1730 } else { 1731 viftable[vifi].v_pkt_in++; 1732 viftable[vifi].v_bytes_in += plen; 1733 } 1734 rt->mfc_pkt_cnt++; 1735 rt->mfc_byte_cnt += plen; 1736 1737 /* 1738 * For each vif, decide if a copy of the packet should be forwarded. 1739 * Forward if: 1740 * - the ttl exceeds the vif's threshold 1741 * - there are group members downstream on interface 1742 */ 1743 for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++) 1744 if ((rt->mfc_ttls[vifi] > 0) && 1745 (ip->ip_ttl > rt->mfc_ttls[vifi])) { 1746 vifp->v_pkt_out++; 1747 vifp->v_bytes_out += plen; 1748 #ifdef PIM 1749 if (vifp->v_flags & VIFF_REGISTER) 1750 pim_register_send(ip, vifp, m, rt); 1751 else 1752 #endif 1753 MC_SEND(ip, vifp, m); 1754 } 1755 1756 /* 1757 * Perform upcall-related bw measuring. 1758 */ 1759 if (rt->mfc_bw_meter != NULL) { 1760 struct bw_meter *x; 1761 struct timeval now; 1762 1763 microtime(&now); 1764 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) 1765 bw_meter_receive_packet(x, plen, &now); 1766 } 1767 1768 return (0); 1769 } 1770 1771 #ifdef RSVP_ISI 1772 /* 1773 * check if a vif number is legal/ok. This is used by ip_output. 1774 */ 1775 int 1776 legal_vif_num(int vif) 1777 { 1778 if (vif >= 0 && vif < numvifs) 1779 return (1); 1780 else 1781 return (0); 1782 } 1783 #endif /* RSVP_ISI */ 1784 1785 static void 1786 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m) 1787 { 1788 struct mbuf *mb_copy; 1789 int hlen = ip->ip_hl << 2; 1790 1791 /* 1792 * Make a new reference to the packet; make sure that 1793 * the IP header is actually copied, not just referenced, 1794 * so that ip_output() only scribbles on the copy. 1795 */ 1796 mb_copy = m_copy(m, 0, M_COPYALL); 1797 M_PULLUP(mb_copy, hlen); 1798 if (mb_copy == NULL) 1799 return; 1800 1801 send_packet(vifp, mb_copy); 1802 } 1803 1804 static void 1805 encap_send(struct ip *ip, struct vif *vifp, struct mbuf *m) 1806 { 1807 struct mbuf *mb_copy; 1808 struct ip *ip_copy; 1809 int i, len = ntohs(ip->ip_len) + sizeof(multicast_encap_iphdr); 1810 1811 /* Take care of delayed checksums */ 1812 if (m->m_pkthdr.csum_flags & (M_TCPV4_CSUM_OUT | M_UDPV4_CSUM_OUT)) { 1813 in_delayed_cksum(m); 1814 m->m_pkthdr.csum_flags &= 1815 ~(M_UDPV4_CSUM_OUT | M_TCPV4_CSUM_OUT); 1816 } 1817 1818 /* 1819 * copy the old packet & pullup its IP header into the 1820 * new mbuf so we can modify it. Try to fill the new 1821 * mbuf since if we don't the ethernet driver will. 1822 */ 1823 MGETHDR(mb_copy, M_DONTWAIT, MT_DATA); 1824 if (mb_copy == NULL) 1825 return; 1826 mb_copy->m_data += max_linkhdr; 1827 mb_copy->m_pkthdr.len = len; 1828 mb_copy->m_len = sizeof(multicast_encap_iphdr); 1829 1830 if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == NULL) { 1831 m_freem(mb_copy); 1832 return; 1833 } 1834 i = MHLEN - max_linkhdr; 1835 if (i > len) 1836 i = len; 1837 mb_copy = m_pullup(mb_copy, i); 1838 if (mb_copy == NULL) 1839 return; 1840 1841 /* 1842 * fill in the encapsulating IP header. 1843 */ 1844 ip_copy = mtod(mb_copy, struct ip *); 1845 *ip_copy = multicast_encap_iphdr; 1846 ip_copy->ip_id = htons(ip_randomid()); 1847 ip_copy->ip_len = htons(len); 1848 ip_copy->ip_src = vifp->v_lcl_addr; 1849 ip_copy->ip_dst = vifp->v_rmt_addr; 1850 1851 /* 1852 * turn the encapsulated IP header back into a valid one. 1853 */ 1854 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr)); 1855 --ip->ip_ttl; 1856 ip->ip_sum = 0; 1857 mb_copy->m_data += sizeof(multicast_encap_iphdr); 1858 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2); 1859 mb_copy->m_data -= sizeof(multicast_encap_iphdr); 1860 1861 send_packet(vifp, mb_copy); 1862 } 1863 1864 static void 1865 send_packet(struct vif *vifp, struct mbuf *m) 1866 { 1867 int error; 1868 int s = splsoftnet(); 1869 1870 if (vifp->v_flags & VIFF_TUNNEL) { 1871 /* If tunnel options */ 1872 ip_output(m, (struct mbuf *)NULL, &vifp->v_route, 1873 IP_FORWARDING, (struct ip_moptions *)NULL, 1874 (struct inpcb *)NULL); 1875 } else { 1876 /* 1877 * if physical interface option, extract the options 1878 * and then send 1879 */ 1880 struct ip_moptions imo; 1881 1882 imo.imo_multicast_ifp = vifp->v_ifp; 1883 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - IPTTLDEC; 1884 imo.imo_multicast_loop = 1; 1885 #ifdef RSVP_ISI 1886 imo.imo_multicast_vif = -1; 1887 #endif 1888 1889 error = ip_output(m, (struct mbuf *)NULL, (struct route *)NULL, 1890 IP_FORWARDING|IP_MULTICASTOPTS, &imo, 1891 (struct inpcb *)NULL); 1892 1893 if (mrtdebug & DEBUG_XMIT) 1894 log(LOG_DEBUG, "phyint_send on vif %ld err %d\n", 1895 (long)(vifp - viftable), error); 1896 } 1897 splx(s); 1898 } 1899 1900 #ifdef RSVP_ISI 1901 int 1902 ip_rsvp_vif_init(struct socket *so, struct mbuf *m) 1903 { 1904 int vifi, s; 1905 1906 if (rsvpdebug) 1907 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n", 1908 so->so_type, so->so_proto->pr_protocol); 1909 1910 if (so->so_type != SOCK_RAW || 1911 so->so_proto->pr_protocol != IPPROTO_RSVP) 1912 return (EOPNOTSUPP); 1913 1914 /* Check mbuf. */ 1915 if (m == NULL || m->m_len != sizeof(int)) { 1916 return (EINVAL); 1917 } 1918 vifi = *(mtod(m, int *)); 1919 1920 if (rsvpdebug) 1921 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", 1922 vifi, rsvp_on); 1923 1924 s = splsoftnet(); 1925 1926 /* Check vif. */ 1927 if (!legal_vif_num(vifi)) { 1928 splx(s); 1929 return (EADDRNOTAVAIL); 1930 } 1931 1932 /* Check if socket is available. */ 1933 if (viftable[vifi].v_rsvpd != NULL) { 1934 splx(s); 1935 return (EADDRINUSE); 1936 } 1937 1938 viftable[vifi].v_rsvpd = so; 1939 /* This may seem silly, but we need to be sure we don't over-increment 1940 * the RSVP counter, in case something slips up. 1941 */ 1942 if (!viftable[vifi].v_rsvp_on) { 1943 viftable[vifi].v_rsvp_on = 1; 1944 rsvp_on++; 1945 } 1946 1947 splx(s); 1948 return (0); 1949 } 1950 1951 int 1952 ip_rsvp_vif_done(struct socket *so, struct mbuf *m) 1953 { 1954 int vifi, s; 1955 1956 if (rsvpdebug) 1957 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n", 1958 so->so_type, so->so_proto->pr_protocol); 1959 1960 if (so->so_type != SOCK_RAW || 1961 so->so_proto->pr_protocol != IPPROTO_RSVP) 1962 return (EOPNOTSUPP); 1963 1964 /* Check mbuf. */ 1965 if (m == NULL || m->m_len != sizeof(int)) { 1966 return (EINVAL); 1967 } 1968 vifi = *(mtod(m, int *)); 1969 1970 s = splsoftnet(); 1971 1972 /* Check vif. */ 1973 if (!legal_vif_num(vifi)) { 1974 splx(s); 1975 return (EADDRNOTAVAIL); 1976 } 1977 1978 if (rsvpdebug) 1979 printf("ip_rsvp_vif_done: v_rsvpd = %x so = %x\n", 1980 viftable[vifi].v_rsvpd, so); 1981 1982 viftable[vifi].v_rsvpd = NULL; 1983 /* 1984 * This may seem silly, but we need to be sure we don't over-decrement 1985 * the RSVP counter, in case something slips up. 1986 */ 1987 if (viftable[vifi].v_rsvp_on) { 1988 viftable[vifi].v_rsvp_on = 0; 1989 rsvp_on--; 1990 } 1991 1992 splx(s); 1993 return (0); 1994 } 1995 1996 void 1997 ip_rsvp_force_done(struct socket *so) 1998 { 1999 int vifi, s; 2000 2001 /* Don't bother if it is not the right type of socket. */ 2002 if (so->so_type != SOCK_RAW || 2003 so->so_proto->pr_protocol != IPPROTO_RSVP) 2004 return; 2005 2006 s = splsoftnet(); 2007 2008 /* 2009 * The socket may be attached to more than one vif...this 2010 * is perfectly legal. 2011 */ 2012 for (vifi = 0; vifi < numvifs; vifi++) { 2013 if (viftable[vifi].v_rsvpd == so) { 2014 viftable[vifi].v_rsvpd = NULL; 2015 /* 2016 * This may seem silly, but we need to be sure we don't 2017 * over-decrement the RSVP counter, in case something 2018 * slips up. 2019 */ 2020 if (viftable[vifi].v_rsvp_on) { 2021 viftable[vifi].v_rsvp_on = 0; 2022 rsvp_on--; 2023 } 2024 } 2025 } 2026 2027 splx(s); 2028 return; 2029 } 2030 2031 void 2032 rsvp_input(struct mbuf *m, struct ifnet *ifp) 2033 { 2034 int vifi, s; 2035 struct ip *ip = mtod(m, struct ip *); 2036 static struct sockaddr_in rsvp_src = { sizeof(sin), AF_INET }; 2037 2038 if (rsvpdebug) 2039 printf("rsvp_input: rsvp_on %d\n", rsvp_on); 2040 2041 /* 2042 * Can still get packets with rsvp_on = 0 if there is a local member 2043 * of the group to which the RSVP packet is addressed. But in this 2044 * case we want to throw the packet away. 2045 */ 2046 if (!rsvp_on) { 2047 m_freem(m); 2048 return; 2049 } 2050 2051 /* 2052 * If the old-style non-vif-associated socket is set, then use 2053 * it and ignore the new ones. 2054 */ 2055 if (ip_rsvpd != NULL) { 2056 if (rsvpdebug) 2057 printf("rsvp_input: " 2058 "Sending packet up old-style socket\n"); 2059 rip_input(m, 0); /*XXX*/ 2060 return; 2061 } 2062 2063 s = splsoftnet(); 2064 2065 if (rsvpdebug) 2066 printf("rsvp_input: check vifs\n"); 2067 2068 /* Find which vif the packet arrived on. */ 2069 for (vifi = 0; vifi < numvifs; vifi++) { 2070 if (viftable[vifi].v_ifp == ifp) 2071 break; 2072 } 2073 2074 if (vifi == numvifs) { 2075 /* Can't find vif packet arrived on. Drop packet. */ 2076 if (rsvpdebug) 2077 printf("rsvp_input: " 2078 "Can't find vif for packet...dropping it.\n"); 2079 m_freem(m); 2080 splx(s); 2081 return; 2082 } 2083 2084 if (rsvpdebug) 2085 printf("rsvp_input: check socket\n"); 2086 2087 if (viftable[vifi].v_rsvpd == NULL) { 2088 /* 2089 * drop packet, since there is no specific socket for this 2090 * interface 2091 */ 2092 if (rsvpdebug) 2093 printf("rsvp_input: No socket defined for vif %d\n", 2094 vifi); 2095 m_freem(m); 2096 splx(s); 2097 return; 2098 } 2099 2100 rsvp_src.sin_addr = ip->ip_src; 2101 2102 if (rsvpdebug && m) 2103 printf("rsvp_input: m->m_len = %d, sbspace() = %d\n", 2104 m->m_len, sbspace(&viftable[vifi].v_rsvpd->so_rcv)); 2105 2106 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) 2107 if (rsvpdebug) 2108 printf("rsvp_input: Failed to append to socket\n"); 2109 else 2110 if (rsvpdebug) 2111 printf("rsvp_input: send packet up\n"); 2112 2113 splx(s); 2114 } 2115 #endif /* RSVP_ISI */ 2116 2117 /* 2118 * Code for bandwidth monitors 2119 */ 2120 2121 /* 2122 * Define common interface for timeval-related methods 2123 */ 2124 #define BW_TIMEVALCMP(tvp, uvp, cmp) timercmp((tvp), (uvp), cmp) 2125 #define BW_TIMEVALDECR(vvp, uvp) timersub((vvp), (uvp), (vvp)) 2126 #define BW_TIMEVALADD(vvp, uvp) timeradd((vvp), (uvp), (vvp)) 2127 2128 static uint32_t 2129 compute_bw_meter_flags(struct bw_upcall *req) 2130 { 2131 uint32_t flags = 0; 2132 2133 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS) 2134 flags |= BW_METER_UNIT_PACKETS; 2135 if (req->bu_flags & BW_UPCALL_UNIT_BYTES) 2136 flags |= BW_METER_UNIT_BYTES; 2137 if (req->bu_flags & BW_UPCALL_GEQ) 2138 flags |= BW_METER_GEQ; 2139 if (req->bu_flags & BW_UPCALL_LEQ) 2140 flags |= BW_METER_LEQ; 2141 2142 return (flags); 2143 } 2144 2145 /* 2146 * Add a bw_meter entry 2147 */ 2148 static int 2149 add_bw_upcall(struct mbuf *m) 2150 { 2151 int s; 2152 struct mfc *mfc; 2153 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC, 2154 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC }; 2155 struct timeval now; 2156 struct bw_meter *x; 2157 uint32_t flags; 2158 struct bw_upcall *req; 2159 2160 if (m == NULL || m->m_len < sizeof(struct bw_upcall)) 2161 return (EINVAL); 2162 2163 req = mtod(m, struct bw_upcall *); 2164 2165 if (!(mrt_api_config & MRT_MFC_BW_UPCALL)) 2166 return (EOPNOTSUPP); 2167 2168 /* Test if the flags are valid */ 2169 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES))) 2170 return (EINVAL); 2171 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))) 2172 return (EINVAL); 2173 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)) 2174 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ)) 2175 return (EINVAL); 2176 2177 /* Test if the threshold time interval is valid */ 2178 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <)) 2179 return (EINVAL); 2180 2181 flags = compute_bw_meter_flags(req); 2182 2183 /* Find if we have already same bw_meter entry */ 2184 s = splsoftnet(); 2185 mfc = mfc_find(&req->bu_src, &req->bu_dst); 2186 if (mfc == NULL) { 2187 splx(s); 2188 return (EADDRNOTAVAIL); 2189 } 2190 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) { 2191 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time, 2192 &req->bu_threshold.b_time, ==)) && 2193 (x->bm_threshold.b_packets == 2194 req->bu_threshold.b_packets) && 2195 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) && 2196 (x->bm_flags & BW_METER_USER_FLAGS) == flags) { 2197 splx(s); 2198 return (0); /* XXX Already installed */ 2199 } 2200 } 2201 2202 /* Allocate the new bw_meter entry */ 2203 x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT); 2204 if (x == NULL) { 2205 splx(s); 2206 return (ENOBUFS); 2207 } 2208 2209 /* Set the new bw_meter entry */ 2210 x->bm_threshold.b_time = req->bu_threshold.b_time; 2211 microtime(&now); 2212 x->bm_start_time = now; 2213 x->bm_threshold.b_packets = req->bu_threshold.b_packets; 2214 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes; 2215 x->bm_measured.b_packets = 0; 2216 x->bm_measured.b_bytes = 0; 2217 x->bm_flags = flags; 2218 x->bm_time_next = NULL; 2219 x->bm_time_hash = BW_METER_BUCKETS; 2220 2221 /* Add the new bw_meter entry to the front of entries for this MFC */ 2222 x->bm_mfc = mfc; 2223 x->bm_mfc_next = mfc->mfc_bw_meter; 2224 mfc->mfc_bw_meter = x; 2225 schedule_bw_meter(x, &now); 2226 splx(s); 2227 2228 return (0); 2229 } 2230 2231 static void 2232 free_bw_list(struct bw_meter *list) 2233 { 2234 while (list != NULL) { 2235 struct bw_meter *x = list; 2236 2237 list = list->bm_mfc_next; 2238 unschedule_bw_meter(x); 2239 free(x, M_BWMETER); 2240 } 2241 } 2242 2243 /* 2244 * Delete one or multiple bw_meter entries 2245 */ 2246 static int 2247 del_bw_upcall(struct mbuf *m) 2248 { 2249 int s; 2250 struct mfc *mfc; 2251 struct bw_meter *x; 2252 struct bw_upcall *req; 2253 2254 if (m == NULL || m->m_len < sizeof(struct bw_upcall)) 2255 return (EINVAL); 2256 2257 req = mtod(m, struct bw_upcall *); 2258 2259 if (!(mrt_api_config & MRT_MFC_BW_UPCALL)) 2260 return (EOPNOTSUPP); 2261 2262 s = splsoftnet(); 2263 /* Find the corresponding MFC entry */ 2264 mfc = mfc_find(&req->bu_src, &req->bu_dst); 2265 if (mfc == NULL) { 2266 splx(s); 2267 return (EADDRNOTAVAIL); 2268 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) { 2269 /* Delete all bw_meter entries for this mfc */ 2270 struct bw_meter *list; 2271 2272 list = mfc->mfc_bw_meter; 2273 mfc->mfc_bw_meter = NULL; 2274 free_bw_list(list); 2275 splx(s); 2276 return (0); 2277 } else { /* Delete a single bw_meter entry */ 2278 struct bw_meter *prev; 2279 uint32_t flags = 0; 2280 2281 flags = compute_bw_meter_flags(req); 2282 2283 /* Find the bw_meter entry to delete */ 2284 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL; 2285 prev = x, x = x->bm_mfc_next) { 2286 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time, 2287 &req->bu_threshold.b_time, ==)) && 2288 (x->bm_threshold.b_packets == 2289 req->bu_threshold.b_packets) && 2290 (x->bm_threshold.b_bytes == 2291 req->bu_threshold.b_bytes) && 2292 (x->bm_flags & BW_METER_USER_FLAGS) == flags) 2293 break; 2294 } 2295 if (x != NULL) { /* Delete entry from the list for this MFC */ 2296 if (prev != NULL) { 2297 /* remove from middle */ 2298 prev->bm_mfc_next = x->bm_mfc_next; 2299 } else { 2300 /* new head of list */ 2301 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next; 2302 } 2303 2304 unschedule_bw_meter(x); 2305 splx(s); 2306 /* Free the bw_meter entry */ 2307 free(x, M_BWMETER); 2308 return (0); 2309 } else { 2310 splx(s); 2311 return (EINVAL); 2312 } 2313 } 2314 /* NOTREACHED */ 2315 } 2316 2317 /* 2318 * Perform bandwidth measurement processing that may result in an upcall 2319 */ 2320 static void 2321 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp) 2322 { 2323 struct timeval delta; 2324 2325 delta = *nowp; 2326 BW_TIMEVALDECR(&delta, &x->bm_start_time); 2327 2328 if (x->bm_flags & BW_METER_GEQ) { 2329 /* Processing for ">=" type of bw_meter entry */ 2330 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) { 2331 /* Reset the bw_meter entry */ 2332 x->bm_start_time = *nowp; 2333 x->bm_measured.b_packets = 0; 2334 x->bm_measured.b_bytes = 0; 2335 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED; 2336 } 2337 2338 /* Record that a packet is received */ 2339 x->bm_measured.b_packets++; 2340 x->bm_measured.b_bytes += plen; 2341 2342 /* Test if we should deliver an upcall */ 2343 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) { 2344 if (((x->bm_flags & BW_METER_UNIT_PACKETS) && 2345 (x->bm_measured.b_packets >= 2346 x->bm_threshold.b_packets)) || 2347 ((x->bm_flags & BW_METER_UNIT_BYTES) && 2348 (x->bm_measured.b_bytes >= 2349 x->bm_threshold.b_bytes))) { 2350 /* Prepare an upcall for delivery */ 2351 bw_meter_prepare_upcall(x, nowp); 2352 x->bm_flags |= BW_METER_UPCALL_DELIVERED; 2353 } 2354 } 2355 } else if (x->bm_flags & BW_METER_LEQ) { 2356 /* Processing for "<=" type of bw_meter entry */ 2357 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) { 2358 /* 2359 * We are behind time with the multicast forwarding 2360 * table scanning for "<=" type of bw_meter entries, 2361 * so test now if we should deliver an upcall. 2362 */ 2363 if (((x->bm_flags & BW_METER_UNIT_PACKETS) && 2364 (x->bm_measured.b_packets <= 2365 x->bm_threshold.b_packets)) || 2366 ((x->bm_flags & BW_METER_UNIT_BYTES) && 2367 (x->bm_measured.b_bytes <= 2368 x->bm_threshold.b_bytes))) { 2369 /* Prepare an upcall for delivery */ 2370 bw_meter_prepare_upcall(x, nowp); 2371 } 2372 /* Reschedule the bw_meter entry */ 2373 unschedule_bw_meter(x); 2374 schedule_bw_meter(x, nowp); 2375 } 2376 2377 /* Record that a packet is received */ 2378 x->bm_measured.b_packets++; 2379 x->bm_measured.b_bytes += plen; 2380 2381 /* Test if we should restart the measuring interval */ 2382 if ((x->bm_flags & BW_METER_UNIT_PACKETS && 2383 x->bm_measured.b_packets <= x->bm_threshold.b_packets) || 2384 (x->bm_flags & BW_METER_UNIT_BYTES && 2385 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) { 2386 /* Don't restart the measuring interval */ 2387 } else { 2388 /* Do restart the measuring interval */ 2389 /* 2390 * XXX: note that we don't unschedule and schedule, 2391 * because this might be too much overhead per packet. 2392 * Instead, when we process all entries for a given 2393 * timer hash bin, we check whether it is really a 2394 * timeout. If not, we reschedule at that time. 2395 */ 2396 x->bm_start_time = *nowp; 2397 x->bm_measured.b_packets = 0; 2398 x->bm_measured.b_bytes = 0; 2399 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED; 2400 } 2401 } 2402 } 2403 2404 /* 2405 * Prepare a bandwidth-related upcall 2406 */ 2407 static void 2408 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp) 2409 { 2410 struct timeval delta; 2411 struct bw_upcall *u; 2412 2413 /* Compute the measured time interval */ 2414 delta = *nowp; 2415 BW_TIMEVALDECR(&delta, &x->bm_start_time); 2416 2417 /* If there are too many pending upcalls, deliver them now */ 2418 if (bw_upcalls_n >= BW_UPCALLS_MAX) 2419 bw_upcalls_send(); 2420 2421 /* Set the bw_upcall entry */ 2422 u = &bw_upcalls[bw_upcalls_n++]; 2423 u->bu_src = x->bm_mfc->mfc_origin; 2424 u->bu_dst = x->bm_mfc->mfc_mcastgrp; 2425 u->bu_threshold.b_time = x->bm_threshold.b_time; 2426 u->bu_threshold.b_packets = x->bm_threshold.b_packets; 2427 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes; 2428 u->bu_measured.b_time = delta; 2429 u->bu_measured.b_packets = x->bm_measured.b_packets; 2430 u->bu_measured.b_bytes = x->bm_measured.b_bytes; 2431 u->bu_flags = 0; 2432 if (x->bm_flags & BW_METER_UNIT_PACKETS) 2433 u->bu_flags |= BW_UPCALL_UNIT_PACKETS; 2434 if (x->bm_flags & BW_METER_UNIT_BYTES) 2435 u->bu_flags |= BW_UPCALL_UNIT_BYTES; 2436 if (x->bm_flags & BW_METER_GEQ) 2437 u->bu_flags |= BW_UPCALL_GEQ; 2438 if (x->bm_flags & BW_METER_LEQ) 2439 u->bu_flags |= BW_UPCALL_LEQ; 2440 } 2441 2442 /* 2443 * Send the pending bandwidth-related upcalls 2444 */ 2445 static void 2446 bw_upcalls_send(void) 2447 { 2448 struct mbuf *m; 2449 int len = bw_upcalls_n * sizeof(bw_upcalls[0]); 2450 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET }; 2451 static struct igmpmsg igmpmsg = { 2452 0, /* unused1 */ 2453 0, /* unused2 */ 2454 IGMPMSG_BW_UPCALL, /* im_msgtype */ 2455 0, /* im_mbz */ 2456 0, /* im_vif */ 2457 0, /* unused3 */ 2458 { 0 }, /* im_src */ 2459 { 0 } }; /* im_dst */ 2460 2461 if (bw_upcalls_n == 0) 2462 return; /* No pending upcalls */ 2463 2464 bw_upcalls_n = 0; 2465 2466 /* 2467 * Allocate a new mbuf, initialize it with the header and 2468 * the payload for the pending calls. 2469 */ 2470 MGETHDR(m, M_DONTWAIT, MT_HEADER); 2471 if (m == NULL) { 2472 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n"); 2473 return; 2474 } 2475 2476 m->m_len = m->m_pkthdr.len = 0; 2477 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg); 2478 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]); 2479 2480 /* 2481 * Send the upcalls 2482 * XXX do we need to set the address in k_igmpsrc ? 2483 */ 2484 mrtstat.mrts_upcalls++; 2485 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) { 2486 log(LOG_WARNING, 2487 "bw_upcalls_send: ip_mrouter socket queue full\n"); 2488 ++mrtstat.mrts_upq_sockfull; 2489 } 2490 } 2491 2492 /* 2493 * Compute the timeout hash value for the bw_meter entries 2494 */ 2495 #define BW_METER_TIMEHASH(bw_meter, hash) do { \ 2496 struct timeval next_timeval = (bw_meter)->bm_start_time; \ 2497 \ 2498 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \ 2499 (hash) = next_timeval.tv_sec; \ 2500 if (next_timeval.tv_usec) \ 2501 (hash)++; /* XXX: make sure we don't timeout early */ \ 2502 (hash) %= BW_METER_BUCKETS; \ 2503 } while (/*CONSTCOND*/ 0) 2504 2505 /* 2506 * Schedule a timer to process periodically bw_meter entry of type "<=" 2507 * by linking the entry in the proper hash bucket. 2508 */ 2509 static void 2510 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp) 2511 { 2512 int time_hash; 2513 2514 if (!(x->bm_flags & BW_METER_LEQ)) 2515 return; /* XXX: we schedule timers only for "<=" entries */ 2516 2517 /* Reset the bw_meter entry */ 2518 x->bm_start_time = *nowp; 2519 x->bm_measured.b_packets = 0; 2520 x->bm_measured.b_bytes = 0; 2521 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED; 2522 2523 /* Compute the timeout hash value and insert the entry */ 2524 BW_METER_TIMEHASH(x, time_hash); 2525 x->bm_time_next = bw_meter_timers[time_hash]; 2526 bw_meter_timers[time_hash] = x; 2527 x->bm_time_hash = time_hash; 2528 } 2529 2530 /* 2531 * Unschedule the periodic timer that processes bw_meter entry of type "<=" 2532 * by removing the entry from the proper hash bucket. 2533 */ 2534 static void 2535 unschedule_bw_meter(struct bw_meter *x) 2536 { 2537 int time_hash; 2538 struct bw_meter *prev, *tmp; 2539 2540 if (!(x->bm_flags & BW_METER_LEQ)) 2541 return; /* XXX: we schedule timers only for "<=" entries */ 2542 2543 /* Compute the timeout hash value and delete the entry */ 2544 time_hash = x->bm_time_hash; 2545 if (time_hash >= BW_METER_BUCKETS) 2546 return; /* Entry was not scheduled */ 2547 2548 for (prev = NULL, tmp = bw_meter_timers[time_hash]; 2549 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next) 2550 if (tmp == x) 2551 break; 2552 2553 if (tmp == NULL) 2554 panic("unschedule_bw_meter: bw_meter entry not found"); 2555 2556 if (prev != NULL) 2557 prev->bm_time_next = x->bm_time_next; 2558 else 2559 bw_meter_timers[time_hash] = x->bm_time_next; 2560 2561 x->bm_time_next = NULL; 2562 x->bm_time_hash = BW_METER_BUCKETS; 2563 } 2564 2565 /* 2566 * Process all "<=" type of bw_meter that should be processed now, 2567 * and for each entry prepare an upcall if necessary. Each processed 2568 * entry is rescheduled again for the (periodic) processing. 2569 * 2570 * This is run periodically (once per second normally). On each round, 2571 * all the potentially matching entries are in the hash slot that we are 2572 * looking at. 2573 */ 2574 static void 2575 bw_meter_process() 2576 { 2577 int s; 2578 static uint32_t last_tv_sec; /* last time we processed this */ 2579 2580 uint32_t loops; 2581 int i; 2582 struct timeval now, process_endtime; 2583 2584 microtime(&now); 2585 if (last_tv_sec == now.tv_sec) 2586 return; /* nothing to do */ 2587 2588 loops = now.tv_sec - last_tv_sec; 2589 last_tv_sec = now.tv_sec; 2590 if (loops > BW_METER_BUCKETS) 2591 loops = BW_METER_BUCKETS; 2592 2593 s = splsoftnet(); 2594 /* 2595 * Process all bins of bw_meter entries from the one after the last 2596 * processed to the current one. On entry, i points to the last bucket 2597 * visited, so we need to increment i at the beginning of the loop. 2598 */ 2599 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) { 2600 struct bw_meter *x, *tmp_list; 2601 2602 if (++i >= BW_METER_BUCKETS) 2603 i = 0; 2604 2605 /* Disconnect the list of bw_meter entries from the bin */ 2606 tmp_list = bw_meter_timers[i]; 2607 bw_meter_timers[i] = NULL; 2608 2609 /* Process the list of bw_meter entries */ 2610 while (tmp_list != NULL) { 2611 x = tmp_list; 2612 tmp_list = tmp_list->bm_time_next; 2613 2614 /* Test if the time interval is over */ 2615 process_endtime = x->bm_start_time; 2616 BW_TIMEVALADD(&process_endtime, 2617 &x->bm_threshold.b_time); 2618 if (BW_TIMEVALCMP(&process_endtime, &now, >)) { 2619 /* Not yet: reschedule, but don't reset */ 2620 int time_hash; 2621 2622 BW_METER_TIMEHASH(x, time_hash); 2623 if (time_hash == i && 2624 process_endtime.tv_sec == now.tv_sec) { 2625 /* 2626 * XXX: somehow the bin processing is 2627 * a bit ahead of time. Put the entry 2628 * in the next bin. 2629 */ 2630 if (++time_hash >= BW_METER_BUCKETS) 2631 time_hash = 0; 2632 } 2633 x->bm_time_next = bw_meter_timers[time_hash]; 2634 bw_meter_timers[time_hash] = x; 2635 x->bm_time_hash = time_hash; 2636 2637 continue; 2638 } 2639 2640 /* Test if we should deliver an upcall */ 2641 if (((x->bm_flags & BW_METER_UNIT_PACKETS) && 2642 (x->bm_measured.b_packets <= 2643 x->bm_threshold.b_packets)) || 2644 ((x->bm_flags & BW_METER_UNIT_BYTES) && 2645 (x->bm_measured.b_bytes <= 2646 x->bm_threshold.b_bytes))) { 2647 /* Prepare an upcall for delivery */ 2648 bw_meter_prepare_upcall(x, &now); 2649 } 2650 2651 /* Reschedule for next processing */ 2652 schedule_bw_meter(x, &now); 2653 } 2654 } 2655 2656 /* Send all upcalls that are pending delivery */ 2657 bw_upcalls_send(); 2658 2659 splx(s); 2660 } 2661 2662 /* 2663 * A periodic function for sending all upcalls that are pending delivery 2664 */ 2665 static void 2666 expire_bw_upcalls_send(void *unused) 2667 { 2668 int s; 2669 2670 s = splsoftnet(); 2671 bw_upcalls_send(); 2672 splx(s); 2673 2674 timeout_add_msec(&bw_upcalls_ch, BW_UPCALLS_PERIOD); 2675 } 2676 2677 /* 2678 * A periodic function for periodic scanning of the multicast forwarding 2679 * table for processing all "<=" bw_meter entries. 2680 */ 2681 static void 2682 expire_bw_meter_process(void *unused) 2683 { 2684 if (mrt_api_config & MRT_MFC_BW_UPCALL) 2685 bw_meter_process(); 2686 2687 timeout_add_msec(&bw_meter_ch, BW_METER_PERIOD); 2688 } 2689 2690 /* 2691 * End of bandwidth monitoring code 2692 */ 2693 2694 #ifdef PIM 2695 /* 2696 * Send the packet up to the user daemon, or eventually do kernel encapsulation 2697 */ 2698 static int 2699 pim_register_send(struct ip *ip, struct vif *vifp, 2700 struct mbuf *m, struct mfc *rt) 2701 { 2702 struct mbuf *mb_copy, *mm; 2703 2704 if (mrtdebug & DEBUG_PIM) 2705 log(LOG_DEBUG, "pim_register_send: "); 2706 2707 mb_copy = pim_register_prepare(ip, m); 2708 if (mb_copy == NULL) 2709 return (ENOBUFS); 2710 2711 /* 2712 * Send all the fragments. Note that the mbuf for each fragment 2713 * is freed by the sending machinery. 2714 */ 2715 for (mm = mb_copy; mm; mm = mb_copy) { 2716 mb_copy = mm->m_nextpkt; 2717 mm->m_nextpkt = NULL; 2718 mm = m_pullup(mm, sizeof(struct ip)); 2719 if (mm != NULL) { 2720 ip = mtod(mm, struct ip *); 2721 if ((mrt_api_config & MRT_MFC_RP) && 2722 !in_nullhost(rt->mfc_rp)) { 2723 pim_register_send_rp(ip, vifp, mm, rt); 2724 } else { 2725 pim_register_send_upcall(ip, vifp, mm, rt); 2726 } 2727 } 2728 } 2729 2730 return (0); 2731 } 2732 2733 /* 2734 * Return a copy of the data packet that is ready for PIM Register 2735 * encapsulation. 2736 * XXX: Note that in the returned copy the IP header is a valid one. 2737 */ 2738 static struct mbuf * 2739 pim_register_prepare(struct ip *ip, struct mbuf *m) 2740 { 2741 struct mbuf *mb_copy = NULL; 2742 int mtu; 2743 2744 /* Take care of delayed checksums */ 2745 if (m->m_pkthdr.csum_flags & (M_TCPV4_CSUM_OUT | M_UDPV4_CSUM_OUT)) { 2746 in_delayed_cksum(m); 2747 m->m_pkthdr.csum_flags &= 2748 ~(M_UDPV4_CSUM_OUT | M_TCPV4_CSUM_OUT); 2749 } 2750 2751 /* 2752 * Copy the old packet & pullup its IP header into the 2753 * new mbuf so we can modify it. 2754 */ 2755 mb_copy = m_copy(m, 0, M_COPYALL); 2756 if (mb_copy == NULL) 2757 return (NULL); 2758 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2); 2759 if (mb_copy == NULL) 2760 return (NULL); 2761 2762 /* take care of the TTL */ 2763 ip = mtod(mb_copy, struct ip *); 2764 --ip->ip_ttl; 2765 2766 /* Compute the MTU after the PIM Register encapsulation */ 2767 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr); 2768 2769 if (ntohs(ip->ip_len) <= mtu) { 2770 /* Turn the IP header into a valid one */ 2771 ip->ip_sum = 0; 2772 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2); 2773 } else { 2774 /* Fragment the packet */ 2775 if (ip_fragment(mb_copy, NULL, mtu) != 0) { 2776 /* XXX: mb_copy was freed by ip_fragment() */ 2777 return (NULL); 2778 } 2779 } 2780 return (mb_copy); 2781 } 2782 2783 /* 2784 * Send an upcall with the data packet to the user-level process. 2785 */ 2786 static int 2787 pim_register_send_upcall(struct ip *ip, struct vif *vifp, 2788 struct mbuf *mb_copy, struct mfc *rt) 2789 { 2790 struct mbuf *mb_first; 2791 int len = ntohs(ip->ip_len); 2792 struct igmpmsg *im; 2793 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET }; 2794 2795 /* Add a new mbuf with an upcall header */ 2796 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER); 2797 if (mb_first == NULL) { 2798 m_freem(mb_copy); 2799 return (ENOBUFS); 2800 } 2801 mb_first->m_data += max_linkhdr; 2802 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg); 2803 mb_first->m_len = sizeof(struct igmpmsg); 2804 mb_first->m_next = mb_copy; 2805 2806 /* Send message to routing daemon */ 2807 im = mtod(mb_first, struct igmpmsg *); 2808 im->im_msgtype = IGMPMSG_WHOLEPKT; 2809 im->im_mbz = 0; 2810 im->im_vif = vifp - viftable; 2811 im->im_src = ip->ip_src; 2812 im->im_dst = ip->ip_dst; 2813 2814 k_igmpsrc.sin_addr = ip->ip_src; 2815 2816 mrtstat.mrts_upcalls++; 2817 2818 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) { 2819 if (mrtdebug & DEBUG_PIM) 2820 log(LOG_WARNING, "mcast: pim_register_send_upcall: " 2821 "ip_mrouter socket queue full"); 2822 ++mrtstat.mrts_upq_sockfull; 2823 return (ENOBUFS); 2824 } 2825 2826 /* Keep statistics */ 2827 pimstat.pims_snd_registers_msgs++; 2828 pimstat.pims_snd_registers_bytes += len; 2829 2830 return (0); 2831 } 2832 2833 /* 2834 * Encapsulate the data packet in PIM Register message and send it to the RP. 2835 */ 2836 static int 2837 pim_register_send_rp(struct ip *ip, struct vif *vifp, 2838 struct mbuf *mb_copy, struct mfc *rt) 2839 { 2840 struct mbuf *mb_first; 2841 struct ip *ip_outer; 2842 struct pim_encap_pimhdr *pimhdr; 2843 int len = ntohs(ip->ip_len); 2844 vifi_t vifi = rt->mfc_parent; 2845 2846 if ((vifi >= numvifs) || in_nullhost(viftable[vifi].v_lcl_addr)) { 2847 m_freem(mb_copy); 2848 return (EADDRNOTAVAIL); /* The iif vif is invalid */ 2849 } 2850 2851 /* Add a new mbuf with the encapsulating header */ 2852 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER); 2853 if (mb_first == NULL) { 2854 m_freem(mb_copy); 2855 return (ENOBUFS); 2856 } 2857 mb_first->m_data += max_linkhdr; 2858 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr); 2859 mb_first->m_next = mb_copy; 2860 2861 mb_first->m_pkthdr.len = len + mb_first->m_len; 2862 2863 /* Fill in the encapsulating IP and PIM header */ 2864 ip_outer = mtod(mb_first, struct ip *); 2865 *ip_outer = pim_encap_iphdr; 2866 ip_outer->ip_id = htons(ip_randomid()); 2867 ip_outer->ip_len = htons(len + sizeof(pim_encap_iphdr) + 2868 sizeof(pim_encap_pimhdr)); 2869 ip_outer->ip_src = viftable[vifi].v_lcl_addr; 2870 ip_outer->ip_dst = rt->mfc_rp; 2871 /* 2872 * Copy the inner header TOS to the outer header, and take care of the 2873 * IP_DF bit. 2874 */ 2875 ip_outer->ip_tos = ip->ip_tos; 2876 if (ntohs(ip->ip_off) & IP_DF) 2877 ip_outer->ip_off |= htons(IP_DF); 2878 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer 2879 + sizeof(pim_encap_iphdr)); 2880 *pimhdr = pim_encap_pimhdr; 2881 /* If the iif crosses a border, set the Border-bit */ 2882 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config) 2883 pimhdr->flags |= htonl(PIM_BORDER_REGISTER); 2884 2885 mb_first->m_data += sizeof(pim_encap_iphdr); 2886 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr)); 2887 mb_first->m_data -= sizeof(pim_encap_iphdr); 2888 2889 send_packet(vifp, mb_first); 2890 2891 /* Keep statistics */ 2892 pimstat.pims_snd_registers_msgs++; 2893 pimstat.pims_snd_registers_bytes += len; 2894 2895 return (0); 2896 } 2897 2898 /* 2899 * PIM-SMv2 and PIM-DM messages processing. 2900 * Receives and verifies the PIM control messages, and passes them 2901 * up to the listening socket, using rip_input(). 2902 * The only message with special processing is the PIM_REGISTER message 2903 * (used by PIM-SM): the PIM header is stripped off, and the inner packet 2904 * is passed to if_simloop(). 2905 */ 2906 void 2907 pim_input(struct mbuf *m, ...) 2908 { 2909 struct ip *ip = mtod(m, struct ip *); 2910 struct pim *pim; 2911 int minlen; 2912 int datalen; 2913 int ip_tos; 2914 int iphlen; 2915 va_list ap; 2916 2917 va_start(ap, m); 2918 iphlen = va_arg(ap, int); 2919 va_end(ap); 2920 2921 datalen = ntohs(ip->ip_len) - iphlen; 2922 2923 /* Keep statistics */ 2924 pimstat.pims_rcv_total_msgs++; 2925 pimstat.pims_rcv_total_bytes += datalen; 2926 2927 /* Validate lengths */ 2928 if (datalen < PIM_MINLEN) { 2929 pimstat.pims_rcv_tooshort++; 2930 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n", 2931 datalen, (u_long)ip->ip_src.s_addr); 2932 m_freem(m); 2933 return; 2934 } 2935 2936 /* 2937 * If the packet is at least as big as a REGISTER, go agead 2938 * and grab the PIM REGISTER header size, to avoid another 2939 * possible m_pullup() later. 2940 * 2941 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8 2942 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28 2943 */ 2944 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? 2945 PIM_REG_MINLEN : PIM_MINLEN); 2946 /* 2947 * Get the IP and PIM headers in contiguous memory, and 2948 * possibly the PIM REGISTER header. 2949 */ 2950 if ((m->m_flags & M_EXT || m->m_len < minlen) && 2951 (m = m_pullup(m, minlen)) == NULL) { 2952 log(LOG_ERR, "pim_input: m_pullup failure\n"); 2953 return; 2954 } 2955 /* m_pullup() may have given us a new mbuf so reset ip. */ 2956 ip = mtod(m, struct ip *); 2957 ip_tos = ip->ip_tos; 2958 2959 /* adjust mbuf to point to the PIM header */ 2960 m->m_data += iphlen; 2961 m->m_len -= iphlen; 2962 pim = mtod(m, struct pim *); 2963 2964 /* 2965 * Validate checksum. If PIM REGISTER, exclude the data packet. 2966 * 2967 * XXX: some older PIMv2 implementations don't make this distinction, 2968 * so for compatibility reason perform the checksum over part of the 2969 * message, and if error, then over the whole message. 2970 */ 2971 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && 2972 in_cksum(m, PIM_MINLEN) == 0) { 2973 /* do nothing, checksum okay */ 2974 } else if (in_cksum(m, datalen)) { 2975 pimstat.pims_rcv_badsum++; 2976 if (mrtdebug & DEBUG_PIM) 2977 log(LOG_DEBUG, "pim_input: invalid checksum"); 2978 m_freem(m); 2979 return; 2980 } 2981 2982 /* PIM version check */ 2983 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) { 2984 pimstat.pims_rcv_badversion++; 2985 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n", 2986 PIM_VT_V(pim->pim_vt), PIM_VERSION); 2987 m_freem(m); 2988 return; 2989 } 2990 2991 /* restore mbuf back to the outer IP */ 2992 m->m_data -= iphlen; 2993 m->m_len += iphlen; 2994 2995 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) { 2996 /* 2997 * Since this is a REGISTER, we'll make a copy of the register 2998 * headers ip + pim + u_int32 + encap_ip, to be passed up to the 2999 * routing daemon. 3000 */ 3001 int s; 3002 struct sockaddr_in dst = { sizeof(dst), AF_INET }; 3003 struct mbuf *mcp; 3004 struct ip *encap_ip; 3005 u_int32_t *reghdr; 3006 struct ifnet *vifp; 3007 3008 s = splsoftnet(); 3009 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) { 3010 splx(s); 3011 if (mrtdebug & DEBUG_PIM) 3012 log(LOG_DEBUG, "pim_input: register vif " 3013 "not set: %d\n", reg_vif_num); 3014 m_freem(m); 3015 return; 3016 } 3017 /* XXX need refcnt? */ 3018 vifp = viftable[reg_vif_num].v_ifp; 3019 splx(s); 3020 3021 /* Validate length */ 3022 if (datalen < PIM_REG_MINLEN) { 3023 pimstat.pims_rcv_tooshort++; 3024 pimstat.pims_rcv_badregisters++; 3025 log(LOG_ERR, "pim_input: register packet size " 3026 "too small %d from %lx\n", 3027 datalen, (u_long)ip->ip_src.s_addr); 3028 m_freem(m); 3029 return; 3030 } 3031 3032 reghdr = (u_int32_t *)(pim + 1); 3033 encap_ip = (struct ip *)(reghdr + 1); 3034 3035 if (mrtdebug & DEBUG_PIM) { 3036 log(LOG_DEBUG, "pim_input[register], encap_ip: " 3037 "%lx -> %lx, encap_ip len %d\n", 3038 (u_long)ntohl(encap_ip->ip_src.s_addr), 3039 (u_long)ntohl(encap_ip->ip_dst.s_addr), 3040 ntohs(encap_ip->ip_len)); 3041 } 3042 3043 /* verify the version number of the inner packet */ 3044 if (encap_ip->ip_v != IPVERSION) { 3045 pimstat.pims_rcv_badregisters++; 3046 if (mrtdebug & DEBUG_PIM) { 3047 log(LOG_DEBUG, "pim_input: invalid IP version" 3048 " (%d) of the inner packet\n", 3049 encap_ip->ip_v); 3050 } 3051 m_freem(m); 3052 return; 3053 } 3054 3055 /* verify the inner packet is destined to a mcast group */ 3056 if (!IN_MULTICAST(encap_ip->ip_dst.s_addr)) { 3057 pimstat.pims_rcv_badregisters++; 3058 if (mrtdebug & DEBUG_PIM) 3059 log(LOG_DEBUG, 3060 "pim_input: inner packet of register is" 3061 " not multicast %lx\n", 3062 (u_long)ntohl(encap_ip->ip_dst.s_addr)); 3063 m_freem(m); 3064 return; 3065 } 3066 3067 /* If a NULL_REGISTER, pass it to the daemon */ 3068 if ((ntohl(*reghdr) & PIM_NULL_REGISTER)) 3069 goto pim_input_to_daemon; 3070 3071 /* 3072 * Copy the TOS from the outer IP header to the inner 3073 * IP header. 3074 */ 3075 if (encap_ip->ip_tos != ip_tos) { 3076 /* Outer TOS -> inner TOS */ 3077 encap_ip->ip_tos = ip_tos; 3078 /* Recompute the inner header checksum. Sigh... */ 3079 3080 /* adjust mbuf to point to the inner IP header */ 3081 m->m_data += (iphlen + PIM_MINLEN); 3082 m->m_len -= (iphlen + PIM_MINLEN); 3083 3084 encap_ip->ip_sum = 0; 3085 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2); 3086 3087 /* restore mbuf to point back to the outer IP header */ 3088 m->m_data -= (iphlen + PIM_MINLEN); 3089 m->m_len += (iphlen + PIM_MINLEN); 3090 } 3091 3092 /* 3093 * Decapsulate the inner IP packet and loopback to forward it 3094 * as a normal multicast packet. Also, make a copy of the 3095 * outer_iphdr + pimhdr + reghdr + encap_iphdr 3096 * to pass to the daemon later, so it can take the appropriate 3097 * actions (e.g., send back PIM_REGISTER_STOP). 3098 * XXX: here m->m_data points to the outer IP header. 3099 */ 3100 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN); 3101 if (mcp == NULL) { 3102 log(LOG_ERR, "pim_input: pim register: could not " 3103 "copy register head\n"); 3104 m_freem(m); 3105 return; 3106 } 3107 3108 /* Keep statistics */ 3109 /* XXX: registers_bytes include only the encap. mcast pkt */ 3110 pimstat.pims_rcv_registers_msgs++; 3111 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len); 3112 3113 /* forward the inner ip packet; point m_data at the inner ip. */ 3114 m_adj(m, iphlen + PIM_MINLEN); 3115 3116 if (mrtdebug & DEBUG_PIM) { 3117 log(LOG_DEBUG, 3118 "pim_input: forwarding decapsulated register: " 3119 "src %lx, dst %lx, vif %d\n", 3120 (u_long)ntohl(encap_ip->ip_src.s_addr), 3121 (u_long)ntohl(encap_ip->ip_dst.s_addr), 3122 reg_vif_num); 3123 } 3124 /* NB: vifp was collected above; can it change on us? */ 3125 looutput(vifp, m, (struct sockaddr *)&dst, 3126 (struct rtentry *)NULL); 3127 3128 /* prepare the register head to send to the mrouting daemon */ 3129 m = mcp; 3130 } 3131 3132 pim_input_to_daemon: 3133 /* 3134 * Pass the PIM message up to the daemon; if it is a Register message, 3135 * pass the 'head' only up to the daemon. This includes the 3136 * outer IP header, PIM header, PIM-Register header and the 3137 * inner IP header. 3138 * XXX: the outer IP header pkt size of a Register is not adjust to 3139 * reflect the fact that the inner multicast data is truncated. 3140 */ 3141 rip_input(m); 3142 3143 return; 3144 } 3145 3146 /* 3147 * Sysctl for pim variables. 3148 */ 3149 int 3150 pim_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, 3151 void *newp, size_t newlen) 3152 { 3153 /* All sysctl names at this level are terminal. */ 3154 if (namelen != 1) 3155 return (ENOTDIR); 3156 3157 switch (name[0]) { 3158 case PIMCTL_STATS: 3159 if (newp != NULL) 3160 return (EPERM); 3161 return (sysctl_struct(oldp, oldlenp, newp, newlen, 3162 &pimstat, sizeof(pimstat))); 3163 3164 default: 3165 return (ENOPROTOOPT); 3166 } 3167 /* NOTREACHED */ 3168 } 3169 3170 3171 #endif /* PIM */ 3172