2 * hostapd / Driver interface for development testing
3 * Copyright (c) 2004-2008, 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.
23 #include "ieee802_1x.h"
26 #include "accounting.h"
27 #include "radius/radius.h"
28 #include "l2_packet/l2_packet.h"
29 #include "ieee802_11.h"
30 #include "hw_features.h"
31 #include "wps_hostapd.h"
34 struct test_client_socket {
35 struct test_client_socket *next;
37 struct sockaddr_un un;
39 struct test_driver_bss *bss;
42 struct test_driver_bss {
43 struct test_driver_bss *next;
44 char ifname[IFNAMSIZ + 1];
49 size_t wps_beacon_ie_len;
50 u8 *wps_probe_resp_ie;
51 size_t wps_probe_resp_ie_len;
57 struct test_driver_data {
58 struct hostapd_data *hapd;
59 struct test_client_socket *cli;
61 struct test_driver_bss *bss;
63 char *own_socket_path;
67 static void test_driver_free_bss(struct test_driver_bss *bss)
70 free(bss->wps_beacon_ie);
71 free(bss->wps_probe_resp_ie);
76 static void test_driver_free_priv(struct test_driver_data *drv)
78 struct test_driver_bss *bss, *prev;
87 test_driver_free_bss(prev);
89 free(drv->own_socket_path);
90 free(drv->socket_dir);
95 static struct test_client_socket *
96 test_driver_get_cli(struct test_driver_data *drv, struct sockaddr_un *from,
99 struct test_client_socket *cli = drv->cli;
102 if (cli->unlen == fromlen &&
103 strncmp(cli->un.sun_path, from->sun_path,
104 fromlen - sizeof(cli->un.sun_family)) == 0)
113 static int test_driver_send_eapol(void *priv, const u8 *addr, const u8 *data,
114 size_t data_len, int encrypt,
117 struct test_driver_data *drv = priv;
118 struct test_client_socket *cli;
121 struct l2_ethhdr eth;
123 if (drv->test_socket < 0)
128 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
134 wpa_printf(MSG_DEBUG, "%s: no destination client entry",
139 memcpy(eth.h_dest, addr, ETH_ALEN);
140 memcpy(eth.h_source, own_addr, ETH_ALEN);
141 eth.h_proto = htons(ETH_P_EAPOL);
143 io[0].iov_base = "EAPOL ";
145 io[1].iov_base = ð
146 io[1].iov_len = sizeof(eth);
147 io[2].iov_base = (u8 *) data;
148 io[2].iov_len = data_len;
150 memset(&msg, 0, sizeof(msg));
153 msg.msg_name = &cli->un;
154 msg.msg_namelen = cli->unlen;
155 return sendmsg(drv->test_socket, &msg, 0);
159 static int test_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
160 u16 proto, const u8 *data, size_t data_len)
162 struct test_driver_data *drv = priv;
165 struct l2_ethhdr eth;
167 struct sockaddr_un addr;
170 int ret = 0, broadcast = 0, count = 0;
172 if (drv->test_socket < 0 || drv->socket_dir == NULL) {
173 wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d "
175 __func__, drv->test_socket, drv->socket_dir);
179 broadcast = memcmp(dst, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
180 snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dst));
182 memcpy(eth.h_dest, dst, ETH_ALEN);
183 memcpy(eth.h_source, src, ETH_ALEN);
184 eth.h_proto = htons(proto);
186 io[0].iov_base = "ETHER ";
188 io[1].iov_base = ð
189 io[1].iov_len = sizeof(eth);
190 io[2].iov_base = (u8 *) data;
191 io[2].iov_len = data_len;
193 memset(&msg, 0, sizeof(msg));
197 dir = opendir(drv->socket_dir);
199 perror("test_driver: opendir");
202 while ((dent = readdir(dir))) {
203 #ifdef _DIRENT_HAVE_D_TYPE
204 /* Skip the file if it is not a socket. Also accept
205 * DT_UNKNOWN (0) in case the C library or underlying file
206 * system does not support d_type. */
207 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
209 #endif /* _DIRENT_HAVE_D_TYPE */
210 if (strcmp(dent->d_name, ".") == 0 ||
211 strcmp(dent->d_name, "..") == 0)
214 memset(&addr, 0, sizeof(addr));
215 addr.sun_family = AF_UNIX;
216 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
217 drv->socket_dir, dent->d_name);
219 if (strcmp(addr.sun_path, drv->own_socket_path) == 0)
221 if (!broadcast && strstr(dent->d_name, desttxt) == NULL)
224 wpa_printf(MSG_DEBUG, "%s: Send ether frame to %s",
225 __func__, dent->d_name);
227 msg.msg_name = &addr;
228 msg.msg_namelen = sizeof(addr);
229 ret = sendmsg(drv->test_socket, &msg, 0);
231 perror("driver_test: sendmsg");
236 if (!broadcast && count == 0) {
237 wpa_printf(MSG_DEBUG, "%s: Destination " MACSTR " not found",
238 __func__, MAC2STR(dst));
246 static int test_driver_send_mgmt_frame(void *priv, const void *buf,
247 size_t len, int flags)
249 struct test_driver_data *drv = priv;
253 int ret = 0, broadcast = 0;
255 struct sockaddr_un addr;
258 struct ieee80211_hdr *hdr;
261 if (drv->test_socket < 0 || len < 10 || drv->socket_dir == NULL) {
262 wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d len=%lu"
264 __func__, drv->test_socket, (unsigned long) len,
271 broadcast = memcmp(dest, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
272 snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dest));
274 io[0].iov_base = "MLME ";
276 io[1].iov_base = (void *) buf;
279 memset(&msg, 0, sizeof(msg));
283 dir = opendir(drv->socket_dir);
285 perror("test_driver: opendir");
288 while ((dent = readdir(dir))) {
289 #ifdef _DIRENT_HAVE_D_TYPE
290 /* Skip the file if it is not a socket. Also accept
291 * DT_UNKNOWN (0) in case the C library or underlying file
292 * system does not support d_type. */
293 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
295 #endif /* _DIRENT_HAVE_D_TYPE */
296 if (strcmp(dent->d_name, ".") == 0 ||
297 strcmp(dent->d_name, "..") == 0)
300 memset(&addr, 0, sizeof(addr));
301 addr.sun_family = AF_UNIX;
302 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
303 drv->socket_dir, dent->d_name);
305 if (strcmp(addr.sun_path, drv->own_socket_path) == 0)
307 if (!broadcast && strstr(dent->d_name, desttxt) == NULL)
310 wpa_printf(MSG_DEBUG, "%s: Send management frame to %s",
311 __func__, dent->d_name);
313 msg.msg_name = &addr;
314 msg.msg_namelen = sizeof(addr);
315 ret = sendmsg(drv->test_socket, &msg, 0);
317 perror("driver_test: sendmsg");
321 hdr = (struct ieee80211_hdr *) buf;
322 fc = le_to_host16(hdr->frame_control);
323 ieee802_11_mgmt_cb(drv->hapd, (u8 *) buf, len, WLAN_FC_GET_STYPE(fc),
330 static void test_driver_scan(struct test_driver_data *drv,
331 struct sockaddr_un *from, socklen_t fromlen,
334 char buf[512], *pos, *end;
336 struct test_driver_bss *bss;
341 /* data: optional [ ' ' | STA-addr | ' ' | IEs(hex) ] */
343 wpa_printf(MSG_DEBUG, "test_driver: SCAN");
347 hwaddr_aton(data + 1, sa)) {
348 wpa_printf(MSG_DEBUG, "test_driver: Unexpected SCAN "
356 ielen = os_strlen(data) / 2;
357 if (ielen > sizeof(ie))
359 if (hexstr2bin(data, ie, ielen) < 0)
362 wpa_printf(MSG_DEBUG, "test_driver: Scan from " MACSTR,
364 wpa_hexdump(MSG_MSGDUMP, "test_driver: scan IEs", ie, ielen);
366 hostapd_wps_probe_req_rx(drv->hapd, sa, ie, ielen);
369 for (bss = drv->bss; bss; bss = bss->next) {
371 end = buf + sizeof(buf);
373 /* reply: SCANRESP BSSID SSID IEs */
374 ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
375 MAC2STR(bss->bssid));
376 if (ret < 0 || ret >= end - pos)
379 pos += wpa_snprintf_hex(pos, end - pos,
380 bss->ssid, bss->ssid_len);
381 ret = snprintf(pos, end - pos, " ");
382 if (ret < 0 || ret >= end - pos)
385 pos += wpa_snprintf_hex(pos, end - pos, bss->ie, bss->ielen);
386 pos += wpa_snprintf_hex(pos, end - pos, bss->wps_probe_resp_ie,
387 bss->wps_probe_resp_ie_len);
390 ret = snprintf(pos, end - pos, " PRIVACY");
391 if (ret < 0 || ret >= end - pos)
396 sendto(drv->test_socket, buf, pos - buf, 0,
397 (struct sockaddr *) from, fromlen);
402 static struct hostapd_data * test_driver_get_hapd(struct test_driver_data *drv,
403 struct test_driver_bss *bss)
405 struct hostapd_iface *iface = drv->hapd->iface;
406 struct hostapd_data *hapd = NULL;
410 wpa_printf(MSG_DEBUG, "%s: bss == NULL", __func__);
414 for (i = 0; i < iface->num_bss; i++) {
415 hapd = iface->bss[i];
416 if (memcmp(hapd->own_addr, bss->bssid, ETH_ALEN) == 0)
419 if (i == iface->num_bss) {
420 wpa_printf(MSG_DEBUG, "%s: no matching interface entry found "
421 "for BSSID " MACSTR, __func__, MAC2STR(bss->bssid));
429 static int test_driver_new_sta(struct test_driver_data *drv,
430 struct test_driver_bss *bss, const u8 *addr,
431 const u8 *ie, size_t ielen)
433 struct hostapd_data *hapd;
434 struct sta_info *sta;
437 hapd = test_driver_get_hapd(drv, bss);
441 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
442 HOSTAPD_LEVEL_INFO, "associated");
444 sta = ap_get_sta(hapd, addr);
446 accounting_sta_stop(hapd, sta);
448 sta = ap_sta_add(hapd, addr);
453 if (hapd->conf->wpa) {
454 if (ie == NULL || ielen == 0) {
455 printf("test_driver: no IE from STA\n");
458 if (sta->wpa_sm == NULL)
459 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
461 if (sta->wpa_sm == NULL) {
462 printf("test_driver: Failed to initialize WPA state "
466 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
468 if (res != WPA_IE_OK) {
469 printf("WPA/RSN information element rejected? "
475 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
476 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
477 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
479 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
481 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
487 static void test_driver_assoc(struct test_driver_data *drv,
488 struct sockaddr_un *from, socklen_t fromlen,
491 struct test_client_socket *cli;
492 u8 ie[256], ssid[32];
493 size_t ielen, ssid_len = 0;
494 char *pos, *pos2, cmd[50];
495 struct test_driver_bss *bss;
497 /* data: STA-addr SSID(hex) IEs(hex) */
499 cli = os_zalloc(sizeof(*cli));
503 if (hwaddr_aton(data, cli->addr)) {
504 printf("test_socket: Invalid MAC address '%s' in ASSOC\n",
512 pos2 = strchr(pos, ' ');
515 ssid_len = (pos2 - pos) / 2;
516 if (hexstr2bin(pos, ssid, ssid_len) < 0) {
517 wpa_printf(MSG_DEBUG, "%s: Invalid SSID", __func__);
521 wpa_hexdump_ascii(MSG_DEBUG, "test_driver_assoc: SSID",
525 ielen = strlen(pos) / 2;
526 if (ielen > sizeof(ie))
528 if (hexstr2bin(pos, ie, ielen) < 0)
532 for (bss = drv->bss; bss; bss = bss->next) {
533 if (bss->ssid_len == ssid_len &&
534 memcmp(bss->ssid, ssid, ssid_len) == 0)
538 wpa_printf(MSG_DEBUG, "%s: No matching SSID found from "
539 "configured BSSes", __func__);
545 memcpy(&cli->un, from, sizeof(cli->un));
546 cli->unlen = fromlen;
547 cli->next = drv->cli;
549 wpa_hexdump_ascii(MSG_DEBUG, "test_socket: ASSOC sun_path",
550 (const u8 *) cli->un.sun_path,
551 cli->unlen - sizeof(cli->un.sun_family));
553 snprintf(cmd, sizeof(cmd), "ASSOCRESP " MACSTR " 0",
554 MAC2STR(bss->bssid));
555 sendto(drv->test_socket, cmd, strlen(cmd), 0,
556 (struct sockaddr *) from, fromlen);
558 if (test_driver_new_sta(drv, bss, cli->addr, ie, ielen) < 0) {
559 wpa_printf(MSG_DEBUG, "test_driver: failed to add new STA");
564 static void test_driver_disassoc(struct test_driver_data *drv,
565 struct sockaddr_un *from, socklen_t fromlen)
567 struct test_client_socket *cli;
568 struct sta_info *sta;
570 cli = test_driver_get_cli(drv, from, fromlen);
574 hostapd_logger(drv->hapd, cli->addr, HOSTAPD_MODULE_IEEE80211,
575 HOSTAPD_LEVEL_INFO, "disassociated");
577 sta = ap_get_sta(drv->hapd, cli->addr);
579 sta->flags &= ~WLAN_STA_ASSOC;
580 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
581 sta->acct_terminate_cause =
582 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
583 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
584 ap_free_sta(drv->hapd, sta);
589 static void test_driver_eapol(struct test_driver_data *drv,
590 struct sockaddr_un *from, socklen_t fromlen,
591 u8 *data, size_t datalen)
593 struct test_client_socket *cli;
595 u8 *proto = data + 2 * ETH_ALEN;
596 /* Skip Ethernet header */
597 wpa_printf(MSG_DEBUG, "test_driver: dst=" MACSTR " src="
598 MACSTR " proto=%04x",
599 MAC2STR(data), MAC2STR(data + ETH_ALEN),
600 WPA_GET_BE16(proto));
604 cli = test_driver_get_cli(drv, from, fromlen);
606 struct hostapd_data *hapd;
607 hapd = test_driver_get_hapd(drv, cli->bss);
610 ieee802_1x_receive(hapd, cli->addr, data, datalen);
612 wpa_printf(MSG_DEBUG, "test_socket: EAPOL from unknown "
618 static void test_driver_ether(struct test_driver_data *drv,
619 struct sockaddr_un *from, socklen_t fromlen,
620 u8 *data, size_t datalen)
622 struct l2_ethhdr *eth;
624 if (datalen < sizeof(*eth))
627 eth = (struct l2_ethhdr *) data;
628 wpa_printf(MSG_DEBUG, "test_driver: RX ETHER dst=" MACSTR " src="
629 MACSTR " proto=%04x",
630 MAC2STR(eth->h_dest), MAC2STR(eth->h_source),
631 be_to_host16(eth->h_proto));
633 #ifdef CONFIG_IEEE80211R
634 if (be_to_host16(eth->h_proto) == ETH_P_RRB) {
635 wpa_ft_rrb_rx(drv->hapd->wpa_auth, eth->h_source,
636 data + sizeof(*eth), datalen - sizeof(*eth));
638 #endif /* CONFIG_IEEE80211R */
642 static void test_driver_mlme(struct test_driver_data *drv,
643 struct sockaddr_un *from, socklen_t fromlen,
644 u8 *data, size_t datalen)
646 struct ieee80211_hdr *hdr;
649 hdr = (struct ieee80211_hdr *) data;
651 if (test_driver_get_cli(drv, from, fromlen) == NULL && datalen >= 16) {
652 struct test_client_socket *cli;
653 cli = os_zalloc(sizeof(*cli));
656 wpa_printf(MSG_DEBUG, "Adding client entry for " MACSTR,
657 MAC2STR(hdr->addr2));
658 memcpy(cli->addr, hdr->addr2, ETH_ALEN);
659 memcpy(&cli->un, from, sizeof(cli->un));
660 cli->unlen = fromlen;
661 cli->next = drv->cli;
665 wpa_hexdump(MSG_MSGDUMP, "test_driver_mlme: received frame",
667 fc = le_to_host16(hdr->frame_control);
668 if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT) {
669 wpa_printf(MSG_ERROR, "%s: received non-mgmt frame",
673 ieee802_11_mgmt(drv->hapd, data, datalen, WLAN_FC_GET_STYPE(fc), NULL);
677 static void test_driver_receive_unix(int sock, void *eloop_ctx, void *sock_ctx)
679 struct test_driver_data *drv = eloop_ctx;
682 struct sockaddr_un from;
683 socklen_t fromlen = sizeof(from);
685 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
686 (struct sockaddr *) &from, &fromlen);
688 perror("recvfrom(test_socket)");
693 wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
695 if (strncmp(buf, "SCAN", 4) == 0) {
696 test_driver_scan(drv, &from, fromlen, buf + 4);
697 } else if (strncmp(buf, "ASSOC ", 6) == 0) {
698 test_driver_assoc(drv, &from, fromlen, buf + 6);
699 } else if (strcmp(buf, "DISASSOC") == 0) {
700 test_driver_disassoc(drv, &from, fromlen);
701 } else if (strncmp(buf, "EAPOL ", 6) == 0) {
702 test_driver_eapol(drv, &from, fromlen, (u8 *) buf + 6,
704 } else if (strncmp(buf, "ETHER ", 6) == 0) {
705 test_driver_ether(drv, &from, fromlen, (u8 *) buf + 6,
707 } else if (strncmp(buf, "MLME ", 5) == 0) {
708 test_driver_mlme(drv, &from, fromlen, (u8 *) buf + 5, res - 5);
710 wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
716 static struct test_driver_bss *
717 test_driver_get_bss(struct test_driver_data *drv, const char *ifname)
719 struct test_driver_bss *bss;
721 for (bss = drv->bss; bss; bss = bss->next) {
722 if (strcmp(bss->ifname, ifname) == 0)
729 static int test_driver_set_generic_elem(const char *ifname, void *priv,
730 const u8 *elem, size_t elem_len)
732 struct test_driver_data *drv = priv;
733 struct test_driver_bss *bss;
735 bss = test_driver_get_bss(drv, ifname);
747 bss->ie = malloc(elem_len);
748 if (bss->ie == NULL) {
753 memcpy(bss->ie, elem, elem_len);
754 bss->ielen = elem_len;
759 static int test_driver_set_wps_beacon_ie(const char *ifname, void *priv,
760 const u8 *ie, size_t len)
762 struct test_driver_data *drv = priv;
763 struct test_driver_bss *bss;
765 bss = test_driver_get_bss(drv, ifname);
769 free(bss->wps_beacon_ie);
772 bss->wps_beacon_ie = NULL;
773 bss->wps_beacon_ie_len = 0;
777 bss->wps_beacon_ie = malloc(len);
778 if (bss->wps_beacon_ie == NULL) {
779 bss->wps_beacon_ie_len = 0;
783 memcpy(bss->wps_beacon_ie, ie, len);
784 bss->wps_beacon_ie_len = len;
789 static int test_driver_set_wps_probe_resp_ie(const char *ifname, void *priv,
790 const u8 *ie, size_t len)
792 struct test_driver_data *drv = priv;
793 struct test_driver_bss *bss;
795 bss = test_driver_get_bss(drv, ifname);
799 free(bss->wps_probe_resp_ie);
802 bss->wps_probe_resp_ie = NULL;
803 bss->wps_probe_resp_ie_len = 0;
807 bss->wps_probe_resp_ie = malloc(len);
808 if (bss->wps_probe_resp_ie == NULL) {
809 bss->wps_probe_resp_ie_len = 0;
813 memcpy(bss->wps_probe_resp_ie, ie, len);
814 bss->wps_probe_resp_ie_len = len;
819 static int test_driver_sta_deauth(void *priv, const u8 *addr, int reason)
821 struct test_driver_data *drv = priv;
822 struct test_client_socket *cli;
824 if (drv->test_socket < 0)
829 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
837 return sendto(drv->test_socket, "DEAUTH", 6, 0,
838 (struct sockaddr *) &cli->un, cli->unlen);
842 static int test_driver_sta_disassoc(void *priv, const u8 *addr, int reason)
844 struct test_driver_data *drv = priv;
845 struct test_client_socket *cli;
847 if (drv->test_socket < 0)
852 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
860 return sendto(drv->test_socket, "DISASSOC", 8, 0,
861 (struct sockaddr *) &cli->un, cli->unlen);
865 static struct hostapd_hw_modes *
866 test_driver_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
868 struct hostapd_hw_modes *modes;
872 modes = os_zalloc(*num_modes * sizeof(struct hostapd_hw_modes));
875 modes[0].mode = HOSTAPD_MODE_IEEE80211G;
876 modes[0].num_channels = 1;
877 modes[0].num_rates = 1;
878 modes[0].channels = os_zalloc(sizeof(struct hostapd_channel_data));
879 modes[0].rates = os_zalloc(sizeof(struct hostapd_rate_data));
880 if (modes[0].channels == NULL || modes[0].rates == NULL) {
881 hostapd_free_hw_features(modes, *num_modes);
884 modes[0].channels[0].chan = 1;
885 modes[0].channels[0].freq = 2412;
886 modes[0].channels[0].flag = 0;
887 modes[0].rates[0].rate = 10;
888 modes[0].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
889 HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
891 modes[1].mode = HOSTAPD_MODE_IEEE80211B;
892 modes[1].num_channels = 1;
893 modes[1].num_rates = 1;
894 modes[1].channels = os_zalloc(sizeof(struct hostapd_channel_data));
895 modes[1].rates = os_zalloc(sizeof(struct hostapd_rate_data));
896 if (modes[1].channels == NULL || modes[1].rates == NULL) {
897 hostapd_free_hw_features(modes, *num_modes);
900 modes[1].channels[0].chan = 1;
901 modes[1].channels[0].freq = 2412;
902 modes[1].channels[0].flag = 0;
903 modes[1].rates[0].rate = 10;
904 modes[1].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
905 HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
907 modes[2].mode = HOSTAPD_MODE_IEEE80211A;
908 modes[2].num_channels = 1;
909 modes[2].num_rates = 1;
910 modes[2].channels = os_zalloc(sizeof(struct hostapd_channel_data));
911 modes[2].rates = os_zalloc(sizeof(struct hostapd_rate_data));
912 if (modes[2].channels == NULL || modes[2].rates == NULL) {
913 hostapd_free_hw_features(modes, *num_modes);
916 modes[2].channels[0].chan = 60;
917 modes[2].channels[0].freq = 5300;
918 modes[2].channels[0].flag = 0;
919 modes[2].rates[0].rate = 60;
920 modes[2].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
921 HOSTAPD_RATE_MANDATORY;
927 static int test_driver_bss_add(void *priv, const char *ifname, const u8 *bssid)
929 struct test_driver_data *drv = priv;
930 struct test_driver_bss *bss;
932 wpa_printf(MSG_DEBUG, "%s(ifname=%s bssid=" MACSTR ")",
933 __func__, ifname, MAC2STR(bssid));
935 bss = os_zalloc(sizeof(*bss));
939 os_strlcpy(bss->ifname, ifname, IFNAMSIZ);
940 memcpy(bss->bssid, bssid, ETH_ALEN);
942 bss->next = drv->bss;
949 static int test_driver_bss_remove(void *priv, const char *ifname)
951 struct test_driver_data *drv = priv;
952 struct test_driver_bss *bss, *prev;
953 struct test_client_socket *cli, *prev_c;
955 wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
957 for (prev = NULL, bss = drv->bss; bss; prev = bss, bss = bss->next) {
958 if (strcmp(bss->ifname, ifname) != 0)
962 prev->next = bss->next;
964 drv->bss = bss->next;
966 for (prev_c = NULL, cli = drv->cli; cli;
967 prev_c = cli, cli = cli->next) {
971 prev_c->next = cli->next;
973 drv->cli = cli->next;
978 test_driver_free_bss(bss);
986 static int test_driver_if_add(const char *iface, void *priv,
987 enum hostapd_driver_if_type type, char *ifname,
990 wpa_printf(MSG_DEBUG, "%s(iface=%s type=%d ifname=%s)",
991 __func__, iface, type, ifname);
996 static int test_driver_if_update(void *priv, enum hostapd_driver_if_type type,
997 char *ifname, const u8 *addr)
999 wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
1004 static int test_driver_if_remove(void *priv, enum hostapd_driver_if_type type,
1005 const char *ifname, const u8 *addr)
1007 wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
1012 static int test_driver_valid_bss_mask(void *priv, const u8 *addr,
1019 static int test_driver_set_ssid(const char *ifname, void *priv, const u8 *buf,
1022 struct test_driver_data *drv = priv;
1023 struct test_driver_bss *bss;
1025 wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
1026 wpa_hexdump_ascii(MSG_DEBUG, "test_driver_set_ssid: SSID", buf, len);
1028 for (bss = drv->bss; bss; bss = bss->next) {
1029 if (strcmp(bss->ifname, ifname) != 0)
1032 if (len < 0 || (size_t) len > sizeof(bss->ssid))
1035 memcpy(bss->ssid, buf, len);
1036 bss->ssid_len = len;
1045 static int test_driver_set_privacy(const char *ifname, void *priv, int enabled)
1047 struct test_driver_data *drv = priv;
1048 struct test_driver_bss *bss;
1050 wpa_printf(MSG_DEBUG, "%s(ifname=%s enabled=%d)",
1051 __func__, ifname, enabled);
1053 for (bss = drv->bss; bss; bss = bss->next) {
1054 if (strcmp(bss->ifname, ifname) != 0)
1057 bss->privacy = enabled;
1066 static int test_driver_set_encryption(const char *iface, void *priv,
1067 const char *alg, const u8 *addr, int idx,
1068 const u8 *key, size_t key_len, int txkey)
1070 wpa_printf(MSG_DEBUG, "%s(iface=%s alg=%s idx=%d txkey=%d)",
1071 __func__, iface, alg, idx, txkey);
1073 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
1075 wpa_hexdump_key(MSG_DEBUG, " key", key, key_len);
1080 static int test_driver_set_sta_vlan(void *priv, const u8 *addr,
1081 const char *ifname, int vlan_id)
1083 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " ifname=%s vlan_id=%d)",
1084 __func__, MAC2STR(addr), ifname, vlan_id);
1089 static int test_driver_sta_add(const char *ifname, void *priv, const u8 *addr,
1090 u16 aid, u16 capability, u8 *supp_rates,
1091 size_t supp_rates_len, int flags,
1092 u16 listen_interval)
1094 struct test_driver_data *drv = priv;
1095 struct test_client_socket *cli;
1096 struct test_driver_bss *bss;
1098 wpa_printf(MSG_DEBUG, "%s(ifname=%s addr=" MACSTR " aid=%d "
1099 "capability=0x%x flags=0x%x listen_interval=%d)",
1100 __func__, ifname, MAC2STR(addr), aid, capability, flags,
1102 wpa_hexdump(MSG_DEBUG, "test_driver_sta_add - supp_rates",
1103 supp_rates, supp_rates_len);
1107 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
1112 wpa_printf(MSG_DEBUG, "%s: no matching client entry",
1117 for (bss = drv->bss; bss; bss = bss->next) {
1118 if (strcmp(ifname, bss->ifname) == 0)
1122 wpa_printf(MSG_DEBUG, "%s: No matching interface found from "
1123 "configured BSSes", __func__);
1133 static void * test_driver_init(struct hostapd_data *hapd)
1135 struct test_driver_data *drv;
1136 struct sockaddr_un addr;
1138 drv = os_zalloc(sizeof(struct test_driver_data));
1140 printf("Could not allocate memory for test driver data\n");
1143 drv->bss = os_zalloc(sizeof(*drv->bss));
1144 if (drv->bss == NULL) {
1145 printf("Could not allocate memory for test driver BSS data\n");
1152 /* Generate a MAC address to help testing with multiple APs */
1153 hapd->own_addr[0] = 0x02; /* locally administered */
1154 sha1_prf((const u8 *) hapd->conf->iface, strlen(hapd->conf->iface),
1155 "hostapd test bssid generation",
1156 (const u8 *) hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len,
1157 hapd->own_addr + 1, ETH_ALEN - 1);
1159 os_strlcpy(drv->bss->ifname, hapd->conf->iface, IFNAMSIZ);
1160 memcpy(drv->bss->bssid, hapd->own_addr, ETH_ALEN);
1162 if (hapd->conf->test_socket) {
1163 if (strlen(hapd->conf->test_socket) >= sizeof(addr.sun_path)) {
1164 printf("Too long test_socket path\n");
1165 test_driver_free_priv(drv);
1168 if (strncmp(hapd->conf->test_socket, "DIR:", 4) == 0) {
1169 size_t len = strlen(hapd->conf->test_socket) + 30;
1170 drv->socket_dir = strdup(hapd->conf->test_socket + 4);
1171 drv->own_socket_path = malloc(len);
1172 if (drv->own_socket_path) {
1173 snprintf(drv->own_socket_path, len,
1175 hapd->conf->test_socket + 4,
1176 MAC2STR(hapd->own_addr));
1179 drv->own_socket_path = strdup(hapd->conf->test_socket);
1181 if (drv->own_socket_path == NULL) {
1182 test_driver_free_priv(drv);
1186 drv->test_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
1187 if (drv->test_socket < 0) {
1188 perror("socket(PF_UNIX)");
1189 test_driver_free_priv(drv);
1193 memset(&addr, 0, sizeof(addr));
1194 addr.sun_family = AF_UNIX;
1195 os_strlcpy(addr.sun_path, drv->own_socket_path,
1196 sizeof(addr.sun_path));
1197 if (bind(drv->test_socket, (struct sockaddr *) &addr,
1198 sizeof(addr)) < 0) {
1199 perror("bind(PF_UNIX)");
1200 close(drv->test_socket);
1201 unlink(drv->own_socket_path);
1202 test_driver_free_priv(drv);
1205 eloop_register_read_sock(drv->test_socket,
1206 test_driver_receive_unix, drv, NULL);
1208 drv->test_socket = -1;
1214 static void test_driver_deinit(void *priv)
1216 struct test_driver_data *drv = priv;
1217 struct test_client_socket *cli, *prev;
1226 if (drv->test_socket >= 0) {
1227 eloop_unregister_read_sock(drv->test_socket);
1228 close(drv->test_socket);
1229 unlink(drv->own_socket_path);
1232 /* There should be only one BSS remaining at this point. */
1233 if (drv->bss == NULL)
1234 wpa_printf(MSG_ERROR, "%s: drv->bss == NULL", __func__);
1235 else if (drv->bss->next)
1236 wpa_printf(MSG_ERROR, "%s: drv->bss->next != NULL", __func__);
1238 test_driver_free_priv(drv);
1242 const struct wpa_driver_ops wpa_driver_test_ops = {
1244 .init = test_driver_init,
1245 .deinit = test_driver_deinit,
1246 .send_eapol = test_driver_send_eapol,
1247 .send_mgmt_frame = test_driver_send_mgmt_frame,
1248 .set_generic_elem = test_driver_set_generic_elem,
1249 .sta_deauth = test_driver_sta_deauth,
1250 .sta_disassoc = test_driver_sta_disassoc,
1251 .get_hw_feature_data = test_driver_get_hw_feature_data,
1252 .bss_add = test_driver_bss_add,
1253 .bss_remove = test_driver_bss_remove,
1254 .if_add = test_driver_if_add,
1255 .if_update = test_driver_if_update,
1256 .if_remove = test_driver_if_remove,
1257 .valid_bss_mask = test_driver_valid_bss_mask,
1258 .set_ssid = test_driver_set_ssid,
1259 .set_privacy = test_driver_set_privacy,
1260 .set_encryption = test_driver_set_encryption,
1261 .set_sta_vlan = test_driver_set_sta_vlan,
1262 .sta_add = test_driver_sta_add,
1263 .send_ether = test_driver_send_ether,
1264 .set_wps_beacon_ie = test_driver_set_wps_beacon_ie,
1265 .set_wps_probe_resp_ie = test_driver_set_wps_probe_resp_ie,