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