xref: /titanic_41/usr/src/cmd/cmd-inet/sbin/dhcpagent/states.h (revision 1915be19b7a1f7c113b706bd54d6ca393e8d8107)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	STATES_H
27 #define	STATES_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <netinet/dhcp.h>
34 #include <libinetutil.h>
35 
36 #include "common.h"
37 #include "ipc_action.h"
38 #include "async.h"
39 #include "packet.h"
40 #include "util.h"
41 
42 /*
43  * interfaces for state transition/action functions.  these functions
44  * can be found in suitably named .c files, such as inform.c, select.c,
45  * renew.c, etc.
46  */
47 
48 #ifdef	__cplusplus
49 extern "C" {
50 #endif
51 
52 /*
53  * DHCP state machine representation: includes all of the information used for
54  * a state machine instance.  For IPv4, this represents a single logical
55  * interface and (usually) a leased address.  For IPv6, it represents a
56  * DUID+IAID combination.  Note that if multiple DUID+IAID instances are one
57  * day allowed per interface, this will need to become a list.
58  */
59 struct dhcp_smach_s {
60 	dhcp_smach_t	*dsm_next;	/* Note: must be first */
61 	dhcp_smach_t	*dsm_prev;
62 
63 	/*
64 	 * The name of the state machine.  This is currently just a pointer to
65 	 * the controlling LIF's name, but could be otherwise.
66 	 */
67 	const char	*dsm_name;
68 	dhcp_lif_t	*dsm_lif;	/* Controlling LIF */
69 	uint_t		dsm_hold_count;	/* reference count */
70 
71 	dhcp_lease_t	*dsm_leases;	/* List of leases */
72 	uint_t		dsm_lif_wait;	/* LIFs waiting on DAD */
73 	uint_t		dsm_lif_down;	/* LIFs failed */
74 
75 	/*
76 	 * each state machine can have at most one pending asynchronous
77 	 * action, which is represented in a `struct async_action'.
78 	 * if that asynchronous action was a result of a user request,
79 	 * then the `struct ipc_action' is used to hold information
80 	 * about the user request.  these structures are opaque to
81 	 * users of the ifslist, and the functional interfaces
82 	 * provided in async.[ch] and ipc_action.[ch] should be used
83 	 * to maintain them.
84 	 */
85 
86 	ipc_action_t	dsm_ia;
87 	async_action_t	dsm_async;
88 
89 	uchar_t		*dsm_cid;	/* client id */
90 	uchar_t		dsm_cidlen;	/* client id len */
91 
92 	/*
93 	 * current state of the machine
94 	 */
95 
96 	DHCPSTATE	dsm_state;
97 
98 	uint16_t	dsm_dflags;	/* DHCP_IF_* (shared with IPC) */
99 
100 	uint16_t	*dsm_prl;	/* if non-NULL, param request list */
101 	uint_t		dsm_prllen;	/* param request list len */
102 
103 	uint_t		dsm_nrouters;	/* the number of default routers */
104 	struct in_addr	*dsm_routers;	/* an array of default routers */
105 
106 	in6_addr_t	dsm_server;	/* our DHCP server */
107 	uchar_t		*dsm_serverid;	/* server DUID for v6 */
108 	uint_t		dsm_serveridlen; /* DUID length */
109 
110 	/*
111 	 * We retain the very first ack obtained on the state machine to
112 	 * provide access to options which were originally assigned by
113 	 * the server but may not have been included in subsequent
114 	 * acks, as there are servers which do this and customers have
115 	 * had unsatisfactory results when using our agent with them.
116 	 * ipc_event() in agent.c provides a fallback to the original
117 	 * ack when the current ack doesn't have the information
118 	 * requested.
119 	 *
120 	 * Note that neither of these is actually a list of packets.  There's
121 	 * exactly one packet here, so use free_pkt_entry.
122 	 */
123 	PKT_LIST	*dsm_ack;
124 	PKT_LIST	*dsm_orig_ack;
125 
126 	/*
127 	 * other miscellaneous variables set or needed in the process
128 	 * of acquiring a lease.
129 	 */
130 
131 	int		dsm_offer_wait;	/* seconds between sending offers */
132 	iu_timer_id_t	dsm_offer_timer; /* timer associated with offer wait */
133 
134 	/*
135 	 * time we sent the DISCOVER relative to dsm_neg_hrtime, so that the
136 	 * REQUEST can have the same pkt->secs.
137 	 */
138 
139 	uint16_t	dsm_disc_secs;
140 
141 	/*
142 	 * this is a chain of packets which have been received on this
143 	 * state machine over some interval of time.  the packets may have
144 	 * to meet some criteria in order to be put on this list.  in
145 	 * general, packets are put on this list through recv_pkt()
146 	 */
147 
148 	PKT_LIST	*dsm_recv_pkt_list;
149 
150 	/*
151 	 * these three fields are initially zero, and get incremented
152 	 * as the ifslist goes from INIT -> BOUND.  if and when the
153 	 * ifslist moves to the RENEWING state, these fields are
154 	 * reset, so they always either indicate the number of packets
155 	 * sent, received, and declined while obtaining the current
156 	 * lease (if BOUND), or the number of packets sent, received,
157 	 * and declined while attempting to obtain a future lease
158 	 * (if any other state).
159 	 */
160 
161 	uint32_t	dsm_sent;
162 	uint32_t	dsm_received;
163 	uint32_t	dsm_bad_offers;
164 
165 	/*
166 	 * dsm_send_pkt.pkt is dynamically allocated to be as big a
167 	 * packet as we can send out on this state machine.  the remainder
168 	 * of this information is needed to make it easy to handle
169 	 * retransmissions.  note that other than dsm_bad_offers, all
170 	 * of these fields are maintained internally in send_pkt(),
171 	 * and consequently should never need to be modified by any
172 	 * other functions.
173 	 */
174 
175 	dhcp_pkt_t	dsm_send_pkt;
176 	union {
177 		struct sockaddr_in v4;
178 		struct sockaddr_in6 v6;
179 	} dsm_send_dest;
180 
181 	/*
182 	 * For v4, dsm_send_tcenter is used to track the central timer value in
183 	 * milliseconds (4000, 8000, 16000, 32000, 64000), and dsm_send_timeout
184 	 * is that value plus the +/- 1000 millisecond fuzz.
185 	 *
186 	 * For v6, dsm_send_tcenter is the MRT (maximum retransmit timer)
187 	 * value, and dsm_send_timeout must be set to the IRT (initial
188 	 * retransmit timer) value by the sender.
189 	 */
190 	uint_t		dsm_send_timeout;
191 	uint_t		dsm_send_tcenter;
192 	stop_func_t	*dsm_send_stop_func;
193 	uint32_t	dsm_packet_sent;
194 	iu_timer_id_t	dsm_retrans_timer;
195 
196 	/*
197 	 * The host name we've been asked to request is remembered
198 	 * here between the DISCOVER and the REQUEST.  (v4 only)
199 	 */
200 	char		*dsm_reqhost;
201 
202 	/*
203 	 * V4 and V6 use slightly different timers.  For v4, we must count
204 	 * seconds from the point where we first try to configure the
205 	 * interface.  For v6, only seconds while performing a transaction
206 	 * matter.
207 	 *
208 	 * In v4, `dsm_neg_hrtime' represents the time since DHCP started
209 	 * configuring the interface, and is used for computing the pkt->secs
210 	 * field in v4.  In v6, it represents the time since the current
211 	 * transaction (if any) was started, and is used for the ELAPSED_TIME
212 	 * option.
213 	 *
214 	 * `dsm_newstart_monosec' represents the time the ACKed REQUEST was
215 	 * sent, which represents the start time of a new batch of leases.
216 	 * When the lease time actually begins (and thus becomes current),
217 	 * `dsm_curstart_monosec' is set to `dsm_newstart_monosec'.
218 	 */
219 	hrtime_t	dsm_neg_hrtime;
220 	monosec_t	dsm_newstart_monosec;
221 	monosec_t	dsm_curstart_monosec;
222 
223 	int		dsm_script_fd;
224 	pid_t		dsm_script_pid;
225 	pid_t		dsm_script_helper_pid;
226 	const char	*dsm_script_event;
227 	iu_event_id_t	dsm_script_event_id;
228 	void		*dsm_callback_arg;
229 	script_callback_t *dsm_script_callback;
230 
231 	iu_timer_id_t	dsm_start_timer;
232 };
233 
234 #define	dsm_isv6	dsm_lif->lif_pif->pif_isv6
235 #define	dsm_hwtype	dsm_lif->lif_pif->pif_hwtype
236 
237 struct dhcp_lease_s {
238 	dhcp_lease_t	*dl_next;	/* Note: must be first */
239 	dhcp_lease_t	*dl_prev;
240 
241 	dhcp_smach_t	*dl_smach;	/* back pointer to state machine */
242 	dhcp_lif_t	*dl_lifs;	/* LIFs configured by this lease */
243 	uint_t		dl_nlifs;	/* Number of configured LIFs */
244 	uint_t		dl_hold_count;	/* reference counter */
245 	boolean_t	dl_removed;	/* Set if removed from list */
246 	boolean_t	dl_stale;	/* not updated by Renew/bind */
247 
248 	/*
249 	 * the following fields are set when a lease is acquired, and
250 	 * may be updated over the lifetime of the lease.  they are
251 	 * all reset by reset_smach().
252 	 */
253 
254 	dhcp_timer_t	dl_t1;		/* relative renewal start time, hbo */
255 	dhcp_timer_t	dl_t2;		/* relative rebinding start time, hbo */
256 };
257 
258 /* The IU event callback functions */
259 iu_eh_callback_t	dhcp_acknak_global;
260 iu_eh_callback_t	dhcp_packet_lif;
261 
262 /* Common state-machine related routines throughout dhcpagent */
263 boolean_t	dhcp_adopt(void);
264 void		dhcp_adopt_complete(dhcp_smach_t *);
265 boolean_t	dhcp_bound(dhcp_smach_t *, PKT_LIST *);
266 void		dhcp_bound_complete(dhcp_smach_t *);
267 int		dhcp_drop(dhcp_smach_t *, void *);
268 void		dhcp_deprecate(iu_tq_t *, void *);
269 void		dhcp_expire(iu_tq_t *, void *);
270 boolean_t	dhcp_extending(dhcp_smach_t *);
271 void		dhcp_inform(dhcp_smach_t *);
272 void		dhcp_init_reboot(dhcp_smach_t *);
273 void		dhcp_rebind(iu_tq_t *, void *);
274 int		dhcp_release(dhcp_smach_t *, void *);
275 void		dhcp_renew(iu_tq_t *, void *);
276 void		dhcp_requesting(iu_tq_t *, void *);
277 void		dhcp_restart(dhcp_smach_t *);
278 void		dhcp_selecting(dhcp_smach_t *);
279 boolean_t	set_start_timer(dhcp_smach_t *);
280 void		send_declines(dhcp_smach_t *);
281 void		send_v6_request(dhcp_smach_t *);
282 boolean_t	save_server_id(dhcp_smach_t *, PKT_LIST *);
283 void		server_unicast_option(dhcp_smach_t *, PKT_LIST *);
284 
285 /* State machine support functions in states.c */
286 dhcp_smach_t	*insert_smach(dhcp_lif_t *, int *);
287 void		hold_smach(dhcp_smach_t *);
288 void		release_smach(dhcp_smach_t *);
289 void		remove_smach(dhcp_smach_t *);
290 dhcp_smach_t	*next_smach(dhcp_smach_t *, boolean_t);
291 dhcp_smach_t	*primary_smach(boolean_t);
292 void		make_primary(dhcp_smach_t *);
293 dhcp_smach_t	*lookup_smach(const char *, boolean_t);
294 dhcp_smach_t	*lookup_smach_by_uindex(uint16_t, dhcp_smach_t *, boolean_t);
295 dhcp_smach_t	*lookup_smach_by_xid(uint32_t, dhcp_smach_t *, boolean_t);
296 dhcp_smach_t	*lookup_smach_by_event(iu_event_id_t);
297 void		finished_smach(dhcp_smach_t *, int);
298 boolean_t	set_smach_state(dhcp_smach_t *, DHCPSTATE);
299 int		get_smach_cid(dhcp_smach_t *);
300 boolean_t	verify_smach(dhcp_smach_t *);
301 uint_t		smach_count(void);
302 void		reset_smach(dhcp_smach_t *);
303 void		refresh_smachs(iu_eh_t *, int, void *);
304 void		refresh_smach(dhcp_smach_t *);
305 void		nuke_smach_list(void);
306 boolean_t	schedule_smach_timer(dhcp_smach_t *, int, uint32_t,
307 		    iu_tq_callback_t *);
308 void		cancel_offer_timer(dhcp_smach_t *);
309 void		discard_default_routes(dhcp_smach_t *);
310 void		remove_default_routes(dhcp_smach_t *);
311 boolean_t	is_bound_state(DHCPSTATE);
312 
313 /* Lease-related support functions in states.c */
314 dhcp_lease_t	*insert_lease(dhcp_smach_t *);
315 void		hold_lease(dhcp_lease_t *);
316 void		release_lease(dhcp_lease_t *);
317 void		remove_lease(dhcp_lease_t *);
318 void		deprecate_leases(dhcp_smach_t *);
319 void		cancel_lease_timers(dhcp_lease_t *);
320 boolean_t	schedule_lease_timer(dhcp_lease_t *, dhcp_timer_t *,
321 		    iu_tq_callback_t *);
322 
323 #ifdef	__cplusplus
324 }
325 #endif
326 
327 #endif	/* STATES_H */
328