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