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