]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_var.h
df7a968af0066d06e2c37c0751b0cd41e04a5274
[apple/xnu.git] / bsd / netinet / in_var.h
1 /*
2 * Copyright (c) 2000-2008 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 * $FreeBSD: src/sys/netinet/in_var.h,v 1.33.2.2 2001/07/17 10:50:01 ru Exp $
62 */
63
64 #ifndef _NETINET_IN_VAR_H_
65 #define _NETINET_IN_VAR_H_
66 #include <sys/appleapiopts.h>
67
68 #include <sys/queue.h>
69 #ifdef __APPLE__
70 #include <sys/kern_event.h>
71 #endif
72
73 #ifdef 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 #endif /* 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 u_int32_t ifra_unused; /* not used: used to be 'dlt' */
108 };
109
110 /*
111 * Event data, internet style.
112 */
113 struct kev_in_data {
114 struct net_event_data link_data;
115 struct in_addr ia_addr;
116 u_int32_t ia_net; /* network number of interface */
117 u_int32_t ia_netmask; /* mask of net part */
118 u_int32_t ia_subnet; /* subnet number, including net */
119 u_int32_t ia_subnetmask; /* mask of subnet part */
120 struct in_addr ia_netbroadcast;/* to recognize net broadcasts */
121 struct in_addr ia_dstaddr;
122 };
123
124 struct kev_in_collision {
125 struct net_event_data link_data; /* link colliding arp was received on */
126 struct in_addr ia_ipaddr; /* IP address we and another node are using */
127 u_char hw_len; /* length of hardware address */
128 u_char hw_addr[0]; /* variable length hardware address */
129 };
130
131 #ifdef __APPLE_API_PRIVATE
132 struct kev_in_portinuse {
133 u_int16_t port; /* conflicting port number in host order */
134 u_int32_t req_pid; /* PID port requestor */
135 u_int32_t reserved[2];
136 };
137 #endif
138
139
140 /*
141 * Define inet event subclass and specific inet events.
142 */
143
144 #define KEV_INET_SUBCLASS 1
145
146 #define KEV_INET_NEW_ADDR 1
147 #define KEV_INET_CHANGED_ADDR 2
148 #define KEV_INET_ADDR_DELETED 3
149 #define KEV_INET_SIFDSTADDR 4
150 #define KEV_INET_SIFBRDADDR 5
151 #define KEV_INET_SIFNETMASK 6
152 #define KEV_INET_ARPCOLLISION 7 /* use kev_in_collision */
153
154 #ifdef __APPLE_API_PRIVATE
155 #define KEV_INET_PORTINUSE 8 /* use ken_in_portinuse */
156 #endif
157
158 #ifdef KERNEL_PRIVATE
159 #include <net/if_var.h>
160 #include <kern/locks.h>
161 /*
162 * Given a pointer to an in_ifaddr (ifaddr),
163 * return a pointer to the addr as a sockaddr_in.
164 */
165 #define IA_SIN(ia) (&(((struct in_ifaddr *)(ia))->ia_addr))
166 #define IA_DSTSIN(ia) (&(((struct in_ifaddr *)(ia))->ia_dstaddr))
167
168 #define IN_LNAOF(in, ifa) \
169 ((ntohl((in).s_addr) & ~((struct in_ifaddr *)(ifa)->ia_subnetmask))
170
171 /*
172 * Hash table for IPv4 addresses.
173 */
174 __private_extern__ TAILQ_HEAD(in_ifaddrhead, in_ifaddr) in_ifaddrhead;
175 __private_extern__ TAILQ_HEAD(in_ifaddrhashhead, in_ifaddr) *in_ifaddrhashtbl;
176 __private_extern__ lck_rw_t *in_ifaddr_rwlock;
177
178 #define INADDR_HASH(x) (&in_ifaddrhashtbl[inaddr_hashval(x)])
179
180 extern struct ifqueue ipintrq; /* ip packet input queue */
181 extern struct in_addr zeroin_addr;
182 extern u_char inetctlerrmap[];
183
184 extern int apple_hwcksum_tx;
185 extern int apple_hwcksum_rx;
186
187 /*
188 * Macro for finding the interface (ifnet structure) corresponding to one
189 * of our IP addresses.
190 */
191 #define INADDR_TO_IFP(addr, ifp) \
192 /* struct in_addr addr; */ \
193 /* struct ifnet *ifp; */ \
194 { \
195 struct in_ifaddr *ia; \
196 \
197 lck_rw_lock_shared(in_ifaddr_rwlock); \
198 TAILQ_FOREACH(ia, INADDR_HASH((addr).s_addr), ia_hash) \
199 if (IA_SIN(ia)->sin_addr.s_addr == (addr).s_addr) \
200 break; \
201 (ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \
202 lck_rw_done(in_ifaddr_rwlock); \
203 }
204
205 /*
206 * Macro for finding the internet address structure (in_ifaddr) corresponding
207 * to a given interface (ifnet structure). Caller is responsible for freeing
208 * the reference.
209 */
210 #define IFP_TO_IA(ifp, ia) \
211 /* struct ifnet *ifp; */ \
212 /* struct in_ifaddr *ia; */ \
213 { \
214 lck_rw_lock_shared(in_ifaddr_rwlock); \
215 for ((ia) = TAILQ_FIRST(&in_ifaddrhead); \
216 (ia) != NULL && (ia)->ia_ifp != (ifp); \
217 (ia) = TAILQ_NEXT((ia), ia_link)) \
218 continue; \
219 if ((ia) != NULL) \
220 ifaref(&(ia)->ia_ifa); \
221 lck_rw_done(in_ifaddr_rwlock); \
222 }
223
224 /*
225 * This information should be part of the ifnet structure but we don't wish
226 * to change that - as it might break a number of things
227 */
228
229 struct router_info {
230 struct ifnet *rti_ifp;
231 int rti_type; /* type of router which is querier on this interface */
232 int rti_time; /* # of slow timeouts since last old query */
233 struct router_info *rti_next;
234 };
235
236 /*
237 * Internet multicast address structure. There is one of these for each IP
238 * multicast group to which this host belongs on a given network interface.
239 * For every entry on the interface's if_multiaddrs list which represents
240 * an IP multicast group, there is one of these structures. They are also
241 * kept on a system-wide list to make it easier to keep our legacy IGMP code
242 * compatible with the rest of the world (see IN_FIRST_MULTI et al, below).
243 */
244 struct in_multi {
245 LIST_ENTRY(in_multi) inm_link; /* queue macro glue */
246 struct in_addr inm_addr; /* IP multicast address, convenience */
247 struct ifnet *inm_ifp; /* back pointer to ifnet */
248 struct ifmultiaddr *inm_ifma; /* back pointer to ifmultiaddr */
249 u_int inm_timer; /* IGMP membership report timer */
250 u_int inm_state; /* state of the membership */
251 struct router_info *inm_rti; /* router info*/
252 };
253
254 #ifdef SYSCTL_DECL
255 SYSCTL_DECL(_net_inet_ip);
256 SYSCTL_DECL(_net_inet_raw);
257 #endif
258
259 extern LIST_HEAD(in_multihead, in_multi) in_multihead;
260
261 /*
262 * Structure used by macros below to remember position when stepping through
263 * all of the in_multi records.
264 */
265 struct in_multistep {
266 struct in_multi *i_inm;
267 };
268
269 /*
270 * Macro for looking up the in_multi record for a given IP multicast address
271 * on a given interface. If no matching record is found, "inm" is set null.
272 */
273 #define IN_LOOKUP_MULTI(addr, ifp, inm) \
274 /* struct in_addr addr; */ \
275 /* struct ifnet *ifp; */ \
276 /* struct in_multi *inm; */ \
277 do { \
278 struct ifmultiaddr *ifma; \
279 \
280 LIST_FOREACH(ifma, &((ifp)->if_multiaddrs), ifma_link) { \
281 if (ifma->ifma_addr->sa_family == AF_INET \
282 && ((struct sockaddr_in *)ifma->ifma_addr)->sin_addr.s_addr == \
283 (addr).s_addr) \
284 break; \
285 } \
286 (inm) = ifma ? ifma->ifma_protospec : NULL; \
287 } while(0)
288
289 /*
290 * Macro to step through all of the in_multi records, one at a time.
291 * The current position is remembered in "step", which the caller must
292 * provide. IN_FIRST_MULTI(), below, must be called to initialize "step"
293 * and get the first record. Both macros return a NULL "inm" when there
294 * are no remaining records.
295 */
296 #define IN_NEXT_MULTI(step, inm) \
297 /* struct in_multistep step; */ \
298 /* struct in_multi *inm; */ \
299 do { \
300 if (((inm) = (step).i_inm) != NULL) \
301 (step).i_inm = LIST_NEXT((step).i_inm, inm_link); \
302 } while(0)
303
304 #define IN_FIRST_MULTI(step, inm) \
305 /* struct in_multistep step; */ \
306 /* struct in_multi *inm; */ \
307 do { \
308 (step).i_inm = LIST_FIRST(&in_multihead); \
309 IN_NEXT_MULTI((step), (inm)); \
310 } while(0)
311
312 struct route;
313
314 extern void in_ifaddr_init(void);
315 extern struct in_multi *in_addmulti(struct in_addr *, struct ifnet *);
316 extern void in_delmulti(struct in_multi **);
317 extern int in_control(struct socket *, u_long, caddr_t, struct ifnet *,
318 struct proc *);
319 extern void in_rtqdrain(void);
320 extern struct radix_node *in_validate(struct radix_node *);
321 extern void ip_input(struct mbuf *);
322 extern int in_ifadown(struct ifaddr *ifa, int);
323 extern void in_ifscrub(struct ifnet *, struct in_ifaddr *, int);
324 extern int ipflow_fastforward(struct mbuf *);
325 #if IPFLOW
326 extern void ipflow_create(const struct route *, struct mbuf *);
327 extern void ipflow_slowtimo(void);
328 #endif /* IPFLOW */
329 extern u_int32_t inaddr_hashval(u_int32_t);
330
331 #endif /* KERNEL_PRIVATE */
332
333 /* INET6 stuff */
334 #include <netinet6/in6_var.h>
335
336 #endif /* _NETINET_IN_VAR_H_ */