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