Home | History | Annotate | Line # | Download | only in netinet
in_var.h revision 1.75
      1 /*	$NetBSD: in_var.h,v 1.75 2016/07/06 05:27:52 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 /* 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 
    108 #ifdef _KERNEL
    109 	struct pslist_entry	ia_hash_pslist_entry;
    110 #endif
    111 };
    112 
    113 struct	in_aliasreq {
    114 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
    115 	struct	sockaddr_in ifra_addr;
    116 	struct	sockaddr_in ifra_dstaddr;
    117 #define	ifra_broadaddr	ifra_dstaddr
    118 	struct	sockaddr_in ifra_mask;
    119 };
    120 
    121 /*
    122  * Given a pointer to an in_ifaddr (ifaddr),
    123  * return a pointer to the addr as a sockaddr_in.
    124  */
    125 #define	IA_SIN(ia) (&(((struct in_ifaddr *)(ia))->ia_addr))
    126 
    127 #ifdef _KERNEL
    128 
    129 /* Note: 61, 127, 251, 509, 1021, 2039 are good. */
    130 #ifndef IN_IFADDR_HASH_SIZE
    131 #define IN_IFADDR_HASH_SIZE	509
    132 #endif
    133 
    134 /*
    135  * This is a bit unconventional, and wastes a little bit of space, but
    136  * because we want a very even hash function we don't use & in_ifaddrhash
    137  * here, but rather % the hash size, which should obviously be prime.
    138  */
    139 
    140 #define	IN_IFADDR_HASH(x) in_ifaddrhashtbl[(u_long)(x) % IN_IFADDR_HASH_SIZE]
    141 
    142 LIST_HEAD(in_ifaddrhashhead, in_ifaddr);	/* Type of the hash head */
    143 TAILQ_HEAD(in_ifaddrhead, in_ifaddr);		/* Type of the list head */
    144 
    145 extern	u_long in_ifaddrhash;			/* size of hash table - 1 */
    146 extern  struct in_ifaddrhashhead *in_ifaddrhashtbl;	/* Hash table head */
    147 extern  struct in_ifaddrhead in_ifaddrhead;		/* List head (in ip_input) */
    148 
    149 extern struct pslist_head *in_ifaddrhashtbl_pslist;
    150 extern u_long in_ifaddrhash_pslist;
    151 
    152 #define IN_IFADDR_HASH_PSLIST(x)					\
    153 	in_ifaddrhashtbl_pslist[(u_long)(x) % IN_IFADDR_HASH_SIZE]
    154 
    155 #define IN_ADDRHASH_READER_FOREACH(__ia, __addr)			\
    156 	PSLIST_READER_FOREACH((__ia), &IN_IFADDR_HASH_PSLIST(__addr),	\
    157 	    struct in_ifaddr, ia_hash_pslist_entry)
    158 #define IN_ADDRHASH_WRITER_INSERT_HEAD(__ia)				\
    159 	PSLIST_WRITER_INSERT_HEAD(					\
    160 	    &IN_IFADDR_HASH_PSLIST((__ia)->ia_addr.sin_addr.s_addr),	\
    161 	    (__ia), ia_hash_pslist_entry)
    162 #define IN_ADDRHASH_WRITER_REMOVE(__ia)					\
    163 	PSLIST_WRITER_REMOVE((__ia), ia_hash_pslist_entry)
    164 #define IN_ADDRHASH_ENTRY_INIT(__ia)					\
    165 	PSLIST_ENTRY_INIT((__ia), ia_hash_pslist_entry);
    166 #define IN_ADDRHASH_ENTRY_DESTROY(__ia)					\
    167 	PSLIST_ENTRY_DESTROY((__ia), ia_hash_pslist_entry);
    168 
    169 extern	const	int	inetctlerrmap[];
    170 
    171 /*
    172  * Macro for finding whether an internet address (in_addr) belongs to one
    173  * of our interfaces (in_ifaddr).  NULL if the address isn't ours.
    174  */
    175 #define INADDR_TO_IA(addr, ia) \
    176 	/* struct in_addr addr; */ \
    177 	/* struct in_ifaddr *ia; */ \
    178 { \
    179 	LIST_FOREACH(ia, &IN_IFADDR_HASH((addr).s_addr), ia_hash) { \
    180 		if (in_hosteq(ia->ia_addr.sin_addr, (addr))) \
    181 			break; \
    182 	} \
    183 }
    184 
    185 /*
    186  * Macro for finding the next in_ifaddr structure with the same internet
    187  * address as ia. Call only with a valid ia pointer.
    188  * Will set ia to NULL if none found.
    189  */
    190 
    191 #define NEXT_IA_WITH_SAME_ADDR(ia) \
    192 	/* struct in_ifaddr *ia; */ \
    193 { \
    194 	struct in_addr addr; \
    195 	addr = ia->ia_addr.sin_addr; \
    196 	do { \
    197 		ia = LIST_NEXT(ia, ia_hash); \
    198 	} while ((ia != NULL) && !in_hosteq(ia->ia_addr.sin_addr, addr)); \
    199 }
    200 
    201 /*
    202  * Macro for finding the interface (ifnet structure) corresponding to one
    203  * of our IP addresses.
    204  */
    205 #define INADDR_TO_IFP(addr, ifp) \
    206 	/* struct in_addr addr; */ \
    207 	/* struct ifnet *ifp; */ \
    208 { \
    209 	struct in_ifaddr *ia; \
    210 \
    211 	INADDR_TO_IA(addr, ia); \
    212 	(ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \
    213 }
    214 
    215 /*
    216  * Macro for finding an internet address structure (in_ifaddr) corresponding
    217  * to a given interface (ifnet structure).
    218  */
    219 #define IFP_TO_IA(ifp, ia) \
    220 	/* struct ifnet *ifp; */ \
    221 	/* struct in_ifaddr *ia; */ \
    222 { \
    223 	struct ifaddr *ifa; \
    224 \
    225 	IFADDR_FOREACH(ifa, ifp) { \
    226 		if (ifa->ifa_addr->sa_family == AF_INET) \
    227 			break; \
    228 	} \
    229 	(ia) = ifatoia(ifa); \
    230 }
    231 
    232 #include <netinet/in_selsrc.h>
    233 /*
    234  * IPv4 per-interface state.
    235  */
    236 struct in_ifinfo {
    237 	struct lltable		*ii_llt;	/* ARP state */
    238 	struct in_ifsysctl	*ii_selsrc;
    239 };
    240 
    241 #endif /* _KERNEL */
    242 
    243 /*
    244  * Internet multicast address structure.  There is one of these for each IP
    245  * multicast group to which this host belongs on a given network interface.
    246  * They are kept in a linked list, rooted in the interface's in_ifaddr
    247  * structure.
    248  */
    249 struct router_info;
    250 
    251 struct in_multi {
    252 	LIST_ENTRY(in_multi) inm_list;	/* list of multicast addresses */
    253 	struct	router_info *inm_rti;	/* router version info */
    254 	struct	ifnet *inm_ifp;		/* back pointer to ifnet */
    255 	struct	in_addr inm_addr;	/* IP multicast address */
    256 	u_int	inm_refcount;		/* no. membership claims by sockets */
    257 	u_int	inm_timer;		/* IGMP membership report timer */
    258 	u_int	inm_state;		/* state of membership */
    259 };
    260 
    261 #ifdef _KERNEL
    262 
    263 #include <net/pktqueue.h>
    264 
    265 extern pktqueue_t *ip_pktq;
    266 
    267 extern int ip_dad_count;		/* Duplicate Address Detection probes */
    268 
    269 /*
    270  * Structure used by functions below to remember position when stepping
    271  * through all of the in_multi records.
    272  */
    273 struct in_multistep {
    274 	int i_n;
    275 	struct in_multi *i_inm;
    276 };
    277 
    278 bool in_multi_group(struct in_addr, struct ifnet *, int);
    279 struct in_multi *in_first_multi(struct in_multistep *);
    280 struct in_multi *in_next_multi(struct in_multistep *);
    281 struct in_multi *in_lookup_multi(struct in_addr, struct ifnet *);
    282 struct in_multi *in_addmulti(struct in_addr *, struct ifnet *);
    283 void in_delmulti(struct in_multi *);
    284 
    285 void in_multi_lock(int);
    286 void in_multi_unlock(void);
    287 int in_multi_lock_held(void);
    288 
    289 struct ifaddr;
    290 
    291 int	in_ifinit(struct ifnet *,
    292 	    struct in_ifaddr *, const struct sockaddr_in *, int, int);
    293 void	in_savemkludge(struct in_ifaddr *);
    294 void	in_restoremkludge(struct in_ifaddr *, struct ifnet *);
    295 void	in_purgemkludge(struct ifnet *);
    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 *);
    300 void	in_purgeaddr(struct ifaddr *);
    301 void	in_purgeif(struct ifnet *);
    302 int	ipflow_fastforward(struct mbuf *);
    303 
    304 struct ipid_state;
    305 typedef struct ipid_state ipid_state_t;
    306 
    307 ipid_state_t *	ip_id_init(void);
    308 void		ip_id_fini(ipid_state_t *);
    309 uint16_t	ip_randomid(ipid_state_t *, uint16_t);
    310 
    311 extern ipid_state_t *	ip_ids;
    312 extern uint16_t		ip_id;
    313 extern int		ip_do_randomid;
    314 
    315 /*
    316  * ip_newid_range: "allocate" num contiguous IP IDs.
    317  *
    318  * => Return the first ID.
    319  */
    320 static __inline uint16_t
    321 ip_newid_range(const struct in_ifaddr *ia, u_int num)
    322 {
    323 	uint16_t id;
    324 
    325 	if (ip_do_randomid) {
    326 		/* XXX ignore num */
    327 		return ip_randomid(ip_ids, ia ? ia->ia_idsalt : 0);
    328 	}
    329 
    330 	/* Never allow an IP ID of 0 (detect wrap). */
    331 	if ((uint16_t)(ip_id + num) < ip_id) {
    332 		ip_id = 1;
    333 	}
    334 	id = htons(ip_id);
    335 	ip_id += num;
    336 	return id;
    337 }
    338 
    339 static __inline uint16_t
    340 ip_newid(const struct in_ifaddr *ia)
    341 {
    342 
    343 	return ip_newid_range(ia, 1);
    344 }
    345 
    346 #ifdef SYSCTLFN_PROTO
    347 int	sysctl_inpcblist(SYSCTLFN_PROTO);
    348 #endif
    349 
    350 #define LLTABLE(ifp)	\
    351 	((struct in_ifinfo *)(ifp)->if_afdata[AF_INET])->ii_llt
    352 
    353 #endif	/* !_KERNEL */
    354 
    355 /* INET6 stuff */
    356 #include <netinet6/in6_var.h>
    357 
    358 #endif /* !_NETINET_IN_VAR_H_ */
    359