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