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