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