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