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