xref: /titanic_41/usr/src/cmd/cmd-inet/sbin/dhcpagent/interface.h (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	INTERFACE_H
27 #define	INTERFACE_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * interface.[ch] encapsulate all of the agent's knowledge of network
33  * interfaces from the DHCP agent's perspective.  see interface.c
34  * for documentation on how to use the exported functions.  note that
35  * there are not functional interfaces for manipulating all of the fields
36  * in an ifslist -- please read the comments in the ifslist structure
37  * definition below for the rules on accessing various fields.
38  */
39 
40 #ifdef	__cplusplus
41 extern "C" {
42 #endif
43 
44 #include <netinet/in.h>
45 #include <sys/socket.h>
46 #include <net/if.h>			/* IFNAMSIZ */
47 #include <sys/types.h>
48 #include <netinet/dhcp.h>
49 #include <dhcpagent_ipc.h>
50 #include <libinetutil.h>
51 
52 #include "async.h"
53 #include "agent.h"
54 #include "dlpi_io.h"
55 #include "ipc_action.h"
56 #include "packet.h"
57 #include "util.h"
58 
59 enum { DHCP_T1_TIMER, DHCP_T2_TIMER, DHCP_LEASE_TIMER };
60 
61 typedef int script_callback_t (struct ifslist *, const char *);
62 
63 struct ifslist {
64 
65 	/*
66 	 * ifslist chain pointers, maintained by insert_ifs() /
67 	 * remove_ifs().
68 	 */
69 
70 	struct ifslist		*next;
71 	struct ifslist		*prev;
72 
73 	/*
74 	 * hold count on this ifslist, maintained by hold_ifs() /
75 	 * release_ifs() -- see below for a discussion of ifs memory
76 	 * management.
77 	 */
78 
79 	uchar_t			if_hold_count;
80 
81 	/*
82 	 * each interface can have at most one pending asynchronous
83 	 * action, which is represented in a `struct async_action'.
84 	 * if that asynchronous action was a result of a user request,
85 	 * then the `struct ipc_action' is used to hold information
86 	 * about the user request.  these structures are opaque to
87 	 * users of the ifslist, and the functional interfaces
88 	 * provided in async.[ch] and ipc_action.[ch] should be used
89 	 * to maintain them.
90 	 */
91 
92 	struct ipc_action	if_ia;
93 	struct async_action	if_async;
94 
95 	/*
96 	 * current state of the interface
97 	 */
98 
99 	DHCPSTATE		if_state;
100 
101 	/*
102 	 * flags specific to DHCP (see dhcpagent_ipc.h)
103 	 */
104 
105 	uint16_t		if_dflags;
106 
107 	/*
108 	 * general interface information -- this information is initialized
109 	 * in insert_ifs() and does not change over the lifetime of the
110 	 * interface.
111 	 */
112 
113 	char		if_name[IFNAMSIZ];
114 
115 	uint16_t	if_max;		/* largest DHCP packet on this if */
116 	uint16_t	if_min;		/* minimum mtu size on this if */
117 	uint16_t	if_opt;		/* amount of space for options in PKT */
118 
119 	uchar_t		*if_hwaddr;	/* our link-layer address */
120 	uchar_t		if_hwlen;	/* our link-layer address len */
121 	uchar_t		if_hwtype;	/* type of link-layer */
122 
123 	uchar_t		*if_cid;	/* client id, if set in defaults file */
124 	uchar_t		if_cidlen;	/* client id len */
125 
126 	uchar_t		*if_prl;	/* if non-NULL, param request list */
127 	uchar_t		if_prllen;	/* param request list len */
128 
129 		/*
130 		 * the destination address is the broadcast address of
131 		 * the interface, in DLPI terms (which means it
132 		 * includes both a link-layer broadcast address and a
133 		 * sap, and the order isn't consistent.)  fun, huh?
134 		 * blame AT&T.  we store it as a token like this
135 		 * because it's generally how we need to use it.  we
136 		 * can pull it apart using the saplen and sap_before
137 		 * fields below.
138 		 */
139 
140 	uchar_t		*if_daddr;	/* our destination address */
141 	uchar_t		if_dlen;	/* our destination address len */
142 
143 	uchar_t		if_saplen;	/* the SAP len */
144 	uchar_t		if_sap_before;	/* does SAP come before address? */
145 
146 		/*
147 		 * network descriptors; one is used for the DLPI
148 		 * traffic before we have our IP address configured;
149 		 * the other two are used afterwards.  there have to
150 		 * be two socket descriptors since:
151 		 *
152 		 * o  we need one to be bound to IPPORT_BOOTPC and
153 		 *    and INADDR_BROADCAST, so it can receive all
154 		 *    broadcast traffic.  this is if_sock_fd.  it
155 		 *    is also used as a general descriptor to perform
156 		 *    socket-related ioctls on, like SIOCGIFFLAGS.
157 		 *
158 		 * o  we need another to be bound to IPPORT_BOOTPC and
159 		 *    the IP address given to us by the DHCP server,
160 		 *    so we can guarantee the IP address of outgoing
161 		 *    packets when multihomed. (the problem being that
162 		 *    if a packet goes out with the wrong IP address,
163 		 *    then the server's response will come back on the
164 		 *    wrong interface).  this is if_sock_ip_fd.
165 		 *
166 		 * note that if_sock_fd is created in init_ifs() but
167 		 * not bound until dhcp_bound(); this is because we
168 		 * cannot even bind to the broadcast address until we
169 		 * have an IP address.
170 		 *
171 		 * if_sock_ip_fd isn't created until dhcp_bound(),
172 		 * since we don't need it until then and we can't
173 		 * bind it until after we have an IP address anyway.
174 		 *
175 		 * both socket descriptors are closed in reset_ifs().
176 		 */
177 
178 	int		if_dlpi_fd;
179 	int		if_sock_fd;
180 	int		if_sock_ip_fd;
181 
182 	/*
183 	 * the following fields are set when a lease is acquired, and
184 	 * may be updated over the lifetime of the lease.  they are
185 	 * all reset by reset_ifs().
186 	 */
187 
188 	iu_timer_id_t	if_timer[3];	/* T1, T2, and LEASE timers */
189 
190 	lease_t		if_t1;		/* relative renewal start time, hbo */
191 	lease_t		if_t2;		/* relative rebinding start time, hbo */
192 	lease_t		if_lease;	/* relative expire time, hbo */
193 
194 	unsigned int	if_nrouters;	/* the number of default routers */
195 	struct in_addr	*if_routers;	/* an array of default routers */
196 	struct in_addr	if_server;	/* our DHCP server, nbo */
197 
198 	/*
199 	 * while in any states except ADOPTING, INIT, INFORMATION and
200 	 * INFORM_SENT, the following three fields are equal to what
201 	 * we believe the current address, netmask, and broadcast
202 	 * address on the interface to be.  this is so we can detect
203 	 * if the user changes them and abandon the interface.
204 	 */
205 
206 	struct in_addr	if_addr;	/* our IP address, nbo */
207 	struct in_addr	if_netmask;	/* our netmask, nbo */
208 	struct in_addr	if_broadcast;	/* our broadcast address, nbo */
209 
210 	PKT_LIST	*if_ack;	/* ACK from the server */
211 
212 	/*
213 	 * We retain the very first ack obtained on the interface to
214 	 * provide access to options which were originally assigned by
215 	 * the server but may not have been included in subsequent
216 	 * acks, as there are servers which do this and customers have
217 	 * had unsatisfactory results when using our agent with them.
218 	 * ipc_event() in agent.c provides a fallback to the original
219 	 * ack when the current ack doesn't have the information
220 	 * requested.
221 	 */
222 
223 	PKT_LIST	*if_orig_ack;
224 
225 	/*
226 	 * other miscellaneous variables set or needed in the process
227 	 * of acquiring a lease.
228 	 */
229 
230 	int		if_offer_wait;	/* seconds between sending offers */
231 	iu_timer_id_t	if_offer_timer;	/* timer associated with offer wait */
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