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