]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/drivers/driver_test.c
Share driver beacon configuration handlers
[thirdparty/hostap.git] / src / drivers / driver_test.c
1 /*
2 * WPA Supplicant - testing driver interface
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15 /* Make sure we get winsock2.h for Windows build to get sockaddr_storage */
16 #include "build_config.h"
17 #ifdef CONFIG_NATIVE_WINDOWS
18 #include <winsock2.h>
19 #endif /* CONFIG_NATIVE_WINDOWS */
20
21 #include "includes.h"
22
23 #ifndef CONFIG_NATIVE_WINDOWS
24 #include <sys/un.h>
25 #include <dirent.h>
26 #include <sys/stat.h>
27 #define DRIVER_TEST_UNIX
28 #endif /* CONFIG_NATIVE_WINDOWS */
29
30 #include "common.h"
31 #include "driver.h"
32 #include "l2_packet/l2_packet.h"
33 #include "eloop.h"
34 #include "sha1.h"
35 #include "ieee802_11_defs.h"
36
37
38 #ifdef HOSTAPD
39
40 #include "../../hostapd/hostapd.h"
41 #include "../../hostapd/wpa.h"
42 #include "../../hostapd/hw_features.h"
43 #include "../../hostapd/wps_hostapd.h"
44
45
46 struct test_client_socket {
47 struct test_client_socket *next;
48 u8 addr[ETH_ALEN];
49 struct sockaddr_un un;
50 socklen_t unlen;
51 struct test_driver_bss *bss;
52 };
53
54 struct test_driver_bss {
55 struct test_driver_bss *next;
56 char ifname[IFNAMSIZ + 1];
57 u8 bssid[ETH_ALEN];
58 u8 *ie;
59 size_t ielen;
60 u8 *wps_beacon_ie;
61 size_t wps_beacon_ie_len;
62 u8 *wps_probe_resp_ie;
63 size_t wps_probe_resp_ie_len;
64 u8 ssid[32];
65 size_t ssid_len;
66 int privacy;
67 };
68
69 struct test_driver_data {
70 struct hostapd_data *hapd;
71 struct test_client_socket *cli;
72 int test_socket;
73 struct test_driver_bss *bss;
74 char *socket_dir;
75 char *own_socket_path;
76 int udp_port;
77 };
78
79 #else /* HOSTAPD */
80
81 struct wpa_driver_test_global {
82 int dummy;
83 };
84
85 struct wpa_driver_test_data {
86 struct wpa_driver_test_global *global;
87 void *ctx;
88 u8 own_addr[ETH_ALEN];
89 int test_socket;
90 #ifdef DRIVER_TEST_UNIX
91 struct sockaddr_un hostapd_addr;
92 #endif /* DRIVER_TEST_UNIX */
93 int hostapd_addr_set;
94 struct sockaddr_in hostapd_addr_udp;
95 int hostapd_addr_udp_set;
96 char *own_socket_path;
97 char *test_dir;
98 u8 bssid[ETH_ALEN];
99 u8 ssid[32];
100 size_t ssid_len;
101 #define MAX_SCAN_RESULTS 30
102 struct wpa_scan_res *scanres[MAX_SCAN_RESULTS];
103 size_t num_scanres;
104 int use_associnfo;
105 u8 assoc_wpa_ie[80];
106 size_t assoc_wpa_ie_len;
107 int use_mlme;
108 int associated;
109 u8 *probe_req_ie;
110 size_t probe_req_ie_len;
111 int ibss;
112 int privacy;
113 };
114
115 #endif /* HOSTAPD */
116
117
118 #ifdef HOSTAPD
119
120 static void test_driver_free_bss(struct test_driver_bss *bss)
121 {
122 free(bss->ie);
123 free(bss->wps_beacon_ie);
124 free(bss->wps_probe_resp_ie);
125 free(bss);
126 }
127
128
129 static void test_driver_free_priv(struct test_driver_data *drv)
130 {
131 struct test_driver_bss *bss, *prev;
132
133 if (drv == NULL)
134 return;
135
136 bss = drv->bss;
137 while (bss) {
138 prev = bss;
139 bss = bss->next;
140 test_driver_free_bss(prev);
141 }
142 free(drv->own_socket_path);
143 free(drv->socket_dir);
144 free(drv);
145 }
146
147
148 static struct test_client_socket *
149 test_driver_get_cli(struct test_driver_data *drv, struct sockaddr_un *from,
150 socklen_t fromlen)
151 {
152 struct test_client_socket *cli = drv->cli;
153
154 while (cli) {
155 if (cli->unlen == fromlen &&
156 strncmp(cli->un.sun_path, from->sun_path,
157 fromlen - sizeof(cli->un.sun_family)) == 0)
158 return cli;
159 cli = cli->next;
160 }
161
162 return NULL;
163 }
164
165
166 static int test_driver_send_eapol(void *priv, const u8 *addr, const u8 *data,
167 size_t data_len, int encrypt,
168 const u8 *own_addr)
169 {
170 struct test_driver_data *drv = priv;
171 struct test_client_socket *cli;
172 struct msghdr msg;
173 struct iovec io[3];
174 struct l2_ethhdr eth;
175
176 if (drv->test_socket < 0)
177 return -1;
178
179 cli = drv->cli;
180 while (cli) {
181 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
182 break;
183 cli = cli->next;
184 }
185
186 if (!cli) {
187 wpa_printf(MSG_DEBUG, "%s: no destination client entry",
188 __func__);
189 return -1;
190 }
191
192 memcpy(eth.h_dest, addr, ETH_ALEN);
193 memcpy(eth.h_source, own_addr, ETH_ALEN);
194 eth.h_proto = host_to_be16(ETH_P_EAPOL);
195
196 io[0].iov_base = "EAPOL ";
197 io[0].iov_len = 6;
198 io[1].iov_base = &eth;
199 io[1].iov_len = sizeof(eth);
200 io[2].iov_base = (u8 *) data;
201 io[2].iov_len = data_len;
202
203 memset(&msg, 0, sizeof(msg));
204 msg.msg_iov = io;
205 msg.msg_iovlen = 3;
206 msg.msg_name = &cli->un;
207 msg.msg_namelen = cli->unlen;
208 return sendmsg(drv->test_socket, &msg, 0);
209 }
210
211
212 static int test_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
213 u16 proto, const u8 *data, size_t data_len)
214 {
215 struct test_driver_data *drv = priv;
216 struct msghdr msg;
217 struct iovec io[3];
218 struct l2_ethhdr eth;
219 char desttxt[30];
220 struct sockaddr_un addr;
221 struct dirent *dent;
222 DIR *dir;
223 int ret = 0, broadcast = 0, count = 0;
224
225 if (drv->test_socket < 0 || drv->socket_dir == NULL) {
226 wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d "
227 "socket_dir=%p)",
228 __func__, drv->test_socket, drv->socket_dir);
229 return -1;
230 }
231
232 broadcast = memcmp(dst, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
233 snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dst));
234
235 memcpy(eth.h_dest, dst, ETH_ALEN);
236 memcpy(eth.h_source, src, ETH_ALEN);
237 eth.h_proto = host_to_be16(proto);
238
239 io[0].iov_base = "ETHER ";
240 io[0].iov_len = 6;
241 io[1].iov_base = &eth;
242 io[1].iov_len = sizeof(eth);
243 io[2].iov_base = (u8 *) data;
244 io[2].iov_len = data_len;
245
246 memset(&msg, 0, sizeof(msg));
247 msg.msg_iov = io;
248 msg.msg_iovlen = 3;
249
250 dir = opendir(drv->socket_dir);
251 if (dir == NULL) {
252 perror("test_driver: opendir");
253 return -1;
254 }
255 while ((dent = readdir(dir))) {
256 #ifdef _DIRENT_HAVE_D_TYPE
257 /* Skip the file if it is not a socket. Also accept
258 * DT_UNKNOWN (0) in case the C library or underlying file
259 * system does not support d_type. */
260 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
261 continue;
262 #endif /* _DIRENT_HAVE_D_TYPE */
263 if (strcmp(dent->d_name, ".") == 0 ||
264 strcmp(dent->d_name, "..") == 0)
265 continue;
266
267 memset(&addr, 0, sizeof(addr));
268 addr.sun_family = AF_UNIX;
269 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
270 drv->socket_dir, dent->d_name);
271
272 if (strcmp(addr.sun_path, drv->own_socket_path) == 0)
273 continue;
274 if (!broadcast && strstr(dent->d_name, desttxt) == NULL)
275 continue;
276
277 wpa_printf(MSG_DEBUG, "%s: Send ether frame to %s",
278 __func__, dent->d_name);
279
280 msg.msg_name = &addr;
281 msg.msg_namelen = sizeof(addr);
282 ret = sendmsg(drv->test_socket, &msg, 0);
283 if (ret < 0)
284 perror("driver_test: sendmsg");
285 count++;
286 }
287 closedir(dir);
288
289 if (!broadcast && count == 0) {
290 wpa_printf(MSG_DEBUG, "%s: Destination " MACSTR " not found",
291 __func__, MAC2STR(dst));
292 return -1;
293 }
294
295 return ret;
296 }
297
298
299 static int wpa_driver_test_send_mlme(void *priv, const u8 *buf, size_t len)
300 {
301 struct test_driver_data *drv = priv;
302 struct msghdr msg;
303 struct iovec io[2];
304 const u8 *dest;
305 int ret = 0, broadcast = 0;
306 char desttxt[30];
307 struct sockaddr_un addr;
308 struct dirent *dent;
309 DIR *dir;
310 struct ieee80211_hdr *hdr;
311 u16 fc;
312
313 if (drv->test_socket < 0 || len < 10 || drv->socket_dir == NULL) {
314 wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d len=%lu"
315 " socket_dir=%p)",
316 __func__, drv->test_socket, (unsigned long) len,
317 drv->socket_dir);
318 return -1;
319 }
320
321 dest = buf;
322 dest += 4;
323 broadcast = memcmp(dest, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
324 snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dest));
325
326 io[0].iov_base = "MLME ";
327 io[0].iov_len = 5;
328 io[1].iov_base = (void *) buf;
329 io[1].iov_len = len;
330
331 memset(&msg, 0, sizeof(msg));
332 msg.msg_iov = io;
333 msg.msg_iovlen = 2;
334
335 dir = opendir(drv->socket_dir);
336 if (dir == NULL) {
337 perror("test_driver: opendir");
338 return -1;
339 }
340 while ((dent = readdir(dir))) {
341 #ifdef _DIRENT_HAVE_D_TYPE
342 /* Skip the file if it is not a socket. Also accept
343 * DT_UNKNOWN (0) in case the C library or underlying file
344 * system does not support d_type. */
345 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
346 continue;
347 #endif /* _DIRENT_HAVE_D_TYPE */
348 if (strcmp(dent->d_name, ".") == 0 ||
349 strcmp(dent->d_name, "..") == 0)
350 continue;
351
352 memset(&addr, 0, sizeof(addr));
353 addr.sun_family = AF_UNIX;
354 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
355 drv->socket_dir, dent->d_name);
356
357 if (strcmp(addr.sun_path, drv->own_socket_path) == 0)
358 continue;
359 if (!broadcast && strstr(dent->d_name, desttxt) == NULL)
360 continue;
361
362 wpa_printf(MSG_DEBUG, "%s: Send management frame to %s",
363 __func__, dent->d_name);
364
365 msg.msg_name = &addr;
366 msg.msg_namelen = sizeof(addr);
367 ret = sendmsg(drv->test_socket, &msg, 0);
368 if (ret < 0)
369 perror("driver_test: sendmsg");
370 }
371 closedir(dir);
372
373 hdr = (struct ieee80211_hdr *) buf;
374 fc = le_to_host16(hdr->frame_control);
375 hostapd_mgmt_tx_cb(drv->hapd, (u8 *) buf, len, WLAN_FC_GET_STYPE(fc),
376 ret >= 0);
377
378 return ret;
379 }
380
381
382 static void test_driver_scan(struct test_driver_data *drv,
383 struct sockaddr_un *from, socklen_t fromlen,
384 char *data)
385 {
386 char buf[512], *pos, *end;
387 int ret;
388 struct test_driver_bss *bss;
389 u8 sa[ETH_ALEN];
390 u8 ie[512];
391 size_t ielen;
392
393 /* data: optional [ ' ' | STA-addr | ' ' | IEs(hex) ] */
394
395 wpa_printf(MSG_DEBUG, "test_driver: SCAN");
396
397 if (*data) {
398 if (*data != ' ' ||
399 hwaddr_aton(data + 1, sa)) {
400 wpa_printf(MSG_DEBUG, "test_driver: Unexpected SCAN "
401 "command format");
402 return;
403 }
404
405 data += 18;
406 while (*data == ' ')
407 data++;
408 ielen = os_strlen(data) / 2;
409 if (ielen > sizeof(ie))
410 ielen = sizeof(ie);
411 if (hexstr2bin(data, ie, ielen) < 0)
412 ielen = 0;
413
414 wpa_printf(MSG_DEBUG, "test_driver: Scan from " MACSTR,
415 MAC2STR(sa));
416 wpa_hexdump(MSG_MSGDUMP, "test_driver: scan IEs", ie, ielen);
417
418 hostapd_wps_probe_req_rx(drv->hapd, sa, ie, ielen);
419 }
420
421 for (bss = drv->bss; bss; bss = bss->next) {
422 pos = buf;
423 end = buf + sizeof(buf);
424
425 /* reply: SCANRESP BSSID SSID IEs */
426 ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
427 MAC2STR(bss->bssid));
428 if (ret < 0 || ret >= end - pos)
429 return;
430 pos += ret;
431 pos += wpa_snprintf_hex(pos, end - pos,
432 bss->ssid, bss->ssid_len);
433 ret = snprintf(pos, end - pos, " ");
434 if (ret < 0 || ret >= end - pos)
435 return;
436 pos += ret;
437 pos += wpa_snprintf_hex(pos, end - pos, bss->ie, bss->ielen);
438 pos += wpa_snprintf_hex(pos, end - pos, bss->wps_probe_resp_ie,
439 bss->wps_probe_resp_ie_len);
440
441 if (bss->privacy) {
442 ret = snprintf(pos, end - pos, " PRIVACY");
443 if (ret < 0 || ret >= end - pos)
444 return;
445 pos += ret;
446 }
447
448 sendto(drv->test_socket, buf, pos - buf, 0,
449 (struct sockaddr *) from, fromlen);
450 }
451 }
452
453
454 static struct hostapd_data * test_driver_get_hapd(struct test_driver_data *drv,
455 struct test_driver_bss *bss)
456 {
457 struct hostapd_iface *iface = drv->hapd->iface;
458 struct hostapd_data *hapd = NULL;
459 size_t i;
460
461 if (bss == NULL) {
462 wpa_printf(MSG_DEBUG, "%s: bss == NULL", __func__);
463 return NULL;
464 }
465
466 for (i = 0; i < iface->num_bss; i++) {
467 hapd = iface->bss[i];
468 if (memcmp(hapd->own_addr, bss->bssid, ETH_ALEN) == 0)
469 break;
470 }
471 if (i == iface->num_bss) {
472 wpa_printf(MSG_DEBUG, "%s: no matching interface entry found "
473 "for BSSID " MACSTR, __func__, MAC2STR(bss->bssid));
474 return NULL;
475 }
476
477 return hapd;
478 }
479
480
481 static int test_driver_new_sta(struct test_driver_data *drv,
482 struct test_driver_bss *bss, const u8 *addr,
483 const u8 *ie, size_t ielen)
484 {
485 struct hostapd_data *hapd;
486
487 hapd = test_driver_get_hapd(drv, bss);
488 if (hapd == NULL)
489 return -1;
490
491 return hostapd_notif_assoc(hapd, addr, ie, ielen);
492 }
493
494
495 static void test_driver_assoc(struct test_driver_data *drv,
496 struct sockaddr_un *from, socklen_t fromlen,
497 char *data)
498 {
499 struct test_client_socket *cli;
500 u8 ie[256], ssid[32];
501 size_t ielen, ssid_len = 0;
502 char *pos, *pos2, cmd[50];
503 struct test_driver_bss *bss;
504
505 /* data: STA-addr SSID(hex) IEs(hex) */
506
507 cli = os_zalloc(sizeof(*cli));
508 if (cli == NULL)
509 return;
510
511 if (hwaddr_aton(data, cli->addr)) {
512 printf("test_socket: Invalid MAC address '%s' in ASSOC\n",
513 data);
514 free(cli);
515 return;
516 }
517 pos = data + 17;
518 while (*pos == ' ')
519 pos++;
520 pos2 = strchr(pos, ' ');
521 ielen = 0;
522 if (pos2) {
523 ssid_len = (pos2 - pos) / 2;
524 if (hexstr2bin(pos, ssid, ssid_len) < 0) {
525 wpa_printf(MSG_DEBUG, "%s: Invalid SSID", __func__);
526 free(cli);
527 return;
528 }
529 wpa_hexdump_ascii(MSG_DEBUG, "test_driver_assoc: SSID",
530 ssid, ssid_len);
531
532 pos = pos2 + 1;
533 ielen = strlen(pos) / 2;
534 if (ielen > sizeof(ie))
535 ielen = sizeof(ie);
536 if (hexstr2bin(pos, ie, ielen) < 0)
537 ielen = 0;
538 }
539
540 for (bss = drv->bss; bss; bss = bss->next) {
541 if (bss->ssid_len == ssid_len &&
542 memcmp(bss->ssid, ssid, ssid_len) == 0)
543 break;
544 }
545 if (bss == NULL) {
546 wpa_printf(MSG_DEBUG, "%s: No matching SSID found from "
547 "configured BSSes", __func__);
548 free(cli);
549 return;
550 }
551
552 cli->bss = bss;
553 memcpy(&cli->un, from, sizeof(cli->un));
554 cli->unlen = fromlen;
555 cli->next = drv->cli;
556 drv->cli = cli;
557 wpa_hexdump_ascii(MSG_DEBUG, "test_socket: ASSOC sun_path",
558 (const u8 *) cli->un.sun_path,
559 cli->unlen - sizeof(cli->un.sun_family));
560
561 snprintf(cmd, sizeof(cmd), "ASSOCRESP " MACSTR " 0",
562 MAC2STR(bss->bssid));
563 sendto(drv->test_socket, cmd, strlen(cmd), 0,
564 (struct sockaddr *) from, fromlen);
565
566 if (test_driver_new_sta(drv, bss, cli->addr, ie, ielen) < 0) {
567 wpa_printf(MSG_DEBUG, "test_driver: failed to add new STA");
568 }
569 }
570
571
572 static void test_driver_disassoc(struct test_driver_data *drv,
573 struct sockaddr_un *from, socklen_t fromlen)
574 {
575 struct test_client_socket *cli;
576
577 cli = test_driver_get_cli(drv, from, fromlen);
578 if (!cli)
579 return;
580
581 hostapd_notif_disassoc(drv->hapd, cli->addr);
582 }
583
584
585 static void test_driver_eapol(struct test_driver_data *drv,
586 struct sockaddr_un *from, socklen_t fromlen,
587 u8 *data, size_t datalen)
588 {
589 struct test_client_socket *cli;
590 if (datalen > 14) {
591 /* Skip Ethernet header */
592 wpa_printf(MSG_DEBUG, "test_driver: dst=" MACSTR " src="
593 MACSTR " proto=%04x",
594 MAC2STR(data), MAC2STR(data + ETH_ALEN),
595 WPA_GET_BE16(data + 2 * ETH_ALEN));
596 data += 14;
597 datalen -= 14;
598 }
599 cli = test_driver_get_cli(drv, from, fromlen);
600 if (cli) {
601 struct hostapd_data *hapd;
602 hapd = test_driver_get_hapd(drv, cli->bss);
603 if (hapd == NULL)
604 return;
605 hostapd_eapol_receive(hapd, cli->addr, data, datalen);
606 } else {
607 wpa_printf(MSG_DEBUG, "test_socket: EAPOL from unknown "
608 "client");
609 }
610 }
611
612
613 static void test_driver_ether(struct test_driver_data *drv,
614 struct sockaddr_un *from, socklen_t fromlen,
615 u8 *data, size_t datalen)
616 {
617 struct l2_ethhdr *eth;
618
619 if (datalen < sizeof(*eth))
620 return;
621
622 eth = (struct l2_ethhdr *) data;
623 wpa_printf(MSG_DEBUG, "test_driver: RX ETHER dst=" MACSTR " src="
624 MACSTR " proto=%04x",
625 MAC2STR(eth->h_dest), MAC2STR(eth->h_source),
626 be_to_host16(eth->h_proto));
627
628 #ifdef CONFIG_IEEE80211R
629 if (be_to_host16(eth->h_proto) == ETH_P_RRB) {
630 wpa_ft_rrb_rx(drv->hapd->wpa_auth, eth->h_source,
631 data + sizeof(*eth), datalen - sizeof(*eth));
632 }
633 #endif /* CONFIG_IEEE80211R */
634 }
635
636
637 static void test_driver_mlme(struct test_driver_data *drv,
638 struct sockaddr_un *from, socklen_t fromlen,
639 u8 *data, size_t datalen)
640 {
641 struct ieee80211_hdr *hdr;
642 u16 fc;
643
644 hdr = (struct ieee80211_hdr *) data;
645
646 if (test_driver_get_cli(drv, from, fromlen) == NULL && datalen >= 16) {
647 struct test_client_socket *cli;
648 cli = os_zalloc(sizeof(*cli));
649 if (cli == NULL)
650 return;
651 wpa_printf(MSG_DEBUG, "Adding client entry for " MACSTR,
652 MAC2STR(hdr->addr2));
653 memcpy(cli->addr, hdr->addr2, ETH_ALEN);
654 memcpy(&cli->un, from, sizeof(cli->un));
655 cli->unlen = fromlen;
656 cli->next = drv->cli;
657 drv->cli = cli;
658 }
659
660 wpa_hexdump(MSG_MSGDUMP, "test_driver_mlme: received frame",
661 data, datalen);
662 fc = le_to_host16(hdr->frame_control);
663 if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT) {
664 wpa_printf(MSG_ERROR, "%s: received non-mgmt frame",
665 __func__);
666 return;
667 }
668 hostapd_mgmt_rx(drv->hapd, data, datalen, WLAN_FC_GET_STYPE(fc), NULL);
669 }
670
671
672 static void test_driver_receive_unix(int sock, void *eloop_ctx, void *sock_ctx)
673 {
674 struct test_driver_data *drv = eloop_ctx;
675 char buf[2000];
676 int res;
677 struct sockaddr_un from;
678 socklen_t fromlen = sizeof(from);
679
680 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
681 (struct sockaddr *) &from, &fromlen);
682 if (res < 0) {
683 perror("recvfrom(test_socket)");
684 return;
685 }
686 buf[res] = '\0';
687
688 wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
689
690 if (strncmp(buf, "SCAN", 4) == 0) {
691 test_driver_scan(drv, &from, fromlen, buf + 4);
692 } else if (strncmp(buf, "ASSOC ", 6) == 0) {
693 test_driver_assoc(drv, &from, fromlen, buf + 6);
694 } else if (strcmp(buf, "DISASSOC") == 0) {
695 test_driver_disassoc(drv, &from, fromlen);
696 } else if (strncmp(buf, "EAPOL ", 6) == 0) {
697 test_driver_eapol(drv, &from, fromlen, (u8 *) buf + 6,
698 res - 6);
699 } else if (strncmp(buf, "ETHER ", 6) == 0) {
700 test_driver_ether(drv, &from, fromlen, (u8 *) buf + 6,
701 res - 6);
702 } else if (strncmp(buf, "MLME ", 5) == 0) {
703 test_driver_mlme(drv, &from, fromlen, (u8 *) buf + 5, res - 5);
704 } else {
705 wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
706 (u8 *) buf, res);
707 }
708 }
709
710
711 static struct test_driver_bss *
712 test_driver_get_bss(struct test_driver_data *drv, const char *ifname)
713 {
714 struct test_driver_bss *bss;
715
716 for (bss = drv->bss; bss; bss = bss->next) {
717 if (strcmp(bss->ifname, ifname) == 0)
718 return bss;
719 }
720 return NULL;
721 }
722
723
724 static int test_driver_set_generic_elem(const char *ifname, void *priv,
725 const u8 *elem, size_t elem_len)
726 {
727 struct test_driver_data *drv = priv;
728 struct test_driver_bss *bss;
729
730 bss = test_driver_get_bss(drv, ifname);
731 if (bss == NULL)
732 return -1;
733
734 free(bss->ie);
735
736 if (elem == NULL) {
737 bss->ie = NULL;
738 bss->ielen = 0;
739 return 0;
740 }
741
742 bss->ie = malloc(elem_len);
743 if (bss->ie == NULL) {
744 bss->ielen = 0;
745 return -1;
746 }
747
748 memcpy(bss->ie, elem, elem_len);
749 bss->ielen = elem_len;
750 return 0;
751 }
752
753
754 static int test_driver_set_wps_beacon_ie(const char *ifname, void *priv,
755 const u8 *ie, size_t len)
756 {
757 struct test_driver_data *drv = priv;
758 struct test_driver_bss *bss;
759
760 wpa_hexdump(MSG_DEBUG, "test_driver: Beacon WPS IE", ie, len);
761 bss = test_driver_get_bss(drv, ifname);
762 if (bss == NULL)
763 return -1;
764
765 free(bss->wps_beacon_ie);
766
767 if (ie == NULL) {
768 bss->wps_beacon_ie = NULL;
769 bss->wps_beacon_ie_len = 0;
770 return 0;
771 }
772
773 bss->wps_beacon_ie = malloc(len);
774 if (bss->wps_beacon_ie == NULL) {
775 bss->wps_beacon_ie_len = 0;
776 return -1;
777 }
778
779 memcpy(bss->wps_beacon_ie, ie, len);
780 bss->wps_beacon_ie_len = len;
781 return 0;
782 }
783
784
785 static int test_driver_set_wps_probe_resp_ie(const char *ifname, void *priv,
786 const u8 *ie, size_t len)
787 {
788 struct test_driver_data *drv = priv;
789 struct test_driver_bss *bss;
790
791 wpa_hexdump(MSG_DEBUG, "test_driver: ProbeResp WPS IE", ie, len);
792 bss = test_driver_get_bss(drv, ifname);
793 if (bss == NULL)
794 return -1;
795
796 free(bss->wps_probe_resp_ie);
797
798 if (ie == NULL) {
799 bss->wps_probe_resp_ie = NULL;
800 bss->wps_probe_resp_ie_len = 0;
801 return 0;
802 }
803
804 bss->wps_probe_resp_ie = malloc(len);
805 if (bss->wps_probe_resp_ie == NULL) {
806 bss->wps_probe_resp_ie_len = 0;
807 return -1;
808 }
809
810 memcpy(bss->wps_probe_resp_ie, ie, len);
811 bss->wps_probe_resp_ie_len = len;
812 return 0;
813 }
814
815
816 static int test_driver_sta_deauth(void *priv, const u8 *addr, int reason)
817 {
818 struct test_driver_data *drv = priv;
819 struct test_client_socket *cli;
820
821 if (drv->test_socket < 0)
822 return -1;
823
824 cli = drv->cli;
825 while (cli) {
826 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
827 break;
828 cli = cli->next;
829 }
830
831 if (!cli)
832 return -1;
833
834 return sendto(drv->test_socket, "DEAUTH", 6, 0,
835 (struct sockaddr *) &cli->un, cli->unlen);
836 }
837
838
839 static int test_driver_sta_disassoc(void *priv, const u8 *addr, int reason)
840 {
841 struct test_driver_data *drv = priv;
842 struct test_client_socket *cli;
843
844 if (drv->test_socket < 0)
845 return -1;
846
847 cli = drv->cli;
848 while (cli) {
849 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
850 break;
851 cli = cli->next;
852 }
853
854 if (!cli)
855 return -1;
856
857 return sendto(drv->test_socket, "DISASSOC", 8, 0,
858 (struct sockaddr *) &cli->un, cli->unlen);
859 }
860
861
862 static int test_driver_bss_add(void *priv, const char *ifname, const u8 *bssid)
863 {
864 struct test_driver_data *drv = priv;
865 struct test_driver_bss *bss;
866
867 wpa_printf(MSG_DEBUG, "%s(ifname=%s bssid=" MACSTR ")",
868 __func__, ifname, MAC2STR(bssid));
869
870 bss = os_zalloc(sizeof(*bss));
871 if (bss == NULL)
872 return -1;
873
874 os_strlcpy(bss->ifname, ifname, IFNAMSIZ);
875 memcpy(bss->bssid, bssid, ETH_ALEN);
876
877 bss->next = drv->bss;
878 drv->bss = bss;
879
880 return 0;
881 }
882
883
884 static int test_driver_bss_remove(void *priv, const char *ifname)
885 {
886 struct test_driver_data *drv = priv;
887 struct test_driver_bss *bss, *prev;
888 struct test_client_socket *cli, *prev_c;
889
890 wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
891
892 for (prev = NULL, bss = drv->bss; bss; prev = bss, bss = bss->next) {
893 if (strcmp(bss->ifname, ifname) != 0)
894 continue;
895
896 if (prev)
897 prev->next = bss->next;
898 else
899 drv->bss = bss->next;
900
901 for (prev_c = NULL, cli = drv->cli; cli;
902 prev_c = cli, cli = cli->next) {
903 if (cli->bss != bss)
904 continue;
905 if (prev_c)
906 prev_c->next = cli->next;
907 else
908 drv->cli = cli->next;
909 free(cli);
910 break;
911 }
912
913 test_driver_free_bss(bss);
914 return 0;
915 }
916
917 return -1;
918 }
919
920
921 static int test_driver_if_add(const char *iface, void *priv,
922 enum hostapd_driver_if_type type, char *ifname,
923 const u8 *addr)
924 {
925 wpa_printf(MSG_DEBUG, "%s(iface=%s type=%d ifname=%s)",
926 __func__, iface, type, ifname);
927 return 0;
928 }
929
930
931 static int test_driver_if_update(void *priv, enum hostapd_driver_if_type type,
932 char *ifname, const u8 *addr)
933 {
934 wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
935 return 0;
936 }
937
938
939 static int test_driver_if_remove(void *priv, enum hostapd_driver_if_type type,
940 const char *ifname, const u8 *addr)
941 {
942 wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
943 return 0;
944 }
945
946
947 static int test_driver_valid_bss_mask(void *priv, const u8 *addr,
948 const u8 *mask)
949 {
950 return 0;
951 }
952
953
954 static int test_driver_set_ssid(const char *ifname, void *priv, const u8 *buf,
955 int len)
956 {
957 struct test_driver_data *drv = priv;
958 struct test_driver_bss *bss;
959
960 wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
961 wpa_hexdump_ascii(MSG_DEBUG, "test_driver_set_ssid: SSID", buf, len);
962
963 for (bss = drv->bss; bss; bss = bss->next) {
964 if (strcmp(bss->ifname, ifname) != 0)
965 continue;
966
967 if (len < 0 || (size_t) len > sizeof(bss->ssid))
968 return -1;
969
970 memcpy(bss->ssid, buf, len);
971 bss->ssid_len = len;
972
973 return 0;
974 }
975
976 return -1;
977 }
978
979
980 static int test_driver_set_privacy(const char *ifname, void *priv, int enabled)
981 {
982 struct test_driver_data *drv = priv;
983 struct test_driver_bss *bss;
984
985 wpa_printf(MSG_DEBUG, "%s(ifname=%s enabled=%d)",
986 __func__, ifname, enabled);
987
988 for (bss = drv->bss; bss; bss = bss->next) {
989 if (strcmp(bss->ifname, ifname) != 0)
990 continue;
991
992 bss->privacy = enabled;
993
994 return 0;
995 }
996
997 return -1;
998 }
999
1000
1001 static int test_driver_set_key(const char *iface, void *priv, wpa_alg alg,
1002 const u8 *addr, int key_idx, int set_tx,
1003 const u8 *seq, size_t seq_len,
1004 const u8 *key, size_t key_len)
1005 {
1006 wpa_printf(MSG_DEBUG, "%s(iface=%s alg=%d idx=%d set_tx=%d)",
1007 __func__, iface, alg, key_idx, set_tx);
1008 if (addr)
1009 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
1010 if (key)
1011 wpa_hexdump_key(MSG_DEBUG, " key", key, key_len);
1012 return 0;
1013 }
1014
1015
1016 static int test_driver_set_sta_vlan(void *priv, const u8 *addr,
1017 const char *ifname, int vlan_id)
1018 {
1019 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " ifname=%s vlan_id=%d)",
1020 __func__, MAC2STR(addr), ifname, vlan_id);
1021 return 0;
1022 }
1023
1024
1025 static int test_driver_sta_add(const char *ifname, void *priv,
1026 struct hostapd_sta_add_params *params)
1027 {
1028 struct test_driver_data *drv = priv;
1029 struct test_client_socket *cli;
1030 struct test_driver_bss *bss;
1031
1032 wpa_printf(MSG_DEBUG, "%s(ifname=%s addr=" MACSTR " aid=%d "
1033 "capability=0x%x flags=0x%x listen_interval=%d)",
1034 __func__, ifname, MAC2STR(params->addr), params->aid,
1035 params->capability, params->flags,
1036 params->listen_interval);
1037 wpa_hexdump(MSG_DEBUG, "test_driver_sta_add - supp_rates",
1038 params->supp_rates, params->supp_rates_len);
1039
1040 cli = drv->cli;
1041 while (cli) {
1042 if (os_memcmp(cli->addr, params->addr, ETH_ALEN) == 0)
1043 break;
1044 cli = cli->next;
1045 }
1046 if (!cli) {
1047 wpa_printf(MSG_DEBUG, "%s: no matching client entry",
1048 __func__);
1049 return -1;
1050 }
1051
1052 for (bss = drv->bss; bss; bss = bss->next) {
1053 if (strcmp(ifname, bss->ifname) == 0)
1054 break;
1055 }
1056 if (bss == NULL) {
1057 wpa_printf(MSG_DEBUG, "%s: No matching interface found from "
1058 "configured BSSes", __func__);
1059 return -1;
1060 }
1061
1062 cli->bss = bss;
1063
1064 return 0;
1065 }
1066
1067
1068 static void * test_driver_init(struct hostapd_data *hapd,
1069 struct wpa_init_params *params)
1070 {
1071 struct test_driver_data *drv;
1072 struct sockaddr_un addr_un;
1073 struct sockaddr_in addr_in;
1074 struct sockaddr *addr;
1075 socklen_t alen;
1076
1077 drv = os_zalloc(sizeof(struct test_driver_data));
1078 if (drv == NULL) {
1079 printf("Could not allocate memory for test driver data\n");
1080 return NULL;
1081 }
1082 drv->bss = os_zalloc(sizeof(*drv->bss));
1083 if (drv->bss == NULL) {
1084 printf("Could not allocate memory for test driver BSS data\n");
1085 free(drv);
1086 return NULL;
1087 }
1088
1089 drv->hapd = hapd;
1090
1091 /* Generate a MAC address to help testing with multiple APs */
1092 hapd->own_addr[0] = 0x02; /* locally administered */
1093 sha1_prf((const u8 *) params->ifname, strlen(params->ifname),
1094 "hostapd test bssid generation",
1095 params->ssid, params->ssid_len,
1096 hapd->own_addr + 1, ETH_ALEN - 1);
1097
1098 os_strlcpy(drv->bss->ifname, params->ifname, IFNAMSIZ);
1099 memcpy(drv->bss->bssid, hapd->own_addr, ETH_ALEN);
1100
1101 if (params->test_socket) {
1102 if (os_strlen(params->test_socket) >=
1103 sizeof(addr_un.sun_path)) {
1104 printf("Too long test_socket path\n");
1105 test_driver_free_priv(drv);
1106 return NULL;
1107 }
1108 if (strncmp(params->test_socket, "DIR:", 4) == 0) {
1109 size_t len = strlen(params->test_socket) + 30;
1110 drv->socket_dir = strdup(params->test_socket + 4);
1111 drv->own_socket_path = malloc(len);
1112 if (drv->own_socket_path) {
1113 snprintf(drv->own_socket_path, len,
1114 "%s/AP-" MACSTR,
1115 params->test_socket + 4,
1116 MAC2STR(hapd->own_addr));
1117 }
1118 } else if (strncmp(params->test_socket, "UDP:", 4) == 0) {
1119 drv->udp_port = atoi(params->test_socket + 4);
1120 } else {
1121 drv->own_socket_path = strdup(params->test_socket);
1122 }
1123 if (drv->own_socket_path == NULL && drv->udp_port == 0) {
1124 test_driver_free_priv(drv);
1125 return NULL;
1126 }
1127
1128 drv->test_socket = socket(drv->udp_port ? PF_INET : PF_UNIX,
1129 SOCK_DGRAM, 0);
1130 if (drv->test_socket < 0) {
1131 perror("socket");
1132 test_driver_free_priv(drv);
1133 return NULL;
1134 }
1135
1136 if (drv->udp_port) {
1137 os_memset(&addr_in, 0, sizeof(addr_in));
1138 addr_in.sin_family = AF_INET;
1139 addr_in.sin_port = htons(drv->udp_port);
1140 addr = (struct sockaddr *) &addr_in;
1141 alen = sizeof(addr_in);
1142 } else {
1143 os_memset(&addr_un, 0, sizeof(addr_un));
1144 addr_un.sun_family = AF_UNIX;
1145 os_strlcpy(addr_un.sun_path, drv->own_socket_path,
1146 sizeof(addr_un.sun_path));
1147 addr = (struct sockaddr *) &addr_un;
1148 alen = sizeof(addr_un);
1149 }
1150 if (bind(drv->test_socket, addr, alen) < 0) {
1151 perror("bind(PF_UNIX)");
1152 close(drv->test_socket);
1153 if (drv->own_socket_path)
1154 unlink(drv->own_socket_path);
1155 test_driver_free_priv(drv);
1156 return NULL;
1157 }
1158 eloop_register_read_sock(drv->test_socket,
1159 test_driver_receive_unix, drv, NULL);
1160 } else
1161 drv->test_socket = -1;
1162
1163 return drv;
1164 }
1165
1166
1167 static void test_driver_deinit(void *priv)
1168 {
1169 struct test_driver_data *drv = priv;
1170 struct test_client_socket *cli, *prev;
1171
1172 cli = drv->cli;
1173 while (cli) {
1174 prev = cli;
1175 cli = cli->next;
1176 free(prev);
1177 }
1178
1179 if (drv->test_socket >= 0) {
1180 eloop_unregister_read_sock(drv->test_socket);
1181 close(drv->test_socket);
1182 if (drv->own_socket_path)
1183 unlink(drv->own_socket_path);
1184 }
1185
1186 /* There should be only one BSS remaining at this point. */
1187 if (drv->bss == NULL)
1188 wpa_printf(MSG_ERROR, "%s: drv->bss == NULL", __func__);
1189 else if (drv->bss->next)
1190 wpa_printf(MSG_ERROR, "%s: drv->bss->next != NULL", __func__);
1191
1192 test_driver_free_priv(drv);
1193 }
1194
1195 #else /* HOSTAPD */
1196
1197 static void wpa_driver_test_poll(void *eloop_ctx, void *timeout_ctx)
1198 {
1199 struct wpa_driver_test_data *drv = eloop_ctx;
1200
1201 #ifdef DRIVER_TEST_UNIX
1202 if (drv->associated && drv->hostapd_addr_set) {
1203 struct stat st;
1204 if (stat(drv->hostapd_addr.sun_path, &st) < 0) {
1205 wpa_printf(MSG_DEBUG, "%s: lost connection to AP: %s",
1206 __func__, strerror(errno));
1207 drv->associated = 0;
1208 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1209 }
1210 }
1211 #endif /* DRIVER_TEST_UNIX */
1212
1213 eloop_register_timeout(1, 0, wpa_driver_test_poll, drv, NULL);
1214 }
1215
1216
1217 static int wpa_driver_test_set_wpa(void *priv, int enabled)
1218 {
1219 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
1220 return 0;
1221 }
1222
1223
1224 static void wpa_driver_test_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1225 {
1226 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
1227 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
1228 }
1229
1230
1231 #ifdef DRIVER_TEST_UNIX
1232 static void wpa_driver_scan_dir(struct wpa_driver_test_data *drv,
1233 const char *path)
1234 {
1235 struct dirent *dent;
1236 DIR *dir;
1237 struct sockaddr_un addr;
1238 char cmd[512], *pos, *end;
1239 int ret;
1240
1241 dir = opendir(path);
1242 if (dir == NULL)
1243 return;
1244
1245 end = cmd + sizeof(cmd);
1246 pos = cmd;
1247 ret = os_snprintf(pos, end - pos, "SCAN " MACSTR,
1248 MAC2STR(drv->own_addr));
1249 if (ret >= 0 && ret < end - pos)
1250 pos += ret;
1251 if (drv->probe_req_ie) {
1252 ret = os_snprintf(pos, end - pos, " ");
1253 if (ret >= 0 && ret < end - pos)
1254 pos += ret;
1255 pos += wpa_snprintf_hex(pos, end - pos, drv->probe_req_ie,
1256 drv->probe_req_ie_len);
1257 }
1258 end[-1] = '\0';
1259
1260 while ((dent = readdir(dir))) {
1261 if (os_strncmp(dent->d_name, "AP-", 3) != 0 &&
1262 os_strncmp(dent->d_name, "STA-", 4) != 0)
1263 continue;
1264 if (drv->own_socket_path) {
1265 size_t olen, dlen;
1266 olen = os_strlen(drv->own_socket_path);
1267 dlen = os_strlen(dent->d_name);
1268 if (olen >= dlen &&
1269 os_strcmp(dent->d_name,
1270 drv->own_socket_path + olen - dlen) == 0)
1271 continue;
1272 }
1273 wpa_printf(MSG_DEBUG, "%s: SCAN %s", __func__, dent->d_name);
1274
1275 os_memset(&addr, 0, sizeof(addr));
1276 addr.sun_family = AF_UNIX;
1277 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
1278 path, dent->d_name);
1279
1280 if (sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
1281 (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1282 perror("sendto(test_socket)");
1283 }
1284 }
1285 closedir(dir);
1286 }
1287 #endif /* DRIVER_TEST_UNIX */
1288
1289
1290 static int wpa_driver_test_scan(void *priv,
1291 struct wpa_driver_scan_params *params)
1292 {
1293 struct wpa_driver_test_data *drv = priv;
1294 size_t i;
1295
1296 wpa_printf(MSG_DEBUG, "%s: priv=%p", __func__, priv);
1297 for (i = 0; i < params->num_ssids; i++)
1298 wpa_hexdump(MSG_DEBUG, "Scan SSID",
1299 params->ssids[i].ssid, params->ssids[i].ssid_len);
1300 wpa_hexdump(MSG_DEBUG, "Scan extra IE(s)",
1301 params->extra_ies, params->extra_ies_len);
1302
1303 drv->num_scanres = 0;
1304
1305 #ifdef DRIVER_TEST_UNIX
1306 if (drv->test_socket >= 0 && drv->test_dir)
1307 wpa_driver_scan_dir(drv, drv->test_dir);
1308
1309 if (drv->test_socket >= 0 && drv->hostapd_addr_set &&
1310 sendto(drv->test_socket, "SCAN", 4, 0,
1311 (struct sockaddr *) &drv->hostapd_addr,
1312 sizeof(drv->hostapd_addr)) < 0) {
1313 perror("sendto(test_socket)");
1314 }
1315 #endif /* DRIVER_TEST_UNIX */
1316
1317 if (drv->test_socket >= 0 && drv->hostapd_addr_udp_set &&
1318 sendto(drv->test_socket, "SCAN", 4, 0,
1319 (struct sockaddr *) &drv->hostapd_addr_udp,
1320 sizeof(drv->hostapd_addr_udp)) < 0) {
1321 perror("sendto(test_socket)");
1322 }
1323
1324 eloop_cancel_timeout(wpa_driver_test_scan_timeout, drv, drv->ctx);
1325 eloop_register_timeout(1, 0, wpa_driver_test_scan_timeout, drv,
1326 drv->ctx);
1327 return 0;
1328 }
1329
1330
1331 static struct wpa_scan_results * wpa_driver_test_get_scan_results2(void *priv)
1332 {
1333 struct wpa_driver_test_data *drv = priv;
1334 struct wpa_scan_results *res;
1335 size_t i;
1336
1337 res = os_zalloc(sizeof(*res));
1338 if (res == NULL)
1339 return NULL;
1340
1341 res->res = os_zalloc(drv->num_scanres * sizeof(struct wpa_scan_res *));
1342 if (res->res == NULL) {
1343 os_free(res);
1344 return NULL;
1345 }
1346
1347 for (i = 0; i < drv->num_scanres; i++) {
1348 struct wpa_scan_res *r;
1349 if (drv->scanres[i] == NULL)
1350 continue;
1351 r = os_malloc(sizeof(*r) + drv->scanres[i]->ie_len);
1352 if (r == NULL)
1353 break;
1354 os_memcpy(r, drv->scanres[i],
1355 sizeof(*r) + drv->scanres[i]->ie_len);
1356 res->res[res->num++] = r;
1357 }
1358
1359 return res;
1360 }
1361
1362
1363 static int wpa_driver_test_set_key(void *priv, wpa_alg alg, const u8 *addr,
1364 int key_idx, int set_tx,
1365 const u8 *seq, size_t seq_len,
1366 const u8 *key, size_t key_len)
1367 {
1368 wpa_printf(MSG_DEBUG, "%s: priv=%p alg=%d key_idx=%d set_tx=%d",
1369 __func__, priv, alg, key_idx, set_tx);
1370 if (addr) {
1371 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
1372 }
1373 if (seq) {
1374 wpa_hexdump(MSG_DEBUG, " seq", seq, seq_len);
1375 }
1376 if (key) {
1377 wpa_hexdump(MSG_DEBUG, " key", key, key_len);
1378 }
1379 return 0;
1380 }
1381
1382
1383 static int wpa_driver_test_associate(
1384 void *priv, struct wpa_driver_associate_params *params)
1385 {
1386 struct wpa_driver_test_data *drv = priv;
1387 wpa_printf(MSG_DEBUG, "%s: priv=%p freq=%d pairwise_suite=%d "
1388 "group_suite=%d key_mgmt_suite=%d auth_alg=%d mode=%d",
1389 __func__, priv, params->freq, params->pairwise_suite,
1390 params->group_suite, params->key_mgmt_suite,
1391 params->auth_alg, params->mode);
1392 if (params->bssid) {
1393 wpa_printf(MSG_DEBUG, " bssid=" MACSTR,
1394 MAC2STR(params->bssid));
1395 }
1396 if (params->ssid) {
1397 wpa_hexdump_ascii(MSG_DEBUG, " ssid",
1398 params->ssid, params->ssid_len);
1399 }
1400 if (params->wpa_ie) {
1401 wpa_hexdump(MSG_DEBUG, " wpa_ie",
1402 params->wpa_ie, params->wpa_ie_len);
1403 drv->assoc_wpa_ie_len = params->wpa_ie_len;
1404 if (drv->assoc_wpa_ie_len > sizeof(drv->assoc_wpa_ie))
1405 drv->assoc_wpa_ie_len = sizeof(drv->assoc_wpa_ie);
1406 os_memcpy(drv->assoc_wpa_ie, params->wpa_ie,
1407 drv->assoc_wpa_ie_len);
1408 } else
1409 drv->assoc_wpa_ie_len = 0;
1410
1411 drv->ibss = params->mode == IEEE80211_MODE_IBSS;
1412 drv->privacy = params->key_mgmt_suite &
1413 (WPA_KEY_MGMT_IEEE8021X |
1414 WPA_KEY_MGMT_PSK |
1415 WPA_KEY_MGMT_WPA_NONE |
1416 WPA_KEY_MGMT_FT_IEEE8021X |
1417 WPA_KEY_MGMT_FT_PSK |
1418 WPA_KEY_MGMT_IEEE8021X_SHA256 |
1419 WPA_KEY_MGMT_PSK_SHA256);
1420 if (params->wep_key_len[params->wep_tx_keyidx])
1421 drv->privacy = 1;
1422
1423 #ifdef DRIVER_TEST_UNIX
1424 if (drv->test_dir && params->bssid &&
1425 params->mode != IEEE80211_MODE_IBSS) {
1426 os_memset(&drv->hostapd_addr, 0, sizeof(drv->hostapd_addr));
1427 drv->hostapd_addr.sun_family = AF_UNIX;
1428 os_snprintf(drv->hostapd_addr.sun_path,
1429 sizeof(drv->hostapd_addr.sun_path),
1430 "%s/AP-" MACSTR,
1431 drv->test_dir, MAC2STR(params->bssid));
1432 drv->hostapd_addr_set = 1;
1433 }
1434 #endif /* DRIVER_TEST_UNIX */
1435
1436 if (drv->test_socket >= 0 &&
1437 (drv->hostapd_addr_set || drv->hostapd_addr_udp_set)) {
1438 char cmd[200], *pos, *end;
1439 int ret;
1440 end = cmd + sizeof(cmd);
1441 pos = cmd;
1442 ret = os_snprintf(pos, end - pos, "ASSOC " MACSTR " ",
1443 MAC2STR(drv->own_addr));
1444 if (ret >= 0 && ret < end - pos)
1445 pos += ret;
1446 pos += wpa_snprintf_hex(pos, end - pos, params->ssid,
1447 params->ssid_len);
1448 ret = os_snprintf(pos, end - pos, " ");
1449 if (ret >= 0 && ret < end - pos)
1450 pos += ret;
1451 pos += wpa_snprintf_hex(pos, end - pos, params->wpa_ie,
1452 params->wpa_ie_len);
1453 end[-1] = '\0';
1454 #ifdef DRIVER_TEST_UNIX
1455 if (drv->hostapd_addr_set &&
1456 sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
1457 (struct sockaddr *) &drv->hostapd_addr,
1458 sizeof(drv->hostapd_addr)) < 0) {
1459 perror("sendto(test_socket)");
1460 return -1;
1461 }
1462 #endif /* DRIVER_TEST_UNIX */
1463 if (drv->hostapd_addr_udp_set &&
1464 sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
1465 (struct sockaddr *) &drv->hostapd_addr_udp,
1466 sizeof(drv->hostapd_addr_udp)) < 0) {
1467 perror("sendto(test_socket)");
1468 return -1;
1469 }
1470
1471 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
1472 drv->ssid_len = params->ssid_len;
1473 } else {
1474 drv->associated = 1;
1475 if (params->mode == IEEE80211_MODE_IBSS) {
1476 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
1477 drv->ssid_len = params->ssid_len;
1478 if (params->bssid)
1479 os_memcpy(drv->bssid, params->bssid, ETH_ALEN);
1480 else {
1481 os_get_random(drv->bssid, ETH_ALEN);
1482 drv->bssid[0] &= ~0x01;
1483 drv->bssid[0] |= 0x02;
1484 }
1485 }
1486 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1487 }
1488
1489 return 0;
1490 }
1491
1492
1493 static int wpa_driver_test_get_bssid(void *priv, u8 *bssid)
1494 {
1495 struct wpa_driver_test_data *drv = priv;
1496 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1497 return 0;
1498 }
1499
1500
1501 static int wpa_driver_test_get_ssid(void *priv, u8 *ssid)
1502 {
1503 struct wpa_driver_test_data *drv = priv;
1504 os_memcpy(ssid, drv->ssid, 32);
1505 return drv->ssid_len;
1506 }
1507
1508
1509 static int wpa_driver_test_send_disassoc(struct wpa_driver_test_data *drv)
1510 {
1511 #ifdef DRIVER_TEST_UNIX
1512 if (drv->test_socket >= 0 &&
1513 sendto(drv->test_socket, "DISASSOC", 8, 0,
1514 (struct sockaddr *) &drv->hostapd_addr,
1515 sizeof(drv->hostapd_addr)) < 0) {
1516 perror("sendto(test_socket)");
1517 return -1;
1518 }
1519 #endif /* DRIVER_TEST_UNIX */
1520 if (drv->test_socket >= 0 && drv->hostapd_addr_udp_set &&
1521 sendto(drv->test_socket, "DISASSOC", 8, 0,
1522 (struct sockaddr *) &drv->hostapd_addr_udp,
1523 sizeof(drv->hostapd_addr_udp)) < 0) {
1524 perror("sendto(test_socket)");
1525 return -1;
1526 }
1527 return 0;
1528 }
1529
1530
1531 static int wpa_driver_test_deauthenticate(void *priv, const u8 *addr,
1532 int reason_code)
1533 {
1534 struct wpa_driver_test_data *drv = priv;
1535 wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d",
1536 __func__, MAC2STR(addr), reason_code);
1537 os_memset(drv->bssid, 0, ETH_ALEN);
1538 drv->associated = 0;
1539 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1540 return wpa_driver_test_send_disassoc(drv);
1541 }
1542
1543
1544 static int wpa_driver_test_disassociate(void *priv, const u8 *addr,
1545 int reason_code)
1546 {
1547 struct wpa_driver_test_data *drv = priv;
1548 wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d",
1549 __func__, MAC2STR(addr), reason_code);
1550 os_memset(drv->bssid, 0, ETH_ALEN);
1551 drv->associated = 0;
1552 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1553 return wpa_driver_test_send_disassoc(drv);
1554 }
1555
1556
1557 static void wpa_driver_test_scanresp(struct wpa_driver_test_data *drv,
1558 struct sockaddr *from,
1559 socklen_t fromlen,
1560 const char *data)
1561 {
1562 struct wpa_scan_res *res;
1563 const char *pos, *pos2;
1564 size_t len;
1565 u8 *ie_pos, *ie_start, *ie_end;
1566 #define MAX_IE_LEN 1000
1567
1568 wpa_printf(MSG_DEBUG, "test_driver: SCANRESP %s", data);
1569 if (drv->num_scanres >= MAX_SCAN_RESULTS) {
1570 wpa_printf(MSG_DEBUG, "test_driver: No room for the new scan "
1571 "result");
1572 return;
1573 }
1574
1575 /* SCANRESP BSSID SSID IEs */
1576
1577 res = os_zalloc(sizeof(*res) + MAX_IE_LEN);
1578 if (res == NULL)
1579 return;
1580 ie_start = ie_pos = (u8 *) (res + 1);
1581 ie_end = ie_pos + MAX_IE_LEN;
1582
1583 if (hwaddr_aton(data, res->bssid)) {
1584 wpa_printf(MSG_DEBUG, "test_driver: invalid BSSID in scanres");
1585 os_free(res);
1586 return;
1587 }
1588
1589 pos = data + 17;
1590 while (*pos == ' ')
1591 pos++;
1592 pos2 = os_strchr(pos, ' ');
1593 if (pos2 == NULL) {
1594 wpa_printf(MSG_DEBUG, "test_driver: invalid SSID termination "
1595 "in scanres");
1596 os_free(res);
1597 return;
1598 }
1599 len = (pos2 - pos) / 2;
1600 if (len > 32)
1601 len = 32;
1602 /*
1603 * Generate SSID IE from the SSID field since this IE is not included
1604 * in the main IE field.
1605 */
1606 *ie_pos++ = WLAN_EID_SSID;
1607 *ie_pos++ = len;
1608 if (hexstr2bin(pos, ie_pos, len) < 0) {
1609 wpa_printf(MSG_DEBUG, "test_driver: invalid SSID in scanres");
1610 os_free(res);
1611 return;
1612 }
1613 ie_pos += len;
1614
1615 pos = pos2 + 1;
1616 pos2 = os_strchr(pos, ' ');
1617 if (pos2 == NULL)
1618 len = os_strlen(pos) / 2;
1619 else
1620 len = (pos2 - pos) / 2;
1621 if ((int) len > ie_end - ie_pos)
1622 len = ie_end - ie_pos;
1623 if (hexstr2bin(pos, ie_pos, len) < 0) {
1624 wpa_printf(MSG_DEBUG, "test_driver: invalid IEs in scanres");
1625 os_free(res);
1626 return;
1627 }
1628 ie_pos += len;
1629 res->ie_len = ie_pos - ie_start;
1630
1631 if (pos2) {
1632 pos = pos2 + 1;
1633 while (*pos == ' ')
1634 pos++;
1635 if (os_strstr(pos, "PRIVACY"))
1636 res->caps |= IEEE80211_CAP_PRIVACY;
1637 if (os_strstr(pos, "IBSS"))
1638 res->caps |= IEEE80211_CAP_IBSS;
1639 }
1640
1641 os_free(drv->scanres[drv->num_scanres]);
1642 drv->scanres[drv->num_scanres++] = res;
1643 }
1644
1645
1646 static void wpa_driver_test_assocresp(struct wpa_driver_test_data *drv,
1647 struct sockaddr *from,
1648 socklen_t fromlen,
1649 const char *data)
1650 {
1651 /* ASSOCRESP BSSID <res> */
1652 if (hwaddr_aton(data, drv->bssid)) {
1653 wpa_printf(MSG_DEBUG, "test_driver: invalid BSSID in "
1654 "assocresp");
1655 }
1656 if (drv->use_associnfo) {
1657 union wpa_event_data event;
1658 os_memset(&event, 0, sizeof(event));
1659 event.assoc_info.req_ies = drv->assoc_wpa_ie;
1660 event.assoc_info.req_ies_len = drv->assoc_wpa_ie_len;
1661 wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &event);
1662 }
1663 drv->associated = 1;
1664 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1665 }
1666
1667
1668 static void wpa_driver_test_disassoc(struct wpa_driver_test_data *drv,
1669 struct sockaddr *from,
1670 socklen_t fromlen)
1671 {
1672 drv->associated = 0;
1673 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1674 }
1675
1676
1677 static void wpa_driver_test_eapol(struct wpa_driver_test_data *drv,
1678 struct sockaddr *from,
1679 socklen_t fromlen,
1680 const u8 *data, size_t data_len)
1681 {
1682 const u8 *src = drv->bssid;
1683
1684 if (data_len > 14) {
1685 /* Skip Ethernet header */
1686 src = data + ETH_ALEN;
1687 data += 14;
1688 data_len -= 14;
1689 }
1690 wpa_supplicant_rx_eapol(drv->ctx, src, data, data_len);
1691 }
1692
1693
1694 static void wpa_driver_test_mlme(struct wpa_driver_test_data *drv,
1695 struct sockaddr *from,
1696 socklen_t fromlen,
1697 const u8 *data, size_t data_len)
1698 {
1699 #ifdef CONFIG_CLIENT_MLME
1700 struct ieee80211_rx_status rx_status;
1701 os_memset(&rx_status, 0, sizeof(rx_status));
1702 wpa_supplicant_sta_rx(drv->ctx, data, data_len, &rx_status);
1703 #endif /* CONFIG_CLIENT_MLME */
1704 }
1705
1706
1707 static void wpa_driver_test_scan_cmd(struct wpa_driver_test_data *drv,
1708 struct sockaddr *from,
1709 socklen_t fromlen,
1710 const u8 *data, size_t data_len)
1711 {
1712 char buf[512], *pos, *end;
1713 int ret;
1714
1715 /* data: optional [ STA-addr | ' ' | IEs(hex) ] */
1716
1717 if (!drv->ibss)
1718 return;
1719
1720 pos = buf;
1721 end = buf + sizeof(buf);
1722
1723 /* reply: SCANRESP BSSID SSID IEs */
1724 ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
1725 MAC2STR(drv->bssid));
1726 if (ret < 0 || ret >= end - pos)
1727 return;
1728 pos += ret;
1729 pos += wpa_snprintf_hex(pos, end - pos,
1730 drv->ssid, drv->ssid_len);
1731 ret = snprintf(pos, end - pos, " ");
1732 if (ret < 0 || ret >= end - pos)
1733 return;
1734 pos += ret;
1735 pos += wpa_snprintf_hex(pos, end - pos, drv->assoc_wpa_ie,
1736 drv->assoc_wpa_ie_len);
1737
1738 if (drv->privacy) {
1739 ret = snprintf(pos, end - pos, " PRIVACY");
1740 if (ret < 0 || ret >= end - pos)
1741 return;
1742 pos += ret;
1743 }
1744
1745 ret = snprintf(pos, end - pos, " IBSS");
1746 if (ret < 0 || ret >= end - pos)
1747 return;
1748 pos += ret;
1749
1750 sendto(drv->test_socket, buf, pos - buf, 0,
1751 (struct sockaddr *) from, fromlen);
1752 }
1753
1754
1755 static void wpa_driver_test_receive_unix(int sock, void *eloop_ctx,
1756 void *sock_ctx)
1757 {
1758 struct wpa_driver_test_data *drv = eloop_ctx;
1759 char *buf;
1760 int res;
1761 struct sockaddr_storage from;
1762 socklen_t fromlen = sizeof(from);
1763 const size_t buflen = 2000;
1764
1765 buf = os_malloc(buflen);
1766 if (buf == NULL)
1767 return;
1768 res = recvfrom(sock, buf, buflen - 1, 0,
1769 (struct sockaddr *) &from, &fromlen);
1770 if (res < 0) {
1771 perror("recvfrom(test_socket)");
1772 os_free(buf);
1773 return;
1774 }
1775 buf[res] = '\0';
1776
1777 wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
1778
1779 if (os_strncmp(buf, "SCANRESP ", 9) == 0) {
1780 wpa_driver_test_scanresp(drv, (struct sockaddr *) &from,
1781 fromlen, buf + 9);
1782 } else if (os_strncmp(buf, "ASSOCRESP ", 10) == 0) {
1783 wpa_driver_test_assocresp(drv, (struct sockaddr *) &from,
1784 fromlen, buf + 10);
1785 } else if (os_strcmp(buf, "DISASSOC") == 0) {
1786 wpa_driver_test_disassoc(drv, (struct sockaddr *) &from,
1787 fromlen);
1788 } else if (os_strcmp(buf, "DEAUTH") == 0) {
1789 wpa_driver_test_disassoc(drv, (struct sockaddr *) &from,
1790 fromlen);
1791 } else if (os_strncmp(buf, "EAPOL ", 6) == 0) {
1792 wpa_driver_test_eapol(drv, (struct sockaddr *) &from, fromlen,
1793 (const u8 *) buf + 6, res - 6);
1794 } else if (os_strncmp(buf, "MLME ", 5) == 0) {
1795 wpa_driver_test_mlme(drv, (struct sockaddr *) &from, fromlen,
1796 (const u8 *) buf + 5, res - 5);
1797 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
1798 wpa_driver_test_scan_cmd(drv, (struct sockaddr *) &from,
1799 fromlen,
1800 (const u8 *) buf + 5, res - 5);
1801 } else {
1802 wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
1803 (u8 *) buf, res);
1804 }
1805 os_free(buf);
1806 }
1807
1808
1809 static void * wpa_driver_test_init2(void *ctx, const char *ifname,
1810 void *global_priv)
1811 {
1812 struct wpa_driver_test_data *drv;
1813
1814 drv = os_zalloc(sizeof(*drv));
1815 if (drv == NULL)
1816 return NULL;
1817 drv->global = global_priv;
1818 drv->ctx = ctx;
1819 drv->test_socket = -1;
1820
1821 /* Set dummy BSSID and SSID for testing. */
1822 drv->bssid[0] = 0x02;
1823 drv->bssid[1] = 0x00;
1824 drv->bssid[2] = 0x00;
1825 drv->bssid[3] = 0x00;
1826 drv->bssid[4] = 0x00;
1827 drv->bssid[5] = 0x01;
1828 os_memcpy(drv->ssid, "test", 5);
1829 drv->ssid_len = 4;
1830
1831 /* Generate a MAC address to help testing with multiple STAs */
1832 drv->own_addr[0] = 0x02; /* locally administered */
1833 sha1_prf((const u8 *) ifname, os_strlen(ifname),
1834 "wpa_supplicant test mac addr generation",
1835 NULL, 0, drv->own_addr + 1, ETH_ALEN - 1);
1836 eloop_register_timeout(1, 0, wpa_driver_test_poll, drv, NULL);
1837
1838 return drv;
1839 }
1840
1841
1842 static void wpa_driver_test_close_test_socket(struct wpa_driver_test_data *drv)
1843 {
1844 if (drv->test_socket >= 0) {
1845 eloop_unregister_read_sock(drv->test_socket);
1846 close(drv->test_socket);
1847 drv->test_socket = -1;
1848 }
1849
1850 if (drv->own_socket_path) {
1851 unlink(drv->own_socket_path);
1852 os_free(drv->own_socket_path);
1853 drv->own_socket_path = NULL;
1854 }
1855 }
1856
1857
1858 static void wpa_driver_test_deinit(void *priv)
1859 {
1860 struct wpa_driver_test_data *drv = priv;
1861 int i;
1862 wpa_driver_test_close_test_socket(drv);
1863 eloop_cancel_timeout(wpa_driver_test_scan_timeout, drv, drv->ctx);
1864 eloop_cancel_timeout(wpa_driver_test_poll, drv, NULL);
1865 os_free(drv->test_dir);
1866 for (i = 0; i < MAX_SCAN_RESULTS; i++)
1867 os_free(drv->scanres[i]);
1868 os_free(drv->probe_req_ie);
1869 os_free(drv);
1870 }
1871
1872
1873 static int wpa_driver_test_attach(struct wpa_driver_test_data *drv,
1874 const char *dir)
1875 {
1876 #ifdef DRIVER_TEST_UNIX
1877 static unsigned int counter = 0;
1878 struct sockaddr_un addr;
1879 size_t len;
1880
1881 os_free(drv->own_socket_path);
1882 if (dir) {
1883 len = os_strlen(dir) + 30;
1884 drv->own_socket_path = os_malloc(len);
1885 if (drv->own_socket_path == NULL)
1886 return -1;
1887 os_snprintf(drv->own_socket_path, len, "%s/STA-" MACSTR,
1888 dir, MAC2STR(drv->own_addr));
1889 } else {
1890 drv->own_socket_path = os_malloc(100);
1891 if (drv->own_socket_path == NULL)
1892 return -1;
1893 os_snprintf(drv->own_socket_path, 100,
1894 "/tmp/wpa_supplicant_test-%d-%d",
1895 getpid(), counter++);
1896 }
1897
1898 drv->test_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
1899 if (drv->test_socket < 0) {
1900 perror("socket(PF_UNIX)");
1901 os_free(drv->own_socket_path);
1902 drv->own_socket_path = NULL;
1903 return -1;
1904 }
1905
1906 os_memset(&addr, 0, sizeof(addr));
1907 addr.sun_family = AF_UNIX;
1908 os_strlcpy(addr.sun_path, drv->own_socket_path, sizeof(addr.sun_path));
1909 if (bind(drv->test_socket, (struct sockaddr *) &addr,
1910 sizeof(addr)) < 0) {
1911 perror("bind(PF_UNIX)");
1912 close(drv->test_socket);
1913 unlink(drv->own_socket_path);
1914 os_free(drv->own_socket_path);
1915 drv->own_socket_path = NULL;
1916 return -1;
1917 }
1918
1919 eloop_register_read_sock(drv->test_socket,
1920 wpa_driver_test_receive_unix, drv, NULL);
1921
1922 return 0;
1923 #else /* DRIVER_TEST_UNIX */
1924 return -1;
1925 #endif /* DRIVER_TEST_UNIX */
1926 }
1927
1928
1929 static int wpa_driver_test_attach_udp(struct wpa_driver_test_data *drv,
1930 char *dst)
1931 {
1932 char *pos;
1933
1934 pos = os_strchr(dst, ':');
1935 if (pos == NULL)
1936 return -1;
1937 *pos++ = '\0';
1938 wpa_printf(MSG_DEBUG, "%s: addr=%s port=%s", __func__, dst, pos);
1939
1940 drv->test_socket = socket(PF_INET, SOCK_DGRAM, 0);
1941 if (drv->test_socket < 0) {
1942 perror("socket(PF_INET)");
1943 return -1;
1944 }
1945
1946 os_memset(&drv->hostapd_addr_udp, 0, sizeof(drv->hostapd_addr_udp));
1947 drv->hostapd_addr_udp.sin_family = AF_INET;
1948 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
1949 {
1950 int a[4];
1951 u8 *pos;
1952 sscanf(dst, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
1953 pos = (u8 *) &drv->hostapd_addr_udp.sin_addr;
1954 *pos++ = a[0];
1955 *pos++ = a[1];
1956 *pos++ = a[2];
1957 *pos++ = a[3];
1958 }
1959 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
1960 inet_aton(dst, &drv->hostapd_addr_udp.sin_addr);
1961 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
1962 drv->hostapd_addr_udp.sin_port = htons(atoi(pos));
1963
1964 drv->hostapd_addr_udp_set = 1;
1965
1966 eloop_register_read_sock(drv->test_socket,
1967 wpa_driver_test_receive_unix, drv, NULL);
1968
1969 return 0;
1970 }
1971
1972
1973 static int wpa_driver_test_set_param(void *priv, const char *param)
1974 {
1975 struct wpa_driver_test_data *drv = priv;
1976 const char *pos;
1977
1978 wpa_printf(MSG_DEBUG, "%s: param='%s'", __func__, param);
1979 if (param == NULL)
1980 return 0;
1981
1982 wpa_driver_test_close_test_socket(drv);
1983
1984 #ifdef DRIVER_TEST_UNIX
1985 pos = os_strstr(param, "test_socket=");
1986 if (pos) {
1987 const char *pos2;
1988 size_t len;
1989
1990 pos += 12;
1991 pos2 = os_strchr(pos, ' ');
1992 if (pos2)
1993 len = pos2 - pos;
1994 else
1995 len = os_strlen(pos);
1996 if (len > sizeof(drv->hostapd_addr.sun_path))
1997 return -1;
1998 os_memset(&drv->hostapd_addr, 0, sizeof(drv->hostapd_addr));
1999 drv->hostapd_addr.sun_family = AF_UNIX;
2000 os_memcpy(drv->hostapd_addr.sun_path, pos, len);
2001 drv->hostapd_addr_set = 1;
2002 }
2003 #endif /* DRIVER_TEST_UNIX */
2004
2005 pos = os_strstr(param, "test_dir=");
2006 if (pos) {
2007 char *end;
2008 os_free(drv->test_dir);
2009 drv->test_dir = os_strdup(pos + 9);
2010 if (drv->test_dir == NULL)
2011 return -1;
2012 end = os_strchr(drv->test_dir, ' ');
2013 if (end)
2014 *end = '\0';
2015 if (wpa_driver_test_attach(drv, drv->test_dir))
2016 return -1;
2017 } else {
2018 pos = os_strstr(param, "test_udp=");
2019 if (pos) {
2020 char *dst, *epos;
2021 dst = os_strdup(pos + 9);
2022 if (dst == NULL)
2023 return -1;
2024 epos = os_strchr(dst, ' ');
2025 if (epos)
2026 *epos = '\0';
2027 if (wpa_driver_test_attach_udp(drv, dst))
2028 return -1;
2029 os_free(dst);
2030 } else if (wpa_driver_test_attach(drv, NULL))
2031 return -1;
2032 }
2033
2034 if (os_strstr(param, "use_associnfo=1")) {
2035 wpa_printf(MSG_DEBUG, "test_driver: Use AssocInfo events");
2036 drv->use_associnfo = 1;
2037 }
2038
2039 #ifdef CONFIG_CLIENT_MLME
2040 if (os_strstr(param, "use_mlme=1")) {
2041 wpa_printf(MSG_DEBUG, "test_driver: Use internal MLME");
2042 drv->use_mlme = 1;
2043 }
2044 #endif /* CONFIG_CLIENT_MLME */
2045
2046 return 0;
2047 }
2048
2049
2050 static const u8 * wpa_driver_test_get_mac_addr(void *priv)
2051 {
2052 struct wpa_driver_test_data *drv = priv;
2053 wpa_printf(MSG_DEBUG, "%s", __func__);
2054 return drv->own_addr;
2055 }
2056
2057
2058 static int wpa_driver_test_send_eapol(void *priv, const u8 *dest, u16 proto,
2059 const u8 *data, size_t data_len)
2060 {
2061 struct wpa_driver_test_data *drv = priv;
2062 char *msg;
2063 size_t msg_len;
2064 struct l2_ethhdr eth;
2065 struct sockaddr *addr;
2066 socklen_t alen;
2067 #ifdef DRIVER_TEST_UNIX
2068 struct sockaddr_un addr_un;
2069 #endif /* DRIVER_TEST_UNIX */
2070
2071 wpa_hexdump(MSG_MSGDUMP, "test_send_eapol TX frame", data, data_len);
2072
2073 os_memset(&eth, 0, sizeof(eth));
2074 os_memcpy(eth.h_dest, dest, ETH_ALEN);
2075 os_memcpy(eth.h_source, drv->own_addr, ETH_ALEN);
2076 eth.h_proto = host_to_be16(proto);
2077
2078 msg_len = 6 + sizeof(eth) + data_len;
2079 msg = os_malloc(msg_len);
2080 if (msg == NULL)
2081 return -1;
2082 os_memcpy(msg, "EAPOL ", 6);
2083 os_memcpy(msg + 6, &eth, sizeof(eth));
2084 os_memcpy(msg + 6 + sizeof(eth), data, data_len);
2085
2086 if (os_memcmp(dest, drv->bssid, ETH_ALEN) == 0 ||
2087 drv->test_dir == NULL) {
2088 if (drv->hostapd_addr_udp_set) {
2089 addr = (struct sockaddr *) &drv->hostapd_addr_udp;
2090 alen = sizeof(drv->hostapd_addr_udp);
2091 } else {
2092 #ifdef DRIVER_TEST_UNIX
2093 addr = (struct sockaddr *) &drv->hostapd_addr;
2094 alen = sizeof(drv->hostapd_addr);
2095 #else /* DRIVER_TEST_UNIX */
2096 os_free(msg);
2097 return -1;
2098 #endif /* DRIVER_TEST_UNIX */
2099 }
2100 } else {
2101 #ifdef DRIVER_TEST_UNIX
2102 struct stat st;
2103 os_memset(&addr_un, 0, sizeof(addr_un));
2104 addr_un.sun_family = AF_UNIX;
2105 os_snprintf(addr_un.sun_path, sizeof(addr_un.sun_path),
2106 "%s/STA-" MACSTR, drv->test_dir, MAC2STR(dest));
2107 if (stat(addr_un.sun_path, &st) < 0) {
2108 os_snprintf(addr_un.sun_path, sizeof(addr_un.sun_path),
2109 "%s/AP-" MACSTR,
2110 drv->test_dir, MAC2STR(dest));
2111 }
2112 addr = (struct sockaddr *) &addr_un;
2113 alen = sizeof(addr_un);
2114 #else /* DRIVER_TEST_UNIX */
2115 os_free(msg);
2116 return -1;
2117 #endif /* DRIVER_TEST_UNIX */
2118 }
2119
2120 if (sendto(drv->test_socket, msg, msg_len, 0, addr, alen) < 0) {
2121 perror("sendmsg(test_socket)");
2122 os_free(msg);
2123 return -1;
2124 }
2125
2126 os_free(msg);
2127 return 0;
2128 }
2129
2130
2131 static int wpa_driver_test_get_capa(void *priv, struct wpa_driver_capa *capa)
2132 {
2133 struct wpa_driver_test_data *drv = priv;
2134 os_memset(capa, 0, sizeof(*capa));
2135 capa->key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2136 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2137 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2138 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK |
2139 WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE |
2140 WPA_DRIVER_CAPA_KEY_MGMT_FT |
2141 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2142 capa->enc = WPA_DRIVER_CAPA_ENC_WEP40 |
2143 WPA_DRIVER_CAPA_ENC_WEP104 |
2144 WPA_DRIVER_CAPA_ENC_TKIP |
2145 WPA_DRIVER_CAPA_ENC_CCMP;
2146 capa->auth = WPA_DRIVER_AUTH_OPEN |
2147 WPA_DRIVER_AUTH_SHARED |
2148 WPA_DRIVER_AUTH_LEAP;
2149 if (drv->use_mlme)
2150 capa->flags |= WPA_DRIVER_FLAGS_USER_SPACE_MLME;
2151 capa->max_scan_ssids = 2;
2152
2153 return 0;
2154 }
2155
2156
2157 static int wpa_driver_test_mlme_setprotection(void *priv, const u8 *addr,
2158 int protect_type,
2159 int key_type)
2160 {
2161 wpa_printf(MSG_DEBUG, "%s: protect_type=%d key_type=%d",
2162 __func__, protect_type, key_type);
2163
2164 if (addr) {
2165 wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR,
2166 __func__, MAC2STR(addr));
2167 }
2168
2169 return 0;
2170 }
2171
2172
2173 #ifdef CONFIG_CLIENT_MLME
2174 static int wpa_driver_test_set_channel(void *priv, hostapd_hw_mode phymode,
2175 int chan, int freq)
2176 {
2177 wpa_printf(MSG_DEBUG, "%s: phymode=%d chan=%d freq=%d",
2178 __func__, phymode, chan, freq);
2179 return 0;
2180 }
2181
2182
2183 static int wpa_driver_test_send_mlme(void *priv, const u8 *data,
2184 size_t data_len)
2185 {
2186 struct wpa_driver_test_data *drv = priv;
2187 struct msghdr msg;
2188 struct iovec io[2];
2189 struct sockaddr_un addr;
2190 const u8 *dest;
2191 struct dirent *dent;
2192 DIR *dir;
2193
2194 wpa_hexdump(MSG_MSGDUMP, "test_send_mlme", data, data_len);
2195 if (data_len < 10)
2196 return -1;
2197 dest = data + 4;
2198
2199 io[0].iov_base = "MLME ";
2200 io[0].iov_len = 5;
2201 io[1].iov_base = (u8 *) data;
2202 io[1].iov_len = data_len;
2203
2204 os_memset(&msg, 0, sizeof(msg));
2205 msg.msg_iov = io;
2206 msg.msg_iovlen = 2;
2207 if (os_memcmp(dest, drv->bssid, ETH_ALEN) == 0 ||
2208 drv->test_dir == NULL) {
2209 if (drv->hostapd_addr_udp_set) {
2210 msg.msg_name = &drv->hostapd_addr_udp;
2211 msg.msg_namelen = sizeof(drv->hostapd_addr_udp);
2212 } else {
2213 #ifdef DRIVER_TEST_UNIX
2214 msg.msg_name = &drv->hostapd_addr;
2215 msg.msg_namelen = sizeof(drv->hostapd_addr);
2216 #endif /* DRIVER_TEST_UNIX */
2217 }
2218 } else if (os_memcmp(dest, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0)
2219 {
2220 dir = opendir(drv->test_dir);
2221 if (dir == NULL)
2222 return -1;
2223 while ((dent = readdir(dir))) {
2224 #ifdef _DIRENT_HAVE_D_TYPE
2225 /* Skip the file if it is not a socket.
2226 * Also accept DT_UNKNOWN (0) in case
2227 * the C library or underlying file
2228 * system does not support d_type. */
2229 if (dent->d_type != DT_SOCK &&
2230 dent->d_type != DT_UNKNOWN)
2231 continue;
2232 #endif /* _DIRENT_HAVE_D_TYPE */
2233 if (os_strcmp(dent->d_name, ".") == 0 ||
2234 os_strcmp(dent->d_name, "..") == 0)
2235 continue;
2236 wpa_printf(MSG_DEBUG, "%s: Send broadcast MLME to %s",
2237 __func__, dent->d_name);
2238 os_memset(&addr, 0, sizeof(addr));
2239 addr.sun_family = AF_UNIX;
2240 os_snprintf(addr.sun_path, sizeof(addr.sun_path),
2241 "%s/%s", drv->test_dir, dent->d_name);
2242
2243 msg.msg_name = &addr;
2244 msg.msg_namelen = sizeof(addr);
2245
2246 if (sendmsg(drv->test_socket, &msg, 0) < 0)
2247 perror("sendmsg(test_socket)");
2248 }
2249 closedir(dir);
2250 return 0;
2251 } else {
2252 struct stat st;
2253 os_memset(&addr, 0, sizeof(addr));
2254 addr.sun_family = AF_UNIX;
2255 os_snprintf(addr.sun_path, sizeof(addr.sun_path),
2256 "%s/AP-" MACSTR, drv->test_dir, MAC2STR(dest));
2257 if (stat(addr.sun_path, &st) < 0) {
2258 os_snprintf(addr.sun_path, sizeof(addr.sun_path),
2259 "%s/STA-" MACSTR,
2260 drv->test_dir, MAC2STR(dest));
2261 }
2262 msg.msg_name = &addr;
2263 msg.msg_namelen = sizeof(addr);
2264 }
2265
2266 if (sendmsg(drv->test_socket, &msg, 0) < 0) {
2267 perror("sendmsg(test_socket)");
2268 return -1;
2269 }
2270
2271 return 0;
2272 }
2273
2274
2275 static int wpa_driver_test_mlme_add_sta(void *priv, const u8 *addr,
2276 const u8 *supp_rates,
2277 size_t supp_rates_len)
2278 {
2279 wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, MAC2STR(addr));
2280 return 0;
2281 }
2282
2283
2284 static int wpa_driver_test_mlme_remove_sta(void *priv, const u8 *addr)
2285 {
2286 wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, MAC2STR(addr));
2287 return 0;
2288 }
2289
2290
2291 static int wpa_driver_test_set_ssid(void *priv, const u8 *ssid,
2292 size_t ssid_len)
2293 {
2294 wpa_printf(MSG_DEBUG, "%s", __func__);
2295 return 0;
2296 }
2297
2298
2299 static int wpa_driver_test_set_bssid(void *priv, const u8 *bssid)
2300 {
2301 wpa_printf(MSG_DEBUG, "%s: bssid=" MACSTR, __func__, MAC2STR(bssid));
2302 return 0;
2303 }
2304 #endif /* CONFIG_CLIENT_MLME */
2305
2306
2307 static int wpa_driver_test_set_probe_req_ie(void *priv, const u8 *ies,
2308 size_t ies_len)
2309 {
2310 struct wpa_driver_test_data *drv = priv;
2311
2312 os_free(drv->probe_req_ie);
2313 if (ies) {
2314 drv->probe_req_ie = os_malloc(ies_len);
2315 if (drv->probe_req_ie == NULL) {
2316 drv->probe_req_ie_len = 0;
2317 return -1;
2318 }
2319 os_memcpy(drv->probe_req_ie, ies, ies_len);
2320 drv->probe_req_ie_len = ies_len;
2321 } else {
2322 drv->probe_req_ie = NULL;
2323 drv->probe_req_ie_len = 0;
2324 }
2325 return 0;
2326 }
2327
2328
2329 static void * wpa_driver_test_global_init(void)
2330 {
2331 struct wpa_driver_test_global *global;
2332
2333 global = os_zalloc(sizeof(*global));
2334 return global;
2335 }
2336
2337
2338 static void wpa_driver_test_global_deinit(void *priv)
2339 {
2340 struct wpa_driver_test_global *global = priv;
2341 os_free(global);
2342 }
2343
2344
2345 static struct wpa_interface_info *
2346 wpa_driver_test_get_interfaces(void *global_priv)
2347 {
2348 /* struct wpa_driver_test_global *global = priv; */
2349 struct wpa_interface_info *iface;
2350
2351 iface = os_zalloc(sizeof(*iface));
2352 if (iface == NULL)
2353 return iface;
2354 iface->ifname = os_strdup("sta0");
2355 iface->desc = os_strdup("test interface 0");
2356 iface->drv_name = "test";
2357 iface->next = os_zalloc(sizeof(*iface));
2358 if (iface->next) {
2359 iface->next->ifname = os_strdup("sta1");
2360 iface->next->desc = os_strdup("test interface 1");
2361 iface->next->drv_name = "test";
2362 }
2363
2364 return iface;
2365 }
2366
2367 #endif /* HOSTAPD */
2368
2369
2370 #if defined(HOSTAPD) || defined(CONFIG_CLIENT_MLME)
2371 static struct hostapd_hw_modes *
2372 wpa_driver_test_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
2373 {
2374 struct hostapd_hw_modes *modes;
2375
2376 *num_modes = 3;
2377 *flags = 0;
2378 modes = os_zalloc(*num_modes * sizeof(struct hostapd_hw_modes));
2379 if (modes == NULL)
2380 return NULL;
2381 modes[0].mode = HOSTAPD_MODE_IEEE80211G;
2382 modes[0].num_channels = 1;
2383 modes[0].num_rates = 1;
2384 modes[0].channels = os_zalloc(sizeof(struct hostapd_channel_data));
2385 modes[0].rates = os_zalloc(sizeof(struct hostapd_rate_data));
2386 if (modes[0].channels == NULL || modes[0].rates == NULL)
2387 goto fail;
2388 modes[0].channels[0].chan = 1;
2389 modes[0].channels[0].freq = 2412;
2390 modes[0].channels[0].flag = 0;
2391 modes[0].rates[0].rate = 10;
2392 modes[0].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
2393 HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
2394
2395 modes[1].mode = HOSTAPD_MODE_IEEE80211B;
2396 modes[1].num_channels = 1;
2397 modes[1].num_rates = 1;
2398 modes[1].channels = os_zalloc(sizeof(struct hostapd_channel_data));
2399 modes[1].rates = os_zalloc(sizeof(struct hostapd_rate_data));
2400 if (modes[1].channels == NULL || modes[1].rates == NULL)
2401 goto fail;
2402 modes[1].channels[0].chan = 1;
2403 modes[1].channels[0].freq = 2412;
2404 modes[1].channels[0].flag = 0;
2405 modes[1].rates[0].rate = 10;
2406 modes[1].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
2407 HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
2408
2409 modes[2].mode = HOSTAPD_MODE_IEEE80211A;
2410 modes[2].num_channels = 1;
2411 modes[2].num_rates = 1;
2412 modes[2].channels = os_zalloc(sizeof(struct hostapd_channel_data));
2413 modes[2].rates = os_zalloc(sizeof(struct hostapd_rate_data));
2414 if (modes[2].channels == NULL || modes[2].rates == NULL)
2415 goto fail;
2416 modes[2].channels[0].chan = 60;
2417 modes[2].channels[0].freq = 5300;
2418 modes[2].channels[0].flag = 0;
2419 modes[2].rates[0].rate = 60;
2420 modes[2].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
2421 HOSTAPD_RATE_MANDATORY;
2422
2423 return modes;
2424
2425 fail:
2426 if (modes) {
2427 size_t i;
2428 for (i = 0; i < *num_modes; i++) {
2429 os_free(modes[i].channels);
2430 os_free(modes[i].rates);
2431 }
2432 os_free(modes);
2433 }
2434 return NULL;
2435 }
2436 #endif /* HOSTAPD || CONFIG_CLIENT_MLME */
2437
2438
2439 const struct wpa_driver_ops wpa_driver_test_ops = {
2440 "test",
2441 "wpa_supplicant test driver",
2442 #ifdef HOSTAPD
2443 .hapd_init = test_driver_init,
2444 .hapd_deinit = test_driver_deinit,
2445 .hapd_send_eapol = test_driver_send_eapol,
2446 .send_mlme = wpa_driver_test_send_mlme,
2447 .set_generic_elem = test_driver_set_generic_elem,
2448 .sta_deauth = test_driver_sta_deauth,
2449 .sta_disassoc = test_driver_sta_disassoc,
2450 .get_hw_feature_data = wpa_driver_test_get_hw_feature_data,
2451 .bss_add = test_driver_bss_add,
2452 .bss_remove = test_driver_bss_remove,
2453 .if_add = test_driver_if_add,
2454 .if_update = test_driver_if_update,
2455 .if_remove = test_driver_if_remove,
2456 .valid_bss_mask = test_driver_valid_bss_mask,
2457 .hapd_set_ssid = test_driver_set_ssid,
2458 .set_privacy = test_driver_set_privacy,
2459 .hapd_set_key = test_driver_set_key,
2460 .set_sta_vlan = test_driver_set_sta_vlan,
2461 .sta_add = test_driver_sta_add,
2462 .send_ether = test_driver_send_ether,
2463 .set_wps_beacon_ie = test_driver_set_wps_beacon_ie,
2464 .set_wps_probe_resp_ie = test_driver_set_wps_probe_resp_ie,
2465 #else /* HOSTAPD */
2466 wpa_driver_test_get_bssid,
2467 wpa_driver_test_get_ssid,
2468 wpa_driver_test_set_wpa,
2469 wpa_driver_test_set_key,
2470 NULL /* init */,
2471 wpa_driver_test_deinit,
2472 wpa_driver_test_set_param,
2473 NULL /* set_countermeasures */,
2474 NULL /* set_drop_unencrypted */,
2475 NULL /* scan */,
2476 NULL /* get_scan_results */,
2477 wpa_driver_test_deauthenticate,
2478 wpa_driver_test_disassociate,
2479 wpa_driver_test_associate,
2480 NULL /* set_auth_alg */,
2481 NULL /* add_pmkid */,
2482 NULL /* remove_pmkid */,
2483 NULL /* flush_pmkid */,
2484 wpa_driver_test_get_capa,
2485 NULL /* poll */,
2486 NULL /* get_ifname */,
2487 wpa_driver_test_get_mac_addr,
2488 wpa_driver_test_send_eapol,
2489 NULL /* set_operstate */,
2490 wpa_driver_test_mlme_setprotection,
2491 #ifdef CONFIG_CLIENT_MLME
2492 wpa_driver_test_get_hw_feature_data,
2493 wpa_driver_test_set_channel,
2494 wpa_driver_test_set_ssid,
2495 wpa_driver_test_set_bssid,
2496 wpa_driver_test_send_mlme,
2497 wpa_driver_test_mlme_add_sta,
2498 wpa_driver_test_mlme_remove_sta,
2499 #else /* CONFIG_CLIENT_MLME */
2500 NULL /* get_hw_feature_data */,
2501 NULL /* set_channel */,
2502 NULL /* set_ssid */,
2503 NULL /* set_bssid */,
2504 NULL /* send_mlme */,
2505 NULL /* mlme_add_sta */,
2506 NULL /* mlme_remove_sta */,
2507 #endif /* CONFIG_CLIENT_MLME */
2508 NULL /* update_ft_ies */,
2509 NULL /* send_ft_action */,
2510 wpa_driver_test_get_scan_results2,
2511 wpa_driver_test_set_probe_req_ie,
2512 NULL /* set_mode */,
2513 NULL /* set_country */,
2514 wpa_driver_test_global_init,
2515 wpa_driver_test_global_deinit,
2516 wpa_driver_test_init2,
2517 wpa_driver_test_get_interfaces,
2518 wpa_driver_test_scan,
2519 NULL /* authenticate */,
2520 NULL /* set_beacon */,
2521 NULL /* set_beacon_int */,
2522 NULL /* hapd_init */,
2523 NULL /* hapd_deinit */,
2524 NULL /* set_ieee8021x */,
2525 NULL /* set_privacy */,
2526 NULL /* hapd_set_key */,
2527 NULL /* get_seqnum */,
2528 NULL /* get_seqnum_igtk */,
2529 NULL /* flush */,
2530 NULL /* set_generic_elem */,
2531 NULL /* read_sta_data */,
2532 NULL /* hapd_send_eapol */,
2533 NULL /* sta_deauth */,
2534 NULL /* sta_disassoc */,
2535 NULL /* sta_remove */,
2536 NULL /* hapd_get_ssid */,
2537 NULL /* hapd_set_ssid */,
2538 NULL /* hapd_set_countermeasures */,
2539 NULL /* sta_add */,
2540 NULL /* get_inact_sec */,
2541 NULL /* sta_clear_stats */,
2542 NULL /* set_freq */,
2543 NULL /* set_rts */,
2544 NULL /* set_frag */,
2545 NULL /* set_retry */,
2546 NULL /* sta_set_flags */,
2547 NULL /* set_rate_sets */,
2548 NULL /* set_ieee80211d */,
2549 NULL /* hapd_set_beacon */,
2550 NULL /* set_internal_bridge */,
2551 NULL /* set_broadcast_ssid */,
2552 NULL /* set_cts_protect */,
2553 NULL /* set_preamble */,
2554 NULL /* set_short_slot_time */,
2555 NULL /* set_tx_queue_params */,
2556 NULL /* bss_add */,
2557 NULL /* bss_remove */,
2558 NULL /* valid_bss_mask */,
2559 NULL /* passive_scan */,
2560 NULL /* if_add */,
2561 NULL /* if_update */,
2562 NULL /* if_remove */,
2563 NULL /* set_sta_vlan */,
2564 NULL /* commit */,
2565 NULL /* send_ether */,
2566 NULL /* set_radius_acl_auth */,
2567 NULL /* set_radius_acl_expire */,
2568 NULL /* set_ht_params */,
2569 NULL /* set_wps_beacon_ie */,
2570 NULL /* set_wps_probe_resp_ie */,
2571 NULL /* get_neighbor_bss */
2572 #endif /* HOSTAPD */
2573 };