Home | History | Annotate | Download | only in drivers
      1 /*
      2  * WPA Supplicant - privilege separated driver interface
      3  * Copyright (c) 2007-2009, Jouni Malinen <j (at) 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 int wpa_driver_privsep_disassociate(void *priv, const u8 *addr,
    308 					int reason_code)
    309 {
    310 	//struct wpa_driver_privsep_data *drv = priv;
    311 	wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d",
    312 		   __func__, MAC2STR(addr), reason_code);
    313 	wpa_printf(MSG_DEBUG, "%s - TODO", __func__);
    314 	return 0;
    315 }
    316 
    317 
    318 static void wpa_driver_privsep_event_assoc(void *ctx,
    319 					   enum wpa_event_type event,
    320 					   u8 *buf, size_t len)
    321 {
    322 	union wpa_event_data data;
    323 	int inc_data = 0;
    324 	u8 *pos, *end;
    325 	int ie_len;
    326 
    327 	os_memset(&data, 0, sizeof(data));
    328 
    329 	pos = buf;
    330 	end = buf + len;
    331 
    332 	if (end - pos < (int) sizeof(int))
    333 		return;
    334 	os_memcpy(&ie_len, pos, sizeof(int));
    335 	pos += sizeof(int);
    336 	if (ie_len < 0 || ie_len > end - pos)
    337 		return;
    338 	if (ie_len) {
    339 		data.assoc_info.req_ies = pos;
    340 		data.assoc_info.req_ies_len = ie_len;
    341 		pos += ie_len;
    342 		inc_data = 1;
    343 	}
    344 
    345 	wpa_supplicant_event(ctx, event, inc_data ? &data : NULL);
    346 }
    347 
    348 
    349 static void wpa_driver_privsep_event_interface_status(void *ctx, u8 *buf,
    350 						      size_t len)
    351 {
    352 	union wpa_event_data data;
    353 	int ievent;
    354 
    355 	if (len < sizeof(int) ||
    356 	    len - sizeof(int) > sizeof(data.interface_status.ifname))
    357 		return;
    358 
    359 	os_memcpy(&ievent, buf, sizeof(int));
    360 
    361 	os_memset(&data, 0, sizeof(data));
    362 	data.interface_status.ievent = ievent;
    363 	os_memcpy(data.interface_status.ifname, buf + sizeof(int),
    364 		  len - sizeof(int));
    365 	wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &data);
    366 }
    367 
    368 
    369 static void wpa_driver_privsep_event_michael_mic_failure(
    370 	void *ctx, u8 *buf, size_t len)
    371 {
    372 	union wpa_event_data data;
    373 
    374 	if (len != sizeof(int))
    375 		return;
    376 
    377 	os_memset(&data, 0, sizeof(data));
    378 	os_memcpy(&data.michael_mic_failure.unicast, buf, sizeof(int));
    379 	wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
    380 }
    381 
    382 
    383 static void wpa_driver_privsep_event_pmkid_candidate(void *ctx, u8 *buf,
    384 						     size_t len)
    385 {
    386 	union wpa_event_data data;
    387 
    388 	if (len != sizeof(struct pmkid_candidate))
    389 		return;
    390 
    391 	os_memset(&data, 0, sizeof(data));
    392 	os_memcpy(&data.pmkid_candidate, buf, len);
    393 	wpa_supplicant_event(ctx, EVENT_PMKID_CANDIDATE, &data);
    394 }
    395 
    396 
    397 static void wpa_driver_privsep_event_stkstart(void *ctx, u8 *buf, size_t len)
    398 {
    399 	union wpa_event_data data;
    400 
    401 	if (len != ETH_ALEN)
    402 		return;
    403 
    404 	os_memset(&data, 0, sizeof(data));
    405 	os_memcpy(data.stkstart.peer, buf, ETH_ALEN);
    406 	wpa_supplicant_event(ctx, EVENT_STKSTART, &data);
    407 }
    408 
    409 
    410 static void wpa_driver_privsep_event_ft_response(void *ctx, u8 *buf,
    411 						 size_t len)
    412 {
    413 	union wpa_event_data data;
    414 
    415 	if (len < sizeof(int) + ETH_ALEN)
    416 		return;
    417 
    418 	os_memset(&data, 0, sizeof(data));
    419 	os_memcpy(&data.ft_ies.ft_action, buf, sizeof(int));
    420 	os_memcpy(data.ft_ies.target_ap, buf + sizeof(int), ETH_ALEN);
    421 	data.ft_ies.ies = buf + sizeof(int) + ETH_ALEN;
    422 	data.ft_ies.ies_len = len - sizeof(int) - ETH_ALEN;
    423 	wpa_supplicant_event(ctx, EVENT_FT_RESPONSE, &data);
    424 }
    425 
    426 
    427 static void wpa_driver_privsep_event_rx_eapol(void *ctx, u8 *buf, size_t len)
    428 {
    429 	if (len < ETH_ALEN)
    430 		return;
    431 	drv_event_eapol_rx(ctx, buf, buf + ETH_ALEN, len - ETH_ALEN);
    432 }
    433 
    434 
    435 static void wpa_driver_privsep_receive(int sock, void *eloop_ctx,
    436 				       void *sock_ctx)
    437 {
    438 	struct wpa_driver_privsep_data *drv = eloop_ctx;
    439 	u8 *buf, *event_buf;
    440 	size_t event_len;
    441 	int res, event;
    442 	enum privsep_event e;
    443 	struct sockaddr_un from;
    444 	socklen_t fromlen = sizeof(from);
    445 	const size_t buflen = 2000;
    446 
    447 	buf = os_malloc(buflen);
    448 	if (buf == NULL)
    449 		return;
    450 	res = recvfrom(sock, buf, buflen, 0,
    451 		       (struct sockaddr *) &from, &fromlen);
    452 	if (res < 0) {
    453 		perror("recvfrom(priv_socket)");
    454 		os_free(buf);
    455 		return;
    456 	}
    457 
    458 	wpa_printf(MSG_DEBUG, "privsep_driver: received %u bytes", res);
    459 
    460 	if (res < (int) sizeof(int)) {
    461 		wpa_printf(MSG_DEBUG, "Too short event message (len=%d)", res);
    462 		return;
    463 	}
    464 
    465 	os_memcpy(&event, buf, sizeof(int));
    466 	event_buf = &buf[sizeof(int)];
    467 	event_len = res - sizeof(int);
    468 	wpa_printf(MSG_DEBUG, "privsep: Event %d received (len=%lu)",
    469 		   event, (unsigned long) event_len);
    470 
    471 	e = event;
    472 	switch (e) {
    473 	case PRIVSEP_EVENT_SCAN_RESULTS:
    474 		wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, NULL);
    475 		break;
    476 	case PRIVSEP_EVENT_ASSOC:
    477 		wpa_driver_privsep_event_assoc(drv->ctx, EVENT_ASSOC,
    478 					       event_buf, event_len);
    479 		break;
    480 	case PRIVSEP_EVENT_DISASSOC:
    481 		wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
    482 		break;
    483 	case PRIVSEP_EVENT_ASSOCINFO:
    484 		wpa_driver_privsep_event_assoc(drv->ctx, EVENT_ASSOCINFO,
    485 					       event_buf, event_len);
    486 		break;
    487 	case PRIVSEP_EVENT_MICHAEL_MIC_FAILURE:
    488 		wpa_driver_privsep_event_michael_mic_failure(
    489 			drv->ctx, event_buf, event_len);
    490 		break;
    491 	case PRIVSEP_EVENT_INTERFACE_STATUS:
    492 		wpa_driver_privsep_event_interface_status(drv->ctx, event_buf,
    493 							  event_len);
    494 		break;
    495 	case PRIVSEP_EVENT_PMKID_CANDIDATE:
    496 		wpa_driver_privsep_event_pmkid_candidate(drv->ctx, event_buf,
    497 							 event_len);
    498 		break;
    499 	case PRIVSEP_EVENT_STKSTART:
    500 		wpa_driver_privsep_event_stkstart(drv->ctx, event_buf,
    501 						  event_len);
    502 		break;
    503 	case PRIVSEP_EVENT_FT_RESPONSE:
    504 		wpa_driver_privsep_event_ft_response(drv->ctx, event_buf,
    505 						     event_len);
    506 		break;
    507 	case PRIVSEP_EVENT_RX_EAPOL:
    508 		wpa_driver_privsep_event_rx_eapol(drv->ctx, event_buf,
    509 						  event_len);
    510 		break;
    511 	}
    512 
    513 	os_free(buf);
    514 }
    515 
    516 
    517 static void * wpa_driver_privsep_init(void *ctx, const char *ifname)
    518 {
    519 	struct wpa_driver_privsep_data *drv;
    520 
    521 	drv = os_zalloc(sizeof(*drv));
    522 	if (drv == NULL)
    523 		return NULL;
    524 	drv->ctx = ctx;
    525 	drv->priv_socket = -1;
    526 	drv->cmd_socket = -1;
    527 	os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
    528 
    529 	return drv;
    530 }
    531 
    532 
    533 static void wpa_driver_privsep_deinit(void *priv)
    534 {
    535 	struct wpa_driver_privsep_data *drv = priv;
    536 
    537 	if (drv->priv_socket >= 0) {
    538 		wpa_priv_reg_cmd(drv, PRIVSEP_CMD_UNREGISTER);
    539 		eloop_unregister_read_sock(drv->priv_socket);
    540 		close(drv->priv_socket);
    541 	}
    542 
    543 	if (drv->own_socket_path) {
    544 		unlink(drv->own_socket_path);
    545 		os_free(drv->own_socket_path);
    546 	}
    547 
    548 	if (drv->cmd_socket >= 0) {
    549 		eloop_unregister_read_sock(drv->cmd_socket);
    550 		close(drv->cmd_socket);
    551 	}
    552 
    553 	if (drv->own_cmd_path) {
    554 		unlink(drv->own_cmd_path);
    555 		os_free(drv->own_cmd_path);
    556 	}
    557 
    558 	os_free(drv);
    559 }
    560 
    561 
    562 static int wpa_driver_privsep_set_param(void *priv, const char *param)
    563 {
    564 	struct wpa_driver_privsep_data *drv = priv;
    565 	const char *pos;
    566 	char *own_dir, *priv_dir;
    567 	static unsigned int counter = 0;
    568 	size_t len;
    569 	struct sockaddr_un addr;
    570 
    571 	wpa_printf(MSG_DEBUG, "%s: param='%s'", __func__, param);
    572 	if (param == NULL)
    573 		pos = NULL;
    574 	else
    575 		pos = os_strstr(param, "own_dir=");
    576 	if (pos) {
    577 		char *end;
    578 		own_dir = os_strdup(pos + 8);
    579 		if (own_dir == NULL)
    580 			return -1;
    581 		end = os_strchr(own_dir, ' ');
    582 		if (end)
    583 			*end = '\0';
    584 	} else {
    585 		own_dir = os_strdup("/tmp");
    586 		if (own_dir == NULL)
    587 			return -1;
    588 	}
    589 
    590 	if (param == NULL)
    591 		pos = NULL;
    592 	else
    593 		pos = os_strstr(param, "priv_dir=");
    594 	if (pos) {
    595 		char *end;
    596 		priv_dir = os_strdup(pos + 9);
    597 		if (priv_dir == NULL) {
    598 			os_free(own_dir);
    599 			return -1;
    600 		}
    601 		end = os_strchr(priv_dir, ' ');
    602 		if (end)
    603 			*end = '\0';
    604 	} else {
    605 		priv_dir = os_strdup("/var/run/wpa_priv");
    606 		if (priv_dir == NULL) {
    607 			os_free(own_dir);
    608 			return -1;
    609 		}
    610 	}
    611 
    612 	len = os_strlen(own_dir) + 50;
    613 	drv->own_socket_path = os_malloc(len);
    614 	if (drv->own_socket_path == NULL) {
    615 		os_free(priv_dir);
    616 		os_free(own_dir);
    617 		return -1;
    618 	}
    619 	os_snprintf(drv->own_socket_path, len, "%s/wpa_privsep-%d-%d",
    620 		    own_dir, getpid(), counter++);
    621 
    622 	len = os_strlen(own_dir) + 50;
    623 	drv->own_cmd_path = os_malloc(len);
    624 	if (drv->own_cmd_path == NULL) {
    625 		os_free(drv->own_socket_path);
    626 		drv->own_socket_path = NULL;
    627 		os_free(priv_dir);
    628 		os_free(own_dir);
    629 		return -1;
    630 	}
    631 	os_snprintf(drv->own_cmd_path, len, "%s/wpa_privsep-%d-%d",
    632 		    own_dir, getpid(), counter++);
    633 
    634 	os_free(own_dir);
    635 
    636 	drv->priv_addr.sun_family = AF_UNIX;
    637 	os_snprintf(drv->priv_addr.sun_path, sizeof(drv->priv_addr.sun_path),
    638 		    "%s/%s", priv_dir, drv->ifname);
    639 	os_free(priv_dir);
    640 
    641 	drv->priv_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
    642 	if (drv->priv_socket < 0) {
    643 		perror("socket(PF_UNIX)");
    644 		os_free(drv->own_socket_path);
    645 		drv->own_socket_path = NULL;
    646 		return -1;
    647 	}
    648 
    649 	os_memset(&addr, 0, sizeof(addr));
    650 	addr.sun_family = AF_UNIX;
    651 	os_strlcpy(addr.sun_path, drv->own_socket_path, sizeof(addr.sun_path));
    652 	if (bind(drv->priv_socket, (struct sockaddr *) &addr, sizeof(addr)) <
    653 	    0) {
    654 		perror("privsep-set-params priv-sock: bind(PF_UNIX)");
    655 		close(drv->priv_socket);
    656 		drv->priv_socket = -1;
    657 		unlink(drv->own_socket_path);
    658 		os_free(drv->own_socket_path);
    659 		drv->own_socket_path = NULL;
    660 		return -1;
    661 	}
    662 
    663 	eloop_register_read_sock(drv->priv_socket, wpa_driver_privsep_receive,
    664 				 drv, NULL);
    665 
    666 	drv->cmd_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
    667 	if (drv->cmd_socket < 0) {
    668 		perror("socket(PF_UNIX)");
    669 		os_free(drv->own_cmd_path);
    670 		drv->own_cmd_path = NULL;
    671 		return -1;
    672 	}
    673 
    674 	os_memset(&addr, 0, sizeof(addr));
    675 	addr.sun_family = AF_UNIX;
    676 	os_strlcpy(addr.sun_path, drv->own_cmd_path, sizeof(addr.sun_path));
    677 	if (bind(drv->cmd_socket, (struct sockaddr *) &addr, sizeof(addr)) < 0)
    678 	{
    679 		perror("privsep-set-params cmd-sock: bind(PF_UNIX)");
    680 		close(drv->cmd_socket);
    681 		drv->cmd_socket = -1;
    682 		unlink(drv->own_cmd_path);
    683 		os_free(drv->own_cmd_path);
    684 		drv->own_cmd_path = NULL;
    685 		return -1;
    686 	}
    687 
    688 	if (wpa_priv_reg_cmd(drv, PRIVSEP_CMD_REGISTER) < 0) {
    689 		wpa_printf(MSG_ERROR, "Failed to register with wpa_priv");
    690 		return -1;
    691 	}
    692 
    693 	return 0;
    694 }
    695 
    696 
    697 static int wpa_driver_privsep_get_capa(void *priv,
    698 				       struct wpa_driver_capa *capa)
    699 {
    700 	struct wpa_driver_privsep_data *drv = priv;
    701 	int res;
    702 	size_t len = sizeof(*capa);
    703 
    704 	res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_CAPA, NULL, 0, capa, &len);
    705 	if (res < 0 || len != sizeof(*capa))
    706 		return -1;
    707 	return 0;
    708 }
    709 
    710 
    711 static const u8 * wpa_driver_privsep_get_mac_addr(void *priv)
    712 {
    713 	struct wpa_driver_privsep_data *drv = priv;
    714 	wpa_printf(MSG_DEBUG, "%s", __func__);
    715 	return drv->own_addr;
    716 }
    717 
    718 
    719 static int wpa_driver_privsep_set_country(void *priv, const char *alpha2)
    720 {
    721 	struct wpa_driver_privsep_data *drv = priv;
    722 	wpa_printf(MSG_DEBUG, "%s country='%s'", __func__, alpha2);
    723 	return wpa_priv_cmd(drv, PRIVSEP_CMD_SET_COUNTRY, alpha2,
    724 			    os_strlen(alpha2), NULL, NULL);
    725 }
    726 
    727 
    728 struct wpa_driver_ops wpa_driver_privsep_ops = {
    729 	"privsep",
    730 	"wpa_supplicant privilege separated driver",
    731 	.get_bssid = wpa_driver_privsep_get_bssid,
    732 	.get_ssid = wpa_driver_privsep_get_ssid,
    733 	.set_key = wpa_driver_privsep_set_key,
    734 	.init = wpa_driver_privsep_init,
    735 	.deinit = wpa_driver_privsep_deinit,
    736 	.set_param = wpa_driver_privsep_set_param,
    737 	.scan2 = wpa_driver_privsep_scan,
    738 	.deauthenticate = wpa_driver_privsep_deauthenticate,
    739 	.disassociate = wpa_driver_privsep_disassociate,
    740 	.associate = wpa_driver_privsep_associate,
    741 	.get_capa = wpa_driver_privsep_get_capa,
    742 	.get_mac_addr = wpa_driver_privsep_get_mac_addr,
    743 	.get_scan_results2 = wpa_driver_privsep_get_scan_results2,
    744 	.set_country = wpa_driver_privsep_set_country,
    745 };
    746 
    747 
    748 struct wpa_driver_ops *wpa_drivers[] =
    749 {
    750 	&wpa_driver_privsep_ops,
    751 	NULL
    752 };
    753