in_var.h revision 7090
155714Skris/*
255714Skris * Copyright (c) 1985, 1986, 1993
355714Skris *	The Regents of the University of California.  All rights reserved.
455714Skris *
555714Skris * Redistribution and use in source and binary forms, with or without
655714Skris * modification, are permitted provided that the following conditions
755714Skris * are met:
855714Skris * 1. Redistributions of source code must retain the above copyright
955714Skris *    notice, this list of conditions and the following disclaimer.
1055714Skris * 2. Redistributions in binary form must reproduce the above copyright
1155714Skris *    notice, this list of conditions and the following disclaimer in the
1255714Skris *    documentation and/or other materials provided with the distribution.
1355714Skris * 3. All advertising materials mentioning features or use of this software
1455714Skris *    must display the following acknowledgement:
1555714Skris *	This product includes software developed by the University of
1655714Skris *	California, Berkeley and its contributors.
1755714Skris * 4. Neither the name of the University nor the names of its contributors
1855714Skris *    may be used to endorse or promote products derived from this software
1955714Skris *    without specific prior written permission.
2055714Skris *
2155714Skris * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3155714Skris * SUCH DAMAGE.
3255714Skris *
3355714Skris *	@(#)in_var.h	8.1 (Berkeley) 6/10/93
3455714Skris * $Id: in_var.h,v 1.7 1994/10/25 22:13:32 swallace Exp $
3555714Skris */
3655714Skris
3755714Skris#ifndef _NETINET_IN_VAR_H_
3855714Skris#define _NETINET_IN_VAR_H_
3955714Skris
4055714Skris/*
4155714Skris * Interface address, Internet version.  One of these structures
4255714Skris * is allocated for each interface with an Internet address.
4355714Skris * The ifaddr structure contains the protocol-independent part
4455714Skris * of the structure and is assumed to be first.
4555714Skris */
4655714Skrisstruct in_ifaddr {
4755714Skris	struct	ifaddr ia_ifa;		/* protocol-independent info */
4855714Skris#define	ia_ifp		ia_ifa.ifa_ifp
4955714Skris#define ia_flags	ia_ifa.ifa_flags
5055714Skris					/* ia_{,sub}net{,mask} in host order */
5155714Skris	u_long	ia_net;			/* network number of interface */
5255714Skris	u_long	ia_netmask;		/* mask of net part */
5355714Skris	u_long	ia_subnet;		/* subnet number, including net */
5455714Skris	u_long	ia_subnetmask;		/* mask of subnet part */
5555714Skris	struct	in_addr ia_netbroadcast; /* to recognize net broadcasts */
5655714Skris	struct	in_ifaddr *ia_next;	/* next in list of internet addresses */
5755714Skris	struct	sockaddr_in ia_addr;	/* reserve space for interface name */
5855714Skris	struct	sockaddr_in ia_dstaddr; /* reserve space for broadcast addr */
5955714Skris#define	ia_broadaddr	ia_dstaddr
6055714Skris	struct	sockaddr_in ia_sockmask; /* reserve space for general netmask */
6168651Skris	struct	in_multi *ia_multiaddrs; /* list of multicast addresses */
6268651Skris};
6368651Skris
6468651Skrisstruct	in_aliasreq {
6555714Skris	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
6655714Skris	struct	sockaddr_in ifra_addr;
6755714Skris	struct	sockaddr_in ifra_broadaddr;
6855714Skris#define ifra_dstaddr ifra_broadaddr
6955714Skris	struct	sockaddr_in ifra_mask;
7055714Skris};
7155714Skris/*
7255714Skris * Given a pointer to an in_ifaddr (ifaddr),
7355714Skris * return a pointer to the addr as a sockaddr_in.
7455714Skris */
7555714Skris#define IA_SIN(ia)    (&(((struct in_ifaddr *)(ia))->ia_addr))
7655714Skris#define IA_DSTSIN(ia) (&(((struct in_ifaddr *)(ia))->ia_dstaddr))
7755714Skris
7855714Skris#define IN_LNAOF(in, ifa) \
7955714Skris	((ntohl((in).s_addr) & ~((struct in_ifaddr *)(ifa)->ia_subnetmask))
8055714Skris
8155714Skris
8255714Skris#ifdef	KERNEL
8355714Skrisextern	struct	in_ifaddr *in_ifaddr;
8455714Skrisextern	struct	ifqueue	ipintrq;		/* ip packet input queue */
8555714Skrisextern	struct	in_addr zeroin_addr;
8655714Skrisextern	u_char	inetctlerrmap[];
8755714Skrisextern	int rtq_reallyold;	/* XXX */
8855714Skrisextern	int rtq_minreallyold;	/* XXX */
8955714Skrisextern	int rtq_toomany;	/* XXX */
9055714Skris
9155714Skris/*
9255714Skris * Macro for finding the interface (ifnet structure) corresponding to one
9355714Skris * of our IP addresses.
9455714Skris */
9555714Skris#define INADDR_TO_IFP(addr, ifp) \
9655714Skris	/* struct in_addr addr; */ \
9755714Skris	/* struct ifnet *ifp; */ \
9855714Skris{ \
9955714Skris	register struct in_ifaddr *ia; \
10055714Skris\
10155714Skris	for (ia = in_ifaddr; \
10255714Skris	    ia != NULL && ((ia->ia_ifp->if_flags & IFF_POINTOPOINT)? \
10355714Skris		IA_DSTSIN(ia):IA_SIN(ia))->sin_addr.s_addr != (addr).s_addr; \
10455714Skris	    ia = ia->ia_next) \
10555714Skris		 continue; \
10655714Skris	(ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \
10755714Skris}
10855714Skris
10955714Skris/*
11055714Skris * Macro for finding the internet address structure (in_ifaddr) corresponding
11155714Skris * to a given interface (ifnet structure).
11255714Skris */
11355714Skris#define IFP_TO_IA(ifp, ia) \
11455714Skris	/* struct ifnet *ifp; */ \
11555714Skris	/* struct in_ifaddr *ia; */ \
11655714Skris{ \
11755714Skris	for ((ia) = in_ifaddr; \
11855714Skris	    (ia) != NULL && (ia)->ia_ifp != (ifp); \
11955714Skris	    (ia) = (ia)->ia_next) \
12055714Skris		continue; \
12155714Skris}
12255714Skris#endif
12355714Skris
12455714Skris/*
12555714Skris * This information should be part of the ifnet structure but we don't wish
12655714Skris * to change that - as it might break a number of things
12755714Skris */
12855714Skris
12955714Skrisstruct router_info {
13055714Skris	struct ifnet *ifp;
13155714Skris	int    type; /* type of router which is querier on this interface */
13255714Skris	int    time; /* # of slow timeouts since last old query */
13355714Skris	struct router_info *next;
13468651Skris};
13568651Skris
13655714Skris/*
13755714Skris * Internet multicast address structure.  There is one of these for each IP
13855714Skris * multicast group to which this host belongs on a given network interface.
13955714Skris * They are kept in a linked list, rooted in the interface's in_ifaddr
14055714Skris * structure.
14155714Skris */
14255714Skrisstruct in_multi {
14355714Skris	struct	in_addr inm_addr;	/* IP multicast address */
14455714Skris	struct	ifnet *inm_ifp;		/* back pointer to ifnet */
14555714Skris	struct	in_ifaddr *inm_ia;	/* back pointer to in_ifaddr */
14655714Skris	u_int	inm_refcount;		/* no. membership claims by sockets */
14755714Skris	u_int	inm_timer;		/* IGMP membership report timer */
14855714Skris	struct	in_multi *inm_next;	/* ptr to next multicast address */
14955714Skris	u_int	inm_state;		/*  state of the membership */
15055714Skris	struct	router_info *inm_rti;	/* router info*/
15155714Skris};
15255714Skris
15355714Skris#ifdef KERNEL
15459191Skris/*
15559191Skris * Structure used by macros below to remember position when stepping through
15659191Skris * all of the in_multi records.
15759191Skris */
15859191Skrisstruct in_multistep {
15955714Skris	struct in_ifaddr *i_ia;
16055714Skris	struct in_multi *i_inm;
16155714Skris};
16255714Skris
16355714Skris/*
16455714Skris * Macro for looking up the in_multi record for a given IP multicast address
16555714Skris * on a given interface.  If no matching record is found, "inm" returns NULL.
16655714Skris */
16755714Skris#define IN_LOOKUP_MULTI(addr, ifp, inm) \
16855714Skris	/* struct in_addr addr; */ \
16955714Skris	/* struct ifnet *ifp; */ \
17055714Skris	/* struct in_multi *inm; */ \
17155714Skris{ \
17255714Skris	register struct in_ifaddr *ia; \
17355714Skris\
17455714Skris	IFP_TO_IA((ifp), ia); \
17555714Skris	if (ia == NULL) \
17655714Skris		(inm) = NULL; \
17755714Skris	else \
17859191Skris		for ((inm) = ia->ia_multiaddrs; \
17959191Skris		    (inm) != NULL && (inm)->inm_addr.s_addr != (addr).s_addr; \
18055714Skris		     (inm) = inm->inm_next) \
18155714Skris			 continue; \
18255714Skris}
18359191Skris
18459191Skris/*
18559191Skris * Macro to step through all of the in_multi records, one at a time.
18659191Skris * The current position is remembered in "step", which the caller must
18759191Skris * provide.  IN_FIRST_MULTI(), below, must be called to initialize "step"
18855714Skris * and get the first record.  Both macros return a NULL "inm" when there
18955714Skris * are no remaining records.
19055714Skris */
19159191Skris#define IN_NEXT_MULTI(step, inm) \
19259191Skris	/* struct in_multistep  step; */ \
19359191Skris	/* struct in_multi *inm; */ \
19455714Skris{ \
19555714Skris	if (((inm) = (step).i_inm) != NULL) \
19655714Skris		(step).i_inm = (inm)->inm_next; \
19759191Skris	else \
19855714Skris		while ((step).i_ia != NULL) { \
19955714Skris			(inm) = (step).i_ia->ia_multiaddrs; \
20055714Skris			(step).i_ia = (step).i_ia->ia_next; \
20155714Skris			if ((inm) != NULL) { \
20255714Skris				(step).i_inm = (inm)->inm_next; \
20355714Skris				break; \
20455714Skris			} \
20555714Skris		} \
20655714Skris}
20755714Skris
20855714Skris#define IN_FIRST_MULTI(step, inm) \
20955714Skris	/* struct in_multistep step; */ \
21055714Skris	/* struct in_multi *inm; */ \
21155714Skris{ \
21255714Skris	(step).i_ia = in_ifaddr; \
21355714Skris	(step).i_inm = NULL; \
21455714Skris	IN_NEXT_MULTI((step), (inm)); \
21555714Skris}
21655714Skris
21755714Skrisint	in_ifinit __P((struct ifnet *,
21855714Skris	    struct in_ifaddr *, struct sockaddr_in *, int));
21955714Skrisstruct	in_multi *in_addmulti __P((struct in_addr *, struct ifnet *));
22055714Skrisvoid	in_delmulti __P((struct in_multi *));
22155714Skrisvoid	in_ifscrub __P((struct ifnet *, struct in_ifaddr *));
22255714Skrisint	in_control __P((struct socket *, int, caddr_t, struct ifnet *));
22355714Skris
22455714Skris#endif
22555714Skris#endif
22655714Skris