Home | History | Annotate | Line # | Download | only in netinet
in_var.h revision 1.92
      1 /*	$NetBSD: in_var.h,v 1.92 2017/01/16 07:33:36 ryo 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 #ifdef _KERNEL
    119 static inline void
    120 ia4_acquire(struct in_ifaddr *ia, struct psref *psref)
    121 {
    122 
    123 	KASSERT(ia != NULL);
    124 	ifa_acquire(&ia->ia_ifa, psref);
    125 }
    126 
    127 static inline void
    128 ia4_release(struct in_ifaddr *ia, struct psref *psref)
    129 {
    130 
    131 	if (ia == NULL)
    132 		return;
    133 	ifa_release(&ia->ia_ifa, psref);
    134 }
    135 #endif
    136 
    137 struct	in_aliasreq {
    138 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
    139 	struct	sockaddr_in ifra_addr;
    140 	struct	sockaddr_in ifra_dstaddr;
    141 #define	ifra_broadaddr	ifra_dstaddr
    142 	struct	sockaddr_in ifra_mask;
    143 };
    144 
    145 /*
    146  * Given a pointer to an in_ifaddr (ifaddr),
    147  * return a pointer to the addr as a sockaddr_in.
    148  */
    149 #define	IA_SIN(ia) (&(((struct in_ifaddr *)(ia))->ia_addr))
    150 
    151 #ifdef _KERNEL
    152 
    153 /* Note: 61, 127, 251, 509, 1021, 2039 are good. */
    154 #ifndef IN_IFADDR_HASH_SIZE
    155 #define IN_IFADDR_HASH_SIZE	509
    156 #endif
    157 
    158 /*
    159  * This is a bit unconventional, and wastes a little bit of space, but
    160  * because we want a very even hash function we don't use & in_ifaddrhash
    161  * here, but rather % the hash size, which should obviously be prime.
    162  */
    163 
    164 #define	IN_IFADDR_HASH(x) in_ifaddrhashtbl[(u_long)(x) % IN_IFADDR_HASH_SIZE]
    165 
    166 LIST_HEAD(in_ifaddrhashhead, in_ifaddr);	/* Type of the hash head */
    167 TAILQ_HEAD(in_ifaddrhead, in_ifaddr);		/* Type of the list head */
    168 
    169 extern	u_long in_ifaddrhash;			/* size of hash table - 1 */
    170 extern  struct in_ifaddrhashhead *in_ifaddrhashtbl;	/* Hash table head */
    171 extern  struct in_ifaddrhead in_ifaddrhead;		/* List head (in ip_input) */
    172 
    173 extern pserialize_t in_ifaddrhash_psz;
    174 extern struct pslist_head *in_ifaddrhashtbl_pslist;
    175 extern u_long in_ifaddrhash_pslist;
    176 extern struct pslist_head in_ifaddrhead_pslist;
    177 
    178 #define IN_IFADDR_HASH_PSLIST(x)					\
    179 	in_ifaddrhashtbl_pslist[(u_long)(x) % IN_IFADDR_HASH_SIZE]
    180 
    181 #define IN_ADDRHASH_READER_FOREACH(__ia, __addr)			\
    182 	PSLIST_READER_FOREACH((__ia), &IN_IFADDR_HASH_PSLIST(__addr),	\
    183 	    struct in_ifaddr, ia_hash_pslist_entry)
    184 #define IN_ADDRHASH_WRITER_INSERT_HEAD(__ia)				\
    185 	PSLIST_WRITER_INSERT_HEAD(					\
    186 	    &IN_IFADDR_HASH_PSLIST((__ia)->ia_addr.sin_addr.s_addr),	\
    187 	    (__ia), ia_hash_pslist_entry)
    188 #define IN_ADDRHASH_WRITER_REMOVE(__ia)					\
    189 	PSLIST_WRITER_REMOVE((__ia), ia_hash_pslist_entry)
    190 #define IN_ADDRHASH_ENTRY_INIT(__ia)					\
    191 	PSLIST_ENTRY_INIT((__ia), ia_hash_pslist_entry);
    192 #define IN_ADDRHASH_ENTRY_DESTROY(__ia)					\
    193 	PSLIST_ENTRY_DESTROY((__ia), ia_hash_pslist_entry);
    194 #define IN_ADDRHASH_READER_NEXT(__ia)					\
    195 	PSLIST_READER_NEXT((__ia), struct in_ifaddr, ia_hash_pslist_entry)
    196 
    197 #define IN_ADDRLIST_ENTRY_INIT(__ia)					\
    198 	PSLIST_ENTRY_INIT((__ia), ia_pslist_entry)
    199 #define IN_ADDRLIST_ENTRY_DESTROY(__ia)					\
    200 	PSLIST_ENTRY_DESTROY((__ia), ia_pslist_entry);
    201 #define IN_ADDRLIST_READER_EMPTY()					\
    202 	(PSLIST_READER_FIRST(&in_ifaddrhead_pslist, struct in_ifaddr,	\
    203 	                     ia_pslist_entry) == NULL)
    204 #define IN_ADDRLIST_READER_FIRST()					\
    205 	PSLIST_READER_FIRST(&in_ifaddrhead_pslist, struct in_ifaddr,	\
    206 	                    ia_pslist_entry)
    207 #define IN_ADDRLIST_READER_NEXT(__ia)					\
    208 	PSLIST_READER_NEXT((__ia), struct in_ifaddr, ia_pslist_entry)
    209 #define IN_ADDRLIST_READER_FOREACH(__ia)				\
    210 	PSLIST_READER_FOREACH((__ia), &in_ifaddrhead_pslist,		\
    211 	                      struct in_ifaddr, ia_pslist_entry)
    212 #define IN_ADDRLIST_WRITER_INSERT_HEAD(__ia)				\
    213 	PSLIST_WRITER_INSERT_HEAD(&in_ifaddrhead_pslist, (__ia),	\
    214 	    ia_pslist_entry)
    215 #define IN_ADDRLIST_WRITER_REMOVE(__ia)					\
    216 	PSLIST_WRITER_REMOVE((__ia), ia_pslist_entry)
    217 #define IN_ADDRLIST_WRITER_FOREACH(__ia)				\
    218 	PSLIST_WRITER_FOREACH((__ia), &in_ifaddrhead_pslist,		\
    219 	                      struct in_ifaddr, ia_pslist_entry)
    220 #define IN_ADDRLIST_WRITER_FIRST()					\
    221 	PSLIST_WRITER_FIRST(&in_ifaddrhead_pslist, struct in_ifaddr,	\
    222 	                    ia_pslist_entry)
    223 #define IN_ADDRLIST_WRITER_NEXT(__ia)					\
    224 	PSLIST_WRITER_NEXT((__ia), struct in_ifaddr, ia_pslist_entry)
    225 #define IN_ADDRLIST_WRITER_INSERT_AFTER(__ia, __new)			\
    226 	PSLIST_WRITER_INSERT_AFTER((__ia), (__new), ia_pslist_entry)
    227 #define IN_ADDRLIST_WRITER_EMPTY()					\
    228 	(PSLIST_WRITER_FIRST(&in_ifaddrhead_pslist, struct in_ifaddr,	\
    229 	    ia_pslist_entry) == NULL)
    230 #define IN_ADDRLIST_WRITER_INSERT_TAIL(__new)				\
    231 	do {								\
    232 		if (IN_ADDRLIST_WRITER_EMPTY()) {			\
    233 			IN_ADDRLIST_WRITER_INSERT_HEAD((__new));	\
    234 		} else {						\
    235 			struct in_ifaddr *__ia;				\
    236 			IN_ADDRLIST_WRITER_FOREACH(__ia) {		\
    237 				if (IN_ADDRLIST_WRITER_NEXT(__ia) == NULL) { \
    238 					IN_ADDRLIST_WRITER_INSERT_AFTER(__ia,\
    239 					    (__new));			\
    240 					break;				\
    241 				}					\
    242 			}						\
    243 		}							\
    244 	} while (0)
    245 
    246 extern	const	int	inetctlerrmap[];
    247 
    248 /*
    249  * Find whether an internet address (in_addr) belongs to one
    250  * of our interfaces (in_ifaddr).  NULL if the address isn't ours.
    251  */
    252 static inline struct in_ifaddr *
    253 in_get_ia(struct in_addr addr)
    254 {
    255 	struct in_ifaddr *ia;
    256 
    257 	IN_ADDRHASH_READER_FOREACH(ia, addr.s_addr) {
    258 		if (in_hosteq(ia->ia_addr.sin_addr, addr))
    259 			break;
    260 	}
    261 
    262 	return ia;
    263 }
    264 
    265 static inline struct in_ifaddr *
    266 in_get_ia_psref(struct in_addr addr, struct psref *psref)
    267 {
    268 	struct in_ifaddr *ia;
    269 	int s;
    270 
    271 	s = pserialize_read_enter();
    272 	ia = in_get_ia(addr);
    273 	if (ia != NULL)
    274 		ia4_acquire(ia, psref);
    275 	pserialize_read_exit(s);
    276 
    277 	return ia;
    278 }
    279 
    280 /*
    281  * Find whether an internet address (in_addr) belongs to a specified
    282  * interface.  NULL if the address isn't ours.
    283  */
    284 static inline struct in_ifaddr *
    285 in_get_ia_on_iface(struct in_addr addr, struct ifnet *ifp)
    286 {
    287 	struct in_ifaddr *ia;
    288 
    289 	IN_ADDRHASH_READER_FOREACH(ia, addr.s_addr) {
    290 		if (in_hosteq(ia->ia_addr.sin_addr, addr) &&
    291 		    ia->ia_ifp == ifp)
    292 			break;
    293 	}
    294 
    295 	return ia;
    296 }
    297 
    298 static inline struct in_ifaddr *
    299 in_get_ia_on_iface_psref(struct in_addr addr, struct ifnet *ifp, struct psref *psref)
    300 {
    301 	struct in_ifaddr *ia;
    302 	int s;
    303 
    304 	s = pserialize_read_enter();
    305 	ia = in_get_ia_on_iface(addr, ifp);
    306 	if (ia != NULL)
    307 		ia4_acquire(ia, psref);
    308 	pserialize_read_exit(s);
    309 
    310 	return ia;
    311 }
    312 
    313 /*
    314  * Find an internet address structure (in_ifaddr) corresponding
    315  * to a given interface (ifnet structure).
    316  */
    317 static inline struct in_ifaddr *
    318 in_get_ia_from_ifp(struct ifnet *ifp)
    319 {
    320 	struct ifaddr *ifa;
    321 
    322 	IFADDR_READER_FOREACH(ifa, ifp) {
    323 		if (ifa->ifa_addr->sa_family == AF_INET)
    324 			break;
    325 	}
    326 
    327 	return ifatoia(ifa);
    328 }
    329 
    330 static inline struct in_ifaddr *
    331 in_get_ia_from_ifp_psref(struct ifnet *ifp, struct psref *psref)
    332 {
    333 	struct in_ifaddr *ia;
    334 	int s;
    335 
    336 	s = pserialize_read_enter();
    337 	ia = in_get_ia_from_ifp(ifp);
    338 	if (ia != NULL)
    339 		ia4_acquire(ia, psref);
    340 	pserialize_read_exit(s);
    341 
    342 	return ia;
    343 }
    344 
    345 #include <netinet/in_selsrc.h>
    346 /*
    347  * IPv4 per-interface state.
    348  */
    349 struct in_ifinfo {
    350 	struct lltable		*ii_llt;	/* ARP state */
    351 	struct in_ifsysctl	*ii_selsrc;
    352 };
    353 
    354 #endif /* _KERNEL */
    355 
    356 /*
    357  * Internet multicast address structure.  There is one of these for each IP
    358  * multicast group to which this host belongs on a given network interface.
    359  * They are kept in a linked list, rooted in the interface's in_ifaddr
    360  * structure.
    361  */
    362 struct router_info;
    363 
    364 struct in_multi {
    365 	LIST_ENTRY(in_multi) inm_list;	/* list of multicast addresses */
    366 	struct	router_info *inm_rti;	/* router version info */
    367 	struct	ifnet *inm_ifp;		/* back pointer to ifnet */
    368 	struct	in_addr inm_addr;	/* IP multicast address */
    369 	u_int	inm_refcount;		/* no. membership claims by sockets */
    370 	u_int	inm_timer;		/* IGMP membership report timer */
    371 	u_int	inm_state;		/* state of membership */
    372 };
    373 
    374 #ifdef _KERNEL
    375 
    376 #include <net/pktqueue.h>
    377 
    378 extern pktqueue_t *ip_pktq;
    379 
    380 extern int ip_dad_count;		/* Duplicate Address Detection probes */
    381 #if defined(INET) && NARP > 0
    382 extern int arp_debug;
    383 #define arplog(level, fmt, args...) \
    384 	do { if (arp_debug) log(level, "%s: " fmt, __func__, ##args);} while (0)
    385 #else
    386 #define arplog(level, fmt, args...)
    387 #endif
    388 
    389 /*
    390  * Structure used by functions below to remember position when stepping
    391  * through all of the in_multi records.
    392  */
    393 struct in_multistep {
    394 	int i_n;
    395 	struct in_multi *i_inm;
    396 };
    397 
    398 bool in_multi_group(struct in_addr, struct ifnet *, int);
    399 struct in_multi *in_first_multi(struct in_multistep *);
    400 struct in_multi *in_next_multi(struct in_multistep *);
    401 struct in_multi *in_lookup_multi(struct in_addr, struct ifnet *);
    402 struct in_multi *in_addmulti(struct in_addr *, struct ifnet *);
    403 void in_delmulti(struct in_multi *);
    404 
    405 void in_multi_lock(int);
    406 void in_multi_unlock(void);
    407 int in_multi_lock_held(void);
    408 
    409 struct ifaddr;
    410 
    411 int	in_ifinit(struct ifnet *, struct in_ifaddr *,
    412     const struct sockaddr_in *, const struct sockaddr_in *, int);
    413 void	in_savemkludge(struct in_ifaddr *);
    414 void	in_restoremkludge(struct in_ifaddr *, struct ifnet *);
    415 void	in_purgemkludge(struct ifnet *);
    416 void	in_setmaxmtu(void);
    417 const char *in_fmtaddr(char *, struct in_addr);
    418 int	in_control(struct socket *, u_long, void *, struct ifnet *);
    419 void	in_purgeaddr(struct ifaddr *);
    420 void	in_purgeif(struct ifnet *);
    421 void	in_addrhash_insert(struct in_ifaddr *);
    422 void	in_addrhash_remove(struct in_ifaddr *);
    423 int	ipflow_fastforward(struct mbuf *);
    424 
    425 struct ipid_state;
    426 typedef struct ipid_state ipid_state_t;
    427 
    428 ipid_state_t *	ip_id_init(void);
    429 void		ip_id_fini(ipid_state_t *);
    430 uint16_t	ip_randomid(ipid_state_t *, uint16_t);
    431 
    432 extern ipid_state_t *	ip_ids;
    433 extern uint16_t		ip_id;
    434 extern int		ip_do_randomid;
    435 
    436 /*
    437  * ip_newid_range: "allocate" num contiguous IP IDs.
    438  *
    439  * => Return the first ID.
    440  */
    441 static __inline uint16_t
    442 ip_newid_range(const struct in_ifaddr *ia, u_int num)
    443 {
    444 	uint16_t id;
    445 
    446 	if (ip_do_randomid) {
    447 		/* XXX ignore num */
    448 		return ip_randomid(ip_ids, ia ? ia->ia_idsalt : 0);
    449 	}
    450 
    451 	/* Never allow an IP ID of 0 (detect wrap). */
    452 	if ((uint16_t)(ip_id + num) < ip_id) {
    453 		ip_id = 1;
    454 	}
    455 	id = htons(ip_id);
    456 	ip_id += num;
    457 	return id;
    458 }
    459 
    460 static __inline uint16_t
    461 ip_newid(const struct in_ifaddr *ia)
    462 {
    463 
    464 	return ip_newid_range(ia, 1);
    465 }
    466 
    467 #ifdef SYSCTLFN_PROTO
    468 int	sysctl_inpcblist(SYSCTLFN_PROTO);
    469 #endif
    470 
    471 #define LLTABLE(ifp)	\
    472 	((struct in_ifinfo *)(ifp)->if_afdata[AF_INET])->ii_llt
    473 
    474 #endif	/* !_KERNEL */
    475 
    476 /* INET6 stuff */
    477 #include <netinet6/in6_var.h>
    478 
    479 #endif /* !_NETINET_IN_VAR_H_ */
    480