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