• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
wpa_priv_reg_cmd(struct wpa_driver_privsep_data * drv,int cmd)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 		wpa_printf(MSG_ERROR, "sendto: %s", strerror(errno));
39 	return res < 0 ? -1 : 0;
40 }
41 
42 
wpa_priv_cmd(struct wpa_driver_privsep_data * drv,int cmd,const void * data,size_t data_len,void * reply,size_t * reply_len)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 		wpa_printf(MSG_ERROR, "sendmsg(cmd_socket): %s",
63 			   strerror(errno));
64 		return -1;
65 	}
66 
67 	if (reply) {
68 		fd_set rfds;
69 		struct timeval tv;
70 		int res;
71 
72 		FD_ZERO(&rfds);
73 		FD_SET(drv->cmd_socket, &rfds);
74 		tv.tv_sec = 5;
75 		tv.tv_usec = 0;
76 		res = select(drv->cmd_socket + 1, &rfds, NULL, NULL, &tv);
77 		if (res < 0 && errno != EINTR) {
78 			wpa_printf(MSG_ERROR, "select: %s", strerror(errno));
79 			return -1;
80 		}
81 
82 		if (FD_ISSET(drv->cmd_socket, &rfds)) {
83 			res = recv(drv->cmd_socket, reply, *reply_len, 0);
84 			if (res < 0) {
85 				wpa_printf(MSG_ERROR, "recv: %s",
86 					   strerror(errno));
87 				return -1;
88 			}
89 			*reply_len = res;
90 		} else {
91 			wpa_printf(MSG_DEBUG, "PRIVSEP: Timeout while waiting "
92 				   "for reply (cmd=%d)", cmd);
93 			return -1;
94 		}
95 	}
96 
97 	return 0;
98 }
99 
100 
wpa_driver_privsep_scan(void * priv,struct wpa_driver_scan_params * params)101 static int wpa_driver_privsep_scan(void *priv,
102 				   struct wpa_driver_scan_params *params)
103 {
104 	struct wpa_driver_privsep_data *drv = priv;
105 	struct privsep_cmd_scan scan;
106 	size_t i;
107 
108 	wpa_printf(MSG_DEBUG, "%s: priv=%p", __func__, priv);
109 	os_memset(&scan, 0, sizeof(scan));
110 	scan.num_ssids = params->num_ssids;
111 	for (i = 0; i < params->num_ssids; i++) {
112 		if (!params->ssids[i].ssid)
113 			continue;
114 		scan.ssid_lens[i] = params->ssids[i].ssid_len;
115 		os_memcpy(scan.ssids[i], params->ssids[i].ssid,
116 			  scan.ssid_lens[i]);
117 	}
118 
119 	for (i = 0; i < PRIVSEP_MAX_SCAN_FREQS &&
120 		     params->freqs && params->freqs[i]; i++)
121 		scan.freqs[i] = params->freqs[i];
122 	scan.num_freqs = i;
123 
124 	return wpa_priv_cmd(drv, PRIVSEP_CMD_SCAN, &scan, sizeof(scan),
125 			    NULL, NULL);
126 }
127 
128 
129 static struct wpa_scan_results *
wpa_driver_privsep_get_scan_results2(void * priv)130 wpa_driver_privsep_get_scan_results2(void *priv)
131 {
132 	struct wpa_driver_privsep_data *drv = priv;
133 	int res, num;
134 	u8 *buf, *pos, *end;
135 	size_t reply_len = 60000;
136 	struct wpa_scan_results *results;
137 	struct wpa_scan_res *r;
138 
139 	buf = os_malloc(reply_len);
140 	if (buf == NULL)
141 		return NULL;
142 	res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_SCAN_RESULTS,
143 			   NULL, 0, buf, &reply_len);
144 	if (res < 0) {
145 		os_free(buf);
146 		return NULL;
147 	}
148 
149 	wpa_printf(MSG_DEBUG, "privsep: Received %lu bytes of scan results",
150 		   (unsigned long) reply_len);
151 	if (reply_len < sizeof(int)) {
152 		wpa_printf(MSG_DEBUG, "privsep: Invalid scan result len %lu",
153 			   (unsigned long) reply_len);
154 		os_free(buf);
155 		return NULL;
156 	}
157 
158 	pos = buf;
159 	end = buf + reply_len;
160 	os_memcpy(&num, pos, sizeof(int));
161 	if (num < 0 || num > 1000) {
162 		os_free(buf);
163 		return NULL;
164 	}
165 	pos += sizeof(int);
166 
167 	results = os_zalloc(sizeof(*results));
168 	if (results == NULL) {
169 		os_free(buf);
170 		return NULL;
171 	}
172 
173 	results->res = os_calloc(num, sizeof(struct wpa_scan_res *));
174 	if (results->res == NULL) {
175 		os_free(results);
176 		os_free(buf);
177 		return NULL;
178 	}
179 
180 	while (results->num < (size_t) num && end - pos > (int) sizeof(int)) {
181 		int len;
182 		os_memcpy(&len, pos, sizeof(int));
183 		pos += sizeof(int);
184 		if (len < 0 || len > 10000 || len > end - pos)
185 			break;
186 
187 		r = os_memdup(pos, len);
188 		if (r == NULL)
189 			break;
190 		pos += len;
191 		if (sizeof(*r) + r->ie_len + r->beacon_ie_len > (size_t) len) {
192 			wpa_printf(MSG_ERROR,
193 				   "privsep: Invalid scan result len (%d + %d + %d > %d)",
194 				   (int) sizeof(*r), (int) r->ie_len,
195 				   (int) r->beacon_ie_len, len);
196 			os_free(r);
197 			break;
198 		}
199 
200 		results->res[results->num++] = r;
201 	}
202 
203 	os_free(buf);
204 	return results;
205 }
206 
207 
wpa_driver_privsep_set_key(void * priv,struct wpa_driver_set_key_params * params)208 static int wpa_driver_privsep_set_key(void *priv,
209 				      struct wpa_driver_set_key_params *params)
210 {
211 	struct wpa_driver_privsep_data *drv = priv;
212 	struct privsep_cmd_set_key cmd;
213 	enum wpa_alg alg = params->alg;
214 	const u8 *addr = params->addr;
215 	int key_idx = params->key_idx;
216 	int set_tx = params->set_tx;
217 	const u8 *seq = params->seq;
218 	size_t seq_len = params->seq_len;
219 	const u8 *key = params->key;
220 	size_t key_len = params->key_len;
221 
222 	wpa_printf(MSG_DEBUG, "%s: priv=%p alg=%d key_idx=%d set_tx=%d",
223 		   __func__, priv, alg, key_idx, set_tx);
224 
225 	os_memset(&cmd, 0, sizeof(cmd));
226 	cmd.alg = alg;
227 	if (addr)
228 		os_memcpy(cmd.addr, addr, ETH_ALEN);
229 	else
230 		os_memset(cmd.addr, 0xff, ETH_ALEN);
231 	cmd.key_idx = key_idx;
232 	cmd.set_tx = set_tx;
233 	cmd.key_flag = params->key_flag;
234 	if (seq && seq_len > 0 && seq_len < sizeof(cmd.seq)) {
235 		os_memcpy(cmd.seq, seq, seq_len);
236 		cmd.seq_len = seq_len;
237 	}
238 	if (key && key_len > 0 && key_len < sizeof(cmd.key)) {
239 		os_memcpy(cmd.key, key, key_len);
240 		cmd.key_len = key_len;
241 	}
242 
243 	return wpa_priv_cmd(drv, PRIVSEP_CMD_SET_KEY, &cmd, sizeof(cmd),
244 			    NULL, NULL);
245 }
246 
247 
wpa_driver_privsep_authenticate(void * priv,struct wpa_driver_auth_params * params)248 static int wpa_driver_privsep_authenticate(
249 	void *priv, struct wpa_driver_auth_params *params)
250 {
251 	struct wpa_driver_privsep_data *drv = priv;
252 	struct privsep_cmd_authenticate *data;
253 	int i, res;
254 	size_t buflen;
255 	u8 *pos;
256 
257 	wpa_printf(MSG_DEBUG, "%s: priv=%p freq=%d bssid=" MACSTR_SEC
258 		   " auth_alg=%d local_state_change=%d p2p=%d",
259 		   __func__, priv, params->freq, MAC2STR_SEC(params->bssid),
260 		   params->auth_alg, params->local_state_change, params->p2p);
261 
262 	buflen = sizeof(*data) + params->ie_len + params->auth_data_len;
263 	data = os_zalloc(buflen);
264 	if (data == NULL)
265 		return -1;
266 
267 	data->freq = params->freq;
268 	os_memcpy(data->bssid, params->bssid, ETH_ALEN);
269 	os_memcpy(data->ssid, params->ssid, params->ssid_len);
270 	data->ssid_len = params->ssid_len;
271 	data->auth_alg = params->auth_alg;
272 	data->ie_len = params->ie_len;
273 	for (i = 0; i < 4; i++) {
274 		if (params->wep_key[i])
275 			os_memcpy(data->wep_key[i], params->wep_key[i],
276 				  params->wep_key_len[i]);
277 		data->wep_key_len[i] = params->wep_key_len[i];
278 	}
279 	data->wep_tx_keyidx = params->wep_tx_keyidx;
280 	data->local_state_change = params->local_state_change;
281 	data->p2p = params->p2p;
282 	pos = (u8 *) (data + 1);
283 	if (params->ie_len) {
284 		os_memcpy(pos, params->ie, params->ie_len);
285 		pos += params->ie_len;
286 	}
287 	if (params->auth_data_len)
288 		os_memcpy(pos, params->auth_data, params->auth_data_len);
289 
290 	res = wpa_priv_cmd(drv, PRIVSEP_CMD_AUTHENTICATE, data, buflen,
291 			   NULL, NULL);
292 	os_free(data);
293 
294 	return res;
295 }
296 
297 
wpa_driver_privsep_associate(void * priv,struct wpa_driver_associate_params * params)298 static int wpa_driver_privsep_associate(
299 	void *priv, struct wpa_driver_associate_params *params)
300 {
301 	struct wpa_driver_privsep_data *drv = priv;
302 	struct privsep_cmd_associate *data;
303 	int res;
304 	size_t buflen;
305 
306 	wpa_printf(MSG_DEBUG, "%s: priv=%p freq=%d pairwise_suite=%d "
307 		   "group_suite=%d key_mgmt_suite=%d auth_alg=%d mode=%d",
308 		   __func__, priv, params->freq.freq, params->pairwise_suite,
309 		   params->group_suite, params->key_mgmt_suite,
310 		   params->auth_alg, params->mode);
311 
312 	buflen = sizeof(*data) + params->wpa_ie_len;
313 	data = os_zalloc(buflen);
314 	if (data == NULL)
315 		return -1;
316 
317 	if (params->bssid)
318 		os_memcpy(data->bssid, params->bssid, ETH_ALEN);
319 	os_memcpy(data->ssid, params->ssid, params->ssid_len);
320 	data->ssid_len = params->ssid_len;
321 	data->hwmode = params->freq.mode;
322 	data->freq = params->freq.freq;
323 	data->channel = params->freq.channel;
324 	data->pairwise_suite = params->pairwise_suite;
325 	data->group_suite = params->group_suite;
326 	data->key_mgmt_suite = params->key_mgmt_suite;
327 	data->auth_alg = params->auth_alg;
328 	data->mode = params->mode;
329 	data->wpa_ie_len = params->wpa_ie_len;
330 	if (params->wpa_ie)
331 		os_memcpy(data + 1, params->wpa_ie, params->wpa_ie_len);
332 	/* TODO: add support for other assoc parameters */
333 
334 	res = wpa_priv_cmd(drv, PRIVSEP_CMD_ASSOCIATE, data, buflen,
335 			   NULL, NULL);
336 	os_free(data);
337 
338 	return res;
339 }
340 
341 
wpa_driver_privsep_get_bssid(void * priv,u8 * bssid)342 static int wpa_driver_privsep_get_bssid(void *priv, u8 *bssid)
343 {
344 	struct wpa_driver_privsep_data *drv = priv;
345 	int res;
346 	size_t len = ETH_ALEN;
347 
348 	res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_BSSID, NULL, 0, bssid, &len);
349 	if (res < 0 || len != ETH_ALEN)
350 		return -1;
351 	return 0;
352 }
353 
354 
wpa_driver_privsep_get_ssid(void * priv,u8 * ssid)355 static int wpa_driver_privsep_get_ssid(void *priv, u8 *ssid)
356 {
357 	struct wpa_driver_privsep_data *drv = priv;
358 	int res, ssid_len;
359 	u8 reply[sizeof(int) + SSID_MAX_LEN];
360 	size_t len = sizeof(reply);
361 
362 	res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_SSID, NULL, 0, reply, &len);
363 	if (res < 0 || len < sizeof(int))
364 		return -1;
365 	os_memcpy(&ssid_len, reply, sizeof(int));
366 	if (ssid_len < 0 || ssid_len > SSID_MAX_LEN ||
367 	    sizeof(int) + ssid_len > len) {
368 		wpa_printf(MSG_DEBUG, "privsep: Invalid get SSID reply");
369 		return -1;
370 	}
371 	os_memcpy(ssid, &reply[sizeof(int)], ssid_len);
372 	return ssid_len;
373 }
374 
375 
wpa_driver_privsep_deauthenticate(void * priv,const u8 * addr,u16 reason_code)376 static int wpa_driver_privsep_deauthenticate(void *priv, const u8 *addr,
377 					     u16 reason_code)
378 {
379 	//struct wpa_driver_privsep_data *drv = priv;
380 	wpa_printf(MSG_DEBUG, "%s addr=" MACSTR_SEC " reason_code=%d",
381 		   __func__, MAC2STR_SEC(addr), reason_code);
382 	wpa_printf(MSG_DEBUG, "%s - TODO", __func__);
383 	return 0;
384 }
385 
386 
wpa_driver_privsep_event_auth(void * ctx,u8 * buf,size_t len)387 static void wpa_driver_privsep_event_auth(void *ctx, u8 *buf, size_t len)
388 {
389 	union wpa_event_data data;
390 	struct privsep_event_auth *auth;
391 
392 	os_memset(&data, 0, sizeof(data));
393 	if (len < sizeof(*auth))
394 		return;
395 	auth = (struct privsep_event_auth *) buf;
396 	if (len < sizeof(*auth) + auth->ies_len)
397 		return;
398 
399 	os_memcpy(data.auth.peer, auth->peer, ETH_ALEN);
400 	os_memcpy(data.auth.bssid, auth->bssid, ETH_ALEN);
401 	data.auth.auth_type = auth->auth_type;
402 	data.auth.auth_transaction = auth->auth_transaction;
403 	data.auth.status_code = auth->status_code;
404 	if (auth->ies_len) {
405 		data.auth.ies = (u8 *) (auth + 1);
406 		data.auth.ies_len = auth->ies_len;
407 	}
408 
409 	wpa_supplicant_event(ctx, EVENT_AUTH, &data);
410 }
411 
412 
wpa_driver_privsep_event_assoc(void * ctx,enum wpa_event_type event,u8 * buf,size_t len)413 static void wpa_driver_privsep_event_assoc(void *ctx,
414 					   enum wpa_event_type event,
415 					   u8 *buf, size_t len)
416 {
417 	union wpa_event_data data;
418 	int inc_data = 0;
419 	u8 *pos, *end;
420 	int ie_len;
421 
422 	os_memset(&data, 0, sizeof(data));
423 
424 	pos = buf;
425 	end = buf + len;
426 
427 	if (end - pos < (int) sizeof(int))
428 		return;
429 	os_memcpy(&ie_len, pos, sizeof(int));
430 	pos += sizeof(int);
431 	if (ie_len < 0 || ie_len > end - pos)
432 		return;
433 	if (ie_len) {
434 		data.assoc_info.req_ies = pos;
435 		data.assoc_info.req_ies_len = ie_len;
436 		pos += ie_len;
437 		inc_data = 1;
438 	}
439 
440 	wpa_supplicant_event(ctx, event, inc_data ? &data : NULL);
441 }
442 
443 
wpa_driver_privsep_event_interface_status(void * ctx,u8 * buf,size_t len)444 static void wpa_driver_privsep_event_interface_status(void *ctx, u8 *buf,
445 						      size_t len)
446 {
447 	union wpa_event_data data;
448 	int ievent;
449 
450 	if (len < sizeof(int) ||
451 	    len - sizeof(int) > sizeof(data.interface_status.ifname))
452 		return;
453 
454 	os_memcpy(&ievent, buf, sizeof(int));
455 
456 	os_memset(&data, 0, sizeof(data));
457 	data.interface_status.ievent = ievent;
458 	os_memcpy(data.interface_status.ifname, buf + sizeof(int),
459 		  len - sizeof(int));
460 	wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &data);
461 }
462 
463 
wpa_driver_privsep_event_michael_mic_failure(void * ctx,u8 * buf,size_t len)464 static void wpa_driver_privsep_event_michael_mic_failure(
465 	void *ctx, u8 *buf, size_t len)
466 {
467 	union wpa_event_data data;
468 
469 	if (len != sizeof(int))
470 		return;
471 
472 	os_memset(&data, 0, sizeof(data));
473 	os_memcpy(&data.michael_mic_failure.unicast, buf, sizeof(int));
474 	wpa_supplicant_event(ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
475 }
476 
477 
wpa_driver_privsep_event_pmkid_candidate(void * ctx,u8 * buf,size_t len)478 static void wpa_driver_privsep_event_pmkid_candidate(void *ctx, u8 *buf,
479 						     size_t len)
480 {
481 	union wpa_event_data data;
482 
483 	if (len != sizeof(struct pmkid_candidate))
484 		return;
485 
486 	os_memset(&data, 0, sizeof(data));
487 	os_memcpy(&data.pmkid_candidate, buf, len);
488 	wpa_supplicant_event(ctx, EVENT_PMKID_CANDIDATE, &data);
489 }
490 
491 
wpa_driver_privsep_event_ft_response(void * ctx,u8 * buf,size_t len)492 static void wpa_driver_privsep_event_ft_response(void *ctx, u8 *buf,
493 						 size_t len)
494 {
495 	union wpa_event_data data;
496 
497 	if (len < sizeof(int) + ETH_ALEN)
498 		return;
499 
500 	os_memset(&data, 0, sizeof(data));
501 	os_memcpy(&data.ft_ies.ft_action, buf, sizeof(int));
502 	os_memcpy(data.ft_ies.target_ap, buf + sizeof(int), ETH_ALEN);
503 	data.ft_ies.ies = buf + sizeof(int) + ETH_ALEN;
504 	data.ft_ies.ies_len = len - sizeof(int) - ETH_ALEN;
505 	wpa_supplicant_event(ctx, EVENT_FT_RESPONSE, &data);
506 }
507 
508 
wpa_driver_privsep_event_rx_eapol(void * ctx,u8 * buf,size_t len)509 static void wpa_driver_privsep_event_rx_eapol(void *ctx, u8 *buf, size_t len)
510 {
511 	if (len < ETH_ALEN)
512 		return;
513 	drv_event_eapol_rx(ctx, buf, buf + ETH_ALEN, len - ETH_ALEN);
514 }
515 
516 
wpa_driver_privsep_receive(int sock,void * eloop_ctx,void * sock_ctx)517 static void wpa_driver_privsep_receive(int sock, void *eloop_ctx,
518 				       void *sock_ctx)
519 {
520 	struct wpa_driver_privsep_data *drv = eloop_ctx;
521 	u8 *buf, *event_buf;
522 	size_t event_len;
523 	int res, event;
524 	enum privsep_event e;
525 	struct sockaddr_un from;
526 	socklen_t fromlen = sizeof(from);
527 	const size_t buflen = 2000;
528 
529 	buf = os_malloc(buflen);
530 	if (buf == NULL)
531 		return;
532 	res = recvfrom(sock, buf, buflen, 0,
533 		       (struct sockaddr *) &from, &fromlen);
534 	if (res < 0) {
535 		wpa_printf(MSG_ERROR, "recvfrom(priv_socket): %s",
536 			   strerror(errno));
537 		os_free(buf);
538 		return;
539 	}
540 
541 	wpa_printf(MSG_DEBUG, "privsep_driver: received %u bytes", res);
542 
543 	if (res < (int) sizeof(int)) {
544 		wpa_printf(MSG_DEBUG, "Too short event message (len=%d)", res);
545 		return;
546 	}
547 
548 	os_memcpy(&event, buf, sizeof(int));
549 	event_buf = &buf[sizeof(int)];
550 	event_len = res - sizeof(int);
551 	wpa_printf(MSG_DEBUG, "privsep: Event %d received (len=%lu)",
552 		   event, (unsigned long) event_len);
553 
554 	e = event;
555 	switch (e) {
556 	case PRIVSEP_EVENT_SCAN_RESULTS:
557 		wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, NULL);
558 		break;
559 	case PRIVSEP_EVENT_SCAN_STARTED:
560 		wpa_supplicant_event(drv->ctx, EVENT_SCAN_STARTED, NULL);
561 		break;
562 	case PRIVSEP_EVENT_ASSOC:
563 		wpa_driver_privsep_event_assoc(drv->ctx, EVENT_ASSOC,
564 					       event_buf, event_len);
565 		break;
566 	case PRIVSEP_EVENT_DISASSOC:
567 		wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
568 		break;
569 	case PRIVSEP_EVENT_ASSOCINFO:
570 		wpa_driver_privsep_event_assoc(drv->ctx, EVENT_ASSOCINFO,
571 					       event_buf, event_len);
572 		break;
573 	case PRIVSEP_EVENT_MICHAEL_MIC_FAILURE:
574 		wpa_driver_privsep_event_michael_mic_failure(
575 			drv->ctx, event_buf, event_len);
576 		break;
577 	case PRIVSEP_EVENT_INTERFACE_STATUS:
578 		wpa_driver_privsep_event_interface_status(drv->ctx, event_buf,
579 							  event_len);
580 		break;
581 	case PRIVSEP_EVENT_PMKID_CANDIDATE:
582 		wpa_driver_privsep_event_pmkid_candidate(drv->ctx, event_buf,
583 							 event_len);
584 		break;
585 	case PRIVSEP_EVENT_FT_RESPONSE:
586 		wpa_driver_privsep_event_ft_response(drv->ctx, event_buf,
587 						     event_len);
588 		break;
589 	case PRIVSEP_EVENT_RX_EAPOL:
590 		wpa_driver_privsep_event_rx_eapol(drv->ctx, event_buf,
591 						  event_len);
592 		break;
593 	case PRIVSEP_EVENT_AUTH:
594 		wpa_driver_privsep_event_auth(drv->ctx, event_buf, event_len);
595 		break;
596 	}
597 
598 	os_free(buf);
599 }
600 
601 
wpa_driver_privsep_init(void * ctx,const char * ifname)602 static void * wpa_driver_privsep_init(void *ctx, const char *ifname)
603 {
604 	struct wpa_driver_privsep_data *drv;
605 
606 	drv = os_zalloc(sizeof(*drv));
607 	if (drv == NULL)
608 		return NULL;
609 	drv->ctx = ctx;
610 	drv->priv_socket = -1;
611 	drv->cmd_socket = -1;
612 	os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
613 
614 	return drv;
615 }
616 
617 
wpa_driver_privsep_deinit(void * priv)618 static void wpa_driver_privsep_deinit(void *priv)
619 {
620 	struct wpa_driver_privsep_data *drv = priv;
621 
622 	if (drv->priv_socket >= 0) {
623 		wpa_priv_reg_cmd(drv, PRIVSEP_CMD_UNREGISTER);
624 		eloop_unregister_read_sock(drv->priv_socket);
625 		close(drv->priv_socket);
626 	}
627 
628 	if (drv->own_socket_path) {
629 		unlink(drv->own_socket_path);
630 		os_free(drv->own_socket_path);
631 	}
632 
633 	if (drv->cmd_socket >= 0) {
634 		eloop_unregister_read_sock(drv->cmd_socket);
635 		close(drv->cmd_socket);
636 	}
637 
638 	if (drv->own_cmd_path) {
639 		unlink(drv->own_cmd_path);
640 		os_free(drv->own_cmd_path);
641 	}
642 
643 	os_free(drv);
644 }
645 
646 
wpa_driver_privsep_set_param(void * priv,const char * param)647 static int wpa_driver_privsep_set_param(void *priv, const char *param)
648 {
649 	struct wpa_driver_privsep_data *drv = priv;
650 	const char *pos;
651 	char *own_dir, *priv_dir;
652 	static unsigned int counter = 0;
653 	size_t len;
654 	struct sockaddr_un addr;
655 
656 	wpa_printf(MSG_DEBUG, "%s: param='%s'", __func__, param);
657 	if (param == NULL)
658 		pos = NULL;
659 	else
660 		pos = os_strstr(param, "own_dir=");
661 	if (pos) {
662 		char *end;
663 		own_dir = os_strdup(pos + 8);
664 		if (own_dir == NULL)
665 			return -1;
666 		end = os_strchr(own_dir, ' ');
667 		if (end)
668 			*end = '\0';
669 	} else {
670 		own_dir = os_strdup("/tmp");
671 		if (own_dir == NULL)
672 			return -1;
673 	}
674 
675 	if (param == NULL)
676 		pos = NULL;
677 	else
678 		pos = os_strstr(param, "priv_dir=");
679 	if (pos) {
680 		char *end;
681 		priv_dir = os_strdup(pos + 9);
682 		if (priv_dir == NULL) {
683 			os_free(own_dir);
684 			return -1;
685 		}
686 		end = os_strchr(priv_dir, ' ');
687 		if (end)
688 			*end = '\0';
689 	} else {
690 		priv_dir = os_strdup("/var/run/wpa_priv");
691 		if (priv_dir == NULL) {
692 			os_free(own_dir);
693 			return -1;
694 		}
695 	}
696 
697 	len = os_strlen(own_dir) + 50;
698 	drv->own_socket_path = os_malloc(len);
699 	if (drv->own_socket_path == NULL) {
700 		os_free(priv_dir);
701 		os_free(own_dir);
702 		return -1;
703 	}
704 	os_snprintf(drv->own_socket_path, len, "%s/wpa_privsep-%d-%d",
705 		    own_dir, getpid(), counter++);
706 
707 	len = os_strlen(own_dir) + 50;
708 	drv->own_cmd_path = os_malloc(len);
709 	if (drv->own_cmd_path == NULL) {
710 		os_free(drv->own_socket_path);
711 		drv->own_socket_path = NULL;
712 		os_free(priv_dir);
713 		os_free(own_dir);
714 		return -1;
715 	}
716 	os_snprintf(drv->own_cmd_path, len, "%s/wpa_privsep-%d-%d",
717 		    own_dir, getpid(), counter++);
718 
719 	os_free(own_dir);
720 
721 	drv->priv_addr.sun_family = AF_UNIX;
722 	os_snprintf(drv->priv_addr.sun_path, sizeof(drv->priv_addr.sun_path),
723 		    "%s/%s", priv_dir, drv->ifname);
724 	os_free(priv_dir);
725 
726 	drv->priv_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
727 	if (drv->priv_socket < 0) {
728 		wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
729 		os_free(drv->own_socket_path);
730 		drv->own_socket_path = NULL;
731 		return -1;
732 	}
733 
734 	os_memset(&addr, 0, sizeof(addr));
735 	addr.sun_family = AF_UNIX;
736 	os_strlcpy(addr.sun_path, drv->own_socket_path, sizeof(addr.sun_path));
737 	if (bind(drv->priv_socket, (struct sockaddr *) &addr, sizeof(addr)) <
738 	    0) {
739 		wpa_printf(MSG_ERROR,
740 			   "privsep-set-params priv-sock: bind(PF_UNIX): %s",
741 			   strerror(errno));
742 		close(drv->priv_socket);
743 		drv->priv_socket = -1;
744 		unlink(drv->own_socket_path);
745 		os_free(drv->own_socket_path);
746 		drv->own_socket_path = NULL;
747 		return -1;
748 	}
749 
750 	eloop_register_read_sock(drv->priv_socket, wpa_driver_privsep_receive,
751 				 drv, NULL);
752 
753 	drv->cmd_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
754 	if (drv->cmd_socket < 0) {
755 		wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
756 		os_free(drv->own_cmd_path);
757 		drv->own_cmd_path = NULL;
758 		return -1;
759 	}
760 
761 	os_memset(&addr, 0, sizeof(addr));
762 	addr.sun_family = AF_UNIX;
763 	os_strlcpy(addr.sun_path, drv->own_cmd_path, sizeof(addr.sun_path));
764 	if (bind(drv->cmd_socket, (struct sockaddr *) &addr, sizeof(addr)) < 0)
765 	{
766 		wpa_printf(MSG_ERROR,
767 			   "privsep-set-params cmd-sock: bind(PF_UNIX): %s",
768 			   strerror(errno));
769 		close(drv->cmd_socket);
770 		drv->cmd_socket = -1;
771 		unlink(drv->own_cmd_path);
772 		os_free(drv->own_cmd_path);
773 		drv->own_cmd_path = NULL;
774 		return -1;
775 	}
776 
777 	if (wpa_priv_reg_cmd(drv, PRIVSEP_CMD_REGISTER) < 0) {
778 		wpa_printf(MSG_ERROR, "Failed to register with wpa_priv");
779 		return -1;
780 	}
781 
782 	return 0;
783 }
784 
785 
wpa_driver_privsep_get_capa(void * priv,struct wpa_driver_capa * capa)786 static int wpa_driver_privsep_get_capa(void *priv,
787 				       struct wpa_driver_capa *capa)
788 {
789 	struct wpa_driver_privsep_data *drv = priv;
790 	int res;
791 	size_t len = sizeof(*capa);
792 
793 	res = wpa_priv_cmd(drv, PRIVSEP_CMD_GET_CAPA, NULL, 0, capa, &len);
794 	if (res < 0 || len != sizeof(*capa))
795 		return -1;
796 	/* For now, no support for passing extended_capa pointers */
797 	capa->extended_capa = NULL;
798 	capa->extended_capa_mask = NULL;
799 	capa->extended_capa_len = 0;
800 	/* Control port is not yet supported */
801 	capa->flags &= ~WPA_DRIVER_FLAGS_CONTROL_PORT;
802 	return 0;
803 }
804 
805 
wpa_driver_privsep_get_mac_addr(void * priv)806 static const u8 * wpa_driver_privsep_get_mac_addr(void *priv)
807 {
808 	struct wpa_driver_privsep_data *drv = priv;
809 	wpa_printf(MSG_DEBUG, "%s", __func__);
810 	return drv->own_addr;
811 }
812 
813 
wpa_driver_privsep_set_country(void * priv,const char * alpha2)814 static int wpa_driver_privsep_set_country(void *priv, const char *alpha2)
815 {
816 	struct wpa_driver_privsep_data *drv = priv;
817 	wpa_printf(MSG_DEBUG, "%s country='%s'", __func__, alpha2);
818 	return wpa_priv_cmd(drv, PRIVSEP_CMD_SET_COUNTRY, alpha2,
819 			    os_strlen(alpha2), NULL, NULL);
820 }
821 
822 
823 struct wpa_driver_ops wpa_driver_privsep_ops = {
824 	"privsep",
825 	"wpa_supplicant privilege separated driver",
826 	.get_bssid = wpa_driver_privsep_get_bssid,
827 	.get_ssid = wpa_driver_privsep_get_ssid,
828 	.set_key = wpa_driver_privsep_set_key,
829 	.init = wpa_driver_privsep_init,
830 	.deinit = wpa_driver_privsep_deinit,
831 	.set_param = wpa_driver_privsep_set_param,
832 	.scan2 = wpa_driver_privsep_scan,
833 	.deauthenticate = wpa_driver_privsep_deauthenticate,
834 	.authenticate = wpa_driver_privsep_authenticate,
835 	.associate = wpa_driver_privsep_associate,
836 	.get_capa = wpa_driver_privsep_get_capa,
837 	.get_mac_addr = wpa_driver_privsep_get_mac_addr,
838 	.get_scan_results2 = wpa_driver_privsep_get_scan_results2,
839 	.set_country = wpa_driver_privsep_set_country,
840 };
841 
842 
843 const struct wpa_driver_ops *const wpa_drivers[] =
844 {
845 	&wpa_driver_privsep_ops,
846 	NULL
847 };
848