in_var.h revision 167729
1139823Simp/*-
21541Srgrimes * Copyright (c) 1985, 1986, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
2910939Swollman *	@(#)in_var.h	8.2 (Berkeley) 1/9/95
3050477Speter * $FreeBSD: head/sys/netinet/in_var.h 167729 2007-03-20 00:36:10Z bms $
311541Srgrimes */
321541Srgrimes
332169Spaul#ifndef _NETINET_IN_VAR_H_
342169Spaul#define _NETINET_IN_VAR_H_
352169Spaul
367280Swollman#include <sys/queue.h>
3784102Sjlemon#include <sys/fnv_hash.h>
387280Swollman
391541Srgrimes/*
401541Srgrimes * Interface address, Internet version.  One of these structures
4121666Swollman * is allocated for each Internet address on an interface.
421541Srgrimes * The ifaddr structure contains the protocol-independent part
431541Srgrimes * of the structure and is assumed to be first.
441541Srgrimes */
451541Srgrimesstruct in_ifaddr {
461541Srgrimes	struct	ifaddr ia_ifa;		/* protocol-independent info */
471541Srgrimes#define	ia_ifp		ia_ifa.ifa_ifp
481541Srgrimes#define ia_flags	ia_ifa.ifa_flags
491541Srgrimes					/* ia_{,sub}net{,mask} in host order */
501541Srgrimes	u_long	ia_net;			/* network number of interface */
511541Srgrimes	u_long	ia_netmask;		/* mask of net part */
521541Srgrimes	u_long	ia_subnet;		/* subnet number, including net */
531541Srgrimes	u_long	ia_subnetmask;		/* mask of subnet part */
541541Srgrimes	struct	in_addr ia_netbroadcast; /* to recognize net broadcasts */
5584102Sjlemon	LIST_ENTRY(in_ifaddr) ia_hash;	/* entry in bucket of inet addresses */
5684102Sjlemon	TAILQ_ENTRY(in_ifaddr) ia_link;	/* list of internet addresses */
571541Srgrimes	struct	sockaddr_in ia_addr;	/* reserve space for interface name */
581541Srgrimes	struct	sockaddr_in ia_dstaddr; /* reserve space for broadcast addr */
591541Srgrimes#define	ia_broadaddr	ia_dstaddr
601541Srgrimes	struct	sockaddr_in ia_sockmask; /* reserve space for general netmask */
611541Srgrimes};
621541Srgrimes
631541Srgrimesstruct	in_aliasreq {
641541Srgrimes	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
651541Srgrimes	struct	sockaddr_in ifra_addr;
661541Srgrimes	struct	sockaddr_in ifra_broadaddr;
671541Srgrimes#define ifra_dstaddr ifra_broadaddr
681541Srgrimes	struct	sockaddr_in ifra_mask;
691541Srgrimes};
701541Srgrimes/*
711541Srgrimes * Given a pointer to an in_ifaddr (ifaddr),
721541Srgrimes * return a pointer to the addr as a sockaddr_in.
731541Srgrimes */
743865Sswallace#define IA_SIN(ia)    (&(((struct in_ifaddr *)(ia))->ia_addr))
753865Sswallace#define IA_DSTSIN(ia) (&(((struct in_ifaddr *)(ia))->ia_dstaddr))
761541Srgrimes
771541Srgrimes#define IN_LNAOF(in, ifa) \
781541Srgrimes	((ntohl((in).s_addr) & ~((struct in_ifaddr *)(ifa)->ia_subnetmask))
791541Srgrimes
808876Srgrimes
8155205Speter#ifdef	_KERNEL
827090Sbdeextern	u_char	inetctlerrmap[];
831541Srgrimes
84133874Srwatson/*
8584102Sjlemon * Hash table for IP addresses.
8684102Sjlemon */
8784102Sjlemonextern	LIST_HEAD(in_ifaddrhashhead, in_ifaddr) *in_ifaddrhashtbl;
8884102Sjlemonextern	TAILQ_HEAD(in_ifaddrhead, in_ifaddr) in_ifaddrhead;
8984102Sjlemonextern	u_long in_ifaddrhmask;			/* mask for hash table */
9084102Sjlemon
9184102Sjlemon#define INADDR_NHASH_LOG2       9
9284102Sjlemon#define INADDR_NHASH		(1 << INADDR_NHASH_LOG2)
9384102Sjlemon#define INADDR_HASHVAL(x)	fnv_32_buf((&(x)), sizeof(x), FNV1_32_INIT)
9484102Sjlemon#define INADDR_HASH(x) \
9584102Sjlemon	(&in_ifaddrhashtbl[INADDR_HASHVAL(x) & in_ifaddrhmask])
9684102Sjlemon
97162615Sbms/*
98162625Sbms * Macro for finding the internet address structure (in_ifaddr)
99162615Sbms * corresponding to one of our IP addresses (in_addr).
100162615Sbms */
101162615Sbms#define INADDR_TO_IFADDR(addr, ia) \
102162615Sbms	/* struct in_addr addr; */ \
103162615Sbms	/* struct in_ifaddr *ia; */ \
104162615Sbmsdo { \
105162615Sbms\
106162615Sbms	LIST_FOREACH(ia, INADDR_HASH((addr).s_addr), ia_hash) \
107162615Sbms		if (IA_SIN(ia)->sin_addr.s_addr == (addr).s_addr) \
108162615Sbms			break; \
109162615Sbms} while (0)
11084102Sjlemon
1111541Srgrimes/*
1121541Srgrimes * Macro for finding the interface (ifnet structure) corresponding to one
1131541Srgrimes * of our IP addresses.
1141541Srgrimes */
1151541Srgrimes#define INADDR_TO_IFP(addr, ifp) \
1161541Srgrimes	/* struct in_addr addr; */ \
1171541Srgrimes	/* struct ifnet *ifp; */ \
1181541Srgrimes{ \
11979821Sru	struct in_ifaddr *ia; \
1201541Srgrimes\
121162615Sbms	INADDR_TO_IFADDR(addr, ia); \
1221541Srgrimes	(ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \
1231541Srgrimes}
1241541Srgrimes
1251541Srgrimes/*
1261541Srgrimes * Macro for finding the internet address structure (in_ifaddr) corresponding
1271541Srgrimes * to a given interface (ifnet structure).
1281541Srgrimes */
1291541Srgrimes#define IFP_TO_IA(ifp, ia) \
1301541Srgrimes	/* struct ifnet *ifp; */ \
1311541Srgrimes	/* struct in_ifaddr *ia; */ \
1321541Srgrimes{ \
13371998Sphk	for ((ia) = TAILQ_FIRST(&in_ifaddrhead); \
1341541Srgrimes	    (ia) != NULL && (ia)->ia_ifp != (ifp); \
13571998Sphk	    (ia) = TAILQ_NEXT((ia), ia_link)) \
1361541Srgrimes		continue; \
1371541Srgrimes}
1381541Srgrimes#endif
1391541Srgrimes
1401541Srgrimes/*
1412531Swollman * This information should be part of the ifnet structure but we don't wish
1422531Swollman * to change that - as it might break a number of things
1432531Swollman */
1442531Swollman
1452531Swollmanstruct router_info {
14614622Sfenner	struct ifnet *rti_ifp;
14714622Sfenner	int    rti_type; /* type of router which is querier on this interface */
14814622Sfenner	int    rti_time; /* # of slow timeouts since last old query */
149119180Srwatson	SLIST_ENTRY(router_info) rti_list;
1502531Swollman};
1512531Swollman
1522531Swollman/*
1531541Srgrimes * Internet multicast address structure.  There is one of these for each IP
1541541Srgrimes * multicast group to which this host belongs on a given network interface.
15521666Swollman * For every entry on the interface's if_multiaddrs list which represents
15621666Swollman * an IP multicast group, there is one of these structures.  They are also
15721666Swollman * kept on a system-wide list to make it easier to keep our legacy IGMP code
15821666Swollman * compatible with the rest of the world (see IN_FIRST_MULTI et al, below).
1591541Srgrimes */
1601541Srgrimesstruct in_multi {
16160938Sjake	LIST_ENTRY(in_multi) inm_link;	/* queue macro glue */
16221666Swollman	struct	in_addr inm_addr;	/* IP multicast address, convenience */
1631541Srgrimes	struct	ifnet *inm_ifp;		/* back pointer to ifnet */
16421666Swollman	struct	ifmultiaddr *inm_ifma;	/* back pointer to ifmultiaddr */
1651541Srgrimes	u_int	inm_timer;		/* IGMP membership report timer */
1662531Swollman	u_int	inm_state;		/*  state of the membership */
1672531Swollman	struct	router_info *inm_rti;	/* router info*/
168167729Sbms	u_int	inm_refcount;		/* reference count */
1691541Srgrimes};
1701541Srgrimes
17155205Speter#ifdef _KERNEL
17244078Sdfr
17344078Sdfr#ifdef SYSCTL_DECL
174136713SandreSYSCTL_DECL(_net_inet);
17544078SdfrSYSCTL_DECL(_net_inet_ip);
17644078SdfrSYSCTL_DECL(_net_inet_raw);
17744078Sdfr#endif
17844078Sdfr
17960938Sjakeextern LIST_HEAD(in_multihead, in_multi) in_multihead;
18021666Swollman
1811541Srgrimes/*
182148682Srwatson * Lock macros for IPv4 layer multicast address lists.  IPv4 lock goes
183148682Srwatson * before link layer multicast locks in the lock order.  In most cases,
184148682Srwatson * consumers of IN_*_MULTI() macros should acquire the locks before
185148682Srwatson * calling them; users of the in_{add,del}multi() functions should not.
186148682Srwatson */
187148682Srwatsonextern struct mtx in_multi_mtx;
188148682Srwatson#define	IN_MULTI_LOCK()		mtx_lock(&in_multi_mtx)
189148682Srwatson#define	IN_MULTI_UNLOCK()	mtx_unlock(&in_multi_mtx)
190148682Srwatson#define	IN_MULTI_LOCK_ASSERT()	mtx_assert(&in_multi_mtx, MA_OWNED)
191148682Srwatson
192148682Srwatson/*
1931541Srgrimes * Structure used by macros below to remember position when stepping through
1941541Srgrimes * all of the in_multi records.
1951541Srgrimes */
1961541Srgrimesstruct in_multistep {
1971541Srgrimes	struct in_multi *i_inm;
1981541Srgrimes};
1991541Srgrimes
2001541Srgrimes/*
2011541Srgrimes * Macro for looking up the in_multi record for a given IP multicast address
20221666Swollman * on a given interface.  If no matching record is found, "inm" is set null.
2031541Srgrimes */
2041541Srgrimes#define IN_LOOKUP_MULTI(addr, ifp, inm) \
2051541Srgrimes	/* struct in_addr addr; */ \
2061541Srgrimes	/* struct ifnet *ifp; */ \
2071541Srgrimes	/* struct in_multi *inm; */ \
20820407Swollmando { \
20979821Sru	struct ifmultiaddr *ifma; \
2101541Srgrimes\
211148682Srwatson	IN_MULTI_LOCK_ASSERT(); \
212148653Srwatson	IF_ADDR_LOCK(ifp); \
21372084Sphk	TAILQ_FOREACH(ifma, &((ifp)->if_multiaddrs), ifma_link) { \
21421666Swollman		if (ifma->ifma_addr->sa_family == AF_INET \
21521929Swollman		    && ((struct sockaddr_in *)ifma->ifma_addr)->sin_addr.s_addr == \
21621666Swollman		    (addr).s_addr) \
21721666Swollman			break; \
21821666Swollman	} \
21921666Swollman	(inm) = ifma ? ifma->ifma_protospec : 0; \
220148653Srwatson	IF_ADDR_UNLOCK(ifp); \
22120407Swollman} while(0)
2221541Srgrimes
2231541Srgrimes/*
2241541Srgrimes * Macro to step through all of the in_multi records, one at a time.
2251541Srgrimes * The current position is remembered in "step", which the caller must
2261541Srgrimes * provide.  IN_FIRST_MULTI(), below, must be called to initialize "step"
2271541Srgrimes * and get the first record.  Both macros return a NULL "inm" when there
2281541Srgrimes * are no remaining records.
2291541Srgrimes */
2301541Srgrimes#define IN_NEXT_MULTI(step, inm) \
2311541Srgrimes	/* struct in_multistep  step; */ \
2321541Srgrimes	/* struct in_multi *inm; */ \
23320407Swollmando { \
234148682Srwatson	IN_MULTI_LOCK_ASSERT(); \
2351541Srgrimes	if (((inm) = (step).i_inm) != NULL) \
23671998Sphk		(step).i_inm = LIST_NEXT((step).i_inm, inm_link); \
23720407Swollman} while(0)
2381541Srgrimes
2391541Srgrimes#define IN_FIRST_MULTI(step, inm) \
2401541Srgrimes	/* struct in_multistep step; */ \
2411541Srgrimes	/* struct in_multi *inm; */ \
24220407Swollmando { \
243148682Srwatson	IN_MULTI_LOCK_ASSERT(); \
24471998Sphk	(step).i_inm = LIST_FIRST(&in_multihead); \
2451541Srgrimes	IN_NEXT_MULTI((step), (inm)); \
24620407Swollman} while(0)
2471541Srgrimes
24836192Sdgstruct	route;
24992723Salfredstruct	in_multi *in_addmulti(struct in_addr *, struct ifnet *);
25092723Salfredvoid	in_delmulti(struct in_multi *);
251162718Sbmsvoid	in_delmulti_locked(struct in_multi *);
25292723Salfredint	in_control(struct socket *, u_long, caddr_t, struct ifnet *,
25393085Sbde	    struct thread *);
25492723Salfredvoid	in_rtqdrain(void);
25592723Salfredvoid	ip_input(struct mbuf *);
25692723Salfredint	in_ifadown(struct ifaddr *ifa, int);
25792723Salfredvoid	in_ifscrub(struct ifnet *, struct in_ifaddr *);
258154518Sandrestruct	mbuf	*ip_fastforward(struct mbuf *);
2592169Spaul
26055205Speter#endif /* _KERNEL */
26110939Swollman
26252904Sshin/* INET6 stuff */
26352904Sshin#include <netinet6/in6_var.h>
26452904Sshin
26510939Swollman#endif /* _NETINET_IN_VAR_H_ */
266