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