1 /* $NetBSD: in_var.h,v 1.61 2008/02/06 03:20:51 matt Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Public Access Networks Corporation ("Panix"). It was developed under
9 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1985, 1986, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)in_var.h 8.2 (Berkeley) 1/9/95
64 #ifndef _NETINET_IN_VAR_H_
65 #define _NETINET_IN_VAR_H_
67 #include <sys/queue.h>
70 * Interface address, Internet version. One of these structures
71 * is allocated for each interface with an Internet address.
72 * The ifaddr structure contains the protocol-independent part
73 * of the structure and is assumed to be first.
76 struct ifaddr ia_ifa
; /* protocol-independent info */
77 #define ia_ifp ia_ifa.ifa_ifp
78 #define ia_flags ia_ifa.ifa_flags
79 /* ia_{,sub}net{,mask} in host order */
80 u_int32_t ia_net
; /* network number of interface */
81 u_int32_t ia_netmask
; /* mask of net part */
82 u_int32_t ia_subnet
; /* subnet number, including net */
83 u_int32_t ia_subnetmask
; /* mask of subnet part */
84 struct in_addr ia_netbroadcast
; /* to recognize net broadcasts */
85 LIST_ENTRY(in_ifaddr
) ia_hash
; /* entry in bucket of inet addresses */
86 TAILQ_ENTRY(in_ifaddr
) ia_list
; /* list of internet addresses */
87 struct sockaddr_in ia_addr
; /* reserve space for interface name */
88 struct sockaddr_in ia_dstaddr
; /* reserve space for broadcast addr */
89 #define ia_broadaddr ia_dstaddr
90 struct sockaddr_in ia_sockmask
; /* reserve space for general netmask */
91 LIST_HEAD(, in_multi
) ia_multiaddrs
; /* list of multicast addresses */
92 struct in_multi
*ia_allhosts
; /* multicast address record for
93 the allhosts multicast group */
94 uint16_t ia_idsalt
; /* ip_id salt for this ia */
98 char ifra_name
[IFNAMSIZ
]; /* if name, e.g. "en0" */
99 struct sockaddr_in ifra_addr
;
100 struct sockaddr_in ifra_dstaddr
;
101 #define ifra_broadaddr ifra_dstaddr
102 struct sockaddr_in ifra_mask
;
105 * Given a pointer to an in_ifaddr (ifaddr),
106 * return a pointer to the addr as a sockaddr_in.
108 #define IA_SIN(ia) (&(((struct in_ifaddr *)(ia))->ia_addr))
112 #ifndef IN_IFADDR_HASH_SIZE
113 #define IN_IFADDR_HASH_SIZE 509 /* 61, 127, 251, 509, 1021, 2039 are good */
115 #ifndef IN_MULTI_HASH_SIZE
116 #define IN_MULTI_HASH_SIZE 509 /* 61, 127, 251, 509, 1021, 2039 are good */
120 * This is a bit unconventional, and wastes a little bit of space, but
121 * because we want a very even hash function we don't use & in_ifaddrhash
122 * here, but rather % the hash size, which should obviously be prime.
125 #define IN_IFADDR_HASH(x) in_ifaddrhashtbl[(u_long)(x) % IN_IFADDR_HASH_SIZE]
126 #define IN_MULTI_HASH(x, ifp) \
127 (in_multihashtbl[(u_long)((x) ^ (ifp->if_index)) % IN_MULTI_HASH_SIZE])
129 LIST_HEAD(in_ifaddrhashhead
, in_ifaddr
); /* Type of the hash head */
130 TAILQ_HEAD(in_ifaddrhead
, in_ifaddr
); /* Type of the list head */
131 LIST_HEAD(in_multihashhead
, in_multi
); /* Type of the hash head */
134 extern u_long in_ifaddrhash
; /* size of hash table - 1 */
135 extern int in_ifaddrentries
; /* total number of addrs */
136 extern struct in_ifaddrhashhead
*in_ifaddrhashtbl
; /* Hash table head */
137 extern struct in_ifaddrhead in_ifaddrhead
; /* List head (in ip_input) */
139 extern u_long in_multihash
; /* size of hash table - 1 */
140 extern int in_multientries
; /* total number of addrs */
141 extern struct in_multihashhead
*in_multihashtbl
; /* Hash table head */
143 extern struct ifqueue ipintrq
; /* ip packet input queue */
144 extern const int inetctlerrmap
[];
148 * Macro for finding whether an internet address (in_addr) belongs to one
149 * of our interfaces (in_ifaddr). NULL if the address isn't ours.
151 #define INADDR_TO_IA(addr, ia) \
152 /* struct in_addr addr; */ \
153 /* struct in_ifaddr *ia; */ \
155 LIST_FOREACH(ia, &IN_IFADDR_HASH((addr).s_addr), ia_hash) { \
156 if (in_hosteq(ia->ia_addr.sin_addr, (addr))) \
162 * Macro for finding the next in_ifaddr structure with the same internet
163 * address as ia. Call only with a valid ia pointer.
164 * Will set ia to NULL if none found.
167 #define NEXT_IA_WITH_SAME_ADDR(ia) \
168 /* struct in_ifaddr *ia; */ \
170 struct in_addr addr; \
171 addr = ia->ia_addr.sin_addr; \
173 ia = LIST_NEXT(ia, ia_hash); \
174 } while ((ia != NULL) && !in_hosteq(ia->ia_addr.sin_addr, addr)); \
178 * Macro for finding the interface (ifnet structure) corresponding to one
179 * of our IP addresses.
181 #define INADDR_TO_IFP(addr, ifp) \
182 /* struct in_addr addr; */ \
183 /* struct ifnet *ifp; */ \
185 struct in_ifaddr *ia; \
187 INADDR_TO_IA(addr, ia); \
188 (ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \
192 * Macro for finding an internet address structure (in_ifaddr) corresponding
193 * to a given interface (ifnet structure).
195 #define IFP_TO_IA(ifp, ia) \
196 /* struct ifnet *ifp; */ \
197 /* struct in_ifaddr *ia; */ \
199 struct ifaddr *ifa; \
201 IFADDR_FOREACH(ifa, ifp) { \
202 if (ifa->ifa_addr->sa_family == AF_INET) \
205 (ia) = ifatoia(ifa); \
210 * Per-interface router version information.
213 LIST_ENTRY(router_info
) rti_link
;
214 struct ifnet
*rti_ifp
;
215 int rti_type
; /* type of router on this interface */
216 int rti_age
; /* time since last v1 query */
220 * Internet multicast address structure. There is one of these for each IP
221 * multicast group to which this host belongs on a given network interface.
222 * They are kept in a linked list, rooted in the interface's in_ifaddr
226 LIST_ENTRY(in_multi
) inm_list
; /* list of multicast addresses */
227 struct router_info
*inm_rti
; /* router version info */
228 struct ifnet
*inm_ifp
; /* back pointer to ifnet */
229 struct in_addr inm_addr
; /* IP multicast address */
230 u_int inm_refcount
; /* no. membership claims by sockets */
231 u_int inm_timer
; /* IGMP membership report timer */
232 u_int inm_state
; /* state of membership */
237 * Structure used by macros below to remember position when stepping through
238 * all of the in_multi records.
240 struct in_multistep
{
242 struct in_multi
*i_inm
;
246 * Macro for looking up the in_multi record for a given IP multicast address
247 * on a given interface. If no matching record is found, "inm" returns NULL.
249 #define IN_LOOKUP_MULTI(addr, ifp, inm) \
250 /* struct in_addr addr; */ \
251 /* struct ifnet *ifp; */ \
252 /* struct in_multi *inm; */ \
254 LIST_FOREACH((inm), &IN_MULTI_HASH(((addr).s_addr), (ifp)), inm_list) {\
255 if (in_hosteq((inm)->inm_addr, (addr)) && \
256 (inm)->inm_ifp == (ifp)) \
262 * Macro to step through all of the in_multi records, one at a time.
263 * The current position is remembered in "step", which the caller must
264 * provide. IN_FIRST_MULTI(), below, must be called to initialize "step"
265 * and get the first record. Both macros return a NULL "inm" when there
266 * are no remaining records.
268 #define IN_NEXT_MULTI(step, inm) \
269 /* struct in_multistep step; */ \
270 /* struct in_multi *inm; */ \
272 while ((step).i_inm == NULL && (step).i_n < IN_MULTI_HASH_SIZE) \
273 (step).i_inm = LIST_FIRST(&in_multihashtbl[++(step).i_n]); \
274 if (((inm) = (step).i_inm) != NULL) \
275 (step).i_inm = LIST_NEXT((inm), inm_list); \
278 #define IN_FIRST_MULTI(step, inm) \
279 /* struct in_multistep step; */ \
280 /* struct in_multi *inm; */ \
283 (step).i_inm = LIST_FIRST(&in_multihashtbl[0]); \
284 IN_NEXT_MULTI((step), (inm)); \
289 int in_ifinit(struct ifnet
*,
290 struct in_ifaddr
*, const struct sockaddr_in
*, int);
291 void in_savemkludge(struct in_ifaddr
*);
292 void in_restoremkludge(struct in_ifaddr
*, struct ifnet
*);
293 void in_purgemkludge(struct ifnet
*);
294 struct in_multi
*in_addmulti(struct in_addr
*, struct ifnet
*);
295 void in_delmulti(struct in_multi
*);
296 void in_ifscrub(struct ifnet
*, struct in_ifaddr
*);
297 void in_setmaxmtu(void);
298 const char *in_fmtaddr(struct in_addr
);
299 int in_control(struct socket
*, u_long
, void *, struct ifnet
*,
301 void in_purgeaddr(struct ifaddr
*);
302 void in_purgeif(struct ifnet
*);
303 void ip_input(struct mbuf
*);
304 int ipflow_fastforward(struct mbuf
*);
305 void ip_initid(void);
307 extern uint16_t ip_id
;
308 static __inline
uint16_t ip_newid(const struct in_ifaddr
*);
310 uint16_t ip_randomid(uint16_t);
311 extern int ip_do_randomid
;
314 * ip_newid_range: "allocate" num contiguous ip_ids.
316 * => return the first id.
319 static __inline
uint16_t
320 ip_newid_range(const struct in_ifaddr
*ia
, unsigned int num
)
324 if (ip_do_randomid
) {
326 return ip_randomid(ia
? ia
->ia_idsalt
: 0);
330 * never allow an ip_id of 0. (detect wrap)
332 if ((uint16_t)(ip_id
+ num
) < ip_id
)
340 static __inline
uint16_t
341 ip_newid(const struct in_ifaddr
*ia
)
343 return ip_newid_range(ia
, 1);
346 #ifdef SYSCTLFN_PROTO
347 int sysctl_inpcblist(SYSCTLFN_PROTO
);
353 #include <netinet6/in6_var.h>
355 #endif /* !_NETINET_IN_VAR_H_ */