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