xref: /original-bsd/sys/netinet/in_var.h (revision 3f14a87d)
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.8 (Berkeley) 04/07/93
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 #define IN_LNAOF(in, ifa) \
48 	((ntohl((in).s_addr) & ~((struct in_ifaddr *)(ifa)->ia_subnetmask))
49 
50 
51 #ifdef	KERNEL
52 extern	struct	in_ifaddr *in_ifaddr;
53 extern	struct	ifqueue	ipintrq;		/* ip packet input queue */
54 void	in_socktrim __P((struct sockaddr_in *));
55 
56 
57 /*
58  * Macro for finding the interface (ifnet structure) corresponding to one
59  * of our IP addresses.
60  */
61 #define INADDR_TO_IFP(addr, ifp) \
62 	/* struct in_addr addr; */ \
63 	/* struct ifnet *ifp; */ \
64 { \
65 	register struct in_ifaddr *ia; \
66 \
67 	for (ia = in_ifaddr; \
68 	    ia != NULL && IA_SIN(ia)->sin_addr.s_addr != (addr).s_addr; \
69 	    ia = ia->ia_next) \
70 		 continue; \
71 	(ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \
72 }
73 
74 /*
75  * Macro for finding the internet address structure (in_ifaddr) corresponding
76  * to a given interface (ifnet structure).
77  */
78 #define IFP_TO_IA(ifp, ia) \
79 	/* struct ifnet *ifp; */ \
80 	/* struct in_ifaddr *ia; */ \
81 { \
82 	for ((ia) = in_ifaddr; \
83 	    (ia) != NULL && (ia)->ia_ifp != (ifp); \
84 	    (ia) = (ia)->ia_next) \
85 		continue; \
86 }
87 #endif
88 
89 /*
90  * Internet multicast address structure.  There is one of these for each IP
91  * multicast group to which this host belongs on a given network interface.
92  * They are kept in a linked list, rooted in the interface's in_ifaddr
93  * structure.
94  */
95 struct in_multi {
96 	struct	in_addr inm_addr;	/* IP multicast address */
97 	struct	ifnet *inm_ifp;		/* back pointer to ifnet */
98 	struct	in_ifaddr *inm_ia;	/* back pointer to in_ifaddr */
99 	u_int	inm_refcount;		/* no. membership claims by sockets */
100 	u_int	inm_timer;		/* IGMP membership report timer */
101 	struct	in_multi *inm_next;	/* ptr to next multicast address */
102 };
103 
104 #ifdef KERNEL
105 /*
106  * Structure used by macros below to remember position when stepping through
107  * all of the in_multi records.
108  */
109 struct in_multistep {
110 	struct in_ifaddr *i_ia;
111 	struct in_multi *i_inm;
112 };
113 
114 /*
115  * Macro for looking up the in_multi record for a given IP multicast address
116  * on a given interface.  If no matching record is found, "inm" returns NULL.
117  */
118 #define IN_LOOKUP_MULTI(addr, ifp, inm) \
119 	/* struct in_addr addr; */ \
120 	/* struct ifnet *ifp; */ \
121 	/* struct in_multi *inm; */ \
122 { \
123 	register struct in_ifaddr *ia; \
124 \
125 	IFP_TO_IA((ifp), ia); \
126 	if (ia == NULL) \
127 		(inm) = NULL; \
128 	else \
129 		for ((inm) = ia->ia_multiaddrs; \
130 		    (inm) != NULL && (inm)->inm_addr.s_addr != (addr).s_addr; \
131 		     (inm) = inm->inm_next) \
132 			 continue; \
133 }
134 
135 /*
136  * Macro to step through all of the in_multi records, one at a time.
137  * The current position is remembered in "step", which the caller must
138  * provide.  IN_FIRST_MULTI(), below, must be called to initialize "step"
139  * and get the first record.  Both macros return a NULL "inm" when there
140  * are no remaining records.
141  */
142 #define IN_NEXT_MULTI(step, inm) \
143 	/* struct in_multistep  step; */ \
144 	/* struct in_multi *inm; */ \
145 { \
146 	if (((inm) = (step).i_inm) != NULL) \
147 		(step).i_inm = (inm)->inm_next; \
148 	else \
149 		while ((step).i_ia != NULL) { \
150 			(inm) = (step).i_ia->ia_multiaddrs; \
151 			(step).i_ia = (step).i_ia->ia_next; \
152 			if ((inm) != NULL) { \
153 				(step).i_inm = (inm)->inm_next; \
154 				break; \
155 			} \
156 		} \
157 }
158 
159 #define IN_FIRST_MULTI(step, inm) \
160 	/* struct in_multistep step; */ \
161 	/* struct in_multi *inm; */ \
162 { \
163 	(step).i_ia = in_ifaddr; \
164 	(step).i_inm = NULL; \
165 	IN_NEXT_MULTI((step), (inm)); \
166 }
167 
168 struct	in_multi *in_addmulti __P((struct in_addr *, struct ifnet *));
169 int	in_delmulti __P((struct in_multi *));
170 #endif
171