xref: /freebsd/contrib/wpa/src/drivers/driver_privsep.c (revision 8d20be1e22095c27faf8fe8b2f0d089739cc742e)
1 /*
2  * WPA Supplicant - privilege separated driver interface
3  * Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 #include <sys/un.h>
11 
12 #include "common.h"
13 #include "driver.h"
14 #include "eloop.h"
15 #include "common/privsep_commands.h"
16 
17 
18 struct wpa_driver_privsep_data {
19 	void *ctx;
20 	u8 own_addr[ETH_ALEN];
21 	int priv_socket;
22 	char *own_socket_path;
23 	int cmd_socket;
24 	char *own_cmd_path;
25 	struct sockaddr_un priv_addr;
26 	char ifname[16];
27 };
28 
29 
30 static int wpa_priv_reg_cmd(struct wpa_driver_privsep_data *drv, int cmd)
31 {
32 	int res;
33 
34 	res = sendto(drv->priv_socket, &cmd, sizeof(cmd), 0,
35 		     (struct sockaddr *) &drv->priv_addr,
36 		     sizeof(drv->priv_addr));
37 	if (res < 0)
38 		perror("sendto");
39 	return res < 0 ? -1 : 0;
40 }
41 
42 
43 static int wpa_priv_cmd(struct wpa_driver_privsep_data *drv, int cmd,
44 			const void *data, size_t data_len,
45 			void *reply, size_t *reply_len)
46 {
47 	struct msghdr msg;
48 	struct iovec io[2];
49 
50 	io[0].iov_base = &cmd;
51 	io[0].iov_len = sizeof(cmd);
52 	io[1].iov_base = (u8 *) data;
53 	io[1].iov_len = data_len;
54 
55 	os_memset(&msg, 0, sizeof(msg));
56 	msg.msg_iov = io;
57 	msg.msg_iovlen = data ? 2 : 1;
58 	msg.msg_name = &drv->priv_addr;
59 	msg.msg_namelen = sizeof(drv->priv_addr);
60 
61 	if (sendmsg(drv->cmd_socket, &msg, 0) < 0) {
62 		perror("sendmsg(cmd_socket)");
63 		return -1;
64 	}
65 
66 	if (reply) {
67 		fd_set rfds;
68 		struct timeval tv;
69 		int res;
70 
71 		FD_ZERO(&rfds);
72 		FD_SET(drv->cmd_socket, &rfds);
73 		tv.tv_sec = 5;
74 		tv.tv_usec = 0;
75 		res = select(drv->cmd_socket + 1, &rfds, NULL, NULL, &tv);
76 		if (res < 0 && errno != EINTR) {
77 			perror("select");
78 			return -1;
79 		}
80 
81 		if (FD_ISSET(drv->cmd_socket, &rfds)) {
82 			res = recv(drv->cmd_socket, reply, *reply_len, 0);
83 			if (res < 0) {
84 				perror("recv");
85 				return -1;
86 			}
87 			*reply_len = res;
88 		} else {
89 			wpa_printf(MSG_DEBUG, "PRIVSEP: Timeout while waiting "
90 				   "for reply (cmd=%d)", cmd);
91 			return -1;
92 		}
93 	}
94 
95 	return 0;
96 }
97 
98 
99 static int wpa_driver_privsep_scan(void *priv,
100 				   struct wpa_driver_scan_params *params)
101 {
102 	struct wpa_driver_privsep_data *drv = priv;
103 	const u8 *ssid = params->ssids[0].ssid;
104 	size_t ssid_len = params->ssids[0].ssid_len;
105 	wpa_printf(MSG_DEBUG, "%s: priv=%p", __func__, priv);
106 	return wpa_priv_cmd(drv, PRIVSEP_CMD_SCAN, ssid, ssid_len,
107 			    NULL, NULL);
108 }
109 
110 
111 static struct wpa_scan_results *
112 wpa_driver_privsep_get_scan_results2(void *priv)
113 {
114 	struct wpa_driver_privsep_data *drv = priv;
115 	int res, num;
116 	u8 *buf, *pos, *end;
117 	size_t reply_len = 60000;
118 	struct wpa_scan_results *results;
119 	struct wpa_scan_res *r;
120 
121 	buf = os_malloc(reply_len);
122 	if (buf == NULL)
123 		return NULL;
124 	res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_SCAN_RESULTS,
125 			   NULL, 0, buf, &reply_len);
126 	if (res < 0) {
127 		os_free(buf);
128 		return NULL;
129 	}
130 
131 	wpa_printf(MSG_DEBUG, "privsep: Received %lu bytes of scan results",
132 		   (unsigned long) reply_len);
133 	if (reply_len < sizeof(int)) {
134 		wpa_printf(MSG_DEBUG, "privsep: Invalid scan result len %lu",
135 			   (unsigned long) reply_len);
136 		os_free(buf);
137 		return NULL;
138 	}
139 
140 	pos = buf;
141 	end = buf + reply_len;
142 	os_memcpy(&num, pos, sizeof(int));
143 	if (num < 0 || num > 1000) {
144 		os_free(buf);
145 		return NULL;
146 	}
147 	pos += sizeof(int);
148 
149 	results = os_zalloc(sizeof(*results));
150 	if (results == NULL) {
151 		os_free(buf);
152 		return NULL;
153 	}
154 
155 	results->res = os_calloc(num, sizeof(struct wpa_scan_res *));
156 	if (results->res == NULL) {
157 		os_free(results);
158 		os_free(buf);
159 		return NULL;
160 	}
161 
162 	while (results->num < (size_t) num && pos + sizeof(int) < end) {
163 		int len;
164 		os_memcpy(&len, pos, sizeof(int));
165 		pos += sizeof(int);
166 		if (len < 0 || len > 10000 || pos + len > end)
167 			break;
168 
169 		r = os_malloc(len);
170 		if (r == NULL)
171 			break;
172 		os_memcpy(r, pos, len);
173 		pos += len;
174 		if (sizeof(*r) + r->ie_len > (size_t) len) {
175 			os_free(r);
176 			break;
177 		}
178 
179 		results->res[results->num++] = r;
180 	}
181 
182 	os_free(buf);
183 	return results;
184 }
185 
186 
187 static int wpa_driver_privsep_set_key(const char *ifname, void *priv,
188 				      enum wpa_alg alg, const u8 *addr,
189 				      int key_idx, int set_tx,
190 				      const u8 *seq, size_t seq_len,
191 				      const u8 *key, size_t key_len)
192 {
193 	struct wpa_driver_privsep_data *drv = priv;
194 	struct privsep_cmd_set_key cmd;
195 
196 	wpa_printf(MSG_DEBUG, "%s: priv=%p alg=%d key_idx=%d set_tx=%d",
197 		   __func__, priv, alg, key_idx, set_tx);
198 
199 	os_memset(&cmd, 0, sizeof(cmd));
200 	cmd.alg = alg;
201 	if (addr)
202 		os_memcpy(cmd.addr, addr, ETH_ALEN);
203 	else
204 		os_memset(cmd.addr, 0xff, ETH_ALEN);
205 	cmd.key_idx = key_idx;
206 	cmd.set_tx = set_tx;
207 	if (seq && seq_len > 0 && seq_len < sizeof(cmd.seq)) {
208 		os_memcpy(cmd.seq, seq, seq_len);
209 		cmd.seq_len = seq_len;
210 	}
211 	if (key && key_len > 0 && key_len < sizeof(cmd.key)) {
212 		os_memcpy(cmd.key, key, key_len);
213 		cmd.key_len = key_len;
214 	}
215 
216 	return wpa_priv_cmd(drv, PRIVSEP_CMD_SET_KEY, &cmd, sizeof(cmd),
217 			    NULL, NULL);
218 }
219 
220 
221 static int wpa_driver_privsep_associate(
222 	void *priv, struct wpa_driver_associate_params *params)
223 {
224 	struct wpa_driver_privsep_data *drv = priv;
225 	struct privsep_cmd_associate *data;
226 	int res;
227 	size_t buflen;
228 
229 	wpa_printf(MSG_DEBUG, "%s: priv=%p freq=%d pairwise_suite=%d "
230 		   "group_suite=%d key_mgmt_suite=%d auth_alg=%d mode=%d",
231 		   __func__, priv, params->freq, params->pairwise_suite,
232 		   params->group_suite, params->key_mgmt_suite,
233 		   params->auth_alg, params->mode);
234 
235 	buflen = sizeof(*data) + params->wpa_ie_len;
236 	data = os_zalloc(buflen);
237 	if (data == NULL)
238 		return -1;
239 
240 	if (params->bssid)
241 		os_memcpy(data->bssid, params->bssid, ETH_ALEN);
242 	os_memcpy(data->ssid, params->ssid, params->ssid_len);
243 	data->ssid_len = params->ssid_len;
244 	data->freq = params->freq;
245 	data->pairwise_suite = params->pairwise_suite;
246 	data->group_suite = params->group_suite;
247 	data->key_mgmt_suite = params->key_mgmt_suite;
248 	data->auth_alg = params->auth_alg;
249 	data->mode = params->mode;
250 	data->wpa_ie_len = params->wpa_ie_len;
251 	if (params->wpa_ie)
252 		os_memcpy(data + 1, params->wpa_ie, params->wpa_ie_len);
253 	/* TODO: add support for other assoc parameters */
254 
255 	res = wpa_priv_cmd(drv, PRIVSEP_CMD_ASSOCIATE, data, buflen,
256 			   NULL, NULL);
257 	os_free(data);
258 
259 	return res;
260 }
261 
262 
263 static int wpa_driver_privsep_get_bssid(void *priv, u8 *bssid)
264 {
265 	struct wpa_driver_privsep_data *drv = priv;
266 	int res;
267 	size_t len = ETH_ALEN;
268 
269 	res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_BSSID, NULL, 0, bssid, &len);
270 	if (res < 0 || len != ETH_ALEN)
271 		return -1;
272 	return 0;
273 }
274 
275 
276 static int wpa_driver_privsep_get_ssid(void *priv, u8 *ssid)
277 {
278 	struct wpa_driver_privsep_data *drv = priv;
279 	int res, ssid_len;
280 	u8 reply[sizeof(int) + 32];
281 	size_t len = sizeof(reply);
282 
283 	res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_SSID, NULL, 0, reply, &len);
284 	if (res < 0 || len < sizeof(int))
285 		return -1;
286 	os_memcpy(&ssid_len, reply, sizeof(int));
287 	if (ssid_len < 0 || ssid_len > 32 || sizeof(int) + ssid_len > len) {
288 		wpa_printf(MSG_DEBUG, "privsep: Invalid get SSID reply");
289 		return -1;
290 	}
291 	os_memcpy(ssid, &reply[sizeof(int)], ssid_len);
292 	return ssid_len;
293 }
294 
295 
296 static int wpa_driver_privsep_deauthenticate(void *priv, const u8 *addr,
297 					  int reason_code)
298 {
299 	//struct wpa_driver_privsep_data *drv = priv;
300 	wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d",
301 		   __func__, MAC2STR(addr), reason_code);
302 	wpa_printf(MSG_DEBUG, "%s - TODO", __func__);
303 	return 0;
304 }
305 
306 
307 static void wpa_driver_privsep_event_assoc(void *ctx,
308 					   enum wpa_event_type event,
309 					   u8 *buf, size_t len)
310 {
311 	union wpa_event_data data;
312 	int inc_data = 0;
313 	u8 *pos, *end;
314 	int ie_len;
315 
316 	os_memset(&data, 0, sizeof(data));
317 
318 	pos = buf;
319 	end = buf + len;
320 
321 	if (end - pos < (int) sizeof(int))
322 		return;
323 	os_memcpy(&ie_len, pos, sizeof(int));
324 	pos += sizeof(int);
325 	if (ie_len < 0 || ie_len > end - pos)
326 		return;
327 	if (ie_len) {
328 		data.assoc_info.req_ies = pos;
329 		data.assoc_info.req_ies_len = ie_len;
330 		pos += ie_len;
331 		inc_data = 1;
332 	}
333 
334 	wpa_supplicant_event(ctx, event, inc_data ? &data : NULL);
335 }
336 
337 
338 static void wpa_driver_privsep_event_interface_status(void *ctx, u8 *buf,
339 						      size_t len)
340 {
341 	union wpa_event_data data;
342 	int ievent;
343 
344 	if (len < sizeof(int) ||
345 	    len - sizeof(int) > sizeof(data.interface_status.ifname))
346 		return;
347 
348 	os_memcpy(&ievent, buf, sizeof(int));
349 
350 	os_memset(&data, 0, sizeof(data));
351 	data.interface_status.ievent = ievent;
352 	os_memcpy(data.interface_status.ifname, buf + sizeof(int),
353 		  len - sizeof(int));
354 	wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &data);
355 }
356 
357 
358 static void wpa_driver_privsep_event_michael_mic_failure(
359 	void *ctx, u8 *buf, size_t len)
360 {
361 	union wpa_event_data data;
362 
363 	if (len != sizeof(int))
364 		return;
365 
366 	os_memset(&data, 0, sizeof(data));
367 	os_memcpy(&data.michael_mic_failure.unicast, buf, sizeof(int));
368 	wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
369 }
370 
371 
372 static void wpa_driver_privsep_event_pmkid_candidate(void *ctx, u8 *buf,
373 						     size_t len)
374 {
375 	union wpa_event_data data;
376 
377 	if (len != sizeof(struct pmkid_candidate))
378 		return;
379 
380 	os_memset(&data, 0, sizeof(data));
381 	os_memcpy(&data.pmkid_candidate, buf, len);
382 	wpa_supplicant_event(ctx, EVENT_PMKID_CANDIDATE, &data);
383 }
384 
385 
386 static void wpa_driver_privsep_event_stkstart(void *ctx, u8 *buf, size_t len)
387 {
388 	union wpa_event_data data;
389 
390 	if (len != ETH_ALEN)
391 		return;
392 
393 	os_memset(&data, 0, sizeof(data));
394 	os_memcpy(data.stkstart.peer, buf, ETH_ALEN);
395 	wpa_supplicant_event(ctx, EVENT_STKSTART, &data);
396 }
397 
398 
399 static void wpa_driver_privsep_event_ft_response(void *ctx, u8 *buf,
400 						 size_t len)
401 {
402 	union wpa_event_data data;
403 
404 	if (len < sizeof(int) + ETH_ALEN)
405 		return;
406 
407 	os_memset(&data, 0, sizeof(data));
408 	os_memcpy(&data.ft_ies.ft_action, buf, sizeof(int));
409 	os_memcpy(data.ft_ies.target_ap, buf + sizeof(int), ETH_ALEN);
410 	data.ft_ies.ies = buf + sizeof(int) + ETH_ALEN;
411 	data.ft_ies.ies_len = len - sizeof(int) - ETH_ALEN;
412 	wpa_supplicant_event(ctx, EVENT_FT_RESPONSE, &data);
413 }
414 
415 
416 static void wpa_driver_privsep_event_rx_eapol(void *ctx, u8 *buf, size_t len)
417 {
418 	if (len < ETH_ALEN)
419 		return;
420 	drv_event_eapol_rx(ctx, buf, buf + ETH_ALEN, len - ETH_ALEN);
421 }
422 
423 
424 static void wpa_driver_privsep_receive(int sock, void *eloop_ctx,
425 				       void *sock_ctx)
426 {
427 	struct wpa_driver_privsep_data *drv = eloop_ctx;
428 	u8 *buf, *event_buf;
429 	size_t event_len;
430 	int res, event;
431 	enum privsep_event e;
432 	struct sockaddr_un from;
433 	socklen_t fromlen = sizeof(from);
434 	const size_t buflen = 2000;
435 
436 	buf = os_malloc(buflen);
437 	if (buf == NULL)
438 		return;
439 	res = recvfrom(sock, buf, buflen, 0,
440 		       (struct sockaddr *) &from, &fromlen);
441 	if (res < 0) {
442 		perror("recvfrom(priv_socket)");
443 		os_free(buf);
444 		return;
445 	}
446 
447 	wpa_printf(MSG_DEBUG, "privsep_driver: received %u bytes", res);
448 
449 	if (res < (int) sizeof(int)) {
450 		wpa_printf(MSG_DEBUG, "Too short event message (len=%d)", res);
451 		return;
452 	}
453 
454 	os_memcpy(&event, buf, sizeof(int));
455 	event_buf = &buf[sizeof(int)];
456 	event_len = res - sizeof(int);
457 	wpa_printf(MSG_DEBUG, "privsep: Event %d received (len=%lu)",
458 		   event, (unsigned long) event_len);
459 
460 	e = event;
461 	switch (e) {
462 	case PRIVSEP_EVENT_SCAN_RESULTS:
463 		wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, NULL);
464 		break;
465 	case PRIVSEP_EVENT_ASSOC:
466 		wpa_driver_privsep_event_assoc(drv->ctx, EVENT_ASSOC,
467 					       event_buf, event_len);
468 		break;
469 	case PRIVSEP_EVENT_DISASSOC:
470 		wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
471 		break;
472 	case PRIVSEP_EVENT_ASSOCINFO:
473 		wpa_driver_privsep_event_assoc(drv->ctx, EVENT_ASSOCINFO,
474 					       event_buf, event_len);
475 		break;
476 	case PRIVSEP_EVENT_MICHAEL_MIC_FAILURE:
477 		wpa_driver_privsep_event_michael_mic_failure(
478 			drv->ctx, event_buf, event_len);
479 		break;
480 	case PRIVSEP_EVENT_INTERFACE_STATUS:
481 		wpa_driver_privsep_event_interface_status(drv->ctx, event_buf,
482 							  event_len);
483 		break;
484 	case PRIVSEP_EVENT_PMKID_CANDIDATE:
485 		wpa_driver_privsep_event_pmkid_candidate(drv->ctx, event_buf,
486 							 event_len);
487 		break;
488 	case PRIVSEP_EVENT_STKSTART:
489 		wpa_driver_privsep_event_stkstart(drv->ctx, event_buf,
490 						  event_len);
491 		break;
492 	case PRIVSEP_EVENT_FT_RESPONSE:
493 		wpa_driver_privsep_event_ft_response(drv->ctx, event_buf,
494 						     event_len);
495 		break;
496 	case PRIVSEP_EVENT_RX_EAPOL:
497 		wpa_driver_privsep_event_rx_eapol(drv->ctx, event_buf,
498 						  event_len);
499 		break;
500 	}
501 
502 	os_free(buf);
503 }
504 
505 
506 static void * wpa_driver_privsep_init(void *ctx, const char *ifname)
507 {
508 	struct wpa_driver_privsep_data *drv;
509 
510 	drv = os_zalloc(sizeof(*drv));
511 	if (drv == NULL)
512 		return NULL;
513 	drv->ctx = ctx;
514 	drv->priv_socket = -1;
515 	drv->cmd_socket = -1;
516 	os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
517 
518 	return drv;
519 }
520 
521 
522 static void wpa_driver_privsep_deinit(void *priv)
523 {
524 	struct wpa_driver_privsep_data *drv = priv;
525 
526 	if (drv->priv_socket >= 0) {
527 		wpa_priv_reg_cmd(drv, PRIVSEP_CMD_UNREGISTER);
528 		eloop_unregister_read_sock(drv->priv_socket);
529 		close(drv->priv_socket);
530 	}
531 
532 	if (drv->own_socket_path) {
533 		unlink(drv->own_socket_path);
534 		os_free(drv->own_socket_path);
535 	}
536 
537 	if (drv->cmd_socket >= 0) {
538 		eloop_unregister_read_sock(drv->cmd_socket);
539 		close(drv->cmd_socket);
540 	}
541 
542 	if (drv->own_cmd_path) {
543 		unlink(drv->own_cmd_path);
544 		os_free(drv->own_cmd_path);
545 	}
546 
547 	os_free(drv);
548 }
549 
550 
551 static int wpa_driver_privsep_set_param(void *priv, const char *param)
552 {
553 	struct wpa_driver_privsep_data *drv = priv;
554 	const char *pos;
555 	char *own_dir, *priv_dir;
556 	static unsigned int counter = 0;
557 	size_t len;
558 	struct sockaddr_un addr;
559 
560 	wpa_printf(MSG_DEBUG, "%s: param='%s'", __func__, param);
561 	if (param == NULL)
562 		pos = NULL;
563 	else
564 		pos = os_strstr(param, "own_dir=");
565 	if (pos) {
566 		char *end;
567 		own_dir = os_strdup(pos + 8);
568 		if (own_dir == NULL)
569 			return -1;
570 		end = os_strchr(own_dir, ' ');
571 		if (end)
572 			*end = '\0';
573 	} else {
574 		own_dir = os_strdup("/tmp");
575 		if (own_dir == NULL)
576 			return -1;
577 	}
578 
579 	if (param == NULL)
580 		pos = NULL;
581 	else
582 		pos = os_strstr(param, "priv_dir=");
583 	if (pos) {
584 		char *end;
585 		priv_dir = os_strdup(pos + 9);
586 		if (priv_dir == NULL) {
587 			os_free(own_dir);
588 			return -1;
589 		}
590 		end = os_strchr(priv_dir, ' ');
591 		if (end)
592 			*end = '\0';
593 	} else {
594 		priv_dir = os_strdup("/var/run/wpa_priv");
595 		if (priv_dir == NULL) {
596 			os_free(own_dir);
597 			return -1;
598 		}
599 	}
600 
601 	len = os_strlen(own_dir) + 50;
602 	drv->own_socket_path = os_malloc(len);
603 	if (drv->own_socket_path == NULL) {
604 		os_free(priv_dir);
605 		os_free(own_dir);
606 		return -1;
607 	}
608 	os_snprintf(drv->own_socket_path, len, "%s/wpa_privsep-%d-%d",
609 		    own_dir, getpid(), counter++);
610 
611 	len = os_strlen(own_dir) + 50;
612 	drv->own_cmd_path = os_malloc(len);
613 	if (drv->own_cmd_path == NULL) {
614 		os_free(drv->own_socket_path);
615 		drv->own_socket_path = NULL;
616 		os_free(priv_dir);
617 		os_free(own_dir);
618 		return -1;
619 	}
620 	os_snprintf(drv->own_cmd_path, len, "%s/wpa_privsep-%d-%d",
621 		    own_dir, getpid(), counter++);
622 
623 	os_free(own_dir);
624 
625 	drv->priv_addr.sun_family = AF_UNIX;
626 	os_snprintf(drv->priv_addr.sun_path, sizeof(drv->priv_addr.sun_path),
627 		    "%s/%s", priv_dir, drv->ifname);
628 	os_free(priv_dir);
629 
630 	drv->priv_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
631 	if (drv->priv_socket < 0) {
632 		perror("socket(PF_UNIX)");
633 		os_free(drv->own_socket_path);
634 		drv->own_socket_path = NULL;
635 		return -1;
636 	}
637 
638 	os_memset(&addr, 0, sizeof(addr));
639 	addr.sun_family = AF_UNIX;
640 	os_strlcpy(addr.sun_path, drv->own_socket_path, sizeof(addr.sun_path));
641 	if (bind(drv->priv_socket, (struct sockaddr *) &addr, sizeof(addr)) <
642 	    0) {
643 		perror("privsep-set-params priv-sock: bind(PF_UNIX)");
644 		close(drv->priv_socket);
645 		drv->priv_socket = -1;
646 		unlink(drv->own_socket_path);
647 		os_free(drv->own_socket_path);
648 		drv->own_socket_path = NULL;
649 		return -1;
650 	}
651 
652 	eloop_register_read_sock(drv->priv_socket, wpa_driver_privsep_receive,
653 				 drv, NULL);
654 
655 	drv->cmd_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
656 	if (drv->cmd_socket < 0) {
657 		perror("socket(PF_UNIX)");
658 		os_free(drv->own_cmd_path);
659 		drv->own_cmd_path = NULL;
660 		return -1;
661 	}
662 
663 	os_memset(&addr, 0, sizeof(addr));
664 	addr.sun_family = AF_UNIX;
665 	os_strlcpy(addr.sun_path, drv->own_cmd_path, sizeof(addr.sun_path));
666 	if (bind(drv->cmd_socket, (struct sockaddr *) &addr, sizeof(addr)) < 0)
667 	{
668 		perror("privsep-set-params cmd-sock: bind(PF_UNIX)");
669 		close(drv->cmd_socket);
670 		drv->cmd_socket = -1;
671 		unlink(drv->own_cmd_path);
672 		os_free(drv->own_cmd_path);
673 		drv->own_cmd_path = NULL;
674 		return -1;
675 	}
676 
677 	if (wpa_priv_reg_cmd(drv, PRIVSEP_CMD_REGISTER) < 0) {
678 		wpa_printf(MSG_ERROR, "Failed to register with wpa_priv");
679 		return -1;
680 	}
681 
682 	return 0;
683 }
684 
685 
686 static int wpa_driver_privsep_get_capa(void *priv,
687 				       struct wpa_driver_capa *capa)
688 {
689 	struct wpa_driver_privsep_data *drv = priv;
690 	int res;
691 	size_t len = sizeof(*capa);
692 
693 	res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_CAPA, NULL, 0, capa, &len);
694 	if (res < 0 || len != sizeof(*capa))
695 		return -1;
696 	return 0;
697 }
698 
699 
700 static const u8 * wpa_driver_privsep_get_mac_addr(void *priv)
701 {
702 	struct wpa_driver_privsep_data *drv = priv;
703 	wpa_printf(MSG_DEBUG, "%s", __func__);
704 	return drv->own_addr;
705 }
706 
707 
708 static int wpa_driver_privsep_set_country(void *priv, const char *alpha2)
709 {
710 	struct wpa_driver_privsep_data *drv = priv;
711 	wpa_printf(MSG_DEBUG, "%s country='%s'", __func__, alpha2);
712 	return wpa_priv_cmd(drv, PRIVSEP_CMD_SET_COUNTRY, alpha2,
713 			    os_strlen(alpha2), NULL, NULL);
714 }
715 
716 
717 struct wpa_driver_ops wpa_driver_privsep_ops = {
718 	"privsep",
719 	"wpa_supplicant privilege separated driver",
720 	.get_bssid = wpa_driver_privsep_get_bssid,
721 	.get_ssid = wpa_driver_privsep_get_ssid,
722 	.set_key = wpa_driver_privsep_set_key,
723 	.init = wpa_driver_privsep_init,
724 	.deinit = wpa_driver_privsep_deinit,
725 	.set_param = wpa_driver_privsep_set_param,
726 	.scan2 = wpa_driver_privsep_scan,
727 	.deauthenticate = wpa_driver_privsep_deauthenticate,
728 	.associate = wpa_driver_privsep_associate,
729 	.get_capa = wpa_driver_privsep_get_capa,
730 	.get_mac_addr = wpa_driver_privsep_get_mac_addr,
731 	.get_scan_results2 = wpa_driver_privsep_get_scan_results2,
732 	.set_country = wpa_driver_privsep_set_country,
733 };
734 
735 
736 struct wpa_driver_ops *wpa_drivers[] =
737 {
738 	&wpa_driver_privsep_ops,
739 	NULL
740 };
741