xref: /original-bsd/sys/netinet/in_var.h (revision 860e07fc)
1 /*
2  * Copyright (c) 1985, 1986 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)in_var.h	7.7 (Berkeley) 07/06/92
8  */
9 
10 /*
11  * Interface address, Internet version.  One of these structures
12  * is allocated for each interface with an Internet address.
13  * The ifaddr structure contains the protocol-independent part
14  * of the structure and is assumed to be first.
15  */
16 struct in_ifaddr {
17 	struct	ifaddr ia_ifa;		/* protocol-independent info */
18 #define	ia_ifp		ia_ifa.ifa_ifp
19 #define ia_flags	ia_ifa.ifa_flags
20 					/* ia_{,sub}net{,mask} in host order */
21 	u_long	ia_net;			/* network number of interface */
22 	u_long	ia_netmask;		/* mask of net part */
23 	u_long	ia_subnet;		/* subnet number, including net */
24 	u_long	ia_subnetmask;		/* mask of subnet part */
25 	struct	in_addr ia_netbroadcast; /* to recognize net broadcasts */
26 	struct	in_ifaddr *ia_next;	/* next in list of internet addresses */
27 	struct	sockaddr_in ia_addr;	/* reserve space for interface name */
28 	struct	sockaddr_in ia_dstaddr; /* reserve space for broadcast addr */
29 #define	ia_broadaddr	ia_dstaddr
30 	struct	sockaddr_in ia_sockmask; /* reserve space for general netmask */
31 	struct	in_multi *ia_multiaddrs; /* list of multicast addresses */
32 };
33 
34 struct	in_aliasreq {
35 	char	ifra_name[IFNAMSIZ];		/* if name, e.g. "en0" */
36 	struct	sockaddr_in ifra_addr;
37 	struct	sockaddr_in ifra_broadaddr;
38 #define ifra_dstaddr ifra_broadaddr
39 	struct	sockaddr_in ifra_mask;
40 };
41 /*
42  * Given a pointer to an in_ifaddr (ifaddr),
43  * return a pointer to the addr as a sockaddr_in.
44  */
45 #define	IA_SIN(ia) (&(((struct in_ifaddr *)(ia))->ia_addr))
46 
47 #ifdef	KERNEL
48 struct	in_ifaddr *in_ifaddr;
49 struct	in_ifaddr *in_iaonnetof();
50 struct	ifqueue	ipintrq;		/* ip packet input queue */
51 #endif
52 
53 #ifdef KERNEL
54 /*
55  * Macro for finding the interface (ifnet structure) corresponding to one
56  * of our IP addresses.
57  */
58 #define INADDR_TO_IFP(addr, ifp) \
59 	/* struct in_addr addr; */ \
60 	/* struct ifnet *ifp; */ \
61 { \
62 	register struct in_ifaddr *ia; \
63 \
64 	for (ia = in_ifaddr; \
65 	    ia != NULL && IA_SIN(ia)->sin_addr.s_addr != (addr).s_addr; \
66 	    ia = ia->ia_next) \
67 		 continue; \
68 	(ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \
69 }
70 
71 /*
72  * Macro for finding the internet address structure (in_ifaddr) corresponding
73  * to a given interface (ifnet structure).
74  */
75 #define IFP_TO_IA(ifp, ia) \
76 	/* struct ifnet *ifp; */ \
77 	/* struct in_ifaddr *ia; */ \
78 { \
79 	for ((ia) = in_ifaddr; \
80 	    (ia) != NULL && (ia)->ia_ifp != (ifp); \
81 	    (ia) = (ia)->ia_next) \
82 		continue; \
83 }
84 #endif
85 
86 /*
87  * Internet multicast address structure.  There is one of these for each IP
88  * multicast group to which this host belongs on a given network interface.
89  * They are kept in a linked list, rooted in the interface's in_ifaddr
90  * structure.
91  */
92 struct in_multi {
93 	struct	in_addr inm_addr;	/* IP multicast address */
94 	struct	ifnet *inm_ifp;		/* back pointer to ifnet */
95 	struct	in_ifaddr *inm_ia;	/* back pointer to in_ifaddr */
96 	u_int	inm_refcount;		/* no. membership claims by sockets */
97 	u_int	inm_timer;		/* IGMP membership report timer */
98 	struct	in_multi *inm_next;	/* ptr to next multicast address */
99 };
100 
101 #ifdef KERNEL
102 /*
103  * Structure used by macros below to remember position when stepping through
104  * all of the in_multi records.
105  */
106 struct in_multistep {
107 	struct in_ifaddr *i_ia;
108 	struct in_multi *i_inm;
109 };
110 
111 /*
112  * Macro for looking up the in_multi record for a given IP multicast address
113  * on a given interface.  If no matching record is found, "inm" returns NULL.
114  */
115 #define IN_LOOKUP_MULTI(addr, ifp, inm) \
116 	/* struct in_addr addr; */ \
117 	/* struct ifnet *ifp; */ \
118 	/* struct in_multi *inm; */ \
119 { \
120 	register struct in_ifaddr *ia; \
121 \
122 	IFP_TO_IA((ifp), ia); \
123 	if (ia == NULL) \
124 		(inm) = NULL; \
125 	else \
126 		for ((inm) = ia->ia_multiaddrs; \
127 		    (inm) != NULL && (inm)->inm_addr.s_addr != (addr).s_addr; \
128 		     (inm) = inm->inm_next) \
129 			 continue; \
130 }
131 
132 /*
133  * Macro to step through all of the in_multi records, one at a time.
134  * The current position is remembered in "step", which the caller must
135  * provide.  IN_FIRST_MULTI(), below, must be called to initialize "step"
136  * and get the first record.  Both macros return a NULL "inm" when there
137  * are no remaining records.
138  */
139 #define IN_NEXT_MULTI(step, inm) \
140 	/* struct in_multistep  step; */ \
141 	/* struct in_multi *inm; */ \
142 { \
143 	if (((inm) = (step).i_inm) != NULL) \
144 		(step).i_inm = (inm)->inm_next; \
145 	else \
146 		while ((step).i_ia != NULL) { \
147 			(inm) = (step).i_ia->ia_multiaddrs; \
148 			(step).i_ia = (step).i_ia->ia_next; \
149 			if ((inm) != NULL) { \
150 				(step).i_inm = (inm)->inm_next; \
151 				break; \
152 			} \
153 		} \
154 }
155 
156 #define IN_FIRST_MULTI(step, inm) \
157 	/* struct in_multistep step; */ \
158 	/* struct in_multi *inm; */ \
159 { \
160 	(step).i_ia = in_ifaddr; \
161 	(step).i_inm = NULL; \
162 	IN_NEXT_MULTI((step), (inm)); \
163 }
164 
165 struct	in_multi *in_addmulti __P((struct in_addr *, struct ifnet *));
166 int	in_delmulti __P((struct in_multi *));
167 #endif
168