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