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