in_var.h revision 1.78.2.3 1 /* $NetBSD: in_var.h,v 1.78.2.3 2017/01/07 08:56:51 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
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.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
19 *
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.
31 */
32
33 /*
34 * Copyright (c) 1985, 1986, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
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.
48 *
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
59 * SUCH DAMAGE.
60 *
61 * @(#)in_var.h 8.2 (Berkeley) 1/9/95
62 */
63
64 #ifndef _NETINET_IN_VAR_H_
65 #define _NETINET_IN_VAR_H_
66
67 #include <sys/queue.h>
68
69 #define IN_IFF_TENTATIVE 0x01 /* tentative address */
70 #define IN_IFF_DUPLICATED 0x02 /* DAD detected duplicate */
71 #define IN_IFF_DETACHED 0x04 /* may be detached from the link */
72 #define IN_IFF_TRYTENTATIVE 0x08 /* intent to try DAD */
73
74 /* do not input/output */
75 #define IN_IFF_NOTREADY \
76 (IN_IFF_TRYTENTATIVE | IN_IFF_TENTATIVE | IN_IFF_DUPLICATED)
77
78 /*
79 * Interface address, Internet version. One of these structures
80 * is allocated for each interface with an Internet address.
81 * The ifaddr structure contains the protocol-independent part
82 * of the structure and is assumed to be first.
83 */
84 struct in_ifaddr {
85 struct ifaddr ia_ifa; /* protocol-independent info */
86 #define ia_ifp ia_ifa.ifa_ifp
87 #define ia_flags ia_ifa.ifa_flags
88 /* ia_{,sub}net{,mask} in host order */
89 u_int32_t ia_net; /* network number of interface */
90 u_int32_t ia_netmask; /* mask of net part */
91 u_int32_t ia_subnet; /* subnet number, including net */
92 u_int32_t ia_subnetmask; /* mask of subnet part */
93 struct in_addr ia_netbroadcast; /* to recognize net broadcasts */
94 LIST_ENTRY(in_ifaddr) ia_hash; /* entry in bucket of inet addresses */
95 TAILQ_ENTRY(in_ifaddr) ia_list; /* list of internet addresses */
96 struct sockaddr_in ia_addr; /* reserve space for interface name */
97 struct sockaddr_in ia_dstaddr; /* reserve space for broadcast addr */
98 #define ia_broadaddr ia_dstaddr
99 struct sockaddr_in ia_sockmask; /* reserve space for general netmask */
100 LIST_HEAD(, in_multi) ia_multiaddrs; /* list of multicast addresses */
101 struct in_multi *ia_allhosts; /* multicast address record for
102 the allhosts multicast group */
103 uint16_t ia_idsalt; /* ip_id salt for this ia */
104 int ia4_flags; /* address flags */
105 void (*ia_dad_start) (struct ifaddr *); /* DAD start function */
106 void (*ia_dad_stop) (struct ifaddr *); /* DAD stop function */
107 time_t ia_dad_defended; /* last time of DAD defence */
108
109 #ifdef _KERNEL
110 struct pslist_entry ia_hash_pslist_entry;
111 struct pslist_entry ia_pslist_entry;
112 #endif
113 };
114
115 #ifdef _KERNEL
116 static inline void
117 ia4_acquire(struct in_ifaddr *ia, struct psref *psref)
118 {
119
120 KASSERT(ia != NULL);
121 ifa_acquire(&ia->ia_ifa, psref);
122 }
123
124 static inline void
125 ia4_release(struct in_ifaddr *ia, struct psref *psref)
126 {
127
128 if (ia == NULL)
129 return;
130 ifa_release(&ia->ia_ifa, psref);
131 }
132 #endif
133
134 struct in_aliasreq {
135 char ifra_name[IFNAMSIZ]; /* if name, e.g. "en0" */
136 struct sockaddr_in ifra_addr;
137 struct sockaddr_in ifra_dstaddr;
138 #define ifra_broadaddr ifra_dstaddr
139 struct sockaddr_in ifra_mask;
140 };
141
142 /*
143 * Given a pointer to an in_ifaddr (ifaddr),
144 * return a pointer to the addr as a sockaddr_in.
145 */
146 #define IA_SIN(ia) (&(((struct in_ifaddr *)(ia))->ia_addr))
147
148 #ifdef _KERNEL
149
150 /* Note: 61, 127, 251, 509, 1021, 2039 are good. */
151 #ifndef IN_IFADDR_HASH_SIZE
152 #define IN_IFADDR_HASH_SIZE 509
153 #endif
154
155 /*
156 * This is a bit unconventional, and wastes a little bit of space, but
157 * because we want a very even hash function we don't use & in_ifaddrhash
158 * here, but rather % the hash size, which should obviously be prime.
159 */
160
161 #define IN_IFADDR_HASH(x) in_ifaddrhashtbl[(u_long)(x) % IN_IFADDR_HASH_SIZE]
162
163 LIST_HEAD(in_ifaddrhashhead, in_ifaddr); /* Type of the hash head */
164 TAILQ_HEAD(in_ifaddrhead, in_ifaddr); /* Type of the list head */
165
166 extern u_long in_ifaddrhash; /* size of hash table - 1 */
167 extern struct in_ifaddrhashhead *in_ifaddrhashtbl; /* Hash table head */
168 extern struct in_ifaddrhead in_ifaddrhead; /* List head (in ip_input) */
169
170 extern pserialize_t in_ifaddrhash_psz;
171 extern struct pslist_head *in_ifaddrhashtbl_pslist;
172 extern u_long in_ifaddrhash_pslist;
173 extern struct pslist_head in_ifaddrhead_pslist;
174
175 #define IN_IFADDR_HASH_PSLIST(x) \
176 in_ifaddrhashtbl_pslist[(u_long)(x) % IN_IFADDR_HASH_SIZE]
177
178 #define IN_ADDRHASH_READER_FOREACH(__ia, __addr) \
179 PSLIST_READER_FOREACH((__ia), &IN_IFADDR_HASH_PSLIST(__addr), \
180 struct in_ifaddr, ia_hash_pslist_entry)
181 #define IN_ADDRHASH_WRITER_INSERT_HEAD(__ia) \
182 PSLIST_WRITER_INSERT_HEAD( \
183 &IN_IFADDR_HASH_PSLIST((__ia)->ia_addr.sin_addr.s_addr), \
184 (__ia), ia_hash_pslist_entry)
185 #define IN_ADDRHASH_WRITER_REMOVE(__ia) \
186 PSLIST_WRITER_REMOVE((__ia), ia_hash_pslist_entry)
187 #define IN_ADDRHASH_ENTRY_INIT(__ia) \
188 PSLIST_ENTRY_INIT((__ia), ia_hash_pslist_entry);
189 #define IN_ADDRHASH_ENTRY_DESTROY(__ia) \
190 PSLIST_ENTRY_DESTROY((__ia), ia_hash_pslist_entry);
191 #define IN_ADDRHASH_READER_NEXT(__ia) \
192 PSLIST_READER_NEXT((__ia), struct in_ifaddr, ia_hash_pslist_entry)
193
194 #define IN_ADDRLIST_ENTRY_INIT(__ia) \
195 PSLIST_ENTRY_INIT((__ia), ia_pslist_entry)
196 #define IN_ADDRLIST_ENTRY_DESTROY(__ia) \
197 PSLIST_ENTRY_DESTROY((__ia), ia_pslist_entry);
198 #define IN_ADDRLIST_READER_EMPTY() \
199 (PSLIST_READER_FIRST(&in_ifaddrhead_pslist, struct in_ifaddr, \
200 ia_pslist_entry) == NULL)
201 #define IN_ADDRLIST_READER_FIRST() \
202 PSLIST_READER_FIRST(&in_ifaddrhead_pslist, struct in_ifaddr, \
203 ia_pslist_entry)
204 #define IN_ADDRLIST_READER_NEXT(__ia) \
205 PSLIST_READER_NEXT((__ia), struct in_ifaddr, ia_pslist_entry)
206 #define IN_ADDRLIST_READER_FOREACH(__ia) \
207 PSLIST_READER_FOREACH((__ia), &in_ifaddrhead_pslist, \
208 struct in_ifaddr, ia_pslist_entry)
209 #define IN_ADDRLIST_WRITER_INSERT_HEAD(__ia) \
210 PSLIST_WRITER_INSERT_HEAD(&in_ifaddrhead_pslist, (__ia), \
211 ia_pslist_entry)
212 #define IN_ADDRLIST_WRITER_REMOVE(__ia) \
213 PSLIST_WRITER_REMOVE((__ia), ia_pslist_entry)
214 #define IN_ADDRLIST_WRITER_FOREACH(__ia) \
215 PSLIST_WRITER_FOREACH((__ia), &in_ifaddrhead_pslist, \
216 struct in_ifaddr, ia_pslist_entry)
217 #define IN_ADDRLIST_WRITER_FIRST() \
218 PSLIST_WRITER_FIRST(&in_ifaddrhead_pslist, struct in_ifaddr, \
219 ia_pslist_entry)
220 #define IN_ADDRLIST_WRITER_NEXT(__ia) \
221 PSLIST_WRITER_NEXT((__ia), struct in_ifaddr, ia_pslist_entry)
222 #define IN_ADDRLIST_WRITER_INSERT_AFTER(__ia, __new) \
223 PSLIST_WRITER_INSERT_AFTER((__ia), (__new), ia_pslist_entry)
224 #define IN_ADDRLIST_WRITER_EMPTY() \
225 (PSLIST_WRITER_FIRST(&in_ifaddrhead_pslist, struct in_ifaddr, \
226 ia_pslist_entry) == NULL)
227 #define IN_ADDRLIST_WRITER_INSERT_TAIL(__new) \
228 do { \
229 if (IN_ADDRLIST_WRITER_EMPTY()) { \
230 IN_ADDRLIST_WRITER_INSERT_HEAD((__new)); \
231 } else { \
232 struct in_ifaddr *__ia; \
233 IN_ADDRLIST_WRITER_FOREACH(__ia) { \
234 if (IN_ADDRLIST_WRITER_NEXT(__ia) == NULL) { \
235 IN_ADDRLIST_WRITER_INSERT_AFTER(__ia,\
236 (__new)); \
237 break; \
238 } \
239 } \
240 } \
241 } while (0)
242
243 extern const int inetctlerrmap[];
244
245 /*
246 * Find whether an internet address (in_addr) belongs to one
247 * of our interfaces (in_ifaddr). NULL if the address isn't ours.
248 */
249 static inline struct in_ifaddr *
250 in_get_ia(struct in_addr addr)
251 {
252 struct in_ifaddr *ia;
253
254 IN_ADDRHASH_READER_FOREACH(ia, addr.s_addr) {
255 if (in_hosteq(ia->ia_addr.sin_addr, addr))
256 break;
257 }
258
259 return ia;
260 }
261
262 static inline struct in_ifaddr *
263 in_get_ia_psref(struct in_addr addr, struct psref *psref)
264 {
265 struct in_ifaddr *ia;
266 int s;
267
268 s = pserialize_read_enter();
269 ia = in_get_ia(addr);
270 if (ia != NULL)
271 ia4_acquire(ia, psref);
272 pserialize_read_exit(s);
273
274 return ia;
275 }
276
277 /*
278 * Find whether an internet address (in_addr) belongs to a specified
279 * interface. NULL if the address isn't ours.
280 */
281 static inline struct in_ifaddr *
282 in_get_ia_on_iface(struct in_addr addr, struct ifnet *ifp)
283 {
284 struct in_ifaddr *ia;
285
286 IN_ADDRHASH_READER_FOREACH(ia, addr.s_addr) {
287 if (in_hosteq(ia->ia_addr.sin_addr, addr) &&
288 ia->ia_ifp == ifp)
289 break;
290 }
291
292 return ia;
293 }
294
295 static inline struct in_ifaddr *
296 in_get_ia_on_iface_psref(struct in_addr addr, struct ifnet *ifp, struct psref *psref)
297 {
298 struct in_ifaddr *ia;
299 int s;
300
301 s = pserialize_read_enter();
302 ia = in_get_ia_on_iface(addr, ifp);
303 if (ia != NULL)
304 ia4_acquire(ia, psref);
305 pserialize_read_exit(s);
306
307 return ia;
308 }
309
310 /*
311 * Find an internet address structure (in_ifaddr) corresponding
312 * to a given interface (ifnet structure).
313 */
314 static inline struct in_ifaddr *
315 in_get_ia_from_ifp(struct ifnet *ifp)
316 {
317 struct ifaddr *ifa;
318
319 IFADDR_READER_FOREACH(ifa, ifp) {
320 if (ifa->ifa_addr->sa_family == AF_INET)
321 break;
322 }
323
324 return ifatoia(ifa);
325 }
326
327 static inline struct in_ifaddr *
328 in_get_ia_from_ifp_psref(struct ifnet *ifp, struct psref *psref)
329 {
330 struct in_ifaddr *ia;
331 int s;
332
333 s = pserialize_read_enter();
334 ia = in_get_ia_from_ifp(ifp);
335 if (ia != NULL)
336 ia4_acquire(ia, psref);
337 pserialize_read_exit(s);
338
339 return ia;
340 }
341
342 #include <netinet/in_selsrc.h>
343 /*
344 * IPv4 per-interface state.
345 */
346 struct in_ifinfo {
347 struct lltable *ii_llt; /* ARP state */
348 struct in_ifsysctl *ii_selsrc;
349 };
350
351 #endif /* _KERNEL */
352
353 /*
354 * Internet multicast address structure. There is one of these for each IP
355 * multicast group to which this host belongs on a given network interface.
356 * They are kept in a linked list, rooted in the interface's in_ifaddr
357 * structure.
358 */
359 struct router_info;
360
361 struct in_multi {
362 LIST_ENTRY(in_multi) inm_list; /* list of multicast addresses */
363 struct router_info *inm_rti; /* router version info */
364 struct ifnet *inm_ifp; /* back pointer to ifnet */
365 struct in_addr inm_addr; /* IP multicast address */
366 u_int inm_refcount; /* no. membership claims by sockets */
367 u_int inm_timer; /* IGMP membership report timer */
368 u_int inm_state; /* state of membership */
369 };
370
371 #ifdef _KERNEL
372
373 #include <net/pktqueue.h>
374
375 extern pktqueue_t *ip_pktq;
376
377 extern int ip_dad_count; /* Duplicate Address Detection probes */
378 #if defined(INET) && NARP > 0
379 extern int arp_debug;
380 #define arplog(level, fmt, args...) \
381 do { if (arp_debug) log(level, "%s: " fmt, __func__, ##args);} while (0)
382 #else
383 #define arplog(level, fmt, args...)
384 #endif
385
386 /*
387 * Structure used by functions below to remember position when stepping
388 * through all of the in_multi records.
389 */
390 struct in_multistep {
391 int i_n;
392 struct in_multi *i_inm;
393 };
394
395 bool in_multi_group(struct in_addr, struct ifnet *, int);
396 struct in_multi *in_first_multi(struct in_multistep *);
397 struct in_multi *in_next_multi(struct in_multistep *);
398 struct in_multi *in_lookup_multi(struct in_addr, struct ifnet *);
399 struct in_multi *in_addmulti(struct in_addr *, struct ifnet *);
400 void in_delmulti(struct in_multi *);
401
402 void in_multi_lock(int);
403 void in_multi_unlock(void);
404 int in_multi_lock_held(void);
405
406 struct ifaddr;
407
408 int in_ifinit(struct ifnet *, struct in_ifaddr *,
409 const struct sockaddr_in *, const struct sockaddr_in *, int);
410 void in_savemkludge(struct in_ifaddr *);
411 void in_restoremkludge(struct in_ifaddr *, struct ifnet *);
412 void in_purgemkludge(struct ifnet *);
413 void in_setmaxmtu(void);
414 const char *in_fmtaddr(struct in_addr);
415 int in_control(struct socket *, u_long, void *, struct ifnet *);
416 void in_purgeaddr(struct ifaddr *);
417 void in_purgeif(struct ifnet *);
418 void in_addrhash_insert(struct in_ifaddr *);
419 void in_addrhash_remove(struct in_ifaddr *);
420 int ipflow_fastforward(struct mbuf *);
421
422 struct ipid_state;
423 typedef struct ipid_state ipid_state_t;
424
425 ipid_state_t * ip_id_init(void);
426 void ip_id_fini(ipid_state_t *);
427 uint16_t ip_randomid(ipid_state_t *, uint16_t);
428
429 extern ipid_state_t * ip_ids;
430 extern uint16_t ip_id;
431 extern int ip_do_randomid;
432
433 /*
434 * ip_newid_range: "allocate" num contiguous IP IDs.
435 *
436 * => Return the first ID.
437 */
438 static __inline uint16_t
439 ip_newid_range(const struct in_ifaddr *ia, u_int num)
440 {
441 uint16_t id;
442
443 if (ip_do_randomid) {
444 /* XXX ignore num */
445 return ip_randomid(ip_ids, ia ? ia->ia_idsalt : 0);
446 }
447
448 /* Never allow an IP ID of 0 (detect wrap). */
449 if ((uint16_t)(ip_id + num) < ip_id) {
450 ip_id = 1;
451 }
452 id = htons(ip_id);
453 ip_id += num;
454 return id;
455 }
456
457 static __inline uint16_t
458 ip_newid(const struct in_ifaddr *ia)
459 {
460
461 return ip_newid_range(ia, 1);
462 }
463
464 #ifdef SYSCTLFN_PROTO
465 int sysctl_inpcblist(SYSCTLFN_PROTO);
466 #endif
467
468 #define LLTABLE(ifp) \
469 ((struct in_ifinfo *)(ifp)->if_afdata[AF_INET])->ii_llt
470
471 #endif /* !_KERNEL */
472
473 /* INET6 stuff */
474 #include <netinet6/in6_var.h>
475
476 #endif /* !_NETINET_IN_VAR_H_ */
477