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