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