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