| 1 | /* |
| 2 | * Copyright (c) 2000-2020 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
| 5 | * |
| 6 | * This file contains Original Code and/or Modifications of Original Code |
| 7 | * as defined in and that are subject to the Apple Public Source License |
| 8 | * Version 2.0 (the 'License'). You may not use this file except in |
| 9 | * compliance with the License. The rights granted to you under the License |
| 10 | * may not be used to create, or enable the creation or redistribution of, |
| 11 | * unlawful or unlicensed copies of an Apple operating system, or to |
| 12 | * circumvent, violate, or enable the circumvention or violation of, any |
| 13 | * terms of an Apple operating system software license agreement. |
| 14 | * |
| 15 | * Please obtain a copy of the License at |
| 16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
| 17 | * |
| 18 | * The Original Code and all software distributed under the License are |
| 19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
| 20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
| 23 | * Please see the License for the specific language governing rights and |
| 24 | * limitations under the License. |
| 25 | * |
| 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
| 27 | */ |
| 28 | /* |
| 29 | * Copyright (c) 1988 Stephen Deering. |
| 30 | * Copyright (c) 1992, 1993 |
| 31 | * The Regents of the University of California. All rights reserved. |
| 32 | * |
| 33 | * This code is derived from software contributed to Berkeley by |
| 34 | * Stephen Deering of Stanford University. |
| 35 | * |
| 36 | * Redistribution and use in source and binary forms, with or without |
| 37 | * modification, are permitted provided that the following conditions |
| 38 | * are met: |
| 39 | * 1. Redistributions of source code must retain the above copyright |
| 40 | * notice, this list of conditions and the following disclaimer. |
| 41 | * 2. Redistributions in binary form must reproduce the above copyright |
| 42 | * notice, this list of conditions and the following disclaimer in the |
| 43 | * documentation and/or other materials provided with the distribution. |
| 44 | * 3. All advertising materials mentioning features or use of this software |
| 45 | * must display the following acknowledgement: |
| 46 | * This product includes software developed by the University of |
| 47 | * California, Berkeley and its contributors. |
| 48 | * 4. Neither the name of the University nor the names of its contributors |
| 49 | * may be used to endorse or promote products derived from this software |
| 50 | * without specific prior written permission. |
| 51 | * |
| 52 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 53 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 54 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 55 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 56 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 57 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 58 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 59 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 60 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 61 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 62 | * SUCH DAMAGE. |
| 63 | * |
| 64 | * from: @(#)igmp_var.h 8.1 (Berkeley) 7/19/93 |
| 65 | */ |
| 66 | |
| 67 | #ifndef _NETINET_IGMP_VAR_H_ |
| 68 | #define _NETINET_IGMP_VAR_H_ |
| 69 | #include <sys/appleapiopts.h> |
| 70 | |
| 71 | #include <stdint.h> |
| 72 | #include <sys/types.h> |
| 73 | |
| 74 | /* |
| 75 | * Internet Group Management Protocol (IGMP), |
| 76 | * implementation-specific definitions. |
| 77 | * |
| 78 | * Written by Steve Deering, Stanford, May 1988. |
| 79 | * |
| 80 | * MULTICAST Revision: 3.5.1.3 |
| 81 | */ |
| 82 | |
| 83 | struct igmpstat_v3 { |
| 84 | /* |
| 85 | * Structure header (to insulate ABI changes). |
| 86 | */ |
| 87 | uint32_t igps_version; /* version of this structure */ |
| 88 | uint32_t igps_len; /* length of this structure */ |
| 89 | /* |
| 90 | * Message statistics. |
| 91 | */ |
| 92 | uint64_t igps_rcv_total; /* total IGMP messages received */ |
| 93 | uint64_t igps_rcv_tooshort; /* received with too few bytes */ |
| 94 | uint64_t igps_rcv_badttl; /* received with ttl other than 1 */ |
| 95 | uint64_t igps_rcv_badsum; /* received with bad checksum */ |
| 96 | /* |
| 97 | * Query statistics. |
| 98 | */ |
| 99 | uint64_t igps_rcv_v1v2_queries; /* received IGMPv1/IGMPv2 queries */ |
| 100 | uint64_t igps_rcv_v3_queries; /* received IGMPv3 queries */ |
| 101 | uint64_t igps_rcv_badqueries; /* received invalid queries */ |
| 102 | uint64_t igps_rcv_gen_queries; /* received general queries */ |
| 103 | uint64_t igps_rcv_group_queries;/* received group queries */ |
| 104 | uint64_t igps_rcv_gsr_queries; /* received group-source queries */ |
| 105 | uint64_t igps_drop_gsr_queries; /* dropped group-source queries */ |
| 106 | /* |
| 107 | * Report statistics. |
| 108 | */ |
| 109 | uint64_t igps_rcv_reports; /* received membership reports */ |
| 110 | uint64_t igps_rcv_badreports; /* received invalid reports */ |
| 111 | uint64_t igps_rcv_ourreports; /* received reports for our groups */ |
| 112 | uint64_t igps_rcv_nora; /* received w/o Router Alert option */ |
| 113 | uint64_t igps_snd_reports; /* sent membership reports */ |
| 114 | /* |
| 115 | * Padding for future additions. |
| 116 | */ |
| 117 | uint64_t __igps_pad[4]; |
| 118 | } __attribute__((aligned(8))); |
| 119 | |
| 120 | /* |
| 121 | * Old IGMPv2 stat structure for backward compatibility |
| 122 | * |
| 123 | */ |
| 124 | |
| 125 | struct igmpstat { |
| 126 | u_int igps_rcv_total; /* total IGMP messages received */ |
| 127 | u_int igps_rcv_tooshort; /* received with too few bytes */ |
| 128 | u_int igps_rcv_badsum; /* received with bad checksum */ |
| 129 | u_int igps_rcv_queries; /* received membership queries */ |
| 130 | u_int igps_rcv_badqueries; /* received invalid queries */ |
| 131 | u_int igps_rcv_reports; /* received membership reports */ |
| 132 | u_int igps_rcv_badreports; /* received invalid reports */ |
| 133 | u_int igps_rcv_ourreports; /* received reports for our groups */ |
| 134 | u_int igps_snd_reports; /* sent membership reports */ |
| 135 | }; |
| 136 | |
| 137 | #define IGPS_VERSION_3 3 |
| 138 | #define IGPS_VERSION3_LEN 168 |
| 139 | |
| 140 | #ifdef PRIVATE |
| 141 | /* |
| 142 | * Per-interface IGMP router version information. |
| 143 | */ |
| 144 | #ifndef XNU_KERNEL_PRIVATE |
| 145 | struct igmp_ifinfo { |
| 146 | #else |
| 147 | struct igmp_ifinfo_u { |
| 148 | #endif /* XNU_KERNEL_PRIVATE */ |
| 149 | uint32_t igi_ifindex; /* interface this instance belongs to */ |
| 150 | uint32_t igi_version; /* IGMPv3 Host Compatibility Mode */ |
| 151 | uint32_t igi_v1_timer; /* IGMPv1 Querier Present timer (s) */ |
| 152 | uint32_t igi_v2_timer; /* IGMPv2 Querier Present timer (s) */ |
| 153 | uint32_t igi_v3_timer; /* IGMPv3 General Query (interface) timer (s)*/ |
| 154 | uint32_t igi_flags; /* IGMP per-interface flags */ |
| 155 | uint32_t igi_rv; /* IGMPv3 Robustness Variable */ |
| 156 | uint32_t igi_qi; /* IGMPv3 Query Interval (s) */ |
| 157 | uint32_t igi_qri; /* IGMPv3 Query Response Interval (s) */ |
| 158 | uint32_t igi_uri; /* IGMPv3 Unsolicited Report Interval (s) */ |
| 159 | }; |
| 160 | |
| 161 | #define IGIF_SILENT 0x00000001 /* Do not use IGMP on this ifp */ |
| 162 | #define IGIF_LOOPBACK 0x00000002 /* Send IGMP reports to loopback */ |
| 163 | |
| 164 | /* |
| 165 | * IGMP version tag. |
| 166 | */ |
| 167 | #define IGMP_VERSION_NONE 0 /* Invalid */ |
| 168 | #define IGMP_VERSION_1 1 |
| 169 | #define IGMP_VERSION_2 2 |
| 170 | #define IGMP_VERSION_3 3 /* Default */ |
| 171 | #endif /* PRIVATE */ |
| 172 | |
| 173 | #ifdef BSD_KERNEL_PRIVATE |
| 174 | #include <libkern/libkern.h> |
| 175 | #define IGMP_DEBUG 1 |
| 176 | #ifdef IGMP_DEBUG |
| 177 | extern int igmp_debug; |
| 178 | |
| 179 | #define IGMP_PRINTF(x) do { if (igmp_debug) printf x; } while (0) |
| 180 | #define IGMP_INET_PRINTF(addr, x) do { \ |
| 181 | if (igmp_debug) { \ |
| 182 | char _igmp_inet_buf[MAX_IPv4_STR_LEN]; \ |
| 183 | inet_ntop(AF_INET, &(addr), _igmp_inet_buf, \ |
| 184 | sizeof(_igmp_inet_buf)); \ |
| 185 | printf x; \ |
| 186 | } \ |
| 187 | } while (0) |
| 188 | #else |
| 189 | #define IGMP_PRINTF(x) |
| 190 | #endif |
| 191 | |
| 192 | #define OIGMPSTAT_ADD(name, val) os_atomic_add(&igmpstat.name, (val), relaxed) |
| 193 | #define OIGMPSTAT_INC(name) OIGMPSTAT_ADD(name, 1) |
| 194 | |
| 195 | #define IGMPSTAT_ADD(name, val) os_atomic_add(&igmpstat_v3.name, (val), relaxed) |
| 196 | #define IGMPSTAT_INC(name) IGMPSTAT_ADD(name, 1) |
| 197 | |
| 198 | #define IGMP_RANDOM_DELAY(X) (random() % (X) + 1) |
| 199 | |
| 200 | #define IGMP_MAX_STATE_CHANGES 24 /* Max pending changes per group */ |
| 201 | |
| 202 | /* |
| 203 | * IGMP per-group states. |
| 204 | */ |
| 205 | #define IGMP_NOT_MEMBER 0 /* Can garbage collect in_multi */ |
| 206 | #define IGMP_SILENT_MEMBER 1 /* Do not perform IGMP for group */ |
| 207 | #define IGMP_REPORTING_MEMBER 2 /* IGMPv1/2/3 we are reporter */ |
| 208 | #define IGMP_IDLE_MEMBER 3 /* IGMPv1/2 we reported last */ |
| 209 | #define IGMP_LAZY_MEMBER 4 /* IGMPv1/2 other member reporting */ |
| 210 | #define IGMP_SLEEPING_MEMBER 5 /* IGMPv1/2 start query response */ |
| 211 | #define IGMP_AWAKENING_MEMBER 6 /* IGMPv1/2 group timer will start */ |
| 212 | #define IGMP_G_QUERY_PENDING_MEMBER 7 /* IGMPv3 group query pending */ |
| 213 | #define IGMP_SG_QUERY_PENDING_MEMBER 8 /* IGMPv3 source query pending */ |
| 214 | #define IGMP_LEAVING_MEMBER 9 /* IGMPv3 dying gasp (pending last */ |
| 215 | /* retransmission of INCLUDE {}) */ |
| 216 | /* |
| 217 | * IGMPv3 protocol control variables. |
| 218 | */ |
| 219 | #define IGMP_RV_INIT 2 /* Robustness Variable */ |
| 220 | #define IGMP_RV_MIN 1 |
| 221 | #define IGMP_RV_MAX 7 |
| 222 | |
| 223 | #define IGMP_QI_INIT 125 /* Query Interval (s) */ |
| 224 | #define IGMP_QI_MIN 1 |
| 225 | #define IGMP_QI_MAX 255 |
| 226 | |
| 227 | #define IGMP_QRI_INIT 10 /* Query Response Interval (s) */ |
| 228 | #define IGMP_QRI_MIN 1 |
| 229 | #define IGMP_QRI_MAX 255 |
| 230 | |
| 231 | #define IGMP_URI_INIT 3 /* Unsolicited Report Interval (s) */ |
| 232 | #define IGMP_URI_MIN 0 |
| 233 | #define IGMP_URI_MAX 10 |
| 234 | |
| 235 | #define IGMP_MAX_G_GS_PACKETS 8 /* # of packets to answer G/GS */ |
| 236 | #define IGMP_MAX_STATE_CHANGE_PACKETS 8 /* # of packets per state change */ |
| 237 | #define IGMP_MAX_RESPONSE_PACKETS 16 /* # of packets for general query */ |
| 238 | #define IGMP_MAX_RESPONSE_BURST 4 /* # of responses to send at once */ |
| 239 | #define IGMP_RESPONSE_BURST_INTERVAL 1 /* 1 second */ |
| 240 | |
| 241 | /* |
| 242 | * IGMP-specific mbuf flags. |
| 243 | */ |
| 244 | #define M_IGMPV2 M_PROTO1 /* Packet is IGMPv2 */ |
| 245 | #define M_IGMPV3_HDR M_PROTO2 /* Packet has IGMPv3 headers */ |
| 246 | #define M_GROUPREC M_PROTO3 /* mbuf chain is a group record */ |
| 247 | #define M_IGMP_LOOP M_LOOP /* transmit on loif, not real ifp */ |
| 248 | |
| 249 | /* |
| 250 | * Default amount of leading space for IGMPv3 to allocate at the |
| 251 | * beginning of its mbuf packet chains, to avoid fragmentation and |
| 252 | * unnecessary allocation of leading mbufs. |
| 253 | */ |
| 254 | #define RAOPT_LEN 4 /* Length of IP Router Alert option */ |
| 255 | #define IGMP_LEADINGSPACE \ |
| 256 | (sizeof(struct ip) + RAOPT_LEN + sizeof(struct igmp_report)) |
| 257 | |
| 258 | struct igmp_ifinfo { |
| 259 | decl_lck_mtx_data(, igi_lock); |
| 260 | uint32_t igi_refcnt; /* reference count */ |
| 261 | uint32_t igi_debug; /* see ifa_debug flags */ |
| 262 | LIST_ENTRY(igmp_ifinfo) igi_link; |
| 263 | struct ifnet *igi_ifp; /* interface this instance belongs to */ |
| 264 | uint32_t igi_version; /* IGMPv3 Host Compatibility Mode */ |
| 265 | uint32_t igi_v1_timer; /* IGMPv1 Querier Present timer (s) */ |
| 266 | uint32_t igi_v2_timer; /* IGMPv2 Querier Present timer (s) */ |
| 267 | uint32_t igi_v3_timer; /* IGMPv3 General Query (interface) timer (s)*/ |
| 268 | uint32_t igi_flags; /* IGMP per-interface flags */ |
| 269 | uint32_t igi_rv; /* IGMPv3 Robustness Variable */ |
| 270 | uint32_t igi_qi; /* IGMPv3 Query Interval (s) */ |
| 271 | uint32_t igi_qri; /* IGMPv3 Query Response Interval (s) */ |
| 272 | uint32_t igi_uri; /* IGMPv3 Unsolicited Report Interval (s) */ |
| 273 | SLIST_HEAD(, in_multi) igi_relinmhead; /* released groups */ |
| 274 | struct ifqueue igi_gq; /* queue of general query responses */ |
| 275 | struct ifqueue igi_v2q; /* queue of v1/v2 packets */ |
| 276 | }; |
| 277 | |
| 278 | #define IGI_LOCK_ASSERT_HELD(_igi) \ |
| 279 | LCK_MTX_ASSERT(&(_igi)->igi_lock, LCK_MTX_ASSERT_OWNED) |
| 280 | |
| 281 | #define IGI_LOCK_ASSERT_NOTHELD(_igi) \ |
| 282 | LCK_MTX_ASSERT(&(_igi)->igi_lock, LCK_MTX_ASSERT_NOTOWNED) |
| 283 | |
| 284 | #define IGI_LOCK(_igi) \ |
| 285 | lck_mtx_lock(&(_igi)->igi_lock) |
| 286 | |
| 287 | #define IGI_LOCK_SPIN(_igi) \ |
| 288 | lck_mtx_lock_spin(&(_igi)->igi_lock) |
| 289 | |
| 290 | #define IGI_CONVERT_LOCK(_igi) do { \ |
| 291 | IGI_LOCK_ASSERT_HELD(_igi); \ |
| 292 | lck_mtx_convert_spin(&(_igi)->igi_lock); \ |
| 293 | } while (0) |
| 294 | |
| 295 | #define IGI_UNLOCK(_igi) \ |
| 296 | lck_mtx_unlock(&(_igi)->igi_lock) |
| 297 | |
| 298 | #define IGI_ADDREF(_igi) \ |
| 299 | igi_addref(_igi, 0) |
| 300 | |
| 301 | #define IGI_ADDREF_LOCKED(_igi) \ |
| 302 | igi_addref(_igi, 1) |
| 303 | |
| 304 | #define IGI_REMREF(_igi) \ |
| 305 | igi_remref(_igi) |
| 306 | |
| 307 | /* |
| 308 | * Per-link IGMP context. |
| 309 | */ |
| 310 | #define IGMP_IFINFO(ifp) ((ifp)->if_igi) |
| 311 | |
| 312 | /* |
| 313 | * IGMP timer schedule parameters |
| 314 | */ |
| 315 | struct igmp_tparams { |
| 316 | int qpt; /* querier_present_timers_running */ |
| 317 | int it; /* interface_timers_running */ |
| 318 | int cst; /* current_state_timers_running */ |
| 319 | int sct; /* state_change_timers_running */ |
| 320 | bool fast; /* fast timer */ |
| 321 | }; |
| 322 | |
| 323 | extern void igmp_init(struct protosw *, struct domain *); |
| 324 | extern int igmp_change_state(struct in_multi *, struct igmp_tparams *); |
| 325 | extern struct igmp_ifinfo *igmp_domifattach(struct ifnet *, zalloc_flags_t); |
| 326 | extern void igmp_domifreattach(struct igmp_ifinfo *); |
| 327 | extern void igmp_domifdetach(struct ifnet *); |
| 328 | extern void igmp_input(struct mbuf *, int); |
| 329 | extern int igmp_joingroup(struct in_multi *); |
| 330 | extern void igmp_leavegroup(struct in_multi *); |
| 331 | extern void igmp_set_timeout(struct igmp_tparams *); |
| 332 | extern void igmp_set_fast_timeout(struct igmp_tparams *); |
| 333 | extern void igi_addref(struct igmp_ifinfo *, int); |
| 334 | extern void igi_remref(struct igmp_ifinfo *); |
| 335 | __private_extern__ void igmp_initsilent(struct ifnet *, struct igmp_ifinfo *); |
| 336 | |
| 337 | SYSCTL_DECL(_net_inet_igmp); |
| 338 | |
| 339 | #endif /* BSD_KERNEL_PRIVATE */ |
| 340 | |
| 341 | /* |
| 342 | * Names for IGMP sysctl objects |
| 343 | */ |
| 344 | #define IGMPCTL_STATS 1 /* statistics (read-only) */ |
| 345 | #define IGMPCTL_MAXID 2 |
| 346 | |
| 347 | #ifdef BSD_KERNEL_PRIVATE |
| 348 | #define IGMPCTL_NAMES { \ |
| 349 | { 0, 0 }, \ |
| 350 | { "stats", CTLTYPE_STRUCT }, \ |
| 351 | } |
| 352 | |
| 353 | #endif /* BSD_KERNEL_PRIVATE */ |
| 354 | #endif |
| 355 | |