1 /*
2  * libdhcpcd
3  * Copyright 2009-2015 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef DHCPCD_H
28 #define DHCPCD_H
29 
30 #include <net/if.h>
31 #include <netinet/in.h>
32 
33 #include <stdbool.h>
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 #ifndef DHCPCD_SOCKET
40 #define DHCPCD_SOCKET		"/var/run/dhcpcd/sock"
41 #endif
42 #ifndef DHCPCD_UNPRIV_SOCKET
43 #define DHCPCD_UNPRIV_SOCKET	"/var/run/dhcpcd/unpriv.sock"
44 #endif
45 #ifndef DHCPCD_OSOCKET
46 #define DHCPCD_OSOCKET		"/var/run/dhcpcd.sock"
47 #endif
48 #ifndef DHCPCD_UNPRIV_OSOCKET
49 #define DHCPCD_UNPRIV_OSOCKET	"/var/run/dhcpcd.unpriv.sock"
50 #endif
51 
52 #ifndef WPA_CTRL_DIR
53 #define WPA_CTRL_DIR		"/var/run/wpa_supplicant"
54 #endif
55 
56 #ifndef DHCPCD_TMP_DIR
57 #define DHCPCD_TMP_DIR		"/tmp/dhcpcd"
58 #endif
59 #ifndef DHCPCD_TMP_DIR_PERM
60 #define DHCPCD_TMP_DIR_PERM	0700
61 #endif
62 
63 #define DHCPCD_RETRYOPEN	100	/* milliseconds */
64 #define DHCPCD_WPA_PING		500	/* milliseconds */
65 #define DHCPCD_WPA_SCAN_LONG	60000	/* milliseconds */
66 #define DHCPCD_WPA_SCAN_SHORT	5000	/* milliseconds */
67 #define DHCPCD_WI_HIST_MAX	10	/* Recall 10 scans for averages */
68 
69 /* Each non printable byte of the SSID is represented as \000 */
70 #define IF_SSIDSIZE		((32 * 4) + 1)
71 #define IF_BSSIDSIZE		64
72 #define FLAGSIZE		64
73 #define TYPESIZE		8
74 #define REASONSIZE		16
75 
76 #define WPA_FREQ_IS_2G(f)	((f) >= 2402 && (f) <= 2472)
77 #define WPA_FREQ_IS_5G(f)	((f) >= 5170 && (f) <= 5835)
78 
79 #define DHC_UNKNOWN		 0
80 #define DHC_DOWN		 1
81 #define DHC_OPENED		 2
82 #define DHC_INITIALISED		 3
83 #define DHC_DISCONNECTED	 4
84 #define DHC_CONNECTING		 5
85 #define DHC_AF_WAITING		 6
86 #define DHC_CONNECTED		 7
87 #define DHC_MAX			 8
88 extern const char * const dhcpcd_cstates[];
89 
90 #define DHT_UNKNOWN		 0
91 #define DHT_LINK		 1
92 #define DHT_DHCP		 2
93 #define DHT_IPV4		 2
94 #define DHT_IPV4LL		 3
95 #define DHT_IPV6		 4
96 #define DHT_RA			 5
97 #define DHT_DHCP6		 6
98 #define DHT_MAX			 7
99 
100 #define DHS_UNKNOWN		 0
101 #define DHS_DUMP		 1
102 #define DHS_TEST		 2
103 #define DHS_STOPPED		 3
104 #define DHS_FAIL		 4
105 #define DHS_STOP		 5
106 #define DHS_PREINIT		 6
107 #define DHS_DEPARTED		 7
108 #define DHS_NOCARRIER		 8
109 #define DHS_NAK			 9
110 #define DHS_EXPIRE		10
111 #define DHS_RECONFIGURE		11
112 #define DHS_CARRIER		12
113 #define DHS_STATIC		13
114 #define DHS_3RDPARTY		14
115 #define DHS_IPV4LL		15
116 #define DHS_INFORM		16
117 #define DHS_BOUND		17
118 #define DHS_RENEW		18
119 #define DHS_REBIND		19
120 #define	DHS_REBOOT		20
121 #define DHS_ROUTERADVERT	21
122 #define DHS_DELEGATED		22
123 #define DHS_NOCARRIER_ROAMING	23
124 #define DHR_MAX			24
125 
126 typedef struct dhcpcd_wi_avs {
127 	int value;
128 	int average;
129 } DHCPCD_WI_AV;
130 
131 typedef struct dhcpcd_wi_scan {
132 	struct dhcpcd_wi_scan *next;
133 	char bssid[IF_BSSIDSIZE];
134 	unsigned int flags;
135 #define WSF_SECURE		0x001
136 #define WSF_PSK			0x002
137 #define WSF_WEP			0x010
138 #define WSF_WPA			0x020
139 #define WSF_2G			0x100
140 #define WSF_5G			0x200
141 	int frequency;
142 	DHCPCD_WI_AV quality;
143 	DHCPCD_WI_AV noise;
144 	DHCPCD_WI_AV level;
145 	DHCPCD_WI_AV strength;
146 	char ssid[IF_SSIDSIZE];
147 	char wpa_flags[FLAGSIZE];
148 } DHCPCD_WI_SCAN;
149 
150 #ifdef IN_LIBDHCPCD
151 typedef struct dhcpcd_if {
152 	struct dhcpcd_if *next;
153 	const char *ifname;
154 	unsigned int type;
155 	unsigned int state;
156 	const char *reason;
157 
158 	unsigned int ifflags;
159 	bool up;
160 	bool wireless;
161 	const char *ssid;
162 	int freq;
163 
164 	char *data;
165 	size_t data_len;
166 
167 	char *last_message;
168 
169 	struct dhcpcd_connection *con;
170 } DHCPCD_IF;
171 #else
172 typedef struct dhcpcd_if {
173 	struct dhcpcd_if *next;
174 	const char *ifname;
175 	unsigned int type;
176 	unsigned int state;
177 	const char *reason;
178 
179 	unsigned int ifflags;
180 	bool up;
181 	bool wireless;
182 	const char *ssid;
183 	int freq;
184 } DHCPCD_IF;
185 #endif
186 
187 typedef struct dhcpcd_config {
188 	struct dhcpcd_config *next;
189 	char *option;
190 	char *value;
191 } DHCPCD_OPTION;
192 
193 #ifdef IN_LIBDHCPCD
194 typedef struct dhcpcd_wi_hist {
195 	struct dhcpcd_wi_hist *next;
196 	char ifname[IF_NAMESIZE];
197 	char bssid[IF_BSSIDSIZE];
198 	int quality;
199 	int noise;
200 	int level;
201 	int strength;
202 } DHCPCD_WI_HIST;
203 
204 typedef struct dhcpcd_wpa {
205 	struct dhcpcd_wpa *next;
206 	char ifname[IF_NAMESIZE];
207 	unsigned int status;
208 	int command_fd;
209 	char *command_path;
210 	int listen_fd;
211 	char *listen_path;
212 	bool attached;
213 	struct dhcpcd_connection *con;
214 } DHCPCD_WPA;
215 
216 typedef struct dhcpcd_connection {
217 	struct dhcpcd_connection *next;
218 	bool open;
219 	bool privileged;
220 	int command_fd;
221 	int listen_fd;
222 	const char *progname;
223 
224 	DHCPCD_IF *interfaces;
225 	DHCPCD_WPA *wpa;
226 	DHCPCD_WI_HIST *wi_history;
227 
228 	void (*if_cb)(DHCPCD_IF *, void *);
229 	void *if_context;
230 	void (*status_cb)(struct dhcpcd_connection *,
231 	    unsigned int, const char *, void *);
232 	void *status_context;
233 	bool wpa_started;
234 	void (*wi_scanresults_cb)(DHCPCD_WPA *, void *);
235 	void *wi_scanresults_context;
236 	void (*wpa_status_cb)(DHCPCD_WPA *, unsigned int, const char *, void *);
237 	void *wpa_status_context;
238 
239 	char *buf;
240 	size_t buflen;
241 
242 	char *version;
243 	bool terminate_commands;
244 	char *error;
245 	int err;
246 	int errors;
247 	unsigned int status;
248 	bool af_waiting;
249 
250 	char *cffile;
251 } DHCPCD_CONNECTION;
252 
253 #else
254 typedef void *DHCPCD_CONFIG;
255 typedef void *DHCPCD_WPA;
256 typedef void *DHCPCD_CONNECTION;
257 #endif
258 
259 DHCPCD_CONNECTION * dhcpcd_new(void);
260 const char * dhcpcd_version(DHCPCD_CONNECTION *);
261 void dhcpcd_set_progname(DHCPCD_CONNECTION *, const char *);
262 const char * dhcpcd_get_progname(const DHCPCD_CONNECTION *);
263 unsigned int dhcpcd_status(DHCPCD_CONNECTION *, const char **);
264 bool dhcpcd_af_waiting(const DHCPCD_CONNECTION *);
265 const char * dhcpcd_cffile(DHCPCD_CONNECTION *);
266 bool dhcpcd_realloc(DHCPCD_CONNECTION *, size_t);
267 int dhcpcd_open(DHCPCD_CONNECTION *, bool priv);
268 void dhcpcd_close(DHCPCD_CONNECTION *);
269 void dhcpcd_free(DHCPCD_CONNECTION *);
270 void dhcpcd_set_if_callback(DHCPCD_CONNECTION *,
271     void (*)(DHCPCD_IF *, void *), void *);
272 void dhcpcd_set_status_callback(DHCPCD_CONNECTION *,
273     void (*)(DHCPCD_CONNECTION *, unsigned int, const char *, void *), void *);
274 int dhcpcd_get_fd(DHCPCD_CONNECTION *);
275 bool dhcpcd_privileged(DHCPCD_CONNECTION *);
276 void dhcpcd_dispatch(DHCPCD_CONNECTION *);
277 DHCPCD_IF * dhcpcd_interfaces(DHCPCD_CONNECTION *);
278 char **dhcpcd_interface_names(DHCPCD_CONNECTION *, size_t *);
279 void dhcpcd_freev(char **);
280 char **dhcpcd_interface_names_sorted(DHCPCD_CONNECTION *);
281 DHCPCD_IF * dhcpcd_get_if(DHCPCD_CONNECTION *, const char *, unsigned int);
282 DHCPCD_CONNECTION * dhcpcd_if_connection(DHCPCD_IF *);
283 const char *dhcpcd_get_value(const DHCPCD_IF *, const char *);
284 const char *dhcpcd_get_prefix_value(const DHCPCD_IF *, const char *,
285     const char *);
286 ssize_t dhcpcd_encode_string_escape(char *, size_t, const char *, size_t);
287 ssize_t dhcpcd_decode_string_escape(char *, size_t, const char *);
288 ssize_t dhcpcd_decode_hex(char *, size_t, const char *);
289 char * dhcpcd_if_message(DHCPCD_IF *, bool *);
290 
291 ssize_t dhcpcd_command(DHCPCD_CONNECTION *, const char *, char **);
292 ssize_t dhcpcd_command_arg(DHCPCD_CONNECTION *, const char *, const char *,
293     char **);
294 #define dhcpcd_rebind(c, i)	dhcpcd_command_arg((c), "-n", (i), NULL)
295 #define dhcpcd_release(c, i)	dhcpcd_command_arg((c), "-k", (i), NULL)
296 
297 void dhcpcd_wpa_start(DHCPCD_CONNECTION *);
298 DHCPCD_WPA *dhcpcd_wpa_find(DHCPCD_CONNECTION *, const char *);
299 DHCPCD_WPA *dhcpcd_wpa_new(DHCPCD_CONNECTION *, const char *);
300 DHCPCD_CONNECTION *dhcpcd_wpa_connection(DHCPCD_WPA *);
301 int dhcpcd_wpa_open(DHCPCD_WPA *);
302 void dhcpcd_wpa_close(DHCPCD_WPA *);
303 void dhcpcd_wpa_dispatch(DHCPCD_WPA *);
304 int dhcpcd_wpa_get_fd(DHCPCD_WPA *);
305 DHCPCD_IF *dhcpcd_wpa_if(DHCPCD_WPA *);
306 void dhcpcd_wpa_if_event(DHCPCD_IF *);
307 void dhcpcd_wpa_set_scan_callback(DHCPCD_CONNECTION *,
308     void (*)(DHCPCD_WPA *, void *), void *);
309 void dhcpcd_wpa_set_status_callback(DHCPCD_CONNECTION *,
310     void (*)(DHCPCD_WPA *, unsigned int, const char *, void *), void *);
311 int dhcpcd_wi_scan_compare(DHCPCD_WI_SCAN *a, DHCPCD_WI_SCAN *b);
312 DHCPCD_WI_SCAN * dhcpcd_wi_scans(DHCPCD_IF *);
313 bool dhcpcd_wi_associated(DHCPCD_IF *i, DHCPCD_WI_SCAN *s);
314 void dhcpcd_wi_scans_free(DHCPCD_WI_SCAN *);
315 void dhcpcd_wi_history_clear(DHCPCD_CONNECTION *);
316 bool dhcpcd_wpa_set_network(DHCPCD_WPA *, int, const char *, const char *);
317 int dhcpcd_wpa_find_network_new(DHCPCD_WPA *, const char *);
318 bool dhcpcd_wpa_command(DHCPCD_WPA *, const char *);
319 bool dhcpcd_wpa_command_arg(DHCPCD_WPA *, const char *, const char *);
320 unsigned int dhcpcd_wpa_status(DHCPCD_WPA *, const char **);
321 int dhcpcd_wpa_freq(DHCPCD_WPA *);
322 #define WST_BSSID	0x01
323 #define WST_FLAGS	0x02
324 #define WST_FREQ	0x03
325 int dhcpcd_wi_print_tooltip(char *, size_t, DHCPCD_WI_SCAN *, unsigned int);
326 
327 bool dhcpcd_wpa_ping(DHCPCD_WPA *);
328 bool dhcpcd_wpa_can_background_scan(DHCPCD_WPA *);
329 bool dhcpcd_wpa_scan(DHCPCD_WPA *);
330 bool dhcpcd_wpa_reconfigure(DHCPCD_WPA *);
331 bool dhcpcd_wpa_reassociate(DHCPCD_WPA *);
332 bool dhcpcd_wpa_disconnect(DHCPCD_WPA *);
333 bool dhcpcd_wpa_config_write(DHCPCD_WPA *);
334 int dhcpcd_wpa_network_find_new(DHCPCD_WPA *, const char *);
335 bool dhcpcd_wpa_network_disable(DHCPCD_WPA *, int);
336 bool dhcpcd_wpa_network_enable(DHCPCD_WPA *, int);
337 bool dhcpcd_wpa_network_select(DHCPCD_WPA *, int);
338 bool dhcpcd_wpa_network_remove(DHCPCD_WPA *, int);
339 char * dhcpcd_wpa_network_get(DHCPCD_WPA *, int, const char *);
340 bool dhcpcd_wpa_network_set(DHCPCD_WPA *, int, const char *, const char *);
341 
342 #define DHCPCD_WPA_SUCCESS	 0
343 #define DHCPCD_WPA_ERR		-1
344 #define DHCPCD_WPA_ERR_SET	-2
345 #define DHCPCD_WPA_ERR_SET_PSK	-3
346 #define DHCPCD_WPA_ERR_ENABLE	-4
347 #define DHCPCD_WPA_ERR_WRITE	-5
348 #define DHCPCD_WPA_ERR_SELECT	-6
349 #define DHCPCD_WPA_ERR_ASSOC	-7
350 #define DHCPCD_WPA_ERR_DISCONN	-8
351 #define DHCPCD_WPA_ERR_RECONF	-9
352 int dhcpcd_wpa_configure(DHCPCD_WPA *w, DHCPCD_WI_SCAN *s, const char *p);
353 int dhcpcd_wpa_select(DHCPCD_WPA *w, DHCPCD_WI_SCAN *s);
354 
355 char ** dhcpcd_config_blocks(DHCPCD_CONNECTION *, const char *);
356 DHCPCD_OPTION *dhcpcd_config_read(DHCPCD_CONNECTION *,
357     const char *, const char *);
358 void dhcpcd_config_free(DHCPCD_OPTION *);
359 const char * dhcpcd_config_get(DHCPCD_OPTION *, const char *);
360 const char * dhcpcd_config_get_static(DHCPCD_OPTION *, const char *);
361 bool dhcpcd_config_set(DHCPCD_OPTION **, const char *, const char *);
362 bool dhcpcd_config_set_static(DHCPCD_OPTION **, const char *, const char *);
363 bool dhcpcd_config_writeable(DHCPCD_CONNECTION *con);
364 bool dhcpcd_config_write(DHCPCD_CONNECTION *,
365     const char *, const char *, const DHCPCD_OPTION *);
366 
367 #ifdef __cplusplus
368 }
369 #endif
370 
371 #endif
372