]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/drivers/driver_test.c
Provide own_addr buffer in hapd_init() parameters
[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 *own_addr,
817 const u8 *addr, int reason)
818 {
819 struct test_driver_data *drv = priv;
820 struct test_client_socket *cli;
821
822 if (drv->test_socket < 0)
823 return -1;
824
825 cli = drv->cli;
826 while (cli) {
827 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
828 break;
829 cli = cli->next;
830 }
831
832 if (!cli)
833 return -1;
834
835 return sendto(drv->test_socket, "DEAUTH", 6, 0,
836 (struct sockaddr *) &cli->un, cli->unlen);
837 }
838
839
840 static int test_driver_sta_disassoc(void *priv, const u8 *own_addr,
841 const u8 *addr, int reason)
842 {
843 struct test_driver_data *drv = priv;
844 struct test_client_socket *cli;
845
846 if (drv->test_socket < 0)
847 return -1;
848
849 cli = drv->cli;
850 while (cli) {
851 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
852 break;
853 cli = cli->next;
854 }
855
856 if (!cli)
857 return -1;
858
859 return sendto(drv->test_socket, "DISASSOC", 8, 0,
860 (struct sockaddr *) &cli->un, cli->unlen);
861 }
862
863
864 static int test_driver_bss_add(void *priv, const char *ifname, const u8 *bssid)
865 {
866 struct test_driver_data *drv = priv;
867 struct test_driver_bss *bss;
868
869 wpa_printf(MSG_DEBUG, "%s(ifname=%s bssid=" MACSTR ")",
870 __func__, ifname, MAC2STR(bssid));
871
872 bss = os_zalloc(sizeof(*bss));
873 if (bss == NULL)
874 return -1;
875
876 os_strlcpy(bss->ifname, ifname, IFNAMSIZ);
877 memcpy(bss->bssid, bssid, ETH_ALEN);
878
879 bss->next = drv->bss;
880 drv->bss = bss;
881
882 return 0;
883 }
884
885
886 static int test_driver_bss_remove(void *priv, const char *ifname)
887 {
888 struct test_driver_data *drv = priv;
889 struct test_driver_bss *bss, *prev;
890 struct test_client_socket *cli, *prev_c;
891
892 wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
893
894 for (prev = NULL, bss = drv->bss; bss; prev = bss, bss = bss->next) {
895 if (strcmp(bss->ifname, ifname) != 0)
896 continue;
897
898 if (prev)
899 prev->next = bss->next;
900 else
901 drv->bss = bss->next;
902
903 for (prev_c = NULL, cli = drv->cli; cli;
904 prev_c = cli, cli = cli->next) {
905 if (cli->bss != bss)
906 continue;
907 if (prev_c)
908 prev_c->next = cli->next;
909 else
910 drv->cli = cli->next;
911 free(cli);
912 break;
913 }
914
915 test_driver_free_bss(bss);
916 return 0;
917 }
918
919 return -1;
920 }
921
922
923 static int test_driver_if_add(const char *iface, void *priv,
924 enum hostapd_driver_if_type type, char *ifname,
925 const u8 *addr)
926 {
927 wpa_printf(MSG_DEBUG, "%s(iface=%s type=%d ifname=%s)",
928 __func__, iface, type, ifname);
929 return 0;
930 }
931
932
933 static int test_driver_if_update(void *priv, enum hostapd_driver_if_type type,
934 char *ifname, const u8 *addr)
935 {
936 wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
937 return 0;
938 }
939
940
941 static int test_driver_if_remove(void *priv, enum hostapd_driver_if_type type,
942 const char *ifname, const u8 *addr)
943 {
944 wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
945 return 0;
946 }
947
948
949 static int test_driver_valid_bss_mask(void *priv, const u8 *addr,
950 const u8 *mask)
951 {
952 return 0;
953 }
954
955
956 static int test_driver_set_ssid(const char *ifname, void *priv, const u8 *buf,
957 int len)
958 {
959 struct test_driver_data *drv = priv;
960 struct test_driver_bss *bss;
961
962 wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
963 wpa_hexdump_ascii(MSG_DEBUG, "test_driver_set_ssid: SSID", buf, len);
964
965 for (bss = drv->bss; bss; bss = bss->next) {
966 if (strcmp(bss->ifname, ifname) != 0)
967 continue;
968
969 if (len < 0 || (size_t) len > sizeof(bss->ssid))
970 return -1;
971
972 memcpy(bss->ssid, buf, len);
973 bss->ssid_len = len;
974
975 return 0;
976 }
977
978 return -1;
979 }
980
981
982 static int test_driver_set_privacy(const char *ifname, void *priv, int enabled)
983 {
984 struct test_driver_data *drv = priv;
985 struct test_driver_bss *bss;
986
987 wpa_printf(MSG_DEBUG, "%s(ifname=%s enabled=%d)",
988 __func__, ifname, enabled);
989
990 for (bss = drv->bss; bss; bss = bss->next) {
991 if (strcmp(bss->ifname, ifname) != 0)
992 continue;
993
994 bss->privacy = enabled;
995
996 return 0;
997 }
998
999 return -1;
1000 }
1001
1002
1003 static int test_driver_set_key(const char *iface, void *priv, wpa_alg alg,
1004 const u8 *addr, int key_idx, int set_tx,
1005 const u8 *seq, size_t seq_len,
1006 const u8 *key, size_t key_len)
1007 {
1008 wpa_printf(MSG_DEBUG, "%s(iface=%s alg=%d idx=%d set_tx=%d)",
1009 __func__, iface, alg, key_idx, set_tx);
1010 if (addr)
1011 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
1012 if (key)
1013 wpa_hexdump_key(MSG_DEBUG, " key", key, key_len);
1014 return 0;
1015 }
1016
1017
1018 static int test_driver_set_sta_vlan(void *priv, const u8 *addr,
1019 const char *ifname, int vlan_id)
1020 {
1021 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " ifname=%s vlan_id=%d)",
1022 __func__, MAC2STR(addr), ifname, vlan_id);
1023 return 0;
1024 }
1025
1026
1027 static int test_driver_sta_add(const char *ifname, void *priv,
1028 struct hostapd_sta_add_params *params)
1029 {
1030 struct test_driver_data *drv = priv;
1031 struct test_client_socket *cli;
1032 struct test_driver_bss *bss;
1033
1034 wpa_printf(MSG_DEBUG, "%s(ifname=%s addr=" MACSTR " aid=%d "
1035 "capability=0x%x flags=0x%x listen_interval=%d)",
1036 __func__, ifname, MAC2STR(params->addr), params->aid,
1037 params->capability, params->flags,
1038 params->listen_interval);
1039 wpa_hexdump(MSG_DEBUG, "test_driver_sta_add - supp_rates",
1040 params->supp_rates, params->supp_rates_len);
1041
1042 cli = drv->cli;
1043 while (cli) {
1044 if (os_memcmp(cli->addr, params->addr, ETH_ALEN) == 0)
1045 break;
1046 cli = cli->next;
1047 }
1048 if (!cli) {
1049 wpa_printf(MSG_DEBUG, "%s: no matching client entry",
1050 __func__);
1051 return -1;
1052 }
1053
1054 for (bss = drv->bss; bss; bss = bss->next) {
1055 if (strcmp(ifname, bss->ifname) == 0)
1056 break;
1057 }
1058 if (bss == NULL) {
1059 wpa_printf(MSG_DEBUG, "%s: No matching interface found from "
1060 "configured BSSes", __func__);
1061 return -1;
1062 }
1063
1064 cli->bss = bss;
1065
1066 return 0;
1067 }
1068
1069
1070 static void * test_driver_init(struct hostapd_data *hapd,
1071 struct wpa_init_params *params)
1072 {
1073 struct test_driver_data *drv;
1074 struct sockaddr_un addr_un;
1075 struct sockaddr_in addr_in;
1076 struct sockaddr *addr;
1077 socklen_t alen;
1078
1079 drv = os_zalloc(sizeof(struct test_driver_data));
1080 if (drv == NULL) {
1081 printf("Could not allocate memory for test driver data\n");
1082 return NULL;
1083 }
1084 drv->bss = os_zalloc(sizeof(*drv->bss));
1085 if (drv->bss == NULL) {
1086 printf("Could not allocate memory for test driver BSS data\n");
1087 free(drv);
1088 return NULL;
1089 }
1090
1091 drv->hapd = hapd;
1092
1093 /* Generate a MAC address to help testing with multiple APs */
1094 params->own_addr[0] = 0x02; /* locally administered */
1095 sha1_prf((const u8 *) params->ifname, strlen(params->ifname),
1096 "hostapd test bssid generation",
1097 params->ssid, params->ssid_len,
1098 params->own_addr + 1, ETH_ALEN - 1);
1099
1100 os_strlcpy(drv->bss->ifname, params->ifname, IFNAMSIZ);
1101 memcpy(drv->bss->bssid, params->own_addr, ETH_ALEN);
1102
1103 if (params->test_socket) {
1104 if (os_strlen(params->test_socket) >=
1105 sizeof(addr_un.sun_path)) {
1106 printf("Too long test_socket path\n");
1107 test_driver_free_priv(drv);
1108 return NULL;
1109 }
1110 if (strncmp(params->test_socket, "DIR:", 4) == 0) {
1111 size_t len = strlen(params->test_socket) + 30;
1112 drv->socket_dir = strdup(params->test_socket + 4);
1113 drv->own_socket_path = malloc(len);
1114 if (drv->own_socket_path) {
1115 snprintf(drv->own_socket_path, len,
1116 "%s/AP-" MACSTR,
1117 params->test_socket + 4,
1118 MAC2STR(params->own_addr));
1119 }
1120 } else if (strncmp(params->test_socket, "UDP:", 4) == 0) {
1121 drv->udp_port = atoi(params->test_socket + 4);
1122 } else {
1123 drv->own_socket_path = strdup(params->test_socket);
1124 }
1125 if (drv->own_socket_path == NULL && drv->udp_port == 0) {
1126 test_driver_free_priv(drv);
1127 return NULL;
1128 }
1129
1130 drv->test_socket = socket(drv->udp_port ? PF_INET : PF_UNIX,
1131 SOCK_DGRAM, 0);
1132 if (drv->test_socket < 0) {
1133 perror("socket");
1134 test_driver_free_priv(drv);
1135 return NULL;
1136 }
1137
1138 if (drv->udp_port) {
1139 os_memset(&addr_in, 0, sizeof(addr_in));
1140 addr_in.sin_family = AF_INET;
1141 addr_in.sin_port = htons(drv->udp_port);
1142 addr = (struct sockaddr *) &addr_in;
1143 alen = sizeof(addr_in);
1144 } else {
1145 os_memset(&addr_un, 0, sizeof(addr_un));
1146 addr_un.sun_family = AF_UNIX;
1147 os_strlcpy(addr_un.sun_path, drv->own_socket_path,
1148 sizeof(addr_un.sun_path));
1149 addr = (struct sockaddr *) &addr_un;
1150 alen = sizeof(addr_un);
1151 }
1152 if (bind(drv->test_socket, addr, alen) < 0) {
1153 perror("bind(PF_UNIX)");
1154 close(drv->test_socket);
1155 if (drv->own_socket_path)
1156 unlink(drv->own_socket_path);
1157 test_driver_free_priv(drv);
1158 return NULL;
1159 }
1160 eloop_register_read_sock(drv->test_socket,
1161 test_driver_receive_unix, drv, NULL);
1162 } else
1163 drv->test_socket = -1;
1164
1165 return drv;
1166 }
1167
1168
1169 static void test_driver_deinit(void *priv)
1170 {
1171 struct test_driver_data *drv = priv;
1172 struct test_client_socket *cli, *prev;
1173
1174 cli = drv->cli;
1175 while (cli) {
1176 prev = cli;
1177 cli = cli->next;
1178 free(prev);
1179 }
1180
1181 if (drv->test_socket >= 0) {
1182 eloop_unregister_read_sock(drv->test_socket);
1183 close(drv->test_socket);
1184 if (drv->own_socket_path)
1185 unlink(drv->own_socket_path);
1186 }
1187
1188 /* There should be only one BSS remaining at this point. */
1189 if (drv->bss == NULL)
1190 wpa_printf(MSG_ERROR, "%s: drv->bss == NULL", __func__);
1191 else if (drv->bss->next)
1192 wpa_printf(MSG_ERROR, "%s: drv->bss->next != NULL", __func__);
1193
1194 test_driver_free_priv(drv);
1195 }
1196
1197 #else /* HOSTAPD */
1198
1199 static void wpa_driver_test_poll(void *eloop_ctx, void *timeout_ctx)
1200 {
1201 struct wpa_driver_test_data *drv = eloop_ctx;
1202
1203 #ifdef DRIVER_TEST_UNIX
1204 if (drv->associated && drv->hostapd_addr_set) {
1205 struct stat st;
1206 if (stat(drv->hostapd_addr.sun_path, &st) < 0) {
1207 wpa_printf(MSG_DEBUG, "%s: lost connection to AP: %s",
1208 __func__, strerror(errno));
1209 drv->associated = 0;
1210 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1211 }
1212 }
1213 #endif /* DRIVER_TEST_UNIX */
1214
1215 eloop_register_timeout(1, 0, wpa_driver_test_poll, drv, NULL);
1216 }
1217
1218
1219 static int wpa_driver_test_set_wpa(void *priv, int enabled)
1220 {
1221 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
1222 return 0;
1223 }
1224
1225
1226 static void wpa_driver_test_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1227 {
1228 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
1229 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
1230 }
1231
1232
1233 #ifdef DRIVER_TEST_UNIX
1234 static void wpa_driver_scan_dir(struct wpa_driver_test_data *drv,
1235 const char *path)
1236 {
1237 struct dirent *dent;
1238 DIR *dir;
1239 struct sockaddr_un addr;
1240 char cmd[512], *pos, *end;
1241 int ret;
1242
1243 dir = opendir(path);
1244 if (dir == NULL)
1245 return;
1246
1247 end = cmd + sizeof(cmd);
1248 pos = cmd;
1249 ret = os_snprintf(pos, end - pos, "SCAN " MACSTR,
1250 MAC2STR(drv->own_addr));
1251 if (ret >= 0 && ret < end - pos)
1252 pos += ret;
1253 if (drv->probe_req_ie) {
1254 ret = os_snprintf(pos, end - pos, " ");
1255 if (ret >= 0 && ret < end - pos)
1256 pos += ret;
1257 pos += wpa_snprintf_hex(pos, end - pos, drv->probe_req_ie,
1258 drv->probe_req_ie_len);
1259 }
1260 end[-1] = '\0';
1261
1262 while ((dent = readdir(dir))) {
1263 if (os_strncmp(dent->d_name, "AP-", 3) != 0 &&
1264 os_strncmp(dent->d_name, "STA-", 4) != 0)
1265 continue;
1266 if (drv->own_socket_path) {
1267 size_t olen, dlen;
1268 olen = os_strlen(drv->own_socket_path);
1269 dlen = os_strlen(dent->d_name);
1270 if (olen >= dlen &&
1271 os_strcmp(dent->d_name,
1272 drv->own_socket_path + olen - dlen) == 0)
1273 continue;
1274 }
1275 wpa_printf(MSG_DEBUG, "%s: SCAN %s", __func__, dent->d_name);
1276
1277 os_memset(&addr, 0, sizeof(addr));
1278 addr.sun_family = AF_UNIX;
1279 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
1280 path, dent->d_name);
1281
1282 if (sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
1283 (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1284 perror("sendto(test_socket)");
1285 }
1286 }
1287 closedir(dir);
1288 }
1289 #endif /* DRIVER_TEST_UNIX */
1290
1291
1292 static int wpa_driver_test_scan(void *priv,
1293 struct wpa_driver_scan_params *params)
1294 {
1295 struct wpa_driver_test_data *drv = priv;
1296 size_t i;
1297
1298 wpa_printf(MSG_DEBUG, "%s: priv=%p", __func__, priv);
1299 for (i = 0; i < params->num_ssids; i++)
1300 wpa_hexdump(MSG_DEBUG, "Scan SSID",
1301 params->ssids[i].ssid, params->ssids[i].ssid_len);
1302 wpa_hexdump(MSG_DEBUG, "Scan extra IE(s)",
1303 params->extra_ies, params->extra_ies_len);
1304
1305 drv->num_scanres = 0;
1306
1307 #ifdef DRIVER_TEST_UNIX
1308 if (drv->test_socket >= 0 && drv->test_dir)
1309 wpa_driver_scan_dir(drv, drv->test_dir);
1310
1311 if (drv->test_socket >= 0 && drv->hostapd_addr_set &&
1312 sendto(drv->test_socket, "SCAN", 4, 0,
1313 (struct sockaddr *) &drv->hostapd_addr,
1314 sizeof(drv->hostapd_addr)) < 0) {
1315 perror("sendto(test_socket)");
1316 }
1317 #endif /* DRIVER_TEST_UNIX */
1318
1319 if (drv->test_socket >= 0 && drv->hostapd_addr_udp_set &&
1320 sendto(drv->test_socket, "SCAN", 4, 0,
1321 (struct sockaddr *) &drv->hostapd_addr_udp,
1322 sizeof(drv->hostapd_addr_udp)) < 0) {
1323 perror("sendto(test_socket)");
1324 }
1325
1326 eloop_cancel_timeout(wpa_driver_test_scan_timeout, drv, drv->ctx);
1327 eloop_register_timeout(1, 0, wpa_driver_test_scan_timeout, drv,
1328 drv->ctx);
1329 return 0;
1330 }
1331
1332
1333 static struct wpa_scan_results * wpa_driver_test_get_scan_results2(void *priv)
1334 {
1335 struct wpa_driver_test_data *drv = priv;
1336 struct wpa_scan_results *res;
1337 size_t i;
1338
1339 res = os_zalloc(sizeof(*res));
1340 if (res == NULL)
1341 return NULL;
1342
1343 res->res = os_zalloc(drv->num_scanres * sizeof(struct wpa_scan_res *));
1344 if (res->res == NULL) {
1345 os_free(res);
1346 return NULL;
1347 }
1348
1349 for (i = 0; i < drv->num_scanres; i++) {
1350 struct wpa_scan_res *r;
1351 if (drv->scanres[i] == NULL)
1352 continue;
1353 r = os_malloc(sizeof(*r) + drv->scanres[i]->ie_len);
1354 if (r == NULL)
1355 break;
1356 os_memcpy(r, drv->scanres[i],
1357 sizeof(*r) + drv->scanres[i]->ie_len);
1358 res->res[res->num++] = r;
1359 }
1360
1361 return res;
1362 }
1363
1364
1365 static int wpa_driver_test_set_key(void *priv, wpa_alg alg, const u8 *addr,
1366 int key_idx, int set_tx,
1367 const u8 *seq, size_t seq_len,
1368 const u8 *key, size_t key_len)
1369 {
1370 wpa_printf(MSG_DEBUG, "%s: priv=%p alg=%d key_idx=%d set_tx=%d",
1371 __func__, priv, alg, key_idx, set_tx);
1372 if (addr) {
1373 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
1374 }
1375 if (seq) {
1376 wpa_hexdump(MSG_DEBUG, " seq", seq, seq_len);
1377 }
1378 if (key) {
1379 wpa_hexdump(MSG_DEBUG, " key", key, key_len);
1380 }
1381 return 0;
1382 }
1383
1384
1385 static int wpa_driver_test_associate(
1386 void *priv, struct wpa_driver_associate_params *params)
1387 {
1388 struct wpa_driver_test_data *drv = priv;
1389 wpa_printf(MSG_DEBUG, "%s: priv=%p freq=%d pairwise_suite=%d "
1390 "group_suite=%d key_mgmt_suite=%d auth_alg=%d mode=%d",
1391 __func__, priv, params->freq, params->pairwise_suite,
1392 params->group_suite, params->key_mgmt_suite,
1393 params->auth_alg, params->mode);
1394 if (params->bssid) {
1395 wpa_printf(MSG_DEBUG, " bssid=" MACSTR,
1396 MAC2STR(params->bssid));
1397 }
1398 if (params->ssid) {
1399 wpa_hexdump_ascii(MSG_DEBUG, " ssid",
1400 params->ssid, params->ssid_len);
1401 }
1402 if (params->wpa_ie) {
1403 wpa_hexdump(MSG_DEBUG, " wpa_ie",
1404 params->wpa_ie, params->wpa_ie_len);
1405 drv->assoc_wpa_ie_len = params->wpa_ie_len;
1406 if (drv->assoc_wpa_ie_len > sizeof(drv->assoc_wpa_ie))
1407 drv->assoc_wpa_ie_len = sizeof(drv->assoc_wpa_ie);
1408 os_memcpy(drv->assoc_wpa_ie, params->wpa_ie,
1409 drv->assoc_wpa_ie_len);
1410 } else
1411 drv->assoc_wpa_ie_len = 0;
1412
1413 drv->ibss = params->mode == IEEE80211_MODE_IBSS;
1414 drv->privacy = params->key_mgmt_suite &
1415 (WPA_KEY_MGMT_IEEE8021X |
1416 WPA_KEY_MGMT_PSK |
1417 WPA_KEY_MGMT_WPA_NONE |
1418 WPA_KEY_MGMT_FT_IEEE8021X |
1419 WPA_KEY_MGMT_FT_PSK |
1420 WPA_KEY_MGMT_IEEE8021X_SHA256 |
1421 WPA_KEY_MGMT_PSK_SHA256);
1422 if (params->wep_key_len[params->wep_tx_keyidx])
1423 drv->privacy = 1;
1424
1425 #ifdef DRIVER_TEST_UNIX
1426 if (drv->test_dir && params->bssid &&
1427 params->mode != IEEE80211_MODE_IBSS) {
1428 os_memset(&drv->hostapd_addr, 0, sizeof(drv->hostapd_addr));
1429 drv->hostapd_addr.sun_family = AF_UNIX;
1430 os_snprintf(drv->hostapd_addr.sun_path,
1431 sizeof(drv->hostapd_addr.sun_path),
1432 "%s/AP-" MACSTR,
1433 drv->test_dir, MAC2STR(params->bssid));
1434 drv->hostapd_addr_set = 1;
1435 }
1436 #endif /* DRIVER_TEST_UNIX */
1437
1438 if (drv->test_socket >= 0 &&
1439 (drv->hostapd_addr_set || drv->hostapd_addr_udp_set)) {
1440 char cmd[200], *pos, *end;
1441 int ret;
1442 end = cmd + sizeof(cmd);
1443 pos = cmd;
1444 ret = os_snprintf(pos, end - pos, "ASSOC " MACSTR " ",
1445 MAC2STR(drv->own_addr));
1446 if (ret >= 0 && ret < end - pos)
1447 pos += ret;
1448 pos += wpa_snprintf_hex(pos, end - pos, params->ssid,
1449 params->ssid_len);
1450 ret = os_snprintf(pos, end - pos, " ");
1451 if (ret >= 0 && ret < end - pos)
1452 pos += ret;
1453 pos += wpa_snprintf_hex(pos, end - pos, params->wpa_ie,
1454 params->wpa_ie_len);
1455 end[-1] = '\0';
1456 #ifdef DRIVER_TEST_UNIX
1457 if (drv->hostapd_addr_set &&
1458 sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
1459 (struct sockaddr *) &drv->hostapd_addr,
1460 sizeof(drv->hostapd_addr)) < 0) {
1461 perror("sendto(test_socket)");
1462 return -1;
1463 }
1464 #endif /* DRIVER_TEST_UNIX */
1465 if (drv->hostapd_addr_udp_set &&
1466 sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
1467 (struct sockaddr *) &drv->hostapd_addr_udp,
1468 sizeof(drv->hostapd_addr_udp)) < 0) {
1469 perror("sendto(test_socket)");
1470 return -1;
1471 }
1472
1473 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
1474 drv->ssid_len = params->ssid_len;
1475 } else {
1476 drv->associated = 1;
1477 if (params->mode == IEEE80211_MODE_IBSS) {
1478 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
1479 drv->ssid_len = params->ssid_len;
1480 if (params->bssid)
1481 os_memcpy(drv->bssid, params->bssid, ETH_ALEN);
1482 else {
1483 os_get_random(drv->bssid, ETH_ALEN);
1484 drv->bssid[0] &= ~0x01;
1485 drv->bssid[0] |= 0x02;
1486 }
1487 }
1488 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1489 }
1490
1491 return 0;
1492 }
1493
1494
1495 static int wpa_driver_test_get_bssid(void *priv, u8 *bssid)
1496 {
1497 struct wpa_driver_test_data *drv = priv;
1498 os_memcpy(bssid, drv->bssid, ETH_ALEN);
1499 return 0;
1500 }
1501
1502
1503 static int wpa_driver_test_get_ssid(void *priv, u8 *ssid)
1504 {
1505 struct wpa_driver_test_data *drv = priv;
1506 os_memcpy(ssid, drv->ssid, 32);
1507 return drv->ssid_len;
1508 }
1509
1510
1511 static int wpa_driver_test_send_disassoc(struct wpa_driver_test_data *drv)
1512 {
1513 #ifdef DRIVER_TEST_UNIX
1514 if (drv->test_socket >= 0 &&
1515 sendto(drv->test_socket, "DISASSOC", 8, 0,
1516 (struct sockaddr *) &drv->hostapd_addr,
1517 sizeof(drv->hostapd_addr)) < 0) {
1518 perror("sendto(test_socket)");
1519 return -1;
1520 }
1521 #endif /* DRIVER_TEST_UNIX */
1522 if (drv->test_socket >= 0 && drv->hostapd_addr_udp_set &&
1523 sendto(drv->test_socket, "DISASSOC", 8, 0,
1524 (struct sockaddr *) &drv->hostapd_addr_udp,
1525 sizeof(drv->hostapd_addr_udp)) < 0) {
1526 perror("sendto(test_socket)");
1527 return -1;
1528 }
1529 return 0;
1530 }
1531
1532
1533 static int wpa_driver_test_deauthenticate(void *priv, const u8 *addr,
1534 int reason_code)
1535 {
1536 struct wpa_driver_test_data *drv = priv;
1537 wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d",
1538 __func__, MAC2STR(addr), reason_code);
1539 os_memset(drv->bssid, 0, ETH_ALEN);
1540 drv->associated = 0;
1541 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1542 return wpa_driver_test_send_disassoc(drv);
1543 }
1544
1545
1546 static int wpa_driver_test_disassociate(void *priv, const u8 *addr,
1547 int reason_code)
1548 {
1549 struct wpa_driver_test_data *drv = priv;
1550 wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d",
1551 __func__, MAC2STR(addr), reason_code);
1552 os_memset(drv->bssid, 0, ETH_ALEN);
1553 drv->associated = 0;
1554 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1555 return wpa_driver_test_send_disassoc(drv);
1556 }
1557
1558
1559 static void wpa_driver_test_scanresp(struct wpa_driver_test_data *drv,
1560 struct sockaddr *from,
1561 socklen_t fromlen,
1562 const char *data)
1563 {
1564 struct wpa_scan_res *res;
1565 const char *pos, *pos2;
1566 size_t len;
1567 u8 *ie_pos, *ie_start, *ie_end;
1568 #define MAX_IE_LEN 1000
1569
1570 wpa_printf(MSG_DEBUG, "test_driver: SCANRESP %s", data);
1571 if (drv->num_scanres >= MAX_SCAN_RESULTS) {
1572 wpa_printf(MSG_DEBUG, "test_driver: No room for the new scan "
1573 "result");
1574 return;
1575 }
1576
1577 /* SCANRESP BSSID SSID IEs */
1578
1579 res = os_zalloc(sizeof(*res) + MAX_IE_LEN);
1580 if (res == NULL)
1581 return;
1582 ie_start = ie_pos = (u8 *) (res + 1);
1583 ie_end = ie_pos + MAX_IE_LEN;
1584
1585 if (hwaddr_aton(data, res->bssid)) {
1586 wpa_printf(MSG_DEBUG, "test_driver: invalid BSSID in scanres");
1587 os_free(res);
1588 return;
1589 }
1590
1591 pos = data + 17;
1592 while (*pos == ' ')
1593 pos++;
1594 pos2 = os_strchr(pos, ' ');
1595 if (pos2 == NULL) {
1596 wpa_printf(MSG_DEBUG, "test_driver: invalid SSID termination "
1597 "in scanres");
1598 os_free(res);
1599 return;
1600 }
1601 len = (pos2 - pos) / 2;
1602 if (len > 32)
1603 len = 32;
1604 /*
1605 * Generate SSID IE from the SSID field since this IE is not included
1606 * in the main IE field.
1607 */
1608 *ie_pos++ = WLAN_EID_SSID;
1609 *ie_pos++ = len;
1610 if (hexstr2bin(pos, ie_pos, len) < 0) {
1611 wpa_printf(MSG_DEBUG, "test_driver: invalid SSID in scanres");
1612 os_free(res);
1613 return;
1614 }
1615 ie_pos += len;
1616
1617 pos = pos2 + 1;
1618 pos2 = os_strchr(pos, ' ');
1619 if (pos2 == NULL)
1620 len = os_strlen(pos) / 2;
1621 else
1622 len = (pos2 - pos) / 2;
1623 if ((int) len > ie_end - ie_pos)
1624 len = ie_end - ie_pos;
1625 if (hexstr2bin(pos, ie_pos, len) < 0) {
1626 wpa_printf(MSG_DEBUG, "test_driver: invalid IEs in scanres");
1627 os_free(res);
1628 return;
1629 }
1630 ie_pos += len;
1631 res->ie_len = ie_pos - ie_start;
1632
1633 if (pos2) {
1634 pos = pos2 + 1;
1635 while (*pos == ' ')
1636 pos++;
1637 if (os_strstr(pos, "PRIVACY"))
1638 res->caps |= IEEE80211_CAP_PRIVACY;
1639 if (os_strstr(pos, "IBSS"))
1640 res->caps |= IEEE80211_CAP_IBSS;
1641 }
1642
1643 os_free(drv->scanres[drv->num_scanres]);
1644 drv->scanres[drv->num_scanres++] = res;
1645 }
1646
1647
1648 static void wpa_driver_test_assocresp(struct wpa_driver_test_data *drv,
1649 struct sockaddr *from,
1650 socklen_t fromlen,
1651 const char *data)
1652 {
1653 /* ASSOCRESP BSSID <res> */
1654 if (hwaddr_aton(data, drv->bssid)) {
1655 wpa_printf(MSG_DEBUG, "test_driver: invalid BSSID in "
1656 "assocresp");
1657 }
1658 if (drv->use_associnfo) {
1659 union wpa_event_data event;
1660 os_memset(&event, 0, sizeof(event));
1661 event.assoc_info.req_ies = drv->assoc_wpa_ie;
1662 event.assoc_info.req_ies_len = drv->assoc_wpa_ie_len;
1663 wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &event);
1664 }
1665 drv->associated = 1;
1666 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1667 }
1668
1669
1670 static void wpa_driver_test_disassoc(struct wpa_driver_test_data *drv,
1671 struct sockaddr *from,
1672 socklen_t fromlen)
1673 {
1674 drv->associated = 0;
1675 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1676 }
1677
1678
1679 static void wpa_driver_test_eapol(struct wpa_driver_test_data *drv,
1680 struct sockaddr *from,
1681 socklen_t fromlen,
1682 const u8 *data, size_t data_len)
1683 {
1684 const u8 *src = drv->bssid;
1685
1686 if (data_len > 14) {
1687 /* Skip Ethernet header */
1688 src = data + ETH_ALEN;
1689 data += 14;
1690 data_len -= 14;
1691 }
1692 wpa_supplicant_rx_eapol(drv->ctx, src, data, data_len);
1693 }
1694
1695
1696 static void wpa_driver_test_mlme(struct wpa_driver_test_data *drv,
1697 struct sockaddr *from,
1698 socklen_t fromlen,
1699 const u8 *data, size_t data_len)
1700 {
1701 #ifdef CONFIG_CLIENT_MLME
1702 struct ieee80211_rx_status rx_status;
1703 os_memset(&rx_status, 0, sizeof(rx_status));
1704 wpa_supplicant_sta_rx(drv->ctx, data, data_len, &rx_status);
1705 #endif /* CONFIG_CLIENT_MLME */
1706 }
1707
1708
1709 static void wpa_driver_test_scan_cmd(struct wpa_driver_test_data *drv,
1710 struct sockaddr *from,
1711 socklen_t fromlen,
1712 const u8 *data, size_t data_len)
1713 {
1714 char buf[512], *pos, *end;
1715 int ret;
1716
1717 /* data: optional [ STA-addr | ' ' | IEs(hex) ] */
1718
1719 if (!drv->ibss)
1720 return;
1721
1722 pos = buf;
1723 end = buf + sizeof(buf);
1724
1725 /* reply: SCANRESP BSSID SSID IEs */
1726 ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
1727 MAC2STR(drv->bssid));
1728 if (ret < 0 || ret >= end - pos)
1729 return;
1730 pos += ret;
1731 pos += wpa_snprintf_hex(pos, end - pos,
1732 drv->ssid, drv->ssid_len);
1733 ret = snprintf(pos, end - pos, " ");
1734 if (ret < 0 || ret >= end - pos)
1735 return;
1736 pos += ret;
1737 pos += wpa_snprintf_hex(pos, end - pos, drv->assoc_wpa_ie,
1738 drv->assoc_wpa_ie_len);
1739
1740 if (drv->privacy) {
1741 ret = snprintf(pos, end - pos, " PRIVACY");
1742 if (ret < 0 || ret >= end - pos)
1743 return;
1744 pos += ret;
1745 }
1746
1747 ret = snprintf(pos, end - pos, " IBSS");
1748 if (ret < 0 || ret >= end - pos)
1749 return;
1750 pos += ret;
1751
1752 sendto(drv->test_socket, buf, pos - buf, 0,
1753 (struct sockaddr *) from, fromlen);
1754 }
1755
1756
1757 static void wpa_driver_test_receive_unix(int sock, void *eloop_ctx,
1758 void *sock_ctx)
1759 {
1760 struct wpa_driver_test_data *drv = eloop_ctx;
1761 char *buf;
1762 int res;
1763 struct sockaddr_storage from;
1764 socklen_t fromlen = sizeof(from);
1765 const size_t buflen = 2000;
1766
1767 buf = os_malloc(buflen);
1768 if (buf == NULL)
1769 return;
1770 res = recvfrom(sock, buf, buflen - 1, 0,
1771 (struct sockaddr *) &from, &fromlen);
1772 if (res < 0) {
1773 perror("recvfrom(test_socket)");
1774 os_free(buf);
1775 return;
1776 }
1777 buf[res] = '\0';
1778
1779 wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
1780
1781 if (os_strncmp(buf, "SCANRESP ", 9) == 0) {
1782 wpa_driver_test_scanresp(drv, (struct sockaddr *) &from,
1783 fromlen, buf + 9);
1784 } else if (os_strncmp(buf, "ASSOCRESP ", 10) == 0) {
1785 wpa_driver_test_assocresp(drv, (struct sockaddr *) &from,
1786 fromlen, buf + 10);
1787 } else if (os_strcmp(buf, "DISASSOC") == 0) {
1788 wpa_driver_test_disassoc(drv, (struct sockaddr *) &from,
1789 fromlen);
1790 } else if (os_strcmp(buf, "DEAUTH") == 0) {
1791 wpa_driver_test_disassoc(drv, (struct sockaddr *) &from,
1792 fromlen);
1793 } else if (os_strncmp(buf, "EAPOL ", 6) == 0) {
1794 wpa_driver_test_eapol(drv, (struct sockaddr *) &from, fromlen,
1795 (const u8 *) buf + 6, res - 6);
1796 } else if (os_strncmp(buf, "MLME ", 5) == 0) {
1797 wpa_driver_test_mlme(drv, (struct sockaddr *) &from, fromlen,
1798 (const u8 *) buf + 5, res - 5);
1799 } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
1800 wpa_driver_test_scan_cmd(drv, (struct sockaddr *) &from,
1801 fromlen,
1802 (const u8 *) buf + 5, res - 5);
1803 } else {
1804 wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
1805 (u8 *) buf, res);
1806 }
1807 os_free(buf);
1808 }
1809
1810
1811 static void * wpa_driver_test_init2(void *ctx, const char *ifname,
1812 void *global_priv)
1813 {
1814 struct wpa_driver_test_data *drv;
1815
1816 drv = os_zalloc(sizeof(*drv));
1817 if (drv == NULL)
1818 return NULL;
1819 drv->global = global_priv;
1820 drv->ctx = ctx;
1821 drv->test_socket = -1;
1822
1823 /* Set dummy BSSID and SSID for testing. */
1824 drv->bssid[0] = 0x02;
1825 drv->bssid[1] = 0x00;
1826 drv->bssid[2] = 0x00;
1827 drv->bssid[3] = 0x00;
1828 drv->bssid[4] = 0x00;
1829 drv->bssid[5] = 0x01;
1830 os_memcpy(drv->ssid, "test", 5);
1831 drv->ssid_len = 4;
1832
1833 /* Generate a MAC address to help testing with multiple STAs */
1834 drv->own_addr[0] = 0x02; /* locally administered */
1835 sha1_prf((const u8 *) ifname, os_strlen(ifname),
1836 "wpa_supplicant test mac addr generation",
1837 NULL, 0, drv->own_addr + 1, ETH_ALEN - 1);
1838 eloop_register_timeout(1, 0, wpa_driver_test_poll, drv, NULL);
1839
1840 return drv;
1841 }
1842
1843
1844 static void wpa_driver_test_close_test_socket(struct wpa_driver_test_data *drv)
1845 {
1846 if (drv->test_socket >= 0) {
1847 eloop_unregister_read_sock(drv->test_socket);
1848 close(drv->test_socket);
1849 drv->test_socket = -1;
1850 }
1851
1852 if (drv->own_socket_path) {
1853 unlink(drv->own_socket_path);
1854 os_free(drv->own_socket_path);
1855 drv->own_socket_path = NULL;
1856 }
1857 }
1858
1859
1860 static void wpa_driver_test_deinit(void *priv)
1861 {
1862 struct wpa_driver_test_data *drv = priv;
1863 int i;
1864 wpa_driver_test_close_test_socket(drv);
1865 eloop_cancel_timeout(wpa_driver_test_scan_timeout, drv, drv->ctx);
1866 eloop_cancel_timeout(wpa_driver_test_poll, drv, NULL);
1867 os_free(drv->test_dir);
1868 for (i = 0; i < MAX_SCAN_RESULTS; i++)
1869 os_free(drv->scanres[i]);
1870 os_free(drv->probe_req_ie);
1871 os_free(drv);
1872 }
1873
1874
1875 static int wpa_driver_test_attach(struct wpa_driver_test_data *drv,
1876 const char *dir)
1877 {
1878 #ifdef DRIVER_TEST_UNIX
1879 static unsigned int counter = 0;
1880 struct sockaddr_un addr;
1881 size_t len;
1882
1883 os_free(drv->own_socket_path);
1884 if (dir) {
1885 len = os_strlen(dir) + 30;
1886 drv->own_socket_path = os_malloc(len);
1887 if (drv->own_socket_path == NULL)
1888 return -1;
1889 os_snprintf(drv->own_socket_path, len, "%s/STA-" MACSTR,
1890 dir, MAC2STR(drv->own_addr));
1891 } else {
1892 drv->own_socket_path = os_malloc(100);
1893 if (drv->own_socket_path == NULL)
1894 return -1;
1895 os_snprintf(drv->own_socket_path, 100,
1896 "/tmp/wpa_supplicant_test-%d-%d",
1897 getpid(), counter++);
1898 }
1899
1900 drv->test_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
1901 if (drv->test_socket < 0) {
1902 perror("socket(PF_UNIX)");
1903 os_free(drv->own_socket_path);
1904 drv->own_socket_path = NULL;
1905 return -1;
1906 }
1907
1908 os_memset(&addr, 0, sizeof(addr));
1909 addr.sun_family = AF_UNIX;
1910 os_strlcpy(addr.sun_path, drv->own_socket_path, sizeof(addr.sun_path));
1911 if (bind(drv->test_socket, (struct sockaddr *) &addr,
1912 sizeof(addr)) < 0) {
1913 perror("bind(PF_UNIX)");
1914 close(drv->test_socket);
1915 unlink(drv->own_socket_path);
1916 os_free(drv->own_socket_path);
1917 drv->own_socket_path = NULL;
1918 return -1;
1919 }
1920
1921 eloop_register_read_sock(drv->test_socket,
1922 wpa_driver_test_receive_unix, drv, NULL);
1923
1924 return 0;
1925 #else /* DRIVER_TEST_UNIX */
1926 return -1;
1927 #endif /* DRIVER_TEST_UNIX */
1928 }
1929
1930
1931 static int wpa_driver_test_attach_udp(struct wpa_driver_test_data *drv,
1932 char *dst)
1933 {
1934 char *pos;
1935
1936 pos = os_strchr(dst, ':');
1937 if (pos == NULL)
1938 return -1;
1939 *pos++ = '\0';
1940 wpa_printf(MSG_DEBUG, "%s: addr=%s port=%s", __func__, dst, pos);
1941
1942 drv->test_socket = socket(PF_INET, SOCK_DGRAM, 0);
1943 if (drv->test_socket < 0) {
1944 perror("socket(PF_INET)");
1945 return -1;
1946 }
1947
1948 os_memset(&drv->hostapd_addr_udp, 0, sizeof(drv->hostapd_addr_udp));
1949 drv->hostapd_addr_udp.sin_family = AF_INET;
1950 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
1951 {
1952 int a[4];
1953 u8 *pos;
1954 sscanf(dst, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
1955 pos = (u8 *) &drv->hostapd_addr_udp.sin_addr;
1956 *pos++ = a[0];
1957 *pos++ = a[1];
1958 *pos++ = a[2];
1959 *pos++ = a[3];
1960 }
1961 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
1962 inet_aton(dst, &drv->hostapd_addr_udp.sin_addr);
1963 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
1964 drv->hostapd_addr_udp.sin_port = htons(atoi(pos));
1965
1966 drv->hostapd_addr_udp_set = 1;
1967
1968 eloop_register_read_sock(drv->test_socket,
1969 wpa_driver_test_receive_unix, drv, NULL);
1970
1971 return 0;
1972 }
1973
1974
1975 static int wpa_driver_test_set_param(void *priv, const char *param)
1976 {
1977 struct wpa_driver_test_data *drv = priv;
1978 const char *pos;
1979
1980 wpa_printf(MSG_DEBUG, "%s: param='%s'", __func__, param);
1981 if (param == NULL)
1982 return 0;
1983
1984 wpa_driver_test_close_test_socket(drv);
1985
1986 #ifdef DRIVER_TEST_UNIX
1987 pos = os_strstr(param, "test_socket=");
1988 if (pos) {
1989 const char *pos2;
1990 size_t len;
1991
1992 pos += 12;
1993 pos2 = os_strchr(pos, ' ');
1994 if (pos2)
1995 len = pos2 - pos;
1996 else
1997 len = os_strlen(pos);
1998 if (len > sizeof(drv->hostapd_addr.sun_path))
1999 return -1;
2000 os_memset(&drv->hostapd_addr, 0, sizeof(drv->hostapd_addr));
2001 drv->hostapd_addr.sun_family = AF_UNIX;
2002 os_memcpy(drv->hostapd_addr.sun_path, pos, len);
2003 drv->hostapd_addr_set = 1;
2004 }
2005 #endif /* DRIVER_TEST_UNIX */
2006
2007 pos = os_strstr(param, "test_dir=");
2008 if (pos) {
2009 char *end;
2010 os_free(drv->test_dir);
2011 drv->test_dir = os_strdup(pos + 9);
2012 if (drv->test_dir == NULL)
2013 return -1;
2014 end = os_strchr(drv->test_dir, ' ');
2015 if (end)
2016 *end = '\0';
2017 if (wpa_driver_test_attach(drv, drv->test_dir))
2018 return -1;
2019 } else {
2020 pos = os_strstr(param, "test_udp=");
2021 if (pos) {
2022 char *dst, *epos;
2023 dst = os_strdup(pos + 9);
2024 if (dst == NULL)
2025 return -1;
2026 epos = os_strchr(dst, ' ');
2027 if (epos)
2028 *epos = '\0';
2029 if (wpa_driver_test_attach_udp(drv, dst))
2030 return -1;
2031 os_free(dst);
2032 } else if (wpa_driver_test_attach(drv, NULL))
2033 return -1;
2034 }
2035
2036 if (os_strstr(param, "use_associnfo=1")) {
2037 wpa_printf(MSG_DEBUG, "test_driver: Use AssocInfo events");
2038 drv->use_associnfo = 1;
2039 }
2040
2041 #ifdef CONFIG_CLIENT_MLME
2042 if (os_strstr(param, "use_mlme=1")) {
2043 wpa_printf(MSG_DEBUG, "test_driver: Use internal MLME");
2044 drv->use_mlme = 1;
2045 }
2046 #endif /* CONFIG_CLIENT_MLME */
2047
2048 return 0;
2049 }
2050
2051
2052 static const u8 * wpa_driver_test_get_mac_addr(void *priv)
2053 {
2054 struct wpa_driver_test_data *drv = priv;
2055 wpa_printf(MSG_DEBUG, "%s", __func__);
2056 return drv->own_addr;
2057 }
2058
2059
2060 static int wpa_driver_test_send_eapol(void *priv, const u8 *dest, u16 proto,
2061 const u8 *data, size_t data_len)
2062 {
2063 struct wpa_driver_test_data *drv = priv;
2064 char *msg;
2065 size_t msg_len;
2066 struct l2_ethhdr eth;
2067 struct sockaddr *addr;
2068 socklen_t alen;
2069 #ifdef DRIVER_TEST_UNIX
2070 struct sockaddr_un addr_un;
2071 #endif /* DRIVER_TEST_UNIX */
2072
2073 wpa_hexdump(MSG_MSGDUMP, "test_send_eapol TX frame", data, data_len);
2074
2075 os_memset(&eth, 0, sizeof(eth));
2076 os_memcpy(eth.h_dest, dest, ETH_ALEN);
2077 os_memcpy(eth.h_source, drv->own_addr, ETH_ALEN);
2078 eth.h_proto = host_to_be16(proto);
2079
2080 msg_len = 6 + sizeof(eth) + data_len;
2081 msg = os_malloc(msg_len);
2082 if (msg == NULL)
2083 return -1;
2084 os_memcpy(msg, "EAPOL ", 6);
2085 os_memcpy(msg + 6, &eth, sizeof(eth));
2086 os_memcpy(msg + 6 + sizeof(eth), data, data_len);
2087
2088 if (os_memcmp(dest, drv->bssid, ETH_ALEN) == 0 ||
2089 drv->test_dir == NULL) {
2090 if (drv->hostapd_addr_udp_set) {
2091 addr = (struct sockaddr *) &drv->hostapd_addr_udp;
2092 alen = sizeof(drv->hostapd_addr_udp);
2093 } else {
2094 #ifdef DRIVER_TEST_UNIX
2095 addr = (struct sockaddr *) &drv->hostapd_addr;
2096 alen = sizeof(drv->hostapd_addr);
2097 #else /* DRIVER_TEST_UNIX */
2098 os_free(msg);
2099 return -1;
2100 #endif /* DRIVER_TEST_UNIX */
2101 }
2102 } else {
2103 #ifdef DRIVER_TEST_UNIX
2104 struct stat st;
2105 os_memset(&addr_un, 0, sizeof(addr_un));
2106 addr_un.sun_family = AF_UNIX;
2107 os_snprintf(addr_un.sun_path, sizeof(addr_un.sun_path),
2108 "%s/STA-" MACSTR, drv->test_dir, MAC2STR(dest));
2109 if (stat(addr_un.sun_path, &st) < 0) {
2110 os_snprintf(addr_un.sun_path, sizeof(addr_un.sun_path),
2111 "%s/AP-" MACSTR,
2112 drv->test_dir, MAC2STR(dest));
2113 }
2114 addr = (struct sockaddr *) &addr_un;
2115 alen = sizeof(addr_un);
2116 #else /* DRIVER_TEST_UNIX */
2117 os_free(msg);
2118 return -1;
2119 #endif /* DRIVER_TEST_UNIX */
2120 }
2121
2122 if (sendto(drv->test_socket, msg, msg_len, 0, addr, alen) < 0) {
2123 perror("sendmsg(test_socket)");
2124 os_free(msg);
2125 return -1;
2126 }
2127
2128 os_free(msg);
2129 return 0;
2130 }
2131
2132
2133 static int wpa_driver_test_get_capa(void *priv, struct wpa_driver_capa *capa)
2134 {
2135 struct wpa_driver_test_data *drv = priv;
2136 os_memset(capa, 0, sizeof(*capa));
2137 capa->key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2138 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2139 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2140 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK |
2141 WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE |
2142 WPA_DRIVER_CAPA_KEY_MGMT_FT |
2143 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2144 capa->enc = WPA_DRIVER_CAPA_ENC_WEP40 |
2145 WPA_DRIVER_CAPA_ENC_WEP104 |
2146 WPA_DRIVER_CAPA_ENC_TKIP |
2147 WPA_DRIVER_CAPA_ENC_CCMP;
2148 capa->auth = WPA_DRIVER_AUTH_OPEN |
2149 WPA_DRIVER_AUTH_SHARED |
2150 WPA_DRIVER_AUTH_LEAP;
2151 if (drv->use_mlme)
2152 capa->flags |= WPA_DRIVER_FLAGS_USER_SPACE_MLME;
2153 capa->max_scan_ssids = 2;
2154
2155 return 0;
2156 }
2157
2158
2159 static int wpa_driver_test_mlme_setprotection(void *priv, const u8 *addr,
2160 int protect_type,
2161 int key_type)
2162 {
2163 wpa_printf(MSG_DEBUG, "%s: protect_type=%d key_type=%d",
2164 __func__, protect_type, key_type);
2165
2166 if (addr) {
2167 wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR,
2168 __func__, MAC2STR(addr));
2169 }
2170
2171 return 0;
2172 }
2173
2174
2175 #ifdef CONFIG_CLIENT_MLME
2176 static int wpa_driver_test_set_channel(void *priv, hostapd_hw_mode phymode,
2177 int chan, int freq)
2178 {
2179 wpa_printf(MSG_DEBUG, "%s: phymode=%d chan=%d freq=%d",
2180 __func__, phymode, chan, freq);
2181 return 0;
2182 }
2183
2184
2185 static int wpa_driver_test_send_mlme(void *priv, const u8 *data,
2186 size_t data_len)
2187 {
2188 struct wpa_driver_test_data *drv = priv;
2189 struct msghdr msg;
2190 struct iovec io[2];
2191 struct sockaddr_un addr;
2192 const u8 *dest;
2193 struct dirent *dent;
2194 DIR *dir;
2195
2196 wpa_hexdump(MSG_MSGDUMP, "test_send_mlme", data, data_len);
2197 if (data_len < 10)
2198 return -1;
2199 dest = data + 4;
2200
2201 io[0].iov_base = "MLME ";
2202 io[0].iov_len = 5;
2203 io[1].iov_base = (u8 *) data;
2204 io[1].iov_len = data_len;
2205
2206 os_memset(&msg, 0, sizeof(msg));
2207 msg.msg_iov = io;
2208 msg.msg_iovlen = 2;
2209 if (os_memcmp(dest, drv->bssid, ETH_ALEN) == 0 ||
2210 drv->test_dir == NULL) {
2211 if (drv->hostapd_addr_udp_set) {
2212 msg.msg_name = &drv->hostapd_addr_udp;
2213 msg.msg_namelen = sizeof(drv->hostapd_addr_udp);
2214 } else {
2215 #ifdef DRIVER_TEST_UNIX
2216 msg.msg_name = &drv->hostapd_addr;
2217 msg.msg_namelen = sizeof(drv->hostapd_addr);
2218 #endif /* DRIVER_TEST_UNIX */
2219 }
2220 } else if (os_memcmp(dest, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0)
2221 {
2222 dir = opendir(drv->test_dir);
2223 if (dir == NULL)
2224 return -1;
2225 while ((dent = readdir(dir))) {
2226 #ifdef _DIRENT_HAVE_D_TYPE
2227 /* Skip the file if it is not a socket.
2228 * Also accept DT_UNKNOWN (0) in case
2229 * the C library or underlying file
2230 * system does not support d_type. */
2231 if (dent->d_type != DT_SOCK &&
2232 dent->d_type != DT_UNKNOWN)
2233 continue;
2234 #endif /* _DIRENT_HAVE_D_TYPE */
2235 if (os_strcmp(dent->d_name, ".") == 0 ||
2236 os_strcmp(dent->d_name, "..") == 0)
2237 continue;
2238 wpa_printf(MSG_DEBUG, "%s: Send broadcast MLME to %s",
2239 __func__, dent->d_name);
2240 os_memset(&addr, 0, sizeof(addr));
2241 addr.sun_family = AF_UNIX;
2242 os_snprintf(addr.sun_path, sizeof(addr.sun_path),
2243 "%s/%s", drv->test_dir, dent->d_name);
2244
2245 msg.msg_name = &addr;
2246 msg.msg_namelen = sizeof(addr);
2247
2248 if (sendmsg(drv->test_socket, &msg, 0) < 0)
2249 perror("sendmsg(test_socket)");
2250 }
2251 closedir(dir);
2252 return 0;
2253 } else {
2254 struct stat st;
2255 os_memset(&addr, 0, sizeof(addr));
2256 addr.sun_family = AF_UNIX;
2257 os_snprintf(addr.sun_path, sizeof(addr.sun_path),
2258 "%s/AP-" MACSTR, drv->test_dir, MAC2STR(dest));
2259 if (stat(addr.sun_path, &st) < 0) {
2260 os_snprintf(addr.sun_path, sizeof(addr.sun_path),
2261 "%s/STA-" MACSTR,
2262 drv->test_dir, MAC2STR(dest));
2263 }
2264 msg.msg_name = &addr;
2265 msg.msg_namelen = sizeof(addr);
2266 }
2267
2268 if (sendmsg(drv->test_socket, &msg, 0) < 0) {
2269 perror("sendmsg(test_socket)");
2270 return -1;
2271 }
2272
2273 return 0;
2274 }
2275
2276
2277 static int wpa_driver_test_mlme_add_sta(void *priv, const u8 *addr,
2278 const u8 *supp_rates,
2279 size_t supp_rates_len)
2280 {
2281 wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, MAC2STR(addr));
2282 return 0;
2283 }
2284
2285
2286 static int wpa_driver_test_mlme_remove_sta(void *priv, const u8 *addr)
2287 {
2288 wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, MAC2STR(addr));
2289 return 0;
2290 }
2291
2292
2293 static int wpa_driver_test_set_ssid(void *priv, const u8 *ssid,
2294 size_t ssid_len)
2295 {
2296 wpa_printf(MSG_DEBUG, "%s", __func__);
2297 return 0;
2298 }
2299
2300
2301 static int wpa_driver_test_set_bssid(void *priv, const u8 *bssid)
2302 {
2303 wpa_printf(MSG_DEBUG, "%s: bssid=" MACSTR, __func__, MAC2STR(bssid));
2304 return 0;
2305 }
2306 #endif /* CONFIG_CLIENT_MLME */
2307
2308
2309 static int wpa_driver_test_set_probe_req_ie(void *priv, const u8 *ies,
2310 size_t ies_len)
2311 {
2312 struct wpa_driver_test_data *drv = priv;
2313
2314 os_free(drv->probe_req_ie);
2315 if (ies) {
2316 drv->probe_req_ie = os_malloc(ies_len);
2317 if (drv->probe_req_ie == NULL) {
2318 drv->probe_req_ie_len = 0;
2319 return -1;
2320 }
2321 os_memcpy(drv->probe_req_ie, ies, ies_len);
2322 drv->probe_req_ie_len = ies_len;
2323 } else {
2324 drv->probe_req_ie = NULL;
2325 drv->probe_req_ie_len = 0;
2326 }
2327 return 0;
2328 }
2329
2330
2331 static void * wpa_driver_test_global_init(void)
2332 {
2333 struct wpa_driver_test_global *global;
2334
2335 global = os_zalloc(sizeof(*global));
2336 return global;
2337 }
2338
2339
2340 static void wpa_driver_test_global_deinit(void *priv)
2341 {
2342 struct wpa_driver_test_global *global = priv;
2343 os_free(global);
2344 }
2345
2346
2347 static struct wpa_interface_info *
2348 wpa_driver_test_get_interfaces(void *global_priv)
2349 {
2350 /* struct wpa_driver_test_global *global = priv; */
2351 struct wpa_interface_info *iface;
2352
2353 iface = os_zalloc(sizeof(*iface));
2354 if (iface == NULL)
2355 return iface;
2356 iface->ifname = os_strdup("sta0");
2357 iface->desc = os_strdup("test interface 0");
2358 iface->drv_name = "test";
2359 iface->next = os_zalloc(sizeof(*iface));
2360 if (iface->next) {
2361 iface->next->ifname = os_strdup("sta1");
2362 iface->next->desc = os_strdup("test interface 1");
2363 iface->next->drv_name = "test";
2364 }
2365
2366 return iface;
2367 }
2368
2369 #endif /* HOSTAPD */
2370
2371
2372 #if defined(HOSTAPD) || defined(CONFIG_CLIENT_MLME)
2373 static struct hostapd_hw_modes *
2374 wpa_driver_test_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
2375 {
2376 struct hostapd_hw_modes *modes;
2377
2378 *num_modes = 3;
2379 *flags = 0;
2380 modes = os_zalloc(*num_modes * sizeof(struct hostapd_hw_modes));
2381 if (modes == NULL)
2382 return NULL;
2383 modes[0].mode = HOSTAPD_MODE_IEEE80211G;
2384 modes[0].num_channels = 1;
2385 modes[0].num_rates = 1;
2386 modes[0].channels = os_zalloc(sizeof(struct hostapd_channel_data));
2387 modes[0].rates = os_zalloc(sizeof(struct hostapd_rate_data));
2388 if (modes[0].channels == NULL || modes[0].rates == NULL)
2389 goto fail;
2390 modes[0].channels[0].chan = 1;
2391 modes[0].channels[0].freq = 2412;
2392 modes[0].channels[0].flag = 0;
2393 modes[0].rates[0].rate = 10;
2394 modes[0].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
2395 HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
2396
2397 modes[1].mode = HOSTAPD_MODE_IEEE80211B;
2398 modes[1].num_channels = 1;
2399 modes[1].num_rates = 1;
2400 modes[1].channels = os_zalloc(sizeof(struct hostapd_channel_data));
2401 modes[1].rates = os_zalloc(sizeof(struct hostapd_rate_data));
2402 if (modes[1].channels == NULL || modes[1].rates == NULL)
2403 goto fail;
2404 modes[1].channels[0].chan = 1;
2405 modes[1].channels[0].freq = 2412;
2406 modes[1].channels[0].flag = 0;
2407 modes[1].rates[0].rate = 10;
2408 modes[1].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
2409 HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
2410
2411 modes[2].mode = HOSTAPD_MODE_IEEE80211A;
2412 modes[2].num_channels = 1;
2413 modes[2].num_rates = 1;
2414 modes[2].channels = os_zalloc(sizeof(struct hostapd_channel_data));
2415 modes[2].rates = os_zalloc(sizeof(struct hostapd_rate_data));
2416 if (modes[2].channels == NULL || modes[2].rates == NULL)
2417 goto fail;
2418 modes[2].channels[0].chan = 60;
2419 modes[2].channels[0].freq = 5300;
2420 modes[2].channels[0].flag = 0;
2421 modes[2].rates[0].rate = 60;
2422 modes[2].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
2423 HOSTAPD_RATE_MANDATORY;
2424
2425 return modes;
2426
2427 fail:
2428 if (modes) {
2429 size_t i;
2430 for (i = 0; i < *num_modes; i++) {
2431 os_free(modes[i].channels);
2432 os_free(modes[i].rates);
2433 }
2434 os_free(modes);
2435 }
2436 return NULL;
2437 }
2438 #endif /* HOSTAPD || CONFIG_CLIENT_MLME */
2439
2440
2441 const struct wpa_driver_ops wpa_driver_test_ops = {
2442 "test",
2443 "wpa_supplicant test driver",
2444 #ifdef HOSTAPD
2445 .hapd_init = test_driver_init,
2446 .hapd_deinit = test_driver_deinit,
2447 .hapd_send_eapol = test_driver_send_eapol,
2448 .send_mlme = wpa_driver_test_send_mlme,
2449 .set_generic_elem = test_driver_set_generic_elem,
2450 .sta_deauth = test_driver_sta_deauth,
2451 .sta_disassoc = test_driver_sta_disassoc,
2452 .get_hw_feature_data = wpa_driver_test_get_hw_feature_data,
2453 .bss_add = test_driver_bss_add,
2454 .bss_remove = test_driver_bss_remove,
2455 .if_add = test_driver_if_add,
2456 .if_update = test_driver_if_update,
2457 .if_remove = test_driver_if_remove,
2458 .valid_bss_mask = test_driver_valid_bss_mask,
2459 .hapd_set_ssid = test_driver_set_ssid,
2460 .set_privacy = test_driver_set_privacy,
2461 .hapd_set_key = test_driver_set_key,
2462 .set_sta_vlan = test_driver_set_sta_vlan,
2463 .sta_add = test_driver_sta_add,
2464 .send_ether = test_driver_send_ether,
2465 .set_wps_beacon_ie = test_driver_set_wps_beacon_ie,
2466 .set_wps_probe_resp_ie = test_driver_set_wps_probe_resp_ie,
2467 #else /* HOSTAPD */
2468 wpa_driver_test_get_bssid,
2469 wpa_driver_test_get_ssid,
2470 wpa_driver_test_set_wpa,
2471 wpa_driver_test_set_key,
2472 NULL /* init */,
2473 wpa_driver_test_deinit,
2474 wpa_driver_test_set_param,
2475 NULL /* set_countermeasures */,
2476 NULL /* set_drop_unencrypted */,
2477 NULL /* scan */,
2478 NULL /* get_scan_results */,
2479 wpa_driver_test_deauthenticate,
2480 wpa_driver_test_disassociate,
2481 wpa_driver_test_associate,
2482 NULL /* set_auth_alg */,
2483 NULL /* add_pmkid */,
2484 NULL /* remove_pmkid */,
2485 NULL /* flush_pmkid */,
2486 wpa_driver_test_get_capa,
2487 NULL /* poll */,
2488 NULL /* get_ifname */,
2489 wpa_driver_test_get_mac_addr,
2490 wpa_driver_test_send_eapol,
2491 NULL /* set_operstate */,
2492 wpa_driver_test_mlme_setprotection,
2493 #ifdef CONFIG_CLIENT_MLME
2494 wpa_driver_test_get_hw_feature_data,
2495 wpa_driver_test_set_channel,
2496 wpa_driver_test_set_ssid,
2497 wpa_driver_test_set_bssid,
2498 wpa_driver_test_send_mlme,
2499 wpa_driver_test_mlme_add_sta,
2500 wpa_driver_test_mlme_remove_sta,
2501 #else /* CONFIG_CLIENT_MLME */
2502 NULL /* get_hw_feature_data */,
2503 NULL /* set_channel */,
2504 NULL /* set_ssid */,
2505 NULL /* set_bssid */,
2506 NULL /* send_mlme */,
2507 NULL /* mlme_add_sta */,
2508 NULL /* mlme_remove_sta */,
2509 #endif /* CONFIG_CLIENT_MLME */
2510 NULL /* update_ft_ies */,
2511 NULL /* send_ft_action */,
2512 wpa_driver_test_get_scan_results2,
2513 wpa_driver_test_set_probe_req_ie,
2514 NULL /* set_mode */,
2515 NULL /* set_country */,
2516 wpa_driver_test_global_init,
2517 wpa_driver_test_global_deinit,
2518 wpa_driver_test_init2,
2519 wpa_driver_test_get_interfaces,
2520 wpa_driver_test_scan,
2521 NULL /* authenticate */,
2522 NULL /* set_beacon */,
2523 NULL /* set_beacon_int */,
2524 NULL /* hapd_init */,
2525 NULL /* hapd_deinit */,
2526 NULL /* set_ieee8021x */,
2527 NULL /* set_privacy */,
2528 NULL /* hapd_set_key */,
2529 NULL /* get_seqnum */,
2530 NULL /* get_seqnum_igtk */,
2531 NULL /* flush */,
2532 NULL /* set_generic_elem */,
2533 NULL /* read_sta_data */,
2534 NULL /* hapd_send_eapol */,
2535 NULL /* sta_deauth */,
2536 NULL /* sta_disassoc */,
2537 NULL /* sta_remove */,
2538 NULL /* hapd_get_ssid */,
2539 NULL /* hapd_set_ssid */,
2540 NULL /* hapd_set_countermeasures */,
2541 NULL /* sta_add */,
2542 NULL /* get_inact_sec */,
2543 NULL /* sta_clear_stats */,
2544 NULL /* set_freq */,
2545 NULL /* set_rts */,
2546 NULL /* set_frag */,
2547 NULL /* set_retry */,
2548 NULL /* sta_set_flags */,
2549 NULL /* set_rate_sets */,
2550 NULL /* set_ieee80211d */,
2551 NULL /* hapd_set_beacon */,
2552 NULL /* set_internal_bridge */,
2553 NULL /* set_broadcast_ssid */,
2554 NULL /* set_cts_protect */,
2555 NULL /* set_preamble */,
2556 NULL /* set_short_slot_time */,
2557 NULL /* set_tx_queue_params */,
2558 NULL /* bss_add */,
2559 NULL /* bss_remove */,
2560 NULL /* valid_bss_mask */,
2561 NULL /* passive_scan */,
2562 NULL /* if_add */,
2563 NULL /* if_update */,
2564 NULL /* if_remove */,
2565 NULL /* set_sta_vlan */,
2566 NULL /* commit */,
2567 NULL /* send_ether */,
2568 NULL /* set_radius_acl_auth */,
2569 NULL /* set_radius_acl_expire */,
2570 NULL /* set_ht_params */,
2571 NULL /* set_wps_beacon_ie */,
2572 NULL /* set_wps_probe_resp_ie */
2573 #endif /* HOSTAPD */
2574 };