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