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