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