• 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 
25 struct wpa_priv_interface {
26 	struct wpa_priv_interface *next;
27 	char *driver_name;
28 	char *ifname;
29 	char *sock_name;
30 	int fd;
31 
32 	struct wpa_driver_ops *driver;
33 	void *drv_priv;
34 	struct sockaddr_un drv_addr;
35 	int wpas_registered;
36 
37 	/* TODO: add support for multiple l2 connections */
38 	struct l2_packet_data *l2;
39 	struct sockaddr_un l2_addr;
40 };
41 
42 
wpa_priv_cmd_register(struct wpa_priv_interface * iface,struct sockaddr_un * from)43 static void wpa_priv_cmd_register(struct wpa_priv_interface *iface,
44 				  struct sockaddr_un *from)
45 {
46 	if (iface->drv_priv) {
47 		wpa_printf(MSG_DEBUG, "Cleaning up forgotten driver instance");
48 		if (iface->driver->deinit)
49 			iface->driver->deinit(iface->drv_priv);
50 		iface->drv_priv = NULL;
51 		iface->wpas_registered = 0;
52 	}
53 
54 	if (iface->l2) {
55 		wpa_printf(MSG_DEBUG, "Cleaning up forgotten l2_packet "
56 			   "instance");
57 		l2_packet_deinit(iface->l2);
58 		iface->l2 = NULL;
59 	}
60 
61 	if (iface->driver->init == NULL)
62 		return;
63 
64 	iface->drv_priv = iface->driver->init(iface, iface->ifname);
65 	if (iface->drv_priv == NULL) {
66 		wpa_printf(MSG_DEBUG, "Failed to initialize driver wrapper");
67 		return;
68 	}
69 
70 	wpa_printf(MSG_DEBUG, "Driver wrapper '%s' initialized for interface "
71 		   "'%s'", iface->driver_name, iface->ifname);
72 
73 	os_memcpy(&iface->drv_addr, from, sizeof(iface->drv_addr));
74 	iface->wpas_registered = 1;
75 
76 	if (iface->driver->set_param &&
77 	    iface->driver->set_param(iface->drv_priv, NULL) < 0) {
78 		wpa_printf(MSG_ERROR, "Driver interface rejected param");
79 	}
80 }
81 
82 
wpa_priv_cmd_unregister(struct wpa_priv_interface * iface,struct sockaddr_un * from)83 static void wpa_priv_cmd_unregister(struct wpa_priv_interface *iface,
84 				    struct sockaddr_un *from)
85 {
86 	if (iface->drv_priv) {
87 		if (iface->driver->deinit)
88 			iface->driver->deinit(iface->drv_priv);
89 		iface->drv_priv = NULL;
90 		iface->wpas_registered = 0;
91 	}
92 }
93 
94 
wpa_priv_cmd_scan(struct wpa_priv_interface * iface,char * buf,size_t len)95 static void wpa_priv_cmd_scan(struct wpa_priv_interface *iface,
96 			      char *buf, size_t len)
97 {
98 	struct wpa_driver_scan_params params;
99 
100 	if (iface->drv_priv == NULL)
101 		return;
102 
103 	os_memset(&params, 0, sizeof(params));
104 	if (len) {
105 		params.ssids[0].ssid = (u8 *) buf;
106 		params.ssids[0].ssid_len = len;
107 		params.num_ssids = 1;
108 	}
109 
110 	if (iface->driver->scan2)
111 		iface->driver->scan2(iface->drv_priv, &params);
112 }
113 
114 
wpa_priv_get_scan_results2(struct wpa_priv_interface * iface,struct sockaddr_un * from)115 static void wpa_priv_get_scan_results2(struct wpa_priv_interface *iface,
116 				       struct sockaddr_un *from)
117 {
118 	struct wpa_scan_results *res;
119 	u8 *buf = NULL, *pos, *end;
120 	int val;
121 	size_t i;
122 
123 	res = iface->driver->get_scan_results2(iface->drv_priv);
124 	if (res == NULL)
125 		goto fail;
126 
127 	buf = os_malloc(60000);
128 	if (buf == NULL)
129 		goto fail;
130 	pos = buf;
131 	end = buf + 60000;
132 	val = res->num;
133 	os_memcpy(pos, &val, sizeof(int));
134 	pos += sizeof(int);
135 
136 	for (i = 0; i < res->num; i++) {
137 		struct wpa_scan_res *r = res->res[i];
138 		val = sizeof(*r) + r->ie_len;
139 		if (end - pos < (int) sizeof(int) + val)
140 			break;
141 		os_memcpy(pos, &val, sizeof(int));
142 		pos += sizeof(int);
143 		os_memcpy(pos, r, val);
144 		pos += val;
145 	}
146 
147 	sendto(iface->fd, buf, pos - buf, 0, (struct sockaddr *) from,
148 	       sizeof(*from));
149 
150 	os_free(buf);
151 	wpa_scan_results_free(res);
152 	return;
153 
154 fail:
155 	os_free(buf);
156 	wpa_scan_results_free(res);
157 	sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
158 }
159 
160 
wpa_priv_cmd_get_scan_results(struct wpa_priv_interface * iface,struct sockaddr_un * from)161 static void wpa_priv_cmd_get_scan_results(struct wpa_priv_interface *iface,
162 					  struct sockaddr_un *from)
163 {
164 	if (iface->drv_priv == NULL)
165 		return;
166 
167 	if (iface->driver->get_scan_results2)
168 		wpa_priv_get_scan_results2(iface, from);
169 	else
170 		sendto(iface->fd, "", 0, 0, (struct sockaddr *) from,
171 		       sizeof(*from));
172 }
173 
174 
wpa_priv_cmd_associate(struct wpa_priv_interface * iface,void * buf,size_t len)175 static void wpa_priv_cmd_associate(struct wpa_priv_interface *iface,
176 				   void *buf, size_t len)
177 {
178 	struct wpa_driver_associate_params params;
179 	struct privsep_cmd_associate *assoc;
180 	u8 *bssid;
181 	int res;
182 
183 	if (iface->drv_priv == NULL || iface->driver->associate == NULL)
184 		return;
185 
186 	if (len < sizeof(*assoc)) {
187 		wpa_printf(MSG_DEBUG, "Invalid association request");
188 		return;
189 	}
190 
191 	assoc = buf;
192 	if (sizeof(*assoc) + assoc->wpa_ie_len > len) {
193 		wpa_printf(MSG_DEBUG, "Association request overflow");
194 		return;
195 	}
196 
197 	os_memset(&params, 0, sizeof(params));
198 	bssid = assoc->bssid;
199 	if (bssid[0] | bssid[1] | bssid[2] | bssid[3] | bssid[4] | bssid[5])
200 		params.bssid = bssid;
201 	params.ssid = assoc->ssid;
202 	if (assoc->ssid_len > 32)
203 		return;
204 	params.ssid_len = assoc->ssid_len;
205 	params.freq = assoc->freq;
206 	if (assoc->wpa_ie_len) {
207 		params.wpa_ie = (u8 *) (assoc + 1);
208 		params.wpa_ie_len = assoc->wpa_ie_len;
209 	}
210 	params.pairwise_suite = assoc->pairwise_suite;
211 	params.group_suite = assoc->group_suite;
212 	params.key_mgmt_suite = assoc->key_mgmt_suite;
213 	params.auth_alg = assoc->auth_alg;
214 	params.mode = assoc->mode;
215 
216 	res = iface->driver->associate(iface->drv_priv, &params);
217 	wpa_printf(MSG_DEBUG, "drv->associate: res=%d", res);
218 }
219 
220 
wpa_priv_cmd_get_bssid(struct wpa_priv_interface * iface,struct sockaddr_un * from)221 static void wpa_priv_cmd_get_bssid(struct wpa_priv_interface *iface,
222 				   struct sockaddr_un *from)
223 {
224 	u8 bssid[ETH_ALEN];
225 
226 	if (iface->drv_priv == NULL)
227 		goto fail;
228 
229 	if (iface->driver->get_bssid == NULL ||
230 	    iface->driver->get_bssid(iface->drv_priv, bssid) < 0)
231 		goto fail;
232 
233 	sendto(iface->fd, bssid, ETH_ALEN, 0, (struct sockaddr *) from,
234 	       sizeof(*from));
235 	return;
236 
237 fail:
238 	sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
239 }
240 
241 
wpa_priv_cmd_get_ssid(struct wpa_priv_interface * iface,struct sockaddr_un * from)242 static void wpa_priv_cmd_get_ssid(struct wpa_priv_interface *iface,
243 				  struct sockaddr_un *from)
244 {
245 	u8 ssid[sizeof(int) + 32];
246 	int res;
247 
248 	if (iface->drv_priv == NULL)
249 		goto fail;
250 
251 	if (iface->driver->get_ssid == NULL)
252 		goto fail;
253 
254 	res = iface->driver->get_ssid(iface->drv_priv, &ssid[sizeof(int)]);
255 	if (res < 0 || res > 32)
256 		goto fail;
257 	os_memcpy(ssid, &res, sizeof(int));
258 
259 	sendto(iface->fd, ssid, sizeof(ssid), 0, (struct sockaddr *) from,
260 	       sizeof(*from));
261 	return;
262 
263 fail:
264 	sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
265 }
266 
267 
wpa_priv_cmd_set_key(struct wpa_priv_interface * iface,void * buf,size_t len)268 static void wpa_priv_cmd_set_key(struct wpa_priv_interface *iface,
269 				 void *buf, size_t len)
270 {
271 	struct privsep_cmd_set_key *params;
272 	int res;
273 
274 	if (iface->drv_priv == NULL || iface->driver->set_key == NULL)
275 		return;
276 
277 	if (len != sizeof(*params)) {
278 		wpa_printf(MSG_DEBUG, "Invalid set_key request");
279 		return;
280 	}
281 
282 	params = buf;
283 
284 	res = iface->driver->set_key(iface->ifname, iface->drv_priv,
285 				     params->alg,
286 				     params->addr, params->key_idx,
287 				     params->set_tx,
288 				     params->seq_len ? params->seq : NULL,
289 				     params->seq_len,
290 				     params->key_len ? params->key : NULL,
291 				     params->key_len);
292 	wpa_printf(MSG_DEBUG, "drv->set_key: res=%d", res);
293 }
294 
295 
wpa_priv_cmd_get_capa(struct wpa_priv_interface * iface,struct sockaddr_un * from)296 static void wpa_priv_cmd_get_capa(struct wpa_priv_interface *iface,
297 				  struct sockaddr_un *from)
298 {
299 	struct wpa_driver_capa capa;
300 
301 	if (iface->drv_priv == NULL)
302 		goto fail;
303 
304 	if (iface->driver->get_capa == NULL ||
305 	    iface->driver->get_capa(iface->drv_priv, &capa) < 0)
306 		goto fail;
307 
308 	sendto(iface->fd, &capa, sizeof(capa), 0, (struct sockaddr *) from,
309 	       sizeof(*from));
310 	return;
311 
312 fail:
313 	sendto(iface->fd, "", 0, 0, (struct sockaddr *) from, sizeof(*from));
314 }
315 
316 
wpa_priv_l2_rx(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)317 static void wpa_priv_l2_rx(void *ctx, const u8 *src_addr, const u8 *buf,
318 			   size_t len)
319 {
320 	struct wpa_priv_interface *iface = ctx;
321 	struct msghdr msg;
322 	struct iovec io[2];
323 
324 	io[0].iov_base = (u8 *) src_addr;
325 	io[0].iov_len = ETH_ALEN;
326 	io[1].iov_base = (u8 *) buf;
327 	io[1].iov_len = len;
328 
329 	os_memset(&msg, 0, sizeof(msg));
330 	msg.msg_iov = io;
331 	msg.msg_iovlen = 2;
332 	msg.msg_name = &iface->l2_addr;
333 	msg.msg_namelen = sizeof(iface->l2_addr);
334 
335 	if (sendmsg(iface->fd, &msg, 0) < 0) {
336 		perror("sendmsg(l2 rx)");
337 	}
338 }
339 
340 
wpa_priv_cmd_l2_register(struct wpa_priv_interface * iface,struct sockaddr_un * from,void * buf,size_t len)341 static void wpa_priv_cmd_l2_register(struct wpa_priv_interface *iface,
342 				     struct sockaddr_un *from,
343 				     void *buf, size_t len)
344 {
345 	int *reg_cmd = buf;
346 	u8 own_addr[ETH_ALEN];
347 	int res;
348 	u16 proto;
349 
350 	if (len != 2 * sizeof(int)) {
351 		wpa_printf(MSG_DEBUG, "Invalid l2_register length %lu",
352 			   (unsigned long) len);
353 		return;
354 	}
355 
356 	proto = reg_cmd[0];
357 	if (proto != ETH_P_EAPOL && proto != ETH_P_RSN_PREAUTH) {
358 		wpa_printf(MSG_DEBUG, "Refused l2_packet connection for "
359 			   "ethertype 0x%x", proto);
360 		return;
361 	}
362 
363 	if (iface->l2) {
364 		wpa_printf(MSG_DEBUG, "Cleaning up forgotten l2_packet "
365 			   "instance");
366 		l2_packet_deinit(iface->l2);
367 		iface->l2 = NULL;
368 	}
369 
370 	os_memcpy(&iface->l2_addr, from, sizeof(iface->l2_addr));
371 
372 	iface->l2 = l2_packet_init(iface->ifname, NULL, proto,
373 				   wpa_priv_l2_rx, iface, reg_cmd[1]);
374 	if (iface->l2 == NULL) {
375 		wpa_printf(MSG_DEBUG, "Failed to initialize l2_packet "
376 			   "instance for protocol %d", proto);
377 		return;
378 	}
379 
380 	if (l2_packet_get_own_addr(iface->l2, own_addr) < 0) {
381 		wpa_printf(MSG_DEBUG, "Failed to get own address from "
382 			   "l2_packet");
383 		l2_packet_deinit(iface->l2);
384 		iface->l2 = NULL;
385 		return;
386 	}
387 
388 	res = sendto(iface->fd, own_addr, ETH_ALEN, 0,
389 		     (struct sockaddr *) from, sizeof(*from));
390 	wpa_printf(MSG_DEBUG, "L2 registration: res=%d", res);
391 }
392 
393 
wpa_priv_cmd_l2_unregister(struct wpa_priv_interface * iface,struct sockaddr_un * from)394 static void wpa_priv_cmd_l2_unregister(struct wpa_priv_interface *iface,
395 				       struct sockaddr_un *from)
396 {
397 	if (iface->l2) {
398 		l2_packet_deinit(iface->l2);
399 		iface->l2 = NULL;
400 	}
401 }
402 
403 
wpa_priv_cmd_l2_notify_auth_start(struct wpa_priv_interface * iface,struct sockaddr_un * from)404 static void wpa_priv_cmd_l2_notify_auth_start(struct wpa_priv_interface *iface,
405 					      struct sockaddr_un *from)
406 {
407 	if (iface->l2)
408 		l2_packet_notify_auth_start(iface->l2);
409 }
410 
411 
wpa_priv_cmd_l2_send(struct wpa_priv_interface * iface,struct sockaddr_un * from,void * buf,size_t len)412 static void wpa_priv_cmd_l2_send(struct wpa_priv_interface *iface,
413 				 struct sockaddr_un *from,
414 				 void *buf, size_t len)
415 {
416 	u8 *dst_addr;
417 	u16 proto;
418 	int res;
419 
420 	if (iface->l2 == NULL)
421 		return;
422 
423 	if (len < ETH_ALEN + 2) {
424 		wpa_printf(MSG_DEBUG, "Too short L2 send packet (len=%lu)",
425 			   (unsigned long) len);
426 		return;
427 	}
428 
429 	dst_addr = buf;
430 	os_memcpy(&proto, buf + ETH_ALEN, 2);
431 
432 	if (proto != ETH_P_EAPOL && proto != ETH_P_RSN_PREAUTH) {
433 		wpa_printf(MSG_DEBUG, "Refused l2_packet send for ethertype "
434 			   "0x%x", proto);
435 		return;
436 	}
437 
438 	res = l2_packet_send(iface->l2, dst_addr, proto, buf + ETH_ALEN + 2,
439 			     len - ETH_ALEN - 2);
440 	wpa_printf(MSG_DEBUG, "L2 send: res=%d", res);
441 }
442 
443 
wpa_priv_cmd_set_country(struct wpa_priv_interface * iface,char * buf)444 static void wpa_priv_cmd_set_country(struct wpa_priv_interface *iface,
445 				     char *buf)
446 {
447 	if (iface->drv_priv == NULL || iface->driver->set_country == NULL ||
448 	    *buf == '\0')
449 		return;
450 
451 	iface->driver->set_country(iface->drv_priv, buf);
452 }
453 
454 
wpa_priv_receive(int sock,void * eloop_ctx,void * sock_ctx)455 static void wpa_priv_receive(int sock, void *eloop_ctx, void *sock_ctx)
456 {
457 	struct wpa_priv_interface *iface = eloop_ctx;
458 	char buf[2000], *pos;
459 	void *cmd_buf;
460 	size_t cmd_len;
461 	int res, cmd;
462 	struct sockaddr_un from;
463 	socklen_t fromlen = sizeof(from);
464 
465 	res = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *) &from,
466 		       &fromlen);
467 	if (res < 0) {
468 		perror("recvfrom");
469 		return;
470 	}
471 
472 	if (res < (int) sizeof(int)) {
473 		wpa_printf(MSG_DEBUG, "Too short command (len=%d)", res);
474 		return;
475 	}
476 
477 	os_memcpy(&cmd, buf, sizeof(int));
478 	wpa_printf(MSG_DEBUG, "Command %d for interface %s",
479 		   cmd, iface->ifname);
480 	cmd_buf = &buf[sizeof(int)];
481 	cmd_len = res - sizeof(int);
482 
483 	switch (cmd) {
484 	case PRIVSEP_CMD_REGISTER:
485 		wpa_priv_cmd_register(iface, &from);
486 		break;
487 	case PRIVSEP_CMD_UNREGISTER:
488 		wpa_priv_cmd_unregister(iface, &from);
489 		break;
490 	case PRIVSEP_CMD_SCAN:
491 		wpa_priv_cmd_scan(iface, cmd_buf, cmd_len);
492 		break;
493 	case PRIVSEP_CMD_GET_SCAN_RESULTS:
494 		wpa_priv_cmd_get_scan_results(iface, &from);
495 		break;
496 	case PRIVSEP_CMD_ASSOCIATE:
497 		wpa_priv_cmd_associate(iface, cmd_buf, cmd_len);
498 		break;
499 	case PRIVSEP_CMD_GET_BSSID:
500 		wpa_priv_cmd_get_bssid(iface, &from);
501 		break;
502 	case PRIVSEP_CMD_GET_SSID:
503 		wpa_priv_cmd_get_ssid(iface, &from);
504 		break;
505 	case PRIVSEP_CMD_SET_KEY:
506 		wpa_priv_cmd_set_key(iface, cmd_buf, cmd_len);
507 		break;
508 	case PRIVSEP_CMD_GET_CAPA:
509 		wpa_priv_cmd_get_capa(iface, &from);
510 		break;
511 	case PRIVSEP_CMD_L2_REGISTER:
512 		wpa_priv_cmd_l2_register(iface, &from, cmd_buf, cmd_len);
513 		break;
514 	case PRIVSEP_CMD_L2_UNREGISTER:
515 		wpa_priv_cmd_l2_unregister(iface, &from);
516 		break;
517 	case PRIVSEP_CMD_L2_NOTIFY_AUTH_START:
518 		wpa_priv_cmd_l2_notify_auth_start(iface, &from);
519 		break;
520 	case PRIVSEP_CMD_L2_SEND:
521 		wpa_priv_cmd_l2_send(iface, &from, cmd_buf, cmd_len);
522 		break;
523 	case PRIVSEP_CMD_SET_COUNTRY:
524 		pos = cmd_buf;
525 		if (pos + cmd_len >= buf + sizeof(buf))
526 			break;
527 		pos[cmd_len] = '\0';
528 		wpa_priv_cmd_set_country(iface, pos);
529 		break;
530 	}
531 }
532 
533 
wpa_priv_interface_deinit(struct wpa_priv_interface * iface)534 static void wpa_priv_interface_deinit(struct wpa_priv_interface *iface)
535 {
536 	if (iface->drv_priv && iface->driver->deinit)
537 		iface->driver->deinit(iface->drv_priv);
538 
539 	if (iface->fd >= 0) {
540 		eloop_unregister_read_sock(iface->fd);
541 		close(iface->fd);
542 		unlink(iface->sock_name);
543 	}
544 
545 	if (iface->l2)
546 		l2_packet_deinit(iface->l2);
547 
548 	os_free(iface->ifname);
549 	os_free(iface->driver_name);
550 	os_free(iface->sock_name);
551 	os_free(iface);
552 }
553 
554 
555 extern struct wpa_driver_ops *wpa_drivers[];
556 
557 static struct wpa_priv_interface *
wpa_priv_interface_init(const char * dir,const char * params)558 wpa_priv_interface_init(const char *dir, const char *params)
559 {
560 	struct wpa_priv_interface *iface;
561 	char *pos;
562 	size_t len;
563 	struct sockaddr_un addr;
564 	int i;
565 
566 	pos = os_strchr(params, ':');
567 	if (pos == NULL)
568 		return NULL;
569 
570 	iface = os_zalloc(sizeof(*iface));
571 	if (iface == NULL)
572 		return NULL;
573 	iface->fd = -1;
574 
575 	len = pos - params;
576 	iface->driver_name = dup_binstr(params, len);
577 	if (iface->driver_name == NULL) {
578 		wpa_priv_interface_deinit(iface);
579 		return NULL;
580 	}
581 
582 	for (i = 0; wpa_drivers[i]; i++) {
583 		if (os_strcmp(iface->driver_name,
584 			      wpa_drivers[i]->name) == 0) {
585 			iface->driver = wpa_drivers[i];
586 			break;
587 		}
588 	}
589 	if (iface->driver == NULL) {
590 		wpa_printf(MSG_ERROR, "Unsupported driver '%s'",
591 			   iface->driver_name);
592 		wpa_priv_interface_deinit(iface);
593 		return NULL;
594 	}
595 
596 	pos++;
597 	iface->ifname = os_strdup(pos);
598 	if (iface->ifname == NULL) {
599 		wpa_priv_interface_deinit(iface);
600 		return NULL;
601 	}
602 
603 	len = os_strlen(dir) + 1 + os_strlen(iface->ifname);
604 	iface->sock_name = os_malloc(len + 1);
605 	if (iface->sock_name == NULL) {
606 		wpa_priv_interface_deinit(iface);
607 		return NULL;
608 	}
609 
610 	os_snprintf(iface->sock_name, len + 1, "%s/%s", dir, iface->ifname);
611 	if (os_strlen(iface->sock_name) >= sizeof(addr.sun_path)) {
612 		wpa_priv_interface_deinit(iface);
613 		return NULL;
614 	}
615 
616 	iface->fd = socket(PF_UNIX, SOCK_DGRAM, 0);
617 	if (iface->fd < 0) {
618 		perror("socket(PF_UNIX)");
619 		wpa_priv_interface_deinit(iface);
620 		return NULL;
621 	}
622 
623 	os_memset(&addr, 0, sizeof(addr));
624 	addr.sun_family = AF_UNIX;
625 	os_strlcpy(addr.sun_path, iface->sock_name, sizeof(addr.sun_path));
626 
627 	if (bind(iface->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
628 		wpa_printf(MSG_DEBUG, "bind(PF_UNIX) failed: %s",
629 			   strerror(errno));
630 		if (connect(iface->fd, (struct sockaddr *) &addr,
631 			    sizeof(addr)) < 0) {
632 			wpa_printf(MSG_DEBUG, "Socket exists, but does not "
633 				   "allow connections - assuming it was "
634 				   "leftover from forced program termination");
635 			if (unlink(iface->sock_name) < 0) {
636 				perror("unlink[ctrl_iface]");
637 				wpa_printf(MSG_ERROR, "Could not unlink "
638 					   "existing ctrl_iface socket '%s'",
639 					   iface->sock_name);
640 				goto fail;
641 			}
642 			if (bind(iface->fd, (struct sockaddr *) &addr,
643 				 sizeof(addr)) < 0) {
644 				perror("wpa-priv-iface-init: bind(PF_UNIX)");
645 				goto fail;
646 			}
647 			wpa_printf(MSG_DEBUG, "Successfully replaced leftover "
648 				   "socket '%s'", iface->sock_name);
649 		} else {
650 			wpa_printf(MSG_INFO, "Socket exists and seems to be "
651 				   "in use - cannot override it");
652 			wpa_printf(MSG_INFO, "Delete '%s' manually if it is "
653 				   "not used anymore", iface->sock_name);
654 			goto fail;
655 		}
656 	}
657 
658 	if (chmod(iface->sock_name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
659 		perror("chmod");
660 		goto fail;
661 	}
662 
663 	eloop_register_read_sock(iface->fd, wpa_priv_receive, iface, NULL);
664 
665 	return iface;
666 
667 fail:
668 	wpa_priv_interface_deinit(iface);
669 	return NULL;
670 }
671 
672 
wpa_priv_send_event(struct wpa_priv_interface * iface,int event,const void * data,size_t data_len)673 static int wpa_priv_send_event(struct wpa_priv_interface *iface, int event,
674 			       const void *data, size_t data_len)
675 {
676 	struct msghdr msg;
677 	struct iovec io[2];
678 
679 	io[0].iov_base = &event;
680 	io[0].iov_len = sizeof(event);
681 	io[1].iov_base = (u8 *) data;
682 	io[1].iov_len = data_len;
683 
684 	os_memset(&msg, 0, sizeof(msg));
685 	msg.msg_iov = io;
686 	msg.msg_iovlen = data ? 2 : 1;
687 	msg.msg_name = &iface->drv_addr;
688 	msg.msg_namelen = sizeof(iface->drv_addr);
689 
690 	if (sendmsg(iface->fd, &msg, 0) < 0) {
691 		perror("sendmsg(wpas_socket)");
692 		return -1;
693 	}
694 
695 	return 0;
696 }
697 
698 
wpa_priv_send_assoc(struct wpa_priv_interface * iface,int event,union wpa_event_data * data)699 static void wpa_priv_send_assoc(struct wpa_priv_interface *iface, int event,
700 				union wpa_event_data *data)
701 {
702 	size_t buflen = 3 * sizeof(int);
703 	u8 *buf, *pos;
704 	int len;
705 
706 	if (data) {
707 		buflen += data->assoc_info.req_ies_len +
708 			data->assoc_info.resp_ies_len +
709 			data->assoc_info.beacon_ies_len;
710 	}
711 
712 	buf = os_malloc(buflen);
713 	if (buf == NULL)
714 		return;
715 
716 	pos = buf;
717 
718 	if (data && data->assoc_info.req_ies) {
719 		len = data->assoc_info.req_ies_len;
720 		os_memcpy(pos, &len, sizeof(int));
721 		pos += sizeof(int);
722 		os_memcpy(pos, data->assoc_info.req_ies, len);
723 		pos += len;
724 	} else {
725 		len = 0;
726 		os_memcpy(pos, &len, sizeof(int));
727 		pos += sizeof(int);
728 	}
729 
730 	if (data && data->assoc_info.resp_ies) {
731 		len = data->assoc_info.resp_ies_len;
732 		os_memcpy(pos, &len, sizeof(int));
733 		pos += sizeof(int);
734 		os_memcpy(pos, data->assoc_info.resp_ies, len);
735 		pos += len;
736 	} else {
737 		len = 0;
738 		os_memcpy(pos, &len, sizeof(int));
739 		pos += sizeof(int);
740 	}
741 
742 	if (data && data->assoc_info.beacon_ies) {
743 		len = data->assoc_info.beacon_ies_len;
744 		os_memcpy(pos, &len, sizeof(int));
745 		pos += sizeof(int);
746 		os_memcpy(pos, data->assoc_info.beacon_ies, len);
747 		pos += len;
748 	} else {
749 		len = 0;
750 		os_memcpy(pos, &len, sizeof(int));
751 		pos += sizeof(int);
752 	}
753 
754 	wpa_priv_send_event(iface, event, buf, buflen);
755 
756 	os_free(buf);
757 }
758 
759 
wpa_priv_send_interface_status(struct wpa_priv_interface * iface,union wpa_event_data * data)760 static void wpa_priv_send_interface_status(struct wpa_priv_interface *iface,
761 					   union wpa_event_data *data)
762 {
763 	int ievent;
764 	size_t len, maxlen;
765 	u8 *buf;
766 	char *ifname;
767 
768 	if (data == NULL)
769 		return;
770 
771 	ievent = data->interface_status.ievent;
772 	maxlen = sizeof(data->interface_status.ifname);
773 	ifname = data->interface_status.ifname;
774 	for (len = 0; len < maxlen && ifname[len]; len++)
775 		;
776 
777 	buf = os_malloc(sizeof(int) + len);
778 	if (buf == NULL)
779 		return;
780 
781 	os_memcpy(buf, &ievent, sizeof(int));
782 	os_memcpy(buf + sizeof(int), ifname, len);
783 
784 	wpa_priv_send_event(iface, PRIVSEP_EVENT_INTERFACE_STATUS,
785 			    buf, sizeof(int) + len);
786 
787 	os_free(buf);
788 
789 }
790 
791 
wpa_priv_send_ft_response(struct wpa_priv_interface * iface,union wpa_event_data * data)792 static void wpa_priv_send_ft_response(struct wpa_priv_interface *iface,
793 				      union wpa_event_data *data)
794 {
795 	size_t len;
796 	u8 *buf, *pos;
797 
798 	if (data == NULL || data->ft_ies.ies == NULL)
799 		return;
800 
801 	len = sizeof(int) + ETH_ALEN + data->ft_ies.ies_len;
802 	buf = os_malloc(len);
803 	if (buf == NULL)
804 		return;
805 
806 	pos = buf;
807 	os_memcpy(pos, &data->ft_ies.ft_action, sizeof(int));
808 	pos += sizeof(int);
809 	os_memcpy(pos, data->ft_ies.target_ap, ETH_ALEN);
810 	pos += ETH_ALEN;
811 	os_memcpy(pos, data->ft_ies.ies, data->ft_ies.ies_len);
812 
813 	wpa_priv_send_event(iface, PRIVSEP_EVENT_FT_RESPONSE, buf, len);
814 
815 	os_free(buf);
816 
817 }
818 
819 
wpa_supplicant_event(void * ctx,enum wpa_event_type event,union wpa_event_data * data)820 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
821 			  union wpa_event_data *data)
822 {
823 	struct wpa_priv_interface *iface = ctx;
824 
825 	wpa_printf(MSG_DEBUG, "%s - event=%d", __func__, event);
826 
827 	if (!iface->wpas_registered) {
828 		wpa_printf(MSG_DEBUG, "Driver event received, but "
829 			   "wpa_supplicant not registered");
830 		return;
831 	}
832 
833 	switch (event) {
834 	case EVENT_ASSOC:
835 		wpa_priv_send_assoc(iface, PRIVSEP_EVENT_ASSOC, data);
836 		break;
837 	case EVENT_DISASSOC:
838 		wpa_priv_send_event(iface, PRIVSEP_EVENT_DISASSOC, NULL, 0);
839 		break;
840 	case EVENT_ASSOCINFO:
841 		if (data == NULL)
842 			return;
843 		wpa_priv_send_assoc(iface, PRIVSEP_EVENT_ASSOCINFO, data);
844 		break;
845 	case EVENT_MICHAEL_MIC_FAILURE:
846 		if (data == NULL)
847 			return;
848 		wpa_priv_send_event(iface, PRIVSEP_EVENT_MICHAEL_MIC_FAILURE,
849 				    &data->michael_mic_failure.unicast,
850 				    sizeof(int));
851 		break;
852 	case EVENT_SCAN_RESULTS:
853 		wpa_priv_send_event(iface, PRIVSEP_EVENT_SCAN_RESULTS, NULL,
854 				    0);
855 		break;
856 	case EVENT_INTERFACE_STATUS:
857 		wpa_priv_send_interface_status(iface, data);
858 		break;
859 	case EVENT_PMKID_CANDIDATE:
860 		if (data == NULL)
861 			return;
862 		wpa_priv_send_event(iface, PRIVSEP_EVENT_PMKID_CANDIDATE,
863 				    &data->pmkid_candidate,
864 				    sizeof(struct pmkid_candidate));
865 		break;
866 	case EVENT_STKSTART:
867 		if (data == NULL)
868 			return;
869 		wpa_priv_send_event(iface, PRIVSEP_EVENT_STKSTART,
870 				    &data->stkstart.peer, ETH_ALEN);
871 		break;
872 	case EVENT_FT_RESPONSE:
873 		wpa_priv_send_ft_response(iface, data);
874 		break;
875 	default:
876 		wpa_printf(MSG_DEBUG, "Unsupported driver event %d - TODO",
877 			   event);
878 		break;
879 	}
880 }
881 
882 
wpa_supplicant_rx_eapol(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)883 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
884 			     const u8 *buf, size_t len)
885 {
886 	struct wpa_priv_interface *iface = ctx;
887 	struct msghdr msg;
888 	struct iovec io[3];
889 	int event = PRIVSEP_EVENT_RX_EAPOL;
890 
891 	wpa_printf(MSG_DEBUG, "RX EAPOL from driver");
892 	io[0].iov_base = &event;
893 	io[0].iov_len = sizeof(event);
894 	io[1].iov_base = (u8 *) src_addr;
895 	io[1].iov_len = ETH_ALEN;
896 	io[2].iov_base = (u8 *) buf;
897 	io[2].iov_len = len;
898 
899 	os_memset(&msg, 0, sizeof(msg));
900 	msg.msg_iov = io;
901 	msg.msg_iovlen = 3;
902 	msg.msg_name = &iface->drv_addr;
903 	msg.msg_namelen = sizeof(iface->drv_addr);
904 
905 	if (sendmsg(iface->fd, &msg, 0) < 0)
906 		perror("sendmsg(wpas_socket)");
907 }
908 
909 
wpa_priv_terminate(int sig,void * signal_ctx)910 static void wpa_priv_terminate(int sig, void *signal_ctx)
911 {
912 	wpa_printf(MSG_DEBUG, "wpa_priv termination requested");
913 	eloop_terminate();
914 }
915 
916 
wpa_priv_fd_workaround(void)917 static void wpa_priv_fd_workaround(void)
918 {
919 #ifdef __linux__
920 	int s, i;
921 	/* When started from pcmcia-cs scripts, wpa_supplicant might start with
922 	 * fd 0, 1, and 2 closed. This will cause some issues because many
923 	 * places in wpa_supplicant are still printing out to stdout. As a
924 	 * workaround, make sure that fd's 0, 1, and 2 are not used for other
925 	 * sockets. */
926 	for (i = 0; i < 3; i++) {
927 		s = open("/dev/null", O_RDWR);
928 		if (s > 2) {
929 			close(s);
930 			break;
931 		}
932 	}
933 #endif /* __linux__ */
934 }
935 
936 
usage(void)937 static void usage(void)
938 {
939 	printf("wpa_priv v" VERSION_STR "\n"
940 	       "Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi> and "
941 	       "contributors\n"
942 	       "\n"
943 	       "usage:\n"
944 	       "  wpa_priv [-Bdd] [-P<pid file>] <driver:ifname> "
945 	       "[driver:ifname ...]\n");
946 }
947 
948 
949 extern int wpa_debug_level;
950 
main(int argc,char * argv[])951 int main(int argc, char *argv[])
952 {
953 	int c, i;
954 	int ret = -1;
955 	char *pid_file = NULL;
956 	int daemonize = 0;
957 	char *ctrl_dir = "/var/run/wpa_priv";
958 	struct wpa_priv_interface *interfaces = NULL, *iface;
959 
960 	if (os_program_init())
961 		return -1;
962 
963 	wpa_priv_fd_workaround();
964 
965 	for (;;) {
966 		c = getopt(argc, argv, "Bc:dP:");
967 		if (c < 0)
968 			break;
969 		switch (c) {
970 		case 'B':
971 			daemonize++;
972 			break;
973 		case 'c':
974 			ctrl_dir = optarg;
975 			break;
976 		case 'd':
977 			wpa_debug_level--;
978 			break;
979 		case 'P':
980 			pid_file = os_rel2abs_path(optarg);
981 			break;
982 		default:
983 			usage();
984 			goto out;
985 		}
986 	}
987 
988 	if (optind >= argc) {
989 		usage();
990 		goto out;
991 	}
992 
993 	wpa_printf(MSG_DEBUG, "wpa_priv control directory: '%s'", ctrl_dir);
994 
995 	if (eloop_init()) {
996 		wpa_printf(MSG_ERROR, "Failed to initialize event loop");
997 		goto out;
998 	}
999 
1000 	for (i = optind; i < argc; i++) {
1001 		wpa_printf(MSG_DEBUG, "Adding driver:interface %s", argv[i]);
1002 		iface = wpa_priv_interface_init(ctrl_dir, argv[i]);
1003 		if (iface == NULL)
1004 			goto out;
1005 		iface->next = interfaces;
1006 		interfaces = iface;
1007 	}
1008 
1009 	if (daemonize && os_daemonize(pid_file))
1010 		goto out;
1011 
1012 	eloop_register_signal_terminate(wpa_priv_terminate, NULL);
1013 	eloop_run();
1014 
1015 	ret = 0;
1016 
1017 out:
1018 	iface = interfaces;
1019 	while (iface) {
1020 		struct wpa_priv_interface *prev = iface;
1021 		iface = iface->next;
1022 		wpa_priv_interface_deinit(prev);
1023 	}
1024 
1025 	eloop_destroy();
1026 
1027 	os_daemonize_terminate(pid_file);
1028 	os_free(pid_file);
1029 	os_program_deinit();
1030 
1031 	return ret;
1032 }
1033