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