in_var.h revision 50477
178342Sbenno/*
278342Sbenno * Copyright (c) 1985, 1986, 1993
378342Sbenno *	The Regents of the University of California.  All rights reserved.
478342Sbenno *
578342Sbenno * Redistribution and use in source and binary forms, with or without
678342Sbenno * modification, are permitted provided that the following conditions
778342Sbenno * are met:
878342Sbenno * 1. Redistributions of source code must retain the above copyright
978342Sbenno *    notice, this list of conditions and the following disclaimer.
1078342Sbenno * 2. Redistributions in binary form must reproduce the above copyright
1178342Sbenno *    notice, this list of conditions and the following disclaimer in the
1278342Sbenno *    documentation and/or other materials provided with the distribution.
1378342Sbenno * 3. All advertising materials mentioning features or use of this software
1478342Sbenno *    must display the following acknowledgement:
1578342Sbenno *	This product includes software developed by the University of
1678342Sbenno *	California, Berkeley and its contributors.
1778342Sbenno * 4. Neither the name of the University nor the names of its contributors
1878342Sbenno *    may be used to endorse or promote products derived from this software
1978342Sbenno *    without specific prior written permission.
2078342Sbenno *
2178342Sbenno * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2278342Sbenno * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2378342Sbenno * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2478342Sbenno * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2578342Sbenno * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2678342Sbenno * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27113038Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28113038Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2978342Sbenno * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3078342Sbenno * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3178342Sbenno * SUCH DAMAGE.
3278342Sbenno *
3378342Sbenno *	@(#)in_var.h	8.2 (Berkeley) 1/9/95
34103597Sgrehan * $FreeBSD: head/sys/netinet/in_var.h 50477 1999-08-28 01:08:13Z peter $
3578342Sbenno */
36171805Smarcel
37171805Smarcel#ifndef _NETINET_IN_VAR_H_
38108940Sgrehan#define _NETINET_IN_VAR_H_
39108940Sgrehan
40143784Sgrehan#include <sys/queue.h>
4192842Salfred
42143784Sgrehan/*
4378342Sbenno * Interface address, Internet version.  One of these structures
44143784Sgrehan * is allocated for each Internet address on an interface.
45143784Sgrehan * The ifaddr structure contains the protocol-independent part
46143784Sgrehan * of the structure and is assumed to be first.
47143784Sgrehan */
48143784Sgrehanstruct in_ifaddr {
49143784Sgrehan	struct	ifaddr ia_ifa;		/* protocol-independent info */
5078342Sbenno#define	ia_ifp		ia_ifa.ifa_ifp
5178342Sbenno#define ia_flags	ia_ifa.ifa_flags
5278342Sbenno					/* ia_{,sub}net{,mask} in host order */
5378342Sbenno	u_long	ia_net;			/* network number of interface */
54143784Sgrehan	u_long	ia_netmask;		/* mask of net part */
55143784Sgrehan	u_long	ia_subnet;		/* subnet number, including net */
56146794Smarcel	u_long	ia_subnetmask;		/* mask of subnet part */
57146794Smarcel	struct	in_addr ia_netbroadcast; /* to recognize net broadcasts */
58143784Sgrehan	TAILQ_ENTRY(in_ifaddr) ia_link;	/* tailq macro glue */
59143784Sgrehan	struct	sockaddr_in ia_addr;	/* reserve space for interface name */
60143784Sgrehan	struct	sockaddr_in ia_dstaddr; /* reserve space for broadcast addr */
6178342Sbenno#define	ia_broadaddr	ia_dstaddr
6278342Sbenno	struct	sockaddr_in ia_sockmask; /* reserve space for general netmask */
63143784Sgrehan};
6484855Smp
65143784Sgrehanstruct	in_aliasreq {
6684855Smp	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
67143784Sgrehan	struct	sockaddr_in ifra_addr;
68143784Sgrehan	struct	sockaddr_in ifra_broadaddr;
69143784Sgrehan#define ifra_dstaddr ifra_broadaddr
70171805Smarcel	struct	sockaddr_in ifra_mask;
71103597Sgrehan};
72171805Smarcel/*
73171805Smarcel * Given a pointer to an in_ifaddr (ifaddr),
74103597Sgrehan * return a pointer to the addr as a sockaddr_in.
75171805Smarcel */
76171805Smarcel#define IA_SIN(ia)    (&(((struct in_ifaddr *)(ia))->ia_addr))
77171805Smarcel#define IA_DSTSIN(ia) (&(((struct in_ifaddr *)(ia))->ia_dstaddr))
78176771Sraj
79183030Smarcel#define IN_LNAOF(in, ifa) \
80146792Smarcel	((ntohl((in).s_addr) & ~((struct in_ifaddr *)(ifa)->ia_subnetmask))
81103597Sgrehan
8278342Sbenno
83#ifdef	KERNEL
84extern	TAILQ_HEAD(in_ifaddrhead, in_ifaddr) in_ifaddrhead;
85extern	struct	ifqueue	ipintrq;		/* ip packet input queue */
86extern	struct	in_addr zeroin_addr;
87extern	u_char	inetctlerrmap[];
88
89/*
90 * Macro for finding the interface (ifnet structure) corresponding to one
91 * of our IP addresses.
92 */
93#define INADDR_TO_IFP(addr, ifp) \
94	/* struct in_addr addr; */ \
95	/* struct ifnet *ifp; */ \
96{ \
97	register struct in_ifaddr *ia; \
98\
99	for (ia = in_ifaddrhead.tqh_first; \
100	    ia != NULL && ((ia->ia_ifp->if_flags & IFF_POINTOPOINT)? \
101		IA_DSTSIN(ia):IA_SIN(ia))->sin_addr.s_addr != (addr).s_addr; \
102	    ia = ia->ia_link.tqe_next) \
103		 continue; \
104	if (ia == NULL) \
105	    for (ia = in_ifaddrhead.tqh_first; \
106		ia != NULL; \
107		ia = ia->ia_link.tqe_next) \
108		    if (ia->ia_ifp->if_flags & IFF_POINTOPOINT && \
109			IA_SIN(ia)->sin_addr.s_addr == (addr).s_addr) \
110			    break; \
111	(ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \
112}
113
114/*
115 * Macro for finding the internet address structure (in_ifaddr) corresponding
116 * to a given interface (ifnet structure).
117 */
118#define IFP_TO_IA(ifp, ia) \
119	/* struct ifnet *ifp; */ \
120	/* struct in_ifaddr *ia; */ \
121{ \
122	for ((ia) = in_ifaddrhead.tqh_first; \
123	    (ia) != NULL && (ia)->ia_ifp != (ifp); \
124	    (ia) = (ia)->ia_link.tqe_next) \
125		continue; \
126}
127#endif
128
129/*
130 * This information should be part of the ifnet structure but we don't wish
131 * to change that - as it might break a number of things
132 */
133
134struct router_info {
135	struct ifnet *rti_ifp;
136	int    rti_type; /* type of router which is querier on this interface */
137	int    rti_time; /* # of slow timeouts since last old query */
138	struct router_info *rti_next;
139};
140
141/*
142 * Internet multicast address structure.  There is one of these for each IP
143 * multicast group to which this host belongs on a given network interface.
144 * For every entry on the interface's if_multiaddrs list which represents
145 * an IP multicast group, there is one of these structures.  They are also
146 * kept on a system-wide list to make it easier to keep our legacy IGMP code
147 * compatible with the rest of the world (see IN_FIRST_MULTI et al, below).
148 */
149struct in_multi {
150	LIST_ENTRY(in_multi) inm_link;	/* queue macro glue */
151	struct	in_addr inm_addr;	/* IP multicast address, convenience */
152	struct	ifnet *inm_ifp;		/* back pointer to ifnet */
153	struct	ifmultiaddr *inm_ifma;	/* back pointer to ifmultiaddr */
154	u_int	inm_timer;		/* IGMP membership report timer */
155	u_int	inm_state;		/*  state of the membership */
156	struct	router_info *inm_rti;	/* router info*/
157};
158
159#ifdef KERNEL
160
161#ifdef SYSCTL_DECL
162SYSCTL_DECL(_net_inet_ip);
163SYSCTL_DECL(_net_inet_raw);
164#endif
165
166extern LIST_HEAD(in_multihead, in_multi) in_multihead;
167
168/*
169 * Structure used by macros below to remember position when stepping through
170 * all of the in_multi records.
171 */
172struct in_multistep {
173	struct in_multi *i_inm;
174};
175
176/*
177 * Macro for looking up the in_multi record for a given IP multicast address
178 * on a given interface.  If no matching record is found, "inm" is set null.
179 */
180#define IN_LOOKUP_MULTI(addr, ifp, inm) \
181	/* struct in_addr addr; */ \
182	/* struct ifnet *ifp; */ \
183	/* struct in_multi *inm; */ \
184do { \
185	register struct ifmultiaddr *ifma; \
186\
187	for (ifma = (ifp)->if_multiaddrs.lh_first; ifma; \
188	     ifma = ifma->ifma_link.le_next) { \
189		if (ifma->ifma_addr->sa_family == AF_INET \
190		    && ((struct sockaddr_in *)ifma->ifma_addr)->sin_addr.s_addr == \
191		    (addr).s_addr) \
192			break; \
193	} \
194	(inm) = ifma ? ifma->ifma_protospec : 0; \
195} while(0)
196
197/*
198 * Macro to step through all of the in_multi records, one at a time.
199 * The current position is remembered in "step", which the caller must
200 * provide.  IN_FIRST_MULTI(), below, must be called to initialize "step"
201 * and get the first record.  Both macros return a NULL "inm" when there
202 * are no remaining records.
203 */
204#define IN_NEXT_MULTI(step, inm) \
205	/* struct in_multistep  step; */ \
206	/* struct in_multi *inm; */ \
207do { \
208	if (((inm) = (step).i_inm) != NULL) \
209		(step).i_inm = (step).i_inm->inm_link.le_next; \
210} while(0)
211
212#define IN_FIRST_MULTI(step, inm) \
213	/* struct in_multistep step; */ \
214	/* struct in_multi *inm; */ \
215do { \
216	(step).i_inm = in_multihead.lh_first; \
217	IN_NEXT_MULTI((step), (inm)); \
218} while(0)
219
220struct	route;
221struct	in_multi *in_addmulti __P((struct in_addr *, struct ifnet *));
222void	in_delmulti __P((struct in_multi *));
223int	in_control __P((struct socket *, u_long, caddr_t, struct ifnet *,
224			struct proc *));
225void	in_rtqdrain __P((void));
226void	ip_input __P((struct mbuf *));
227int	in_ifadown __P((struct ifaddr *ifa));
228void	in_ifscrub __P((struct ifnet *, struct in_ifaddr *));
229int	ipflow_fastforward __P((struct mbuf *));
230void	ipflow_create __P((const struct route *, struct mbuf *));
231void	ipflow_slowtimo __P((void));
232
233#endif /* KERNEL */
234
235#endif /* _NETINET_IN_VAR_H_ */
236