in_var.h revision 139823
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 139823 2005-01-07 01:45:51Z imp $
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
9784102Sjlemon
981541Srgrimes/*
991541Srgrimes * Macro for finding the interface (ifnet structure) corresponding to one
1001541Srgrimes * of our IP addresses.
1011541Srgrimes */
1021541Srgrimes#define INADDR_TO_IFP(addr, ifp) \
1031541Srgrimes	/* struct in_addr addr; */ \
1041541Srgrimes	/* struct ifnet *ifp; */ \
1051541Srgrimes{ \
10679821Sru	struct in_ifaddr *ia; \
1071541Srgrimes\
10884109Sjlemon	LIST_FOREACH(ia, INADDR_HASH((addr).s_addr), ia_hash) \
10979821Sru		if (IA_SIN(ia)->sin_addr.s_addr == (addr).s_addr) \
11079821Sru			break; \
1111541Srgrimes	(ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \
1121541Srgrimes}
1131541Srgrimes
1141541Srgrimes/*
1151541Srgrimes * Macro for finding the internet address structure (in_ifaddr) corresponding
1161541Srgrimes * to a given interface (ifnet structure).
1171541Srgrimes */
1181541Srgrimes#define IFP_TO_IA(ifp, ia) \
1191541Srgrimes	/* struct ifnet *ifp; */ \
1201541Srgrimes	/* struct in_ifaddr *ia; */ \
1211541Srgrimes{ \
12271998Sphk	for ((ia) = TAILQ_FIRST(&in_ifaddrhead); \
1231541Srgrimes	    (ia) != NULL && (ia)->ia_ifp != (ifp); \
12471998Sphk	    (ia) = TAILQ_NEXT((ia), ia_link)) \
1251541Srgrimes		continue; \
1261541Srgrimes}
1271541Srgrimes#endif
1281541Srgrimes
1291541Srgrimes/*
1302531Swollman * This information should be part of the ifnet structure but we don't wish
1312531Swollman * to change that - as it might break a number of things
1322531Swollman */
1332531Swollman
1342531Swollmanstruct router_info {
13514622Sfenner	struct ifnet *rti_ifp;
13614622Sfenner	int    rti_type; /* type of router which is querier on this interface */
13714622Sfenner	int    rti_time; /* # of slow timeouts since last old query */
138119180Srwatson	SLIST_ENTRY(router_info) rti_list;
1392531Swollman};
1402531Swollman
1412531Swollman/*
1421541Srgrimes * Internet multicast address structure.  There is one of these for each IP
1431541Srgrimes * multicast group to which this host belongs on a given network interface.
14421666Swollman * For every entry on the interface's if_multiaddrs list which represents
14521666Swollman * an IP multicast group, there is one of these structures.  They are also
14621666Swollman * kept on a system-wide list to make it easier to keep our legacy IGMP code
14721666Swollman * compatible with the rest of the world (see IN_FIRST_MULTI et al, below).
1481541Srgrimes */
1491541Srgrimesstruct in_multi {
15060938Sjake	LIST_ENTRY(in_multi) inm_link;	/* queue macro glue */
15121666Swollman	struct	in_addr inm_addr;	/* IP multicast address, convenience */
1521541Srgrimes	struct	ifnet *inm_ifp;		/* back pointer to ifnet */
15321666Swollman	struct	ifmultiaddr *inm_ifma;	/* back pointer to ifmultiaddr */
1541541Srgrimes	u_int	inm_timer;		/* IGMP membership report timer */
1552531Swollman	u_int	inm_state;		/*  state of the membership */
1562531Swollman	struct	router_info *inm_rti;	/* router info*/
1571541Srgrimes};
1581541Srgrimes
15955205Speter#ifdef _KERNEL
16044078Sdfr
16144078Sdfr#ifdef SYSCTL_DECL
162136713SandreSYSCTL_DECL(_net_inet);
16344078SdfrSYSCTL_DECL(_net_inet_ip);
16444078SdfrSYSCTL_DECL(_net_inet_raw);
16544078Sdfr#endif
16644078Sdfr
16760938Sjakeextern LIST_HEAD(in_multihead, in_multi) in_multihead;
16821666Swollman
1691541Srgrimes/*
1701541Srgrimes * Structure used by macros below to remember position when stepping through
1711541Srgrimes * all of the in_multi records.
1721541Srgrimes */
1731541Srgrimesstruct in_multistep {
1741541Srgrimes	struct in_multi *i_inm;
1751541Srgrimes};
1761541Srgrimes
1771541Srgrimes/*
1781541Srgrimes * Macro for looking up the in_multi record for a given IP multicast address
17921666Swollman * on a given interface.  If no matching record is found, "inm" is set null.
1801541Srgrimes */
1811541Srgrimes#define IN_LOOKUP_MULTI(addr, ifp, inm) \
1821541Srgrimes	/* struct in_addr addr; */ \
1831541Srgrimes	/* struct ifnet *ifp; */ \
1841541Srgrimes	/* struct in_multi *inm; */ \
18520407Swollmando { \
18679821Sru	struct ifmultiaddr *ifma; \
1871541Srgrimes\
18872084Sphk	TAILQ_FOREACH(ifma, &((ifp)->if_multiaddrs), ifma_link) { \
18921666Swollman		if (ifma->ifma_addr->sa_family == AF_INET \
19021929Swollman		    && ((struct sockaddr_in *)ifma->ifma_addr)->sin_addr.s_addr == \
19121666Swollman		    (addr).s_addr) \
19221666Swollman			break; \
19321666Swollman	} \
19421666Swollman	(inm) = ifma ? ifma->ifma_protospec : 0; \
19520407Swollman} while(0)
1961541Srgrimes
1971541Srgrimes/*
1981541Srgrimes * Macro to step through all of the in_multi records, one at a time.
1991541Srgrimes * The current position is remembered in "step", which the caller must
2001541Srgrimes * provide.  IN_FIRST_MULTI(), below, must be called to initialize "step"
2011541Srgrimes * and get the first record.  Both macros return a NULL "inm" when there
2021541Srgrimes * are no remaining records.
2031541Srgrimes */
2041541Srgrimes#define IN_NEXT_MULTI(step, inm) \
2051541Srgrimes	/* struct in_multistep  step; */ \
2061541Srgrimes	/* struct in_multi *inm; */ \
20720407Swollmando { \
2081541Srgrimes	if (((inm) = (step).i_inm) != NULL) \
20971998Sphk		(step).i_inm = LIST_NEXT((step).i_inm, inm_link); \
21020407Swollman} while(0)
2111541Srgrimes
2121541Srgrimes#define IN_FIRST_MULTI(step, inm) \
2131541Srgrimes	/* struct in_multistep step; */ \
2141541Srgrimes	/* struct in_multi *inm; */ \
21520407Swollmando { \
21671998Sphk	(step).i_inm = LIST_FIRST(&in_multihead); \
2171541Srgrimes	IN_NEXT_MULTI((step), (inm)); \
21820407Swollman} while(0)
2191541Srgrimes
22036192Sdgstruct	route;
22192723Salfredstruct	in_multi *in_addmulti(struct in_addr *, struct ifnet *);
22292723Salfredvoid	in_delmulti(struct in_multi *);
22392723Salfredint	in_control(struct socket *, u_long, caddr_t, struct ifnet *,
22493085Sbde	    struct thread *);
22592723Salfredvoid	in_rtqdrain(void);
22692723Salfredvoid	ip_input(struct mbuf *);
22792723Salfredint	in_ifadown(struct ifaddr *ifa, int);
22892723Salfredvoid	in_ifscrub(struct ifnet *, struct in_ifaddr *);
229122702Sandreint	ip_fastforward(struct mbuf *);
2302169Spaul
23155205Speter#endif /* _KERNEL */
23210939Swollman
23352904Sshin/* INET6 stuff */
23452904Sshin#include <netinet6/in6_var.h>
23552904Sshin
23610939Swollman#endif /* _NETINET_IN_VAR_H_ */
237