• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * WPA Supplicant / privileged helper program
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 #ifdef __linux__
11 #include <fcntl.h>
12 #endif /* __linux__ */
13 #include <sys/un.h>
14 #include <sys/stat.h>
15 
16 #include "common.h"
17 #include "eloop.h"
18 #include "common/version.h"
19 #include "drivers/driver.h"
20 #include "l2_packet/l2_packet.h"
21 #include "common/privsep_commands.h"
22 #include "common/ieee802_11_defs.h"
23 
24 #define WPA_PRIV_MAX_L2 3
25 
26 struct wpa_priv_interface {
27 	struct wpa_priv_interface *next;
28 	char *driver_name;
29 	char *ifname;
30 	char *sock_name;
31 	int fd;
32 
33 	void *ctx;
34 
35 	const struct wpa_driver_ops *driver;
36 	void *drv_priv;
37 	void *drv_global_priv;
38 	struct sockaddr_un drv_addr;
39 	socklen_t drv_addr_len;
40 	int wpas_registered;
41 
42 	struct l2_packet_data *l2[WPA_PRIV_MAX_L2];
43 	struct sockaddr_un l2_addr[WPA_PRIV_MAX_L2];
44 	socklen_t l2_addr_len[WPA_PRIV_MAX_L2];
45 	struct wpa_priv_l2 {
46 		struct wpa_priv_interface *parent;
47 		int idx;
48 	} l2_ctx[WPA_PRIV_MAX_L2];
49 };
50 
51 struct wpa_priv_global {
52 	struct wpa_priv_interface *interfaces;
53 };
54 
55 
wpa_priv_cmd_register(struct wpa_priv_interface * iface,struct sockaddr_un * from,socklen_t fromlen)56 static void wpa_priv_cmd_register(struct wpa_priv_interface *iface,
57 				  struct sockaddr_un *from, socklen_t fromlen)
58 {
59 	int i;
60 
61 	if (iface->drv_priv) {
62 		wpa_printf(MSG_DEBUG, "Cleaning up forgotten driver instance");
63 		if (iface->driver->deinit)
64 			iface->driver->deinit(iface->drv_priv);
65 		iface->drv_priv = NULL;
66 		if (iface->drv_global_priv) {
67 			iface->driver->global_deinit(iface->drv_global_priv);
68 			iface->drv_global_priv = NULL;
69 		}
70 		iface->wpas_registered = 0;
71 	}
72 
73 	for (i = 0; i < WPA_PRIV_MAX_L2; i++) {
74 		if (iface->l2[i]) {
75 			wpa_printf(MSG_DEBUG,
76 				   "Cleaning up forgotten l2_packet instance");
77 			l2_packet_deinit(iface->l2[i]);
78 			iface->l2[i] = NULL;
79 		}
80 	}
81 
82 	if (iface->driver->init2) {
83 		if (iface->driver->global_init) {
84 			iface->drv_global_priv =
85 				iface->driver->global_init(iface->ctx);
86 			if (!iface->drv_global_priv) {
87 				wpa_printf(MSG_INFO,
88 					   "Failed to initialize driver global context");
89 				return;
90 			}
91 		} else {
92 			iface->drv_global_priv = NULL;
93 		}
94 		iface->drv_priv = iface->driver->init2(iface, iface->ifname,
95 						       iface->drv_global_priv);
96 	} else if (iface->driver->init) {
97 		iface->drv_priv = iface->driver->init(iface, iface->ifname);
98 	} else {
99 		return;
100 	}
101 	if (iface->drv_priv == NULL) {
102 		wpa_printf(MSG_DEBUG, "Failed to initialize driver wrapper");
103 		return;
104 	}
105 
106 	wpa_printf(MSG_DEBUG, "Driver wrapper '%s' initialized for interface "
107 		   "'%s'", iface->driver_name, iface->ifname);
108 
109 	os_memcpy(&iface->drv_addr, from, fromlen);
110 	iface->drv_addr_len = fromlen;
111 	iface->wpas_registered = 1;
112 
113 	if (iface->driver->set_param &&
114 	    iface->driver->set_param(iface->drv_priv, NULL) < 0) {
115 		wpa_printf(MSG_ERROR, "Driver interface rejected param");
116 	}
117 }
118 
119 
wpa_priv_cmd_unregister(struct wpa_priv_interface * iface,struct sockaddr_un * from)120 static void wpa_priv_cmd_unregister(struct wpa_priv_interface *iface,
121 				    struct sockaddr_un *from)
122 {
123 	if (iface->drv_priv) {
124 		if (iface->driver->deinit)
125 			iface->driver->deinit(iface->drv_priv);
126 		iface->drv_priv = NULL;
127 		if (iface->drv_global_priv) {
128 			iface->driver->global_deinit(iface->drv_global_priv);
129 			iface->drv_global_priv = NULL;
130 		}
131 		iface->wpas_registered = 0;
132 	}
133 }
134 
135 
wpa_priv_cmd_scan(struct wpa_priv_interface * iface,void * buf,size_t len)136 static void wpa_priv_cmd_scan(struct wpa_priv_interface *iface,
137 			      void *buf, size_t len)
138 {
139 	struct wpa_driver_scan_params params;
140 	struct privsep_cmd_scan *scan;
141 	unsigned int i;
142 	int freqs[PRIVSEP_MAX_SCAN_FREQS + 1];
143 
144 	if (iface->drv_priv == NULL)
145 		return;
146 
147 	if (len < sizeof(*scan)) {
148 		wpa_printf(MSG_DEBUG, "Invalid scan request");
149 		return;
150 	}
151 
152 	scan = buf;
153 
154 	os_memset(&params, 0, sizeof(params));
155 	if (scan->num_ssids > WPAS_MAX_SCAN_SSIDS) {
156 		wpa_printf(MSG_DEBUG, "Invalid scan request (num_ssids)");
157 		return;
158 	}
159 	params.num_ssids = scan->num_ssids;
160 	for (i = 0; i < scan->num_ssids; i++) {
161 		params.ssids[i].ssid = scan->ssids[i];
162 		params.ssids[i].ssid_len = scan->ssid_lens[i];
163 	}
164 
165 	if (scan->num_freqs > PRIVSEP_MAX_SCAN_FREQS) {
166 		wpa_printf(MSG_DEBUG, "Invalid scan request (num_freqs)");
167 		return;
168 	}
169 	if (scan->num_freqs) {
170 		for (i = 0; i < scan->num_freqs; i++)
171 			freqs[i] = scan->freqs[i];
172 		freqs[i] = 0;
173 		params.freqs = freqs;
174 	}
175 
176 	if (iface->driver->scan2)
177 		iface->driver->scan2(iface->drv_priv, &params);
178 }
179 
180 
wpa_priv_get_scan_results2(struct wpa_priv_interface * iface,struct sockaddr_un * from,socklen_t fromlen)181 static void wpa_priv_get_scan_results2(struct wpa_priv_interface *iface,
182 				       struct sockaddr_un *from,
183 				       socklen_t fromlen)
184 {
185 	struct wpa_scan_results *res;
186 	u8 *buf = NULL, *pos, *end;
187 	int val;
188 	size_t i;
189 
190 	if (iface->driver->get_scan_results)
191 		res = iface->driver->get_scan_results(iface->drv_priv, NULL);
192 	else
193 		res = iface->driver->get_scan_results2(iface->drv_priv);
194 	if (res == NULL)
195 		goto fail;
196 
197 	buf = os_malloc(60000);
198 	if (buf == NULL)
199 		goto fail;
200 	pos = buf;
201 	end = buf + 60000;
202 	val = res->num;
203 	os_memcpy(pos, &val, sizeof(int));
204 	pos += sizeof(int);
205 
206 	for (i = 0; i < res->num; i++) {
207 		struct wpa_scan_res *r = res->res[i];
208 		val = sizeof(*r) + r->ie_len + r->beacon_ie_len;
209 		if (end - pos < (int) sizeof(int) + val)
210 			break;
211 		os_memcpy(pos, &val, sizeof(int));
212 		pos += sizeof(int);
213 		os_memcpy(pos, r, val);
214 		pos += val;
215 	}
216 
217 	sendto(iface->fd, buf, pos - buf, 0, (struct sockaddr *) from, fromlen);
218 
219 	os_free(buf);
220 	wpa_scan_results_free(res);
221 	return;
222 
223 fail:
224 	os_free(buf);
225 	wpa_scan_results_free(res);
226 	sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, fromlen);
227 }
228 
229 
wpa_priv_cmd_get_scan_results(struct wpa_priv_interface * iface,struct sockaddr_un * from,socklen_t fromlen)230 static void wpa_priv_cmd_get_scan_results(struct wpa_priv_interface *iface,
231 					  struct sockaddr_un *from,
232 					  socklen_t fromlen)
233 {
234 	if (iface->drv_priv == NULL)
235 		return;
236 
237 	if (iface->driver->get_scan_results || iface->driver->get_scan_results2)
238 		wpa_priv_get_scan_results2(iface, from, fromlen);
239 	else
240 		sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, fromlen);
241 }
242 
243 
wpa_priv_cmd_authenticate(struct wpa_priv_interface * iface,void * buf,size_t len)244 static void wpa_priv_cmd_authenticate(struct wpa_priv_interface *iface,
245 				      void *buf, size_t len)
246 {
247 	struct wpa_driver_auth_params params;
248 	struct privsep_cmd_authenticate *auth;
249 	int res, i;
250 
251 	if (iface->drv_priv == NULL || iface->driver->authenticate == NULL)
252 		return;
253 
254 	if (len < sizeof(*auth)) {
255 		wpa_printf(MSG_DEBUG, "Invalid authentication request");
256 		return;
257 	}
258 
259 	auth = buf;
260 	if (sizeof(*auth) + auth->ie_len + auth->auth_data_len > len) {
261 		wpa_printf(MSG_DEBUG, "Authentication request overflow");
262 		return;
263 	}
264 
265 	os_memset(&params, 0, sizeof(params));
266 	params.freq = auth->freq;
267 	params.bssid = auth->bssid;
268 	params.ssid = auth->ssid;
269 	if (auth->ssid_len > SSID_MAX_LEN)
270 		return;
271 	params.ssid_len = auth->ssid_len;
272 	params.auth_alg = auth->auth_alg;
273 	for (i = 0; i < 4; i++) {
274 		if (auth->wep_key_len[i]) {
275 			params.wep_key[i] = auth->wep_key[i];
276 			params.wep_key_len[i] = auth->wep_key_len[i];
277 		}
278 	}
279 	params.wep_tx_keyidx = auth->wep_tx_keyidx;
280 	params.local_state_change = auth->local_state_change;
281 	params.p2p = auth->p2p;
282 	if (auth->ie_len) {
283 		params.ie = (u8 *) (auth + 1);
284 		params.ie_len = auth->ie_len;
285 	}
286 	if (auth->auth_data_len) {
287 		params.auth_data = ((u8 *) (auth + 1)) + auth->ie_len;
288 		params.auth_data_len = auth->auth_data_len;
289 	}
290 
291 	res = iface->driver->authenticate(iface->drv_priv, &params);
292 	wpa_printf(MSG_DEBUG, "drv->authenticate: res=%d", res);
293 }
294 
295 
wpa_priv_cmd_associate(struct wpa_priv_interface * iface,void * buf,size_t len)296 static void wpa_priv_cmd_associate(struct wpa_priv_interface *iface,
297 				   void *buf, size_t len)
298 {
299 	struct wpa_driver_associate_params params;
300 	struct privsep_cmd_associate *assoc;
301 	u8 *bssid;
302 	int res;
303 
304 	if (iface->drv_priv == NULL || iface->driver->associate == NULL)
305 		return;
306 
307 	if (len < sizeof(*assoc)) {
308 		wpa_printf(MSG_DEBUG, "Invalid association request");
309 		return;
310 	}
311 
312 	assoc = buf;
313 	if (sizeof(*assoc) + assoc->wpa_ie_len > len) {
314 		wpa_printf(MSG_DEBUG, "Association request overflow");
315 		return;
316 	}
317 
318 	os_memset(&params, 0, sizeof(params));
319 	bssid = assoc->bssid;
320 	if (bssid[0] | bssid[1] | bssid[2] | bssid[3] | bssid[4] | bssid[5])
321 		params.bssid = bssid;
322 	params.ssid = assoc->ssid;
323 	if (assoc->ssid_len > SSID_MAX_LEN)
324 		return;
325 	params.ssid_len = assoc->ssid_len;
326 	params.freq.mode = assoc->hwmode;
327 	params.freq.freq = assoc->freq;
328 	params.freq.channel = assoc->channel;
329 	if (assoc->wpa_ie_len) {
330 		params.wpa_ie = (u8 *) (assoc + 1);
331 		params.wpa_ie_len = assoc->wpa_ie_len;
332 	}
333 	params.pairwise_suite = assoc->pairwise_suite;
334 	params.group_suite = assoc->group_suite;
335 	params.key_mgmt_suite = assoc->key_mgmt_suite;
336 	params.auth_alg = assoc->auth_alg;
337 	params.mode = assoc->mode;
338 
339 	res = iface->driver->associate(iface->drv_priv, &params);
340 	wpa_printf(MSG_DEBUG, "drv->associate: res=%d", res);
341 }
342 
343 
wpa_priv_cmd_get_bssid(struct wpa_priv_interface * iface,struct sockaddr_un * from,socklen_t fromlen)344 static void wpa_priv_cmd_get_bssid(struct wpa_priv_interface *iface,
345 				   struct sockaddr_un *from, socklen_t fromlen)
346 {
347 	u8 bssid[ETH_ALEN];
348 
349 	if (iface->drv_priv == NULL)
350 		goto fail;
351 
352 	if (iface->driver->get_bssid == NULL ||
353 	    iface->driver->get_bssid(iface->drv_priv, bssid) < 0)
354 		goto fail;
355 
356 	sendto(iface->fd, bssid, ETH_ALEN, 0, (struct sockaddr *) from,
357 	       fromlen);
358 	return;
359 
360 fail:
361 	sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, fromlen);
362 }
363 
364 
wpa_priv_cmd_get_ssid(struct wpa_priv_interface * iface,struct sockaddr_un * from,socklen_t fromlen)365 static void wpa_priv_cmd_get_ssid(struct wpa_priv_interface *iface,
366 				  struct sockaddr_un *from, socklen_t fromlen)
367 {
368 	u8 ssid[sizeof(int) + SSID_MAX_LEN];
369 	int res;
370 
371 	if (iface->drv_priv == NULL)
372 		goto fail;
373 
374 	if (iface->driver->get_ssid == NULL)
375 		goto fail;
376 
377 	os_memset(ssid, 0, sizeof(ssid));
378 	res = iface->driver->get_ssid(iface->drv_priv, &ssid[sizeof(int)]);
379 	if (res < 0 || res > SSID_MAX_LEN)
380 		goto fail;
381 	os_memcpy(ssid, &res, sizeof(int));
382 
383 	sendto(iface->fd, ssid, sizeof(ssid), 0, (struct sockaddr *) from,
384 	       fromlen);
385 	return;
386 
387 fail:
388 	sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, fromlen);
389 }
390 
391 
wpa_priv_cmd_set_key(struct wpa_priv_interface * iface,void * buf,size_t len)392 static void wpa_priv_cmd_set_key(struct wpa_priv_interface *iface,
393 				 void *buf, size_t len)
394 {
395 	struct privsep_cmd_set_key *params;
396 	int res;
397 	struct wpa_driver_set_key_params p;
398 
399 	if (iface->drv_priv == NULL || iface->driver->set_key == NULL)
400 		return;
401 
402 	if (len != sizeof(*params)) {
403 		wpa_printf(MSG_DEBUG, "Invalid set_key request");
404 		return;
405 	}
406 
407 	params = buf;
408 
409 	os_memset(&p, 0, sizeof(p));
410 	p.ifname = iface->ifname;
411 	p.alg = params->alg;
412 	p.addr = params->addr;
413 	p.key_idx = params->key_idx;
414 	p.set_tx = params->set_tx;
415 	p.seq = params->seq_len ? params->seq : NULL;
416 	p.seq_len = params->seq_len;
417 	p.key = params->key_len ? params->key : NULL;
418 	p.key_len = params->key_len;
419 	p.key_flag = params->key_flag;
420 #ifdef CONFIG_MLD_PATCH_EXT
421 	p.link_id = -1;
422 #endif
423 
424 	res = iface->driver->set_key(iface->drv_priv, &p);
425 	wpa_printf(MSG_DEBUG, "drv->set_key: res=%d", res);
426 }
427 
428 
wpa_priv_cmd_get_capa(struct wpa_priv_interface * iface,struct sockaddr_un * from,socklen_t fromlen)429 static void wpa_priv_cmd_get_capa(struct wpa_priv_interface *iface,
430 				  struct sockaddr_un *from, socklen_t fromlen)
431 {
432 	struct wpa_driver_capa capa;
433 
434 	if (iface->drv_priv == NULL)
435 		goto fail;
436 
437 	if (iface->driver->get_capa == NULL ||
438 	    iface->driver->get_capa(iface->drv_priv, &capa) < 0)
439 		goto fail;
440 
441 	/* For now, no support for passing extended_capa pointers */
442 	capa.extended_capa = NULL;
443 	capa.extended_capa_mask = NULL;
444 	capa.extended_capa_len = 0;
445 	sendto(iface->fd, &capa, sizeof(capa), 0, (struct sockaddr *) from,
446 	       fromlen);
447 	return;
448 
449 fail:
450 	sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, fromlen);
451 }
452 
453 
wpa_priv_l2_rx(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)454 static void wpa_priv_l2_rx(void *ctx, const u8 *src_addr, const u8 *buf,
455 			   size_t len)
456 {
457 	struct wpa_priv_l2 *l2_ctx = ctx;
458 	struct wpa_priv_interface *iface = l2_ctx->parent;
459 	struct msghdr msg;
460 	struct iovec io[2];
461 
462 	io[0].iov_base = (u8 *) src_addr;
463 	io[0].iov_len = ETH_ALEN;
464 	io[1].iov_base = (u8 *) buf;
465 	io[1].iov_len = len;
466 
467 	os_memset(&msg, 0, sizeof(msg));
468 	msg.msg_iov = io;
469 	msg.msg_iovlen = 2;
470 	msg.msg_name = &iface->l2_addr[l2_ctx->idx];
471 	msg.msg_namelen = iface->l2_addr_len[l2_ctx->idx];
472 
473 	if (sendmsg(iface->fd, &msg, 0) < 0) {
474 		wpa_printf(MSG_ERROR, "sendmsg(l2 rx): %s", strerror(errno));
475 	}
476 }
477 
478 
wpa_priv_allowed_l2_proto(u16 proto)479 static int wpa_priv_allowed_l2_proto(u16 proto)
480 {
481 	return proto == ETH_P_EAPOL || proto == ETH_P_RSN_PREAUTH ||
482 		proto == ETH_P_80211_ENCAP;
483 }
484 
485 
wpa_priv_cmd_l2_register(struct wpa_priv_interface * iface,struct sockaddr_un * from,socklen_t fromlen,void * buf,size_t len)486 static void wpa_priv_cmd_l2_register(struct wpa_priv_interface *iface,
487 				     struct sockaddr_un *from,
488 				     socklen_t fromlen,
489 				     void *buf, size_t len)
490 {
491 	int *reg_cmd = buf;
492 	u8 own_addr[ETH_ALEN];
493 	int res;
494 	u16 proto;
495 	int idx;
496 
497 	if (len != 2 * sizeof(int)) {
498 		wpa_printf(MSG_DEBUG, "Invalid l2_register length %lu",
499 			   (unsigned long) len);
500 		return;
501 	}
502 
503 	proto = reg_cmd[0];
504 	if (!wpa_priv_allowed_l2_proto(proto)) {
505 		wpa_printf(MSG_DEBUG, "Refused l2_packet connection for "
506 			   "ethertype 0x%x", proto);
507 		return;
508 	}
509 
510 	for (idx = 0; idx < WPA_PRIV_MAX_L2; idx++) {
511 		if (!iface->l2[idx])
512 			break;
513 	}
514 	if (idx == WPA_PRIV_MAX_L2) {
515 		wpa_printf(MSG_DEBUG, "No free l2_packet connection found");
516 		return;
517 	}
518 
519 	os_memcpy(&iface->l2_addr[idx], from, fromlen);
520 	iface->l2_addr_len[idx] = fromlen;
521 
522 	iface->l2_ctx[idx].idx = idx;
523 	iface->l2_ctx[idx].parent = iface;
524 	iface->l2[idx] = l2_packet_init(iface->ifname, NULL, proto,
525 					wpa_priv_l2_rx, &iface->l2_ctx[idx],
526 					reg_cmd[1]);
527 	if (!iface->l2[idx]) {
528 		wpa_printf(MSG_DEBUG, "Failed to initialize l2_packet "
529 			   "instance for protocol %d", proto);
530 		return;
531 	}
532 
533 	if (l2_packet_get_own_addr(iface->l2[idx], own_addr) < 0) {
534 		wpa_printf(MSG_DEBUG, "Failed to get own address from "
535 			   "l2_packet");
536 		l2_packet_deinit(iface->l2[idx]);
537 		iface->l2[idx] = NULL;
538 		return;
539 	}
540 
541 	res = sendto(iface->fd, own_addr, ETH_ALEN, 0,
542 		     (struct sockaddr *) from, fromlen);
543 	wpa_printf(MSG_DEBUG, "L2 registration[idx=%d]: res=%d", idx, res);
544 }
545 
546 
wpa_priv_cmd_l2_unregister(struct wpa_priv_interface * iface,struct sockaddr_un * from,socklen_t fromlen)547 static void wpa_priv_cmd_l2_unregister(struct wpa_priv_interface *iface,
548 				       struct sockaddr_un *from,
549 				       socklen_t fromlen)
550 {
551 	int idx;
552 
553 	for (idx = 0; idx < WPA_PRIV_MAX_L2; idx++) {
554 		if (iface->l2_addr_len[idx] == fromlen &&
555 		    os_memcmp(&iface->l2_addr[idx], from, fromlen) == 0)
556 			break;
557 	}
558 	if (idx == WPA_PRIV_MAX_L2) {
559 		wpa_printf(MSG_DEBUG,
560 			   "No registered l2_packet socket found for unregister request");
561 		return;
562 	}
563 
564 	if (iface->l2[idx]) {
565 		l2_packet_deinit(iface->l2[idx]);
566 		iface->l2[idx] = NULL;
567 	}
568 }
569 
570 
wpa_priv_cmd_l2_notify_auth_start(struct wpa_priv_interface * iface,struct sockaddr_un * from)571 static void wpa_priv_cmd_l2_notify_auth_start(struct wpa_priv_interface *iface,
572 					      struct sockaddr_un *from)
573 {
574 	int idx;
575 
576 	for (idx = 0; idx < WPA_PRIV_MAX_L2; idx++) {
577 		if (iface->l2[idx])
578 			l2_packet_notify_auth_start(iface->l2[idx]);
579 	}
580 }
581 
582 
wpa_priv_cmd_l2_send(struct wpa_priv_interface * iface,struct sockaddr_un * from,socklen_t fromlen,void * buf,size_t len)583 static void wpa_priv_cmd_l2_send(struct wpa_priv_interface *iface,
584 				 struct sockaddr_un *from, socklen_t fromlen,
585 				 void *buf, size_t len)
586 {
587 	u8 *dst_addr;
588 	u16 proto;
589 	int res;
590 	int idx;
591 
592 	for (idx = 0; idx < WPA_PRIV_MAX_L2; idx++) {
593 		if (iface->l2_addr_len[idx] == fromlen &&
594 		    os_memcmp(&iface->l2_addr[idx], from, fromlen) == 0)
595 			break;
596 	}
597 	if (idx == WPA_PRIV_MAX_L2) {
598 		wpa_printf(MSG_DEBUG,
599 			   "No registered l2_packet socket found for send request");
600 		return;
601 	}
602 
603 	if (iface->l2[idx] == NULL)
604 		return;
605 
606 	if (len < ETH_ALEN + 2) {
607 		wpa_printf(MSG_DEBUG, "Too short L2 send packet (len=%lu)",
608 			   (unsigned long) len);
609 		return;
610 	}
611 
612 	dst_addr = buf;
613 	os_memcpy(&proto, (char *) buf + ETH_ALEN, 2);
614 
615 	if (!wpa_priv_allowed_l2_proto(proto)) {
616 		wpa_printf(MSG_DEBUG, "Refused l2_packet send for ethertype "
617 			   "0x%x", proto);
618 		return;
619 	}
620 
621 	res = l2_packet_send(iface->l2[idx], dst_addr, proto,
622 			     (unsigned char *) buf + ETH_ALEN + 2,
623 			     len - ETH_ALEN - 2);
624 	wpa_printf(MSG_DEBUG, "L2 send[idx=%d]: res=%d", idx, res);
625 }
626 
627 
wpa_priv_cmd_set_country(struct wpa_priv_interface * iface,char * buf)628 static void wpa_priv_cmd_set_country(struct wpa_priv_interface *iface,
629 				     char *buf)
630 {
631 	if (iface->drv_priv == NULL || iface->driver->set_country == NULL ||
632 	    *buf == '\0')
633 		return;
634 
635 	iface->driver->set_country(iface->drv_priv, buf);
636 }
637 
638 
wpa_priv_receive(int sock,void * eloop_ctx,void * sock_ctx)639 static void wpa_priv_receive(int sock, void *eloop_ctx, void *sock_ctx)
640 {
641 	struct wpa_priv_interface *iface = eloop_ctx;
642 	char buf[2000], *pos;
643 	void *cmd_buf;
644 	size_t cmd_len;
645 	int res, cmd;
646 	struct sockaddr_un from;
647 	socklen_t fromlen = sizeof(from);
648 
649 	res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &from,
650 		       &fromlen);
651 	if (res < 0) {
652 		wpa_printf(MSG_ERROR, "recvfrom: %s", strerror(errno));
653 		return;
654 	}
655 
656 	if (res < (int) sizeof(int)) {
657 		wpa_printf(MSG_DEBUG, "Too short command (len=%d)", res);
658 		return;
659 	}
660 
661 	os_memcpy(&cmd, buf, sizeof(int));
662 	wpa_printf(MSG_DEBUG, "Command %d for interface %s",
663 		   cmd, iface->ifname);
664 	cmd_buf = &buf[sizeof(int)];
665 	cmd_len = res - sizeof(int);
666 
667 	switch (cmd) {
668 	case PRIVSEP_CMD_REGISTER:
669 		wpa_priv_cmd_register(iface, &from, fromlen);
670 		break;
671 	case PRIVSEP_CMD_UNREGISTER:
672 		wpa_priv_cmd_unregister(iface, &from);
673 		break;
674 	case PRIVSEP_CMD_SCAN:
675 		wpa_priv_cmd_scan(iface, cmd_buf, cmd_len);
676 		break;
677 	case PRIVSEP_CMD_GET_SCAN_RESULTS:
678 		wpa_priv_cmd_get_scan_results(iface, &from, fromlen);
679 		break;
680 	case PRIVSEP_CMD_ASSOCIATE:
681 		wpa_priv_cmd_associate(iface, cmd_buf, cmd_len);
682 		break;
683 	case PRIVSEP_CMD_GET_BSSID:
684 		wpa_priv_cmd_get_bssid(iface, &from, fromlen);
685 		break;
686 	case PRIVSEP_CMD_GET_SSID:
687 		wpa_priv_cmd_get_ssid(iface, &from, fromlen);
688 		break;
689 	case PRIVSEP_CMD_SET_KEY:
690 		wpa_priv_cmd_set_key(iface, cmd_buf, cmd_len);
691 		break;
692 	case PRIVSEP_CMD_GET_CAPA:
693 		wpa_priv_cmd_get_capa(iface, &from, fromlen);
694 		break;
695 	case PRIVSEP_CMD_L2_REGISTER:
696 		wpa_priv_cmd_l2_register(iface, &from, fromlen,
697 					 cmd_buf, cmd_len);
698 		break;
699 	case PRIVSEP_CMD_L2_UNREGISTER:
700 		wpa_priv_cmd_l2_unregister(iface, &from, fromlen);
701 		break;
702 	case PRIVSEP_CMD_L2_NOTIFY_AUTH_START:
703 		wpa_priv_cmd_l2_notify_auth_start(iface, &from);
704 		break;
705 	case PRIVSEP_CMD_L2_SEND:
706 		wpa_priv_cmd_l2_send(iface, &from, fromlen, cmd_buf, cmd_len);
707 		break;
708 	case PRIVSEP_CMD_SET_COUNTRY:
709 		pos = cmd_buf;
710 		if (pos + cmd_len >= buf + sizeof(buf))
711 			break;
712 		pos[cmd_len] = '\0';
713 		wpa_priv_cmd_set_country(iface, pos);
714 		break;
715 	case PRIVSEP_CMD_AUTHENTICATE:
716 		wpa_priv_cmd_authenticate(iface, cmd_buf, cmd_len);
717 		break;
718 	}
719 }
720 
721 
wpa_priv_interface_deinit(struct wpa_priv_interface * iface)722 static void wpa_priv_interface_deinit(struct wpa_priv_interface *iface)
723 {
724 	int i;
725 
726 	if (iface->drv_priv) {
727 		if (iface->driver->deinit)
728 			iface->driver->deinit(iface->drv_priv);
729 		if (iface->drv_global_priv)
730 			iface->driver->global_deinit(iface->drv_global_priv);
731 	}
732 
733 	if (iface->fd >= 0) {
734 		eloop_unregister_read_sock(iface->fd);
735 		close(iface->fd);
736 		unlink(iface->sock_name);
737 	}
738 
739 	for (i = 0; i < WPA_PRIV_MAX_L2; i++) {
740 		if (iface->l2[i])
741 			l2_packet_deinit(iface->l2[i]);
742 	}
743 
744 	os_free(iface->ifname);
745 	os_free(iface->driver_name);
746 	os_free(iface->sock_name);
747 	os_free(iface);
748 }
749 
750 
751 static struct wpa_priv_interface *
wpa_priv_interface_init(void * ctx,const char * dir,const char * params)752 wpa_priv_interface_init(void *ctx, const char *dir, const char *params)
753 {
754 	struct wpa_priv_interface *iface;
755 	char *pos;
756 	size_t len;
757 	struct sockaddr_un addr;
758 	int i;
759 
760 	pos = os_strchr(params, ':');
761 	if (pos == NULL)
762 		return NULL;
763 
764 	iface = os_zalloc(sizeof(*iface));
765 	if (iface == NULL)
766 		return NULL;
767 	iface->fd = -1;
768 	iface->ctx = ctx;
769 
770 	len = pos - params;
771 	iface->driver_name = dup_binstr(params, len);
772 	if (iface->driver_name == NULL) {
773 		wpa_priv_interface_deinit(iface);
774 		return NULL;
775 	}
776 
777 	for (i = 0; wpa_drivers[i]; i++) {
778 		if (os_strcmp(iface->driver_name,
779 			      wpa_drivers[i]->name) == 0) {
780 			iface->driver = wpa_drivers[i];
781 			break;
782 		}
783 	}
784 	if (iface->driver == NULL) {
785 		wpa_printf(MSG_ERROR, "Unsupported driver '%s'",
786 			   iface->driver_name);
787 		wpa_priv_interface_deinit(iface);
788 		return NULL;
789 	}
790 
791 	pos++;
792 	iface->ifname = os_strdup(pos);
793 	if (iface->ifname == NULL) {
794 		wpa_priv_interface_deinit(iface);
795 		return NULL;
796 	}
797 
798 	len = os_strlen(dir) + 1 + os_strlen(iface->ifname);
799 	iface->sock_name = os_malloc(len + 1);
800 	if (iface->sock_name == NULL) {
801 		wpa_priv_interface_deinit(iface);
802 		return NULL;
803 	}
804 
805 	os_snprintf(iface->sock_name, len + 1, "%s/%s", dir, iface->ifname);
806 	if (os_strlen(iface->sock_name) >= sizeof(addr.sun_path)) {
807 		wpa_priv_interface_deinit(iface);
808 		return NULL;
809 	}
810 
811 	iface->fd = socket(PF_UNIX, SOCK_DGRAM, 0);
812 	if (iface->fd < 0) {
813 		wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
814 		wpa_priv_interface_deinit(iface);
815 		return NULL;
816 	}
817 
818 	os_memset(&addr, 0, sizeof(addr));
819 	addr.sun_family = AF_UNIX;
820 	os_strlcpy(addr.sun_path, iface->sock_name, sizeof(addr.sun_path));
821 
822 	if (bind(iface->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
823 		wpa_printf(MSG_DEBUG, "bind(PF_UNIX) failed: %s",
824 			   strerror(errno));
825 		if (connect(iface->fd, (struct sockaddr *) &addr,
826 			    sizeof(addr)) < 0) {
827 			wpa_printf(MSG_DEBUG, "Socket exists, but does not "
828 				   "allow connections - assuming it was "
829 				   "leftover from forced program termination");
830 			if (unlink(iface->sock_name) < 0) {
831 				wpa_printf(MSG_ERROR,
832 					   "Could not unlink existing ctrl_iface socket '%s': %s",
833 					   iface->sock_name, strerror(errno));
834 				goto fail;
835 			}
836 			if (bind(iface->fd, (struct sockaddr *) &addr,
837 				 sizeof(addr)) < 0) {
838 				wpa_printf(MSG_ERROR,
839 					   "wpa-priv-iface-init: bind(PF_UNIX): %s",
840 					   strerror(errno));
841 				goto fail;
842 			}
843 			wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
844 				   "socket '%s'", iface->sock_name);
845 		} else {
846 			wpa_printf(MSG_INFO, "Socket exists and seems to be "
847 				   "in use - cannot override it");
848 			wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
849 				   "not used anymore", iface->sock_name);
850 			goto fail;
851 		}
852 	}
853 
854 	if (chmod(iface->sock_name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
855 		wpa_printf(MSG_ERROR, "chmod: %s", strerror(errno));
856 		goto fail;
857 	}
858 
859 	eloop_register_read_sock(iface->fd, wpa_priv_receive, iface, NULL);
860 
861 	return iface;
862 
863 fail:
864 	wpa_priv_interface_deinit(iface);
865 	return NULL;
866 }
867 
868 
wpa_priv_send_event(struct wpa_priv_interface * iface,int event,const void * data,size_t data_len)869 static int wpa_priv_send_event(struct wpa_priv_interface *iface, int event,
870 			       const void *data, size_t data_len)
871 {
872 	struct msghdr msg;
873 	struct iovec io[2];
874 
875 	io[0].iov_base = &event;
876 	io[0].iov_len = sizeof(event);
877 	io[1].iov_base = (u8 *) data;
878 	io[1].iov_len = data_len;
879 
880 	os_memset(&msg, 0, sizeof(msg));
881 	msg.msg_iov = io;
882 	msg.msg_iovlen = data ? 2 : 1;
883 	msg.msg_name = &iface->drv_addr;
884 	msg.msg_namelen = iface->drv_addr_len;
885 
886 	if (sendmsg(iface->fd, &msg, 0) < 0) {
887 		wpa_printf(MSG_ERROR, "sendmsg(wpas_socket): %s",
888 			   strerror(errno));
889 		return -1;
890 	}
891 
892 	return 0;
893 }
894 
895 
wpa_priv_send_auth(struct wpa_priv_interface * iface,union wpa_event_data * data)896 static void wpa_priv_send_auth(struct wpa_priv_interface *iface,
897 			       union wpa_event_data *data)
898 {
899 	size_t buflen = sizeof(struct privsep_event_auth) + data->auth.ies_len;
900 	struct privsep_event_auth *auth;
901 	u8 *buf, *pos;
902 
903 	buf = os_zalloc(buflen);
904 	if (buf == NULL)
905 		return;
906 
907 	auth = (struct privsep_event_auth *) buf;
908 	pos = (u8 *) (auth + 1);
909 
910 	os_memcpy(auth->peer, data->auth.peer, ETH_ALEN);
911 	os_memcpy(auth->bssid, data->auth.bssid, ETH_ALEN);
912 	auth->auth_type = data->auth.auth_type;
913 	auth->auth_transaction = data->auth.auth_transaction;
914 	auth->status_code = data->auth.status_code;
915 	if (data->auth.ies) {
916 		os_memcpy(pos, data->auth.ies, data->auth.ies_len);
917 		auth->ies_len = data->auth.ies_len;
918 	}
919 
920 	wpa_priv_send_event(iface, PRIVSEP_EVENT_AUTH, buf, buflen);
921 
922 	os_free(buf);
923 }
924 
925 
wpa_priv_send_assoc(struct wpa_priv_interface * iface,int event,union wpa_event_data * data)926 static void wpa_priv_send_assoc(struct wpa_priv_interface *iface, int event,
927 				union wpa_event_data *data)
928 {
929 	size_t buflen = 3 * sizeof(int);
930 	u8 *buf, *pos;
931 	int len;
932 
933 	if (data) {
934 		buflen += data->assoc_info.req_ies_len +
935 			data->assoc_info.resp_ies_len +
936 			data->assoc_info.beacon_ies_len;
937 	}
938 
939 	buf = os_malloc(buflen);
940 	if (buf == NULL)
941 		return;
942 
943 	pos = buf;
944 
945 	if (data && data->assoc_info.req_ies) {
946 		len = data->assoc_info.req_ies_len;
947 		os_memcpy(pos, &len, sizeof(int));
948 		pos += sizeof(int);
949 		os_memcpy(pos, data->assoc_info.req_ies, len);
950 		pos += len;
951 	} else {
952 		len = 0;
953 		os_memcpy(pos, &len, sizeof(int));
954 		pos += sizeof(int);
955 	}
956 
957 	if (data && data->assoc_info.resp_ies) {
958 		len = data->assoc_info.resp_ies_len;
959 		os_memcpy(pos, &len, sizeof(int));
960 		pos += sizeof(int);
961 		os_memcpy(pos, data->assoc_info.resp_ies, len);
962 		pos += len;
963 	} else {
964 		len = 0;
965 		os_memcpy(pos, &len, sizeof(int));
966 		pos += sizeof(int);
967 	}
968 
969 	if (data && data->assoc_info.beacon_ies) {
970 		len = data->assoc_info.beacon_ies_len;
971 		os_memcpy(pos, &len, sizeof(int));
972 		pos += sizeof(int);
973 		os_memcpy(pos, data->assoc_info.beacon_ies, len);
974 		pos += len;
975 	} else {
976 		len = 0;
977 		os_memcpy(pos, &len, sizeof(int));
978 		pos += sizeof(int);
979 	}
980 
981 	wpa_priv_send_event(iface, event, buf, buflen);
982 
983 	os_free(buf);
984 }
985 
986 
wpa_priv_send_interface_status(struct wpa_priv_interface * iface,union wpa_event_data * data)987 static void wpa_priv_send_interface_status(struct wpa_priv_interface *iface,
988 					   union wpa_event_data *data)
989 {
990 	int ievent;
991 	size_t len, maxlen;
992 	u8 *buf;
993 	char *ifname;
994 
995 	if (data == NULL)
996 		return;
997 
998 	ievent = data->interface_status.ievent;
999 	maxlen = sizeof(data->interface_status.ifname);
1000 	ifname = data->interface_status.ifname;
1001 	for (len = 0; len < maxlen && ifname[len]; len++)
1002 		;
1003 
1004 	buf = os_malloc(sizeof(int) + len);
1005 	if (buf == NULL)
1006 		return;
1007 
1008 	os_memcpy(buf, &ievent, sizeof(int));
1009 	os_memcpy(buf + sizeof(int), ifname, len);
1010 
1011 	wpa_priv_send_event(iface, PRIVSEP_EVENT_INTERFACE_STATUS,
1012 			    buf, sizeof(int) + len);
1013 
1014 	os_free(buf);
1015 
1016 }
1017 
1018 
wpa_priv_send_ft_response(struct wpa_priv_interface * iface,union wpa_event_data * data)1019 static void wpa_priv_send_ft_response(struct wpa_priv_interface *iface,
1020 				      union wpa_event_data *data)
1021 {
1022 	size_t len;
1023 	u8 *buf, *pos;
1024 
1025 	if (data == NULL || data->ft_ies.ies == NULL)
1026 		return;
1027 
1028 	len = sizeof(int) + ETH_ALEN + data->ft_ies.ies_len;
1029 	buf = os_malloc(len);
1030 	if (buf == NULL)
1031 		return;
1032 
1033 	pos = buf;
1034 	os_memcpy(pos, &data->ft_ies.ft_action, sizeof(int));
1035 	pos += sizeof(int);
1036 	os_memcpy(pos, data->ft_ies.target_ap, ETH_ALEN);
1037 	pos += ETH_ALEN;
1038 	os_memcpy(pos, data->ft_ies.ies, data->ft_ies.ies_len);
1039 
1040 	wpa_priv_send_event(iface, PRIVSEP_EVENT_FT_RESPONSE, buf, len);
1041 
1042 	os_free(buf);
1043 
1044 }
1045 
1046 
wpa_supplicant_event(void * ctx,enum wpa_event_type event,union wpa_event_data * data)1047 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1048 			  union wpa_event_data *data)
1049 {
1050 	struct wpa_priv_interface *iface = ctx;
1051 
1052 	wpa_printf(MSG_DEBUG, "%s - event=%d", __func__, event);
1053 
1054 	if (!iface->wpas_registered) {
1055 		wpa_printf(MSG_DEBUG, "Driver event received, but "
1056 			   "wpa_supplicant not registered");
1057 		return;
1058 	}
1059 
1060 	switch (event) {
1061 	case EVENT_ASSOC:
1062 		wpa_priv_send_assoc(iface, PRIVSEP_EVENT_ASSOC, data);
1063 		break;
1064 	case EVENT_DISASSOC:
1065 		wpa_priv_send_event(iface, PRIVSEP_EVENT_DISASSOC, NULL, 0);
1066 		break;
1067 	case EVENT_ASSOCINFO:
1068 		if (data == NULL)
1069 			return;
1070 		wpa_priv_send_assoc(iface, PRIVSEP_EVENT_ASSOCINFO, data);
1071 		break;
1072 	case EVENT_MICHAEL_MIC_FAILURE:
1073 		if (data == NULL)
1074 			return;
1075 		wpa_priv_send_event(iface, PRIVSEP_EVENT_MICHAEL_MIC_FAILURE,
1076 				    &data->michael_mic_failure.unicast,
1077 				    sizeof(int));
1078 		break;
1079 	case EVENT_SCAN_STARTED:
1080 		wpa_priv_send_event(iface, PRIVSEP_EVENT_SCAN_STARTED, NULL,
1081 				    0);
1082 		break;
1083 	case EVENT_SCAN_RESULTS:
1084 		wpa_priv_send_event(iface, PRIVSEP_EVENT_SCAN_RESULTS, NULL,
1085 				    0);
1086 		break;
1087 	case EVENT_INTERFACE_STATUS:
1088 		wpa_priv_send_interface_status(iface, data);
1089 		break;
1090 	case EVENT_PMKID_CANDIDATE:
1091 		if (data == NULL)
1092 			return;
1093 		wpa_priv_send_event(iface, PRIVSEP_EVENT_PMKID_CANDIDATE,
1094 				    &data->pmkid_candidate,
1095 				    sizeof(struct pmkid_candidate));
1096 		break;
1097 	case EVENT_FT_RESPONSE:
1098 		wpa_priv_send_ft_response(iface, data);
1099 		break;
1100 	case EVENT_AUTH:
1101 		wpa_priv_send_auth(iface, data);
1102 		break;
1103 	default:
1104 		wpa_printf(MSG_DEBUG, "Unsupported driver event %d (%s) - TODO",
1105 			   event, event_to_string(event));
1106 		break;
1107 	}
1108 }
1109 
1110 
wpa_supplicant_event_global(void * ctx,enum wpa_event_type event,union wpa_event_data * data)1111 void wpa_supplicant_event_global(void *ctx, enum wpa_event_type event,
1112 				 union wpa_event_data *data)
1113 {
1114 	struct wpa_priv_global *global = ctx;
1115 	struct wpa_priv_interface *iface;
1116 
1117 	if (event != EVENT_INTERFACE_STATUS)
1118 		return;
1119 
1120 	for (iface = global->interfaces; iface; iface = iface->next) {
1121 		if (os_strcmp(iface->ifname, data->interface_status.ifname) ==
1122 		    0)
1123 			break;
1124 	}
1125 	if (iface && iface->driver->get_ifindex) {
1126 		unsigned int ifindex;
1127 
1128 		ifindex = iface->driver->get_ifindex(iface->drv_priv);
1129 		if (ifindex != data->interface_status.ifindex) {
1130 			wpa_printf(MSG_DEBUG,
1131 				   "%s: interface status ifindex %d mismatch (%d)",
1132 				   iface->ifname, ifindex,
1133 				   data->interface_status.ifindex);
1134 			return;
1135 		}
1136 	}
1137 	if (iface)
1138 		wpa_supplicant_event(iface, event, data);
1139 }
1140 
1141 
wpa_supplicant_rx_eapol(void * ctx,const u8 * src_addr,const u8 * buf,size_t len,enum frame_encryption encrypted)1142 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1143 			     const u8 *buf, size_t len,
1144 			     enum frame_encryption encrypted)
1145 {
1146 	struct wpa_priv_interface *iface = ctx;
1147 	struct msghdr msg;
1148 	struct iovec io[3];
1149 	int event = PRIVSEP_EVENT_RX_EAPOL;
1150 
1151 	wpa_printf(MSG_DEBUG, "RX EAPOL from driver");
1152 	io[0].iov_base = &event;
1153 	io[0].iov_len = sizeof(event);
1154 	io[1].iov_base = (u8 *) src_addr;
1155 	io[1].iov_len = ETH_ALEN;
1156 	io[2].iov_base = (u8 *) buf;
1157 	io[2].iov_len = len;
1158 
1159 	os_memset(&msg, 0, sizeof(msg));
1160 	msg.msg_iov = io;
1161 	msg.msg_iovlen = 3;
1162 	msg.msg_name = &iface->drv_addr;
1163 	msg.msg_namelen = iface->drv_addr_len;
1164 
1165 	if (sendmsg(iface->fd, &msg, 0) < 0)
1166 		wpa_printf(MSG_ERROR, "sendmsg(wpas_socket): %s",
1167 			   strerror(errno));
1168 }
1169 
1170 
wpa_priv_terminate(int sig,void * signal_ctx)1171 static void wpa_priv_terminate(int sig, void *signal_ctx)
1172 {
1173 	wpa_printf(MSG_DEBUG, "wpa_priv termination requested");
1174 	eloop_terminate();
1175 }
1176 
1177 
wpa_priv_fd_workaround(void)1178 static void wpa_priv_fd_workaround(void)
1179 {
1180 #ifdef __linux__
1181 	int s, i;
1182 	/* When started from pcmcia-cs scripts, wpa_supplicant might start with
1183 	 * fd 0, 1, and 2 closed. This will cause some issues because many
1184 	 * places in wpa_supplicant are still printing out to stdout. As a
1185 	 * workaround, make sure that fd's 0, 1, and 2 are not used for other
1186 	 * sockets. */
1187 	for (i = 0; i < 3; i++) {
1188 		s = open("/dev/null", O_RDWR);
1189 		if (s > 2) {
1190 			close(s);
1191 			break;
1192 		}
1193 	}
1194 #endif /* __linux__ */
1195 }
1196 
1197 
usage(void)1198 static void usage(void)
1199 {
1200 	printf("wpa_priv v%s\n"
1201 	       "Copyright (c) 2007-2017, Jouni Malinen <j@w1.fi> and "
1202 	       "contributors\n"
1203 	       "\n"
1204 	       "usage:\n"
1205 	       "  wpa_priv [-Bdd] [-c<ctrl dir>] [-P<pid file>] "
1206 	       "<driver:ifname> \\\n"
1207 	       "           [driver:ifname ...]\n",
1208 	       VERSION_STR);
1209 }
1210 
1211 
main(int argc,char * argv[])1212 int main(int argc, char *argv[])
1213 {
1214 	int c, i;
1215 	int ret = -1;
1216 	char *pid_file = NULL;
1217 	int daemonize = 0;
1218 	char *ctrl_dir = "/var/run/wpa_priv";
1219 	struct wpa_priv_global global;
1220 	struct wpa_priv_interface *iface;
1221 
1222 	if (os_program_init())
1223 		return -1;
1224 
1225 	wpa_priv_fd_workaround();
1226 
1227 	os_memset(&global, 0, sizeof(global));
1228 	global.interfaces = NULL;
1229 
1230 	for (;;) {
1231 		c = getopt(argc, argv, "Bc:dP:");
1232 		if (c < 0)
1233 			break;
1234 		switch (c) {
1235 		case 'B':
1236 			daemonize++;
1237 			break;
1238 		case 'c':
1239 			ctrl_dir = optarg;
1240 			break;
1241 		case 'd':
1242 			wpa_debug_level--;
1243 			break;
1244 		case 'P':
1245 			pid_file = os_rel2abs_path(optarg);
1246 			break;
1247 		default:
1248 			usage();
1249 			goto out2;
1250 		}
1251 	}
1252 
1253 	if (optind >= argc) {
1254 		usage();
1255 		goto out2;
1256 	}
1257 
1258 	wpa_printf(MSG_DEBUG, "wpa_priv control directory: '%s'", ctrl_dir);
1259 
1260 	if (eloop_init()) {
1261 		wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1262 		goto out2;
1263 	}
1264 
1265 	for (i = optind; i < argc; i++) {
1266 		wpa_printf(MSG_DEBUG, "Adding driver:interface %s", argv[i]);
1267 		iface = wpa_priv_interface_init(&global, ctrl_dir, argv[i]);
1268 		if (iface == NULL)
1269 			goto out;
1270 		iface->next = global.interfaces;
1271 		global.interfaces = iface;
1272 	}
1273 
1274 	if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
1275 		goto out;
1276 
1277 	eloop_register_signal_terminate(wpa_priv_terminate, NULL);
1278 	eloop_run();
1279 
1280 	ret = 0;
1281 
1282 out:
1283 	iface = global.interfaces;
1284 	while (iface) {
1285 		struct wpa_priv_interface *prev = iface;
1286 		iface = iface->next;
1287 		wpa_priv_interface_deinit(prev);
1288 	}
1289 
1290 	eloop_destroy();
1291 
1292 out2:
1293 	if (daemonize)
1294 		os_daemonize_terminate(pid_file);
1295 	os_free(pid_file);
1296 	os_program_deinit();
1297 
1298 	return ret;
1299 }
1300