in_var.h revision 84102
1238106Sdes/*
2238106Sdes * Copyright (c) 1985, 1986, 1993
3238106Sdes *	The Regents of the University of California.  All rights reserved.
4296415Sdes *
5238106Sdes * Redistribution and use in source and binary forms, with or without
6238106Sdes * modification, are permitted provided that the following conditions
7238106Sdes * are met:
8238106Sdes * 1. Redistributions of source code must retain the above copyright
9238106Sdes *    notice, this list of conditions and the following disclaimer.
10238106Sdes * 2. Redistributions in binary form must reproduce the above copyright
11238106Sdes *    notice, this list of conditions and the following disclaimer in the
12238106Sdes *    documentation and/or other materials provided with the distribution.
13238106Sdes * 3. All advertising materials mentioning features or use of this software
14238106Sdes *    must display the following acknowledgement:
15238106Sdes *	This product includes software developed by the University of
16238106Sdes *	California, Berkeley and its contributors.
17238106Sdes * 4. Neither the name of the University nor the names of its contributors
18238106Sdes *    may be used to endorse or promote products derived from this software
19238106Sdes *    without specific prior written permission.
20238106Sdes *
21238106Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22238106Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23238106Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24238106Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25238106Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26238106Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27238106Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28238106Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29238106Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30238106Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31238106Sdes * SUCH DAMAGE.
32238106Sdes *
33238106Sdes *	@(#)in_var.h	8.2 (Berkeley) 1/9/95
34238106Sdes * $FreeBSD: head/sys/netinet/in_var.h 84102 2001-09-29 04:34:11Z jlemon $
35238106Sdes */
36238106Sdes
37238106Sdes#ifndef _NETINET_IN_VAR_H_
38249141Sdes#define _NETINET_IN_VAR_H_
39238106Sdes
40238106Sdes#include <sys/queue.h>
41238106Sdes#include <sys/fnv_hash.h>
42238106Sdes
43238106Sdes/*
44296415Sdes * Interface address, Internet version.  One of these structures
45238106Sdes * is allocated for each Internet address on an interface.
46238106Sdes * The ifaddr structure contains the protocol-independent part
47296415Sdes * of the structure and is assumed to be first.
48238106Sdes */
49296415Sdesstruct in_ifaddr {
50238106Sdes	struct	ifaddr ia_ifa;		/* protocol-independent info */
51238106Sdes#define	ia_ifp		ia_ifa.ifa_ifp
52296415Sdes#define ia_flags	ia_ifa.ifa_flags
53238106Sdes					/* ia_{,sub}net{,mask} in host order */
54238106Sdes	u_long	ia_net;			/* network number of interface */
55238106Sdes	u_long	ia_netmask;		/* mask of net part */
56238106Sdes	u_long	ia_subnet;		/* subnet number, including net */
57238106Sdes	u_long	ia_subnetmask;		/* mask of subnet part */
58238106Sdes	struct	in_addr ia_netbroadcast; /* to recognize net broadcasts */
59238106Sdes	LIST_ENTRY(in_ifaddr) ia_hash;	/* entry in bucket of inet addresses */
60238106Sdes	TAILQ_ENTRY(in_ifaddr) ia_link;	/* list of internet addresses */
61238106Sdes	struct	sockaddr_in ia_addr;	/* reserve space for interface name */
62238106Sdes	struct	sockaddr_in ia_dstaddr; /* reserve space for broadcast addr */
63238106Sdes#define	ia_broadaddr	ia_dstaddr
64238106Sdes	struct	sockaddr_in ia_sockmask; /* reserve space for general netmask */
65238106Sdes};
66238106Sdes
67238106Sdesstruct	in_aliasreq {
68238106Sdes	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
69238106Sdes	struct	sockaddr_in ifra_addr;
70238106Sdes	struct	sockaddr_in ifra_broadaddr;
71238106Sdes#define ifra_dstaddr ifra_broadaddr
72238106Sdes	struct	sockaddr_in ifra_mask;
73238106Sdes};
74238106Sdes/*
75238106Sdes * Given a pointer to an in_ifaddr (ifaddr),
76238106Sdes * return a pointer to the addr as a sockaddr_in.
77238106Sdes */
78238106Sdes#define IA_SIN(ia)    (&(((struct in_ifaddr *)(ia))->ia_addr))
79238106Sdes#define IA_DSTSIN(ia) (&(((struct in_ifaddr *)(ia))->ia_dstaddr))
80238106Sdes
81238106Sdes#define IN_LNAOF(in, ifa) \
82238106Sdes	((ntohl((in).s_addr) & ~((struct in_ifaddr *)(ifa)->ia_subnetmask))
83238106Sdes
84238106Sdes
85296415Sdes#ifdef	_KERNEL
86238106Sdesextern	struct	ifqueue	ipintrq;		/* ip packet input queue */
87238106Sdesextern	struct	in_addr zeroin_addr;
88238106Sdesextern	u_char	inetctlerrmap[];
89238106Sdes
90238106Sdes/*
91238106Sdes * Hash table for IP addresses.
92238106Sdes */
93238106Sdesextern	LIST_HEAD(in_ifaddrhashhead, in_ifaddr) *in_ifaddrhashtbl;
94238106Sdesextern	TAILQ_HEAD(in_ifaddrhead, in_ifaddr) in_ifaddrhead;
95238106Sdesextern	u_long in_ifaddrhmask;			/* mask for hash table */
96238106Sdes
97238106Sdes#define INADDR_NHASH_LOG2       9
98238106Sdes#define INADDR_NHASH		(1 << INADDR_NHASH_LOG2)
99238106Sdes#define INADDR_HMASK		(INREASS_NHASH - 1)
100238106Sdes#define INADDR_HASHVAL(x)	fnv_32_buf((&(x)), sizeof(x), FNV1_32_INIT)
101238106Sdes#define INADDR_HASH(x) \
102238106Sdes	(&in_ifaddrhashtbl[INADDR_HASHVAL(x) & in_ifaddrhmask])
103238106Sdes
104238106Sdes
105238106Sdes/*
106238106Sdes * Macro for finding the interface (ifnet structure) corresponding to one
107238106Sdes * of our IP addresses.
108238106Sdes */
109238106Sdes#define INADDR_TO_IFP(addr, ifp) \
110238106Sdes	/* struct in_addr addr; */ \
111238106Sdes	/* struct ifnet *ifp; */ \
112238106Sdes{ \
113238106Sdes	struct in_ifaddr *ia; \
114238106Sdes\
115238106Sdes	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) \
116238106Sdes		if (IA_SIN(ia)->sin_addr.s_addr == (addr).s_addr) \
117238106Sdes			break; \
118238106Sdes	(ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \
119238106Sdes}
120238106Sdes
121238106Sdes/*
122238106Sdes * Macro for finding the internet address structure (in_ifaddr) corresponding
123238106Sdes * to a given interface (ifnet structure).
124238106Sdes */
125296415Sdes#define IFP_TO_IA(ifp, ia) \
126238106Sdes	/* struct ifnet *ifp; */ \
127238106Sdes	/* struct in_ifaddr *ia; */ \
128238106Sdes{ \
129238106Sdes	for ((ia) = TAILQ_FIRST(&in_ifaddrhead); \
130296415Sdes	    (ia) != NULL && (ia)->ia_ifp != (ifp); \
131296415Sdes	    (ia) = TAILQ_NEXT((ia), ia_link)) \
132296415Sdes		continue; \
133296415Sdes}
134296415Sdes#endif
135296415Sdes
136238106Sdes/*
137238106Sdes * This information should be part of the ifnet structure but we don't wish
138296415Sdes * to change that - as it might break a number of things
139238106Sdes */
140238106Sdes
141238106Sdesstruct router_info {
142296415Sdes	struct ifnet *rti_ifp;
143296415Sdes	int    rti_type; /* type of router which is querier on this interface */
144296415Sdes	int    rti_time; /* # of slow timeouts since last old query */
145296415Sdes	struct router_info *rti_next;
146296415Sdes};
147296415Sdes
148296415Sdes/*
149296415Sdes * Internet multicast address structure.  There is one of these for each IP
150238106Sdes * multicast group to which this host belongs on a given network interface.
151296415Sdes * For every entry on the interface's if_multiaddrs list which represents
152238106Sdes * an IP multicast group, there is one of these structures.  They are also
153238106Sdes * kept on a system-wide list to make it easier to keep our legacy IGMP code
154238106Sdes * compatible with the rest of the world (see IN_FIRST_MULTI et al, below).
155296415Sdes */
156296415Sdesstruct in_multi {
157238106Sdes	LIST_ENTRY(in_multi) inm_link;	/* queue macro glue */
158296415Sdes	struct	in_addr inm_addr;	/* IP multicast address, convenience */
159296415Sdes	struct	ifnet *inm_ifp;		/* back pointer to ifnet */
160238106Sdes	struct	ifmultiaddr *inm_ifma;	/* back pointer to ifmultiaddr */
161238106Sdes	u_int	inm_timer;		/* IGMP membership report timer */
162238106Sdes	u_int	inm_state;		/*  state of the membership */
163238106Sdes	struct	router_info *inm_rti;	/* router info*/
164238106Sdes};
165238106Sdes
166296415Sdes#ifdef _KERNEL
167296415Sdes
168296415Sdes#ifdef SYSCTL_DECL
169296415SdesSYSCTL_DECL(_net_inet_ip);
170296415SdesSYSCTL_DECL(_net_inet_raw);
171296415Sdes#endif
172296415Sdes
173296415Sdesextern LIST_HEAD(in_multihead, in_multi) in_multihead;
174296415Sdes
175296415Sdes/*
176238106Sdes * Structure used by macros below to remember position when stepping through
177238106Sdes * all of the in_multi records.
178238106Sdes */
179238106Sdesstruct in_multistep {
180238106Sdes	struct in_multi *i_inm;
181238106Sdes};
182238106Sdes
183238106Sdes/*
184238106Sdes * Macro for looking up the in_multi record for a given IP multicast address
185238106Sdes * on a given interface.  If no matching record is found, "inm" is set null.
186238106Sdes */
187238106Sdes#define IN_LOOKUP_MULTI(addr, ifp, inm) \
188238106Sdes	/* struct in_addr addr; */ \
189249141Sdes	/* struct ifnet *ifp; */ \
190249141Sdes	/* struct in_multi *inm; */ \
191249141Sdesdo { \
192249141Sdes	struct ifmultiaddr *ifma; \
193238106Sdes\
194238106Sdes	TAILQ_FOREACH(ifma, &((ifp)->if_multiaddrs), ifma_link) { \
195238106Sdes		if (ifma->ifma_addr->sa_family == AF_INET \
196238106Sdes		    && ((struct sockaddr_in *)ifma->ifma_addr)->sin_addr.s_addr == \
197238106Sdes		    (addr).s_addr) \
198238106Sdes			break; \
199238106Sdes	} \
200238106Sdes	(inm) = ifma ? ifma->ifma_protospec : 0; \
201249141Sdes} while(0)
202238106Sdes
203238106Sdes/*
204238106Sdes * Macro to step through all of the in_multi records, one at a time.
205238106Sdes * The current position is remembered in "step", which the caller must
206238106Sdes * provide.  IN_FIRST_MULTI(), below, must be called to initialize "step"
207296415Sdes * and get the first record.  Both macros return a NULL "inm" when there
208296415Sdes * are no remaining records.
209296415Sdes */
210296415Sdes#define IN_NEXT_MULTI(step, inm) \
211296415Sdes	/* struct in_multistep  step; */ \
212296415Sdes	/* struct in_multi *inm; */ \
213296415Sdesdo { \
214296415Sdes	if (((inm) = (step).i_inm) != NULL) \
215296415Sdes		(step).i_inm = LIST_NEXT((step).i_inm, inm_link); \
216249141Sdes} while(0)
217249141Sdes
218249141Sdes#define IN_FIRST_MULTI(step, inm) \
219249141Sdes	/* struct in_multistep step; */ \
220249141Sdes	/* struct in_multi *inm; */ \
221238106Sdesdo { \
222238106Sdes	(step).i_inm = LIST_FIRST(&in_multihead); \
223238106Sdes	IN_NEXT_MULTI((step), (inm)); \
224238106Sdes} while(0)
225238106Sdes
226238106Sdesstruct	route;
227238106Sdesstruct	in_multi *in_addmulti __P((struct in_addr *, struct ifnet *));
228238106Sdesvoid	in_delmulti __P((struct in_multi *));
229238106Sdesint	in_control __P((struct socket *, u_long, caddr_t, struct ifnet *,
230238106Sdes			struct thread *));
231296415Sdesvoid	in_rtqdrain __P((void));
232238106Sdesvoid	ip_input __P((struct mbuf *));
233296415Sdesint	in_ifadown __P((struct ifaddr *ifa, int));
234238106Sdesvoid	in_ifscrub __P((struct ifnet *, struct in_ifaddr *));
235238106Sdesint	ipflow_fastforward __P((struct mbuf *));
236238106Sdesvoid	ipflow_create __P((const struct route *, struct mbuf *));
237238106Sdesvoid	ipflow_slowtimo __P((void));
238296415Sdes
239238106Sdes#endif /* _KERNEL */
240296415Sdes
241238106Sdes/* INET6 stuff */
242238106Sdes#include <netinet6/in6_var.h>
243238106Sdes
244238106Sdes#endif /* _NETINET_IN_VAR_H_ */
245238106Sdes