1 /* $NetBSD: ip_mroute.c,v 1.121 2011/10/19 01:52:22 dyoung 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.121 2011/10/19 01:52:22 dyoung 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/ioctl.h> 117 #include <sys/syslog.h> 118 119 #include <net/if.h> 120 #include <net/route.h> 121 #include <net/raw_cb.h> 122 123 #include <netinet/in.h> 124 #include <netinet/in_var.h> 125 #include <netinet/in_systm.h> 126 #include <netinet/ip.h> 127 #include <netinet/ip_var.h> 128 #include <netinet/in_pcb.h> 129 #include <netinet/udp.h> 130 #include <netinet/igmp.h> 131 #include <netinet/igmp_var.h> 132 #include <netinet/ip_mroute.h> 133 #ifdef PIM 134 #include <netinet/pim.h> 135 #include <netinet/pim_var.h> 136 #endif 137 #include <netinet/ip_encap.h> 138 139 #ifdef IPSEC 140 #include <netinet6/ipsec.h> 141 #include <netkey/key.h> 142 #endif 143 144 #ifdef FAST_IPSEC 145 #include <netipsec/ipsec.h> 146 #include <netipsec/key.h> 147 #endif 148 149 #define IP_MULTICASTOPTS 0 150 #define M_PULLUP(m, len) \ 151 do { \ 152 if ((m) && ((m)->m_flags & M_EXT || (m)->m_len < (len))) \ 153 (m) = m_pullup((m), (len)); \ 154 } while (/*CONSTCOND*/ 0) 155 156 /* 157 * Globals. All but ip_mrouter and ip_mrtproto could be static, 158 * except for netstat or debugging purposes. 159 */ 160 struct socket *ip_mrouter = NULL; 161 int ip_mrtproto = IGMP_DVMRP; /* for netstat only */ 162 163 #define NO_RTE_FOUND 0x1 164 #define RTE_FOUND 0x2 165 166 #define MFCHASH(a, g) \ 167 ((((a).s_addr >> 20) ^ ((a).s_addr >> 10) ^ (a).s_addr ^ \ 168 ((g).s_addr >> 20) ^ ((g).s_addr >> 10) ^ (g).s_addr) & mfchash) 169 LIST_HEAD(mfchashhdr, mfc) *mfchashtbl; 170 u_long mfchash; 171 172 u_char nexpire[MFCTBLSIZ]; 173 struct vif viftable[MAXVIFS]; 174 struct mrtstat mrtstat; 175 u_int mrtdebug = 0; /* debug level */ 176 #define DEBUG_MFC 0x02 177 #define DEBUG_FORWARD 0x04 178 #define DEBUG_EXPIRE 0x08 179 #define DEBUG_XMIT 0x10 180 #define DEBUG_PIM 0x20 181 182 #define VIFI_INVALID ((vifi_t) -1) 183 184 u_int tbfdebug = 0; /* tbf debug level */ 185 #ifdef RSVP_ISI 186 u_int rsvpdebug = 0; /* rsvp debug level */ 187 extern struct socket *ip_rsvpd; 188 extern int rsvp_on; 189 #endif /* RSVP_ISI */ 190 191 /* vif attachment using sys/netinet/ip_encap.c */ 192 static void vif_input(struct mbuf *, ...); 193 static int vif_encapcheck(struct mbuf *, int, int, void *); 194 195 static const struct protosw vif_protosw = 196 { SOCK_RAW, &inetdomain, IPPROTO_IPV4, PR_ATOMIC|PR_ADDR, 197 vif_input, rip_output, 0, rip_ctloutput, 198 rip_usrreq, 199 0, 0, 0, 0, 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 mfcctl_size = sizeof(struct mfcctl); 1062 int error; 1063 1064 if (mrt_api_config & MRT_API_FLAGS_ALL) 1065 mfcctl_size = sizeof(struct mfcctl2); 1066 1067 /* 1068 * select data size depending on API version. 1069 */ 1070 mfccp = &mfcctl2; 1071 memset(&mfcctl2, 0, sizeof(mfcctl2)); 1072 1073 if (mrt_api_config & MRT_API_FLAGS_ALL) 1074 error = sockopt_get(sopt, mfccp, sizeof(struct mfcctl2)); 1075 else 1076 error = sockopt_get(sopt, mfccp, sizeof(struct mfcctl)); 1077 1078 if (error) 1079 return (error); 1080 1081 s = splsoftnet(); 1082 rt = mfc_find(&mfccp->mfcc_origin, &mfccp->mfcc_mcastgrp); 1083 1084 /* If an entry already exists, just update the fields */ 1085 if (rt) { 1086 if (mrtdebug & DEBUG_MFC) 1087 log(LOG_DEBUG, "add_mfc update o %x g %x p %x\n", 1088 ntohl(mfccp->mfcc_origin.s_addr), 1089 ntohl(mfccp->mfcc_mcastgrp.s_addr), 1090 mfccp->mfcc_parent); 1091 1092 update_mfc_params(rt, mfccp); 1093 1094 splx(s); 1095 return (0); 1096 } 1097 1098 /* 1099 * Find the entry for which the upcall was made and update 1100 */ 1101 nstl = 0; 1102 hash = MFCHASH(mfccp->mfcc_origin, mfccp->mfcc_mcastgrp); 1103 LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) { 1104 if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) && 1105 in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp) && 1106 rt->mfc_stall != NULL) { 1107 if (nstl++) 1108 log(LOG_ERR, "add_mfc %s o %x g %x p %x dbx %p\n", 1109 "multiple kernel entries", 1110 ntohl(mfccp->mfcc_origin.s_addr), 1111 ntohl(mfccp->mfcc_mcastgrp.s_addr), 1112 mfccp->mfcc_parent, rt->mfc_stall); 1113 1114 if (mrtdebug & DEBUG_MFC) 1115 log(LOG_DEBUG, "add_mfc o %x g %x p %x dbg %p\n", 1116 ntohl(mfccp->mfcc_origin.s_addr), 1117 ntohl(mfccp->mfcc_mcastgrp.s_addr), 1118 mfccp->mfcc_parent, rt->mfc_stall); 1119 1120 rte = rt->mfc_stall; 1121 init_mfc_params(rt, mfccp); 1122 rt->mfc_stall = NULL; 1123 1124 rt->mfc_expire = 0; /* Don't clean this guy up */ 1125 nexpire[hash]--; 1126 1127 /* free packets Qed at the end of this entry */ 1128 for (; rte != NULL; rte = nrte) { 1129 nrte = rte->next; 1130 if (rte->ifp) { 1131 #ifdef RSVP_ISI 1132 ip_mdq(rte->m, rte->ifp, rt, -1); 1133 #else 1134 ip_mdq(rte->m, rte->ifp, rt); 1135 #endif /* RSVP_ISI */ 1136 } 1137 m_freem(rte->m); 1138 #ifdef UPCALL_TIMING 1139 collate(&rte->t); 1140 #endif /* UPCALL_TIMING */ 1141 free(rte, M_MRTABLE); 1142 } 1143 } 1144 } 1145 1146 /* 1147 * It is possible that an entry is being inserted without an upcall 1148 */ 1149 if (nstl == 0) { 1150 /* 1151 * No mfc; make a new one 1152 */ 1153 if (mrtdebug & DEBUG_MFC) 1154 log(LOG_DEBUG, "add_mfc no upcall o %x g %x p %x\n", 1155 ntohl(mfccp->mfcc_origin.s_addr), 1156 ntohl(mfccp->mfcc_mcastgrp.s_addr), 1157 mfccp->mfcc_parent); 1158 1159 LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) { 1160 if (in_hosteq(rt->mfc_origin, mfccp->mfcc_origin) && 1161 in_hosteq(rt->mfc_mcastgrp, mfccp->mfcc_mcastgrp)) { 1162 init_mfc_params(rt, mfccp); 1163 if (rt->mfc_expire) 1164 nexpire[hash]--; 1165 rt->mfc_expire = 0; 1166 break; /* XXX */ 1167 } 1168 } 1169 if (rt == NULL) { /* no upcall, so make a new entry */ 1170 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, 1171 M_NOWAIT); 1172 if (rt == NULL) { 1173 splx(s); 1174 return (ENOBUFS); 1175 } 1176 1177 init_mfc_params(rt, mfccp); 1178 rt->mfc_expire = 0; 1179 rt->mfc_stall = NULL; 1180 rt->mfc_bw_meter = NULL; 1181 1182 /* insert new entry at head of hash chain */ 1183 LIST_INSERT_HEAD(&mfchashtbl[hash], rt, mfc_hash); 1184 } 1185 } 1186 1187 splx(s); 1188 return (0); 1189 } 1190 1191 #ifdef UPCALL_TIMING 1192 /* 1193 * collect delay statistics on the upcalls 1194 */ 1195 static void 1196 collate(struct timeval *t) 1197 { 1198 u_int32_t d; 1199 struct timeval tp; 1200 u_int32_t delta; 1201 1202 microtime(&tp); 1203 1204 if (timercmp(t, &tp, <)) { 1205 TV_DELTA(tp, *t, delta); 1206 1207 d = delta >> 10; 1208 if (d > 50) 1209 d = 50; 1210 1211 ++upcall_data[d]; 1212 } 1213 } 1214 #endif /* UPCALL_TIMING */ 1215 1216 /* 1217 * Delete an mfc entry 1218 */ 1219 static int 1220 del_mfc(struct sockopt *sopt) 1221 { 1222 struct mfcctl2 mfcctl2; 1223 struct mfcctl2 *mfccp; 1224 struct mfc *rt; 1225 int s; 1226 int error; 1227 1228 /* 1229 * XXX: for deleting MFC entries the information in entries 1230 * of size "struct mfcctl" is sufficient. 1231 */ 1232 1233 mfccp = &mfcctl2; 1234 memset(&mfcctl2, 0, sizeof(mfcctl2)); 1235 1236 error = sockopt_get(sopt, mfccp, sizeof(struct mfcctl)); 1237 if (error) { 1238 /* Try with the size of mfcctl2. */ 1239 error = sockopt_get(sopt, mfccp, sizeof(struct mfcctl2)); 1240 if (error) 1241 return (error); 1242 } 1243 1244 if (mrtdebug & DEBUG_MFC) 1245 log(LOG_DEBUG, "del_mfc origin %x mcastgrp %x\n", 1246 ntohl(mfccp->mfcc_origin.s_addr), 1247 ntohl(mfccp->mfcc_mcastgrp.s_addr)); 1248 1249 s = splsoftnet(); 1250 1251 rt = mfc_find(&mfccp->mfcc_origin, &mfccp->mfcc_mcastgrp); 1252 if (rt == NULL) { 1253 splx(s); 1254 return (EADDRNOTAVAIL); 1255 } 1256 1257 /* 1258 * free the bw_meter entries 1259 */ 1260 free_bw_list(rt->mfc_bw_meter); 1261 rt->mfc_bw_meter = NULL; 1262 1263 LIST_REMOVE(rt, mfc_hash); 1264 free(rt, M_MRTABLE); 1265 1266 splx(s); 1267 return (0); 1268 } 1269 1270 static int 1271 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src) 1272 { 1273 if (s) { 1274 if (sbappendaddr(&s->so_rcv, sintosa(src), mm, NULL) != 0) { 1275 sorwakeup(s); 1276 return (0); 1277 } 1278 } 1279 m_freem(mm); 1280 return (-1); 1281 } 1282 1283 /* 1284 * IP multicast forwarding function. This function assumes that the packet 1285 * pointed to by "ip" has arrived on (or is about to be sent to) the interface 1286 * pointed to by "ifp", and the packet is to be relayed to other networks 1287 * that have members of the packet's destination IP multicast group. 1288 * 1289 * The packet is returned unscathed to the caller, unless it is 1290 * erroneous, in which case a non-zero return value tells the caller to 1291 * discard it. 1292 */ 1293 1294 #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */ 1295 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */ 1296 1297 int 1298 #ifdef RSVP_ISI 1299 ip_mforward(struct mbuf *m, struct ifnet *ifp, struct ip_moptions *imo) 1300 #else 1301 ip_mforward(struct mbuf *m, struct ifnet *ifp) 1302 #endif /* RSVP_ISI */ 1303 { 1304 struct ip *ip = mtod(m, struct ip *); 1305 struct mfc *rt; 1306 static int srctun = 0; 1307 struct mbuf *mm; 1308 struct sockaddr_in sin; 1309 int s; 1310 vifi_t vifi; 1311 1312 if (mrtdebug & DEBUG_FORWARD) 1313 log(LOG_DEBUG, "ip_mforward: src %x, dst %x, ifp %p\n", 1314 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr), ifp); 1315 1316 if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 || 1317 ((u_char *)(ip + 1))[1] != IPOPT_LSRR) { 1318 /* 1319 * Packet arrived via a physical interface or 1320 * an encapsulated tunnel or a register_vif. 1321 */ 1322 } else { 1323 /* 1324 * Packet arrived through a source-route tunnel. 1325 * Source-route tunnels are no longer supported. 1326 */ 1327 if ((srctun++ % 1000) == 0) 1328 log(LOG_ERR, 1329 "ip_mforward: received source-routed packet from %x\n", 1330 ntohl(ip->ip_src.s_addr)); 1331 1332 return (1); 1333 } 1334 1335 /* 1336 * Clear any in-bound checksum flags for this packet. 1337 */ 1338 m->m_pkthdr.csum_flags = 0; 1339 1340 #ifdef RSVP_ISI 1341 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) { 1342 if (ip->ip_ttl < MAXTTL) 1343 ip->ip_ttl++; /* compensate for -1 in *_send routines */ 1344 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) { 1345 struct vif *vifp = viftable + vifi; 1346 printf("Sending IPPROTO_RSVP from %x to %x on vif %d (%s%s)\n", 1347 ntohl(ip->ip_src), ntohl(ip->ip_dst), vifi, 1348 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "", 1349 vifp->v_ifp->if_xname); 1350 } 1351 return (ip_mdq(m, ifp, NULL, vifi)); 1352 } 1353 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) { 1354 printf("Warning: IPPROTO_RSVP from %x to %x without vif option\n", 1355 ntohl(ip->ip_src), ntohl(ip->ip_dst)); 1356 } 1357 #endif /* RSVP_ISI */ 1358 1359 /* 1360 * Don't forward a packet with time-to-live of zero or one, 1361 * or a packet destined to a local-only group. 1362 */ 1363 if (ip->ip_ttl <= 1 || IN_LOCAL_GROUP(ip->ip_dst.s_addr)) 1364 return (0); 1365 1366 /* 1367 * Determine forwarding vifs from the forwarding cache table 1368 */ 1369 s = splsoftnet(); 1370 ++mrtstat.mrts_mfc_lookups; 1371 rt = mfc_find(&ip->ip_src, &ip->ip_dst); 1372 1373 /* Entry exists, so forward if necessary */ 1374 if (rt != NULL) { 1375 splx(s); 1376 #ifdef RSVP_ISI 1377 return (ip_mdq(m, ifp, rt, -1)); 1378 #else 1379 return (ip_mdq(m, ifp, rt)); 1380 #endif /* RSVP_ISI */ 1381 } else { 1382 /* 1383 * If we don't have a route for packet's origin, 1384 * Make a copy of the packet & send message to routing daemon 1385 */ 1386 1387 struct mbuf *mb0; 1388 struct rtdetq *rte; 1389 u_int32_t hash; 1390 int hlen = ip->ip_hl << 2; 1391 #ifdef UPCALL_TIMING 1392 struct timeval tp; 1393 1394 microtime(&tp); 1395 #endif /* UPCALL_TIMING */ 1396 1397 ++mrtstat.mrts_mfc_misses; 1398 1399 mrtstat.mrts_no_route++; 1400 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC)) 1401 log(LOG_DEBUG, "ip_mforward: no rte s %x g %x\n", 1402 ntohl(ip->ip_src.s_addr), 1403 ntohl(ip->ip_dst.s_addr)); 1404 1405 /* 1406 * Allocate mbufs early so that we don't do extra work if we are 1407 * just going to fail anyway. Make sure to pullup the header so 1408 * that other people can't step on it. 1409 */ 1410 rte = (struct rtdetq *)malloc(sizeof(*rte), M_MRTABLE, 1411 M_NOWAIT); 1412 if (rte == NULL) { 1413 splx(s); 1414 return (ENOBUFS); 1415 } 1416 mb0 = m_copypacket(m, M_DONTWAIT); 1417 M_PULLUP(mb0, hlen); 1418 if (mb0 == NULL) { 1419 free(rte, M_MRTABLE); 1420 splx(s); 1421 return (ENOBUFS); 1422 } 1423 1424 /* is there an upcall waiting for this flow? */ 1425 hash = MFCHASH(ip->ip_src, ip->ip_dst); 1426 LIST_FOREACH(rt, &mfchashtbl[hash], mfc_hash) { 1427 if (in_hosteq(ip->ip_src, rt->mfc_origin) && 1428 in_hosteq(ip->ip_dst, rt->mfc_mcastgrp) && 1429 rt->mfc_stall != NULL) 1430 break; 1431 } 1432 1433 if (rt == NULL) { 1434 int i; 1435 struct igmpmsg *im; 1436 1437 /* 1438 * Locate the vifi for the incoming interface for 1439 * this packet. 1440 * If none found, drop packet. 1441 */ 1442 for (vifi = 0; vifi < numvifs && 1443 viftable[vifi].v_ifp != ifp; vifi++) 1444 ; 1445 if (vifi >= numvifs) /* vif not found, drop packet */ 1446 goto non_fatal; 1447 1448 /* no upcall, so make a new entry */ 1449 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, 1450 M_NOWAIT); 1451 if (rt == NULL) 1452 goto fail; 1453 1454 /* 1455 * Make a copy of the header to send to the user level 1456 * process 1457 */ 1458 mm = m_copym(m, 0, hlen, M_DONTWAIT); 1459 M_PULLUP(mm, hlen); 1460 if (mm == NULL) 1461 goto fail1; 1462 1463 /* 1464 * Send message to routing daemon to install 1465 * a route into the kernel table 1466 */ 1467 1468 im = mtod(mm, struct igmpmsg *); 1469 im->im_msgtype = IGMPMSG_NOCACHE; 1470 im->im_mbz = 0; 1471 im->im_vif = vifi; 1472 1473 mrtstat.mrts_upcalls++; 1474 1475 sockaddr_in_init(&sin, &ip->ip_src, 0); 1476 if (socket_send(ip_mrouter, mm, &sin) < 0) { 1477 log(LOG_WARNING, 1478 "ip_mforward: ip_mrouter socket queue full\n"); 1479 ++mrtstat.mrts_upq_sockfull; 1480 fail1: 1481 free(rt, M_MRTABLE); 1482 fail: 1483 free(rte, M_MRTABLE); 1484 m_freem(mb0); 1485 splx(s); 1486 return (ENOBUFS); 1487 } 1488 1489 /* insert new entry at head of hash chain */ 1490 rt->mfc_origin = ip->ip_src; 1491 rt->mfc_mcastgrp = ip->ip_dst; 1492 rt->mfc_pkt_cnt = 0; 1493 rt->mfc_byte_cnt = 0; 1494 rt->mfc_wrong_if = 0; 1495 rt->mfc_expire = UPCALL_EXPIRE; 1496 nexpire[hash]++; 1497 for (i = 0; i < numvifs; i++) { 1498 rt->mfc_ttls[i] = 0; 1499 rt->mfc_flags[i] = 0; 1500 } 1501 rt->mfc_parent = -1; 1502 1503 /* clear the RP address */ 1504 rt->mfc_rp = zeroin_addr; 1505 1506 rt->mfc_bw_meter = NULL; 1507 1508 /* link into table */ 1509 LIST_INSERT_HEAD(&mfchashtbl[hash], rt, mfc_hash); 1510 /* Add this entry to the end of the queue */ 1511 rt->mfc_stall = rte; 1512 } else { 1513 /* determine if q has overflowed */ 1514 struct rtdetq **p; 1515 int npkts = 0; 1516 1517 /* 1518 * XXX ouch! we need to append to the list, but we 1519 * only have a pointer to the front, so we have to 1520 * scan the entire list every time. 1521 */ 1522 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next) 1523 if (++npkts > MAX_UPQ) { 1524 mrtstat.mrts_upq_ovflw++; 1525 non_fatal: 1526 free(rte, M_MRTABLE); 1527 m_freem(mb0); 1528 splx(s); 1529 return (0); 1530 } 1531 1532 /* Add this entry to the end of the queue */ 1533 *p = rte; 1534 } 1535 1536 rte->next = NULL; 1537 rte->m = mb0; 1538 rte->ifp = ifp; 1539 #ifdef UPCALL_TIMING 1540 rte->t = tp; 1541 #endif /* UPCALL_TIMING */ 1542 1543 splx(s); 1544 1545 return (0); 1546 } 1547 } 1548 1549 1550 /*ARGSUSED*/ 1551 static void 1552 expire_upcalls(void *v) 1553 { 1554 int i; 1555 int s; 1556 1557 s = splsoftnet(); 1558 1559 for (i = 0; i < MFCTBLSIZ; i++) { 1560 struct mfc *rt, *nrt; 1561 1562 if (nexpire[i] == 0) 1563 continue; 1564 1565 for (rt = LIST_FIRST(&mfchashtbl[i]); rt; rt = nrt) { 1566 nrt = LIST_NEXT(rt, mfc_hash); 1567 1568 if (rt->mfc_expire == 0 || --rt->mfc_expire > 0) 1569 continue; 1570 nexpire[i]--; 1571 1572 /* 1573 * free the bw_meter entries 1574 */ 1575 while (rt->mfc_bw_meter != NULL) { 1576 struct bw_meter *x = rt->mfc_bw_meter; 1577 1578 rt->mfc_bw_meter = x->bm_mfc_next; 1579 free(x, M_BWMETER); 1580 } 1581 1582 ++mrtstat.mrts_cache_cleanups; 1583 if (mrtdebug & DEBUG_EXPIRE) 1584 log(LOG_DEBUG, 1585 "expire_upcalls: expiring (%x %x)\n", 1586 ntohl(rt->mfc_origin.s_addr), 1587 ntohl(rt->mfc_mcastgrp.s_addr)); 1588 1589 expire_mfc(rt); 1590 } 1591 } 1592 1593 splx(s); 1594 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, 1595 expire_upcalls, NULL); 1596 } 1597 1598 /* 1599 * Packet forwarding routine once entry in the cache is made 1600 */ 1601 static int 1602 #ifdef RSVP_ISI 1603 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif) 1604 #else 1605 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt) 1606 #endif /* RSVP_ISI */ 1607 { 1608 struct ip *ip = mtod(m, struct ip *); 1609 vifi_t vifi; 1610 struct vif *vifp; 1611 struct sockaddr_in sin; 1612 int plen = ntohs(ip->ip_len) - (ip->ip_hl << 2); 1613 1614 /* 1615 * Macro to send packet on vif. Since RSVP packets don't get counted on 1616 * input, they shouldn't get counted on output, so statistics keeping is 1617 * separate. 1618 */ 1619 #define MC_SEND(ip, vifp, m) do { \ 1620 if ((vifp)->v_flags & VIFF_TUNNEL) \ 1621 encap_send((ip), (vifp), (m)); \ 1622 else \ 1623 phyint_send((ip), (vifp), (m)); \ 1624 } while (/*CONSTCOND*/ 0) 1625 1626 #ifdef RSVP_ISI 1627 /* 1628 * If xmt_vif is not -1, send on only the requested vif. 1629 * 1630 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs. 1631 */ 1632 if (xmt_vif < numvifs) { 1633 #ifdef PIM 1634 if (viftable[xmt_vif].v_flags & VIFF_REGISTER) 1635 pim_register_send(ip, viftable + xmt_vif, m, rt); 1636 else 1637 #endif 1638 MC_SEND(ip, viftable + xmt_vif, m); 1639 return (1); 1640 } 1641 #endif /* RSVP_ISI */ 1642 1643 /* 1644 * Don't forward if it didn't arrive from the parent vif for its origin. 1645 */ 1646 vifi = rt->mfc_parent; 1647 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) { 1648 /* came in the wrong interface */ 1649 if (mrtdebug & DEBUG_FORWARD) 1650 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n", 1651 ifp, vifi, 1652 vifi >= numvifs ? 0 : viftable[vifi].v_ifp); 1653 ++mrtstat.mrts_wrong_if; 1654 ++rt->mfc_wrong_if; 1655 /* 1656 * If we are doing PIM assert processing, send a message 1657 * to the routing daemon. 1658 * 1659 * XXX: A PIM-SM router needs the WRONGVIF detection so it 1660 * can complete the SPT switch, regardless of the type 1661 * of the iif (broadcast media, GRE tunnel, etc). 1662 */ 1663 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) { 1664 struct timeval now; 1665 u_int32_t delta; 1666 1667 #ifdef PIM 1668 if (ifp == &multicast_register_if) 1669 pimstat.pims_rcv_registers_wrongiif++; 1670 #endif 1671 1672 /* Get vifi for the incoming packet */ 1673 for (vifi = 0; 1674 vifi < numvifs && viftable[vifi].v_ifp != ifp; 1675 vifi++) 1676 ; 1677 if (vifi >= numvifs) { 1678 /* The iif is not found: ignore the packet. */ 1679 return (0); 1680 } 1681 1682 if (rt->mfc_flags[vifi] & 1683 MRT_MFC_FLAGS_DISABLE_WRONGVIF) { 1684 /* WRONGVIF disabled: ignore the packet */ 1685 return (0); 1686 } 1687 1688 microtime(&now); 1689 1690 TV_DELTA(rt->mfc_last_assert, now, delta); 1691 1692 if (delta > ASSERT_MSG_TIME) { 1693 struct igmpmsg *im; 1694 int hlen = ip->ip_hl << 2; 1695 struct mbuf *mm = 1696 m_copym(m, 0, hlen, M_DONTWAIT); 1697 1698 M_PULLUP(mm, hlen); 1699 if (mm == NULL) 1700 return (ENOBUFS); 1701 1702 rt->mfc_last_assert = now; 1703 1704 im = mtod(mm, struct igmpmsg *); 1705 im->im_msgtype = IGMPMSG_WRONGVIF; 1706 im->im_mbz = 0; 1707 im->im_vif = vifi; 1708 1709 mrtstat.mrts_upcalls++; 1710 1711 sockaddr_in_init(&sin, &im->im_src, 0); 1712 if (socket_send(ip_mrouter, mm, &sin) < 0) { 1713 log(LOG_WARNING, 1714 "ip_mforward: ip_mrouter socket queue full\n"); 1715 ++mrtstat.mrts_upq_sockfull; 1716 return (ENOBUFS); 1717 } 1718 } 1719 } 1720 return (0); 1721 } 1722 1723 /* If I sourced this packet, it counts as output, else it was input. */ 1724 if (in_hosteq(ip->ip_src, viftable[vifi].v_lcl_addr)) { 1725 viftable[vifi].v_pkt_out++; 1726 viftable[vifi].v_bytes_out += plen; 1727 } else { 1728 viftable[vifi].v_pkt_in++; 1729 viftable[vifi].v_bytes_in += plen; 1730 } 1731 rt->mfc_pkt_cnt++; 1732 rt->mfc_byte_cnt += plen; 1733 1734 /* 1735 * For each vif, decide if a copy of the packet should be forwarded. 1736 * Forward if: 1737 * - the ttl exceeds the vif's threshold 1738 * - there are group members downstream on interface 1739 */ 1740 for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++) 1741 if ((rt->mfc_ttls[vifi] > 0) && 1742 (ip->ip_ttl > rt->mfc_ttls[vifi])) { 1743 vifp->v_pkt_out++; 1744 vifp->v_bytes_out += plen; 1745 #ifdef PIM 1746 if (vifp->v_flags & VIFF_REGISTER) 1747 pim_register_send(ip, vifp, m, rt); 1748 else 1749 #endif 1750 MC_SEND(ip, vifp, m); 1751 } 1752 1753 /* 1754 * Perform upcall-related bw measuring. 1755 */ 1756 if (rt->mfc_bw_meter != NULL) { 1757 struct bw_meter *x; 1758 struct timeval now; 1759 1760 microtime(&now); 1761 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) 1762 bw_meter_receive_packet(x, plen, &now); 1763 } 1764 1765 return (0); 1766 } 1767 1768 #ifdef RSVP_ISI 1769 /* 1770 * check if a vif number is legal/ok. This is used by ip_output. 1771 */ 1772 int 1773 legal_vif_num(int vif) 1774 { 1775 if (vif >= 0 && vif < numvifs) 1776 return (1); 1777 else 1778 return (0); 1779 } 1780 #endif /* RSVP_ISI */ 1781 1782 static void 1783 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m) 1784 { 1785 struct mbuf *mb_copy; 1786 int hlen = ip->ip_hl << 2; 1787 1788 /* 1789 * Make a new reference to the packet; make sure that 1790 * the IP header is actually copied, not just referenced, 1791 * so that ip_output() only scribbles on the copy. 1792 */ 1793 mb_copy = m_copypacket(m, M_DONTWAIT); 1794 M_PULLUP(mb_copy, hlen); 1795 if (mb_copy == NULL) 1796 return; 1797 1798 if (vifp->v_rate_limit <= 0) 1799 tbf_send_packet(vifp, mb_copy); 1800 else 1801 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), 1802 ntohs(ip->ip_len)); 1803 } 1804 1805 static void 1806 encap_send(struct ip *ip, struct vif *vifp, struct mbuf *m) 1807 { 1808 struct mbuf *mb_copy; 1809 struct ip *ip_copy; 1810 int i, len = ntohs(ip->ip_len) + sizeof(multicast_encap_iphdr); 1811 1812 /* Take care of delayed checksums */ 1813 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 1814 in_delayed_cksum(m); 1815 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 1816 } 1817 1818 /* 1819 * copy the old packet & pullup it's IP header into the 1820 * new mbuf so we can modify it. Try to fill the new 1821 * mbuf since if we don't the ethernet driver will. 1822 */ 1823 MGETHDR(mb_copy, M_DONTWAIT, MT_DATA); 1824 if (mb_copy == NULL) 1825 return; 1826 mb_copy->m_data += max_linkhdr; 1827 mb_copy->m_pkthdr.len = len; 1828 mb_copy->m_len = sizeof(multicast_encap_iphdr); 1829 1830 if ((mb_copy->m_next = m_copypacket(m, M_DONTWAIT)) == NULL) { 1831 m_freem(mb_copy); 1832 return; 1833 } 1834 i = MHLEN - max_linkhdr; 1835 if (i > len) 1836 i = len; 1837 mb_copy = m_pullup(mb_copy, i); 1838 if (mb_copy == NULL) 1839 return; 1840 1841 /* 1842 * fill in the encapsulating IP header. 1843 */ 1844 ip_copy = mtod(mb_copy, struct ip *); 1845 *ip_copy = multicast_encap_iphdr; 1846 if (len < IP_MINFRAGSIZE) 1847 ip_copy->ip_id = 0; 1848 else 1849 ip_copy->ip_id = ip_newid(NULL); 1850 ip_copy->ip_len = htons(len); 1851 ip_copy->ip_src = vifp->v_lcl_addr; 1852 ip_copy->ip_dst = vifp->v_rmt_addr; 1853 1854 /* 1855 * turn the encapsulated IP header back into a valid one. 1856 */ 1857 ip = (struct ip *)((char *)ip_copy + sizeof(multicast_encap_iphdr)); 1858 --ip->ip_ttl; 1859 ip->ip_sum = 0; 1860 mb_copy->m_data += sizeof(multicast_encap_iphdr); 1861 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2); 1862 mb_copy->m_data -= sizeof(multicast_encap_iphdr); 1863 1864 if (vifp->v_rate_limit <= 0) 1865 tbf_send_packet(vifp, mb_copy); 1866 else 1867 tbf_control(vifp, mb_copy, ip, ntohs(ip_copy->ip_len)); 1868 } 1869 1870 /* 1871 * De-encapsulate a packet and feed it back through ip input. 1872 */ 1873 static void 1874 vif_input(struct mbuf *m, ...) 1875 { 1876 int off, proto; 1877 va_list ap; 1878 struct vif *vifp; 1879 int s; 1880 struct ifqueue *ifq; 1881 1882 va_start(ap, m); 1883 off = va_arg(ap, int); 1884 proto = va_arg(ap, int); 1885 va_end(ap); 1886 1887 vifp = (struct vif *)encap_getarg(m); 1888 if (!vifp || proto != ENCAP_PROTO) { 1889 m_freem(m); 1890 mrtstat.mrts_bad_tunnel++; 1891 return; 1892 } 1893 1894 m_adj(m, off); 1895 m->m_pkthdr.rcvif = vifp->v_ifp; 1896 ifq = &ipintrq; 1897 s = splnet(); 1898 if (IF_QFULL(ifq)) { 1899 IF_DROP(ifq); 1900 m_freem(m); 1901 } else { 1902 IF_ENQUEUE(ifq, m); 1903 /* 1904 * normally we would need a "schednetisr(NETISR_IP)" 1905 * here but we were called by ip_input and it is going 1906 * to loop back & try to dequeue the packet we just 1907 * queued as soon as we return so we avoid the 1908 * unnecessary software interrrupt. 1909 */ 1910 } 1911 splx(s); 1912 } 1913 1914 /* 1915 * Check if the packet should be received on the vif denoted by arg. 1916 * (The encap selection code will call this once per vif since each is 1917 * registered separately.) 1918 */ 1919 static int 1920 vif_encapcheck(struct mbuf *m, int off, int proto, void *arg) 1921 { 1922 struct vif *vifp; 1923 struct ip ip; 1924 1925 #ifdef DIAGNOSTIC 1926 if (!arg || proto != IPPROTO_IPV4) 1927 panic("unexpected arg in vif_encapcheck"); 1928 #endif 1929 1930 /* 1931 * Accept the packet only if the inner heaader is multicast 1932 * and the outer header matches a tunnel-mode vif. Order 1933 * checks in the hope that common non-matching packets will be 1934 * rejected quickly. Assume that unicast IPv4 traffic in a 1935 * parallel tunnel (e.g. gif(4)) is unlikely. 1936 */ 1937 1938 /* Obtain the outer IP header and the vif pointer. */ 1939 m_copydata((struct mbuf *)m, 0, sizeof(ip), (void *)&ip); 1940 vifp = (struct vif *)arg; 1941 1942 /* 1943 * The outer source must match the vif's remote peer address. 1944 * For a multicast router with several tunnels, this is the 1945 * only check that will fail on packets in other tunnels, 1946 * assuming the local address is the same. 1947 */ 1948 if (!in_hosteq(vifp->v_rmt_addr, ip.ip_src)) 1949 return 0; 1950 1951 /* The outer destination must match the vif's local address. */ 1952 if (!in_hosteq(vifp->v_lcl_addr, ip.ip_dst)) 1953 return 0; 1954 1955 /* The vif must be of tunnel type. */ 1956 if ((vifp->v_flags & VIFF_TUNNEL) == 0) 1957 return 0; 1958 1959 /* Check that the inner destination is multicast. */ 1960 m_copydata((struct mbuf *)m, off, sizeof(ip), (void *)&ip); 1961 if (!IN_MULTICAST(ip.ip_dst.s_addr)) 1962 return 0; 1963 1964 /* 1965 * We have checked that both the outer src and dst addresses 1966 * match the vif, and that the inner destination is multicast 1967 * (224/5). By claiming more than 64, we intend to 1968 * preferentially take packets that also match a parallel 1969 * gif(4). 1970 */ 1971 return 32 + 32 + 5; 1972 } 1973 1974 /* 1975 * Token bucket filter module 1976 */ 1977 static void 1978 tbf_control(struct vif *vifp, struct mbuf *m, struct ip *ip, u_int32_t len) 1979 { 1980 1981 if (len > MAX_BKT_SIZE) { 1982 /* drop if packet is too large */ 1983 mrtstat.mrts_pkt2large++; 1984 m_freem(m); 1985 return; 1986 } 1987 1988 tbf_update_tokens(vifp); 1989 1990 /* 1991 * If there are enough tokens, and the queue is empty, send this packet 1992 * out immediately. Otherwise, try to insert it on this vif's queue. 1993 */ 1994 if (vifp->tbf_q_len == 0) { 1995 if (len <= vifp->tbf_n_tok) { 1996 vifp->tbf_n_tok -= len; 1997 tbf_send_packet(vifp, m); 1998 } else { 1999 /* queue packet and timeout till later */ 2000 tbf_queue(vifp, m); 2001 callout_reset(&vifp->v_repq_ch, TBF_REPROCESS, 2002 tbf_reprocess_q, vifp); 2003 } 2004 } else { 2005 if (vifp->tbf_q_len >= vifp->tbf_max_q_len && 2006 !tbf_dq_sel(vifp, ip)) { 2007 /* queue full, and couldn't make room */ 2008 mrtstat.mrts_q_overflow++; 2009 m_freem(m); 2010 } else { 2011 /* queue length low enough, or made room */ 2012 tbf_queue(vifp, m); 2013 tbf_process_q(vifp); 2014 } 2015 } 2016 } 2017 2018 /* 2019 * adds a packet to the queue at the interface 2020 */ 2021 static void 2022 tbf_queue(struct vif *vifp, struct mbuf *m) 2023 { 2024 int s = splsoftnet(); 2025 2026 /* insert at tail */ 2027 *vifp->tbf_t = m; 2028 vifp->tbf_t = &m->m_nextpkt; 2029 vifp->tbf_q_len++; 2030 2031 splx(s); 2032 } 2033 2034 2035 /* 2036 * processes the queue at the interface 2037 */ 2038 static void 2039 tbf_process_q(struct vif *vifp) 2040 { 2041 struct mbuf *m; 2042 int len; 2043 int s = splsoftnet(); 2044 2045 /* 2046 * Loop through the queue at the interface and send as many packets 2047 * as possible. 2048 */ 2049 for (m = vifp->tbf_q; m != NULL; m = vifp->tbf_q) { 2050 len = ntohs(mtod(m, struct ip *)->ip_len); 2051 2052 /* determine if the packet can be sent */ 2053 if (len <= vifp->tbf_n_tok) { 2054 /* if so, 2055 * reduce no of tokens, dequeue the packet, 2056 * send the packet. 2057 */ 2058 if ((vifp->tbf_q = m->m_nextpkt) == NULL) 2059 vifp->tbf_t = &vifp->tbf_q; 2060 --vifp->tbf_q_len; 2061 2062 m->m_nextpkt = NULL; 2063 vifp->tbf_n_tok -= len; 2064 tbf_send_packet(vifp, m); 2065 } else 2066 break; 2067 } 2068 splx(s); 2069 } 2070 2071 static void 2072 tbf_reprocess_q(void *arg) 2073 { 2074 struct vif *vifp = arg; 2075 2076 if (ip_mrouter == NULL) 2077 return; 2078 2079 tbf_update_tokens(vifp); 2080 tbf_process_q(vifp); 2081 2082 if (vifp->tbf_q_len != 0) 2083 callout_reset(&vifp->v_repq_ch, TBF_REPROCESS, 2084 tbf_reprocess_q, vifp); 2085 } 2086 2087 /* function that will selectively discard a member of the queue 2088 * based on the precedence value and the priority 2089 */ 2090 static int 2091 tbf_dq_sel(struct vif *vifp, struct ip *ip) 2092 { 2093 u_int p; 2094 struct mbuf **mp, *m; 2095 int s = splsoftnet(); 2096 2097 p = priority(vifp, ip); 2098 2099 for (mp = &vifp->tbf_q, m = *mp; 2100 m != NULL; 2101 mp = &m->m_nextpkt, m = *mp) { 2102 if (p > priority(vifp, mtod(m, struct ip *))) { 2103 if ((*mp = m->m_nextpkt) == NULL) 2104 vifp->tbf_t = mp; 2105 --vifp->tbf_q_len; 2106 2107 m_freem(m); 2108 mrtstat.mrts_drop_sel++; 2109 splx(s); 2110 return (1); 2111 } 2112 } 2113 splx(s); 2114 return (0); 2115 } 2116 2117 static void 2118 tbf_send_packet(struct vif *vifp, struct mbuf *m) 2119 { 2120 int error; 2121 int s = splsoftnet(); 2122 2123 if (vifp->v_flags & VIFF_TUNNEL) { 2124 /* If tunnel options */ 2125 ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, NULL, NULL); 2126 } else { 2127 /* if physical interface option, extract the options and then send */ 2128 struct ip_moptions imo; 2129 2130 imo.imo_multicast_ifp = vifp->v_ifp; 2131 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1; 2132 imo.imo_multicast_loop = 1; 2133 #ifdef RSVP_ISI 2134 imo.imo_multicast_vif = -1; 2135 #endif 2136 2137 error = ip_output(m, NULL, NULL, IP_FORWARDING|IP_MULTICASTOPTS, 2138 &imo, NULL); 2139 2140 if (mrtdebug & DEBUG_XMIT) 2141 log(LOG_DEBUG, "phyint_send on vif %ld err %d\n", 2142 (long)(vifp - viftable), error); 2143 } 2144 splx(s); 2145 } 2146 2147 /* determine the current time and then 2148 * the elapsed time (between the last time and time now) 2149 * in milliseconds & update the no. of tokens in the bucket 2150 */ 2151 static void 2152 tbf_update_tokens(struct vif *vifp) 2153 { 2154 struct timeval tp; 2155 u_int32_t tm; 2156 int s = splsoftnet(); 2157 2158 microtime(&tp); 2159 2160 TV_DELTA(tp, vifp->tbf_last_pkt_t, tm); 2161 2162 /* 2163 * This formula is actually 2164 * "time in seconds" * "bytes/second". 2165 * 2166 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8) 2167 * 2168 * The (1000/1024) was introduced in add_vif to optimize 2169 * this divide into a shift. 2170 */ 2171 vifp->tbf_n_tok += tm * vifp->v_rate_limit / 8192; 2172 vifp->tbf_last_pkt_t = tp; 2173 2174 if (vifp->tbf_n_tok > MAX_BKT_SIZE) 2175 vifp->tbf_n_tok = MAX_BKT_SIZE; 2176 2177 splx(s); 2178 } 2179 2180 static int 2181 priority(struct vif *vifp, struct ip *ip) 2182 { 2183 int prio = 50; /* the lowest priority -- default case */ 2184 2185 /* temporary hack; may add general packet classifier some day */ 2186 2187 /* 2188 * The UDP port space is divided up into four priority ranges: 2189 * [0, 16384) : unclassified - lowest priority 2190 * [16384, 32768) : audio - highest priority 2191 * [32768, 49152) : whiteboard - medium priority 2192 * [49152, 65536) : video - low priority 2193 */ 2194 if (ip->ip_p == IPPROTO_UDP) { 2195 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2)); 2196 2197 switch (ntohs(udp->uh_dport) & 0xc000) { 2198 case 0x4000: 2199 prio = 70; 2200 break; 2201 case 0x8000: 2202 prio = 60; 2203 break; 2204 case 0xc000: 2205 prio = 55; 2206 break; 2207 } 2208 2209 if (tbfdebug > 1) 2210 log(LOG_DEBUG, "port %x prio %d\n", 2211 ntohs(udp->uh_dport), prio); 2212 } 2213 2214 return (prio); 2215 } 2216 2217 /* 2218 * End of token bucket filter modifications 2219 */ 2220 #ifdef RSVP_ISI 2221 int 2222 ip_rsvp_vif_init(struct socket *so, struct mbuf *m) 2223 { 2224 int vifi, s; 2225 2226 if (rsvpdebug) 2227 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n", 2228 so->so_type, so->so_proto->pr_protocol); 2229 2230 if (so->so_type != SOCK_RAW || 2231 so->so_proto->pr_protocol != IPPROTO_RSVP) 2232 return (EOPNOTSUPP); 2233 2234 /* Check mbuf. */ 2235 if (m == NULL || m->m_len != sizeof(int)) { 2236 return (EINVAL); 2237 } 2238 vifi = *(mtod(m, int *)); 2239 2240 if (rsvpdebug) 2241 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", 2242 vifi, rsvp_on); 2243 2244 s = splsoftnet(); 2245 2246 /* Check vif. */ 2247 if (!legal_vif_num(vifi)) { 2248 splx(s); 2249 return (EADDRNOTAVAIL); 2250 } 2251 2252 /* Check if socket is available. */ 2253 if (viftable[vifi].v_rsvpd != NULL) { 2254 splx(s); 2255 return (EADDRINUSE); 2256 } 2257 2258 viftable[vifi].v_rsvpd = so; 2259 /* 2260 * This may seem silly, but we need to be sure we don't over-increment 2261 * the RSVP counter, in case something slips up. 2262 */ 2263 if (!viftable[vifi].v_rsvp_on) { 2264 viftable[vifi].v_rsvp_on = 1; 2265 rsvp_on++; 2266 } 2267 2268 splx(s); 2269 return (0); 2270 } 2271 2272 int 2273 ip_rsvp_vif_done(struct socket *so, struct mbuf *m) 2274 { 2275 int vifi, s; 2276 2277 if (rsvpdebug) 2278 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n", 2279 so->so_type, so->so_proto->pr_protocol); 2280 2281 if (so->so_type != SOCK_RAW || 2282 so->so_proto->pr_protocol != IPPROTO_RSVP) 2283 return (EOPNOTSUPP); 2284 2285 /* Check mbuf. */ 2286 if (m == NULL || m->m_len != sizeof(int)) { 2287 return (EINVAL); 2288 } 2289 vifi = *(mtod(m, int *)); 2290 2291 s = splsoftnet(); 2292 2293 /* Check vif. */ 2294 if (!legal_vif_num(vifi)) { 2295 splx(s); 2296 return (EADDRNOTAVAIL); 2297 } 2298 2299 if (rsvpdebug) 2300 printf("ip_rsvp_vif_done: v_rsvpd = %x so = %x\n", 2301 viftable[vifi].v_rsvpd, so); 2302 2303 viftable[vifi].v_rsvpd = NULL; 2304 /* 2305 * This may seem silly, but we need to be sure we don't over-decrement 2306 * the RSVP counter, in case something slips up. 2307 */ 2308 if (viftable[vifi].v_rsvp_on) { 2309 viftable[vifi].v_rsvp_on = 0; 2310 rsvp_on--; 2311 } 2312 2313 splx(s); 2314 return (0); 2315 } 2316 2317 void 2318 ip_rsvp_force_done(struct socket *so) 2319 { 2320 int vifi, s; 2321 2322 /* Don't bother if it is not the right type of socket. */ 2323 if (so->so_type != SOCK_RAW || 2324 so->so_proto->pr_protocol != IPPROTO_RSVP) 2325 return; 2326 2327 s = splsoftnet(); 2328 2329 /* 2330 * The socket may be attached to more than one vif...this 2331 * is perfectly legal. 2332 */ 2333 for (vifi = 0; vifi < numvifs; vifi++) { 2334 if (viftable[vifi].v_rsvpd == so) { 2335 viftable[vifi].v_rsvpd = NULL; 2336 /* 2337 * This may seem silly, but we need to be sure we don't 2338 * over-decrement the RSVP counter, in case something 2339 * slips up. 2340 */ 2341 if (viftable[vifi].v_rsvp_on) { 2342 viftable[vifi].v_rsvp_on = 0; 2343 rsvp_on--; 2344 } 2345 } 2346 } 2347 2348 splx(s); 2349 return; 2350 } 2351 2352 void 2353 rsvp_input(struct mbuf *m, struct ifnet *ifp) 2354 { 2355 int vifi, s; 2356 struct ip *ip = mtod(m, struct ip *); 2357 struct sockaddr_in rsvp_src; 2358 2359 if (rsvpdebug) 2360 printf("rsvp_input: rsvp_on %d\n", rsvp_on); 2361 2362 /* 2363 * Can still get packets with rsvp_on = 0 if there is a local member 2364 * of the group to which the RSVP packet is addressed. But in this 2365 * case we want to throw the packet away. 2366 */ 2367 if (!rsvp_on) { 2368 m_freem(m); 2369 return; 2370 } 2371 2372 /* 2373 * If the old-style non-vif-associated socket is set, then use 2374 * it and ignore the new ones. 2375 */ 2376 if (ip_rsvpd != NULL) { 2377 if (rsvpdebug) 2378 printf("rsvp_input: " 2379 "Sending packet up old-style socket\n"); 2380 rip_input(m); /*XXX*/ 2381 return; 2382 } 2383 2384 s = splsoftnet(); 2385 2386 if (rsvpdebug) 2387 printf("rsvp_input: check vifs\n"); 2388 2389 /* Find which vif the packet arrived on. */ 2390 for (vifi = 0; vifi < numvifs; vifi++) { 2391 if (viftable[vifi].v_ifp == ifp) 2392 break; 2393 } 2394 2395 if (vifi == numvifs) { 2396 /* Can't find vif packet arrived on. Drop packet. */ 2397 if (rsvpdebug) 2398 printf("rsvp_input: " 2399 "Can't find vif for packet...dropping it.\n"); 2400 m_freem(m); 2401 splx(s); 2402 return; 2403 } 2404 2405 if (rsvpdebug) 2406 printf("rsvp_input: check socket\n"); 2407 2408 if (viftable[vifi].v_rsvpd == NULL) { 2409 /* 2410 * drop packet, since there is no specific socket for this 2411 * interface 2412 */ 2413 if (rsvpdebug) 2414 printf("rsvp_input: No socket defined for vif %d\n", 2415 vifi); 2416 m_freem(m); 2417 splx(s); 2418 return; 2419 } 2420 2421 sockaddr_in_init(&rsvp_src, &ip->ip_src, 0); 2422 2423 if (rsvpdebug && m) 2424 printf("rsvp_input: m->m_len = %d, sbspace() = %d\n", 2425 m->m_len, sbspace(&viftable[vifi].v_rsvpd->so_rcv)); 2426 2427 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) 2428 if (rsvpdebug) 2429 printf("rsvp_input: Failed to append to socket\n"); 2430 else 2431 if (rsvpdebug) 2432 printf("rsvp_input: send packet up\n"); 2433 2434 splx(s); 2435 } 2436 #endif /* RSVP_ISI */ 2437 2438 /* 2439 * Code for bandwidth monitors 2440 */ 2441 2442 /* 2443 * Define common interface for timeval-related methods 2444 */ 2445 #define BW_TIMEVALCMP(tvp, uvp, cmp) timercmp((tvp), (uvp), cmp) 2446 #define BW_TIMEVALDECR(vvp, uvp) timersub((vvp), (uvp), (vvp)) 2447 #define BW_TIMEVALADD(vvp, uvp) timeradd((vvp), (uvp), (vvp)) 2448 2449 static uint32_t 2450 compute_bw_meter_flags(struct bw_upcall *req) 2451 { 2452 uint32_t flags = 0; 2453 2454 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS) 2455 flags |= BW_METER_UNIT_PACKETS; 2456 if (req->bu_flags & BW_UPCALL_UNIT_BYTES) 2457 flags |= BW_METER_UNIT_BYTES; 2458 if (req->bu_flags & BW_UPCALL_GEQ) 2459 flags |= BW_METER_GEQ; 2460 if (req->bu_flags & BW_UPCALL_LEQ) 2461 flags |= BW_METER_LEQ; 2462 2463 return flags; 2464 } 2465 2466 /* 2467 * Add a bw_meter entry 2468 */ 2469 static int 2470 add_bw_upcall(struct bw_upcall *req) 2471 { 2472 int s; 2473 struct mfc *mfc; 2474 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC, 2475 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC }; 2476 struct timeval now; 2477 struct bw_meter *x; 2478 uint32_t flags; 2479 2480 if (!(mrt_api_config & MRT_MFC_BW_UPCALL)) 2481 return EOPNOTSUPP; 2482 2483 /* Test if the flags are valid */ 2484 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES))) 2485 return EINVAL; 2486 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))) 2487 return EINVAL; 2488 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)) 2489 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ)) 2490 return EINVAL; 2491 2492 /* Test if the threshold time interval is valid */ 2493 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <)) 2494 return EINVAL; 2495 2496 flags = compute_bw_meter_flags(req); 2497 2498 /* 2499 * Find if we have already same bw_meter entry 2500 */ 2501 s = splsoftnet(); 2502 mfc = mfc_find(&req->bu_src, &req->bu_dst); 2503 if (mfc == NULL) { 2504 splx(s); 2505 return EADDRNOTAVAIL; 2506 } 2507 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) { 2508 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time, 2509 &req->bu_threshold.b_time, ==)) && 2510 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) && 2511 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) && 2512 (x->bm_flags & BW_METER_USER_FLAGS) == flags) { 2513 splx(s); 2514 return 0; /* XXX Already installed */ 2515 } 2516 } 2517 2518 /* Allocate the new bw_meter entry */ 2519 x = (struct bw_meter *)malloc(sizeof(*x), M_BWMETER, M_NOWAIT); 2520 if (x == NULL) { 2521 splx(s); 2522 return ENOBUFS; 2523 } 2524 2525 /* Set the new bw_meter entry */ 2526 x->bm_threshold.b_time = req->bu_threshold.b_time; 2527 microtime(&now); 2528 x->bm_start_time = now; 2529 x->bm_threshold.b_packets = req->bu_threshold.b_packets; 2530 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes; 2531 x->bm_measured.b_packets = 0; 2532 x->bm_measured.b_bytes = 0; 2533 x->bm_flags = flags; 2534 x->bm_time_next = NULL; 2535 x->bm_time_hash = BW_METER_BUCKETS; 2536 2537 /* Add the new bw_meter entry to the front of entries for this MFC */ 2538 x->bm_mfc = mfc; 2539 x->bm_mfc_next = mfc->mfc_bw_meter; 2540 mfc->mfc_bw_meter = x; 2541 schedule_bw_meter(x, &now); 2542 splx(s); 2543 2544 return 0; 2545 } 2546 2547 static void 2548 free_bw_list(struct bw_meter *list) 2549 { 2550 while (list != NULL) { 2551 struct bw_meter *x = list; 2552 2553 list = list->bm_mfc_next; 2554 unschedule_bw_meter(x); 2555 free(x, M_BWMETER); 2556 } 2557 } 2558 2559 /* 2560 * Delete one or multiple bw_meter entries 2561 */ 2562 static int 2563 del_bw_upcall(struct bw_upcall *req) 2564 { 2565 int s; 2566 struct mfc *mfc; 2567 struct bw_meter *x; 2568 2569 if (!(mrt_api_config & MRT_MFC_BW_UPCALL)) 2570 return EOPNOTSUPP; 2571 2572 s = splsoftnet(); 2573 /* Find the corresponding MFC entry */ 2574 mfc = mfc_find(&req->bu_src, &req->bu_dst); 2575 if (mfc == NULL) { 2576 splx(s); 2577 return EADDRNOTAVAIL; 2578 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) { 2579 /* 2580 * Delete all bw_meter entries for this mfc 2581 */ 2582 struct bw_meter *list; 2583 2584 list = mfc->mfc_bw_meter; 2585 mfc->mfc_bw_meter = NULL; 2586 free_bw_list(list); 2587 splx(s); 2588 return 0; 2589 } else { /* Delete a single bw_meter entry */ 2590 struct bw_meter *prev; 2591 uint32_t flags = 0; 2592 2593 flags = compute_bw_meter_flags(req); 2594 2595 /* Find the bw_meter entry to delete */ 2596 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL; 2597 prev = x, x = x->bm_mfc_next) { 2598 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time, 2599 &req->bu_threshold.b_time, ==)) && 2600 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) && 2601 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) && 2602 (x->bm_flags & BW_METER_USER_FLAGS) == flags) 2603 break; 2604 } 2605 if (x != NULL) { /* Delete entry from the list for this MFC */ 2606 if (prev != NULL) 2607 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/ 2608 else 2609 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */ 2610 2611 unschedule_bw_meter(x); 2612 splx(s); 2613 /* Free the bw_meter entry */ 2614 free(x, M_BWMETER); 2615 return 0; 2616 } else { 2617 splx(s); 2618 return EINVAL; 2619 } 2620 } 2621 /* NOTREACHED */ 2622 } 2623 2624 /* 2625 * Perform bandwidth measurement processing that may result in an upcall 2626 */ 2627 static void 2628 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp) 2629 { 2630 struct timeval delta; 2631 2632 delta = *nowp; 2633 BW_TIMEVALDECR(&delta, &x->bm_start_time); 2634 2635 if (x->bm_flags & BW_METER_GEQ) { 2636 /* 2637 * Processing for ">=" type of bw_meter entry 2638 */ 2639 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) { 2640 /* Reset the bw_meter entry */ 2641 x->bm_start_time = *nowp; 2642 x->bm_measured.b_packets = 0; 2643 x->bm_measured.b_bytes = 0; 2644 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED; 2645 } 2646 2647 /* Record that a packet is received */ 2648 x->bm_measured.b_packets++; 2649 x->bm_measured.b_bytes += plen; 2650 2651 /* 2652 * Test if we should deliver an upcall 2653 */ 2654 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) { 2655 if (((x->bm_flags & BW_METER_UNIT_PACKETS) && 2656 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) || 2657 ((x->bm_flags & BW_METER_UNIT_BYTES) && 2658 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) { 2659 /* Prepare an upcall for delivery */ 2660 bw_meter_prepare_upcall(x, nowp); 2661 x->bm_flags |= BW_METER_UPCALL_DELIVERED; 2662 } 2663 } 2664 } else if (x->bm_flags & BW_METER_LEQ) { 2665 /* 2666 * Processing for "<=" type of bw_meter entry 2667 */ 2668 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) { 2669 /* 2670 * We are behind time with the multicast forwarding table 2671 * scanning for "<=" type of bw_meter entries, so test now 2672 * if we should deliver an upcall. 2673 */ 2674 if (((x->bm_flags & BW_METER_UNIT_PACKETS) && 2675 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) || 2676 ((x->bm_flags & BW_METER_UNIT_BYTES) && 2677 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) { 2678 /* Prepare an upcall for delivery */ 2679 bw_meter_prepare_upcall(x, nowp); 2680 } 2681 /* Reschedule the bw_meter entry */ 2682 unschedule_bw_meter(x); 2683 schedule_bw_meter(x, nowp); 2684 } 2685 2686 /* Record that a packet is received */ 2687 x->bm_measured.b_packets++; 2688 x->bm_measured.b_bytes += plen; 2689 2690 /* 2691 * Test if we should restart the measuring interval 2692 */ 2693 if ((x->bm_flags & BW_METER_UNIT_PACKETS && 2694 x->bm_measured.b_packets <= x->bm_threshold.b_packets) || 2695 (x->bm_flags & BW_METER_UNIT_BYTES && 2696 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) { 2697 /* Don't restart the measuring interval */ 2698 } else { 2699 /* Do restart the measuring interval */ 2700 /* 2701 * XXX: note that we don't unschedule and schedule, because this 2702 * might be too much overhead per packet. Instead, when we process 2703 * all entries for a given timer hash bin, we check whether it is 2704 * really a timeout. If not, we reschedule at that time. 2705 */ 2706 x->bm_start_time = *nowp; 2707 x->bm_measured.b_packets = 0; 2708 x->bm_measured.b_bytes = 0; 2709 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED; 2710 } 2711 } 2712 } 2713 2714 /* 2715 * Prepare a bandwidth-related upcall 2716 */ 2717 static void 2718 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp) 2719 { 2720 struct timeval delta; 2721 struct bw_upcall *u; 2722 2723 /* 2724 * Compute the measured time interval 2725 */ 2726 delta = *nowp; 2727 BW_TIMEVALDECR(&delta, &x->bm_start_time); 2728 2729 /* 2730 * If there are too many pending upcalls, deliver them now 2731 */ 2732 if (bw_upcalls_n >= BW_UPCALLS_MAX) 2733 bw_upcalls_send(); 2734 2735 /* 2736 * Set the bw_upcall entry 2737 */ 2738 u = &bw_upcalls[bw_upcalls_n++]; 2739 u->bu_src = x->bm_mfc->mfc_origin; 2740 u->bu_dst = x->bm_mfc->mfc_mcastgrp; 2741 u->bu_threshold.b_time = x->bm_threshold.b_time; 2742 u->bu_threshold.b_packets = x->bm_threshold.b_packets; 2743 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes; 2744 u->bu_measured.b_time = delta; 2745 u->bu_measured.b_packets = x->bm_measured.b_packets; 2746 u->bu_measured.b_bytes = x->bm_measured.b_bytes; 2747 u->bu_flags = 0; 2748 if (x->bm_flags & BW_METER_UNIT_PACKETS) 2749 u->bu_flags |= BW_UPCALL_UNIT_PACKETS; 2750 if (x->bm_flags & BW_METER_UNIT_BYTES) 2751 u->bu_flags |= BW_UPCALL_UNIT_BYTES; 2752 if (x->bm_flags & BW_METER_GEQ) 2753 u->bu_flags |= BW_UPCALL_GEQ; 2754 if (x->bm_flags & BW_METER_LEQ) 2755 u->bu_flags |= BW_UPCALL_LEQ; 2756 } 2757 2758 /* 2759 * Send the pending bandwidth-related upcalls 2760 */ 2761 static void 2762 bw_upcalls_send(void) 2763 { 2764 struct mbuf *m; 2765 int len = bw_upcalls_n * sizeof(bw_upcalls[0]); 2766 struct sockaddr_in k_igmpsrc = { 2767 .sin_len = sizeof(k_igmpsrc), 2768 .sin_family = AF_INET, 2769 }; 2770 static struct igmpmsg igmpmsg = { 0, /* unused1 */ 2771 0, /* unused2 */ 2772 IGMPMSG_BW_UPCALL,/* im_msgtype */ 2773 0, /* im_mbz */ 2774 0, /* im_vif */ 2775 0, /* unused3 */ 2776 { 0 }, /* im_src */ 2777 { 0 } }; /* im_dst */ 2778 2779 if (bw_upcalls_n == 0) 2780 return; /* No pending upcalls */ 2781 2782 bw_upcalls_n = 0; 2783 2784 /* 2785 * Allocate a new mbuf, initialize it with the header and 2786 * the payload for the pending calls. 2787 */ 2788 MGETHDR(m, M_DONTWAIT, MT_HEADER); 2789 if (m == NULL) { 2790 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n"); 2791 return; 2792 } 2793 2794 m->m_len = m->m_pkthdr.len = 0; 2795 m_copyback(m, 0, sizeof(struct igmpmsg), (void *)&igmpmsg); 2796 m_copyback(m, sizeof(struct igmpmsg), len, (void *)&bw_upcalls[0]); 2797 2798 /* 2799 * Send the upcalls 2800 * XXX do we need to set the address in k_igmpsrc ? 2801 */ 2802 mrtstat.mrts_upcalls++; 2803 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) { 2804 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n"); 2805 ++mrtstat.mrts_upq_sockfull; 2806 } 2807 } 2808 2809 /* 2810 * Compute the timeout hash value for the bw_meter entries 2811 */ 2812 #define BW_METER_TIMEHASH(bw_meter, hash) \ 2813 do { \ 2814 struct timeval next_timeval = (bw_meter)->bm_start_time; \ 2815 \ 2816 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \ 2817 (hash) = next_timeval.tv_sec; \ 2818 if (next_timeval.tv_usec) \ 2819 (hash)++; /* XXX: make sure we don't timeout early */ \ 2820 (hash) %= BW_METER_BUCKETS; \ 2821 } while (/*CONSTCOND*/ 0) 2822 2823 /* 2824 * Schedule a timer to process periodically bw_meter entry of type "<=" 2825 * by linking the entry in the proper hash bucket. 2826 */ 2827 static void 2828 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp) 2829 { 2830 int time_hash; 2831 2832 if (!(x->bm_flags & BW_METER_LEQ)) 2833 return; /* XXX: we schedule timers only for "<=" entries */ 2834 2835 /* 2836 * Reset the bw_meter entry 2837 */ 2838 x->bm_start_time = *nowp; 2839 x->bm_measured.b_packets = 0; 2840 x->bm_measured.b_bytes = 0; 2841 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED; 2842 2843 /* 2844 * Compute the timeout hash value and insert the entry 2845 */ 2846 BW_METER_TIMEHASH(x, time_hash); 2847 x->bm_time_next = bw_meter_timers[time_hash]; 2848 bw_meter_timers[time_hash] = x; 2849 x->bm_time_hash = time_hash; 2850 } 2851 2852 /* 2853 * Unschedule the periodic timer that processes bw_meter entry of type "<=" 2854 * by removing the entry from the proper hash bucket. 2855 */ 2856 static void 2857 unschedule_bw_meter(struct bw_meter *x) 2858 { 2859 int time_hash; 2860 struct bw_meter *prev, *tmp; 2861 2862 if (!(x->bm_flags & BW_METER_LEQ)) 2863 return; /* XXX: we schedule timers only for "<=" entries */ 2864 2865 /* 2866 * Compute the timeout hash value and delete the entry 2867 */ 2868 time_hash = x->bm_time_hash; 2869 if (time_hash >= BW_METER_BUCKETS) 2870 return; /* Entry was not scheduled */ 2871 2872 for (prev = NULL, tmp = bw_meter_timers[time_hash]; 2873 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next) 2874 if (tmp == x) 2875 break; 2876 2877 if (tmp == NULL) 2878 panic("unschedule_bw_meter: bw_meter entry not found"); 2879 2880 if (prev != NULL) 2881 prev->bm_time_next = x->bm_time_next; 2882 else 2883 bw_meter_timers[time_hash] = x->bm_time_next; 2884 2885 x->bm_time_next = NULL; 2886 x->bm_time_hash = BW_METER_BUCKETS; 2887 } 2888 2889 /* 2890 * Process all "<=" type of bw_meter that should be processed now, 2891 * and for each entry prepare an upcall if necessary. Each processed 2892 * entry is rescheduled again for the (periodic) processing. 2893 * 2894 * This is run periodically (once per second normally). On each round, 2895 * all the potentially matching entries are in the hash slot that we are 2896 * looking at. 2897 */ 2898 static void 2899 bw_meter_process(void) 2900 { 2901 int s; 2902 static uint32_t last_tv_sec; /* last time we processed this */ 2903 2904 uint32_t loops; 2905 int i; 2906 struct timeval now, process_endtime; 2907 2908 microtime(&now); 2909 if (last_tv_sec == now.tv_sec) 2910 return; /* nothing to do */ 2911 2912 loops = now.tv_sec - last_tv_sec; 2913 last_tv_sec = now.tv_sec; 2914 if (loops > BW_METER_BUCKETS) 2915 loops = BW_METER_BUCKETS; 2916 2917 s = splsoftnet(); 2918 /* 2919 * Process all bins of bw_meter entries from the one after the last 2920 * processed to the current one. On entry, i points to the last bucket 2921 * visited, so we need to increment i at the beginning of the loop. 2922 */ 2923 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) { 2924 struct bw_meter *x, *tmp_list; 2925 2926 if (++i >= BW_METER_BUCKETS) 2927 i = 0; 2928 2929 /* Disconnect the list of bw_meter entries from the bin */ 2930 tmp_list = bw_meter_timers[i]; 2931 bw_meter_timers[i] = NULL; 2932 2933 /* Process the list of bw_meter entries */ 2934 while (tmp_list != NULL) { 2935 x = tmp_list; 2936 tmp_list = tmp_list->bm_time_next; 2937 2938 /* Test if the time interval is over */ 2939 process_endtime = x->bm_start_time; 2940 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time); 2941 if (BW_TIMEVALCMP(&process_endtime, &now, >)) { 2942 /* Not yet: reschedule, but don't reset */ 2943 int time_hash; 2944 2945 BW_METER_TIMEHASH(x, time_hash); 2946 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) { 2947 /* 2948 * XXX: somehow the bin processing is a bit ahead of time. 2949 * Put the entry in the next bin. 2950 */ 2951 if (++time_hash >= BW_METER_BUCKETS) 2952 time_hash = 0; 2953 } 2954 x->bm_time_next = bw_meter_timers[time_hash]; 2955 bw_meter_timers[time_hash] = x; 2956 x->bm_time_hash = time_hash; 2957 2958 continue; 2959 } 2960 2961 /* 2962 * Test if we should deliver an upcall 2963 */ 2964 if (((x->bm_flags & BW_METER_UNIT_PACKETS) && 2965 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) || 2966 ((x->bm_flags & BW_METER_UNIT_BYTES) && 2967 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) { 2968 /* Prepare an upcall for delivery */ 2969 bw_meter_prepare_upcall(x, &now); 2970 } 2971 2972 /* 2973 * Reschedule for next processing 2974 */ 2975 schedule_bw_meter(x, &now); 2976 } 2977 } 2978 2979 /* Send all upcalls that are pending delivery */ 2980 bw_upcalls_send(); 2981 2982 splx(s); 2983 } 2984 2985 /* 2986 * A periodic function for sending all upcalls that are pending delivery 2987 */ 2988 static void 2989 expire_bw_upcalls_send(void *unused) 2990 { 2991 int s; 2992 2993 s = splsoftnet(); 2994 bw_upcalls_send(); 2995 splx(s); 2996 2997 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD, 2998 expire_bw_upcalls_send, NULL); 2999 } 3000 3001 /* 3002 * A periodic function for periodic scanning of the multicast forwarding 3003 * table for processing all "<=" bw_meter entries. 3004 */ 3005 static void 3006 expire_bw_meter_process(void *unused) 3007 { 3008 if (mrt_api_config & MRT_MFC_BW_UPCALL) 3009 bw_meter_process(); 3010 3011 callout_reset(&bw_meter_ch, BW_METER_PERIOD, 3012 expire_bw_meter_process, NULL); 3013 } 3014 3015 /* 3016 * End of bandwidth monitoring code 3017 */ 3018 3019 #ifdef PIM 3020 /* 3021 * Send the packet up to the user daemon, or eventually do kernel encapsulation 3022 */ 3023 static int 3024 pim_register_send(struct ip *ip, struct vif *vifp, 3025 struct mbuf *m, struct mfc *rt) 3026 { 3027 struct mbuf *mb_copy, *mm; 3028 3029 if (mrtdebug & DEBUG_PIM) 3030 log(LOG_DEBUG, "pim_register_send: "); 3031 3032 mb_copy = pim_register_prepare(ip, m); 3033 if (mb_copy == NULL) 3034 return ENOBUFS; 3035 3036 /* 3037 * Send all the fragments. Note that the mbuf for each fragment 3038 * is freed by the sending machinery. 3039 */ 3040 for (mm = mb_copy; mm; mm = mb_copy) { 3041 mb_copy = mm->m_nextpkt; 3042 mm->m_nextpkt = NULL; 3043 mm = m_pullup(mm, sizeof(struct ip)); 3044 if (mm != NULL) { 3045 ip = mtod(mm, struct ip *); 3046 if ((mrt_api_config & MRT_MFC_RP) && 3047 !in_nullhost(rt->mfc_rp)) { 3048 pim_register_send_rp(ip, vifp, mm, rt); 3049 } else { 3050 pim_register_send_upcall(ip, vifp, mm, rt); 3051 } 3052 } 3053 } 3054 3055 return 0; 3056 } 3057 3058 /* 3059 * Return a copy of the data packet that is ready for PIM Register 3060 * encapsulation. 3061 * XXX: Note that in the returned copy the IP header is a valid one. 3062 */ 3063 static struct mbuf * 3064 pim_register_prepare(struct ip *ip, struct mbuf *m) 3065 { 3066 struct mbuf *mb_copy = NULL; 3067 int mtu; 3068 3069 /* Take care of delayed checksums */ 3070 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 3071 in_delayed_cksum(m); 3072 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 3073 } 3074 3075 /* 3076 * Copy the old packet & pullup its IP header into the 3077 * new mbuf so we can modify it. 3078 */ 3079 mb_copy = m_copypacket(m, M_DONTWAIT); 3080 if (mb_copy == NULL) 3081 return NULL; 3082 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2); 3083 if (mb_copy == NULL) 3084 return NULL; 3085 3086 /* take care of the TTL */ 3087 ip = mtod(mb_copy, struct ip *); 3088 --ip->ip_ttl; 3089 3090 /* Compute the MTU after the PIM Register encapsulation */ 3091 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr); 3092 3093 if (ntohs(ip->ip_len) <= mtu) { 3094 /* Turn the IP header into a valid one */ 3095 ip->ip_sum = 0; 3096 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2); 3097 } else { 3098 /* Fragment the packet */ 3099 if (ip_fragment(mb_copy, NULL, mtu) != 0) { 3100 /* XXX: mb_copy was freed by ip_fragment() */ 3101 return NULL; 3102 } 3103 } 3104 return mb_copy; 3105 } 3106 3107 /* 3108 * Send an upcall with the data packet to the user-level process. 3109 */ 3110 static int 3111 pim_register_send_upcall(struct ip *ip, struct vif *vifp, 3112 struct mbuf *mb_copy, struct mfc *rt) 3113 { 3114 struct mbuf *mb_first; 3115 int len = ntohs(ip->ip_len); 3116 struct igmpmsg *im; 3117 struct sockaddr_in k_igmpsrc = { 3118 .sin_len = sizeof(k_igmpsrc), 3119 .sin_family = AF_INET, 3120 }; 3121 3122 /* 3123 * Add a new mbuf with an upcall header 3124 */ 3125 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER); 3126 if (mb_first == NULL) { 3127 m_freem(mb_copy); 3128 return ENOBUFS; 3129 } 3130 mb_first->m_data += max_linkhdr; 3131 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg); 3132 mb_first->m_len = sizeof(struct igmpmsg); 3133 mb_first->m_next = mb_copy; 3134 3135 /* Send message to routing daemon */ 3136 im = mtod(mb_first, struct igmpmsg *); 3137 im->im_msgtype = IGMPMSG_WHOLEPKT; 3138 im->im_mbz = 0; 3139 im->im_vif = vifp - viftable; 3140 im->im_src = ip->ip_src; 3141 im->im_dst = ip->ip_dst; 3142 3143 k_igmpsrc.sin_addr = ip->ip_src; 3144 3145 mrtstat.mrts_upcalls++; 3146 3147 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) { 3148 if (mrtdebug & DEBUG_PIM) 3149 log(LOG_WARNING, 3150 "mcast: pim_register_send_upcall: ip_mrouter socket queue full"); 3151 ++mrtstat.mrts_upq_sockfull; 3152 return ENOBUFS; 3153 } 3154 3155 /* Keep statistics */ 3156 pimstat.pims_snd_registers_msgs++; 3157 pimstat.pims_snd_registers_bytes += len; 3158 3159 return 0; 3160 } 3161 3162 /* 3163 * Encapsulate the data packet in PIM Register message and send it to the RP. 3164 */ 3165 static int 3166 pim_register_send_rp(struct ip *ip, struct vif *vifp, 3167 struct mbuf *mb_copy, struct mfc *rt) 3168 { 3169 struct mbuf *mb_first; 3170 struct ip *ip_outer; 3171 struct pim_encap_pimhdr *pimhdr; 3172 int len = ntohs(ip->ip_len); 3173 vifi_t vifi = rt->mfc_parent; 3174 3175 if ((vifi >= numvifs) || in_nullhost(viftable[vifi].v_lcl_addr)) { 3176 m_freem(mb_copy); 3177 return EADDRNOTAVAIL; /* The iif vif is invalid */ 3178 } 3179 3180 /* 3181 * Add a new mbuf with the encapsulating header 3182 */ 3183 MGETHDR(mb_first, M_DONTWAIT, MT_HEADER); 3184 if (mb_first == NULL) { 3185 m_freem(mb_copy); 3186 return ENOBUFS; 3187 } 3188 mb_first->m_data += max_linkhdr; 3189 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr); 3190 mb_first->m_next = mb_copy; 3191 3192 mb_first->m_pkthdr.len = len + mb_first->m_len; 3193 3194 /* 3195 * Fill in the encapsulating IP and PIM header 3196 */ 3197 ip_outer = mtod(mb_first, struct ip *); 3198 *ip_outer = pim_encap_iphdr; 3199 if (mb_first->m_pkthdr.len < IP_MINFRAGSIZE) 3200 ip_outer->ip_id = 0; 3201 else 3202 ip_outer->ip_id = ip_newid(NULL); 3203 ip_outer->ip_len = htons(len + sizeof(pim_encap_iphdr) + 3204 sizeof(pim_encap_pimhdr)); 3205 ip_outer->ip_src = viftable[vifi].v_lcl_addr; 3206 ip_outer->ip_dst = rt->mfc_rp; 3207 /* 3208 * Copy the inner header TOS to the outer header, and take care of the 3209 * IP_DF bit. 3210 */ 3211 ip_outer->ip_tos = ip->ip_tos; 3212 if (ntohs(ip->ip_off) & IP_DF) 3213 ip_outer->ip_off |= htons(IP_DF); 3214 pimhdr = (struct pim_encap_pimhdr *)((char *)ip_outer 3215 + sizeof(pim_encap_iphdr)); 3216 *pimhdr = pim_encap_pimhdr; 3217 /* If the iif crosses a border, set the Border-bit */ 3218 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config) 3219 pimhdr->flags |= htonl(PIM_BORDER_REGISTER); 3220 3221 mb_first->m_data += sizeof(pim_encap_iphdr); 3222 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr)); 3223 mb_first->m_data -= sizeof(pim_encap_iphdr); 3224 3225 if (vifp->v_rate_limit == 0) 3226 tbf_send_packet(vifp, mb_first); 3227 else 3228 tbf_control(vifp, mb_first, ip, ntohs(ip_outer->ip_len)); 3229 3230 /* Keep statistics */ 3231 pimstat.pims_snd_registers_msgs++; 3232 pimstat.pims_snd_registers_bytes += len; 3233 3234 return 0; 3235 } 3236 3237 /* 3238 * PIM-SMv2 and PIM-DM messages processing. 3239 * Receives and verifies the PIM control messages, and passes them 3240 * up to the listening socket, using rip_input(). 3241 * The only message with special processing is the PIM_REGISTER message 3242 * (used by PIM-SM): the PIM header is stripped off, and the inner packet 3243 * is passed to if_simloop(). 3244 */ 3245 void 3246 pim_input(struct mbuf *m, ...) 3247 { 3248 struct ip *ip = mtod(m, struct ip *); 3249 struct pim *pim; 3250 int minlen; 3251 int datalen; 3252 int ip_tos; 3253 int proto; 3254 int iphlen; 3255 va_list ap; 3256 3257 va_start(ap, m); 3258 iphlen = va_arg(ap, int); 3259 proto = va_arg(ap, int); 3260 va_end(ap); 3261 3262 datalen = ntohs(ip->ip_len) - iphlen; 3263 3264 /* Keep statistics */ 3265 pimstat.pims_rcv_total_msgs++; 3266 pimstat.pims_rcv_total_bytes += datalen; 3267 3268 /* 3269 * Validate lengths 3270 */ 3271 if (datalen < PIM_MINLEN) { 3272 pimstat.pims_rcv_tooshort++; 3273 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n", 3274 datalen, (u_long)ip->ip_src.s_addr); 3275 m_freem(m); 3276 return; 3277 } 3278 3279 /* 3280 * If the packet is at least as big as a REGISTER, go agead 3281 * and grab the PIM REGISTER header size, to avoid another 3282 * possible m_pullup() later. 3283 * 3284 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8 3285 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28 3286 */ 3287 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN); 3288 /* 3289 * Get the IP and PIM headers in contiguous memory, and 3290 * possibly the PIM REGISTER header. 3291 */ 3292 if ((m->m_flags & M_EXT || m->m_len < minlen) && 3293 (m = m_pullup(m, minlen)) == NULL) { 3294 log(LOG_ERR, "pim_input: m_pullup failure\n"); 3295 return; 3296 } 3297 /* m_pullup() may have given us a new mbuf so reset ip. */ 3298 ip = mtod(m, struct ip *); 3299 ip_tos = ip->ip_tos; 3300 3301 /* adjust mbuf to point to the PIM header */ 3302 m->m_data += iphlen; 3303 m->m_len -= iphlen; 3304 pim = mtod(m, struct pim *); 3305 3306 /* 3307 * Validate checksum. If PIM REGISTER, exclude the data packet. 3308 * 3309 * XXX: some older PIMv2 implementations don't make this distinction, 3310 * so for compatibility reason perform the checksum over part of the 3311 * message, and if error, then over the whole message. 3312 */ 3313 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) { 3314 /* do nothing, checksum okay */ 3315 } else if (in_cksum(m, datalen)) { 3316 pimstat.pims_rcv_badsum++; 3317 if (mrtdebug & DEBUG_PIM) 3318 log(LOG_DEBUG, "pim_input: invalid checksum"); 3319 m_freem(m); 3320 return; 3321 } 3322 3323 /* PIM version check */ 3324 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) { 3325 pimstat.pims_rcv_badversion++; 3326 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n", 3327 PIM_VT_V(pim->pim_vt), PIM_VERSION); 3328 m_freem(m); 3329 return; 3330 } 3331 3332 /* restore mbuf back to the outer IP */ 3333 m->m_data -= iphlen; 3334 m->m_len += iphlen; 3335 3336 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) { 3337 /* 3338 * Since this is a REGISTER, we'll make a copy of the register 3339 * headers ip + pim + u_int32 + encap_ip, to be passed up to the 3340 * routing daemon. 3341 */ 3342 int s; 3343 struct sockaddr_in dst = { 3344 .sin_len = sizeof(dst), 3345 .sin_family = AF_INET, 3346 }; 3347 struct mbuf *mcp; 3348 struct ip *encap_ip; 3349 u_int32_t *reghdr; 3350 struct ifnet *vifp; 3351 3352 s = splsoftnet(); 3353 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) { 3354 splx(s); 3355 if (mrtdebug & DEBUG_PIM) 3356 log(LOG_DEBUG, 3357 "pim_input: register vif not set: %d\n", reg_vif_num); 3358 m_freem(m); 3359 return; 3360 } 3361 /* XXX need refcnt? */ 3362 vifp = viftable[reg_vif_num].v_ifp; 3363 splx(s); 3364 3365 /* 3366 * Validate length 3367 */ 3368 if (datalen < PIM_REG_MINLEN) { 3369 pimstat.pims_rcv_tooshort++; 3370 pimstat.pims_rcv_badregisters++; 3371 log(LOG_ERR, 3372 "pim_input: register packet size too small %d from %lx\n", 3373 datalen, (u_long)ip->ip_src.s_addr); 3374 m_freem(m); 3375 return; 3376 } 3377 3378 reghdr = (u_int32_t *)(pim + 1); 3379 encap_ip = (struct ip *)(reghdr + 1); 3380 3381 if (mrtdebug & DEBUG_PIM) { 3382 log(LOG_DEBUG, 3383 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n", 3384 (u_long)ntohl(encap_ip->ip_src.s_addr), 3385 (u_long)ntohl(encap_ip->ip_dst.s_addr), 3386 ntohs(encap_ip->ip_len)); 3387 } 3388 3389 /* verify the version number of the inner packet */ 3390 if (encap_ip->ip_v != IPVERSION) { 3391 pimstat.pims_rcv_badregisters++; 3392 if (mrtdebug & DEBUG_PIM) { 3393 log(LOG_DEBUG, "pim_input: invalid IP version (%d) " 3394 "of the inner packet\n", encap_ip->ip_v); 3395 } 3396 m_freem(m); 3397 return; 3398 } 3399 3400 /* verify the inner packet is destined to a mcast group */ 3401 if (!IN_MULTICAST(encap_ip->ip_dst.s_addr)) { 3402 pimstat.pims_rcv_badregisters++; 3403 if (mrtdebug & DEBUG_PIM) 3404 log(LOG_DEBUG, 3405 "pim_input: inner packet of register is not " 3406 "multicast %lx\n", 3407 (u_long)ntohl(encap_ip->ip_dst.s_addr)); 3408 m_freem(m); 3409 return; 3410 } 3411 3412 /* If a NULL_REGISTER, pass it to the daemon */ 3413 if ((ntohl(*reghdr) & PIM_NULL_REGISTER)) 3414 goto pim_input_to_daemon; 3415 3416 /* 3417 * Copy the TOS from the outer IP header to the inner IP header. 3418 */ 3419 if (encap_ip->ip_tos != ip_tos) { 3420 /* Outer TOS -> inner TOS */ 3421 encap_ip->ip_tos = ip_tos; 3422 /* Recompute the inner header checksum. Sigh... */ 3423 3424 /* adjust mbuf to point to the inner IP header */ 3425 m->m_data += (iphlen + PIM_MINLEN); 3426 m->m_len -= (iphlen + PIM_MINLEN); 3427 3428 encap_ip->ip_sum = 0; 3429 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2); 3430 3431 /* restore mbuf to point back to the outer IP header */ 3432 m->m_data -= (iphlen + PIM_MINLEN); 3433 m->m_len += (iphlen + PIM_MINLEN); 3434 } 3435 3436 /* 3437 * Decapsulate the inner IP packet and loopback to forward it 3438 * as a normal multicast packet. Also, make a copy of the 3439 * outer_iphdr + pimhdr + reghdr + encap_iphdr 3440 * to pass to the daemon later, so it can take the appropriate 3441 * actions (e.g., send back PIM_REGISTER_STOP). 3442 * XXX: here m->m_data points to the outer IP header. 3443 */ 3444 mcp = m_copym(m, 0, iphlen + PIM_REG_MINLEN, M_DONTWAIT); 3445 if (mcp == NULL) { 3446 log(LOG_ERR, 3447 "pim_input: pim register: could not copy register head\n"); 3448 m_freem(m); 3449 return; 3450 } 3451 3452 /* Keep statistics */ 3453 /* XXX: registers_bytes include only the encap. mcast pkt */ 3454 pimstat.pims_rcv_registers_msgs++; 3455 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len); 3456 3457 /* 3458 * forward the inner ip packet; point m_data at the inner ip. 3459 */ 3460 m_adj(m, iphlen + PIM_MINLEN); 3461 3462 if (mrtdebug & DEBUG_PIM) { 3463 log(LOG_DEBUG, 3464 "pim_input: forwarding decapsulated register: " 3465 "src %lx, dst %lx, vif %d\n", 3466 (u_long)ntohl(encap_ip->ip_src.s_addr), 3467 (u_long)ntohl(encap_ip->ip_dst.s_addr), 3468 reg_vif_num); 3469 } 3470 /* NB: vifp was collected above; can it change on us? */ 3471 looutput(vifp, m, (struct sockaddr *)&dst, NULL); 3472 3473 /* prepare the register head to send to the mrouting daemon */ 3474 m = mcp; 3475 } 3476 3477 pim_input_to_daemon: 3478 /* 3479 * Pass the PIM message up to the daemon; if it is a Register message, 3480 * pass the 'head' only up to the daemon. This includes the 3481 * outer IP header, PIM header, PIM-Register header and the 3482 * inner IP header. 3483 * XXX: the outer IP header pkt size of a Register is not adjust to 3484 * reflect the fact that the inner multicast data is truncated. 3485 */ 3486 rip_input(m, iphlen, proto); 3487 3488 return; 3489 } 3490 #endif /* PIM */ 3491