2 * Testing driver interface for a simulated network driver
3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 /* Make sure we get winsock2.h for Windows build to get sockaddr_storage */
16 #include "build_config.h"
17 #ifdef CONFIG_NATIVE_WINDOWS
19 #endif /* CONFIG_NATIVE_WINDOWS */
21 #include "utils/includes.h"
23 #ifndef CONFIG_NATIVE_WINDOWS
27 #define DRIVER_TEST_UNIX
28 #endif /* CONFIG_NATIVE_WINDOWS */
30 #include "utils/common.h"
31 #include "utils/eloop.h"
32 #include "utils/trace.h"
33 #include "common/ieee802_11_defs.h"
34 #include "crypto/sha1.h"
35 #include "l2_packet/l2_packet.h"
39 struct test_client_socket
{
40 struct test_client_socket
*next
;
42 struct sockaddr_un un
;
44 struct test_driver_bss
*bss
;
47 struct test_driver_bss
{
48 struct test_driver_bss
*next
;
50 char ifname
[IFNAMSIZ
+ 1];
55 size_t wps_beacon_ie_len
;
56 u8
*wps_probe_resp_ie
;
57 size_t wps_probe_resp_ie_len
;
63 struct wpa_driver_test_global
{
65 u8 req_addr
[ETH_ALEN
];
68 struct wpa_driver_test_data
{
69 struct wpa_driver_test_global
*global
;
72 char ifname
[IFNAMSIZ
+ 1];
73 u8 own_addr
[ETH_ALEN
];
75 #ifdef DRIVER_TEST_UNIX
76 struct sockaddr_un hostapd_addr
;
77 #endif /* DRIVER_TEST_UNIX */
79 struct sockaddr_in hostapd_addr_udp
;
80 int hostapd_addr_udp_set
;
81 char *own_socket_path
;
86 #define MAX_SCAN_RESULTS 30
87 struct wpa_scan_res
*scanres
[MAX_SCAN_RESULTS
];
91 size_t assoc_wpa_ie_len
;
95 size_t probe_req_ie_len
;
100 struct test_client_socket
*cli
;
101 struct test_driver_bss
*bss
;
106 int probe_req_report
;
107 unsigned int remain_on_channel_freq
;
108 unsigned int remain_on_channel_duration
;
114 static void wpa_driver_test_deinit(void *priv
);
115 static int wpa_driver_test_attach(struct wpa_driver_test_data
*drv
,
116 const char *dir
, int ap
);
117 static void wpa_driver_test_close_test_socket(
118 struct wpa_driver_test_data
*drv
);
119 static void test_remain_on_channel_timeout(void *eloop_ctx
, void *timeout_ctx
);
122 static void test_driver_free_bss(struct test_driver_bss
*bss
)
125 os_free(bss
->wps_beacon_ie
);
126 os_free(bss
->wps_probe_resp_ie
);
131 static void test_driver_free_bsses(struct wpa_driver_test_data
*drv
)
133 struct test_driver_bss
*bss
, *prev_bss
;
139 test_driver_free_bss(prev_bss
);
146 static struct test_client_socket
*
147 test_driver_get_cli(struct wpa_driver_test_data
*drv
, struct sockaddr_un
*from
,
150 struct test_client_socket
*cli
= drv
->cli
;
153 if (cli
->unlen
== fromlen
&&
154 strncmp(cli
->un
.sun_path
, from
->sun_path
,
155 fromlen
- sizeof(cli
->un
.sun_family
)) == 0)
164 static int test_driver_send_eapol(void *priv
, const u8
*addr
, const u8
*data
,
165 size_t data_len
, int encrypt
,
168 struct wpa_driver_test_data
*drv
= priv
;
169 struct test_client_socket
*cli
;
172 struct l2_ethhdr eth
;
174 if (drv
->test_socket
< 0)
179 if (memcmp(cli
->addr
, addr
, ETH_ALEN
) == 0)
185 wpa_printf(MSG_DEBUG
, "%s: no destination client entry",
190 memcpy(eth
.h_dest
, addr
, ETH_ALEN
);
191 memcpy(eth
.h_source
, own_addr
, ETH_ALEN
);
192 eth
.h_proto
= host_to_be16(ETH_P_EAPOL
);
194 io
[0].iov_base
= "EAPOL ";
196 io
[1].iov_base
= ð
;
197 io
[1].iov_len
= sizeof(eth
);
198 io
[2].iov_base
= (u8
*) data
;
199 io
[2].iov_len
= data_len
;
201 memset(&msg
, 0, sizeof(msg
));
204 msg
.msg_name
= &cli
->un
;
205 msg
.msg_namelen
= cli
->unlen
;
206 return sendmsg(drv
->test_socket
, &msg
, 0);
210 static int test_driver_send_ether(void *priv
, const u8
*dst
, const u8
*src
,
211 u16 proto
, const u8
*data
, size_t data_len
)
213 struct wpa_driver_test_data
*drv
= priv
;
216 struct l2_ethhdr eth
;
218 struct sockaddr_un addr
;
221 int ret
= 0, broadcast
= 0, count
= 0;
223 if (drv
->test_socket
< 0 || drv
->test_dir
== NULL
) {
224 wpa_printf(MSG_DEBUG
, "%s: invalid parameters (sock=%d "
226 __func__
, drv
->test_socket
, drv
->test_dir
);
230 broadcast
= memcmp(dst
, "\xff\xff\xff\xff\xff\xff", ETH_ALEN
) == 0;
231 snprintf(desttxt
, sizeof(desttxt
), MACSTR
, MAC2STR(dst
));
233 memcpy(eth
.h_dest
, dst
, ETH_ALEN
);
234 memcpy(eth
.h_source
, src
, ETH_ALEN
);
235 eth
.h_proto
= host_to_be16(proto
);
237 io
[0].iov_base
= "ETHER ";
239 io
[1].iov_base
= ð
;
240 io
[1].iov_len
= sizeof(eth
);
241 io
[2].iov_base
= (u8
*) data
;
242 io
[2].iov_len
= data_len
;
244 memset(&msg
, 0, sizeof(msg
));
248 dir
= opendir(drv
->test_dir
);
250 perror("test_driver: opendir");
253 while ((dent
= readdir(dir
))) {
254 #ifdef _DIRENT_HAVE_D_TYPE
255 /* Skip the file if it is not a socket. Also accept
256 * DT_UNKNOWN (0) in case the C library or underlying file
257 * system does not support d_type. */
258 if (dent
->d_type
!= DT_SOCK
&& dent
->d_type
!= DT_UNKNOWN
)
260 #endif /* _DIRENT_HAVE_D_TYPE */
261 if (strcmp(dent
->d_name
, ".") == 0 ||
262 strcmp(dent
->d_name
, "..") == 0)
265 memset(&addr
, 0, sizeof(addr
));
266 addr
.sun_family
= AF_UNIX
;
267 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s/%s",
268 drv
->test_dir
, dent
->d_name
);
270 if (strcmp(addr
.sun_path
, drv
->own_socket_path
) == 0)
272 if (!broadcast
&& strstr(dent
->d_name
, desttxt
) == NULL
)
275 wpa_printf(MSG_DEBUG
, "%s: Send ether frame to %s",
276 __func__
, dent
->d_name
);
278 msg
.msg_name
= &addr
;
279 msg
.msg_namelen
= sizeof(addr
);
280 ret
= sendmsg(drv
->test_socket
, &msg
, 0);
282 perror("driver_test: sendmsg");
287 if (!broadcast
&& count
== 0) {
288 wpa_printf(MSG_DEBUG
, "%s: Destination " MACSTR
" not found",
289 __func__
, MAC2STR(dst
));
297 static int wpa_driver_test_send_mlme(void *priv
, const u8
*data
,
300 struct wpa_driver_test_data
*drv
= priv
;
304 struct sockaddr_un addr
;
309 struct ieee80211_hdr
*hdr
;
316 union wpa_event_data event
;
318 wpa_hexdump(MSG_MSGDUMP
, "test_send_mlme", data
, data_len
);
319 if (drv
->test_socket
< 0 || data_len
< 10) {
320 wpa_printf(MSG_DEBUG
, "%s: invalid parameters (sock=%d len=%lu"
322 __func__
, drv
->test_socket
,
323 (unsigned long) data_len
,
329 broadcast
= os_memcmp(dest
, "\xff\xff\xff\xff\xff\xff", ETH_ALEN
) == 0;
332 snprintf(desttxt
, sizeof(desttxt
), MACSTR
, MAC2STR(dest
));
335 if (drv
->remain_on_channel_freq
)
336 freq
= drv
->remain_on_channel_freq
;
338 freq
= drv
->current_freq
;
339 wpa_printf(MSG_DEBUG
, "test_driver(%s): MLME TX on freq %d MHz",
341 os_snprintf(cmd
, sizeof(cmd
), "MLME freq=%d ", freq
);
342 io
[0].iov_base
= cmd
;
343 io
[0].iov_len
= os_strlen(cmd
);
344 io
[1].iov_base
= (void *) data
;
345 io
[1].iov_len
= data_len
;
347 os_memset(&msg
, 0, sizeof(msg
));
352 if (drv
->test_dir
== NULL
) {
353 wpa_printf(MSG_DEBUG
, "%s: test_dir == NULL", __func__
);
357 dir
= opendir(drv
->test_dir
);
359 perror("test_driver: opendir");
362 while ((dent
= readdir(dir
))) {
363 #ifdef _DIRENT_HAVE_D_TYPE
364 /* Skip the file if it is not a socket. Also accept
365 * DT_UNKNOWN (0) in case the C library or underlying file
366 * system does not support d_type. */
367 if (dent
->d_type
!= DT_SOCK
&& dent
->d_type
!= DT_UNKNOWN
)
369 #endif /* _DIRENT_HAVE_D_TYPE */
370 if (os_strcmp(dent
->d_name
, ".") == 0 ||
371 os_strcmp(dent
->d_name
, "..") == 0)
374 os_memset(&addr
, 0, sizeof(addr
));
375 addr
.sun_family
= AF_UNIX
;
376 os_snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s/%s",
377 drv
->test_dir
, dent
->d_name
);
379 if (os_strcmp(addr
.sun_path
, drv
->own_socket_path
) == 0)
381 if (!broadcast
&& os_strstr(dent
->d_name
, desttxt
) == NULL
)
384 wpa_printf(MSG_DEBUG
, "%s: Send management frame to %s",
385 __func__
, dent
->d_name
);
387 msg
.msg_name
= &addr
;
388 msg
.msg_namelen
= sizeof(addr
);
389 ret
= sendmsg(drv
->test_socket
, &msg
, 0);
391 perror("driver_test: sendmsg(test_socket)");
396 if (os_memcmp(dest
, drv
->bssid
, ETH_ALEN
) == 0 ||
397 drv
->test_dir
== NULL
) {
398 if (drv
->hostapd_addr_udp_set
) {
399 msg
.msg_name
= &drv
->hostapd_addr_udp
;
400 msg
.msg_namelen
= sizeof(drv
->hostapd_addr_udp
);
402 #ifdef DRIVER_TEST_UNIX
403 msg
.msg_name
= &drv
->hostapd_addr
;
404 msg
.msg_namelen
= sizeof(drv
->hostapd_addr
);
405 #endif /* DRIVER_TEST_UNIX */
407 } else if (broadcast
) {
408 dir
= opendir(drv
->test_dir
);
411 while ((dent
= readdir(dir
))) {
412 #ifdef _DIRENT_HAVE_D_TYPE
413 /* Skip the file if it is not a socket.
414 * Also accept DT_UNKNOWN (0) in case
415 * the C library or underlying file
416 * system does not support d_type. */
417 if (dent
->d_type
!= DT_SOCK
&&
418 dent
->d_type
!= DT_UNKNOWN
)
420 #endif /* _DIRENT_HAVE_D_TYPE */
421 if (os_strcmp(dent
->d_name
, ".") == 0 ||
422 os_strcmp(dent
->d_name
, "..") == 0)
424 wpa_printf(MSG_DEBUG
, "%s: Send broadcast MLME to %s",
425 __func__
, dent
->d_name
);
426 os_memset(&addr
, 0, sizeof(addr
));
427 addr
.sun_family
= AF_UNIX
;
428 os_snprintf(addr
.sun_path
, sizeof(addr
.sun_path
),
429 "%s/%s", drv
->test_dir
, dent
->d_name
);
431 msg
.msg_name
= &addr
;
432 msg
.msg_namelen
= sizeof(addr
);
434 ret
= sendmsg(drv
->test_socket
, &msg
, 0);
436 perror("driver_test: sendmsg(test_socket)");
442 os_memset(&addr
, 0, sizeof(addr
));
443 addr
.sun_family
= AF_UNIX
;
444 os_snprintf(addr
.sun_path
, sizeof(addr
.sun_path
),
445 "%s/AP-" MACSTR
, drv
->test_dir
, MAC2STR(dest
));
446 if (stat(addr
.sun_path
, &st
) < 0) {
447 os_snprintf(addr
.sun_path
, sizeof(addr
.sun_path
),
449 drv
->test_dir
, MAC2STR(dest
));
451 msg
.msg_name
= &addr
;
452 msg
.msg_namelen
= sizeof(addr
);
455 if (sendmsg(drv
->test_socket
, &msg
, 0) < 0) {
456 perror("sendmsg(test_socket)");
461 hdr
= (struct ieee80211_hdr
*) data
;
462 fc
= le_to_host16(hdr
->frame_control
);
464 os_memset(&event
, 0, sizeof(event
));
465 event
.tx_status
.type
= WLAN_FC_GET_TYPE(fc
);
466 event
.tx_status
.stype
= WLAN_FC_GET_STYPE(fc
);
467 event
.tx_status
.dst
= hdr
->addr1
;
468 event
.tx_status
.data
= data
;
469 event
.tx_status
.data_len
= data_len
;
470 event
.tx_status
.ack
= ret
>= 0;
471 wpa_supplicant_event(drv
->ctx
, EVENT_TX_STATUS
, &event
);
477 static void test_driver_scan(struct wpa_driver_test_data
*drv
,
478 struct sockaddr_un
*from
, socklen_t fromlen
,
481 char buf
[512], *pos
, *end
;
483 struct test_driver_bss
*bss
;
487 union wpa_event_data event
;
489 /* data: optional [ ' ' | STA-addr | ' ' | IEs(hex) ] */
491 wpa_printf(MSG_DEBUG
, "test_driver: SCAN");
495 hwaddr_aton(data
+ 1, sa
)) {
496 wpa_printf(MSG_DEBUG
, "test_driver: Unexpected SCAN "
504 ielen
= os_strlen(data
) / 2;
505 if (ielen
> sizeof(ie
))
507 if (hexstr2bin(data
, ie
, ielen
) < 0)
510 wpa_printf(MSG_DEBUG
, "test_driver: Scan from " MACSTR
,
512 wpa_hexdump(MSG_MSGDUMP
, "test_driver: scan IEs", ie
, ielen
);
514 os_memset(&event
, 0, sizeof(event
));
515 event
.rx_probe_req
.sa
= sa
;
516 event
.rx_probe_req
.ie
= ie
;
517 event
.rx_probe_req
.ie_len
= ielen
;
518 wpa_supplicant_event(drv
->ctx
, EVENT_RX_PROBE_REQ
, &event
);
521 for (bss
= drv
->bss
; bss
; bss
= bss
->next
) {
523 end
= buf
+ sizeof(buf
);
525 /* reply: SCANRESP BSSID SSID IEs */
526 ret
= snprintf(pos
, end
- pos
, "SCANRESP " MACSTR
" ",
527 MAC2STR(bss
->bssid
));
528 if (ret
< 0 || ret
>= end
- pos
)
531 pos
+= wpa_snprintf_hex(pos
, end
- pos
,
532 bss
->ssid
, bss
->ssid_len
);
533 ret
= snprintf(pos
, end
- pos
, " ");
534 if (ret
< 0 || ret
>= end
- pos
)
537 pos
+= wpa_snprintf_hex(pos
, end
- pos
, bss
->ie
, bss
->ielen
);
538 pos
+= wpa_snprintf_hex(pos
, end
- pos
, bss
->wps_probe_resp_ie
,
539 bss
->wps_probe_resp_ie_len
);
542 ret
= snprintf(pos
, end
- pos
, " PRIVACY");
543 if (ret
< 0 || ret
>= end
- pos
)
548 sendto(drv
->test_socket
, buf
, pos
- buf
, 0,
549 (struct sockaddr
*) from
, fromlen
);
554 static void test_driver_assoc(struct wpa_driver_test_data
*drv
,
555 struct sockaddr_un
*from
, socklen_t fromlen
,
558 struct test_client_socket
*cli
;
559 u8 ie
[256], ssid
[32];
560 size_t ielen
, ssid_len
= 0;
561 char *pos
, *pos2
, cmd
[50];
562 struct test_driver_bss
*bss
;
564 /* data: STA-addr SSID(hex) IEs(hex) */
566 cli
= os_zalloc(sizeof(*cli
));
570 if (hwaddr_aton(data
, cli
->addr
)) {
571 printf("test_socket: Invalid MAC address '%s' in ASSOC\n",
579 pos2
= strchr(pos
, ' ');
582 ssid_len
= (pos2
- pos
) / 2;
583 if (hexstr2bin(pos
, ssid
, ssid_len
) < 0) {
584 wpa_printf(MSG_DEBUG
, "%s: Invalid SSID", __func__
);
588 wpa_hexdump_ascii(MSG_DEBUG
, "test_driver_assoc: SSID",
592 ielen
= strlen(pos
) / 2;
593 if (ielen
> sizeof(ie
))
595 if (hexstr2bin(pos
, ie
, ielen
) < 0)
599 for (bss
= drv
->bss
; bss
; bss
= bss
->next
) {
600 if (bss
->ssid_len
== ssid_len
&&
601 memcmp(bss
->ssid
, ssid
, ssid_len
) == 0)
605 wpa_printf(MSG_DEBUG
, "%s: No matching SSID found from "
606 "configured BSSes", __func__
);
612 memcpy(&cli
->un
, from
, sizeof(cli
->un
));
613 cli
->unlen
= fromlen
;
614 cli
->next
= drv
->cli
;
616 wpa_hexdump_ascii(MSG_DEBUG
, "test_socket: ASSOC sun_path",
617 (const u8
*) cli
->un
.sun_path
,
618 cli
->unlen
- sizeof(cli
->un
.sun_family
));
620 snprintf(cmd
, sizeof(cmd
), "ASSOCRESP " MACSTR
" 0",
621 MAC2STR(bss
->bssid
));
622 sendto(drv
->test_socket
, cmd
, strlen(cmd
), 0,
623 (struct sockaddr
*) from
, fromlen
);
625 drv_event_assoc(bss
->bss_ctx
, cli
->addr
, ie
, ielen
);
629 static void test_driver_disassoc(struct wpa_driver_test_data
*drv
,
630 struct sockaddr_un
*from
, socklen_t fromlen
)
632 struct test_client_socket
*cli
;
634 cli
= test_driver_get_cli(drv
, from
, fromlen
);
638 drv_event_disassoc(drv
->ctx
, cli
->addr
);
642 static void test_driver_eapol(struct wpa_driver_test_data
*drv
,
643 struct sockaddr_un
*from
, socklen_t fromlen
,
644 u8
*data
, size_t datalen
)
647 struct test_client_socket
*cli
;
649 const u8
*src
= NULL
;
650 union wpa_event_data event
;
654 /* Skip Ethernet header */
655 src
= data
+ ETH_ALEN
;
656 wpa_printf(MSG_DEBUG
, "test_driver: dst=" MACSTR
" src="
657 MACSTR
" proto=%04x",
658 MAC2STR(data
), MAC2STR(src
),
659 WPA_GET_BE16(data
+ 2 * ETH_ALEN
));
664 os_memset(&event
, 0, sizeof(event
));
665 event
.eapol_rx
.data
= data
;
666 event
.eapol_rx
.data_len
= datalen
;
669 cli
= test_driver_get_cli(drv
, from
, fromlen
);
671 event
.eapol_rx
.src
= cli
->addr
;
672 ctx
= cli
->bss
->bss_ctx
;
674 wpa_printf(MSG_DEBUG
, "test_socket: EAPOL from unknown "
680 event
.eapol_rx
.src
= src
;
686 wpa_supplicant_event(ctx
, EVENT_EAPOL_RX
, &event
);
690 static void test_driver_ether(struct wpa_driver_test_data
*drv
,
691 struct sockaddr_un
*from
, socklen_t fromlen
,
692 u8
*data
, size_t datalen
)
694 struct l2_ethhdr
*eth
;
696 if (datalen
< sizeof(*eth
))
699 eth
= (struct l2_ethhdr
*) data
;
700 wpa_printf(MSG_DEBUG
, "test_driver: RX ETHER dst=" MACSTR
" src="
701 MACSTR
" proto=%04x",
702 MAC2STR(eth
->h_dest
), MAC2STR(eth
->h_source
),
703 be_to_host16(eth
->h_proto
));
705 #ifdef CONFIG_IEEE80211R
706 if (be_to_host16(eth
->h_proto
) == ETH_P_RRB
) {
707 union wpa_event_data ev
;
708 os_memset(&ev
, 0, sizeof(ev
));
709 ev
.ft_rrb_rx
.src
= eth
->h_source
;
710 ev
.ft_rrb_rx
.data
= data
+ sizeof(*eth
);
711 ev
.ft_rrb_rx
.data_len
= datalen
- sizeof(*eth
);
713 #endif /* CONFIG_IEEE80211R */
717 static void test_driver_mlme(struct wpa_driver_test_data
*drv
,
718 struct sockaddr_un
*from
, socklen_t fromlen
,
719 u8
*data
, size_t datalen
)
721 struct ieee80211_hdr
*hdr
;
723 union wpa_event_data event
;
724 int freq
= 0, own_freq
;
726 if (datalen
> 6 && os_memcmp(data
, "freq=", 5) == 0) {
728 for (pos
= 5; pos
< datalen
; pos
++) {
729 if (data
[pos
] == ' ')
733 freq
= atoi((const char *) &data
[5]);
734 wpa_printf(MSG_DEBUG
, "test_driver(%s): MLME RX on "
735 "freq %d MHz", drv
->ifname
, freq
);
742 if (drv
->remain_on_channel_freq
)
743 own_freq
= drv
->remain_on_channel_freq
;
745 own_freq
= drv
->current_freq
;
747 if (freq
&& own_freq
&& freq
!= own_freq
) {
748 wpa_printf(MSG_DEBUG
, "test_driver(%s): Ignore MLME RX on "
749 "another frequency %d MHz (own %d MHz)",
750 drv
->ifname
, freq
, own_freq
);
754 hdr
= (struct ieee80211_hdr
*) data
;
756 if (test_driver_get_cli(drv
, from
, fromlen
) == NULL
&& datalen
>= 16) {
757 struct test_client_socket
*cli
;
758 cli
= os_zalloc(sizeof(*cli
));
761 wpa_printf(MSG_DEBUG
, "Adding client entry for " MACSTR
,
762 MAC2STR(hdr
->addr2
));
763 memcpy(cli
->addr
, hdr
->addr2
, ETH_ALEN
);
764 memcpy(&cli
->un
, from
, sizeof(cli
->un
));
765 cli
->unlen
= fromlen
;
766 cli
->next
= drv
->cli
;
770 wpa_hexdump(MSG_MSGDUMP
, "test_driver_mlme: received frame",
772 fc
= le_to_host16(hdr
->frame_control
);
773 if (WLAN_FC_GET_TYPE(fc
) != WLAN_FC_TYPE_MGMT
) {
774 wpa_printf(MSG_ERROR
, "%s: received non-mgmt frame",
779 os_memset(&event
, 0, sizeof(event
));
780 event
.rx_mgmt
.frame
= data
;
781 event
.rx_mgmt
.frame_len
= datalen
;
782 wpa_supplicant_event(drv
->ctx
, EVENT_RX_MGMT
, &event
);
786 static void test_driver_receive_unix(int sock
, void *eloop_ctx
, void *sock_ctx
)
788 struct wpa_driver_test_data
*drv
= eloop_ctx
;
791 struct sockaddr_un from
;
792 socklen_t fromlen
= sizeof(from
);
794 res
= recvfrom(sock
, buf
, sizeof(buf
) - 1, 0,
795 (struct sockaddr
*) &from
, &fromlen
);
797 perror("recvfrom(test_socket)");
802 wpa_printf(MSG_DEBUG
, "test_driver: received %u bytes", res
);
804 if (strncmp(buf
, "SCAN", 4) == 0) {
805 test_driver_scan(drv
, &from
, fromlen
, buf
+ 4);
806 } else if (strncmp(buf
, "ASSOC ", 6) == 0) {
807 test_driver_assoc(drv
, &from
, fromlen
, buf
+ 6);
808 } else if (strcmp(buf
, "DISASSOC") == 0) {
809 test_driver_disassoc(drv
, &from
, fromlen
);
810 } else if (strncmp(buf
, "EAPOL ", 6) == 0) {
811 test_driver_eapol(drv
, &from
, fromlen
, (u8
*) buf
+ 6,
813 } else if (strncmp(buf
, "ETHER ", 6) == 0) {
814 test_driver_ether(drv
, &from
, fromlen
, (u8
*) buf
+ 6,
816 } else if (strncmp(buf
, "MLME ", 5) == 0) {
817 test_driver_mlme(drv
, &from
, fromlen
, (u8
*) buf
+ 5, res
- 5);
819 wpa_hexdump_ascii(MSG_DEBUG
, "Unknown test_socket command",
825 static struct test_driver_bss
*
826 test_driver_get_bss(struct wpa_driver_test_data
*drv
, const char *ifname
)
828 struct test_driver_bss
*bss
;
830 for (bss
= drv
->bss
; bss
; bss
= bss
->next
) {
831 if (os_strcmp(bss
->ifname
, ifname
) == 0)
838 static int test_driver_set_generic_elem(const char *ifname
, void *priv
,
839 const u8
*elem
, size_t elem_len
)
841 struct wpa_driver_test_data
*drv
= priv
;
842 struct test_driver_bss
*bss
;
844 bss
= test_driver_get_bss(drv
, ifname
);
856 bss
->ie
= os_malloc(elem_len
);
857 if (bss
->ie
== NULL
) {
862 memcpy(bss
->ie
, elem
, elem_len
);
863 bss
->ielen
= elem_len
;
868 static int test_driver_set_ap_wps_ie(const char *ifname
, void *priv
,
869 const struct wpabuf
*beacon
,
870 const struct wpabuf
*proberesp
)
872 struct wpa_driver_test_data
*drv
= priv
;
873 struct test_driver_bss
*bss
;
875 bss
= test_driver_get_bss(drv
, ifname
);
880 wpa_printf(MSG_DEBUG
, "test_driver: Clear Beacon WPS IE");
882 wpa_hexdump_buf(MSG_DEBUG
, "test_driver: Beacon WPS IE",
885 os_free(bss
->wps_beacon_ie
);
887 if (beacon
== NULL
) {
888 bss
->wps_beacon_ie
= NULL
;
889 bss
->wps_beacon_ie_len
= 0;
891 bss
->wps_beacon_ie
= os_malloc(wpabuf_len(beacon
));
892 if (bss
->wps_beacon_ie
== NULL
) {
893 bss
->wps_beacon_ie_len
= 0;
897 os_memcpy(bss
->wps_beacon_ie
, wpabuf_head(beacon
),
899 bss
->wps_beacon_ie_len
= wpabuf_len(beacon
);
902 if (proberesp
== NULL
)
903 wpa_printf(MSG_DEBUG
, "test_driver: Clear Probe Response WPS "
906 wpa_hexdump_buf(MSG_DEBUG
, "test_driver: Probe Response WPS "
909 os_free(bss
->wps_probe_resp_ie
);
911 if (proberesp
== NULL
) {
912 bss
->wps_probe_resp_ie
= NULL
;
913 bss
->wps_probe_resp_ie_len
= 0;
915 bss
->wps_probe_resp_ie
= os_malloc(wpabuf_len(proberesp
));
916 if (bss
->wps_probe_resp_ie
== NULL
) {
917 bss
->wps_probe_resp_ie_len
= 0;
921 os_memcpy(bss
->wps_probe_resp_ie
, wpabuf_head(proberesp
),
922 wpabuf_len(proberesp
));
923 bss
->wps_probe_resp_ie_len
= wpabuf_len(proberesp
);
930 static int test_driver_sta_deauth(void *priv
, const u8
*own_addr
,
931 const u8
*addr
, int reason
)
933 struct wpa_driver_test_data
*drv
= priv
;
934 struct test_client_socket
*cli
;
936 if (drv
->test_socket
< 0)
941 if (memcmp(cli
->addr
, addr
, ETH_ALEN
) == 0)
949 return sendto(drv
->test_socket
, "DEAUTH", 6, 0,
950 (struct sockaddr
*) &cli
->un
, cli
->unlen
);
954 static int test_driver_sta_disassoc(void *priv
, const u8
*own_addr
,
955 const u8
*addr
, int reason
)
957 struct wpa_driver_test_data
*drv
= priv
;
958 struct test_client_socket
*cli
;
960 if (drv
->test_socket
< 0)
965 if (memcmp(cli
->addr
, addr
, ETH_ALEN
) == 0)
973 return sendto(drv
->test_socket
, "DISASSOC", 8, 0,
974 (struct sockaddr
*) &cli
->un
, cli
->unlen
);
978 static int test_driver_bss_add(void *priv
, const char *ifname
, const u8
*bssid
,
981 struct wpa_driver_test_data
*drv
= priv
;
982 struct test_driver_bss
*bss
;
984 wpa_printf(MSG_DEBUG
, "%s(ifname=%s bssid=" MACSTR
")",
985 __func__
, ifname
, MAC2STR(bssid
));
987 bss
= os_zalloc(sizeof(*bss
));
991 bss
->bss_ctx
= bss_ctx
;
992 os_strlcpy(bss
->ifname
, ifname
, IFNAMSIZ
);
993 memcpy(bss
->bssid
, bssid
, ETH_ALEN
);
995 bss
->next
= drv
->bss
;
997 drv
->global
->bss_add_used
= 1;
998 os_memcpy(drv
->global
->req_addr
, bssid
, ETH_ALEN
);
1004 static int test_driver_bss_remove(void *priv
, const char *ifname
)
1006 struct wpa_driver_test_data
*drv
= priv
;
1007 struct test_driver_bss
*bss
, *prev
;
1008 struct test_client_socket
*cli
, *prev_c
;
1010 wpa_printf(MSG_DEBUG
, "%s(ifname=%s)", __func__
, ifname
);
1012 for (prev
= NULL
, bss
= drv
->bss
; bss
; prev
= bss
, bss
= bss
->next
) {
1013 if (strcmp(bss
->ifname
, ifname
) != 0)
1017 prev
->next
= bss
->next
;
1019 drv
->bss
= bss
->next
;
1021 for (prev_c
= NULL
, cli
= drv
->cli
; cli
;
1022 prev_c
= cli
, cli
= cli
->next
) {
1023 if (cli
->bss
!= bss
)
1026 prev_c
->next
= cli
->next
;
1028 drv
->cli
= cli
->next
;
1033 test_driver_free_bss(bss
);
1041 static int test_driver_if_add(const char *iface
, void *priv
,
1042 enum wpa_driver_if_type type
, const char *ifname
,
1043 const u8
*addr
, void *bss_ctx
)
1045 wpa_printf(MSG_DEBUG
, "%s(iface=%s type=%d ifname=%s bss_ctx=%p)",
1046 __func__
, iface
, type
, ifname
, bss_ctx
);
1047 if (type
== WPA_IF_AP_BSS
)
1048 return test_driver_bss_add(priv
, ifname
, addr
, bss_ctx
);
1053 static int test_driver_if_remove(void *priv
, enum wpa_driver_if_type type
,
1056 wpa_printf(MSG_DEBUG
, "%s(type=%d ifname=%s)", __func__
, type
, ifname
);
1057 if (type
== WPA_IF_AP_BSS
)
1058 return test_driver_bss_remove(priv
, ifname
);
1063 static int test_driver_valid_bss_mask(void *priv
, const u8
*addr
,
1070 static int test_driver_set_ssid(const char *ifname
, void *priv
, const u8
*buf
,
1073 struct wpa_driver_test_data
*drv
= priv
;
1074 struct test_driver_bss
*bss
;
1076 wpa_printf(MSG_DEBUG
, "%s(ifname=%s)", __func__
, ifname
);
1077 wpa_hexdump_ascii(MSG_DEBUG
, "test_driver_set_ssid: SSID", buf
, len
);
1079 bss
= test_driver_get_bss(drv
, ifname
);
1081 wpa_printf(MSG_DEBUG
, "%s(ifname=%s): failed to find BSS data",
1086 if (len
< 0 || (size_t) len
> sizeof(bss
->ssid
))
1089 os_memcpy(bss
->ssid
, buf
, len
);
1090 bss
->ssid_len
= len
;
1096 static int test_driver_set_privacy(const char *ifname
, void *priv
, int enabled
)
1098 struct wpa_driver_test_data
*drv
= priv
;
1099 struct test_driver_bss
*bss
;
1101 wpa_printf(MSG_DEBUG
, "%s(ifname=%s enabled=%d)",
1102 __func__
, ifname
, enabled
);
1104 bss
= test_driver_get_bss(drv
, ifname
);
1108 bss
->privacy
= enabled
;
1114 static int test_driver_set_sta_vlan(void *priv
, const u8
*addr
,
1115 const char *ifname
, int vlan_id
)
1117 wpa_printf(MSG_DEBUG
, "%s(addr=" MACSTR
" ifname=%s vlan_id=%d)",
1118 __func__
, MAC2STR(addr
), ifname
, vlan_id
);
1123 static int test_driver_sta_add(const char *ifname
, void *priv
,
1124 struct hostapd_sta_add_params
*params
)
1126 struct wpa_driver_test_data
*drv
= priv
;
1127 struct test_client_socket
*cli
;
1128 struct test_driver_bss
*bss
;
1130 wpa_printf(MSG_DEBUG
, "%s(ifname=%s addr=" MACSTR
" aid=%d "
1131 "capability=0x%x listen_interval=%d)",
1132 __func__
, ifname
, MAC2STR(params
->addr
), params
->aid
,
1133 params
->capability
, params
->listen_interval
);
1134 wpa_hexdump(MSG_DEBUG
, "test_driver_sta_add - supp_rates",
1135 params
->supp_rates
, params
->supp_rates_len
);
1139 if (os_memcmp(cli
->addr
, params
->addr
, ETH_ALEN
) == 0)
1144 wpa_printf(MSG_DEBUG
, "%s: no matching client entry",
1149 bss
= test_driver_get_bss(drv
, ifname
);
1151 wpa_printf(MSG_DEBUG
, "%s: No matching interface found from "
1152 "configured BSSes", __func__
);
1162 static struct wpa_driver_test_data
* test_alloc_data(void *ctx
,
1165 struct wpa_driver_test_data
*drv
;
1167 drv
= os_zalloc(sizeof(struct wpa_driver_test_data
));
1169 wpa_printf(MSG_ERROR
, "Could not allocate memory for test "
1175 wpa_trace_add_ref(drv
, ctx
, ctx
);
1176 os_strlcpy(drv
->ifname
, ifname
, IFNAMSIZ
);
1178 /* Generate a MAC address to help testing with multiple STAs */
1179 drv
->own_addr
[0] = 0x02; /* locally administered */
1180 sha1_prf((const u8
*) ifname
, os_strlen(ifname
),
1181 "test mac addr generation",
1182 NULL
, 0, drv
->own_addr
+ 1, ETH_ALEN
- 1);
1188 static void * test_driver_init(struct hostapd_data
*hapd
,
1189 struct wpa_init_params
*params
)
1191 struct wpa_driver_test_data
*drv
;
1192 struct sockaddr_un addr_un
;
1193 struct sockaddr_in addr_in
;
1194 struct sockaddr
*addr
;
1197 drv
= test_alloc_data(hapd
, params
->ifname
);
1201 drv
->bss
= os_zalloc(sizeof(*drv
->bss
));
1202 if (drv
->bss
== NULL
) {
1203 wpa_printf(MSG_ERROR
, "Could not allocate memory for test "
1209 drv
->bss
->bss_ctx
= hapd
;
1210 os_strlcpy(drv
->bss
->ifname
, params
->ifname
, IFNAMSIZ
);
1211 os_memcpy(drv
->bss
->bssid
, drv
->own_addr
, ETH_ALEN
);
1212 os_memcpy(params
->own_addr
, drv
->own_addr
, ETH_ALEN
);
1214 if (params
->test_socket
) {
1215 if (os_strlen(params
->test_socket
) >=
1216 sizeof(addr_un
.sun_path
)) {
1217 printf("Too long test_socket path\n");
1218 wpa_driver_test_deinit(drv
);
1221 if (strncmp(params
->test_socket
, "DIR:", 4) == 0) {
1222 size_t len
= strlen(params
->test_socket
) + 30;
1223 drv
->test_dir
= os_strdup(params
->test_socket
+ 4);
1224 drv
->own_socket_path
= os_malloc(len
);
1225 if (drv
->own_socket_path
) {
1226 snprintf(drv
->own_socket_path
, len
,
1228 params
->test_socket
+ 4,
1229 MAC2STR(params
->own_addr
));
1231 } else if (strncmp(params
->test_socket
, "UDP:", 4) == 0) {
1232 drv
->udp_port
= atoi(params
->test_socket
+ 4);
1234 drv
->own_socket_path
= os_strdup(params
->test_socket
);
1236 if (drv
->own_socket_path
== NULL
&& drv
->udp_port
== 0) {
1237 wpa_driver_test_deinit(drv
);
1241 drv
->test_socket
= socket(drv
->udp_port
? PF_INET
: PF_UNIX
,
1243 if (drv
->test_socket
< 0) {
1245 wpa_driver_test_deinit(drv
);
1249 if (drv
->udp_port
) {
1250 os_memset(&addr_in
, 0, sizeof(addr_in
));
1251 addr_in
.sin_family
= AF_INET
;
1252 addr_in
.sin_port
= htons(drv
->udp_port
);
1253 addr
= (struct sockaddr
*) &addr_in
;
1254 alen
= sizeof(addr_in
);
1256 os_memset(&addr_un
, 0, sizeof(addr_un
));
1257 addr_un
.sun_family
= AF_UNIX
;
1258 os_strlcpy(addr_un
.sun_path
, drv
->own_socket_path
,
1259 sizeof(addr_un
.sun_path
));
1260 addr
= (struct sockaddr
*) &addr_un
;
1261 alen
= sizeof(addr_un
);
1263 if (bind(drv
->test_socket
, addr
, alen
) < 0) {
1264 perror("bind(PF_UNIX)");
1265 close(drv
->test_socket
);
1266 if (drv
->own_socket_path
)
1267 unlink(drv
->own_socket_path
);
1268 wpa_driver_test_deinit(drv
);
1271 eloop_register_read_sock(drv
->test_socket
,
1272 test_driver_receive_unix
, drv
, NULL
);
1274 drv
->test_socket
= -1;
1280 static void wpa_driver_test_poll(void *eloop_ctx
, void *timeout_ctx
)
1282 struct wpa_driver_test_data
*drv
= eloop_ctx
;
1284 #ifdef DRIVER_TEST_UNIX
1285 if (drv
->associated
&& drv
->hostapd_addr_set
) {
1287 if (stat(drv
->hostapd_addr
.sun_path
, &st
) < 0) {
1288 wpa_printf(MSG_DEBUG
, "%s: lost connection to AP: %s",
1289 __func__
, strerror(errno
));
1290 drv
->associated
= 0;
1291 wpa_supplicant_event(drv
->ctx
, EVENT_DISASSOC
, NULL
);
1294 #endif /* DRIVER_TEST_UNIX */
1296 eloop_register_timeout(1, 0, wpa_driver_test_poll
, drv
, NULL
);
1300 static void wpa_driver_test_scan_timeout(void *eloop_ctx
, void *timeout_ctx
)
1302 wpa_printf(MSG_DEBUG
, "Scan timeout - try to get results");
1303 wpa_supplicant_event(timeout_ctx
, EVENT_SCAN_RESULTS
, NULL
);
1307 #ifdef DRIVER_TEST_UNIX
1308 static void wpa_driver_scan_dir(struct wpa_driver_test_data
*drv
,
1311 struct dirent
*dent
;
1313 struct sockaddr_un addr
;
1314 char cmd
[512], *pos
, *end
;
1317 dir
= opendir(path
);
1321 end
= cmd
+ sizeof(cmd
);
1323 ret
= os_snprintf(pos
, end
- pos
, "SCAN " MACSTR
,
1324 MAC2STR(drv
->own_addr
));
1325 if (ret
>= 0 && ret
< end
- pos
)
1327 if (drv
->probe_req_ie
) {
1328 ret
= os_snprintf(pos
, end
- pos
, " ");
1329 if (ret
>= 0 && ret
< end
- pos
)
1331 pos
+= wpa_snprintf_hex(pos
, end
- pos
, drv
->probe_req_ie
,
1332 drv
->probe_req_ie_len
);
1336 while ((dent
= readdir(dir
))) {
1337 if (os_strncmp(dent
->d_name
, "AP-", 3) != 0 &&
1338 os_strncmp(dent
->d_name
, "STA-", 4) != 0)
1340 if (drv
->own_socket_path
) {
1342 olen
= os_strlen(drv
->own_socket_path
);
1343 dlen
= os_strlen(dent
->d_name
);
1345 os_strcmp(dent
->d_name
,
1346 drv
->own_socket_path
+ olen
- dlen
) == 0)
1349 wpa_printf(MSG_DEBUG
, "%s: SCAN %s", __func__
, dent
->d_name
);
1351 os_memset(&addr
, 0, sizeof(addr
));
1352 addr
.sun_family
= AF_UNIX
;
1353 os_snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s/%s",
1354 path
, dent
->d_name
);
1356 if (sendto(drv
->test_socket
, cmd
, os_strlen(cmd
), 0,
1357 (struct sockaddr
*) &addr
, sizeof(addr
)) < 0) {
1358 perror("sendto(test_socket)");
1363 #endif /* DRIVER_TEST_UNIX */
1366 static int wpa_driver_test_scan(void *priv
,
1367 struct wpa_driver_scan_params
*params
)
1369 struct wpa_driver_test_data
*drv
= priv
;
1372 wpa_printf(MSG_DEBUG
, "%s: priv=%p", __func__
, priv
);
1374 os_free(drv
->probe_req_ie
);
1375 if (params
->extra_ies
) {
1376 drv
->probe_req_ie
= os_malloc(params
->extra_ies_len
);
1377 if (drv
->probe_req_ie
== NULL
) {
1378 drv
->probe_req_ie_len
= 0;
1381 os_memcpy(drv
->probe_req_ie
, params
->extra_ies
,
1382 params
->extra_ies_len
);
1383 drv
->probe_req_ie_len
= params
->extra_ies_len
;
1385 drv
->probe_req_ie
= NULL
;
1386 drv
->probe_req_ie_len
= 0;
1389 for (i
= 0; i
< params
->num_ssids
; i
++)
1390 wpa_hexdump(MSG_DEBUG
, "Scan SSID",
1391 params
->ssids
[i
].ssid
, params
->ssids
[i
].ssid_len
);
1392 wpa_hexdump(MSG_DEBUG
, "Scan extra IE(s)",
1393 params
->extra_ies
, params
->extra_ies_len
);
1395 drv
->num_scanres
= 0;
1397 #ifdef DRIVER_TEST_UNIX
1398 if (drv
->test_socket
>= 0 && drv
->test_dir
)
1399 wpa_driver_scan_dir(drv
, drv
->test_dir
);
1401 if (drv
->test_socket
>= 0 && drv
->hostapd_addr_set
&&
1402 sendto(drv
->test_socket
, "SCAN", 4, 0,
1403 (struct sockaddr
*) &drv
->hostapd_addr
,
1404 sizeof(drv
->hostapd_addr
)) < 0) {
1405 perror("sendto(test_socket)");
1407 #endif /* DRIVER_TEST_UNIX */
1409 if (drv
->test_socket
>= 0 && drv
->hostapd_addr_udp_set
&&
1410 sendto(drv
->test_socket
, "SCAN", 4, 0,
1411 (struct sockaddr
*) &drv
->hostapd_addr_udp
,
1412 sizeof(drv
->hostapd_addr_udp
)) < 0) {
1413 perror("sendto(test_socket)");
1416 eloop_cancel_timeout(wpa_driver_test_scan_timeout
, drv
, drv
->ctx
);
1417 eloop_register_timeout(1, 0, wpa_driver_test_scan_timeout
, drv
,
1423 static struct wpa_scan_results
* wpa_driver_test_get_scan_results2(void *priv
)
1425 struct wpa_driver_test_data
*drv
= priv
;
1426 struct wpa_scan_results
*res
;
1429 res
= os_zalloc(sizeof(*res
));
1433 res
->res
= os_zalloc(drv
->num_scanres
* sizeof(struct wpa_scan_res
*));
1434 if (res
->res
== NULL
) {
1439 for (i
= 0; i
< drv
->num_scanres
; i
++) {
1440 struct wpa_scan_res
*r
;
1441 if (drv
->scanres
[i
] == NULL
)
1443 r
= os_malloc(sizeof(*r
) + drv
->scanres
[i
]->ie_len
);
1446 os_memcpy(r
, drv
->scanres
[i
],
1447 sizeof(*r
) + drv
->scanres
[i
]->ie_len
);
1448 res
->res
[res
->num
++] = r
;
1455 static int wpa_driver_test_set_key(const char *ifname
, void *priv
,
1456 enum wpa_alg alg
, const u8
*addr
,
1457 int key_idx
, int set_tx
,
1458 const u8
*seq
, size_t seq_len
,
1459 const u8
*key
, size_t key_len
)
1461 wpa_printf(MSG_DEBUG
, "%s: ifname=%s priv=%p alg=%d key_idx=%d "
1463 __func__
, ifname
, priv
, alg
, key_idx
, set_tx
);
1465 wpa_printf(MSG_DEBUG
, " addr=" MACSTR
, MAC2STR(addr
));
1467 wpa_hexdump(MSG_DEBUG
, " seq", seq
, seq_len
);
1469 wpa_hexdump_key(MSG_DEBUG
, " key", key
, key_len
);
1474 static int wpa_driver_update_mode(struct wpa_driver_test_data
*drv
, int ap
)
1476 if (ap
&& !drv
->ap
) {
1477 wpa_driver_test_close_test_socket(drv
);
1478 wpa_driver_test_attach(drv
, drv
->test_dir
, 1);
1480 } else if (!ap
&& drv
->ap
) {
1481 wpa_driver_test_close_test_socket(drv
);
1482 wpa_driver_test_attach(drv
, drv
->test_dir
, 0);
1490 static int wpa_driver_test_associate(
1491 void *priv
, struct wpa_driver_associate_params
*params
)
1493 struct wpa_driver_test_data
*drv
= priv
;
1494 wpa_printf(MSG_DEBUG
, "%s: priv=%p freq=%d pairwise_suite=%d "
1495 "group_suite=%d key_mgmt_suite=%d auth_alg=%d mode=%d",
1496 __func__
, priv
, params
->freq
, params
->pairwise_suite
,
1497 params
->group_suite
, params
->key_mgmt_suite
,
1498 params
->auth_alg
, params
->mode
);
1499 if (params
->bssid
) {
1500 wpa_printf(MSG_DEBUG
, " bssid=" MACSTR
,
1501 MAC2STR(params
->bssid
));
1504 wpa_hexdump_ascii(MSG_DEBUG
, " ssid",
1505 params
->ssid
, params
->ssid_len
);
1507 if (params
->wpa_ie
) {
1508 wpa_hexdump(MSG_DEBUG
, " wpa_ie",
1509 params
->wpa_ie
, params
->wpa_ie_len
);
1510 drv
->assoc_wpa_ie_len
= params
->wpa_ie_len
;
1511 if (drv
->assoc_wpa_ie_len
> sizeof(drv
->assoc_wpa_ie
))
1512 drv
->assoc_wpa_ie_len
= sizeof(drv
->assoc_wpa_ie
);
1513 os_memcpy(drv
->assoc_wpa_ie
, params
->wpa_ie
,
1514 drv
->assoc_wpa_ie_len
);
1516 drv
->assoc_wpa_ie_len
= 0;
1518 wpa_driver_update_mode(drv
, params
->mode
== IEEE80211_MODE_AP
);
1520 drv
->ibss
= params
->mode
== IEEE80211_MODE_IBSS
;
1521 drv
->privacy
= params
->key_mgmt_suite
&
1522 (WPA_KEY_MGMT_IEEE8021X
|
1524 WPA_KEY_MGMT_WPA_NONE
|
1525 WPA_KEY_MGMT_FT_IEEE8021X
|
1526 WPA_KEY_MGMT_FT_PSK
|
1527 WPA_KEY_MGMT_IEEE8021X_SHA256
|
1528 WPA_KEY_MGMT_PSK_SHA256
);
1529 if (params
->wep_key_len
[params
->wep_tx_keyidx
])
1532 #ifdef DRIVER_TEST_UNIX
1533 if (drv
->test_dir
&& params
->bssid
&&
1534 params
->mode
!= IEEE80211_MODE_IBSS
) {
1535 os_memset(&drv
->hostapd_addr
, 0, sizeof(drv
->hostapd_addr
));
1536 drv
->hostapd_addr
.sun_family
= AF_UNIX
;
1537 os_snprintf(drv
->hostapd_addr
.sun_path
,
1538 sizeof(drv
->hostapd_addr
.sun_path
),
1540 drv
->test_dir
, MAC2STR(params
->bssid
));
1541 drv
->hostapd_addr_set
= 1;
1543 #endif /* DRIVER_TEST_UNIX */
1545 if (params
->mode
== IEEE80211_MODE_AP
) {
1546 struct test_driver_bss
*bss
;
1547 os_memcpy(drv
->ssid
, params
->ssid
, params
->ssid_len
);
1548 drv
->ssid_len
= params
->ssid_len
;
1550 test_driver_free_bsses(drv
);
1551 bss
= drv
->bss
= os_zalloc(sizeof(*drv
->bss
));
1554 os_strlcpy(bss
->ifname
, drv
->ifname
, IFNAMSIZ
);
1555 os_memcpy(bss
->bssid
, drv
->own_addr
, ETH_ALEN
);
1556 os_memcpy(bss
->ssid
, params
->ssid
, params
->ssid_len
);
1557 bss
->ssid_len
= params
->ssid_len
;
1558 bss
->privacy
= drv
->privacy
;
1559 if (params
->wpa_ie
&& params
->wpa_ie_len
) {
1560 bss
->ie
= os_malloc(params
->wpa_ie_len
);
1562 os_memcpy(bss
->ie
, params
->wpa_ie
,
1563 params
->wpa_ie_len
);
1564 bss
->ielen
= params
->wpa_ie_len
;
1567 } else if (drv
->test_socket
>= 0 &&
1568 (drv
->hostapd_addr_set
|| drv
->hostapd_addr_udp_set
)) {
1569 char cmd
[200], *pos
, *end
;
1571 end
= cmd
+ sizeof(cmd
);
1573 ret
= os_snprintf(pos
, end
- pos
, "ASSOC " MACSTR
" ",
1574 MAC2STR(drv
->own_addr
));
1575 if (ret
>= 0 && ret
< end
- pos
)
1577 pos
+= wpa_snprintf_hex(pos
, end
- pos
, params
->ssid
,
1579 ret
= os_snprintf(pos
, end
- pos
, " ");
1580 if (ret
>= 0 && ret
< end
- pos
)
1582 pos
+= wpa_snprintf_hex(pos
, end
- pos
, params
->wpa_ie
,
1583 params
->wpa_ie_len
);
1585 #ifdef DRIVER_TEST_UNIX
1586 if (drv
->hostapd_addr_set
&&
1587 sendto(drv
->test_socket
, cmd
, os_strlen(cmd
), 0,
1588 (struct sockaddr
*) &drv
->hostapd_addr
,
1589 sizeof(drv
->hostapd_addr
)) < 0) {
1590 perror("sendto(test_socket)");
1593 #endif /* DRIVER_TEST_UNIX */
1594 if (drv
->hostapd_addr_udp_set
&&
1595 sendto(drv
->test_socket
, cmd
, os_strlen(cmd
), 0,
1596 (struct sockaddr
*) &drv
->hostapd_addr_udp
,
1597 sizeof(drv
->hostapd_addr_udp
)) < 0) {
1598 perror("sendto(test_socket)");
1602 os_memcpy(drv
->ssid
, params
->ssid
, params
->ssid_len
);
1603 drv
->ssid_len
= params
->ssid_len
;
1605 drv
->associated
= 1;
1606 if (params
->mode
== IEEE80211_MODE_IBSS
) {
1607 os_memcpy(drv
->ssid
, params
->ssid
, params
->ssid_len
);
1608 drv
->ssid_len
= params
->ssid_len
;
1610 os_memcpy(drv
->bssid
, params
->bssid
, ETH_ALEN
);
1612 os_get_random(drv
->bssid
, ETH_ALEN
);
1613 drv
->bssid
[0] &= ~0x01;
1614 drv
->bssid
[0] |= 0x02;
1617 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC
, NULL
);
1624 static int wpa_driver_test_get_bssid(void *priv
, u8
*bssid
)
1626 struct wpa_driver_test_data
*drv
= priv
;
1627 os_memcpy(bssid
, drv
->bssid
, ETH_ALEN
);
1632 static int wpa_driver_test_get_ssid(void *priv
, u8
*ssid
)
1634 struct wpa_driver_test_data
*drv
= priv
;
1635 os_memcpy(ssid
, drv
->ssid
, 32);
1636 return drv
->ssid_len
;
1640 static int wpa_driver_test_send_disassoc(struct wpa_driver_test_data
*drv
)
1642 #ifdef DRIVER_TEST_UNIX
1643 if (drv
->test_socket
>= 0 &&
1644 sendto(drv
->test_socket
, "DISASSOC", 8, 0,
1645 (struct sockaddr
*) &drv
->hostapd_addr
,
1646 sizeof(drv
->hostapd_addr
)) < 0) {
1647 perror("sendto(test_socket)");
1650 #endif /* DRIVER_TEST_UNIX */
1651 if (drv
->test_socket
>= 0 && drv
->hostapd_addr_udp_set
&&
1652 sendto(drv
->test_socket
, "DISASSOC", 8, 0,
1653 (struct sockaddr
*) &drv
->hostapd_addr_udp
,
1654 sizeof(drv
->hostapd_addr_udp
)) < 0) {
1655 perror("sendto(test_socket)");
1662 static int wpa_driver_test_deauthenticate(void *priv
, const u8
*addr
,
1665 struct wpa_driver_test_data
*drv
= priv
;
1666 wpa_printf(MSG_DEBUG
, "%s addr=" MACSTR
" reason_code=%d",
1667 __func__
, MAC2STR(addr
), reason_code
);
1668 os_memset(drv
->bssid
, 0, ETH_ALEN
);
1669 drv
->associated
= 0;
1670 wpa_supplicant_event(drv
->ctx
, EVENT_DISASSOC
, NULL
);
1671 return wpa_driver_test_send_disassoc(drv
);
1675 static int wpa_driver_test_disassociate(void *priv
, const u8
*addr
,
1678 struct wpa_driver_test_data
*drv
= priv
;
1679 wpa_printf(MSG_DEBUG
, "%s addr=" MACSTR
" reason_code=%d",
1680 __func__
, MAC2STR(addr
), reason_code
);
1681 os_memset(drv
->bssid
, 0, ETH_ALEN
);
1682 drv
->associated
= 0;
1683 wpa_supplicant_event(drv
->ctx
, EVENT_DISASSOC
, NULL
);
1684 return wpa_driver_test_send_disassoc(drv
);
1688 static void wpa_driver_test_scanresp(struct wpa_driver_test_data
*drv
,
1689 struct sockaddr
*from
,
1693 struct wpa_scan_res
*res
;
1694 const char *pos
, *pos2
;
1696 u8
*ie_pos
, *ie_start
, *ie_end
;
1697 #define MAX_IE_LEN 1000
1699 wpa_printf(MSG_DEBUG
, "test_driver: SCANRESP %s", data
);
1700 if (drv
->num_scanres
>= MAX_SCAN_RESULTS
) {
1701 wpa_printf(MSG_DEBUG
, "test_driver: No room for the new scan "
1706 /* SCANRESP BSSID SSID IEs */
1708 res
= os_zalloc(sizeof(*res
) + MAX_IE_LEN
);
1711 ie_start
= ie_pos
= (u8
*) (res
+ 1);
1712 ie_end
= ie_pos
+ MAX_IE_LEN
;
1714 if (hwaddr_aton(data
, res
->bssid
)) {
1715 wpa_printf(MSG_DEBUG
, "test_driver: invalid BSSID in scanres");
1723 pos2
= os_strchr(pos
, ' ');
1725 wpa_printf(MSG_DEBUG
, "test_driver: invalid SSID termination "
1730 len
= (pos2
- pos
) / 2;
1734 * Generate SSID IE from the SSID field since this IE is not included
1735 * in the main IE field.
1737 *ie_pos
++ = WLAN_EID_SSID
;
1739 if (hexstr2bin(pos
, ie_pos
, len
) < 0) {
1740 wpa_printf(MSG_DEBUG
, "test_driver: invalid SSID in scanres");
1747 pos2
= os_strchr(pos
, ' ');
1749 len
= os_strlen(pos
) / 2;
1751 len
= (pos2
- pos
) / 2;
1752 if ((int) len
> ie_end
- ie_pos
)
1753 len
= ie_end
- ie_pos
;
1754 if (hexstr2bin(pos
, ie_pos
, len
) < 0) {
1755 wpa_printf(MSG_DEBUG
, "test_driver: invalid IEs in scanres");
1760 res
->ie_len
= ie_pos
- ie_start
;
1766 if (os_strstr(pos
, "PRIVACY"))
1767 res
->caps
|= IEEE80211_CAP_PRIVACY
;
1768 if (os_strstr(pos
, "IBSS"))
1769 res
->caps
|= IEEE80211_CAP_IBSS
;
1772 os_free(drv
->scanres
[drv
->num_scanres
]);
1773 drv
->scanres
[drv
->num_scanres
++] = res
;
1777 static void wpa_driver_test_assocresp(struct wpa_driver_test_data
*drv
,
1778 struct sockaddr
*from
,
1782 /* ASSOCRESP BSSID <res> */
1783 if (hwaddr_aton(data
, drv
->bssid
)) {
1784 wpa_printf(MSG_DEBUG
, "test_driver: invalid BSSID in "
1787 if (drv
->use_associnfo
) {
1788 union wpa_event_data event
;
1789 os_memset(&event
, 0, sizeof(event
));
1790 event
.assoc_info
.req_ies
= drv
->assoc_wpa_ie
;
1791 event
.assoc_info
.req_ies_len
= drv
->assoc_wpa_ie_len
;
1792 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOCINFO
, &event
);
1794 drv
->associated
= 1;
1795 wpa_supplicant_event(drv
->ctx
, EVENT_ASSOC
, NULL
);
1799 static void wpa_driver_test_disassoc(struct wpa_driver_test_data
*drv
,
1800 struct sockaddr
*from
,
1803 drv
->associated
= 0;
1804 wpa_supplicant_event(drv
->ctx
, EVENT_DISASSOC
, NULL
);
1808 static void wpa_driver_test_eapol(struct wpa_driver_test_data
*drv
,
1809 struct sockaddr
*from
,
1811 const u8
*data
, size_t data_len
)
1813 const u8
*src
= drv
->bssid
;
1814 union wpa_event_data event
;
1816 if (data_len
> 14) {
1817 /* Skip Ethernet header */
1818 src
= data
+ ETH_ALEN
;
1823 os_memset(&event
, 0, sizeof(event
));
1824 event
.eapol_rx
.src
= src
;
1825 event
.eapol_rx
.data
= data
;
1826 event
.eapol_rx
.data_len
= data_len
;
1827 wpa_supplicant_event(drv
->ctx
, EVENT_EAPOL_RX
, &event
);
1831 static void wpa_driver_test_mlme(struct wpa_driver_test_data
*drv
,
1832 struct sockaddr
*from
,
1834 const u8
*data
, size_t data_len
)
1836 int freq
= 0, own_freq
;
1837 union wpa_event_data event
;
1839 if (data_len
> 6 && os_memcmp(data
, "freq=", 5) == 0) {
1841 for (pos
= 5; pos
< data_len
; pos
++) {
1842 if (data
[pos
] == ' ')
1845 if (pos
< data_len
) {
1846 freq
= atoi((const char *) &data
[5]);
1847 wpa_printf(MSG_DEBUG
, "test_driver(%s): MLME RX on "
1848 "freq %d MHz", drv
->ifname
, freq
);
1855 if (drv
->remain_on_channel_freq
)
1856 own_freq
= drv
->remain_on_channel_freq
;
1858 own_freq
= drv
->current_freq
;
1860 if (freq
&& own_freq
&& freq
!= own_freq
) {
1861 wpa_printf(MSG_DEBUG
, "test_driver(%s): Ignore MLME RX on "
1862 "another frequency %d MHz (own %d MHz)",
1863 drv
->ifname
, freq
, own_freq
);
1867 os_memset(&event
, 0, sizeof(event
));
1868 event
.mlme_rx
.buf
= data
;
1869 event
.mlme_rx
.len
= data_len
;
1870 event
.mlme_rx
.freq
= freq
;
1871 wpa_supplicant_event(drv
->ctx
, EVENT_MLME_RX
, &event
);
1873 if (drv
->probe_req_report
&& data_len
>= 24) {
1874 const struct ieee80211_mgmt
*mgmt
;
1877 mgmt
= (const struct ieee80211_mgmt
*) data
;
1878 fc
= le_to_host16(mgmt
->frame_control
);
1879 if (WLAN_FC_GET_TYPE(fc
) == WLAN_FC_TYPE_MGMT
&&
1880 WLAN_FC_GET_STYPE(fc
) == WLAN_FC_STYPE_PROBE_REQ
) {
1881 os_memset(&event
, 0, sizeof(event
));
1882 event
.rx_probe_req
.sa
= mgmt
->sa
;
1883 event
.rx_probe_req
.ie
= mgmt
->u
.probe_req
.variable
;
1884 event
.rx_probe_req
.ie_len
=
1885 data_len
- (mgmt
->u
.probe_req
.variable
- data
);
1886 wpa_supplicant_event(drv
->ctx
, EVENT_RX_PROBE_REQ
,
1893 static void wpa_driver_test_scan_cmd(struct wpa_driver_test_data
*drv
,
1894 struct sockaddr
*from
,
1896 const u8
*data
, size_t data_len
)
1898 char buf
[512], *pos
, *end
;
1901 /* data: optional [ STA-addr | ' ' | IEs(hex) ] */
1907 end
= buf
+ sizeof(buf
);
1909 /* reply: SCANRESP BSSID SSID IEs */
1910 ret
= snprintf(pos
, end
- pos
, "SCANRESP " MACSTR
" ",
1911 MAC2STR(drv
->bssid
));
1912 if (ret
< 0 || ret
>= end
- pos
)
1915 pos
+= wpa_snprintf_hex(pos
, end
- pos
,
1916 drv
->ssid
, drv
->ssid_len
);
1917 ret
= snprintf(pos
, end
- pos
, " ");
1918 if (ret
< 0 || ret
>= end
- pos
)
1921 pos
+= wpa_snprintf_hex(pos
, end
- pos
, drv
->assoc_wpa_ie
,
1922 drv
->assoc_wpa_ie_len
);
1925 ret
= snprintf(pos
, end
- pos
, " PRIVACY");
1926 if (ret
< 0 || ret
>= end
- pos
)
1931 ret
= snprintf(pos
, end
- pos
, " IBSS");
1932 if (ret
< 0 || ret
>= end
- pos
)
1936 sendto(drv
->test_socket
, buf
, pos
- buf
, 0,
1937 (struct sockaddr
*) from
, fromlen
);
1941 static void wpa_driver_test_receive_unix(int sock
, void *eloop_ctx
,
1944 struct wpa_driver_test_data
*drv
= eloop_ctx
;
1947 struct sockaddr_storage from
;
1948 socklen_t fromlen
= sizeof(from
);
1949 const size_t buflen
= 2000;
1952 test_driver_receive_unix(sock
, eloop_ctx
, sock_ctx
);
1956 buf
= os_malloc(buflen
);
1959 res
= recvfrom(sock
, buf
, buflen
- 1, 0,
1960 (struct sockaddr
*) &from
, &fromlen
);
1962 perror("recvfrom(test_socket)");
1968 wpa_printf(MSG_DEBUG
, "test_driver: received %u bytes", res
);
1970 if (os_strncmp(buf
, "SCANRESP ", 9) == 0) {
1971 wpa_driver_test_scanresp(drv
, (struct sockaddr
*) &from
,
1973 } else if (os_strncmp(buf
, "ASSOCRESP ", 10) == 0) {
1974 wpa_driver_test_assocresp(drv
, (struct sockaddr
*) &from
,
1976 } else if (os_strcmp(buf
, "DISASSOC") == 0) {
1977 wpa_driver_test_disassoc(drv
, (struct sockaddr
*) &from
,
1979 } else if (os_strcmp(buf
, "DEAUTH") == 0) {
1980 wpa_driver_test_disassoc(drv
, (struct sockaddr
*) &from
,
1982 } else if (os_strncmp(buf
, "EAPOL ", 6) == 0) {
1983 wpa_driver_test_eapol(drv
, (struct sockaddr
*) &from
, fromlen
,
1984 (const u8
*) buf
+ 6, res
- 6);
1985 } else if (os_strncmp(buf
, "MLME ", 5) == 0) {
1986 wpa_driver_test_mlme(drv
, (struct sockaddr
*) &from
, fromlen
,
1987 (const u8
*) buf
+ 5, res
- 5);
1988 } else if (os_strncmp(buf
, "SCAN ", 5) == 0) {
1989 wpa_driver_test_scan_cmd(drv
, (struct sockaddr
*) &from
,
1991 (const u8
*) buf
+ 5, res
- 5);
1993 wpa_hexdump_ascii(MSG_DEBUG
, "Unknown test_socket command",
2000 static void * wpa_driver_test_init2(void *ctx
, const char *ifname
,
2003 struct wpa_driver_test_data
*drv
;
2004 struct wpa_driver_test_global
*global
= global_priv
;
2006 drv
= test_alloc_data(ctx
, ifname
);
2009 drv
->global
= global_priv
;
2010 drv
->test_socket
= -1;
2012 /* Set dummy BSSID and SSID for testing. */
2013 drv
->bssid
[0] = 0x02;
2014 drv
->bssid
[1] = 0x00;
2015 drv
->bssid
[2] = 0x00;
2016 drv
->bssid
[3] = 0x00;
2017 drv
->bssid
[4] = 0x00;
2018 drv
->bssid
[5] = 0x01;
2019 os_memcpy(drv
->ssid
, "test", 5);
2022 if (global
->bss_add_used
) {
2023 os_memcpy(drv
->own_addr
, global
->req_addr
, ETH_ALEN
);
2024 global
->bss_add_used
= 0;
2027 eloop_register_timeout(1, 0, wpa_driver_test_poll
, drv
, NULL
);
2033 static void wpa_driver_test_close_test_socket(struct wpa_driver_test_data
*drv
)
2035 if (drv
->test_socket
>= 0) {
2036 eloop_unregister_read_sock(drv
->test_socket
);
2037 close(drv
->test_socket
);
2038 drv
->test_socket
= -1;
2041 if (drv
->own_socket_path
) {
2042 unlink(drv
->own_socket_path
);
2043 os_free(drv
->own_socket_path
);
2044 drv
->own_socket_path
= NULL
;
2049 static void wpa_driver_test_deinit(void *priv
)
2051 struct wpa_driver_test_data
*drv
= priv
;
2052 struct test_client_socket
*cli
, *prev
;
2063 /* There should be only one BSS remaining at this point. */
2064 if (drv
->bss
== NULL
)
2065 wpa_printf(MSG_ERROR
, "%s: drv->bss == NULL", __func__
);
2066 else if (drv
->bss
->next
)
2067 wpa_printf(MSG_ERROR
, "%s: drv->bss->next != NULL", __func__
);
2068 #endif /* HOSTAPD */
2070 test_driver_free_bsses(drv
);
2072 wpa_driver_test_close_test_socket(drv
);
2073 eloop_cancel_timeout(wpa_driver_test_scan_timeout
, drv
, drv
->ctx
);
2074 eloop_cancel_timeout(wpa_driver_test_poll
, drv
, NULL
);
2075 eloop_cancel_timeout(test_remain_on_channel_timeout
, drv
, NULL
);
2076 os_free(drv
->test_dir
);
2077 for (i
= 0; i
< MAX_SCAN_RESULTS
; i
++)
2078 os_free(drv
->scanres
[i
]);
2079 os_free(drv
->probe_req_ie
);
2080 wpa_trace_remove_ref(drv
, ctx
, drv
->ctx
);
2085 static int wpa_driver_test_attach(struct wpa_driver_test_data
*drv
,
2086 const char *dir
, int ap
)
2088 #ifdef DRIVER_TEST_UNIX
2089 static unsigned int counter
= 0;
2090 struct sockaddr_un addr
;
2093 os_free(drv
->own_socket_path
);
2095 len
= os_strlen(dir
) + 30;
2096 drv
->own_socket_path
= os_malloc(len
);
2097 if (drv
->own_socket_path
== NULL
)
2099 os_snprintf(drv
->own_socket_path
, len
, "%s/%s-" MACSTR
,
2100 dir
, ap
? "AP" : "STA", MAC2STR(drv
->own_addr
));
2102 drv
->own_socket_path
= os_malloc(100);
2103 if (drv
->own_socket_path
== NULL
)
2105 os_snprintf(drv
->own_socket_path
, 100,
2106 "/tmp/wpa_supplicant_test-%d-%d",
2107 getpid(), counter
++);
2110 drv
->test_socket
= socket(PF_UNIX
, SOCK_DGRAM
, 0);
2111 if (drv
->test_socket
< 0) {
2112 perror("socket(PF_UNIX)");
2113 os_free(drv
->own_socket_path
);
2114 drv
->own_socket_path
= NULL
;
2118 os_memset(&addr
, 0, sizeof(addr
));
2119 addr
.sun_family
= AF_UNIX
;
2120 os_strlcpy(addr
.sun_path
, drv
->own_socket_path
, sizeof(addr
.sun_path
));
2121 if (bind(drv
->test_socket
, (struct sockaddr
*) &addr
,
2122 sizeof(addr
)) < 0) {
2123 perror("bind(PF_UNIX)");
2124 close(drv
->test_socket
);
2125 unlink(drv
->own_socket_path
);
2126 os_free(drv
->own_socket_path
);
2127 drv
->own_socket_path
= NULL
;
2131 eloop_register_read_sock(drv
->test_socket
,
2132 wpa_driver_test_receive_unix
, drv
, NULL
);
2135 #else /* DRIVER_TEST_UNIX */
2137 #endif /* DRIVER_TEST_UNIX */
2141 static int wpa_driver_test_attach_udp(struct wpa_driver_test_data
*drv
,
2146 pos
= os_strchr(dst
, ':');
2150 wpa_printf(MSG_DEBUG
, "%s: addr=%s port=%s", __func__
, dst
, pos
);
2152 drv
->test_socket
= socket(PF_INET
, SOCK_DGRAM
, 0);
2153 if (drv
->test_socket
< 0) {
2154 perror("socket(PF_INET)");
2158 os_memset(&drv
->hostapd_addr_udp
, 0, sizeof(drv
->hostapd_addr_udp
));
2159 drv
->hostapd_addr_udp
.sin_family
= AF_INET
;
2160 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
2164 sscanf(dst
, "%d.%d.%d.%d", &a
[0], &a
[1], &a
[2], &a
[3]);
2165 pos
= (u8
*) &drv
->hostapd_addr_udp
.sin_addr
;
2171 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
2172 inet_aton(dst
, &drv
->hostapd_addr_udp
.sin_addr
);
2173 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
2174 drv
->hostapd_addr_udp
.sin_port
= htons(atoi(pos
));
2176 drv
->hostapd_addr_udp_set
= 1;
2178 eloop_register_read_sock(drv
->test_socket
,
2179 wpa_driver_test_receive_unix
, drv
, NULL
);
2185 static int wpa_driver_test_set_param(void *priv
, const char *param
)
2187 struct wpa_driver_test_data
*drv
= priv
;
2190 wpa_printf(MSG_DEBUG
, "%s: param='%s'", __func__
, param
);
2194 wpa_driver_test_close_test_socket(drv
);
2196 #ifdef DRIVER_TEST_UNIX
2197 pos
= os_strstr(param
, "test_socket=");
2203 pos2
= os_strchr(pos
, ' ');
2207 len
= os_strlen(pos
);
2208 if (len
> sizeof(drv
->hostapd_addr
.sun_path
))
2210 os_memset(&drv
->hostapd_addr
, 0, sizeof(drv
->hostapd_addr
));
2211 drv
->hostapd_addr
.sun_family
= AF_UNIX
;
2212 os_memcpy(drv
->hostapd_addr
.sun_path
, pos
, len
);
2213 drv
->hostapd_addr_set
= 1;
2215 #endif /* DRIVER_TEST_UNIX */
2217 pos
= os_strstr(param
, "test_dir=");
2220 os_free(drv
->test_dir
);
2221 drv
->test_dir
= os_strdup(pos
+ 9);
2222 if (drv
->test_dir
== NULL
)
2224 end
= os_strchr(drv
->test_dir
, ' ');
2227 if (wpa_driver_test_attach(drv
, drv
->test_dir
, 0))
2230 pos
= os_strstr(param
, "test_udp=");
2233 dst
= os_strdup(pos
+ 9);
2236 epos
= os_strchr(dst
, ' ');
2239 if (wpa_driver_test_attach_udp(drv
, dst
))
2242 } else if (wpa_driver_test_attach(drv
, NULL
, 0))
2246 if (os_strstr(param
, "use_associnfo=1")) {
2247 wpa_printf(MSG_DEBUG
, "test_driver: Use AssocInfo events");
2248 drv
->use_associnfo
= 1;
2251 #ifdef CONFIG_CLIENT_MLME
2252 if (os_strstr(param
, "use_mlme=1")) {
2253 wpa_printf(MSG_DEBUG
, "test_driver: Use internal MLME");
2256 #endif /* CONFIG_CLIENT_MLME */
2262 static const u8
* wpa_driver_test_get_mac_addr(void *priv
)
2264 struct wpa_driver_test_data
*drv
= priv
;
2265 wpa_printf(MSG_DEBUG
, "%s", __func__
);
2266 return drv
->own_addr
;
2270 static int wpa_driver_test_send_eapol(void *priv
, const u8
*dest
, u16 proto
,
2271 const u8
*data
, size_t data_len
)
2273 struct wpa_driver_test_data
*drv
= priv
;
2276 struct l2_ethhdr eth
;
2277 struct sockaddr
*addr
;
2279 #ifdef DRIVER_TEST_UNIX
2280 struct sockaddr_un addr_un
;
2281 #endif /* DRIVER_TEST_UNIX */
2283 wpa_hexdump(MSG_MSGDUMP
, "test_send_eapol TX frame", data
, data_len
);
2285 os_memset(ð
, 0, sizeof(eth
));
2286 os_memcpy(eth
.h_dest
, dest
, ETH_ALEN
);
2287 os_memcpy(eth
.h_source
, drv
->own_addr
, ETH_ALEN
);
2288 eth
.h_proto
= host_to_be16(proto
);
2290 msg_len
= 6 + sizeof(eth
) + data_len
;
2291 msg
= os_malloc(msg_len
);
2294 os_memcpy(msg
, "EAPOL ", 6);
2295 os_memcpy(msg
+ 6, ð
, sizeof(eth
));
2296 os_memcpy(msg
+ 6 + sizeof(eth
), data
, data_len
);
2298 if (os_memcmp(dest
, drv
->bssid
, ETH_ALEN
) == 0 ||
2299 drv
->test_dir
== NULL
) {
2300 if (drv
->hostapd_addr_udp_set
) {
2301 addr
= (struct sockaddr
*) &drv
->hostapd_addr_udp
;
2302 alen
= sizeof(drv
->hostapd_addr_udp
);
2304 #ifdef DRIVER_TEST_UNIX
2305 addr
= (struct sockaddr
*) &drv
->hostapd_addr
;
2306 alen
= sizeof(drv
->hostapd_addr
);
2307 #else /* DRIVER_TEST_UNIX */
2310 #endif /* DRIVER_TEST_UNIX */
2313 #ifdef DRIVER_TEST_UNIX
2315 os_memset(&addr_un
, 0, sizeof(addr_un
));
2316 addr_un
.sun_family
= AF_UNIX
;
2317 os_snprintf(addr_un
.sun_path
, sizeof(addr_un
.sun_path
),
2318 "%s/STA-" MACSTR
, drv
->test_dir
, MAC2STR(dest
));
2319 if (stat(addr_un
.sun_path
, &st
) < 0) {
2320 os_snprintf(addr_un
.sun_path
, sizeof(addr_un
.sun_path
),
2322 drv
->test_dir
, MAC2STR(dest
));
2324 addr
= (struct sockaddr
*) &addr_un
;
2325 alen
= sizeof(addr_un
);
2326 #else /* DRIVER_TEST_UNIX */
2329 #endif /* DRIVER_TEST_UNIX */
2332 if (sendto(drv
->test_socket
, msg
, msg_len
, 0, addr
, alen
) < 0) {
2333 perror("sendmsg(test_socket)");
2343 static int wpa_driver_test_get_capa(void *priv
, struct wpa_driver_capa
*capa
)
2345 struct wpa_driver_test_data
*drv
= priv
;
2346 os_memset(capa
, 0, sizeof(*capa
));
2347 capa
->key_mgmt
= WPA_DRIVER_CAPA_KEY_MGMT_WPA
|
2348 WPA_DRIVER_CAPA_KEY_MGMT_WPA2
|
2349 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK
|
2350 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK
|
2351 WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE
|
2352 WPA_DRIVER_CAPA_KEY_MGMT_FT
|
2353 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK
;
2354 capa
->enc
= WPA_DRIVER_CAPA_ENC_WEP40
|
2355 WPA_DRIVER_CAPA_ENC_WEP104
|
2356 WPA_DRIVER_CAPA_ENC_TKIP
|
2357 WPA_DRIVER_CAPA_ENC_CCMP
;
2358 capa
->auth
= WPA_DRIVER_AUTH_OPEN
|
2359 WPA_DRIVER_AUTH_SHARED
|
2360 WPA_DRIVER_AUTH_LEAP
;
2362 capa
->flags
|= WPA_DRIVER_FLAGS_USER_SPACE_MLME
;
2363 capa
->flags
|= WPA_DRIVER_FLAGS_AP
;
2364 capa
->max_scan_ssids
= 2;
2370 static int wpa_driver_test_mlme_setprotection(void *priv
, const u8
*addr
,
2374 wpa_printf(MSG_DEBUG
, "%s: protect_type=%d key_type=%d",
2375 __func__
, protect_type
, key_type
);
2378 wpa_printf(MSG_DEBUG
, "%s: addr=" MACSTR
,
2379 __func__
, MAC2STR(addr
));
2386 static int wpa_driver_test_set_channel(void *priv
,
2387 enum hostapd_hw_mode phymode
,
2390 struct wpa_driver_test_data
*drv
= priv
;
2391 wpa_printf(MSG_DEBUG
, "%s: phymode=%d chan=%d freq=%d",
2392 __func__
, phymode
, chan
, freq
);
2393 drv
->current_freq
= freq
;
2398 static int wpa_driver_test_mlme_add_sta(void *priv
, const u8
*addr
,
2399 const u8
*supp_rates
,
2400 size_t supp_rates_len
)
2402 wpa_printf(MSG_DEBUG
, "%s: addr=" MACSTR
, __func__
, MAC2STR(addr
));
2407 static int wpa_driver_test_mlme_remove_sta(void *priv
, const u8
*addr
)
2409 wpa_printf(MSG_DEBUG
, "%s: addr=" MACSTR
, __func__
, MAC2STR(addr
));
2414 static int wpa_driver_test_set_ssid(void *priv
, const u8
*ssid
,
2417 wpa_printf(MSG_DEBUG
, "%s", __func__
);
2422 static int wpa_driver_test_set_bssid(void *priv
, const u8
*bssid
)
2424 wpa_printf(MSG_DEBUG
, "%s: bssid=" MACSTR
, __func__
, MAC2STR(bssid
));
2429 static void * wpa_driver_test_global_init(void)
2431 struct wpa_driver_test_global
*global
;
2433 global
= os_zalloc(sizeof(*global
));
2438 static void wpa_driver_test_global_deinit(void *priv
)
2440 struct wpa_driver_test_global
*global
= priv
;
2445 static struct wpa_interface_info
*
2446 wpa_driver_test_get_interfaces(void *global_priv
)
2448 /* struct wpa_driver_test_global *global = priv; */
2449 struct wpa_interface_info
*iface
;
2451 iface
= os_zalloc(sizeof(*iface
));
2454 iface
->ifname
= os_strdup("sta0");
2455 iface
->desc
= os_strdup("test interface 0");
2456 iface
->drv_name
= "test";
2457 iface
->next
= os_zalloc(sizeof(*iface
));
2459 iface
->next
->ifname
= os_strdup("sta1");
2460 iface
->next
->desc
= os_strdup("test interface 1");
2461 iface
->next
->drv_name
= "test";
2468 static struct hostapd_hw_modes
*
2469 wpa_driver_test_get_hw_feature_data(void *priv
, u16
*num_modes
, u16
*flags
)
2471 struct hostapd_hw_modes
*modes
;
2476 modes
= os_zalloc(*num_modes
* sizeof(struct hostapd_hw_modes
));
2479 modes
[0].mode
= HOSTAPD_MODE_IEEE80211G
;
2480 modes
[0].num_channels
= 11;
2481 modes
[0].num_rates
= 12;
2483 os_zalloc(11 * sizeof(struct hostapd_channel_data
));
2484 modes
[0].rates
= os_zalloc(modes
[0].num_rates
* sizeof(int));
2485 if (modes
[0].channels
== NULL
|| modes
[0].rates
== NULL
)
2487 for (i
= 0; i
< 11; i
++) {
2488 modes
[0].channels
[i
].chan
= i
+ 1;
2489 modes
[0].channels
[i
].freq
= 2412 + 5 * i
;
2490 modes
[0].channels
[i
].flag
= 0;
2492 modes
[0].rates
[0] = 10;
2493 modes
[0].rates
[1] = 20;
2494 modes
[0].rates
[2] = 55;
2495 modes
[0].rates
[3] = 110;
2496 modes
[0].rates
[4] = 60;
2497 modes
[0].rates
[5] = 90;
2498 modes
[0].rates
[6] = 120;
2499 modes
[0].rates
[7] = 180;
2500 modes
[0].rates
[8] = 240;
2501 modes
[0].rates
[9] = 360;
2502 modes
[0].rates
[10] = 480;
2503 modes
[0].rates
[11] = 540;
2505 modes
[1].mode
= HOSTAPD_MODE_IEEE80211B
;
2506 modes
[1].num_channels
= 11;
2507 modes
[1].num_rates
= 4;
2509 os_zalloc(11 * sizeof(struct hostapd_channel_data
));
2510 modes
[1].rates
= os_zalloc(modes
[1].num_rates
* sizeof(int));
2511 if (modes
[1].channels
== NULL
|| modes
[1].rates
== NULL
)
2513 for (i
= 0; i
< 11; i
++) {
2514 modes
[1].channels
[i
].chan
= i
+ 1;
2515 modes
[1].channels
[i
].freq
= 2412 + 5 * i
;
2516 modes
[1].channels
[i
].flag
= 0;
2518 modes
[1].rates
[0] = 10;
2519 modes
[1].rates
[1] = 20;
2520 modes
[1].rates
[2] = 55;
2521 modes
[1].rates
[3] = 110;
2523 modes
[2].mode
= HOSTAPD_MODE_IEEE80211A
;
2524 modes
[2].num_channels
= 1;
2525 modes
[2].num_rates
= 8;
2526 modes
[2].channels
= os_zalloc(sizeof(struct hostapd_channel_data
));
2527 modes
[2].rates
= os_zalloc(modes
[2].num_rates
* sizeof(int));
2528 if (modes
[2].channels
== NULL
|| modes
[2].rates
== NULL
)
2530 modes
[2].channels
[0].chan
= 60;
2531 modes
[2].channels
[0].freq
= 5300;
2532 modes
[2].channels
[0].flag
= 0;
2533 modes
[2].rates
[0] = 60;
2534 modes
[2].rates
[1] = 90;
2535 modes
[2].rates
[2] = 120;
2536 modes
[2].rates
[3] = 180;
2537 modes
[2].rates
[4] = 240;
2538 modes
[2].rates
[5] = 360;
2539 modes
[2].rates
[6] = 480;
2540 modes
[2].rates
[7] = 540;
2546 for (i
= 0; i
< *num_modes
; i
++) {
2547 os_free(modes
[i
].channels
);
2548 os_free(modes
[i
].rates
);
2556 static int wpa_driver_test_set_freq(void *priv
,
2557 struct hostapd_freq_params
*freq
)
2559 struct wpa_driver_test_data
*drv
= priv
;
2560 wpa_printf(MSG_DEBUG
, "test: set_freq %u MHz", freq
->freq
);
2561 drv
->current_freq
= freq
->freq
;
2566 static int wpa_driver_test_send_action(void *priv
, unsigned int freq
,
2567 const u8
*dst
, const u8
*src
,
2568 const u8
*data
, size_t data_len
)
2570 struct wpa_driver_test_data
*drv
= priv
;
2573 struct ieee80211_hdr
*hdr
;
2575 wpa_printf(MSG_DEBUG
, "test: Send Action frame");
2577 if ((drv
->remain_on_channel_freq
&&
2578 freq
!= drv
->remain_on_channel_freq
) ||
2579 (drv
->remain_on_channel_freq
== 0 &&
2580 freq
!= (unsigned int) drv
->current_freq
)) {
2581 wpa_printf(MSG_DEBUG
, "test: Reject Action frame TX on "
2582 "unexpected channel: freq=%u MHz (current_freq=%u "
2583 "MHz, remain-on-channel freq=%u MHz)",
2584 freq
, drv
->current_freq
,
2585 drv
->remain_on_channel_freq
);
2589 buf
= os_zalloc(24 + data_len
);
2592 os_memcpy(buf
+ 24, data
, data_len
);
2593 hdr
= (struct ieee80211_hdr
*) buf
;
2594 hdr
->frame_control
=
2595 IEEE80211_FC(WLAN_FC_TYPE_MGMT
, WLAN_FC_STYPE_ACTION
);
2596 os_memcpy(hdr
->addr1
, dst
, ETH_ALEN
);
2597 os_memcpy(hdr
->addr2
, src
, ETH_ALEN
);
2598 os_memcpy(hdr
->addr3
, "\xff\xff\xff\xff\xff\xff", ETH_ALEN
);
2600 ret
= wpa_driver_test_send_mlme(priv
, buf
, 24 + data_len
);
2606 static int wpa_driver_test_alloc_interface_addr(void *priv
, u8
*addr
)
2608 struct wpa_driver_test_data
*drv
= priv
;
2609 drv
->alloc_iface_idx
++;
2610 addr
[0] = 0x02; /* locally administered */
2611 sha1_prf(drv
->own_addr
, ETH_ALEN
, "hostapd test addr generation",
2612 (const u8
*) &drv
->alloc_iface_idx
,
2613 sizeof(drv
->alloc_iface_idx
),
2614 addr
+ 1, ETH_ALEN
- 1);
2619 static void wpa_driver_test_release_interface_addr(void *priv
, const u8
*addr
)
2624 static void test_remain_on_channel_timeout(void *eloop_ctx
, void *timeout_ctx
)
2626 struct wpa_driver_test_data
*drv
= eloop_ctx
;
2627 union wpa_event_data data
;
2629 wpa_printf(MSG_DEBUG
, "test: Remain-on-channel timeout");
2631 os_memset(&data
, 0, sizeof(data
));
2632 data
.remain_on_channel
.freq
= drv
->remain_on_channel_freq
;
2633 data
.remain_on_channel
.duration
= drv
->remain_on_channel_duration
;
2634 wpa_supplicant_event(drv
->ctx
, EVENT_CANCEL_REMAIN_ON_CHANNEL
, &data
);
2636 drv
->remain_on_channel_freq
= 0;
2640 static int wpa_driver_test_remain_on_channel(void *priv
, unsigned int freq
,
2641 unsigned int duration
)
2643 struct wpa_driver_test_data
*drv
= priv
;
2644 union wpa_event_data data
;
2646 wpa_printf(MSG_DEBUG
, "%s(freq=%u, duration=%u)",
2647 __func__
, freq
, duration
);
2648 if (drv
->remain_on_channel_freq
&&
2649 drv
->remain_on_channel_freq
!= freq
) {
2650 wpa_printf(MSG_DEBUG
, "test: Refuse concurrent "
2651 "remain_on_channel request");
2655 drv
->remain_on_channel_freq
= freq
;
2656 drv
->remain_on_channel_duration
= duration
;
2657 eloop_cancel_timeout(test_remain_on_channel_timeout
, drv
, NULL
);
2658 eloop_register_timeout(duration
/ 1000, (duration
% 1000) * 1000,
2659 test_remain_on_channel_timeout
, drv
, NULL
);
2661 os_memset(&data
, 0, sizeof(data
));
2662 data
.remain_on_channel
.freq
= freq
;
2663 data
.remain_on_channel
.duration
= duration
;
2664 wpa_supplicant_event(drv
->ctx
, EVENT_REMAIN_ON_CHANNEL
, &data
);
2670 static int wpa_driver_test_cancel_remain_on_channel(void *priv
)
2672 struct wpa_driver_test_data
*drv
= priv
;
2673 wpa_printf(MSG_DEBUG
, "%s", __func__
);
2674 if (!drv
->remain_on_channel_freq
)
2676 drv
->remain_on_channel_freq
= 0;
2677 eloop_cancel_timeout(test_remain_on_channel_timeout
, drv
, NULL
);
2682 static int wpa_driver_test_probe_req_report(void *priv
, int report
)
2684 struct wpa_driver_test_data
*drv
= priv
;
2685 wpa_printf(MSG_DEBUG
, "%s(report=%d)", __func__
, report
);
2686 drv
->probe_req_report
= report
;
2691 const struct wpa_driver_ops wpa_driver_test_ops
= {
2693 "wpa_supplicant test driver",
2694 .hapd_init
= test_driver_init
,
2695 .hapd_deinit
= wpa_driver_test_deinit
,
2696 .hapd_send_eapol
= test_driver_send_eapol
,
2697 .send_mlme
= wpa_driver_test_send_mlme
,
2698 .set_generic_elem
= test_driver_set_generic_elem
,
2699 .sta_deauth
= test_driver_sta_deauth
,
2700 .sta_disassoc
= test_driver_sta_disassoc
,
2701 .get_hw_feature_data
= wpa_driver_test_get_hw_feature_data
,
2702 .if_add
= test_driver_if_add
,
2703 .if_remove
= test_driver_if_remove
,
2704 .valid_bss_mask
= test_driver_valid_bss_mask
,
2705 .hapd_set_ssid
= test_driver_set_ssid
,
2706 .set_privacy
= test_driver_set_privacy
,
2707 .set_sta_vlan
= test_driver_set_sta_vlan
,
2708 .sta_add
= test_driver_sta_add
,
2709 .send_ether
= test_driver_send_ether
,
2710 .set_ap_wps_ie
= test_driver_set_ap_wps_ie
,
2711 .get_bssid
= wpa_driver_test_get_bssid
,
2712 .get_ssid
= wpa_driver_test_get_ssid
,
2713 .set_key
= wpa_driver_test_set_key
,
2714 .deinit
= wpa_driver_test_deinit
,
2715 .set_param
= wpa_driver_test_set_param
,
2716 .deauthenticate
= wpa_driver_test_deauthenticate
,
2717 .disassociate
= wpa_driver_test_disassociate
,
2718 .associate
= wpa_driver_test_associate
,
2719 .get_capa
= wpa_driver_test_get_capa
,
2720 .get_mac_addr
= wpa_driver_test_get_mac_addr
,
2721 .send_eapol
= wpa_driver_test_send_eapol
,
2722 .mlme_setprotection
= wpa_driver_test_mlme_setprotection
,
2723 .set_channel
= wpa_driver_test_set_channel
,
2724 .set_ssid
= wpa_driver_test_set_ssid
,
2725 .set_bssid
= wpa_driver_test_set_bssid
,
2726 .mlme_add_sta
= wpa_driver_test_mlme_add_sta
,
2727 .mlme_remove_sta
= wpa_driver_test_mlme_remove_sta
,
2728 .get_scan_results2
= wpa_driver_test_get_scan_results2
,
2729 .global_init
= wpa_driver_test_global_init
,
2730 .global_deinit
= wpa_driver_test_global_deinit
,
2731 .init2
= wpa_driver_test_init2
,
2732 .get_interfaces
= wpa_driver_test_get_interfaces
,
2733 .scan2
= wpa_driver_test_scan
,
2734 .set_freq
= wpa_driver_test_set_freq
,
2735 .send_action
= wpa_driver_test_send_action
,
2736 .alloc_interface_addr
= wpa_driver_test_alloc_interface_addr
,
2737 .release_interface_addr
= wpa_driver_test_release_interface_addr
,
2738 .remain_on_channel
= wpa_driver_test_remain_on_channel
,
2739 .cancel_remain_on_channel
= wpa_driver_test_cancel_remain_on_channel
,
2740 .probe_req_report
= wpa_driver_test_probe_req_report
,