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