xref: /titanic_52/usr/src/lib/libdhcpagent/common/dhcpagent_ipc.h (revision b6c3f7863936abeae522e48a13887dddeb691a45)
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	_DHCPAGENT_IPC_H
27 #define	_DHCPAGENT_IPC_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/socket.h>
32 #include <net/if.h>		/* IFNAMSIZ */
33 #include <stddef.h>
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <netinet/dhcp.h>
37 #include <dhcp_impl.h>
38 
39 /*
40  * dhcpagent_ipc.[ch] comprise the interface used to perform
41  * interprocess communication with the agent.  see dhcpagent_ipc.c for
42  * documentation on how to use the exported functions.
43  */
44 
45 #ifdef	__cplusplus
46 extern "C" {
47 #endif
48 
49 #define	DHCP_AGENT_PATH		"/sbin/dhcpagent"
50 #define	DHCP_IPC_LISTEN_BACKLOG	30
51 #define	IPPORT_DHCPAGENT	4999
52 #define	DHCP_IPC_MAX_WAIT	15	/* max seconds to wait to start agent */
53 
54 /*
55  * return values which should be used by programs which talk to the
56  * agent (for uniformity).
57  */
58 
59 #define	DHCP_EXIT_SUCCESS	0
60 #define	DHCP_EXIT_FAILURE	2
61 #define	DHCP_EXIT_BADARGS	3
62 #define	DHCP_EXIT_TIMEOUT	4
63 #define	DHCP_EXIT_SYSTEM	6
64 
65 /*
66  * opaque types for requests and replies.  users of this api do not
67  * need to understand their contents.
68  */
69 
70 typedef struct dhcp_ipc_request dhcp_ipc_request_t;
71 typedef struct dhcp_ipc_reply   dhcp_ipc_reply_t;
72 
73 /* payloads that can be passed in a request or reply */
74 
75 typedef enum {
76 	DHCP_TYPE_OPTION,
77 	DHCP_TYPE_STATUS,
78 	DHCP_TYPE_OPTNUM,
79 	DHCP_TYPE_NONE
80 } dhcp_data_type_t;
81 
82 /*
83  * requests that can be sent to the agent
84  *
85  * code in dhcpagent relies on the numeric values of these
86  * requests -- but there's no sane reason to change them anyway.
87  *
88  * If any commands are changed, added, or removed, see the ipc_typestr[]
89  * array in dhcpagent_ipc.c.
90  */
91 
92 typedef enum {
93 	DHCP_DROP,	DHCP_EXTEND,  DHCP_PING,    DHCP_RELEASE,
94 	DHCP_START,  	DHCP_STATUS,  DHCP_INFORM,  DHCP_GET_TAG,
95 	DHCP_NIPC,	/* number of supported requests */
96 	DHCP_PRIMARY = 0x100,
97 	DHCP_V6 = 0x200
98 } dhcp_ipc_type_t;
99 
100 /* structure passed with the DHCP_GET_TAG request */
101 
102 typedef struct {
103 	uint_t		category;
104 	uint_t		code;
105 	uint_t		size;
106 } dhcp_optnum_t;
107 
108 #define	DHCP_IPC_CMD(type)	((type) & 0x00ff)
109 #define	DHCP_IPC_FLAGS(type)	((type) & 0xff00)
110 
111 /* special timeout values for dhcp_ipc_make_request() */
112 
113 #define	DHCP_IPC_WAIT_FOREVER	(-1)
114 #define	DHCP_IPC_WAIT_DEFAULT	(-2)
115 
116 /*
117  * errors that can be returned from the provided functions.
118  * note: keep in sync with dhcp_ipc_strerror()
119  */
120 
121 enum {
122 	/* System call errors must be kept contiguous */
123 	DHCP_IPC_SUCCESS,	DHCP_IPC_E_SOCKET,	DHCP_IPC_E_FCNTL,
124 	DHCP_IPC_E_READ,	DHCP_IPC_E_ACCEPT,	DHCP_IPC_E_CLOSE,
125 	DHCP_IPC_E_BIND,	DHCP_IPC_E_LISTEN,	DHCP_IPC_E_MEMORY,
126 	DHCP_IPC_E_CONNECT,	DHCP_IPC_E_WRITEV,	DHCP_IPC_E_POLL,
127 
128 	/* All others follow */
129 	DHCP_IPC_E_TIMEOUT,	DHCP_IPC_E_SRVFAILED,	DHCP_IPC_E_EOF,
130 	DHCP_IPC_E_INVIF,	DHCP_IPC_E_INT,		DHCP_IPC_E_PERM,
131 	DHCP_IPC_E_OUTSTATE,	DHCP_IPC_E_PEND,	DHCP_IPC_E_BOOTP,
132 	DHCP_IPC_E_CMD_UNKNOWN, DHCP_IPC_E_UNKIF,	DHCP_IPC_E_PROTO,
133 	DHCP_IPC_E_FAILEDIF,	DHCP_IPC_E_NOPRIMARY,	DHCP_IPC_E_DOWNIF,
134 	DHCP_IPC_E_NOIPIF,	DHCP_IPC_E_NOVALUE,	DHCP_IPC_E_RUNNING
135 };
136 
137 /*
138  * low-level public dhcpagent ipc functions -- these are for use by
139  * programs that need to communicate with the dhcpagent.  these will
140  * remain relatively stable.
141  */
142 
143 extern const char	*dhcp_ipc_strerror(int);
144 extern dhcp_ipc_request_t *dhcp_ipc_alloc_request(dhcp_ipc_type_t, const char *,
145 			    const void *, uint32_t, dhcp_data_type_t);
146 extern void		*dhcp_ipc_get_data(dhcp_ipc_reply_t *, size_t *,
147 			    dhcp_data_type_t *);
148 extern int		dhcp_ipc_make_request(dhcp_ipc_request_t *,
149 			    dhcp_ipc_reply_t **, int32_t);
150 extern const char	*dhcp_ipc_type_to_string(dhcp_ipc_type_t);
151 
152 /*
153  * high-level public dhcpagent ipc functions
154  */
155 
156 extern int		dhcp_ipc_getinfo(dhcp_optnum_t *, DHCP_OPT **, int32_t);
157 
158 /*
159  * private dhcpagent ipc "server side" functions -- these are only for
160  * use by dhcpagent(1M) and are subject to change.
161  */
162 
163 extern int		dhcp_ipc_init(int *);
164 extern int		dhcp_ipc_accept(int, int *, int *);
165 extern int		dhcp_ipc_recv_request(int, dhcp_ipc_request_t **, int);
166 extern dhcp_ipc_reply_t	*dhcp_ipc_alloc_reply(dhcp_ipc_request_t *, int,
167 			    const void *, uint32_t, dhcp_data_type_t);
168 extern int		dhcp_ipc_send_reply(int, dhcp_ipc_reply_t *);
169 extern int		dhcp_ipc_close(int);
170 
171 /*
172  * values for if_state in the dhcp_status_t
173  *
174  * code in this library and dhcpagent rely on the numeric values of these
175  * requests -- but there's no sane reason to change them anyway.
176  */
177 
178 typedef enum {
179 	INIT,				/* nothing done yet */
180 	SELECTING,			/* sent DISCOVER, waiting for OFFERs */
181 	REQUESTING,			/* sent REQUEST, waiting for ACK/NAK */
182 	PRE_BOUND,			/* have ACK, setting up interface */
183 	BOUND,				/* have a valid lease */
184 	RENEWING,			/* have lease, but trying to renew */
185 	REBINDING,			/* have lease, but trying to rebind */
186 	INFORMATION,			/* sent INFORM, received ACK */
187 	INIT_REBOOT,			/* attempt to use cached ACK/Reply */
188 	ADOPTING,			/* attempting to adopt */
189 	INFORM_SENT,			/* sent INFORM, awaiting ACK */
190 	DECLINING,			/* sent v6 Decline, awaiting Reply */
191 	RELEASING,			/* sent v6 Release, awaiting Reply */
192 	DHCP_NSTATES			/* total number of states */
193 } DHCPSTATE;
194 
195 /* values for if_dflags in the dhcp_status_t */
196 
197 #define	DHCP_IF_PRIMARY		0x0100	/* interface is primary interface */
198 #define	DHCP_IF_BUSY		0x0200	/* asynchronous command pending */
199 #define	DHCP_IF_BOOTP		0x0400	/* interface is using bootp */
200 #define	DHCP_IF_REMOVED		0x0800	/* interface is going away */
201 #define	DHCP_IF_FAILED		0x1000	/* interface configuration problem */
202 #define	DHCP_IF_V6		0x2000	/* DHCPv6 interface */
203 
204 /*
205  * structure passed with the DHCP_STATUS replies
206  *
207  * when parsing a dhcp_status_t, `version' should always be checked
208  * if there is a need to access any fields which were not defined in
209  * version 1 of this structure.
210  *
211  * as new fields are added to the dhcp_status_t, they should be
212  * appended to the structure and the version number incremented.
213  */
214 
215 typedef struct dhcp_status {
216 	uint8_t		version;	/* version of this structure */
217 
218 	char		if_name[IFNAMSIZ];
219 	DHCPSTATE	if_state;	/* state of interface; see above */
220 
221 	time_t		if_began;	/* time lease began (absolute) */
222 	time_t		if_t1;		/* renewing time (absolute) */
223 	time_t		if_t2;		/* rebinding time (absolute) */
224 	time_t		if_lease;	/* lease expiration time (absolute) */
225 
226 	uint16_t	if_dflags;	/* DHCP flags on this if; see above */
227 
228 	/*
229 	 * these three fields are initially zero, and get incremented
230 	 * as if_state goes from INIT -> BOUND (or INIT ->
231 	 * INFORMATION).  if and when the interface moves to the
232 	 * RENEWING state, these fields are reset, so they always
233 	 * either indicate the number of packets sent, received, and
234 	 * declined while obtaining the current lease (if BOUND), or
235 	 * the number of packets sent, received, and declined while
236 	 * attempting to obtain a future lease (if any other state).
237 	 */
238 
239 	uint32_t	if_sent;
240 	uint32_t	if_recv;
241 	uint32_t	if_bad_offers;
242 } dhcp_status_t;
243 
244 #define	DHCP_STATUS_VER		1	/* current version of dhcp_status_t */
245 #define	DHCP_STATUS_VER1_SIZE	(offsetof(dhcp_status_t, if_bad_offers) + \
246 				    sizeof (uint32_t))
247 
248 /*
249  * the remainder of this file contains implementation-specific
250  * artifacts which may change. note that a `dhcp_ipc_request_t' and a
251  * `dhcp_ipc_reply_t' are incomplete types as far as consumers of this
252  * api are concerned.  use these details at your own risk.
253  */
254 
255 typedef hrtime_t dhcp_ipc_id_t;
256 
257 /*
258  * note: the first 4 fields of the dhcp_ipc_request_t and dhcp_ipc_reply_t
259  *	 are intentionally identical; code in dhcpagent_ipc.c counts on it!
260  */
261 
262 struct	dhcp_ipc_request {
263 	dhcp_ipc_type_t  message_type;	/* type of request */
264 	dhcp_ipc_id_t	 ipc_id;	/* per-socket unique request id */
265 	dhcp_data_type_t data_type;	/* type of payload */
266 	uint32_t	 data_length;	/* size of actual data in the buffer */
267 	char		 ifname[IFNAMSIZ];
268 	int32_t		 timeout;	/* timeout in seconds */
269 	uchar_t		 buffer[1];	/* dynamically extended */
270 };
271 
272 struct	dhcp_ipc_reply {
273 	dhcp_ipc_type_t	 message_type;	/* same message type as request */
274 	dhcp_ipc_id_t	 ipc_id;	/* same id as request */
275 	dhcp_data_type_t data_type;	/* type of payload */
276 	uint32_t	 data_length;	/* size of actual data in the buffer */
277 	uint32_t	 return_code;	/* did the request succeed? */
278 	uchar_t		 buffer[1];	/* dynamically extended */
279 };
280 
281 /*
282  * since ansi c won't let us define arrays with 0 elements, the
283  * size of the ipc request/reply structures is off-by-1; use macros.
284  */
285 
286 #define	DHCP_IPC_REPLY_SIZE	(sizeof (dhcp_ipc_reply_t) - 1)
287 #define	DHCP_IPC_REQUEST_SIZE	(sizeof (dhcp_ipc_request_t) - 1)
288 
289 #define	DHCP_IPC_DEFAULT_WAIT	120	/* seconds */
290 
291 #ifdef	__cplusplus
292 }
293 #endif
294 
295 #endif	/* _DHCPAGENT_IPC_H */
296