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