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 /* 550 * Currently the only function calling this ioctl routine is rtioctl_fib(). 551 * Typically, only root can create the raw socket in order to execute 552 * this ioctl method, however the request might be coming from a prison 553 */ 554 error = priv_check(curthread, PRIV_NETINET_MROUTE); 555 if (error) 556 return (error); 557 switch (cmd) { 558 case (SIOCGETVIFCNT): 559 error = get_vif_cnt((struct sioc_vif_req *)data); 560 break; 561 562 case (SIOCGETSGCNT): 563 error = get_sg_cnt((struct sioc_sg_req *)data); 564 break; 565 566 default: 567 error = EINVAL; 568 break; 569 } 570 return error; 571 } 572 573 /* 574 * returns the packet, byte, rpf-failure count for the source group provided 575 */ 576 static int 577 get_sg_cnt(struct sioc_sg_req *req) 578 { 579 struct mfc *rt; 580 581 MRW_RLOCK(); 582 rt = mfc_find(&req->src, &req->grp); 583 if (rt == NULL) { 584 MRW_RUNLOCK(); 585 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff; 586 return EADDRNOTAVAIL; 587 } 588 req->pktcnt = rt->mfc_pkt_cnt; 589 req->bytecnt = rt->mfc_byte_cnt; 590 req->wrong_if = rt->mfc_wrong_if; 591 MRW_RUNLOCK(); 592 return 0; 593 } 594 595 /* 596 * returns the input and output packet and byte counts on the vif provided 597 */ 598 static int 599 get_vif_cnt(struct sioc_vif_req *req) 600 { 601 vifi_t vifi = req->vifi; 602 603 MRW_RLOCK(); 604 if (vifi >= V_numvifs) { 605 MRW_RUNLOCK(); 606 return EINVAL; 607 } 608 609 mtx_lock_spin(&V_viftable[vifi].v_spin); 610 req->icount = V_viftable[vifi].v_pkt_in; 611 req->ocount = V_viftable[vifi].v_pkt_out; 612 req->ibytes = V_viftable[vifi].v_bytes_in; 613 req->obytes = V_viftable[vifi].v_bytes_out; 614 mtx_unlock_spin(&V_viftable[vifi].v_spin); 615 MRW_RUNLOCK(); 616 617 return 0; 618 } 619 620 static void 621 if_detached_event(void *arg __unused, struct ifnet *ifp) 622 { 623 vifi_t vifi; 624 u_long i, vifi_cnt = 0; 625 struct ifnet *free_ptr, *multi_leave; 626 627 MRW_WLOCK(); 628 629 if (V_ip_mrouter == NULL) { 630 MRW_WUNLOCK(); 631 return; 632 } 633 634 /* 635 * Tear down multicast forwarder state associated with this ifnet. 636 * 1. Walk the vif list, matching vifs against this ifnet. 637 * 2. Walk the multicast forwarding cache (mfc) looking for 638 * inner matches with this vif's index. 639 * 3. Expire any matching multicast forwarding cache entries. 640 * 4. Free vif state. This should disable ALLMULTI on the interface. 641 */ 642 restart: 643 for (vifi = 0; vifi < V_numvifs; vifi++) { 644 if (V_viftable[vifi].v_ifp != ifp) 645 continue; 646 for (i = 0; i < mfchashsize; i++) { 647 struct mfc *rt, *nrt; 648 649 LIST_FOREACH_SAFE(rt, &V_mfchashtbl[i], mfc_hash, nrt) { 650 if (rt->mfc_parent == vifi) { 651 expire_mfc(rt); 652 } 653 } 654 } 655 del_vif_locked(vifi, &multi_leave, &free_ptr); 656 if (free_ptr != NULL) 657 vifi_cnt++; 658 if (multi_leave) { 659 MRW_WUNLOCK(); 660 if_allmulti(multi_leave, 0); 661 MRW_WLOCK(); 662 goto restart; 663 } 664 } 665 666 MRW_WUNLOCK(); 667 668 /* 669 * Free IFP. We don't have to use free_ptr here as it is the same 670 * that ifp. Perform free as many times as required in case 671 * refcount is greater than 1. 672 */ 673 for (i = 0; i < vifi_cnt; i++) 674 if_free(ifp); 675 } 676 677 static void 678 ip_mrouter_upcall_thread(void *arg, int pending __unused) 679 { 680 CURVNET_SET((struct vnet *) arg); 681 682 MRW_WLOCK(); 683 bw_upcalls_send(); 684 MRW_WUNLOCK(); 685 686 CURVNET_RESTORE(); 687 } 688 689 /* 690 * Enable multicast forwarding. 691 */ 692 static int 693 ip_mrouter_init(struct socket *so, int version) 694 { 695 696 CTR2(KTR_IPMF, "%s: so %p", __func__, so); 697 698 if (version != 1) 699 return ENOPROTOOPT; 700 701 MRW_TEARDOWN_WLOCK(); 702 MRW_WLOCK(); 703 704 if (ip_mrouter_unloading) { 705 MRW_WUNLOCK(); 706 MRW_TEARDOWN_WUNLOCK(); 707 return ENOPROTOOPT; 708 } 709 710 if (V_ip_mrouter != NULL) { 711 MRW_WUNLOCK(); 712 MRW_TEARDOWN_WUNLOCK(); 713 return EADDRINUSE; 714 } 715 716 V_mfchashtbl = hashinit_flags(mfchashsize, M_MRTABLE, &V_mfchash, 717 HASH_NOWAIT); 718 if (V_mfchashtbl == NULL) { 719 MRW_WUNLOCK(); 720 MRW_TEARDOWN_WUNLOCK(); 721 return (ENOMEM); 722 } 723 724 /* Create upcall ring */ 725 mtx_init(&V_bw_upcalls_ring_mtx, "mroute upcall buf_ring mtx", NULL, MTX_DEF); 726 V_bw_upcalls_ring = buf_ring_alloc(BW_UPCALLS_MAX, M_MRTABLE, 727 M_NOWAIT, &V_bw_upcalls_ring_mtx); 728 if (!V_bw_upcalls_ring) { 729 MRW_WUNLOCK(); 730 MRW_TEARDOWN_WUNLOCK(); 731 return (ENOMEM); 732 } 733 734 TASK_INIT(&V_task, 0, ip_mrouter_upcall_thread, curvnet); 735 taskqueue_cancel(V_task_queue, &V_task, NULL); 736 taskqueue_unblock(V_task_queue); 737 738 callout_reset(&V_expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, 739 curvnet); 740 callout_reset(&V_bw_upcalls_ch, BW_UPCALLS_PERIOD, expire_bw_upcalls_send, 741 curvnet); 742 743 V_ip_mrouter = so; 744 atomic_add_int(&ip_mrouter_cnt, 1); 745 746 /* This is a mutex required by buf_ring init, but not used internally */ 747 mtx_init(&V_buf_ring_mtx, "mroute buf_ring mtx", NULL, MTX_DEF); 748 749 MRW_WUNLOCK(); 750 MRW_TEARDOWN_WUNLOCK(); 751 752 CTR1(KTR_IPMF, "%s: done", __func__); 753 754 return 0; 755 } 756 757 /* 758 * Disable multicast forwarding. 759 */ 760 static int 761 X_ip_mrouter_done(void) 762 { 763 struct ifnet **ifps; 764 int nifp; 765 u_long i; 766 vifi_t vifi; 767 struct bw_upcall *bu; 768 769 MRW_TEARDOWN_WLOCK(); 770 771 if (V_ip_mrouter == NULL) { 772 MRW_TEARDOWN_WUNLOCK(); 773 return (EINVAL); 774 } 775 776 /* 777 * Detach/disable hooks to the reset of the system. 778 */ 779 V_ip_mrouter = NULL; 780 atomic_subtract_int(&ip_mrouter_cnt, 1); 781 V_mrt_api_config = 0; 782 783 /* 784 * Wait for all epoch sections to complete to ensure 785 * V_ip_mrouter = NULL is visible to others. 786 */ 787 NET_EPOCH_WAIT(); 788 789 /* Stop and drain task queue */ 790 taskqueue_block(V_task_queue); 791 while (taskqueue_cancel(V_task_queue, &V_task, NULL)) { 792 taskqueue_drain(V_task_queue, &V_task); 793 } 794 795 ifps = malloc(MAXVIFS * sizeof(*ifps), M_TEMP, M_WAITOK); 796 797 MRW_WLOCK(); 798 taskqueue_cancel(V_task_queue, &V_task, NULL); 799 800 /* Destroy upcall ring */ 801 while ((bu = buf_ring_dequeue_mc(V_bw_upcalls_ring)) != NULL) { 802 free(bu, M_MRTABLE); 803 } 804 buf_ring_free(V_bw_upcalls_ring, M_MRTABLE); 805 mtx_destroy(&V_bw_upcalls_ring_mtx); 806 807 /* 808 * For each phyint in use, prepare to disable promiscuous reception 809 * of all IP multicasts. Defer the actual call until the lock is released; 810 * just record the list of interfaces while locked. Some interfaces use 811 * sx locks in their ioctl routines, which is not allowed while holding 812 * a non-sleepable lock. 813 */ 814 KASSERT(V_numvifs <= MAXVIFS, ("More vifs than possible")); 815 for (vifi = 0, nifp = 0; vifi < V_numvifs; vifi++) { 816 if (!in_nullhost(V_viftable[vifi].v_lcl_addr) && 817 !(V_viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) { 818 ifps[nifp++] = V_viftable[vifi].v_ifp; 819 } 820 } 821 bzero((caddr_t)V_viftable, sizeof(*V_viftable) * MAXVIFS); 822 V_numvifs = 0; 823 V_pim_assert_enabled = 0; 824 825 callout_stop(&V_expire_upcalls_ch); 826 callout_stop(&V_bw_upcalls_ch); 827 828 /* 829 * Free all multicast forwarding cache entries. 830 * Do not use hashdestroy(), as we must perform other cleanup. 831 */ 832 for (i = 0; i < mfchashsize; i++) { 833 struct mfc *rt, *nrt; 834 835 LIST_FOREACH_SAFE(rt, &V_mfchashtbl[i], mfc_hash, nrt) { 836 expire_mfc(rt); 837 } 838 } 839 free(V_mfchashtbl, M_MRTABLE); 840 V_mfchashtbl = NULL; 841 842 bzero(V_nexpire, sizeof(V_nexpire[0]) * mfchashsize); 843 844 V_reg_vif_num = VIFI_INVALID; 845 846 mtx_destroy(&V_buf_ring_mtx); 847 848 MRW_WUNLOCK(); 849 MRW_TEARDOWN_WUNLOCK(); 850 851 /* 852 * Now drop our claim on promiscuous multicast on the interfaces recorded 853 * above. This is safe to do now because ALLMULTI is reference counted. 854 */ 855 for (vifi = 0; vifi < nifp; vifi++) 856 if_allmulti(ifps[vifi], 0); 857 free(ifps, M_TEMP); 858 859 CTR1(KTR_IPMF, "%s: done", __func__); 860 861 return 0; 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_spin_name, "BM[%d] spin", vifcp->vifc_vifi); 1008 mtx_init(&vifp->v_spin, vifp->v_spin_name, NULL, MTX_SPIN); 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_spin); 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 vifi_t vifi; 1582 int plen = ntohs(ip->ip_len); 1583 1584 M_ASSERTMAPPED(m); 1585 MRW_LOCK_ASSERT(); 1586 NET_EPOCH_ASSERT(); 1587 1588 /* 1589 * If xmt_vif is not -1, send on only the requested vif. 1590 * 1591 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.) 1592 */ 1593 if (xmt_vif < V_numvifs) { 1594 if (V_viftable[xmt_vif].v_flags & VIFF_REGISTER) 1595 pim_register_send(ip, V_viftable + xmt_vif, m, rt); 1596 else 1597 phyint_send(ip, V_viftable + xmt_vif, m); 1598 return 1; 1599 } 1600 1601 /* 1602 * Don't forward if it didn't arrive from the parent vif for its origin. 1603 */ 1604 vifi = rt->mfc_parent; 1605 if ((vifi >= V_numvifs) || (V_viftable[vifi].v_ifp != ifp)) { 1606 CTR4(KTR_IPMF, "%s: rx on wrong ifp %p (vifi %d, v_ifp %p)", 1607 __func__, ifp, (int)vifi, V_viftable[vifi].v_ifp); 1608 MRTSTAT_INC(mrts_wrong_if); 1609 ++rt->mfc_wrong_if; 1610 /* 1611 * If we are doing PIM assert processing, send a message 1612 * to the routing daemon. 1613 * 1614 * XXX: A PIM-SM router needs the WRONGVIF detection so it 1615 * can complete the SPT switch, regardless of the type 1616 * of the iif (broadcast media, GRE tunnel, etc). 1617 */ 1618 if (V_pim_assert_enabled && (vifi < V_numvifs) && 1619 V_viftable[vifi].v_ifp) { 1620 if (ifp == V_multicast_register_if) 1621 PIMSTAT_INC(pims_rcv_registers_wrongiif); 1622 1623 /* Get vifi for the incoming packet */ 1624 for (vifi = 0; vifi < V_numvifs && V_viftable[vifi].v_ifp != ifp; vifi++) 1625 ; 1626 if (vifi >= V_numvifs) 1627 return 0; /* The iif is not found: ignore the packet. */ 1628 1629 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF) 1630 return 0; /* WRONGVIF disabled: ignore the packet */ 1631 1632 if (ratecheck(&rt->mfc_last_assert, &pim_assert_interval)) { 1633 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET }; 1634 struct igmpmsg *im; 1635 int hlen = ip->ip_hl << 2; 1636 struct mbuf *mm = m_copym(m, 0, hlen, M_NOWAIT); 1637 1638 if (mm && (!M_WRITABLE(mm) || mm->m_len < hlen)) 1639 mm = m_pullup(mm, hlen); 1640 if (mm == NULL) 1641 return ENOBUFS; 1642 1643 im = mtod(mm, struct igmpmsg *); 1644 im->im_msgtype = IGMPMSG_WRONGVIF; 1645 im->im_mbz = 0; 1646 im->im_vif = vifi; 1647 1648 MRTSTAT_INC(mrts_upcalls); 1649 1650 k_igmpsrc.sin_addr = im->im_src; 1651 if (socket_send(V_ip_mrouter, mm, &k_igmpsrc) < 0) { 1652 CTR1(KTR_IPMF, "%s: socket queue full", __func__); 1653 MRTSTAT_INC(mrts_upq_sockfull); 1654 return ENOBUFS; 1655 } 1656 } 1657 } 1658 return 0; 1659 } 1660 1661 /* If I sourced this packet, it counts as output, else it was input. */ 1662 mtx_lock_spin(&V_viftable[vifi].v_spin); 1663 if (in_hosteq(ip->ip_src, V_viftable[vifi].v_lcl_addr)) { 1664 V_viftable[vifi].v_pkt_out++; 1665 V_viftable[vifi].v_bytes_out += plen; 1666 } else { 1667 V_viftable[vifi].v_pkt_in++; 1668 V_viftable[vifi].v_bytes_in += plen; 1669 } 1670 mtx_unlock_spin(&V_viftable[vifi].v_spin); 1671 1672 rt->mfc_pkt_cnt++; 1673 rt->mfc_byte_cnt += plen; 1674 1675 /* 1676 * For each vif, decide if a copy of the packet should be forwarded. 1677 * Forward if: 1678 * - the ttl exceeds the vif's threshold 1679 * - there are group members downstream on interface 1680 */ 1681 for (vifi = 0; vifi < V_numvifs; vifi++) 1682 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) { 1683 V_viftable[vifi].v_pkt_out++; 1684 V_viftable[vifi].v_bytes_out += plen; 1685 if (V_viftable[vifi].v_flags & VIFF_REGISTER) 1686 pim_register_send(ip, V_viftable + vifi, m, rt); 1687 else 1688 phyint_send(ip, V_viftable + vifi, m); 1689 } 1690 1691 /* 1692 * Perform upcall-related bw measuring. 1693 */ 1694 if ((rt->mfc_bw_meter_geq != NULL) || (rt->mfc_bw_meter_leq != NULL)) { 1695 struct bw_meter *x; 1696 struct timeval now; 1697 1698 microtime(&now); 1699 /* Process meters for Greater-or-EQual case */ 1700 for (x = rt->mfc_bw_meter_geq; x != NULL; x = x->bm_mfc_next) 1701 bw_meter_geq_receive_packet(x, plen, &now); 1702 1703 /* Process meters for Lower-or-EQual case */ 1704 for (x = rt->mfc_bw_meter_leq; x != NULL; x = x->bm_mfc_next) { 1705 /* 1706 * Record that a packet is received. 1707 * Spin lock has to be taken as callout context 1708 * (expire_bw_meter_leq) might modify these fields 1709 * as well 1710 */ 1711 mtx_lock_spin(&x->bm_spin); 1712 x->bm_measured.b_packets++; 1713 x->bm_measured.b_bytes += plen; 1714 mtx_unlock_spin(&x->bm_spin); 1715 } 1716 } 1717 1718 return 0; 1719 } 1720 1721 /* 1722 * Check if a vif number is legal/ok. This is used by in_mcast.c. 1723 */ 1724 static int 1725 X_legal_vif_num(int vif) 1726 { 1727 int ret; 1728 1729 ret = 0; 1730 if (vif < 0) 1731 return (ret); 1732 1733 MRW_RLOCK(); 1734 if (vif < V_numvifs) 1735 ret = 1; 1736 MRW_RUNLOCK(); 1737 1738 return (ret); 1739 } 1740 1741 /* 1742 * Return the local address used by this vif 1743 */ 1744 static u_long 1745 X_ip_mcast_src(int vifi) 1746 { 1747 in_addr_t addr; 1748 1749 addr = INADDR_ANY; 1750 if (vifi < 0) 1751 return (addr); 1752 1753 MRW_RLOCK(); 1754 if (vifi < V_numvifs) 1755 addr = V_viftable[vifi].v_lcl_addr.s_addr; 1756 MRW_RUNLOCK(); 1757 1758 return (addr); 1759 } 1760 1761 static void 1762 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m) 1763 { 1764 struct mbuf *mb_copy; 1765 int hlen = ip->ip_hl << 2; 1766 1767 MRW_LOCK_ASSERT(); 1768 M_ASSERTMAPPED(m); 1769 1770 /* 1771 * Make a new reference to the packet; make sure that 1772 * the IP header is actually copied, not just referenced, 1773 * so that ip_output() only scribbles on the copy. 1774 */ 1775 mb_copy = m_copypacket(m, M_NOWAIT); 1776 if (mb_copy && (!M_WRITABLE(mb_copy) || mb_copy->m_len < hlen)) 1777 mb_copy = m_pullup(mb_copy, hlen); 1778 if (mb_copy == NULL) 1779 return; 1780 1781 send_packet(vifp, mb_copy); 1782 } 1783 1784 static void 1785 send_packet(struct vif *vifp, struct mbuf *m) 1786 { 1787 struct ip_moptions imo; 1788 int error __unused; 1789 1790 MRW_LOCK_ASSERT(); 1791 NET_EPOCH_ASSERT(); 1792 1793 imo.imo_multicast_ifp = vifp->v_ifp; 1794 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1; 1795 imo.imo_multicast_loop = !!in_mcast_loop; 1796 imo.imo_multicast_vif = -1; 1797 STAILQ_INIT(&imo.imo_head); 1798 1799 /* 1800 * Re-entrancy should not be a problem here, because 1801 * the packets that we send out and are looped back at us 1802 * should get rejected because they appear to come from 1803 * the loopback interface, thus preventing looping. 1804 */ 1805 error = ip_output(m, NULL, NULL, IP_FORWARDING, &imo, NULL); 1806 CTR3(KTR_IPMF, "%s: vif %td err %d", __func__, 1807 (ptrdiff_t)(vifp - V_viftable), error); 1808 } 1809 1810 /* 1811 * Stubs for old RSVP socket shim implementation. 1812 */ 1813 1814 static int 1815 X_ip_rsvp_vif(struct socket *so __unused, struct sockopt *sopt __unused) 1816 { 1817 1818 return (EOPNOTSUPP); 1819 } 1820 1821 static void 1822 X_ip_rsvp_force_done(struct socket *so __unused) 1823 { 1824 1825 } 1826 1827 static int 1828 X_rsvp_input(struct mbuf **mp, int *offp, int proto) 1829 { 1830 struct mbuf *m; 1831 1832 m = *mp; 1833 *mp = NULL; 1834 if (!V_rsvp_on) 1835 m_freem(m); 1836 return (IPPROTO_DONE); 1837 } 1838 1839 /* 1840 * Code for bandwidth monitors 1841 */ 1842 1843 /* 1844 * Define common interface for timeval-related methods 1845 */ 1846 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp) 1847 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp)) 1848 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp)) 1849 1850 static uint32_t 1851 compute_bw_meter_flags(struct bw_upcall *req) 1852 { 1853 uint32_t flags = 0; 1854 1855 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS) 1856 flags |= BW_METER_UNIT_PACKETS; 1857 if (req->bu_flags & BW_UPCALL_UNIT_BYTES) 1858 flags |= BW_METER_UNIT_BYTES; 1859 if (req->bu_flags & BW_UPCALL_GEQ) 1860 flags |= BW_METER_GEQ; 1861 if (req->bu_flags & BW_UPCALL_LEQ) 1862 flags |= BW_METER_LEQ; 1863 1864 return flags; 1865 } 1866 1867 static void 1868 expire_bw_meter_leq(void *arg) 1869 { 1870 struct bw_meter *x = arg; 1871 struct timeval now; 1872 /* 1873 * INFO: 1874 * callout is always executed with MRW_WLOCK taken 1875 */ 1876 1877 CURVNET_SET((struct vnet *)x->arg); 1878 1879 microtime(&now); 1880 1881 /* 1882 * Test if we should deliver an upcall 1883 */ 1884 if (((x->bm_flags & BW_METER_UNIT_PACKETS) && 1885 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) || 1886 ((x->bm_flags & BW_METER_UNIT_BYTES) && 1887 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) { 1888 /* Prepare an upcall for delivery */ 1889 bw_meter_prepare_upcall(x, &now); 1890 } 1891 1892 /* Send all upcalls that are pending delivery */ 1893 taskqueue_enqueue(V_task_queue, &V_task); 1894 1895 /* Reset counters */ 1896 x->bm_start_time = now; 1897 /* Spin lock has to be taken as ip_forward context 1898 * might modify these fields as well 1899 */ 1900 mtx_lock_spin(&x->bm_spin); 1901 x->bm_measured.b_bytes = 0; 1902 x->bm_measured.b_packets = 0; 1903 mtx_unlock_spin(&x->bm_spin); 1904 1905 callout_schedule(&x->bm_meter_callout, tvtohz(&x->bm_threshold.b_time)); 1906 1907 CURVNET_RESTORE(); 1908 } 1909 1910 /* 1911 * Add a bw_meter entry 1912 */ 1913 static int 1914 add_bw_upcall(struct bw_upcall *req) 1915 { 1916 struct mfc *mfc; 1917 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC, 1918 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC }; 1919 struct timeval now; 1920 struct bw_meter *x, **bwm_ptr; 1921 uint32_t flags; 1922 1923 if (!(V_mrt_api_config & MRT_MFC_BW_UPCALL)) 1924 return EOPNOTSUPP; 1925 1926 /* Test if the flags are valid */ 1927 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES))) 1928 return EINVAL; 1929 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))) 1930 return EINVAL; 1931 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)) == (BW_UPCALL_GEQ | BW_UPCALL_LEQ)) 1932 return EINVAL; 1933 1934 /* Test if the threshold time interval is valid */ 1935 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <)) 1936 return EINVAL; 1937 1938 flags = compute_bw_meter_flags(req); 1939 1940 /* 1941 * Find if we have already same bw_meter entry 1942 */ 1943 MRW_WLOCK(); 1944 mfc = mfc_find(&req->bu_src, &req->bu_dst); 1945 if (mfc == NULL) { 1946 MRW_WUNLOCK(); 1947 return EADDRNOTAVAIL; 1948 } 1949 1950 /* Choose an appropriate bw_meter list */ 1951 if (req->bu_flags & BW_UPCALL_GEQ) 1952 bwm_ptr = &mfc->mfc_bw_meter_geq; 1953 else 1954 bwm_ptr = &mfc->mfc_bw_meter_leq; 1955 1956 for (x = *bwm_ptr; x != NULL; x = x->bm_mfc_next) { 1957 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time, 1958 &req->bu_threshold.b_time, ==)) 1959 && (x->bm_threshold.b_packets 1960 == req->bu_threshold.b_packets) 1961 && (x->bm_threshold.b_bytes 1962 == req->bu_threshold.b_bytes) 1963 && (x->bm_flags & BW_METER_USER_FLAGS) 1964 == flags) { 1965 MRW_WUNLOCK(); 1966 return 0; /* XXX Already installed */ 1967 } 1968 } 1969 1970 /* Allocate the new bw_meter entry */ 1971 x = malloc(sizeof(*x), M_BWMETER, M_ZERO | M_NOWAIT); 1972 if (x == NULL) { 1973 MRW_WUNLOCK(); 1974 return ENOBUFS; 1975 } 1976 1977 /* Set the new bw_meter entry */ 1978 x->bm_threshold.b_time = req->bu_threshold.b_time; 1979 microtime(&now); 1980 x->bm_start_time = now; 1981 x->bm_threshold.b_packets = req->bu_threshold.b_packets; 1982 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes; 1983 x->bm_measured.b_packets = 0; 1984 x->bm_measured.b_bytes = 0; 1985 x->bm_flags = flags; 1986 x->bm_time_next = NULL; 1987 x->bm_mfc = mfc; 1988 x->arg = curvnet; 1989 sprintf(x->bm_spin_name, "BM spin %p", x); 1990 mtx_init(&x->bm_spin, x->bm_spin_name, NULL, MTX_SPIN); 1991 1992 /* For LEQ case create periodic callout */ 1993 if (req->bu_flags & BW_UPCALL_LEQ) { 1994 callout_init_rw(&x->bm_meter_callout, &mrouter_lock, CALLOUT_SHAREDLOCK); 1995 callout_reset(&x->bm_meter_callout, tvtohz(&x->bm_threshold.b_time), 1996 expire_bw_meter_leq, x); 1997 } 1998 1999 /* Add the new bw_meter entry to the front of entries for this MFC */ 2000 x->bm_mfc_next = *bwm_ptr; 2001 *bwm_ptr = x; 2002 2003 MRW_WUNLOCK(); 2004 2005 return 0; 2006 } 2007 2008 static void 2009 free_bw_list(struct bw_meter *list) 2010 { 2011 while (list != NULL) { 2012 struct bw_meter *x = list; 2013 2014 /* MRW_WLOCK must be held here */ 2015 if (x->bm_flags & BW_METER_LEQ) { 2016 callout_drain(&x->bm_meter_callout); 2017 mtx_destroy(&x->bm_spin); 2018 } 2019 2020 list = list->bm_mfc_next; 2021 free(x, M_BWMETER); 2022 } 2023 } 2024 2025 /* 2026 * Delete one or multiple bw_meter entries 2027 */ 2028 static int 2029 del_bw_upcall(struct bw_upcall *req) 2030 { 2031 struct mfc *mfc; 2032 struct bw_meter *x, **bwm_ptr; 2033 2034 if (!(V_mrt_api_config & MRT_MFC_BW_UPCALL)) 2035 return EOPNOTSUPP; 2036 2037 MRW_WLOCK(); 2038 2039 /* Find the corresponding MFC entry */ 2040 mfc = mfc_find(&req->bu_src, &req->bu_dst); 2041 if (mfc == NULL) { 2042 MRW_WUNLOCK(); 2043 return EADDRNOTAVAIL; 2044 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) { 2045 /* 2046 * Delete all bw_meter entries for this mfc 2047 */ 2048 struct bw_meter *list; 2049 2050 /* Free LEQ list */ 2051 list = mfc->mfc_bw_meter_leq; 2052 mfc->mfc_bw_meter_leq = NULL; 2053 free_bw_list(list); 2054 2055 /* Free GEQ list */ 2056 list = mfc->mfc_bw_meter_geq; 2057 mfc->mfc_bw_meter_geq = NULL; 2058 free_bw_list(list); 2059 MRW_WUNLOCK(); 2060 return 0; 2061 } else { /* Delete a single bw_meter entry */ 2062 struct bw_meter *prev; 2063 uint32_t flags = 0; 2064 2065 flags = compute_bw_meter_flags(req); 2066 2067 /* Choose an appropriate bw_meter list */ 2068 if (req->bu_flags & BW_UPCALL_GEQ) 2069 bwm_ptr = &mfc->mfc_bw_meter_geq; 2070 else 2071 bwm_ptr = &mfc->mfc_bw_meter_leq; 2072 2073 /* Find the bw_meter entry to delete */ 2074 for (prev = NULL, x = *bwm_ptr; x != NULL; 2075 prev = x, x = x->bm_mfc_next) { 2076 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time, &req->bu_threshold.b_time, ==)) && 2077 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) && 2078 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) && 2079 (x->bm_flags & BW_METER_USER_FLAGS) == flags) 2080 break; 2081 } 2082 if (x != NULL) { /* Delete entry from the list for this MFC */ 2083 if (prev != NULL) 2084 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/ 2085 else 2086 *bwm_ptr = x->bm_mfc_next;/* new head of list */ 2087 2088 if (req->bu_flags & BW_UPCALL_LEQ) 2089 callout_stop(&x->bm_meter_callout); 2090 2091 MRW_WUNLOCK(); 2092 /* Free the bw_meter entry */ 2093 free(x, M_BWMETER); 2094 return 0; 2095 } else { 2096 MRW_WUNLOCK(); 2097 return EINVAL; 2098 } 2099 } 2100 __assert_unreachable(); 2101 } 2102 2103 /* 2104 * Perform bandwidth measurement processing that may result in an upcall 2105 */ 2106 static void 2107 bw_meter_geq_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp) 2108 { 2109 struct timeval delta; 2110 2111 MRW_LOCK_ASSERT(); 2112 2113 delta = *nowp; 2114 BW_TIMEVALDECR(&delta, &x->bm_start_time); 2115 2116 /* 2117 * Processing for ">=" type of bw_meter entry. 2118 * bm_spin does not have to be hold here as in GEQ 2119 * case this is the only context accessing bm_measured. 2120 */ 2121 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) { 2122 /* Reset the bw_meter entry */ 2123 x->bm_start_time = *nowp; 2124 x->bm_measured.b_packets = 0; 2125 x->bm_measured.b_bytes = 0; 2126 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED; 2127 } 2128 2129 /* Record that a packet is received */ 2130 x->bm_measured.b_packets++; 2131 x->bm_measured.b_bytes += plen; 2132 2133 /* 2134 * Test if we should deliver an upcall 2135 */ 2136 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) { 2137 if (((x->bm_flags & BW_METER_UNIT_PACKETS) && 2138 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) || 2139 ((x->bm_flags & BW_METER_UNIT_BYTES) && 2140 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) { 2141 /* Prepare an upcall for delivery */ 2142 bw_meter_prepare_upcall(x, nowp); 2143 x->bm_flags |= BW_METER_UPCALL_DELIVERED; 2144 } 2145 } 2146 } 2147 2148 /* 2149 * Prepare a bandwidth-related upcall 2150 */ 2151 static void 2152 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp) 2153 { 2154 struct timeval delta; 2155 struct bw_upcall *u; 2156 2157 MRW_LOCK_ASSERT(); 2158 2159 /* 2160 * Compute the measured time interval 2161 */ 2162 delta = *nowp; 2163 BW_TIMEVALDECR(&delta, &x->bm_start_time); 2164 2165 /* 2166 * Set the bw_upcall entry 2167 */ 2168 u = malloc(sizeof(struct bw_upcall), M_MRTABLE, M_NOWAIT | M_ZERO); 2169 if (!u) { 2170 log(LOG_WARNING, "bw_meter_prepare_upcall: cannot allocate entry\n"); 2171 return; 2172 } 2173 u->bu_src = x->bm_mfc->mfc_origin; 2174 u->bu_dst = x->bm_mfc->mfc_mcastgrp; 2175 u->bu_threshold.b_time = x->bm_threshold.b_time; 2176 u->bu_threshold.b_packets = x->bm_threshold.b_packets; 2177 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes; 2178 u->bu_measured.b_time = delta; 2179 u->bu_measured.b_packets = x->bm_measured.b_packets; 2180 u->bu_measured.b_bytes = x->bm_measured.b_bytes; 2181 u->bu_flags = 0; 2182 if (x->bm_flags & BW_METER_UNIT_PACKETS) 2183 u->bu_flags |= BW_UPCALL_UNIT_PACKETS; 2184 if (x->bm_flags & BW_METER_UNIT_BYTES) 2185 u->bu_flags |= BW_UPCALL_UNIT_BYTES; 2186 if (x->bm_flags & BW_METER_GEQ) 2187 u->bu_flags |= BW_UPCALL_GEQ; 2188 if (x->bm_flags & BW_METER_LEQ) 2189 u->bu_flags |= BW_UPCALL_LEQ; 2190 2191 if (buf_ring_enqueue(V_bw_upcalls_ring, u)) 2192 log(LOG_WARNING, "bw_meter_prepare_upcall: cannot enqueue upcall\n"); 2193 if (buf_ring_count(V_bw_upcalls_ring) > (BW_UPCALLS_MAX / 2)) { 2194 taskqueue_enqueue(V_task_queue, &V_task); 2195 } 2196 } 2197 /* 2198 * Send the pending bandwidth-related upcalls 2199 */ 2200 static void 2201 bw_upcalls_send(void) 2202 { 2203 struct mbuf *m; 2204 int len = 0; 2205 struct bw_upcall *bu; 2206 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET }; 2207 static struct igmpmsg igmpmsg = { 2208 0, /* unused1 */ 2209 0, /* unused2 */ 2210 IGMPMSG_BW_UPCALL,/* im_msgtype */ 2211 0, /* im_mbz */ 2212 0, /* im_vif */ 2213 0, /* unused3 */ 2214 { 0 }, /* im_src */ 2215 { 0 } /* im_dst */ 2216 }; 2217 2218 MRW_LOCK_ASSERT(); 2219 2220 if (buf_ring_empty(V_bw_upcalls_ring)) 2221 return; 2222 2223 /* 2224 * Allocate a new mbuf, initialize it with the header and 2225 * the payload for the pending calls. 2226 */ 2227 m = m_gethdr(M_NOWAIT, MT_DATA); 2228 if (m == NULL) { 2229 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n"); 2230 return; 2231 } 2232 2233 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg); 2234 len += sizeof(struct igmpmsg); 2235 while ((bu = buf_ring_dequeue_mc(V_bw_upcalls_ring)) != NULL) { 2236 m_copyback(m, len, sizeof(struct bw_upcall), (caddr_t)bu); 2237 len += sizeof(struct bw_upcall); 2238 free(bu, M_MRTABLE); 2239 } 2240 2241 /* 2242 * Send the upcalls 2243 * XXX do we need to set the address in k_igmpsrc ? 2244 */ 2245 MRTSTAT_INC(mrts_upcalls); 2246 if (socket_send(V_ip_mrouter, m, &k_igmpsrc) < 0) { 2247 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n"); 2248 MRTSTAT_INC(mrts_upq_sockfull); 2249 } 2250 } 2251 2252 /* 2253 * A periodic function for sending all upcalls that are pending delivery 2254 */ 2255 static void 2256 expire_bw_upcalls_send(void *arg) 2257 { 2258 CURVNET_SET((struct vnet *) arg); 2259 2260 /* This callout is run with MRW_RLOCK taken */ 2261 2262 bw_upcalls_send(); 2263 2264 callout_reset(&V_bw_upcalls_ch, BW_UPCALLS_PERIOD, expire_bw_upcalls_send, 2265 curvnet); 2266 CURVNET_RESTORE(); 2267 } 2268 2269 /* 2270 * End of bandwidth monitoring code 2271 */ 2272 2273 /* 2274 * Send the packet up to the user daemon, or eventually do kernel encapsulation 2275 * 2276 */ 2277 static int 2278 pim_register_send(struct ip *ip, struct vif *vifp, struct mbuf *m, 2279 struct mfc *rt) 2280 { 2281 struct mbuf *mb_copy, *mm; 2282 2283 /* 2284 * Do not send IGMP_WHOLEPKT notifications to userland, if the 2285 * rendezvous point was unspecified, and we were told not to. 2286 */ 2287 if (pim_squelch_wholepkt != 0 && (V_mrt_api_config & MRT_MFC_RP) && 2288 in_nullhost(rt->mfc_rp)) 2289 return 0; 2290 2291 mb_copy = pim_register_prepare(ip, m); 2292 if (mb_copy == NULL) 2293 return ENOBUFS; 2294 2295 /* 2296 * Send all the fragments. Note that the mbuf for each fragment 2297 * is freed by the sending machinery. 2298 */ 2299 for (mm = mb_copy; mm; mm = mb_copy) { 2300 mb_copy = mm->m_nextpkt; 2301 mm->m_nextpkt = 0; 2302 mm = m_pullup(mm, sizeof(struct ip)); 2303 if (mm != NULL) { 2304 ip = mtod(mm, struct ip *); 2305 if ((V_mrt_api_config & MRT_MFC_RP) && !in_nullhost(rt->mfc_rp)) { 2306 pim_register_send_rp(ip, vifp, mm, rt); 2307 } else { 2308 pim_register_send_upcall(ip, vifp, mm, rt); 2309 } 2310 } 2311 } 2312 2313 return 0; 2314 } 2315 2316 /* 2317 * Return a copy of the data packet that is ready for PIM Register 2318 * encapsulation. 2319 * XXX: Note that in the returned copy the IP header is a valid one. 2320 */ 2321 static struct mbuf * 2322 pim_register_prepare(struct ip *ip, struct mbuf *m) 2323 { 2324 struct mbuf *mb_copy = NULL; 2325 int mtu; 2326 2327 /* Take care of delayed checksums */ 2328 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 2329 in_delayed_cksum(m); 2330 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 2331 } 2332 2333 /* 2334 * Copy the old packet & pullup its IP header into the 2335 * new mbuf so we can modify it. 2336 */ 2337 mb_copy = m_copypacket(m, M_NOWAIT); 2338 if (mb_copy == NULL) 2339 return NULL; 2340 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2); 2341 if (mb_copy == NULL) 2342 return NULL; 2343 2344 /* take care of the TTL */ 2345 ip = mtod(mb_copy, struct ip *); 2346 --ip->ip_ttl; 2347 2348 /* Compute the MTU after the PIM Register encapsulation */ 2349 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr); 2350 2351 if (ntohs(ip->ip_len) <= mtu) { 2352 /* Turn the IP header into a valid one */ 2353 ip->ip_sum = 0; 2354 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2); 2355 } else { 2356 /* Fragment the packet */ 2357 mb_copy->m_pkthdr.csum_flags |= CSUM_IP; 2358 if (ip_fragment(ip, &mb_copy, mtu, 0) != 0) { 2359 m_freem(mb_copy); 2360 return NULL; 2361 } 2362 } 2363 return mb_copy; 2364 } 2365 2366 /* 2367 * Send an upcall with the data packet to the user-level process. 2368 */ 2369 static int 2370 pim_register_send_upcall(struct ip *ip, struct vif *vifp, 2371 struct mbuf *mb_copy, struct mfc *rt) 2372 { 2373 struct mbuf *mb_first; 2374 int len = ntohs(ip->ip_len); 2375 struct igmpmsg *im; 2376 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET }; 2377 2378 MRW_LOCK_ASSERT(); 2379 2380 /* 2381 * Add a new mbuf with an upcall header 2382 */ 2383 mb_first = m_gethdr(M_NOWAIT, MT_DATA); 2384 if (mb_first == NULL) { 2385 m_freem(mb_copy); 2386 return ENOBUFS; 2387 } 2388 mb_first->m_data += max_linkhdr; 2389 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg); 2390 mb_first->m_len = sizeof(struct igmpmsg); 2391 mb_first->m_next = mb_copy; 2392 2393 /* Send message to routing daemon */ 2394 im = mtod(mb_first, struct igmpmsg *); 2395 im->im_msgtype = IGMPMSG_WHOLEPKT; 2396 im->im_mbz = 0; 2397 im->im_vif = vifp - V_viftable; 2398 im->im_src = ip->ip_src; 2399 im->im_dst = ip->ip_dst; 2400 2401 k_igmpsrc.sin_addr = ip->ip_src; 2402 2403 MRTSTAT_INC(mrts_upcalls); 2404 2405 if (socket_send(V_ip_mrouter, mb_first, &k_igmpsrc) < 0) { 2406 CTR1(KTR_IPMF, "%s: socket queue full", __func__); 2407 MRTSTAT_INC(mrts_upq_sockfull); 2408 return ENOBUFS; 2409 } 2410 2411 /* Keep statistics */ 2412 PIMSTAT_INC(pims_snd_registers_msgs); 2413 PIMSTAT_ADD(pims_snd_registers_bytes, len); 2414 2415 return 0; 2416 } 2417 2418 /* 2419 * Encapsulate the data packet in PIM Register message and send it to the RP. 2420 */ 2421 static int 2422 pim_register_send_rp(struct ip *ip, struct vif *vifp, struct mbuf *mb_copy, 2423 struct mfc *rt) 2424 { 2425 struct mbuf *mb_first; 2426 struct ip *ip_outer; 2427 struct pim_encap_pimhdr *pimhdr; 2428 int len = ntohs(ip->ip_len); 2429 vifi_t vifi = rt->mfc_parent; 2430 2431 MRW_LOCK_ASSERT(); 2432 2433 if ((vifi >= V_numvifs) || in_nullhost(V_viftable[vifi].v_lcl_addr)) { 2434 m_freem(mb_copy); 2435 return EADDRNOTAVAIL; /* The iif vif is invalid */ 2436 } 2437 2438 /* 2439 * Add a new mbuf with the encapsulating header 2440 */ 2441 mb_first = m_gethdr(M_NOWAIT, MT_DATA); 2442 if (mb_first == NULL) { 2443 m_freem(mb_copy); 2444 return ENOBUFS; 2445 } 2446 mb_first->m_data += max_linkhdr; 2447 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr); 2448 mb_first->m_next = mb_copy; 2449 2450 mb_first->m_pkthdr.len = len + mb_first->m_len; 2451 2452 /* 2453 * Fill in the encapsulating IP and PIM header 2454 */ 2455 ip_outer = mtod(mb_first, struct ip *); 2456 *ip_outer = pim_encap_iphdr; 2457 ip_outer->ip_len = htons(len + sizeof(pim_encap_iphdr) + 2458 sizeof(pim_encap_pimhdr)); 2459 ip_outer->ip_src = V_viftable[vifi].v_lcl_addr; 2460 ip_outer->ip_dst = rt->mfc_rp; 2461 /* 2462 * Copy the inner header TOS to the outer header, and take care of the 2463 * IP_DF bit. 2464 */ 2465 ip_outer->ip_tos = ip->ip_tos; 2466 if (ip->ip_off & htons(IP_DF)) 2467 ip_outer->ip_off |= htons(IP_DF); 2468 ip_fillid(ip_outer, V_ip_random_id); 2469 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer 2470 + sizeof(pim_encap_iphdr)); 2471 *pimhdr = pim_encap_pimhdr; 2472 /* If the iif crosses a border, set the Border-bit */ 2473 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & V_mrt_api_config) 2474 pimhdr->flags |= htonl(PIM_BORDER_REGISTER); 2475 2476 mb_first->m_data += sizeof(pim_encap_iphdr); 2477 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr)); 2478 mb_first->m_data -= sizeof(pim_encap_iphdr); 2479 2480 send_packet(vifp, mb_first); 2481 2482 /* Keep statistics */ 2483 PIMSTAT_INC(pims_snd_registers_msgs); 2484 PIMSTAT_ADD(pims_snd_registers_bytes, len); 2485 2486 return 0; 2487 } 2488 2489 /* 2490 * pim_encapcheck() is called by the encap4_input() path at runtime to 2491 * determine if a packet is for PIM; allowing PIM to be dynamically loaded 2492 * into the kernel. 2493 */ 2494 static int 2495 pim_encapcheck(const struct mbuf *m __unused, int off __unused, 2496 int proto __unused, void *arg __unused) 2497 { 2498 2499 KASSERT(proto == IPPROTO_PIM, ("not for IPPROTO_PIM")); 2500 return (8); /* claim the datagram. */ 2501 } 2502 2503 /* 2504 * PIM-SMv2 and PIM-DM messages processing. 2505 * Receives and verifies the PIM control messages, and passes them 2506 * up to the listening socket, using rip_input(). 2507 * The only message with special processing is the PIM_REGISTER message 2508 * (used by PIM-SM): the PIM header is stripped off, and the inner packet 2509 * is passed to if_simloop(). 2510 */ 2511 static int 2512 pim_input(struct mbuf *m, int off, int proto, void *arg __unused) 2513 { 2514 struct ip *ip = mtod(m, struct ip *); 2515 struct pim *pim; 2516 int iphlen = off; 2517 int minlen; 2518 int datalen = ntohs(ip->ip_len) - iphlen; 2519 int ip_tos; 2520 2521 /* Keep statistics */ 2522 PIMSTAT_INC(pims_rcv_total_msgs); 2523 PIMSTAT_ADD(pims_rcv_total_bytes, datalen); 2524 2525 /* 2526 * Validate lengths 2527 */ 2528 if (datalen < PIM_MINLEN) { 2529 PIMSTAT_INC(pims_rcv_tooshort); 2530 CTR3(KTR_IPMF, "%s: short packet (%d) from 0x%08x", 2531 __func__, datalen, ntohl(ip->ip_src.s_addr)); 2532 m_freem(m); 2533 return (IPPROTO_DONE); 2534 } 2535 2536 /* 2537 * If the packet is at least as big as a REGISTER, go agead 2538 * and grab the PIM REGISTER header size, to avoid another 2539 * possible m_pullup() later. 2540 * 2541 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8 2542 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28 2543 */ 2544 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN); 2545 /* 2546 * Get the IP and PIM headers in contiguous memory, and 2547 * possibly the PIM REGISTER header. 2548 */ 2549 if (m->m_len < minlen && (m = m_pullup(m, minlen)) == NULL) { 2550 CTR1(KTR_IPMF, "%s: m_pullup() failed", __func__); 2551 return (IPPROTO_DONE); 2552 } 2553 2554 /* m_pullup() may have given us a new mbuf so reset ip. */ 2555 ip = mtod(m, struct ip *); 2556 ip_tos = ip->ip_tos; 2557 2558 /* adjust mbuf to point to the PIM header */ 2559 m->m_data += iphlen; 2560 m->m_len -= iphlen; 2561 pim = mtod(m, struct pim *); 2562 2563 /* 2564 * Validate checksum. If PIM REGISTER, exclude the data packet. 2565 * 2566 * XXX: some older PIMv2 implementations don't make this distinction, 2567 * so for compatibility reason perform the checksum over part of the 2568 * message, and if error, then over the whole message. 2569 */ 2570 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) { 2571 /* do nothing, checksum okay */ 2572 } else if (in_cksum(m, datalen)) { 2573 PIMSTAT_INC(pims_rcv_badsum); 2574 CTR1(KTR_IPMF, "%s: invalid checksum", __func__); 2575 m_freem(m); 2576 return (IPPROTO_DONE); 2577 } 2578 2579 /* PIM version check */ 2580 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) { 2581 PIMSTAT_INC(pims_rcv_badversion); 2582 CTR3(KTR_IPMF, "%s: bad version %d expect %d", __func__, 2583 (int)PIM_VT_V(pim->pim_vt), PIM_VERSION); 2584 m_freem(m); 2585 return (IPPROTO_DONE); 2586 } 2587 2588 /* restore mbuf back to the outer IP */ 2589 m->m_data -= iphlen; 2590 m->m_len += iphlen; 2591 2592 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) { 2593 /* 2594 * Since this is a REGISTER, we'll make a copy of the register 2595 * headers ip + pim + u_int32 + encap_ip, to be passed up to the 2596 * routing daemon. 2597 */ 2598 struct sockaddr_in dst = { sizeof(dst), AF_INET }; 2599 struct mbuf *mcp; 2600 struct ip *encap_ip; 2601 u_int32_t *reghdr; 2602 struct ifnet *vifp; 2603 2604 MRW_RLOCK(); 2605 if ((V_reg_vif_num >= V_numvifs) || (V_reg_vif_num == VIFI_INVALID)) { 2606 MRW_RUNLOCK(); 2607 CTR2(KTR_IPMF, "%s: register vif not set: %d", __func__, 2608 (int)V_reg_vif_num); 2609 m_freem(m); 2610 return (IPPROTO_DONE); 2611 } 2612 /* XXX need refcnt? */ 2613 vifp = V_viftable[V_reg_vif_num].v_ifp; 2614 MRW_RUNLOCK(); 2615 2616 /* 2617 * Validate length 2618 */ 2619 if (datalen < PIM_REG_MINLEN) { 2620 PIMSTAT_INC(pims_rcv_tooshort); 2621 PIMSTAT_INC(pims_rcv_badregisters); 2622 CTR1(KTR_IPMF, "%s: register packet size too small", __func__); 2623 m_freem(m); 2624 return (IPPROTO_DONE); 2625 } 2626 2627 reghdr = (u_int32_t *)(pim + 1); 2628 encap_ip = (struct ip *)(reghdr + 1); 2629 2630 CTR3(KTR_IPMF, "%s: register: encap ip src 0x%08x len %d", 2631 __func__, ntohl(encap_ip->ip_src.s_addr), 2632 ntohs(encap_ip->ip_len)); 2633 2634 /* verify the version number of the inner packet */ 2635 if (encap_ip->ip_v != IPVERSION) { 2636 PIMSTAT_INC(pims_rcv_badregisters); 2637 CTR1(KTR_IPMF, "%s: bad encap ip version", __func__); 2638 m_freem(m); 2639 return (IPPROTO_DONE); 2640 } 2641 2642 /* verify the inner packet is destined to a mcast group */ 2643 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) { 2644 PIMSTAT_INC(pims_rcv_badregisters); 2645 CTR2(KTR_IPMF, "%s: bad encap ip dest 0x%08x", __func__, 2646 ntohl(encap_ip->ip_dst.s_addr)); 2647 m_freem(m); 2648 return (IPPROTO_DONE); 2649 } 2650 2651 /* If a NULL_REGISTER, pass it to the daemon */ 2652 if ((ntohl(*reghdr) & PIM_NULL_REGISTER)) 2653 goto pim_input_to_daemon; 2654 2655 /* 2656 * Copy the TOS from the outer IP header to the inner IP header. 2657 */ 2658 if (encap_ip->ip_tos != ip_tos) { 2659 /* Outer TOS -> inner TOS */ 2660 encap_ip->ip_tos = ip_tos; 2661 /* Recompute the inner header checksum. Sigh... */ 2662 2663 /* adjust mbuf to point to the inner IP header */ 2664 m->m_data += (iphlen + PIM_MINLEN); 2665 m->m_len -= (iphlen + PIM_MINLEN); 2666 2667 encap_ip->ip_sum = 0; 2668 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2); 2669 2670 /* restore mbuf to point back to the outer IP header */ 2671 m->m_data -= (iphlen + PIM_MINLEN); 2672 m->m_len += (iphlen + PIM_MINLEN); 2673 } 2674 2675 /* 2676 * Decapsulate the inner IP packet and loopback to forward it 2677 * as a normal multicast packet. Also, make a copy of the 2678 * outer_iphdr + pimhdr + reghdr + encap_iphdr 2679 * to pass to the daemon later, so it can take the appropriate 2680 * actions (e.g., send back PIM_REGISTER_STOP). 2681 * XXX: here m->m_data points to the outer IP header. 2682 */ 2683 mcp = m_copym(m, 0, iphlen + PIM_REG_MINLEN, M_NOWAIT); 2684 if (mcp == NULL) { 2685 CTR1(KTR_IPMF, "%s: m_copym() failed", __func__); 2686 m_freem(m); 2687 return (IPPROTO_DONE); 2688 } 2689 2690 /* Keep statistics */ 2691 /* XXX: registers_bytes include only the encap. mcast pkt */ 2692 PIMSTAT_INC(pims_rcv_registers_msgs); 2693 PIMSTAT_ADD(pims_rcv_registers_bytes, ntohs(encap_ip->ip_len)); 2694 2695 /* 2696 * forward the inner ip packet; point m_data at the inner ip. 2697 */ 2698 m_adj(m, iphlen + PIM_MINLEN); 2699 2700 CTR4(KTR_IPMF, 2701 "%s: forward decap'd REGISTER: src %lx dst %lx vif %d", 2702 __func__, 2703 (u_long)ntohl(encap_ip->ip_src.s_addr), 2704 (u_long)ntohl(encap_ip->ip_dst.s_addr), 2705 (int)V_reg_vif_num); 2706 2707 /* NB: vifp was collected above; can it change on us? */ 2708 if_simloop(vifp, m, dst.sin_family, 0); 2709 2710 /* prepare the register head to send to the mrouting daemon */ 2711 m = mcp; 2712 } 2713 2714 pim_input_to_daemon: 2715 /* 2716 * Pass the PIM message up to the daemon; if it is a Register message, 2717 * pass the 'head' only up to the daemon. This includes the 2718 * outer IP header, PIM header, PIM-Register header and the 2719 * inner IP header. 2720 * XXX: the outer IP header pkt size of a Register is not adjust to 2721 * reflect the fact that the inner multicast data is truncated. 2722 */ 2723 return (rip_input(&m, &off, proto)); 2724 } 2725 2726 static int 2727 sysctl_mfctable(SYSCTL_HANDLER_ARGS) 2728 { 2729 struct mfc *rt; 2730 int error, i; 2731 2732 if (req->newptr) 2733 return (EPERM); 2734 if (V_mfchashtbl == NULL) /* XXX unlocked */ 2735 return (0); 2736 error = sysctl_wire_old_buffer(req, 0); 2737 if (error) 2738 return (error); 2739 2740 MRW_RLOCK(); 2741 if (V_mfchashtbl == NULL) 2742 goto out_locked; 2743 2744 for (i = 0; i < mfchashsize; i++) { 2745 LIST_FOREACH(rt, &V_mfchashtbl[i], mfc_hash) { 2746 error = SYSCTL_OUT(req, rt, sizeof(struct mfc)); 2747 if (error) 2748 goto out_locked; 2749 } 2750 } 2751 out_locked: 2752 MRW_RUNLOCK(); 2753 return (error); 2754 } 2755 2756 static SYSCTL_NODE(_net_inet_ip, OID_AUTO, mfctable, 2757 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_mfctable, 2758 "IPv4 Multicast Forwarding Table " 2759 "(struct *mfc[mfchashsize], netinet/ip_mroute.h)"); 2760 2761 static int 2762 sysctl_viflist(SYSCTL_HANDLER_ARGS) 2763 { 2764 int error, i; 2765 2766 if (req->newptr) 2767 return (EPERM); 2768 if (V_viftable == NULL) /* XXX unlocked */ 2769 return (0); 2770 error = sysctl_wire_old_buffer(req, MROUTE_VIF_SYSCTL_LEN * MAXVIFS); 2771 if (error) 2772 return (error); 2773 2774 MRW_RLOCK(); 2775 /* Copy out user-visible portion of vif entry. */ 2776 for (i = 0; i < MAXVIFS; i++) { 2777 error = SYSCTL_OUT(req, &V_viftable[i], MROUTE_VIF_SYSCTL_LEN); 2778 if (error) 2779 break; 2780 } 2781 MRW_RUNLOCK(); 2782 return (error); 2783 } 2784 2785 SYSCTL_PROC(_net_inet_ip, OID_AUTO, viftable, 2786 CTLTYPE_OPAQUE | CTLFLAG_VNET | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 2787 sysctl_viflist, "S,vif[MAXVIFS]", 2788 "IPv4 Multicast Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)"); 2789 2790 static void 2791 vnet_mroute_init(const void *unused __unused) 2792 { 2793 2794 V_nexpire = malloc(mfchashsize, M_MRTABLE, M_WAITOK|M_ZERO); 2795 2796 V_viftable = mallocarray(MAXVIFS, sizeof(*V_viftable), 2797 M_MRTABLE, M_WAITOK|M_ZERO); 2798 2799 callout_init_rw(&V_expire_upcalls_ch, &mrouter_lock, 0); 2800 callout_init_rw(&V_bw_upcalls_ch, &mrouter_lock, 0); 2801 2802 /* Prepare taskqueue */ 2803 V_task_queue = taskqueue_create_fast("ip_mroute_tskq", M_NOWAIT, 2804 taskqueue_thread_enqueue, &V_task_queue); 2805 taskqueue_start_threads(&V_task_queue, 1, PI_NET, "ip_mroute_tskq task"); 2806 } 2807 2808 VNET_SYSINIT(vnet_mroute_init, SI_SUB_PROTO_MC, SI_ORDER_ANY, vnet_mroute_init, 2809 NULL); 2810 2811 static void 2812 vnet_mroute_uninit(const void *unused __unused) 2813 { 2814 2815 /* Taskqueue should be cancelled and drained before freeing */ 2816 taskqueue_free(V_task_queue); 2817 2818 free(V_viftable, M_MRTABLE); 2819 free(V_nexpire, M_MRTABLE); 2820 V_nexpire = NULL; 2821 } 2822 2823 VNET_SYSUNINIT(vnet_mroute_uninit, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE, 2824 vnet_mroute_uninit, NULL); 2825 2826 static int 2827 ip_mroute_modevent(module_t mod, int type, void *unused) 2828 { 2829 2830 switch (type) { 2831 case MOD_LOAD: 2832 MRW_TEARDOWN_LOCK_INIT(); 2833 MRW_LOCK_INIT(); 2834 2835 if_detach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event, 2836 if_detached_event, NULL, EVENTHANDLER_PRI_ANY); 2837 if (if_detach_event_tag == NULL) { 2838 printf("ip_mroute: unable to register " 2839 "ifnet_departure_event handler\n"); 2840 MRW_LOCK_DESTROY(); 2841 return (EINVAL); 2842 } 2843 2844 if (!powerof2(mfchashsize)) { 2845 printf("WARNING: %s not a power of 2; using default\n", 2846 "net.inet.ip.mfchashsize"); 2847 mfchashsize = MFCHASHSIZE; 2848 } 2849 2850 pim_encap_cookie = ip_encap_attach(&ipv4_encap_cfg, NULL, M_WAITOK); 2851 2852 ip_mcast_src = X_ip_mcast_src; 2853 ip_mforward = X_ip_mforward; 2854 ip_mrouter_done = X_ip_mrouter_done; 2855 ip_mrouter_get = X_ip_mrouter_get; 2856 ip_mrouter_set = X_ip_mrouter_set; 2857 2858 ip_rsvp_force_done = X_ip_rsvp_force_done; 2859 ip_rsvp_vif = X_ip_rsvp_vif; 2860 2861 legal_vif_num = X_legal_vif_num; 2862 mrt_ioctl = X_mrt_ioctl; 2863 rsvp_input_p = X_rsvp_input; 2864 break; 2865 2866 case MOD_UNLOAD: 2867 /* 2868 * Typically module unload happens after the user-level 2869 * process has shutdown the kernel services (the check 2870 * below insures someone can't just yank the module out 2871 * from under a running process). But if the module is 2872 * just loaded and then unloaded w/o starting up a user 2873 * process we still need to cleanup. 2874 */ 2875 MRW_WLOCK(); 2876 if (ip_mrouter_cnt != 0) { 2877 MRW_WUNLOCK(); 2878 return (EINVAL); 2879 } 2880 ip_mrouter_unloading = 1; 2881 MRW_WUNLOCK(); 2882 2883 EVENTHANDLER_DEREGISTER(ifnet_departure_event, if_detach_event_tag); 2884 2885 if (pim_encap_cookie) { 2886 ip_encap_detach(pim_encap_cookie); 2887 pim_encap_cookie = NULL; 2888 } 2889 2890 ip_mcast_src = NULL; 2891 ip_mforward = NULL; 2892 ip_mrouter_done = NULL; 2893 ip_mrouter_get = NULL; 2894 ip_mrouter_set = NULL; 2895 2896 ip_rsvp_force_done = NULL; 2897 ip_rsvp_vif = NULL; 2898 2899 legal_vif_num = NULL; 2900 mrt_ioctl = NULL; 2901 rsvp_input_p = NULL; 2902 2903 MRW_LOCK_DESTROY(); 2904 MRW_TEARDOWN_LOCK_DESTROY(); 2905 break; 2906 2907 default: 2908 return EOPNOTSUPP; 2909 } 2910 return 0; 2911 } 2912 2913 static moduledata_t ip_mroutemod = { 2914 "ip_mroute", 2915 ip_mroute_modevent, 2916 0 2917 }; 2918 2919 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE); 2920