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