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