• 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 #ifdef CONFIG_MLD_PATCH_EXT
418 	p.link_id = -1;
419 #endif
420 	res = iface->driver->set_key(iface->drv_priv, &p);
421 	wpa_printf(MSG_DEBUG, "drv->set_key: res=%d", res);
422 }
423 
424 
wpa_priv_cmd_get_capa(struct wpa_priv_interface * iface,struct sockaddr_un * from,socklen_t fromlen)425 static void wpa_priv_cmd_get_capa(struct wpa_priv_interface *iface,
426 				  struct sockaddr_un *from, socklen_t fromlen)
427 {
428 	struct wpa_driver_capa capa;
429 
430 	if (iface->drv_priv == NULL)
431 		goto fail;
432 
433 	if (iface->driver->get_capa == NULL ||
434 	    iface->driver->get_capa(iface->drv_priv, &capa) < 0)
435 		goto fail;
436 
437 	/* For now, no support for passing extended_capa pointers */
438 	capa.extended_capa = NULL;
439 	capa.extended_capa_mask = NULL;
440 	capa.extended_capa_len = 0;
441 	sendto(iface->fd, &capa, sizeof(capa), 0, (struct sockaddr *) from,
442 	       fromlen);
443 	return;
444 
445 fail:
446 	sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, fromlen);
447 }
448 
449 
wpa_priv_l2_rx(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)450 static void wpa_priv_l2_rx(void *ctx, const u8 *src_addr, const u8 *buf,
451 			   size_t len)
452 {
453 	struct wpa_priv_l2 *l2_ctx = ctx;
454 	struct wpa_priv_interface *iface = l2_ctx->parent;
455 	struct msghdr msg;
456 	struct iovec io[2];
457 
458 	io[0].iov_base = (u8 *) src_addr;
459 	io[0].iov_len = ETH_ALEN;
460 	io[1].iov_base = (u8 *) buf;
461 	io[1].iov_len = len;
462 
463 	os_memset(&msg, 0, sizeof(msg));
464 	msg.msg_iov = io;
465 	msg.msg_iovlen = 2;
466 	msg.msg_name = &iface->l2_addr[l2_ctx->idx];
467 	msg.msg_namelen = iface->l2_addr_len[l2_ctx->idx];
468 
469 	if (sendmsg(iface->fd, &msg, 0) < 0) {
470 		wpa_printf(MSG_ERROR, "sendmsg(l2 rx): %s", strerror(errno));
471 	}
472 }
473 
474 
wpa_priv_allowed_l2_proto(u16 proto)475 static int wpa_priv_allowed_l2_proto(u16 proto)
476 {
477 	return proto == ETH_P_EAPOL || proto == ETH_P_RSN_PREAUTH ||
478 		proto == ETH_P_80211_ENCAP;
479 }
480 
481 
wpa_priv_cmd_l2_register(struct wpa_priv_interface * iface,struct sockaddr_un * from,socklen_t fromlen,void * buf,size_t len)482 static void wpa_priv_cmd_l2_register(struct wpa_priv_interface *iface,
483 				     struct sockaddr_un *from,
484 				     socklen_t fromlen,
485 				     void *buf, size_t len)
486 {
487 	int *reg_cmd = buf;
488 	u8 own_addr[ETH_ALEN];
489 	int res;
490 	u16 proto;
491 	int idx;
492 
493 	if (len != 2 * sizeof(int)) {
494 		wpa_printf(MSG_DEBUG, "Invalid l2_register length %lu",
495 			   (unsigned long) len);
496 		return;
497 	}
498 
499 	proto = reg_cmd[0];
500 	if (!wpa_priv_allowed_l2_proto(proto)) {
501 		wpa_printf(MSG_DEBUG, "Refused l2_packet connection for "
502 			   "ethertype 0x%x", proto);
503 		return;
504 	}
505 
506 	for (idx = 0; idx < WPA_PRIV_MAX_L2; idx++) {
507 		if (!iface->l2[idx])
508 			break;
509 	}
510 	if (idx == WPA_PRIV_MAX_L2) {
511 		wpa_printf(MSG_DEBUG, "No free l2_packet connection found");
512 		return;
513 	}
514 
515 	os_memcpy(&iface->l2_addr[idx], from, fromlen);
516 	iface->l2_addr_len[idx] = fromlen;
517 
518 	iface->l2_ctx[idx].idx = idx;
519 	iface->l2_ctx[idx].parent = iface;
520 	iface->l2[idx] = l2_packet_init(iface->ifname, NULL, proto,
521 					wpa_priv_l2_rx, &iface->l2_ctx[idx],
522 					reg_cmd[1]);
523 	if (!iface->l2[idx]) {
524 		wpa_printf(MSG_DEBUG, "Failed to initialize l2_packet "
525 			   "instance for protocol %d", proto);
526 		return;
527 	}
528 
529 	if (l2_packet_get_own_addr(iface->l2[idx], own_addr) < 0) {
530 		wpa_printf(MSG_DEBUG, "Failed to get own address from "
531 			   "l2_packet");
532 		l2_packet_deinit(iface->l2[idx]);
533 		iface->l2[idx] = NULL;
534 		return;
535 	}
536 
537 	res = sendto(iface->fd, own_addr, ETH_ALEN, 0,
538 		     (struct sockaddr *) from, fromlen);
539 	wpa_printf(MSG_DEBUG, "L2 registration[idx=%d]: res=%d", idx, res);
540 }
541 
542 
wpa_priv_cmd_l2_unregister(struct wpa_priv_interface * iface,struct sockaddr_un * from,socklen_t fromlen)543 static void wpa_priv_cmd_l2_unregister(struct wpa_priv_interface *iface,
544 				       struct sockaddr_un *from,
545 				       socklen_t fromlen)
546 {
547 	int idx;
548 
549 	for (idx = 0; idx < WPA_PRIV_MAX_L2; idx++) {
550 		if (iface->l2_addr_len[idx] == fromlen &&
551 		    os_memcmp(&iface->l2_addr[idx], from, fromlen) == 0)
552 			break;
553 	}
554 	if (idx == WPA_PRIV_MAX_L2) {
555 		wpa_printf(MSG_DEBUG,
556 			   "No registered l2_packet socket found for unregister request");
557 		return;
558 	}
559 
560 	if (iface->l2[idx]) {
561 		l2_packet_deinit(iface->l2[idx]);
562 		iface->l2[idx] = NULL;
563 	}
564 }
565 
566 
wpa_priv_cmd_l2_notify_auth_start(struct wpa_priv_interface * iface,struct sockaddr_un * from)567 static void wpa_priv_cmd_l2_notify_auth_start(struct wpa_priv_interface *iface,
568 					      struct sockaddr_un *from)
569 {
570 	int idx;
571 
572 	for (idx = 0; idx < WPA_PRIV_MAX_L2; idx++) {
573 		if (iface->l2[idx])
574 			l2_packet_notify_auth_start(iface->l2[idx]);
575 	}
576 }
577 
578 
wpa_priv_cmd_l2_send(struct wpa_priv_interface * iface,struct sockaddr_un * from,socklen_t fromlen,void * buf,size_t len)579 static void wpa_priv_cmd_l2_send(struct wpa_priv_interface *iface,
580 				 struct sockaddr_un *from, socklen_t fromlen,
581 				 void *buf, size_t len)
582 {
583 	u8 *dst_addr;
584 	u16 proto;
585 	int res;
586 	int idx;
587 
588 	for (idx = 0; idx < WPA_PRIV_MAX_L2; idx++) {
589 		if (iface->l2_addr_len[idx] == fromlen &&
590 		    os_memcmp(&iface->l2_addr[idx], from, fromlen) == 0)
591 			break;
592 	}
593 	if (idx == WPA_PRIV_MAX_L2) {
594 		wpa_printf(MSG_DEBUG,
595 			   "No registered l2_packet socket found for send request");
596 		return;
597 	}
598 
599 	if (iface->l2[idx] == NULL)
600 		return;
601 
602 	if (len < ETH_ALEN + 2) {
603 		wpa_printf(MSG_DEBUG, "Too short L2 send packet (len=%lu)",
604 			   (unsigned long) len);
605 		return;
606 	}
607 
608 	dst_addr = buf;
609 	os_memcpy(&proto, (char *) buf + ETH_ALEN, 2);
610 
611 	if (!wpa_priv_allowed_l2_proto(proto)) {
612 		wpa_printf(MSG_DEBUG, "Refused l2_packet send for ethertype "
613 			   "0x%x", proto);
614 		return;
615 	}
616 
617 	res = l2_packet_send(iface->l2[idx], dst_addr, proto,
618 			     (unsigned char *) buf + ETH_ALEN + 2,
619 			     len - ETH_ALEN - 2);
620 	wpa_printf(MSG_DEBUG, "L2 send[idx=%d]: res=%d", idx, res);
621 }
622 
623 
wpa_priv_cmd_set_country(struct wpa_priv_interface * iface,char * buf)624 static void wpa_priv_cmd_set_country(struct wpa_priv_interface *iface,
625 				     char *buf)
626 {
627 	if (iface->drv_priv == NULL || iface->driver->set_country == NULL ||
628 	    *buf == '\0')
629 		return;
630 
631 	iface->driver->set_country(iface->drv_priv, buf);
632 }
633 
634 
wpa_priv_receive(int sock,void * eloop_ctx,void * sock_ctx)635 static void wpa_priv_receive(int sock, void *eloop_ctx, void *sock_ctx)
636 {
637 	struct wpa_priv_interface *iface = eloop_ctx;
638 	char buf[2000], *pos;
639 	void *cmd_buf;
640 	size_t cmd_len;
641 	int res, cmd;
642 	struct sockaddr_un from;
643 	socklen_t fromlen = sizeof(from);
644 
645 	res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &from,
646 		       &fromlen);
647 	if (res < 0) {
648 		wpa_printf(MSG_ERROR, "recvfrom: %s", strerror(errno));
649 		return;
650 	}
651 
652 	if (res < (int) sizeof(int)) {
653 		wpa_printf(MSG_DEBUG, "Too short command (len=%d)", res);
654 		return;
655 	}
656 
657 	os_memcpy(&cmd, buf, sizeof(int));
658 	wpa_printf(MSG_DEBUG, "Command %d for interface %s",
659 		   cmd, iface->ifname);
660 	cmd_buf = &buf[sizeof(int)];
661 	cmd_len = res - sizeof(int);
662 
663 	switch (cmd) {
664 	case PRIVSEP_CMD_REGISTER:
665 		wpa_priv_cmd_register(iface, &from, fromlen);
666 		break;
667 	case PRIVSEP_CMD_UNREGISTER:
668 		wpa_priv_cmd_unregister(iface, &from);
669 		break;
670 	case PRIVSEP_CMD_SCAN:
671 		wpa_priv_cmd_scan(iface, cmd_buf, cmd_len);
672 		break;
673 	case PRIVSEP_CMD_GET_SCAN_RESULTS:
674 		wpa_priv_cmd_get_scan_results(iface, &from, fromlen);
675 		break;
676 	case PRIVSEP_CMD_ASSOCIATE:
677 		wpa_priv_cmd_associate(iface, cmd_buf, cmd_len);
678 		break;
679 	case PRIVSEP_CMD_GET_BSSID:
680 		wpa_priv_cmd_get_bssid(iface, &from, fromlen);
681 		break;
682 	case PRIVSEP_CMD_GET_SSID:
683 		wpa_priv_cmd_get_ssid(iface, &from, fromlen);
684 		break;
685 	case PRIVSEP_CMD_SET_KEY:
686 		wpa_priv_cmd_set_key(iface, cmd_buf, cmd_len);
687 		break;
688 	case PRIVSEP_CMD_GET_CAPA:
689 		wpa_priv_cmd_get_capa(iface, &from, fromlen);
690 		break;
691 	case PRIVSEP_CMD_L2_REGISTER:
692 		wpa_priv_cmd_l2_register(iface, &from, fromlen,
693 					 cmd_buf, cmd_len);
694 		break;
695 	case PRIVSEP_CMD_L2_UNREGISTER:
696 		wpa_priv_cmd_l2_unregister(iface, &from, fromlen);
697 		break;
698 	case PRIVSEP_CMD_L2_NOTIFY_AUTH_START:
699 		wpa_priv_cmd_l2_notify_auth_start(iface, &from);
700 		break;
701 	case PRIVSEP_CMD_L2_SEND:
702 		wpa_priv_cmd_l2_send(iface, &from, fromlen, cmd_buf, cmd_len);
703 		break;
704 	case PRIVSEP_CMD_SET_COUNTRY:
705 		pos = cmd_buf;
706 		if (pos + cmd_len >= buf + sizeof(buf))
707 			break;
708 		pos[cmd_len] = '\0';
709 		wpa_priv_cmd_set_country(iface, pos);
710 		break;
711 	case PRIVSEP_CMD_AUTHENTICATE:
712 		wpa_priv_cmd_authenticate(iface, cmd_buf, cmd_len);
713 		break;
714 	}
715 }
716 
717 
wpa_priv_interface_deinit(struct wpa_priv_interface * iface)718 static void wpa_priv_interface_deinit(struct wpa_priv_interface *iface)
719 {
720 	int i;
721 
722 	if (iface->drv_priv) {
723 		if (iface->driver->deinit)
724 			iface->driver->deinit(iface->drv_priv);
725 		if (iface->drv_global_priv)
726 			iface->driver->global_deinit(iface->drv_global_priv);
727 	}
728 
729 	if (iface->fd >= 0) {
730 		eloop_unregister_read_sock(iface->fd);
731 		close(iface->fd);
732 		unlink(iface->sock_name);
733 	}
734 
735 	for (i = 0; i < WPA_PRIV_MAX_L2; i++) {
736 		if (iface->l2[i])
737 			l2_packet_deinit(iface->l2[i]);
738 	}
739 
740 	os_free(iface->ifname);
741 	os_free(iface->driver_name);
742 	os_free(iface->sock_name);
743 	os_free(iface);
744 }
745 
746 
747 static struct wpa_priv_interface *
wpa_priv_interface_init(void * ctx,const char * dir,const char * params)748 wpa_priv_interface_init(void *ctx, const char *dir, const char *params)
749 {
750 	struct wpa_priv_interface *iface;
751 	char *pos;
752 	size_t len;
753 	struct sockaddr_un addr;
754 	int i;
755 
756 	pos = os_strchr(params, ':');
757 	if (pos == NULL)
758 		return NULL;
759 
760 	iface = os_zalloc(sizeof(*iface));
761 	if (iface == NULL)
762 		return NULL;
763 	iface->fd = -1;
764 	iface->ctx = ctx;
765 
766 	len = pos - params;
767 	iface->driver_name = dup_binstr(params, len);
768 	if (iface->driver_name == NULL) {
769 		wpa_priv_interface_deinit(iface);
770 		return NULL;
771 	}
772 
773 	for (i = 0; wpa_drivers[i]; i++) {
774 		if (os_strcmp(iface->driver_name,
775 			      wpa_drivers[i]->name) == 0) {
776 			iface->driver = wpa_drivers[i];
777 			break;
778 		}
779 	}
780 	if (iface->driver == NULL) {
781 		wpa_printf(MSG_ERROR, "Unsupported driver '%s'",
782 			   iface->driver_name);
783 		wpa_priv_interface_deinit(iface);
784 		return NULL;
785 	}
786 
787 	pos++;
788 	iface->ifname = os_strdup(pos);
789 	if (iface->ifname == NULL) {
790 		wpa_priv_interface_deinit(iface);
791 		return NULL;
792 	}
793 
794 	len = os_strlen(dir) + 1 + os_strlen(iface->ifname);
795 	iface->sock_name = os_malloc(len + 1);
796 	if (iface->sock_name == NULL) {
797 		wpa_priv_interface_deinit(iface);
798 		return NULL;
799 	}
800 
801 	os_snprintf(iface->sock_name, len + 1, "%s/%s", dir, iface->ifname);
802 	if (os_strlen(iface->sock_name) >= sizeof(addr.sun_path)) {
803 		wpa_priv_interface_deinit(iface);
804 		return NULL;
805 	}
806 
807 	iface->fd = socket(PF_UNIX, SOCK_DGRAM, 0);
808 	if (iface->fd < 0) {
809 		wpa_printf(MSG_ERROR, "socket(PF_UNIX): %s", strerror(errno));
810 		wpa_priv_interface_deinit(iface);
811 		return NULL;
812 	}
813 
814 	os_memset(&addr, 0, sizeof(addr));
815 	addr.sun_family = AF_UNIX;
816 	os_strlcpy(addr.sun_path, iface->sock_name, sizeof(addr.sun_path));
817 
818 	if (bind(iface->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
819 		wpa_printf(MSG_DEBUG, "bind(PF_UNIX) failed: %s",
820 			   strerror(errno));
821 		if (connect(iface->fd, (struct sockaddr *) &addr,
822 			    sizeof(addr)) < 0) {
823 			wpa_printf(MSG_DEBUG, "Socket exists, but does not "
824 				   "allow connections - assuming it was "
825 				   "leftover from forced program termination");
826 			if (unlink(iface->sock_name) < 0) {
827 				wpa_printf(MSG_ERROR,
828 					   "Could not unlink existing ctrl_iface socket '%s': %s",
829 					   iface->sock_name, strerror(errno));
830 				goto fail;
831 			}
832 			if (bind(iface->fd, (struct sockaddr *) &addr,
833 				 sizeof(addr)) < 0) {
834 				wpa_printf(MSG_ERROR,
835 					   "wpa-priv-iface-init: bind(PF_UNIX): %s",
836 					   strerror(errno));
837 				goto fail;
838 			}
839 			wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
840 				   "socket '%s'", iface->sock_name);
841 		} else {
842 			wpa_printf(MSG_INFO, "Socket exists and seems to be "
843 				   "in use - cannot override it");
844 			wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
845 				   "not used anymore", iface->sock_name);
846 			goto fail;
847 		}
848 	}
849 
850 	if (chmod(iface->sock_name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
851 		wpa_printf(MSG_ERROR, "chmod: %s", strerror(errno));
852 		goto fail;
853 	}
854 
855 	eloop_register_read_sock(iface->fd, wpa_priv_receive, iface, NULL);
856 
857 	return iface;
858 
859 fail:
860 	wpa_priv_interface_deinit(iface);
861 	return NULL;
862 }
863 
864 
wpa_priv_send_event(struct wpa_priv_interface * iface,int event,const void * data,size_t data_len)865 static int wpa_priv_send_event(struct wpa_priv_interface *iface, int event,
866 			       const void *data, size_t data_len)
867 {
868 	struct msghdr msg;
869 	struct iovec io[2];
870 
871 	io[0].iov_base = &event;
872 	io[0].iov_len = sizeof(event);
873 	io[1].iov_base = (u8 *) data;
874 	io[1].iov_len = data_len;
875 
876 	os_memset(&msg, 0, sizeof(msg));
877 	msg.msg_iov = io;
878 	msg.msg_iovlen = data ? 2 : 1;
879 	msg.msg_name = &iface->drv_addr;
880 	msg.msg_namelen = iface->drv_addr_len;
881 
882 	if (sendmsg(iface->fd, &msg, 0) < 0) {
883 		wpa_printf(MSG_ERROR, "sendmsg(wpas_socket): %s",
884 			   strerror(errno));
885 		return -1;
886 	}
887 
888 	return 0;
889 }
890 
891 
wpa_priv_send_auth(struct wpa_priv_interface * iface,union wpa_event_data * data)892 static void wpa_priv_send_auth(struct wpa_priv_interface *iface,
893 			       union wpa_event_data *data)
894 {
895 	size_t buflen = sizeof(struct privsep_event_auth) + data->auth.ies_len;
896 	struct privsep_event_auth *auth;
897 	u8 *buf, *pos;
898 
899 	buf = os_zalloc(buflen);
900 	if (buf == NULL)
901 		return;
902 
903 	auth = (struct privsep_event_auth *) buf;
904 	pos = (u8 *) (auth + 1);
905 
906 	os_memcpy(auth->peer, data->auth.peer, ETH_ALEN);
907 	os_memcpy(auth->bssid, data->auth.bssid, ETH_ALEN);
908 	auth->auth_type = data->auth.auth_type;
909 	auth->auth_transaction = data->auth.auth_transaction;
910 	auth->status_code = data->auth.status_code;
911 	if (data->auth.ies) {
912 		os_memcpy(pos, data->auth.ies, data->auth.ies_len);
913 		auth->ies_len = data->auth.ies_len;
914 	}
915 
916 	wpa_priv_send_event(iface, PRIVSEP_EVENT_AUTH, buf, buflen);
917 
918 	os_free(buf);
919 }
920 
921 
wpa_priv_send_assoc(struct wpa_priv_interface * iface,int event,union wpa_event_data * data)922 static void wpa_priv_send_assoc(struct wpa_priv_interface *iface, int event,
923 				union wpa_event_data *data)
924 {
925 	size_t buflen = 3 * sizeof(int);
926 	u8 *buf, *pos;
927 	int len;
928 
929 	if (data) {
930 		buflen += data->assoc_info.req_ies_len +
931 			data->assoc_info.resp_ies_len +
932 			data->assoc_info.beacon_ies_len;
933 	}
934 
935 	buf = os_malloc(buflen);
936 	if (buf == NULL)
937 		return;
938 
939 	pos = buf;
940 
941 	if (data && data->assoc_info.req_ies) {
942 		len = data->assoc_info.req_ies_len;
943 		os_memcpy(pos, &len, sizeof(int));
944 		pos += sizeof(int);
945 		os_memcpy(pos, data->assoc_info.req_ies, len);
946 		pos += len;
947 	} else {
948 		len = 0;
949 		os_memcpy(pos, &len, sizeof(int));
950 		pos += sizeof(int);
951 	}
952 
953 	if (data && data->assoc_info.resp_ies) {
954 		len = data->assoc_info.resp_ies_len;
955 		os_memcpy(pos, &len, sizeof(int));
956 		pos += sizeof(int);
957 		os_memcpy(pos, data->assoc_info.resp_ies, len);
958 		pos += len;
959 	} else {
960 		len = 0;
961 		os_memcpy(pos, &len, sizeof(int));
962 		pos += sizeof(int);
963 	}
964 
965 	if (data && data->assoc_info.beacon_ies) {
966 		len = data->assoc_info.beacon_ies_len;
967 		os_memcpy(pos, &len, sizeof(int));
968 		pos += sizeof(int);
969 		os_memcpy(pos, data->assoc_info.beacon_ies, len);
970 		pos += len;
971 	} else {
972 		len = 0;
973 		os_memcpy(pos, &len, sizeof(int));
974 		pos += sizeof(int);
975 	}
976 
977 	wpa_priv_send_event(iface, event, buf, buflen);
978 
979 	os_free(buf);
980 }
981 
982 
wpa_priv_send_interface_status(struct wpa_priv_interface * iface,union wpa_event_data * data)983 static void wpa_priv_send_interface_status(struct wpa_priv_interface *iface,
984 					   union wpa_event_data *data)
985 {
986 	int ievent;
987 	size_t len, maxlen;
988 	u8 *buf;
989 	char *ifname;
990 
991 	if (data == NULL)
992 		return;
993 
994 	ievent = data->interface_status.ievent;
995 	maxlen = sizeof(data->interface_status.ifname);
996 	ifname = data->interface_status.ifname;
997 	for (len = 0; len < maxlen && ifname[len]; len++)
998 		;
999 
1000 	buf = os_malloc(sizeof(int) + len);
1001 	if (buf == NULL)
1002 		return;
1003 
1004 	os_memcpy(buf, &ievent, sizeof(int));
1005 	os_memcpy(buf + sizeof(int), ifname, len);
1006 
1007 	wpa_priv_send_event(iface, PRIVSEP_EVENT_INTERFACE_STATUS,
1008 			    buf, sizeof(int) + len);
1009 
1010 	os_free(buf);
1011 
1012 }
1013 
1014 
wpa_priv_send_ft_response(struct wpa_priv_interface * iface,union wpa_event_data * data)1015 static void wpa_priv_send_ft_response(struct wpa_priv_interface *iface,
1016 				      union wpa_event_data *data)
1017 {
1018 	size_t len;
1019 	u8 *buf, *pos;
1020 
1021 	if (data == NULL || data->ft_ies.ies == NULL)
1022 		return;
1023 
1024 	len = sizeof(int) + ETH_ALEN + data->ft_ies.ies_len;
1025 	buf = os_malloc(len);
1026 	if (buf == NULL)
1027 		return;
1028 
1029 	pos = buf;
1030 	os_memcpy(pos, &data->ft_ies.ft_action, sizeof(int));
1031 	pos += sizeof(int);
1032 	os_memcpy(pos, data->ft_ies.target_ap, ETH_ALEN);
1033 	pos += ETH_ALEN;
1034 	os_memcpy(pos, data->ft_ies.ies, data->ft_ies.ies_len);
1035 
1036 	wpa_priv_send_event(iface, PRIVSEP_EVENT_FT_RESPONSE, buf, len);
1037 
1038 	os_free(buf);
1039 
1040 }
1041 
1042 
wpa_supplicant_event(void * ctx,enum wpa_event_type event,union wpa_event_data * data)1043 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1044 			  union wpa_event_data *data)
1045 {
1046 	struct wpa_priv_interface *iface = ctx;
1047 
1048 	wpa_printf(MSG_DEBUG, "%s - event=%d", __func__, event);
1049 
1050 	if (!iface->wpas_registered) {
1051 		wpa_printf(MSG_DEBUG, "Driver event received, but "
1052 			   "wpa_supplicant not registered");
1053 		return;
1054 	}
1055 
1056 	switch (event) {
1057 	case EVENT_ASSOC:
1058 		wpa_priv_send_assoc(iface, PRIVSEP_EVENT_ASSOC, data);
1059 		break;
1060 	case EVENT_DISASSOC:
1061 		wpa_priv_send_event(iface, PRIVSEP_EVENT_DISASSOC, NULL, 0);
1062 		break;
1063 	case EVENT_ASSOCINFO:
1064 		if (data == NULL)
1065 			return;
1066 		wpa_priv_send_assoc(iface, PRIVSEP_EVENT_ASSOCINFO, data);
1067 		break;
1068 	case EVENT_MICHAEL_MIC_FAILURE:
1069 		if (data == NULL)
1070 			return;
1071 		wpa_priv_send_event(iface, PRIVSEP_EVENT_MICHAEL_MIC_FAILURE,
1072 				    &data->michael_mic_failure.unicast,
1073 				    sizeof(int));
1074 		break;
1075 	case EVENT_SCAN_STARTED:
1076 		wpa_priv_send_event(iface, PRIVSEP_EVENT_SCAN_STARTED, NULL,
1077 				    0);
1078 		break;
1079 	case EVENT_SCAN_RESULTS:
1080 		wpa_priv_send_event(iface, PRIVSEP_EVENT_SCAN_RESULTS, NULL,
1081 				    0);
1082 		break;
1083 	case EVENT_INTERFACE_STATUS:
1084 		wpa_priv_send_interface_status(iface, data);
1085 		break;
1086 	case EVENT_PMKID_CANDIDATE:
1087 		if (data == NULL)
1088 			return;
1089 		wpa_priv_send_event(iface, PRIVSEP_EVENT_PMKID_CANDIDATE,
1090 				    &data->pmkid_candidate,
1091 				    sizeof(struct pmkid_candidate));
1092 		break;
1093 	case EVENT_FT_RESPONSE:
1094 		wpa_priv_send_ft_response(iface, data);
1095 		break;
1096 	case EVENT_AUTH:
1097 		wpa_priv_send_auth(iface, data);
1098 		break;
1099 	default:
1100 		wpa_printf(MSG_DEBUG, "Unsupported driver event %d (%s) - TODO",
1101 			   event, event_to_string(event));
1102 		break;
1103 	}
1104 }
1105 
1106 
wpa_supplicant_event_global(void * ctx,enum wpa_event_type event,union wpa_event_data * data)1107 void wpa_supplicant_event_global(void *ctx, enum wpa_event_type event,
1108 				 union wpa_event_data *data)
1109 {
1110 	struct wpa_priv_global *global = ctx;
1111 	struct wpa_priv_interface *iface;
1112 
1113 	if (event != EVENT_INTERFACE_STATUS)
1114 		return;
1115 
1116 	for (iface = global->interfaces; iface; iface = iface->next) {
1117 		if (os_strcmp(iface->ifname, data->interface_status.ifname) ==
1118 		    0)
1119 			break;
1120 	}
1121 	if (iface && iface->driver->get_ifindex) {
1122 		unsigned int ifindex;
1123 
1124 		ifindex = iface->driver->get_ifindex(iface->drv_priv);
1125 		if (ifindex != data->interface_status.ifindex) {
1126 			wpa_printf(MSG_DEBUG,
1127 				   "%s: interface status ifindex %d mismatch (%d)",
1128 				   iface->ifname, ifindex,
1129 				   data->interface_status.ifindex);
1130 			return;
1131 		}
1132 	}
1133 	if (iface)
1134 		wpa_supplicant_event(iface, event, data);
1135 }
1136 
1137 
wpa_supplicant_rx_eapol(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)1138 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1139 			     const u8 *buf, size_t len)
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