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