xref: /titanic_52/usr/src/cmd/cmd-inet/sbin/dhcpagent/interface.h (revision ea8dc4b6d2251b437950c0056bc626b311c73c27)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	INTERFACE_H
28 #define	INTERFACE_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * interface.[ch] encapsulate all of the agent's knowledge of network
34  * interfaces from the DHCP agent's perspective.  see interface.c
35  * for documentation on how to use the exported functions.  note that
36  * there are not functional interfaces for manipulating all of the fields
37  * in an ifslist -- please read the comments in the ifslist structure
38  * definition below for the rules on accessing various fields.
39  */
40 
41 #ifdef	__cplusplus
42 extern "C" {
43 #endif
44 
45 #include <netinet/in.h>
46 #include <sys/socket.h>
47 #include <net/if.h>			/* IFNAMSIZ */
48 #include <sys/types.h>
49 #include <netinet/dhcp.h>
50 #include <dhcpagent_ipc.h>
51 #include <libinetutil.h>
52 
53 #include "async.h"
54 #include "agent.h"
55 #include "dlpi_io.h"
56 #include "ipc_action.h"
57 #include "packet.h"
58 #include "util.h"
59 
60 enum { DHCP_T1_TIMER, DHCP_T2_TIMER, DHCP_LEASE_TIMER };
61 
62 typedef int script_callback_t (struct ifslist *, const char *);
63 
64 struct ifslist {
65 
66 	/*
67 	 * ifslist chain pointers, maintained by insert_ifs() /
68 	 * remove_ifs().
69 	 */
70 
71 	struct ifslist		*next;
72 	struct ifslist		*prev;
73 
74 	/*
75 	 * hold count on this ifslist, maintained by hold_ifs() /
76 	 * release_ifs() -- see below for a discussion of ifs memory
77 	 * management.
78 	 */
79 
80 	uchar_t			if_hold_count;
81 
82 	/*
83 	 * each interface can have at most one pending asynchronous
84 	 * action, which is represented in a `struct async_action'.
85 	 * if that asynchronous action was a result of a user request,
86 	 * then the `struct ipc_action' is used to hold information
87 	 * about the user request.  these structures are opaque to
88 	 * users of the ifslist, and the functional interfaces
89 	 * provided in async.[ch] and ipc_action.[ch] should be used
90 	 * to maintain them.
91 	 */
92 
93 	struct ipc_action	if_ia;
94 	struct async_action	if_async;
95 
96 	/*
97 	 * current state of the interface
98 	 */
99 
100 	DHCPSTATE		if_state;
101 
102 	/*
103 	 * flags specific to DHCP (see dhcpagent_ipc.h)
104 	 */
105 
106 	uint16_t		if_dflags;
107 
108 	/*
109 	 * general interface information -- this information is initialized
110 	 * in insert_ifs() and does not change over the lifetime of the
111 	 * interface.
112 	 */
113 
114 	char		if_name[IFNAMSIZ];
115 
116 	uint16_t	if_max;		/* largest DHCP packet on this if */
117 	uint16_t	if_min;		/* minimum mtu size on this if */
118 	uint16_t	if_opt;		/* amount of space for options in PKT */
119 
120 	uchar_t		*if_hwaddr;	/* our link-layer address */
121 	uchar_t		if_hwlen;	/* our link-layer address len */
122 	uchar_t		if_hwtype;	/* type of link-layer */
123 
124 	uchar_t		*if_cid;	/* client id, if set in defaults file */
125 	uchar_t		if_cidlen;	/* client id len */
126 
127 	uchar_t		*if_prl;	/* if non-NULL, param request list */
128 	uchar_t		if_prllen;	/* param request list len */
129 
130 		/*
131 		 * the destination address is the broadcast address of
132 		 * the interface, in DLPI terms (which means it
133 		 * includes both a link-layer broadcast address and a
134 		 * sap, and the order isn't consistent.)  fun, huh?
135 		 * blame AT&T.  we store it as a token like this
136 		 * because it's generally how we need to use it.  we
137 		 * can pull it apart using the saplen and sap_before
138 		 * fields below.
139 		 */
140 
141 	uchar_t		*if_daddr;	/* our destination address */
142 	uchar_t		if_dlen;	/* our destination address len */
143 
144 	uchar_t		if_saplen;	/* the SAP len */
145 	uchar_t		if_sap_before;	/* does SAP come before address? */
146 
147 		/*
148 		 * network descriptors; one is used for the DLPI
149 		 * traffic before we have our IP address configured;
150 		 * the other two are used afterwards.  there have to
151 		 * be two socket descriptors since:
152 		 *
153 		 * o  we need one to be bound to IPPORT_BOOTPC and
154 		 *    and INADDR_BROADCAST, so it can receive all
155 		 *    broadcast traffic.  this is if_sock_fd.  it
156 		 *    is also used as a general descriptor to perform
157 		 *    socket-related ioctls on, like SIOCGIFFLAGS.
158 		 *
159 		 * o  we need another to be bound to IPPORT_BOOTPC and
160 		 *    the IP address given to us by the DHCP server,
161 		 *    so we can guarantee the IP address of outgoing
162 		 *    packets when multihomed. (the problem being that
163 		 *    if a packet goes out with the wrong IP address,
164 		 *    then the server's response will come back on the
165 		 *    wrong interface).  this is if_sock_ip_fd.
166 		 *
167 		 * note that if_sock_fd is created in init_ifs() but
168 		 * not bound until dhcp_bound(); this is because we
169 		 * cannot even bind to the broadcast address until we
170 		 * have an IP address.
171 		 *
172 		 * if_sock_ip_fd isn't created until dhcp_bound(),
173 		 * since we don't need it until then and we can't
174 		 * bind it until after we have an IP address anyway.
175 		 *
176 		 * both socket descriptors are closed in reset_ifs().
177 		 */
178 
179 	int		if_dlpi_fd;
180 	int		if_sock_fd;
181 	int		if_sock_ip_fd;
182 
183 	/*
184 	 * the following fields are set when a lease is acquired, and
185 	 * may be updated over the lifetime of the lease.  they are
186 	 * all reset by reset_ifs().
187 	 */
188 
189 	iu_timer_id_t	if_timer[3];	/* T1, T2, and LEASE timers */
190 
191 	lease_t		if_t1;		/* relative renewal start time, hbo */
192 	lease_t		if_t2;		/* relative rebinding start time, hbo */
193 	lease_t		if_lease;	/* relative expire time, hbo */
194 
195 	unsigned int	if_nrouters;	/* the number of default routers */
196 	struct in_addr	*if_routers;	/* an array of default routers */
197 	struct in_addr	if_server;	/* our DHCP server, nbo */
198 
199 	/*
200 	 * while in any states except ADOPTING, INIT, INFORMATION and
201 	 * INFORM_SENT, the following three fields are equal to what
202 	 * we believe the current address, netmask, and broadcast
203 	 * address on the interface to be.  this is so we can detect
204 	 * if the user changes them and abandon the interface.
205 	 */
206 
207 	struct in_addr	if_addr;	/* our IP address, nbo */
208 	struct in_addr	if_netmask;	/* our netmask, nbo */
209 	struct in_addr	if_broadcast;	/* our broadcast address, nbo */
210 
211 	PKT_LIST	*if_ack;	/* ACK from the server */
212 
213 	/*
214 	 * We retain the very first ack obtained on the interface to
215 	 * provide access to options which were originally assigned by
216 	 * the server but may not have been included in subsequent
217 	 * acks, as there are servers which do this and customers have
218 	 * had unsatisfactory results when using our agent with them.
219 	 * ipc_event() in agent.c provides a fallback to the original
220 	 * ack when the current ack doesn't have the information
221 	 * requested.
222 	 */
223 
224 	PKT_LIST	*if_orig_ack;
225 
226 	/*
227 	 * other miscellaneous variables set or needed in the process
228 	 * of acquiring a lease.
229 	 */
230 
231 	int		if_offer_wait;	/* seconds between offers */
232 	iu_event_id_t	if_offer_id;	/* event offer id */
233 	iu_event_id_t	if_acknak_id;	/* event acknak id */
234 	iu_event_id_t	if_acknak_bcast_id;
235 
236 		/*
237 		 * `if_neg_monosec' represents the time since lease
238 		 * acquisition or renewal began, and is used for
239 		 * computing the pkt->secs field.  `if_newstart_monosec'
240 		 * represents the time the ACKed REQUEST was sent,
241 		 * which represents the start time of a new lease.
242 		 * when the lease actually begins (and thus becomes
243 		 * current), `if_curstart_monosec' is set to
244 		 * `if_newstart_monosec'.
245 		 */
246 
247 	monosec_t		if_neg_monosec;
248 	monosec_t		if_newstart_monosec;
249 	monosec_t		if_curstart_monosec;
250 
251 		/*
252 		 * time we sent the DISCOVER relative to if_neg_monosec,
253 		 * so that the REQUEST can have the same pkt->secs.
254 		 */
255 
256 	uint16_t		if_disc_secs;
257 
258 		/*
259 		 * the host name we've been asked to request is remembered
260 		 * here between the DISCOVER and the REQUEST
261 		 */
262 	char			*if_reqhost;
263 
264 	/*
265 	 * this is a chain of packets which have been received on this
266 	 * interface over some interval of time.  the packets may have
267 	 * to meet some criteria in order to be put on this list.  in
268 	 * general, packets are put on this list through recv_pkt()
269 	 */
270 
271 	PKT_LIST		*if_recv_pkt_list;
272 
273 	/*
274 	 * these three fields are initially zero, and get incremented
275 	 * as the ifslist goes from INIT -> BOUND.  if and when the
276 	 * ifslist moves to the RENEWING state, these fields are
277 	 * reset, so they always either indicate the number of packets
278 	 * sent, received, and declined while obtaining the current
279 	 * lease (if BOUND), or the number of packets sent, received,
280 	 * and declined while attempting to obtain a future lease
281 	 * (if any other state).
282 	 */
283 
284 	uint32_t		if_sent;
285 	uint32_t		if_received;
286 	uint32_t		if_bad_offers;
287 
288 	/*
289 	 * if_send_pkt.pkt is dynamically allocated to be as big a
290 	 * packet as we can send out on this interface.  the remainder
291 	 * of this information is needed to make it easy to handle
292 	 * retransmissions.  note that other than if_bad_offers, all
293 	 * of these fields are maintained internally in send_pkt(),
294 	 * and consequently should never need to be modified by any
295 	 * other functions.
296 	 */
297 
298 	dhcp_pkt_t		if_send_pkt;
299 	uint32_t		if_send_timeout;
300 	struct sockaddr_in	if_send_dest;
301 	stop_func_t		*if_send_stop_func;
302 	uint32_t		if_packet_sent;
303 	iu_timer_id_t		if_retrans_timer;
304 
305 	int			if_script_fd;
306 	pid_t			if_script_pid;
307 	pid_t			if_script_helper_pid;
308 	const char		*if_script_event;
309 	iu_event_id_t		if_script_event_id;
310 	const char		*if_callback_msg;
311 	script_callback_t	*if_script_callback;
312 };
313 
314 /*
315  * a word on memory management and ifslists:
316  *
317  * since ifslists are often passed as context to callback functions,
318  * they cannot be freed when the interface they represent is dropped
319  * or released (or when those callbacks finally go off, they will be
320  * hosed).  to handle this situation, ifslists are reference counted.
321  * here are the rules for managing ifslists:
322  *
323  * an ifslist is created through insert_ifs().  along with
324  * initializing the ifslist, this puts a hold on the ifslist through
325  * hold_ifs().
326  *
327  * whenever an ifslist is released or dropped (implicitly or
328  * explicitly), remove_ifs() is called, which sets the DHCP_IF_REMOVED
329  * flag and removes the interface from the internal list of managed
330  * interfaces.  lastly, remove_ifs() calls release_ifs() to remove the
331  * hold acquired in insert_ifs().  if this decrements the hold count
332  * on the interface to zero, then free_ifs() is called.  if there are
333  * holds other than the hold acquired in insert_ifs(), the hold count
334  * will still be > 0, and the interface will remain allocated (though
335  * dormant).
336  *
337  * whenever a callback is scheduled against an ifslist, another hold
338  * must be put on the ifslist through hold_ifs().
339  *
340  * whenever a callback is called back against an ifslist,
341  * release_ifs() must be called to decrement the hold count, which may
342  * end up freeing the ifslist if the hold count becomes zero.
343  *
344  * if release_ifs() returns 0, then there are no remaining holds
345  * against this ifslist, and the ifslist in fact no longer exists.
346  *
347  * since some callbacks may take a long time to get called back (such
348  * as timeout callbacks for lease expiration, etc), it is sometimes
349  * more appropriate to cancel the callbacks and call release_ifs() if
350  * the cancellation succeeds.  this is done in remove_ifs() for the
351  * lease, t1, and t2 callbacks.
352  *
353  * in general, a callback should also call verify_ifs() when it gets
354  * called back in addition to release_ifs(), to make sure that the
355  * interface is still in fact under the dhcpagent's control.  to make
356  * coding simpler, there is a third function, check_ifs(), which
357  * performs both the release_ifs() and the verify_ifs().  in addition,
358  * if check_ifs() detects that the callback has the last hold against
359  * a given interface, it informs it instead of performing the final
360  * release, and thus allows it to clean up appropriately before
361  * performing the final release.
362  */
363 
364 int		canonize_ifs(struct ifslist *);
365 int		check_ifs(struct ifslist *);
366 void		hold_ifs(struct ifslist *);
367 struct ifslist *insert_ifs(const char *, boolean_t, int *);
368 struct ifslist *lookup_ifs(const char *);
369 struct ifslist *lookup_ifs_by_xid(uint32_t);
370 void		nuke_ifslist(boolean_t);
371 void		refresh_ifslist(iu_eh_t *, int, void *);
372 int		release_ifs(struct ifslist *);
373 void		remove_ifs(struct ifslist *);
374 void		reset_ifs(struct ifslist *);
375 int		verify_ifs(struct ifslist *);
376 unsigned int	ifs_count(void);
377 void		cancel_ifs_timers(struct ifslist *);
378 int		schedule_ifs_timer(struct ifslist *, int, uint32_t,
379 		    iu_tq_callback_t *);
380 
381 #ifdef	__cplusplus
382 }
383 #endif
384 
385 #endif	/* INTERFACE_H */
386