Home | History | Annotate | Line # | Download | only in netinet
in_var.h revision 1.67
      1 /*	$NetBSD: in_var.h,v 1.67 2014/05/23 19:27:48 rmind 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 /*
     70  * Interface address, Internet version.  One of these structures
     71  * is allocated for each interface with an Internet address.
     72  * The ifaddr structure contains the protocol-independent part
     73  * of the structure and is assumed to be first.
     74  */
     75 struct in_ifaddr {
     76 	struct	ifaddr ia_ifa;		/* protocol-independent info */
     77 #define	ia_ifp		ia_ifa.ifa_ifp
     78 #define ia_flags	ia_ifa.ifa_flags
     79 					/* ia_{,sub}net{,mask} in host order */
     80 	u_int32_t ia_net;		/* network number of interface */
     81 	u_int32_t ia_netmask;		/* mask of net part */
     82 	u_int32_t ia_subnet;		/* subnet number, including net */
     83 	u_int32_t ia_subnetmask;	/* mask of subnet part */
     84 	struct	in_addr ia_netbroadcast; /* to recognize net broadcasts */
     85 	LIST_ENTRY(in_ifaddr) ia_hash;	/* entry in bucket of inet addresses */
     86 	TAILQ_ENTRY(in_ifaddr) ia_list;	/* list of internet addresses */
     87 	struct	sockaddr_in ia_addr;	/* reserve space for interface name */
     88 	struct	sockaddr_in ia_dstaddr;	/* reserve space for broadcast addr */
     89 #define	ia_broadaddr	ia_dstaddr
     90 	struct	sockaddr_in ia_sockmask; /* reserve space for general netmask */
     91 	LIST_HEAD(, in_multi) ia_multiaddrs; /* list of multicast addresses */
     92 	struct	in_multi *ia_allhosts;	/* multicast address record for
     93 					   the allhosts multicast group */
     94 	uint16_t ia_idsalt;		/* ip_id salt for this ia */
     95 };
     96 
     97 struct	in_aliasreq {
     98 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
     99 	struct	sockaddr_in ifra_addr;
    100 	struct	sockaddr_in ifra_dstaddr;
    101 #define	ifra_broadaddr	ifra_dstaddr
    102 	struct	sockaddr_in ifra_mask;
    103 };
    104 /*
    105  * Given a pointer to an in_ifaddr (ifaddr),
    106  * return a pointer to the addr as a sockaddr_in.
    107  */
    108 #define	IA_SIN(ia) (&(((struct in_ifaddr *)(ia))->ia_addr))
    109 
    110 
    111 #ifdef	_KERNEL
    112 #ifndef IN_IFADDR_HASH_SIZE
    113 #define IN_IFADDR_HASH_SIZE 509	/* 61, 127, 251, 509, 1021, 2039 are good */
    114 #endif
    115 #ifndef IN_MULTI_HASH_SIZE
    116 #define IN_MULTI_HASH_SIZE 509	/* 61, 127, 251, 509, 1021, 2039 are good */
    117 #endif
    118 
    119 /*
    120  * This is a bit unconventional, and wastes a little bit of space, but
    121  * because we want a very even hash function we don't use & in_ifaddrhash
    122  * here, but rather % the hash size, which should obviously be prime.
    123  */
    124 
    125 #define	IN_IFADDR_HASH(x) in_ifaddrhashtbl[(u_long)(x) % IN_IFADDR_HASH_SIZE]
    126 #define IN_MULTI_HASH(x, ifp) \
    127 	(in_multihashtbl[(u_long)((x) ^ (ifp->if_index)) % IN_MULTI_HASH_SIZE])
    128 
    129 LIST_HEAD(in_ifaddrhashhead, in_ifaddr);	/* Type of the hash head */
    130 TAILQ_HEAD(in_ifaddrhead, in_ifaddr);		/* Type of the list head */
    131 LIST_HEAD(in_multihashhead, in_multi);		/* Type of the hash head */
    132 
    133 extern	u_long in_ifaddrhash;			/* size of hash table - 1 */
    134 extern  struct in_ifaddrhashhead *in_ifaddrhashtbl;	/* Hash table head */
    135 extern  struct in_ifaddrhead in_ifaddrhead;		/* List head (in ip_input) */
    136 
    137 extern	u_long in_multihash;			/* size of hash table - 1 */
    138 extern  struct in_multihashhead *in_multihashtbl;	/* Hash table head */
    139 
    140 extern	struct	ifqueue	ipintrq;		/* ip packet input queue */
    141 extern	const	int	inetctlerrmap[];
    142 
    143 /*
    144  * Macro for finding whether an internet address (in_addr) belongs to one
    145  * of our interfaces (in_ifaddr).  NULL if the address isn't ours.
    146  */
    147 #define INADDR_TO_IA(addr, ia) \
    148 	/* struct in_addr addr; */ \
    149 	/* struct in_ifaddr *ia; */ \
    150 { \
    151 	LIST_FOREACH(ia, &IN_IFADDR_HASH((addr).s_addr), ia_hash) { \
    152 		if (in_hosteq(ia->ia_addr.sin_addr, (addr))) \
    153 			break; \
    154 	} \
    155 }
    156 
    157 /*
    158  * Macro for finding the next in_ifaddr structure with the same internet
    159  * address as ia. Call only with a valid ia pointer.
    160  * Will set ia to NULL if none found.
    161  */
    162 
    163 #define NEXT_IA_WITH_SAME_ADDR(ia) \
    164 	/* struct in_ifaddr *ia; */ \
    165 { \
    166 	struct in_addr addr; \
    167 	addr = ia->ia_addr.sin_addr; \
    168 	do { \
    169 		ia = LIST_NEXT(ia, ia_hash); \
    170 	} while ((ia != NULL) && !in_hosteq(ia->ia_addr.sin_addr, addr)); \
    171 }
    172 
    173 /*
    174  * Macro for finding the interface (ifnet structure) corresponding to one
    175  * of our IP addresses.
    176  */
    177 #define INADDR_TO_IFP(addr, ifp) \
    178 	/* struct in_addr addr; */ \
    179 	/* struct ifnet *ifp; */ \
    180 { \
    181 	struct in_ifaddr *ia; \
    182 \
    183 	INADDR_TO_IA(addr, ia); \
    184 	(ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \
    185 }
    186 
    187 /*
    188  * Macro for finding an internet address structure (in_ifaddr) corresponding
    189  * to a given interface (ifnet structure).
    190  */
    191 #define IFP_TO_IA(ifp, ia) \
    192 	/* struct ifnet *ifp; */ \
    193 	/* struct in_ifaddr *ia; */ \
    194 { \
    195 	struct ifaddr *ifa; \
    196 \
    197 	IFADDR_FOREACH(ifa, ifp) { \
    198 		if (ifa->ifa_addr->sa_family == AF_INET) \
    199 			break; \
    200 	} \
    201 	(ia) = ifatoia(ifa); \
    202 }
    203 #endif
    204 
    205 /*
    206  * Per-interface router version information.
    207  */
    208 struct router_info {
    209 	LIST_ENTRY(router_info) rti_link;
    210 	struct	ifnet *rti_ifp;
    211 	int	rti_type;	/* type of router on this interface */
    212 	int	rti_age;	/* time since last v1 query */
    213 };
    214 
    215 /*
    216  * Internet multicast address structure.  There is one of these for each IP
    217  * multicast group to which this host belongs on a given network interface.
    218  * They are kept in a linked list, rooted in the interface's in_ifaddr
    219  * structure.
    220  */
    221 struct in_multi {
    222 	LIST_ENTRY(in_multi) inm_list;	/* list of multicast addresses */
    223 	struct	router_info *inm_rti;	/* router version info */
    224 	struct	ifnet *inm_ifp;		/* back pointer to ifnet */
    225 	struct	in_addr inm_addr;	/* IP multicast address */
    226 	u_int	inm_refcount;		/* no. membership claims by sockets */
    227 	u_int	inm_timer;		/* IGMP membership report timer */
    228 	u_int	inm_state;		/* state of membership */
    229 };
    230 
    231 #ifdef _KERNEL
    232 /*
    233  * Structure used by macros below to remember position when stepping through
    234  * all of the in_multi records.
    235  */
    236 struct in_multistep {
    237 	int i_n;
    238 	struct in_multi *i_inm;
    239 };
    240 
    241 /*
    242  * Macro for looking up the in_multi record for a given IP multicast address
    243  * on a given interface.  If no matching record is found, "inm" returns NULL.
    244  */
    245 #define IN_LOOKUP_MULTI(addr, ifp, inm) \
    246 	/* struct in_addr addr; */ \
    247 	/* struct ifnet *ifp; */ \
    248 	/* struct in_multi *inm; */ \
    249 { \
    250 	LIST_FOREACH((inm), &IN_MULTI_HASH(((addr).s_addr), (ifp)), inm_list) {\
    251 		if (in_hosteq((inm)->inm_addr, (addr)) && \
    252 		    (inm)->inm_ifp == (ifp)) \
    253 			break; \
    254 	} \
    255 }
    256 
    257 /*
    258  * Macro to step through all of the in_multi records, one at a time.
    259  * The current position is remembered in "step", which the caller must
    260  * provide.  IN_FIRST_MULTI(), below, must be called to initialize "step"
    261  * and get the first record.  Both macros return a NULL "inm" when there
    262  * are no remaining records.
    263  */
    264 #define IN_NEXT_MULTI(step, inm) \
    265 	/* struct in_multistep  step; */ \
    266 	/* struct in_multi *inm; */ \
    267 { \
    268 	while ((step).i_inm == NULL && (step).i_n < IN_MULTI_HASH_SIZE) \
    269 		(step).i_inm = LIST_FIRST(&in_multihashtbl[++(step).i_n]); \
    270 	if (((inm) = (step).i_inm) != NULL) \
    271 		(step).i_inm = LIST_NEXT((inm), inm_list); \
    272 }
    273 
    274 #define IN_FIRST_MULTI(step, inm) \
    275 	/* struct in_multistep step; */ \
    276 	/* struct in_multi *inm; */ \
    277 { \
    278 	(step).i_n = 0; \
    279 	(step).i_inm = LIST_FIRST(&in_multihashtbl[0]); \
    280 	IN_NEXT_MULTI((step), (inm)); \
    281 }
    282 
    283 struct ifaddr;
    284 
    285 int	in_ifinit(struct ifnet *,
    286 	    struct in_ifaddr *, const struct sockaddr_in *, int);
    287 void	in_savemkludge(struct in_ifaddr *);
    288 void	in_restoremkludge(struct in_ifaddr *, struct ifnet *);
    289 void	in_purgemkludge(struct ifnet *);
    290 struct	in_multi *in_addmulti(struct in_addr *, struct ifnet *);
    291 void	in_delmulti(struct in_multi *);
    292 void	in_ifscrub(struct ifnet *, struct in_ifaddr *);
    293 void	in_setmaxmtu(void);
    294 const char *in_fmtaddr(struct in_addr);
    295 int	in_control(struct socket *, u_long, void *, struct ifnet *,
    296 	    struct lwp *);
    297 void	in_purgeaddr(struct ifaddr *);
    298 void	in_purgeif(struct ifnet *);
    299 int	ipflow_fastforward(struct mbuf *);
    300 
    301 struct ipid_state;
    302 typedef struct ipid_state ipid_state_t;
    303 
    304 ipid_state_t *	ip_id_init(void);
    305 void		ip_id_fini(ipid_state_t *);
    306 uint16_t	ip_randomid(ipid_state_t *, uint16_t);
    307 
    308 extern ipid_state_t *	ip_ids;
    309 extern uint16_t		ip_id;
    310 extern int		ip_do_randomid;
    311 
    312 /*
    313  * ip_newid_range: "allocate" num contiguous IP IDs.
    314  *
    315  * => Return the first ID.
    316  */
    317 static __inline uint16_t
    318 ip_newid_range(const struct in_ifaddr *ia, u_int num)
    319 {
    320 	uint16_t id;
    321 
    322 	if (ip_do_randomid) {
    323 		/* XXX ignore num */
    324 		return ip_randomid(ip_ids, ia ? ia->ia_idsalt : 0);
    325 	}
    326 
    327 	/* Never allow an IP ID of 0 (detect wrap). */
    328 	if ((uint16_t)(ip_id + num) < ip_id) {
    329 		ip_id = 1;
    330 	}
    331 	id = htons(ip_id);
    332 	ip_id += num;
    333 	return id;
    334 }
    335 
    336 static __inline uint16_t
    337 ip_newid(const struct in_ifaddr *ia)
    338 {
    339 
    340 	return ip_newid_range(ia, 1);
    341 }
    342 
    343 #ifdef SYSCTLFN_PROTO
    344 int	sysctl_inpcblist(SYSCTLFN_PROTO);
    345 #endif
    346 
    347 #endif	/* !_KERNEL */
    348 
    349 /* INET6 stuff */
    350 #include <netinet6/in6_var.h>
    351 
    352 #endif /* !_NETINET_IN_VAR_H_ */
    353