1 /* 2 * Copyright (c) 2000-2021 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) 1985, 1986, 1993 30 * The Regents of the University of California. All rights reserved. 31 * 32 * Redistribution and use in source and binary forms, with or without 33 * modification, are permitted provided that the following conditions 34 * are met: 35 * 1. Redistributions of source code must retain the above copyright 36 * notice, this list of conditions and the following disclaimer. 37 * 2. Redistributions in binary form must reproduce the above copyright 38 * notice, this list of conditions and the following disclaimer in the 39 * documentation and/or other materials provided with the distribution. 40 * 3. All advertising materials mentioning features or use of this software 41 * must display the following acknowledgement: 42 * This product includes software developed by the University of 43 * California, Berkeley and its contributors. 44 * 4. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)in_var.h 8.2 (Berkeley) 1/9/95 61 */ 62 63 #ifndef _NETINET_IN_VAR_H_ 64 #define _NETINET_IN_VAR_H_ 65 #include <sys/appleapiopts.h> 66 67 #include <sys/queue.h> 68 #include <sys/kern_event.h> 69 #include <net/if_var.h> 70 #include <net/net_kev.h> 71 #include <netinet/in.h> 72 73 #ifdef BSD_KERNEL_PRIVATE 74 #include <net/route.h> 75 76 /* 77 * Interface address, Internet version. One of these structures 78 * is allocated for each Internet address on an interface. 79 * The ifaddr structure contains the protocol-independent part 80 * of the structure and is assumed to be first. 81 */ 82 struct in_ifaddr { 83 struct ifaddr ia_ifa; /* protocol-independent info */ 84 #define ia_ifp ia_ifa.ifa_ifp 85 #define ia_flags ia_ifa.ifa_flags 86 /* ia_{,sub}net{,mask} in host order */ 87 u_int32_t ia_net; /* network number of interface */ 88 u_int32_t ia_netmask; /* mask of net part */ 89 u_int32_t ia_subnet; /* subnet number, including net */ 90 u_int32_t ia_subnetmask; /* mask of subnet part */ 91 struct in_addr ia_netbroadcast; /* to recognize net broadcasts */ 92 TAILQ_ENTRY(in_ifaddr) ia_link; /* tailq macro glue */ 93 struct sockaddr_in ia_addr; /* reserve space for interface name */ 94 struct sockaddr_in ia_dstaddr; /* reserve space for broadcast addr */ 95 #define ia_broadaddr ia_dstaddr 96 struct sockaddr_in ia_sockmask; /* reserve space for general netmask */ 97 TAILQ_ENTRY(in_ifaddr) ia_hash; /* hash bucket entry */ 98 }; 99 100 #define ifatoia(ifa) ((struct in_ifaddr *)(void *)(ifa)) 101 #endif /* BSD_KERNEL_PRIVATE */ 102 103 struct in_aliasreq { 104 char ifra_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 105 struct sockaddr_in ifra_addr; 106 struct sockaddr_in ifra_broadaddr; 107 #define ifra_dstaddr ifra_broadaddr 108 struct sockaddr_in ifra_mask; 109 }; 110 111 /* 112 * Event data, inet style. 113 */ 114 struct kev_in_data { 115 struct net_event_data link_data; 116 struct in_addr ia_addr; /* interface address */ 117 u_int32_t ia_net; /* network number of interface */ 118 u_int32_t ia_netmask; /* mask of net part */ 119 u_int32_t ia_subnet; /* subnet number, including net */ 120 u_int32_t ia_subnetmask; /* mask of subnet part */ 121 struct in_addr ia_netbroadcast; /* to recognize net broadcasts */ 122 struct in_addr ia_dstaddr; 123 }; 124 125 struct kev_in_collision { 126 struct net_event_data link_data; /* link where ARP was received on */ 127 struct in_addr ia_ipaddr; /* conflicting IP address */ 128 u_char hw_len; /* length of hardware address */ 129 u_char hw_addr[0]; /* variable length hardware address */ 130 }; 131 132 struct kev_in_arpfailure { 133 struct net_event_data link_data; /* link where ARP is being sent */ 134 }; 135 136 struct kev_in_arpalive { 137 struct net_event_data link_data; /* link where ARP was received */ 138 }; 139 140 #ifdef PRIVATE 141 /* 142 * Common structure for KEV_SOCKET_SUBCLASS 143 * Have to place here to avoid declaration dependencies. 144 */ 145 struct kev_socket_event_data { 146 union sockaddr_in_4_6 kev_sockname; 147 union sockaddr_in_4_6 kev_peername; 148 }; 149 150 struct kev_socket_closed { 151 struct kev_socket_event_data ev_data; 152 }; 153 #endif /* PRIVATE */ 154 155 #ifdef __APPLE_API_PRIVATE 156 struct kev_in_portinuse { 157 u_int16_t port; /* conflicting port number in host order */ 158 u_int32_t req_pid; /* PID port requestor */ 159 u_int32_t reserved[2]; 160 }; 161 #endif /* __APPLE_API_PRIVATE */ 162 163 #ifdef BSD_KERNEL_PRIVATE 164 extern void socket_post_kev_msg(uint32_t, struct kev_socket_event_data *, 165 uint32_t); 166 extern void socket_post_kev_msg_closed(struct socket *); 167 168 #include <net/if.h> 169 #include <net/if_var.h> 170 #include <net/if_llatbl.h> 171 #include <kern/locks.h> 172 #include <sys/tree.h> 173 /* 174 * Given a pointer to an in_ifaddr (ifaddr), 175 * return a pointer to the addr as a sockaddr_in. 176 */ 177 #define IA_SIN(ia) (&(((struct in_ifaddr *)(ia))->ia_addr)) 178 #define IA_DSTSIN(ia) (&(((struct in_ifaddr *)(ia))->ia_dstaddr)) 179 180 #define IN_LNAOF(in, ifa) \ 181 ((ntohl((in).s_addr) & ~((struct in_ifaddr *)(ifa)->ia_subnetmask)) 182 183 /* 184 * Hash table for IPv4 addresses. 185 */ 186 extern TAILQ_HEAD(in_ifaddrhead, in_ifaddr) in_ifaddrhead; 187 TAILQ_HEAD(in_ifaddrhashhead, in_ifaddr); 188 extern lck_rw_t in_ifaddr_rwlock; 189 190 #define INADDR_HASH(x) inaddr_hashlookup(x) 191 192 extern u_char inetctlerrmap[]; 193 194 /* 195 * Macro for finding the interface (ifnet structure) corresponding to one 196 * of our IP addresses. 197 */ 198 #define INADDR_TO_IFP(addr, ifp) \ 199 /* struct in_addr addr; */ \ 200 /* struct ifnet *ifp; */ \ 201 { \ 202 struct in_ifaddr *ia; \ 203 \ 204 lck_rw_lock_shared(&in_ifaddr_rwlock); \ 205 TAILQ_FOREACH(ia, INADDR_HASH((addr).s_addr), ia_hash) { \ 206 IFA_LOCK_SPIN(&ia->ia_ifa); \ 207 if (IA_SIN(ia)->sin_addr.s_addr == (addr).s_addr) { \ 208 IFA_UNLOCK(&ia->ia_ifa); \ 209 break; \ 210 } \ 211 IFA_UNLOCK(&ia->ia_ifa); \ 212 } \ 213 (ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \ 214 lck_rw_done(&in_ifaddr_rwlock); \ 215 } 216 217 /* 218 * Macro for finding the internet address structure (in_ifaddr) corresponding 219 * to a given interface (ifnet structure). Caller is responsible for freeing 220 * the reference. 221 */ 222 #define IFP_TO_IA(ifp, ia) \ 223 /* struct ifnet *ifp; */ \ 224 /* struct in_ifaddr *ia; */ \ 225 { \ 226 lck_rw_lock_shared(&in_ifaddr_rwlock); \ 227 for ((ia) = TAILQ_FIRST(&in_ifaddrhead); \ 228 (ia) != NULL && (ia)->ia_ifp != (ifp); \ 229 (ia) = TAILQ_NEXT((ia), ia_link)) \ 230 continue; \ 231 if ((ia) != NULL) \ 232 IFA_ADDREF(&(ia)->ia_ifa); \ 233 lck_rw_done(&in_ifaddr_rwlock); \ 234 } 235 236 /* 237 * This information should be part of the ifnet structure but we don't wish 238 * to change that - as it might break a number of things 239 */ 240 241 /* 242 * Legacy IPv4 IGMP per-link structure. 243 */ 244 struct router_info { 245 struct ifnet *rti_ifp; 246 int rti_type; /* type of router which is querier on this interface */ 247 int rti_time; /* # of slow timeouts since last old query */ 248 SLIST_ENTRY(router_info) rti_list; 249 }; 250 251 /* 252 * IPv4 multicast IGMP-layer source entry. 253 */ 254 struct ip_msource { 255 RB_ENTRY(ip_msource) ims_link; /* RB tree links */ 256 in_addr_t ims_haddr; /* host byte order */ 257 struct ims_st { 258 uint16_t ex; /* # of exclusive members */ 259 uint16_t in; /* # of inclusive members */ 260 } ims_st[2]; /* state at t0, t1 */ 261 uint8_t ims_stp; /* pending query */ 262 }; 263 264 /* 265 * IPv4 multicast PCB-layer source entry. 266 */ 267 struct in_msource { 268 RB_ENTRY(ip_msource) ims_link; /* RB tree links */ 269 in_addr_t ims_haddr; /* host byte order */ 270 uint8_t imsl_st[2]; /* state before/at commit */ 271 }; 272 273 RB_HEAD(ip_msource_tree, ip_msource); /* define struct ip_msource_tree */ 274 275 RB_PROTOTYPE_SC_PREV(__private_extern__, ip_msource_tree, ip_msource, 276 ims_link, ip_msource_cmp); 277 278 /* 279 * IPv4 multicast PCB-layer group filter descriptor. 280 */ 281 struct in_mfilter { 282 struct ip_msource_tree imf_sources; /* source list for (S,G) */ 283 u_long imf_nsrc; /* # of source entries */ 284 uint8_t imf_st[2]; /* state before/at commit */ 285 }; 286 287 struct igmp_ifinfo; 288 289 /* 290 * IPv4 group descriptor. 291 * 292 * For every entry on an ifnet's if_multiaddrs list which represents 293 * an IP multicast group, there is one of these structures. 294 * 295 * If any source filters are present, then a node will exist in the RB-tree 296 * to permit fast lookup by source whenever an operation takes place. 297 * This permits pre-order traversal when we issue reports. 298 * Source filter trees are kept separately from the socket layer to 299 * greatly simplify locking. 300 * 301 * When IGMPv3 is active, inm_timer is the response to group query timer. 302 * The state-change timer inm_sctimer is separate; whenever state changes 303 * for the group the state change record is generated and transmitted, 304 * and kept if retransmissions are necessary. 305 * 306 * The request count here is a count of requests for this address, not a 307 * count of pointers to this structure. 308 * 309 * FUTURE: inm_link is now only used when groups are being purged 310 * on a detaching ifnet. It could be demoted to a SLIST_ENTRY. 311 */ 312 struct in_multi { 313 decl_lck_mtx_data(, inm_lock); 314 u_int32_t inm_refcount; /* reference count */ 315 u_int32_t inm_reqcnt; /* request count for this address */ 316 u_int32_t inm_debug; /* see ifa_debug flags */ 317 LIST_ENTRY(in_multi) inm_link; /* queue macro glue */ 318 struct in_addr inm_addr; /* IP multicast address, convenience */ 319 struct ifnet *inm_ifp; /* back pointer to ifnet */ 320 struct ifmultiaddr *inm_ifma; /* back pointer to ifmultiaddr */ 321 u_int inm_timer; /* IGMPv1/v2 group / v3 query timer */ 322 u_int inm_state; /* state of the membership */ 323 void *inm_rti; /* unused, legacy field */ 324 325 /* New fields for IGMPv3 follow. */ 326 struct igmp_ifinfo *inm_igi; /* IGMP info */ 327 SLIST_ENTRY(in_multi) inm_dtle; /* detached waiting for rele */ 328 SLIST_ENTRY(in_multi) inm_nrele; /* to-be-released by IGMP */ 329 u_int32_t inm_nrelecnt; /* deferred release count */ 330 struct ip_msource_tree inm_srcs; /* tree of sources */ 331 u_long inm_nsrc; /* # of tree entries */ 332 333 struct ifqueue inm_scq; /* queue of pending 334 * state-change packets */ 335 struct timeval inm_lastgsrtv; /* Time of last G-S-R query */ 336 uint16_t inm_sctimer; /* state-change timer */ 337 uint16_t inm_scrv; /* state-change rexmit count */ 338 339 /* 340 * SSM state counters which track state at T0 (the time the last 341 * state-change report's RV timer went to zero) and T1 342 * (time of pending report, i.e. now). 343 * Used for computing IGMPv3 state-change reports. Several refcounts 344 * are maintained here to optimize for common use-cases. 345 */ 346 struct inm_st { 347 uint16_t iss_fmode; /* IGMP filter mode */ 348 uint16_t iss_asm; /* # of ASM listeners */ 349 uint16_t iss_ex; /* # of exclusive members */ 350 uint16_t iss_in; /* # of inclusive members */ 351 uint16_t iss_rec; /* # of recorded sources */ 352 } inm_st[2]; /* state at t0, t1 */ 353 354 void (*inm_trace) /* callback fn for tracing refs */ 355 (struct in_multi *, int); 356 }; 357 358 #define INM_LOCK_ASSERT_HELD(_inm) \ 359 LCK_MTX_ASSERT(&(_inm)->inm_lock, LCK_MTX_ASSERT_OWNED) 360 361 #define INM_LOCK_ASSERT_NOTHELD(_inm) \ 362 LCK_MTX_ASSERT(&(_inm)->inm_lock, LCK_MTX_ASSERT_NOTOWNED) 363 364 #define INM_LOCK(_inm) \ 365 lck_mtx_lock(&(_inm)->inm_lock) 366 367 #define INM_LOCK_SPIN(_inm) \ 368 lck_mtx_lock_spin(&(_inm)->inm_lock) 369 370 #define INM_CONVERT_LOCK(_inm) do { \ 371 INM_LOCK_ASSERT_HELD(_inm); \ 372 lck_mtx_convert_spin(&(_inm)->inm_lock); \ 373 } while (0) 374 375 #define INM_UNLOCK(_inm) \ 376 lck_mtx_unlock(&(_inm)->inm_lock) 377 378 #define INM_ADDREF(_inm) \ 379 inm_addref(_inm, 0) 380 381 #define INM_ADDREF_LOCKED(_inm) \ 382 inm_addref(_inm, 1) 383 384 #define INM_REMREF(_inm) \ 385 inm_remref(_inm, 0) 386 387 #define INM_REMREF_LOCKED(_inm) \ 388 inm_remref(_inm, 1) 389 390 #ifdef SYSCTL_DECL 391 SYSCTL_DECL(_net_inet_ip); 392 SYSCTL_DECL(_net_inet_raw); 393 #endif 394 395 extern LIST_HEAD(in_multihead, in_multi) in_multihead; 396 397 /* 398 * Structure used by macros below to remember position when stepping through 399 * all of the in_multi records. 400 */ 401 struct in_multistep { 402 struct in_multi *i_inm; 403 }; 404 405 /* 406 * Macro for looking up the in_multi record for a given IP multicast address 407 * on a given interface. If no matching record is found, "inm" is set null. 408 * 409 * We do this differently compared other BSD implementations; instead of 410 * walking the if_multiaddrs list at the interface and returning the 411 * ifma_protospec value of a matching entry, we search the global list 412 * of in_multi records and find it that way. Otherwise either the two 413 * structures (in_multi, ifmultiaddr) need to be ref counted both ways, 414 * which will make things too complicated, or they need to reside in the 415 * same protected domain, which they aren't. 416 * 417 * Must be called with in_multihead_lock held. 418 */ 419 #define IN_LOOKUP_MULTI(addr, ifp, inm) \ 420 /* struct in_addr *addr; */ \ 421 /* struct ifnet *ifp; */ \ 422 /* struct in_multi *inm; */ \ 423 do { \ 424 struct in_multistep _step; \ 425 IN_FIRST_MULTI(_step, inm); \ 426 while ((inm) != NULL) { \ 427 INM_LOCK_SPIN(inm); \ 428 if ((inm)->inm_ifp == (ifp) && \ 429 (inm)->inm_addr.s_addr == (addr)->s_addr) { \ 430 INM_ADDREF_LOCKED(inm); \ 431 INM_UNLOCK(inm); \ 432 break; \ 433 } \ 434 INM_UNLOCK(inm); \ 435 IN_NEXT_MULTI(_step, inm); \ 436 } \ 437 } while (0) 438 439 /* 440 * Macro to step through all of the in_multi records, one at a time. 441 * The current position is remembered in "step", which the caller must 442 * provide. IN_FIRST_MULTI(), below, must be called to initialize "step" 443 * and get the first record. Both macros return a NULL "inm" when there 444 * are no remaining records. 445 * 446 * Must be called with in_multihead_lock held. 447 */ 448 #define IN_NEXT_MULTI(step, inm) \ 449 /* struct in_multistep step; */ \ 450 /* struct in_multi *inm; */ \ 451 do { \ 452 in_multihead_lock_assert(LCK_RW_ASSERT_HELD); \ 453 if (((inm) = (step).i_inm) != NULL) \ 454 (step).i_inm = LIST_NEXT((step).i_inm, inm_link); \ 455 } while (0) 456 457 #define IN_FIRST_MULTI(step, inm) \ 458 /* struct in_multistep step; */ \ 459 /* struct in_multi *inm; */ \ 460 do { \ 461 in_multihead_lock_assert(LCK_RW_ASSERT_HELD); \ 462 (step).i_inm = LIST_FIRST(&in_multihead); \ 463 IN_NEXT_MULTI((step), (inm)); \ 464 } while (0) 465 466 extern lck_mtx_t *inet_domain_mutex; 467 extern struct domain *inetdomain; 468 469 struct ip_moptions; 470 struct inpcb; 471 472 /* 473 * Return values for imo_multi_filter(). 474 */ 475 #define MCAST_PASS 0 /* Pass */ 476 #define MCAST_NOTGMEMBER 1 /* This host not a member of group */ 477 #define MCAST_NOTSMEMBER 2 /* This host excluded source */ 478 #define MCAST_MUTED 3 /* [deprecated] */ 479 480 /* 481 * Per-interface IPv4 structures. 482 */ 483 struct in_ifextra { 484 uint32_t netsig_len; 485 u_int8_t netsig[IFNET_SIGNATURELEN]; 486 struct lltable *ii_llt; /* ARP state */ 487 }; 488 #define IN_IFEXTRA(_ifp) (_ifp->if_inetdata) 489 #define LLTABLE(ifp) ((IN_IFEXTRA(ifp) == NULL) ? NULL : IN_IFEXTRA(ifp)->ii_llt) 490 491 extern u_int32_t ipv4_ll_arp_aware; 492 493 extern void in_ifaddr_init(void); 494 extern int imo_multi_filter(const struct ip_moptions *, 495 const struct ifnet *, const struct sockaddr_in *, 496 const struct sockaddr_in *); 497 extern int imo_clone(struct inpcb *, struct inpcb *); 498 extern void inm_commit(struct in_multi *); 499 extern void inm_clear_recorded(struct in_multi *); 500 extern void inm_print(const struct in_multi *); 501 extern int inm_record_source(struct in_multi *inm, const in_addr_t); 502 extern void inm_release(struct in_multi *); 503 extern void in_multi_init(void); 504 extern struct in_multi *in_addmulti(struct in_addr *, struct ifnet *); 505 extern void in_delmulti(struct in_multi *); 506 extern int in_leavegroup(struct in_multi *, struct in_mfilter *); 507 extern int in_multi_detach(struct in_multi *); 508 extern void inm_addref(struct in_multi *, int); 509 extern void inm_remref(struct in_multi *, int); 510 extern void inm_purge(struct in_multi *); 511 extern uint8_t ims_get_mode(const struct in_multi *, 512 const struct ip_msource *, uint8_t); 513 extern int in_control(struct socket *, u_long, caddr_t, struct ifnet *, 514 struct proc *); 515 extern int in_inithead(void **, int); 516 extern void in_rtqdrain(void); 517 extern struct radix_node *in_validate(struct radix_node *); 518 extern void ip_input(struct mbuf *); 519 extern void ip_input_process_list(struct mbuf *); 520 extern int in_ifadown(struct ifaddr *ifa, int); 521 extern void in_ifscrub(struct ifnet *, struct in_ifaddr *, int); 522 extern uint32_t inaddr_hashval(uint32_t); 523 extern struct in_ifaddrhashhead *inaddr_hashlookup(uint32_t); 524 extern void in_purgeaddrs(struct ifnet *); 525 extern void gre_input(struct mbuf *, int); 526 extern void imf_leave(struct in_mfilter *); 527 extern void imf_purge(struct in_mfilter *); 528 extern int inp_join_group(struct inpcb *, struct sockopt *); 529 extern int inp_leave_group(struct inpcb *, struct sockopt *); 530 extern void in_multihead_lock_exclusive(void); 531 extern void in_multihead_lock_shared(void); 532 extern void in_multihead_lock_assert(int); 533 extern void in_multihead_lock_done(void); 534 #endif /* BSD_KERNEL_PRIVATE */ 535 /* INET6 stuff */ 536 #include <netinet6/in6_var.h> 537 #endif /* _NETINET_IN_VAR_H_ */ 538