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.
24 #include "l2_packet/l2_packet.h"
25 #include "hw_features.h"
26 #include "wps_hostapd.h"
29 struct test_client_socket {
30 struct test_client_socket *next;
32 struct sockaddr_un un;
34 struct test_driver_bss *bss;
37 struct test_driver_bss {
38 struct test_driver_bss *next;
39 char ifname[IFNAMSIZ + 1];
44 size_t wps_beacon_ie_len;
45 u8 *wps_probe_resp_ie;
46 size_t wps_probe_resp_ie_len;
52 struct test_driver_data {
53 struct hostapd_data *hapd;
54 struct test_client_socket *cli;
56 struct test_driver_bss *bss;
58 char *own_socket_path;
63 static void test_driver_free_bss(struct test_driver_bss *bss)
66 free(bss->wps_beacon_ie);
67 free(bss->wps_probe_resp_ie);
72 static void test_driver_free_priv(struct test_driver_data *drv)
74 struct test_driver_bss *bss, *prev;
83 test_driver_free_bss(prev);
85 free(drv->own_socket_path);
86 free(drv->socket_dir);
91 static struct test_client_socket *
92 test_driver_get_cli(struct test_driver_data *drv, struct sockaddr_un *from,
95 struct test_client_socket *cli = drv->cli;
98 if (cli->unlen == fromlen &&
99 strncmp(cli->un.sun_path, from->sun_path,
100 fromlen - sizeof(cli->un.sun_family)) == 0)
109 static int test_driver_send_eapol(void *priv, const u8 *addr, const u8 *data,
110 size_t data_len, int encrypt,
113 struct test_driver_data *drv = priv;
114 struct test_client_socket *cli;
117 struct l2_ethhdr eth;
119 if (drv->test_socket < 0)
124 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
130 wpa_printf(MSG_DEBUG, "%s: no destination client entry",
135 memcpy(eth.h_dest, addr, ETH_ALEN);
136 memcpy(eth.h_source, own_addr, ETH_ALEN);
137 eth.h_proto = host_to_be16(ETH_P_EAPOL);
139 io[0].iov_base = "EAPOL ";
141 io[1].iov_base = ð
142 io[1].iov_len = sizeof(eth);
143 io[2].iov_base = (u8 *) data;
144 io[2].iov_len = data_len;
146 memset(&msg, 0, sizeof(msg));
149 msg.msg_name = &cli->un;
150 msg.msg_namelen = cli->unlen;
151 return sendmsg(drv->test_socket, &msg, 0);
155 static int test_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
156 u16 proto, const u8 *data, size_t data_len)
158 struct test_driver_data *drv = priv;
161 struct l2_ethhdr eth;
163 struct sockaddr_un addr;
166 int ret = 0, broadcast = 0, count = 0;
168 if (drv->test_socket < 0 || drv->socket_dir == NULL) {
169 wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d "
171 __func__, drv->test_socket, drv->socket_dir);
175 broadcast = memcmp(dst, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
176 snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dst));
178 memcpy(eth.h_dest, dst, ETH_ALEN);
179 memcpy(eth.h_source, src, ETH_ALEN);
180 eth.h_proto = host_to_be16(proto);
182 io[0].iov_base = "ETHER ";
184 io[1].iov_base = ð
185 io[1].iov_len = sizeof(eth);
186 io[2].iov_base = (u8 *) data;
187 io[2].iov_len = data_len;
189 memset(&msg, 0, sizeof(msg));
193 dir = opendir(drv->socket_dir);
195 perror("test_driver: opendir");
198 while ((dent = readdir(dir))) {
199 #ifdef _DIRENT_HAVE_D_TYPE
200 /* Skip the file if it is not a socket. Also accept
201 * DT_UNKNOWN (0) in case the C library or underlying file
202 * system does not support d_type. */
203 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
205 #endif /* _DIRENT_HAVE_D_TYPE */
206 if (strcmp(dent->d_name, ".") == 0 ||
207 strcmp(dent->d_name, "..") == 0)
210 memset(&addr, 0, sizeof(addr));
211 addr.sun_family = AF_UNIX;
212 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
213 drv->socket_dir, dent->d_name);
215 if (strcmp(addr.sun_path, drv->own_socket_path) == 0)
217 if (!broadcast && strstr(dent->d_name, desttxt) == NULL)
220 wpa_printf(MSG_DEBUG, "%s: Send ether frame to %s",
221 __func__, dent->d_name);
223 msg.msg_name = &addr;
224 msg.msg_namelen = sizeof(addr);
225 ret = sendmsg(drv->test_socket, &msg, 0);
227 perror("driver_test: sendmsg");
232 if (!broadcast && count == 0) {
233 wpa_printf(MSG_DEBUG, "%s: Destination " MACSTR " not found",
234 __func__, MAC2STR(dst));
242 static int test_driver_send_mgmt_frame(void *priv, const void *buf,
243 size_t len, int flags)
245 struct test_driver_data *drv = priv;
249 int ret = 0, broadcast = 0;
251 struct sockaddr_un addr;
254 struct ieee80211_hdr *hdr;
257 if (drv->test_socket < 0 || len < 10 || drv->socket_dir == NULL) {
258 wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d len=%lu"
260 __func__, drv->test_socket, (unsigned long) len,
267 broadcast = memcmp(dest, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
268 snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dest));
270 io[0].iov_base = "MLME ";
272 io[1].iov_base = (void *) buf;
275 memset(&msg, 0, sizeof(msg));
279 dir = opendir(drv->socket_dir);
281 perror("test_driver: opendir");
284 while ((dent = readdir(dir))) {
285 #ifdef _DIRENT_HAVE_D_TYPE
286 /* Skip the file if it is not a socket. Also accept
287 * DT_UNKNOWN (0) in case the C library or underlying file
288 * system does not support d_type. */
289 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
291 #endif /* _DIRENT_HAVE_D_TYPE */
292 if (strcmp(dent->d_name, ".") == 0 ||
293 strcmp(dent->d_name, "..") == 0)
296 memset(&addr, 0, sizeof(addr));
297 addr.sun_family = AF_UNIX;
298 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
299 drv->socket_dir, dent->d_name);
301 if (strcmp(addr.sun_path, drv->own_socket_path) == 0)
303 if (!broadcast && strstr(dent->d_name, desttxt) == NULL)
306 wpa_printf(MSG_DEBUG, "%s: Send management frame to %s",
307 __func__, dent->d_name);
309 msg.msg_name = &addr;
310 msg.msg_namelen = sizeof(addr);
311 ret = sendmsg(drv->test_socket, &msg, 0);
313 perror("driver_test: sendmsg");
317 hdr = (struct ieee80211_hdr *) buf;
318 fc = le_to_host16(hdr->frame_control);
319 hostapd_mgmt_tx_cb(drv->hapd, (u8 *) buf, len, WLAN_FC_GET_STYPE(fc),
326 static void test_driver_scan(struct test_driver_data *drv,
327 struct sockaddr_un *from, socklen_t fromlen,
330 char buf[512], *pos, *end;
332 struct test_driver_bss *bss;
337 /* data: optional [ ' ' | STA-addr | ' ' | IEs(hex) ] */
339 wpa_printf(MSG_DEBUG, "test_driver: SCAN");
343 hwaddr_aton(data + 1, sa)) {
344 wpa_printf(MSG_DEBUG, "test_driver: Unexpected SCAN "
352 ielen = os_strlen(data) / 2;
353 if (ielen > sizeof(ie))
355 if (hexstr2bin(data, ie, ielen) < 0)
358 wpa_printf(MSG_DEBUG, "test_driver: Scan from " MACSTR,
360 wpa_hexdump(MSG_MSGDUMP, "test_driver: scan IEs", ie, ielen);
362 hostapd_wps_probe_req_rx(drv->hapd, sa, ie, ielen);
365 for (bss = drv->bss; bss; bss = bss->next) {
367 end = buf + sizeof(buf);
369 /* reply: SCANRESP BSSID SSID IEs */
370 ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
371 MAC2STR(bss->bssid));
372 if (ret < 0 || ret >= end - pos)
375 pos += wpa_snprintf_hex(pos, end - pos,
376 bss->ssid, bss->ssid_len);
377 ret = snprintf(pos, end - pos, " ");
378 if (ret < 0 || ret >= end - pos)
381 pos += wpa_snprintf_hex(pos, end - pos, bss->ie, bss->ielen);
382 pos += wpa_snprintf_hex(pos, end - pos, bss->wps_probe_resp_ie,
383 bss->wps_probe_resp_ie_len);
386 ret = snprintf(pos, end - pos, " PRIVACY");
387 if (ret < 0 || ret >= end - pos)
392 sendto(drv->test_socket, buf, pos - buf, 0,
393 (struct sockaddr *) from, fromlen);
398 static struct hostapd_data * test_driver_get_hapd(struct test_driver_data *drv,
399 struct test_driver_bss *bss)
401 struct hostapd_iface *iface = drv->hapd->iface;
402 struct hostapd_data *hapd = NULL;
406 wpa_printf(MSG_DEBUG, "%s: bss == NULL", __func__);
410 for (i = 0; i < iface->num_bss; i++) {
411 hapd = iface->bss[i];
412 if (memcmp(hapd->own_addr, bss->bssid, ETH_ALEN) == 0)
415 if (i == iface->num_bss) {
416 wpa_printf(MSG_DEBUG, "%s: no matching interface entry found "
417 "for BSSID " MACSTR, __func__, MAC2STR(bss->bssid));
425 static int test_driver_new_sta(struct test_driver_data *drv,
426 struct test_driver_bss *bss, const u8 *addr,
427 const u8 *ie, size_t ielen)
429 struct hostapd_data *hapd;
431 hapd = test_driver_get_hapd(drv, bss);
435 return hostapd_notif_assoc(hapd, addr, ie, ielen);
439 static void test_driver_assoc(struct test_driver_data *drv,
440 struct sockaddr_un *from, socklen_t fromlen,
443 struct test_client_socket *cli;
444 u8 ie[256], ssid[32];
445 size_t ielen, ssid_len = 0;
446 char *pos, *pos2, cmd[50];
447 struct test_driver_bss *bss;
449 /* data: STA-addr SSID(hex) IEs(hex) */
451 cli = os_zalloc(sizeof(*cli));
455 if (hwaddr_aton(data, cli->addr)) {
456 printf("test_socket: Invalid MAC address '%s' in ASSOC\n",
464 pos2 = strchr(pos, ' ');
467 ssid_len = (pos2 - pos) / 2;
468 if (hexstr2bin(pos, ssid, ssid_len) < 0) {
469 wpa_printf(MSG_DEBUG, "%s: Invalid SSID", __func__);
473 wpa_hexdump_ascii(MSG_DEBUG, "test_driver_assoc: SSID",
477 ielen = strlen(pos) / 2;
478 if (ielen > sizeof(ie))
480 if (hexstr2bin(pos, ie, ielen) < 0)
484 for (bss = drv->bss; bss; bss = bss->next) {
485 if (bss->ssid_len == ssid_len &&
486 memcmp(bss->ssid, ssid, ssid_len) == 0)
490 wpa_printf(MSG_DEBUG, "%s: No matching SSID found from "
491 "configured BSSes", __func__);
497 memcpy(&cli->un, from, sizeof(cli->un));
498 cli->unlen = fromlen;
499 cli->next = drv->cli;
501 wpa_hexdump_ascii(MSG_DEBUG, "test_socket: ASSOC sun_path",
502 (const u8 *) cli->un.sun_path,
503 cli->unlen - sizeof(cli->un.sun_family));
505 snprintf(cmd, sizeof(cmd), "ASSOCRESP " MACSTR " 0",
506 MAC2STR(bss->bssid));
507 sendto(drv->test_socket, cmd, strlen(cmd), 0,
508 (struct sockaddr *) from, fromlen);
510 if (test_driver_new_sta(drv, bss, cli->addr, ie, ielen) < 0) {
511 wpa_printf(MSG_DEBUG, "test_driver: failed to add new STA");
516 static void test_driver_disassoc(struct test_driver_data *drv,
517 struct sockaddr_un *from, socklen_t fromlen)
519 struct test_client_socket *cli;
521 cli = test_driver_get_cli(drv, from, fromlen);
525 hostapd_notif_disassoc(drv->hapd, cli->addr);
529 static void test_driver_eapol(struct test_driver_data *drv,
530 struct sockaddr_un *from, socklen_t fromlen,
531 u8 *data, size_t datalen)
533 struct test_client_socket *cli;
535 /* Skip Ethernet header */
536 wpa_printf(MSG_DEBUG, "test_driver: dst=" MACSTR " src="
537 MACSTR " proto=%04x",
538 MAC2STR(data), MAC2STR(data + ETH_ALEN),
539 WPA_GET_BE16(data + 2 * ETH_ALEN));
543 cli = test_driver_get_cli(drv, from, fromlen);
545 struct hostapd_data *hapd;
546 hapd = test_driver_get_hapd(drv, cli->bss);
549 hostapd_eapol_receive(hapd, cli->addr, data, datalen);
551 wpa_printf(MSG_DEBUG, "test_socket: EAPOL from unknown "
557 static void test_driver_ether(struct test_driver_data *drv,
558 struct sockaddr_un *from, socklen_t fromlen,
559 u8 *data, size_t datalen)
561 struct l2_ethhdr *eth;
563 if (datalen < sizeof(*eth))
566 eth = (struct l2_ethhdr *) data;
567 wpa_printf(MSG_DEBUG, "test_driver: RX ETHER dst=" MACSTR " src="
568 MACSTR " proto=%04x",
569 MAC2STR(eth->h_dest), MAC2STR(eth->h_source),
570 be_to_host16(eth->h_proto));
572 #ifdef CONFIG_IEEE80211R
573 if (be_to_host16(eth->h_proto) == ETH_P_RRB) {
574 wpa_ft_rrb_rx(drv->hapd->wpa_auth, eth->h_source,
575 data + sizeof(*eth), datalen - sizeof(*eth));
577 #endif /* CONFIG_IEEE80211R */
581 static void test_driver_mlme(struct test_driver_data *drv,
582 struct sockaddr_un *from, socklen_t fromlen,
583 u8 *data, size_t datalen)
585 struct ieee80211_hdr *hdr;
588 hdr = (struct ieee80211_hdr *) data;
590 if (test_driver_get_cli(drv, from, fromlen) == NULL && datalen >= 16) {
591 struct test_client_socket *cli;
592 cli = os_zalloc(sizeof(*cli));
595 wpa_printf(MSG_DEBUG, "Adding client entry for " MACSTR,
596 MAC2STR(hdr->addr2));
597 memcpy(cli->addr, hdr->addr2, ETH_ALEN);
598 memcpy(&cli->un, from, sizeof(cli->un));
599 cli->unlen = fromlen;
600 cli->next = drv->cli;
604 wpa_hexdump(MSG_MSGDUMP, "test_driver_mlme: received frame",
606 fc = le_to_host16(hdr->frame_control);
607 if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT) {
608 wpa_printf(MSG_ERROR, "%s: received non-mgmt frame",
612 hostapd_mgmt_rx(drv->hapd, data, datalen, WLAN_FC_GET_STYPE(fc), NULL);
616 static void test_driver_receive_unix(int sock, void *eloop_ctx, void *sock_ctx)
618 struct test_driver_data *drv = eloop_ctx;
621 struct sockaddr_un from;
622 socklen_t fromlen = sizeof(from);
624 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
625 (struct sockaddr *) &from, &fromlen);
627 perror("recvfrom(test_socket)");
632 wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
634 if (strncmp(buf, "SCAN", 4) == 0) {
635 test_driver_scan(drv, &from, fromlen, buf + 4);
636 } else if (strncmp(buf, "ASSOC ", 6) == 0) {
637 test_driver_assoc(drv, &from, fromlen, buf + 6);
638 } else if (strcmp(buf, "DISASSOC") == 0) {
639 test_driver_disassoc(drv, &from, fromlen);
640 } else if (strncmp(buf, "EAPOL ", 6) == 0) {
641 test_driver_eapol(drv, &from, fromlen, (u8 *) buf + 6,
643 } else if (strncmp(buf, "ETHER ", 6) == 0) {
644 test_driver_ether(drv, &from, fromlen, (u8 *) buf + 6,
646 } else if (strncmp(buf, "MLME ", 5) == 0) {
647 test_driver_mlme(drv, &from, fromlen, (u8 *) buf + 5, res - 5);
649 wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
655 static struct test_driver_bss *
656 test_driver_get_bss(struct test_driver_data *drv, const char *ifname)
658 struct test_driver_bss *bss;
660 for (bss = drv->bss; bss; bss = bss->next) {
661 if (strcmp(bss->ifname, ifname) == 0)
668 static int test_driver_set_generic_elem(const char *ifname, void *priv,
669 const u8 *elem, size_t elem_len)
671 struct test_driver_data *drv = priv;
672 struct test_driver_bss *bss;
674 bss = test_driver_get_bss(drv, ifname);
686 bss->ie = malloc(elem_len);
687 if (bss->ie == NULL) {
692 memcpy(bss->ie, elem, elem_len);
693 bss->ielen = elem_len;
698 static int test_driver_set_wps_beacon_ie(const char *ifname, void *priv,
699 const u8 *ie, size_t len)
701 struct test_driver_data *drv = priv;
702 struct test_driver_bss *bss;
704 wpa_hexdump(MSG_DEBUG, "test_driver: Beacon WPS IE", ie, len);
705 bss = test_driver_get_bss(drv, ifname);
709 free(bss->wps_beacon_ie);
712 bss->wps_beacon_ie = NULL;
713 bss->wps_beacon_ie_len = 0;
717 bss->wps_beacon_ie = malloc(len);
718 if (bss->wps_beacon_ie == NULL) {
719 bss->wps_beacon_ie_len = 0;
723 memcpy(bss->wps_beacon_ie, ie, len);
724 bss->wps_beacon_ie_len = len;
729 static int test_driver_set_wps_probe_resp_ie(const char *ifname, void *priv,
730 const u8 *ie, size_t len)
732 struct test_driver_data *drv = priv;
733 struct test_driver_bss *bss;
735 wpa_hexdump(MSG_DEBUG, "test_driver: ProbeResp WPS IE", ie, len);
736 bss = test_driver_get_bss(drv, ifname);
740 free(bss->wps_probe_resp_ie);
743 bss->wps_probe_resp_ie = NULL;
744 bss->wps_probe_resp_ie_len = 0;
748 bss->wps_probe_resp_ie = malloc(len);
749 if (bss->wps_probe_resp_ie == NULL) {
750 bss->wps_probe_resp_ie_len = 0;
754 memcpy(bss->wps_probe_resp_ie, ie, len);
755 bss->wps_probe_resp_ie_len = len;
760 static int test_driver_sta_deauth(void *priv, const u8 *addr, int reason)
762 struct test_driver_data *drv = priv;
763 struct test_client_socket *cli;
765 if (drv->test_socket < 0)
770 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
778 return sendto(drv->test_socket, "DEAUTH", 6, 0,
779 (struct sockaddr *) &cli->un, cli->unlen);
783 static int test_driver_sta_disassoc(void *priv, const u8 *addr, int reason)
785 struct test_driver_data *drv = priv;
786 struct test_client_socket *cli;
788 if (drv->test_socket < 0)
793 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
801 return sendto(drv->test_socket, "DISASSOC", 8, 0,
802 (struct sockaddr *) &cli->un, cli->unlen);
806 static struct hostapd_hw_modes *
807 test_driver_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
809 struct hostapd_hw_modes *modes;
813 modes = os_zalloc(*num_modes * sizeof(struct hostapd_hw_modes));
816 modes[0].mode = HOSTAPD_MODE_IEEE80211G;
817 modes[0].num_channels = 1;
818 modes[0].num_rates = 1;
819 modes[0].channels = os_zalloc(sizeof(struct hostapd_channel_data));
820 modes[0].rates = os_zalloc(sizeof(struct hostapd_rate_data));
821 if (modes[0].channels == NULL || modes[0].rates == NULL) {
822 hostapd_free_hw_features(modes, *num_modes);
825 modes[0].channels[0].chan = 1;
826 modes[0].channels[0].freq = 2412;
827 modes[0].channels[0].flag = 0;
828 modes[0].rates[0].rate = 10;
829 modes[0].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
830 HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
832 modes[1].mode = HOSTAPD_MODE_IEEE80211B;
833 modes[1].num_channels = 1;
834 modes[1].num_rates = 1;
835 modes[1].channels = os_zalloc(sizeof(struct hostapd_channel_data));
836 modes[1].rates = os_zalloc(sizeof(struct hostapd_rate_data));
837 if (modes[1].channels == NULL || modes[1].rates == NULL) {
838 hostapd_free_hw_features(modes, *num_modes);
841 modes[1].channels[0].chan = 1;
842 modes[1].channels[0].freq = 2412;
843 modes[1].channels[0].flag = 0;
844 modes[1].rates[0].rate = 10;
845 modes[1].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
846 HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
848 modes[2].mode = HOSTAPD_MODE_IEEE80211A;
849 modes[2].num_channels = 1;
850 modes[2].num_rates = 1;
851 modes[2].channels = os_zalloc(sizeof(struct hostapd_channel_data));
852 modes[2].rates = os_zalloc(sizeof(struct hostapd_rate_data));
853 if (modes[2].channels == NULL || modes[2].rates == NULL) {
854 hostapd_free_hw_features(modes, *num_modes);
857 modes[2].channels[0].chan = 60;
858 modes[2].channels[0].freq = 5300;
859 modes[2].channels[0].flag = 0;
860 modes[2].rates[0].rate = 60;
861 modes[2].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
862 HOSTAPD_RATE_MANDATORY;
868 static int test_driver_bss_add(void *priv, const char *ifname, const u8 *bssid)
870 struct test_driver_data *drv = priv;
871 struct test_driver_bss *bss;
873 wpa_printf(MSG_DEBUG, "%s(ifname=%s bssid=" MACSTR ")",
874 __func__, ifname, MAC2STR(bssid));
876 bss = os_zalloc(sizeof(*bss));
880 os_strlcpy(bss->ifname, ifname, IFNAMSIZ);
881 memcpy(bss->bssid, bssid, ETH_ALEN);
883 bss->next = drv->bss;
890 static int test_driver_bss_remove(void *priv, const char *ifname)
892 struct test_driver_data *drv = priv;
893 struct test_driver_bss *bss, *prev;
894 struct test_client_socket *cli, *prev_c;
896 wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
898 for (prev = NULL, bss = drv->bss; bss; prev = bss, bss = bss->next) {
899 if (strcmp(bss->ifname, ifname) != 0)
903 prev->next = bss->next;
905 drv->bss = bss->next;
907 for (prev_c = NULL, cli = drv->cli; cli;
908 prev_c = cli, cli = cli->next) {
912 prev_c->next = cli->next;
914 drv->cli = cli->next;
919 test_driver_free_bss(bss);
927 static int test_driver_if_add(const char *iface, void *priv,
928 enum hostapd_driver_if_type type, char *ifname,
931 wpa_printf(MSG_DEBUG, "%s(iface=%s type=%d ifname=%s)",
932 __func__, iface, type, ifname);
937 static int test_driver_if_update(void *priv, enum hostapd_driver_if_type type,
938 char *ifname, const u8 *addr)
940 wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
945 static int test_driver_if_remove(void *priv, enum hostapd_driver_if_type type,
946 const char *ifname, const u8 *addr)
948 wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
953 static int test_driver_valid_bss_mask(void *priv, const u8 *addr,
960 static int test_driver_set_ssid(const char *ifname, void *priv, const u8 *buf,
963 struct test_driver_data *drv = priv;
964 struct test_driver_bss *bss;
966 wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
967 wpa_hexdump_ascii(MSG_DEBUG, "test_driver_set_ssid: SSID", buf, len);
969 for (bss = drv->bss; bss; bss = bss->next) {
970 if (strcmp(bss->ifname, ifname) != 0)
973 if (len < 0 || (size_t) len > sizeof(bss->ssid))
976 memcpy(bss->ssid, buf, len);
986 static int test_driver_set_privacy(const char *ifname, void *priv, int enabled)
988 struct test_driver_data *drv = priv;
989 struct test_driver_bss *bss;
991 wpa_printf(MSG_DEBUG, "%s(ifname=%s enabled=%d)",
992 __func__, ifname, enabled);
994 for (bss = drv->bss; bss; bss = bss->next) {
995 if (strcmp(bss->ifname, ifname) != 0)
998 bss->privacy = enabled;
1007 static int test_driver_set_encryption(const char *iface, void *priv,
1008 const char *alg, const u8 *addr, int idx,
1009 const u8 *key, size_t key_len, int txkey)
1011 wpa_printf(MSG_DEBUG, "%s(iface=%s alg=%s idx=%d txkey=%d)",
1012 __func__, iface, alg, idx, txkey);
1014 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
1016 wpa_hexdump_key(MSG_DEBUG, " key", key, key_len);
1021 static int test_driver_set_sta_vlan(void *priv, const u8 *addr,
1022 const char *ifname, int vlan_id)
1024 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " ifname=%s vlan_id=%d)",
1025 __func__, MAC2STR(addr), ifname, vlan_id);
1030 static int test_driver_sta_add(const char *ifname, void *priv, const u8 *addr,
1031 u16 aid, u16 capability, u8 *supp_rates,
1032 size_t supp_rates_len, int flags,
1033 u16 listen_interval)
1035 struct test_driver_data *drv = priv;
1036 struct test_client_socket *cli;
1037 struct test_driver_bss *bss;
1039 wpa_printf(MSG_DEBUG, "%s(ifname=%s addr=" MACSTR " aid=%d "
1040 "capability=0x%x flags=0x%x listen_interval=%d)",
1041 __func__, ifname, MAC2STR(addr), aid, capability, flags,
1043 wpa_hexdump(MSG_DEBUG, "test_driver_sta_add - supp_rates",
1044 supp_rates, supp_rates_len);
1048 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
1053 wpa_printf(MSG_DEBUG, "%s: no matching client entry",
1058 for (bss = drv->bss; bss; bss = bss->next) {
1059 if (strcmp(ifname, bss->ifname) == 0)
1063 wpa_printf(MSG_DEBUG, "%s: No matching interface found from "
1064 "configured BSSes", __func__);
1074 static void * test_driver_init(struct hostapd_data *hapd)
1076 struct test_driver_data *drv;
1077 struct sockaddr_un addr_un;
1078 struct sockaddr_in addr_in;
1079 struct sockaddr *addr;
1082 drv = os_zalloc(sizeof(struct test_driver_data));
1084 printf("Could not allocate memory for test driver data\n");
1087 drv->bss = os_zalloc(sizeof(*drv->bss));
1088 if (drv->bss == NULL) {
1089 printf("Could not allocate memory for test driver BSS data\n");
1096 /* Generate a MAC address to help testing with multiple APs */
1097 hapd->own_addr[0] = 0x02; /* locally administered */
1098 sha1_prf((const u8 *) hapd->conf->iface, strlen(hapd->conf->iface),
1099 "hostapd test bssid generation",
1100 (const u8 *) hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len,
1101 hapd->own_addr + 1, ETH_ALEN - 1);
1103 os_strlcpy(drv->bss->ifname, hapd->conf->iface, IFNAMSIZ);
1104 memcpy(drv->bss->bssid, hapd->own_addr, ETH_ALEN);
1106 if (hapd->conf->test_socket) {
1107 if (strlen(hapd->conf->test_socket) >=
1108 sizeof(addr_un.sun_path)) {
1109 printf("Too long test_socket path\n");
1110 test_driver_free_priv(drv);
1113 if (strncmp(hapd->conf->test_socket, "DIR:", 4) == 0) {
1114 size_t len = strlen(hapd->conf->test_socket) + 30;
1115 drv->socket_dir = strdup(hapd->conf->test_socket + 4);
1116 drv->own_socket_path = malloc(len);
1117 if (drv->own_socket_path) {
1118 snprintf(drv->own_socket_path, len,
1120 hapd->conf->test_socket + 4,
1121 MAC2STR(hapd->own_addr));
1123 } else if (strncmp(hapd->conf->test_socket, "UDP:", 4) == 0) {
1124 drv->udp_port = atoi(hapd->conf->test_socket + 4);
1126 drv->own_socket_path = strdup(hapd->conf->test_socket);
1128 if (drv->own_socket_path == NULL && drv->udp_port == 0) {
1129 test_driver_free_priv(drv);
1133 drv->test_socket = socket(drv->udp_port ? PF_INET : PF_UNIX,
1135 if (drv->test_socket < 0) {
1137 test_driver_free_priv(drv);
1141 if (drv->udp_port) {
1142 os_memset(&addr_in, 0, sizeof(addr_in));
1143 addr_in.sin_family = AF_INET;
1144 addr_in.sin_port = htons(drv->udp_port);
1145 addr = (struct sockaddr *) &addr_in;
1146 alen = sizeof(addr_in);
1148 os_memset(&addr_un, 0, sizeof(addr_un));
1149 addr_un.sun_family = AF_UNIX;
1150 os_strlcpy(addr_un.sun_path, drv->own_socket_path,
1151 sizeof(addr_un.sun_path));
1152 addr = (struct sockaddr *) &addr_un;
1153 alen = sizeof(addr_un);
1155 if (bind(drv->test_socket, addr, alen) < 0) {
1156 perror("bind(PF_UNIX)");
1157 close(drv->test_socket);
1158 if (drv->own_socket_path)
1159 unlink(drv->own_socket_path);
1160 test_driver_free_priv(drv);
1163 eloop_register_read_sock(drv->test_socket,
1164 test_driver_receive_unix, drv, NULL);
1166 drv->test_socket = -1;
1172 static void test_driver_deinit(void *priv)
1174 struct test_driver_data *drv = priv;
1175 struct test_client_socket *cli, *prev;
1184 if (drv->test_socket >= 0) {
1185 eloop_unregister_read_sock(drv->test_socket);
1186 close(drv->test_socket);
1187 if (drv->own_socket_path)
1188 unlink(drv->own_socket_path);
1191 /* There should be only one BSS remaining at this point. */
1192 if (drv->bss == NULL)
1193 wpa_printf(MSG_ERROR, "%s: drv->bss == NULL", __func__);
1194 else if (drv->bss->next)
1195 wpa_printf(MSG_ERROR, "%s: drv->bss->next != NULL", __func__);
1197 test_driver_free_priv(drv);
1201 const struct wpa_driver_ops wpa_driver_test_ops = {
1203 .init = test_driver_init,
1204 .deinit = test_driver_deinit,
1205 .send_eapol = test_driver_send_eapol,
1206 .send_mgmt_frame = test_driver_send_mgmt_frame,
1207 .set_generic_elem = test_driver_set_generic_elem,
1208 .sta_deauth = test_driver_sta_deauth,
1209 .sta_disassoc = test_driver_sta_disassoc,
1210 .get_hw_feature_data = test_driver_get_hw_feature_data,
1211 .bss_add = test_driver_bss_add,
1212 .bss_remove = test_driver_bss_remove,
1213 .if_add = test_driver_if_add,
1214 .if_update = test_driver_if_update,
1215 .if_remove = test_driver_if_remove,
1216 .valid_bss_mask = test_driver_valid_bss_mask,
1217 .set_ssid = test_driver_set_ssid,
1218 .set_privacy = test_driver_set_privacy,
1219 .set_encryption = test_driver_set_encryption,
1220 .set_sta_vlan = test_driver_set_sta_vlan,
1221 .sta_add = test_driver_sta_add,
1222 .send_ether = test_driver_send_ether,
1223 .set_wps_beacon_ie = test_driver_set_wps_beacon_ie,
1224 .set_wps_probe_resp_ie = test_driver_set_wps_probe_resp_ie,