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"
33 struct test_client_socket {
34 struct test_client_socket *next;
36 struct sockaddr_un un;
38 struct test_driver_bss *bss;
41 struct test_driver_bss {
42 struct test_driver_bss *next;
43 char ifname[IFNAMSIZ + 1];
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;
62 static void test_driver_free_bss(struct test_driver_bss *bss)
69 static void test_driver_free_priv(struct test_driver_data *drv)
71 struct test_driver_bss *bss, *prev;
80 test_driver_free_bss(prev);
82 free(drv->own_socket_path);
83 free(drv->socket_dir);
88 static struct test_client_socket *
89 test_driver_get_cli(struct test_driver_data *drv, struct sockaddr_un *from,
92 struct test_client_socket *cli = drv->cli;
95 if (cli->unlen == fromlen &&
96 strncmp(cli->un.sun_path, from->sun_path,
97 fromlen - sizeof(cli->un.sun_family)) == 0)
106 static int test_driver_send_eapol(void *priv, const u8 *addr, const u8 *data,
107 size_t data_len, int encrypt,
110 struct test_driver_data *drv = priv;
111 struct test_client_socket *cli;
114 struct l2_ethhdr eth;
116 if (drv->test_socket < 0)
121 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
127 wpa_printf(MSG_DEBUG, "%s: no destination client entry",
132 memcpy(eth.h_dest, addr, ETH_ALEN);
133 memcpy(eth.h_source, own_addr, ETH_ALEN);
134 eth.h_proto = htons(ETH_P_EAPOL);
136 io[0].iov_base = "EAPOL ";
138 io[1].iov_base = ð
139 io[1].iov_len = sizeof(eth);
140 io[2].iov_base = (u8 *) data;
141 io[2].iov_len = data_len;
143 memset(&msg, 0, sizeof(msg));
146 msg.msg_name = &cli->un;
147 msg.msg_namelen = cli->unlen;
148 return sendmsg(drv->test_socket, &msg, 0);
152 static int test_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
153 u16 proto, const u8 *data, size_t data_len)
155 struct test_driver_data *drv = priv;
158 struct l2_ethhdr eth;
160 struct sockaddr_un addr;
163 int ret = 0, broadcast = 0, count = 0;
165 if (drv->test_socket < 0 || drv->socket_dir == NULL) {
166 wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d "
168 __func__, drv->test_socket, drv->socket_dir);
172 broadcast = memcmp(dst, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
173 snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dst));
175 memcpy(eth.h_dest, dst, ETH_ALEN);
176 memcpy(eth.h_source, src, ETH_ALEN);
177 eth.h_proto = htons(proto);
179 io[0].iov_base = "ETHER ";
181 io[1].iov_base = ð
182 io[1].iov_len = sizeof(eth);
183 io[2].iov_base = (u8 *) data;
184 io[2].iov_len = data_len;
186 memset(&msg, 0, sizeof(msg));
190 dir = opendir(drv->socket_dir);
192 perror("test_driver: opendir");
195 while ((dent = readdir(dir))) {
196 #ifdef _DIRENT_HAVE_D_TYPE
197 /* Skip the file if it is not a socket. Also accept
198 * DT_UNKNOWN (0) in case the C library or underlying file
199 * system does not support d_type. */
200 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
202 #endif /* _DIRENT_HAVE_D_TYPE */
203 if (strcmp(dent->d_name, ".") == 0 ||
204 strcmp(dent->d_name, "..") == 0)
207 memset(&addr, 0, sizeof(addr));
208 addr.sun_family = AF_UNIX;
209 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
210 drv->socket_dir, dent->d_name);
212 if (strcmp(addr.sun_path, drv->own_socket_path) == 0)
214 if (!broadcast && strstr(dent->d_name, desttxt) == NULL)
217 wpa_printf(MSG_DEBUG, "%s: Send ether frame to %s",
218 __func__, dent->d_name);
220 msg.msg_name = &addr;
221 msg.msg_namelen = sizeof(addr);
222 ret = sendmsg(drv->test_socket, &msg, 0);
224 perror("driver_test: sendmsg");
229 if (!broadcast && count == 0) {
230 wpa_printf(MSG_DEBUG, "%s: Destination " MACSTR " not found",
231 __func__, MAC2STR(dst));
239 static int test_driver_send_mgmt_frame(void *priv, const void *buf,
240 size_t len, int flags)
242 struct test_driver_data *drv = priv;
246 int ret = 0, broadcast = 0;
248 struct sockaddr_un addr;
251 struct ieee80211_hdr *hdr;
254 if (drv->test_socket < 0 || len < 10 || drv->socket_dir == NULL) {
255 wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d len=%lu"
257 __func__, drv->test_socket, (unsigned long) len,
264 broadcast = memcmp(dest, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
265 snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dest));
267 io[0].iov_base = "MLME ";
269 io[1].iov_base = (void *) buf;
272 memset(&msg, 0, sizeof(msg));
276 dir = opendir(drv->socket_dir);
278 perror("test_driver: opendir");
281 while ((dent = readdir(dir))) {
282 #ifdef _DIRENT_HAVE_D_TYPE
283 /* Skip the file if it is not a socket. Also accept
284 * DT_UNKNOWN (0) in case the C library or underlying file
285 * system does not support d_type. */
286 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
288 #endif /* _DIRENT_HAVE_D_TYPE */
289 if (strcmp(dent->d_name, ".") == 0 ||
290 strcmp(dent->d_name, "..") == 0)
293 memset(&addr, 0, sizeof(addr));
294 addr.sun_family = AF_UNIX;
295 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
296 drv->socket_dir, dent->d_name);
298 if (strcmp(addr.sun_path, drv->own_socket_path) == 0)
300 if (!broadcast && strstr(dent->d_name, desttxt) == NULL)
303 wpa_printf(MSG_DEBUG, "%s: Send management frame to %s",
304 __func__, dent->d_name);
306 msg.msg_name = &addr;
307 msg.msg_namelen = sizeof(addr);
308 ret = sendmsg(drv->test_socket, &msg, 0);
310 perror("driver_test: sendmsg");
314 hdr = (struct ieee80211_hdr *) buf;
315 fc = le_to_host16(hdr->frame_control);
316 ieee802_11_mgmt_cb(drv->hapd, (u8 *) buf, len, WLAN_FC_GET_STYPE(fc),
323 static void test_driver_scan(struct test_driver_data *drv,
324 struct sockaddr_un *from, socklen_t fromlen)
326 char buf[512], *pos, *end;
328 struct test_driver_bss *bss;
330 wpa_printf(MSG_DEBUG, "test_driver: SCAN");
332 for (bss = drv->bss; bss; bss = bss->next) {
334 end = buf + sizeof(buf);
336 /* reply: SCANRESP BSSID SSID IEs */
337 ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
338 MAC2STR(bss->bssid));
339 if (ret < 0 || ret >= end - pos)
342 pos += wpa_snprintf_hex(pos, end - pos,
343 bss->ssid, bss->ssid_len);
344 ret = snprintf(pos, end - pos, " ");
345 if (ret < 0 || ret >= end - pos)
348 pos += wpa_snprintf_hex(pos, end - pos, bss->ie, bss->ielen);
351 ret = snprintf(pos, end - pos, " PRIVACY");
352 if (ret < 0 || ret >= end - pos)
357 sendto(drv->test_socket, buf, pos - buf, 0,
358 (struct sockaddr *) from, fromlen);
363 static struct hostapd_data * test_driver_get_hapd(struct test_driver_data *drv,
364 struct test_driver_bss *bss)
366 struct hostapd_iface *iface = drv->hapd->iface;
367 struct hostapd_data *hapd = NULL;
371 wpa_printf(MSG_DEBUG, "%s: bss == NULL", __func__);
375 for (i = 0; i < iface->num_bss; i++) {
376 hapd = iface->bss[i];
377 if (memcmp(hapd->own_addr, bss->bssid, ETH_ALEN) == 0)
380 if (i == iface->num_bss) {
381 wpa_printf(MSG_DEBUG, "%s: no matching interface entry found "
382 "for BSSID " MACSTR, __func__, MAC2STR(bss->bssid));
390 static int test_driver_new_sta(struct test_driver_data *drv,
391 struct test_driver_bss *bss, const u8 *addr,
392 const u8 *ie, size_t ielen)
394 struct hostapd_data *hapd;
395 struct sta_info *sta;
398 hapd = test_driver_get_hapd(drv, bss);
402 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
403 HOSTAPD_LEVEL_INFO, "associated");
405 sta = ap_get_sta(hapd, addr);
407 accounting_sta_stop(hapd, sta);
409 sta = ap_sta_add(hapd, addr);
414 if (hapd->conf->wpa) {
415 if (ie == NULL || ielen == 0) {
416 printf("test_driver: no IE from STA\n");
419 if (sta->wpa_sm == NULL)
420 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
422 if (sta->wpa_sm == NULL) {
423 printf("test_driver: Failed to initialize WPA state "
427 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
429 if (res != WPA_IE_OK) {
430 printf("WPA/RSN information element rejected? "
436 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
437 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
438 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
440 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
442 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
448 static void test_driver_assoc(struct test_driver_data *drv,
449 struct sockaddr_un *from, socklen_t fromlen,
452 struct test_client_socket *cli;
453 u8 ie[256], ssid[32];
454 size_t ielen, ssid_len = 0;
455 char *pos, *pos2, cmd[50];
456 struct test_driver_bss *bss;
458 /* data: STA-addr SSID(hex) IEs(hex) */
460 cli = os_zalloc(sizeof(*cli));
464 if (hwaddr_aton(data, cli->addr)) {
465 printf("test_socket: Invalid MAC address '%s' in ASSOC\n",
473 pos2 = strchr(pos, ' ');
476 ssid_len = (pos2 - pos) / 2;
477 if (hexstr2bin(pos, ssid, ssid_len) < 0) {
478 wpa_printf(MSG_DEBUG, "%s: Invalid SSID", __func__);
482 wpa_hexdump_ascii(MSG_DEBUG, "test_driver_assoc: SSID",
486 ielen = strlen(pos) / 2;
487 if (ielen > sizeof(ie))
489 if (hexstr2bin(pos, ie, ielen) < 0)
493 for (bss = drv->bss; bss; bss = bss->next) {
494 if (bss->ssid_len == ssid_len &&
495 memcmp(bss->ssid, ssid, ssid_len) == 0)
499 wpa_printf(MSG_DEBUG, "%s: No matching SSID found from "
500 "configured BSSes", __func__);
506 memcpy(&cli->un, from, sizeof(cli->un));
507 cli->unlen = fromlen;
508 cli->next = drv->cli;
510 wpa_hexdump_ascii(MSG_DEBUG, "test_socket: ASSOC sun_path",
511 (const u8 *) cli->un.sun_path,
512 cli->unlen - sizeof(cli->un.sun_family));
514 snprintf(cmd, sizeof(cmd), "ASSOCRESP " MACSTR " 0",
515 MAC2STR(bss->bssid));
516 sendto(drv->test_socket, cmd, strlen(cmd), 0,
517 (struct sockaddr *) from, fromlen);
519 if (test_driver_new_sta(drv, bss, cli->addr, ie, ielen) < 0) {
520 wpa_printf(MSG_DEBUG, "test_driver: failed to add new STA");
525 static void test_driver_disassoc(struct test_driver_data *drv,
526 struct sockaddr_un *from, socklen_t fromlen)
528 struct test_client_socket *cli;
529 struct sta_info *sta;
531 cli = test_driver_get_cli(drv, from, fromlen);
535 hostapd_logger(drv->hapd, cli->addr, HOSTAPD_MODULE_IEEE80211,
536 HOSTAPD_LEVEL_INFO, "disassociated");
538 sta = ap_get_sta(drv->hapd, cli->addr);
540 sta->flags &= ~WLAN_STA_ASSOC;
541 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
542 sta->acct_terminate_cause =
543 RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
544 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
545 ap_free_sta(drv->hapd, sta);
550 static void test_driver_eapol(struct test_driver_data *drv,
551 struct sockaddr_un *from, socklen_t fromlen,
552 u8 *data, size_t datalen)
554 struct test_client_socket *cli;
556 u8 *proto = data + 2 * ETH_ALEN;
557 /* Skip Ethernet header */
558 wpa_printf(MSG_DEBUG, "test_driver: dst=" MACSTR " src="
559 MACSTR " proto=%04x",
560 MAC2STR(data), MAC2STR(data + ETH_ALEN),
561 WPA_GET_BE16(proto));
565 cli = test_driver_get_cli(drv, from, fromlen);
567 struct hostapd_data *hapd;
568 hapd = test_driver_get_hapd(drv, cli->bss);
571 ieee802_1x_receive(hapd, cli->addr, data, datalen);
573 wpa_printf(MSG_DEBUG, "test_socket: EAPOL from unknown "
579 static void test_driver_ether(struct test_driver_data *drv,
580 struct sockaddr_un *from, socklen_t fromlen,
581 u8 *data, size_t datalen)
583 struct l2_ethhdr *eth;
585 if (datalen < sizeof(*eth))
588 eth = (struct l2_ethhdr *) data;
589 wpa_printf(MSG_DEBUG, "test_driver: RX ETHER dst=" MACSTR " src="
590 MACSTR " proto=%04x",
591 MAC2STR(eth->h_dest), MAC2STR(eth->h_source),
592 be_to_host16(eth->h_proto));
594 #ifdef CONFIG_IEEE80211R
595 if (be_to_host16(eth->h_proto) == ETH_P_RRB) {
596 wpa_ft_rrb_rx(drv->hapd->wpa_auth, eth->h_source,
597 data + sizeof(*eth), datalen - sizeof(*eth));
599 #endif /* CONFIG_IEEE80211R */
603 static void test_driver_mlme(struct test_driver_data *drv,
604 struct sockaddr_un *from, socklen_t fromlen,
605 u8 *data, size_t datalen)
607 struct ieee80211_hdr *hdr;
610 hdr = (struct ieee80211_hdr *) data;
612 if (test_driver_get_cli(drv, from, fromlen) == NULL && datalen >= 16) {
613 struct test_client_socket *cli;
614 cli = os_zalloc(sizeof(*cli));
617 wpa_printf(MSG_DEBUG, "Adding client entry for " MACSTR,
618 MAC2STR(hdr->addr2));
619 memcpy(cli->addr, hdr->addr2, ETH_ALEN);
620 memcpy(&cli->un, from, sizeof(cli->un));
621 cli->unlen = fromlen;
622 cli->next = drv->cli;
626 wpa_hexdump(MSG_MSGDUMP, "test_driver_mlme: received frame",
628 fc = le_to_host16(hdr->frame_control);
629 if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT) {
630 wpa_printf(MSG_ERROR, "%s: received non-mgmt frame",
634 ieee802_11_mgmt(drv->hapd, data, datalen, WLAN_FC_GET_STYPE(fc), NULL);
638 static void test_driver_receive_unix(int sock, void *eloop_ctx, void *sock_ctx)
640 struct test_driver_data *drv = eloop_ctx;
643 struct sockaddr_un from;
644 socklen_t fromlen = sizeof(from);
646 res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
647 (struct sockaddr *) &from, &fromlen);
649 perror("recvfrom(test_socket)");
654 wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
656 if (strcmp(buf, "SCAN") == 0) {
657 test_driver_scan(drv, &from, fromlen);
658 } else if (strncmp(buf, "ASSOC ", 6) == 0) {
659 test_driver_assoc(drv, &from, fromlen, buf + 6);
660 } else if (strcmp(buf, "DISASSOC") == 0) {
661 test_driver_disassoc(drv, &from, fromlen);
662 } else if (strncmp(buf, "EAPOL ", 6) == 0) {
663 test_driver_eapol(drv, &from, fromlen, (u8 *) buf + 6,
665 } else if (strncmp(buf, "ETHER ", 6) == 0) {
666 test_driver_ether(drv, &from, fromlen, (u8 *) buf + 6,
668 } else if (strncmp(buf, "MLME ", 5) == 0) {
669 test_driver_mlme(drv, &from, fromlen, (u8 *) buf + 5, res - 5);
671 wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
677 static struct test_driver_bss *
678 test_driver_get_bss(struct test_driver_data *drv, const char *ifname)
680 struct test_driver_bss *bss;
682 for (bss = drv->bss; bss; bss = bss->next) {
683 if (strcmp(bss->ifname, ifname) == 0)
690 static int test_driver_set_generic_elem(const char *ifname, void *priv,
691 const u8 *elem, size_t elem_len)
693 struct test_driver_data *drv = priv;
694 struct test_driver_bss *bss;
696 bss = test_driver_get_bss(drv, ifname);
708 bss->ie = malloc(elem_len);
709 if (bss->ie == NULL) {
714 memcpy(bss->ie, elem, elem_len);
715 bss->ielen = elem_len;
720 static int test_driver_sta_deauth(void *priv, const u8 *addr, int reason)
722 struct test_driver_data *drv = priv;
723 struct test_client_socket *cli;
725 if (drv->test_socket < 0)
730 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
738 return sendto(drv->test_socket, "DEAUTH", 6, 0,
739 (struct sockaddr *) &cli->un, cli->unlen);
743 static int test_driver_sta_disassoc(void *priv, const u8 *addr, int reason)
745 struct test_driver_data *drv = priv;
746 struct test_client_socket *cli;
748 if (drv->test_socket < 0)
753 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
761 return sendto(drv->test_socket, "DISASSOC", 8, 0,
762 (struct sockaddr *) &cli->un, cli->unlen);
766 static struct hostapd_hw_modes *
767 test_driver_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
769 struct hostapd_hw_modes *modes;
773 modes = os_zalloc(*num_modes * sizeof(struct hostapd_hw_modes));
776 modes[0].mode = HOSTAPD_MODE_IEEE80211G;
777 modes[0].num_channels = 1;
778 modes[0].num_rates = 1;
779 modes[0].channels = os_zalloc(sizeof(struct hostapd_channel_data));
780 modes[0].rates = os_zalloc(sizeof(struct hostapd_rate_data));
781 if (modes[0].channels == NULL || modes[0].rates == NULL) {
782 hostapd_free_hw_features(modes, *num_modes);
785 modes[0].channels[0].chan = 1;
786 modes[0].channels[0].freq = 2412;
787 modes[0].channels[0].flag = 0;
788 modes[0].rates[0].rate = 10;
789 modes[0].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
790 HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
792 modes[1].mode = HOSTAPD_MODE_IEEE80211B;
793 modes[1].num_channels = 1;
794 modes[1].num_rates = 1;
795 modes[1].channels = os_zalloc(sizeof(struct hostapd_channel_data));
796 modes[1].rates = os_zalloc(sizeof(struct hostapd_rate_data));
797 if (modes[1].channels == NULL || modes[1].rates == NULL) {
798 hostapd_free_hw_features(modes, *num_modes);
801 modes[1].channels[0].chan = 1;
802 modes[1].channels[0].freq = 2412;
803 modes[1].channels[0].flag = 0;
804 modes[1].rates[0].rate = 10;
805 modes[1].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
806 HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
808 modes[2].mode = HOSTAPD_MODE_IEEE80211A;
809 modes[2].num_channels = 1;
810 modes[2].num_rates = 1;
811 modes[2].channels = os_zalloc(sizeof(struct hostapd_channel_data));
812 modes[2].rates = os_zalloc(sizeof(struct hostapd_rate_data));
813 if (modes[2].channels == NULL || modes[2].rates == NULL) {
814 hostapd_free_hw_features(modes, *num_modes);
817 modes[2].channels[0].chan = 60;
818 modes[2].channels[0].freq = 5300;
819 modes[2].channels[0].flag = 0;
820 modes[2].rates[0].rate = 60;
821 modes[2].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
822 HOSTAPD_RATE_MANDATORY;
828 static int test_driver_bss_add(void *priv, const char *ifname, const u8 *bssid)
830 struct test_driver_data *drv = priv;
831 struct test_driver_bss *bss;
833 wpa_printf(MSG_DEBUG, "%s(ifname=%s bssid=" MACSTR ")",
834 __func__, ifname, MAC2STR(bssid));
836 bss = os_zalloc(sizeof(*bss));
840 os_strlcpy(bss->ifname, ifname, IFNAMSIZ);
841 memcpy(bss->bssid, bssid, ETH_ALEN);
843 bss->next = drv->bss;
850 static int test_driver_bss_remove(void *priv, const char *ifname)
852 struct test_driver_data *drv = priv;
853 struct test_driver_bss *bss, *prev;
854 struct test_client_socket *cli, *prev_c;
856 wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
858 for (prev = NULL, bss = drv->bss; bss; prev = bss, bss = bss->next) {
859 if (strcmp(bss->ifname, ifname) != 0)
863 prev->next = bss->next;
865 drv->bss = bss->next;
867 for (prev_c = NULL, cli = drv->cli; cli;
868 prev_c = cli, cli = cli->next) {
872 prev_c->next = cli->next;
874 drv->cli = cli->next;
879 test_driver_free_bss(bss);
887 static int test_driver_if_add(const char *iface, void *priv,
888 enum hostapd_driver_if_type type, char *ifname,
891 wpa_printf(MSG_DEBUG, "%s(iface=%s type=%d ifname=%s)",
892 __func__, iface, type, ifname);
897 static int test_driver_if_update(void *priv, enum hostapd_driver_if_type type,
898 char *ifname, const u8 *addr)
900 wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
905 static int test_driver_if_remove(void *priv, enum hostapd_driver_if_type type,
906 const char *ifname, const u8 *addr)
908 wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
913 static int test_driver_valid_bss_mask(void *priv, const u8 *addr,
920 static int test_driver_set_ssid(const char *ifname, void *priv, const u8 *buf,
923 struct test_driver_data *drv = priv;
924 struct test_driver_bss *bss;
926 wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
927 wpa_hexdump_ascii(MSG_DEBUG, "test_driver_set_ssid: SSID", buf, len);
929 for (bss = drv->bss; bss; bss = bss->next) {
930 if (strcmp(bss->ifname, ifname) != 0)
933 if (len < 0 || (size_t) len > sizeof(bss->ssid))
936 memcpy(bss->ssid, buf, len);
946 static int test_driver_set_privacy(const char *ifname, void *priv, int enabled)
948 struct test_driver_data *drv = priv;
949 struct test_driver_bss *bss;
951 wpa_printf(MSG_DEBUG, "%s(ifname=%s enabled=%d)",
952 __func__, ifname, enabled);
954 for (bss = drv->bss; bss; bss = bss->next) {
955 if (strcmp(bss->ifname, ifname) != 0)
958 bss->privacy = enabled;
967 static int test_driver_set_encryption(const char *iface, void *priv,
968 const char *alg, const u8 *addr, int idx,
969 const u8 *key, size_t key_len, int txkey)
971 wpa_printf(MSG_DEBUG, "%s(iface=%s alg=%s idx=%d txkey=%d)",
972 __func__, iface, alg, idx, txkey);
974 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
976 wpa_hexdump_key(MSG_DEBUG, " key", key, key_len);
981 static int test_driver_set_sta_vlan(void *priv, const u8 *addr,
982 const char *ifname, int vlan_id)
984 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " ifname=%s vlan_id=%d)",
985 __func__, MAC2STR(addr), ifname, vlan_id);
990 static int test_driver_sta_add(const char *ifname, void *priv, const u8 *addr,
991 u16 aid, u16 capability, u8 *supp_rates,
992 size_t supp_rates_len, int flags,
995 struct test_driver_data *drv = priv;
996 struct test_client_socket *cli;
997 struct test_driver_bss *bss;
999 wpa_printf(MSG_DEBUG, "%s(ifname=%s addr=" MACSTR " aid=%d "
1000 "capability=0x%x flags=0x%x listen_interval=%d)",
1001 __func__, ifname, MAC2STR(addr), aid, capability, flags,
1003 wpa_hexdump(MSG_DEBUG, "test_driver_sta_add - supp_rates",
1004 supp_rates, supp_rates_len);
1008 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
1013 wpa_printf(MSG_DEBUG, "%s: no matching client entry",
1018 for (bss = drv->bss; bss; bss = bss->next) {
1019 if (strcmp(ifname, bss->ifname) == 0)
1023 wpa_printf(MSG_DEBUG, "%s: No matching interface found from "
1024 "configured BSSes", __func__);
1034 static void * test_driver_init(struct hostapd_data *hapd)
1036 struct test_driver_data *drv;
1037 struct sockaddr_un addr;
1039 drv = os_zalloc(sizeof(struct test_driver_data));
1041 printf("Could not allocate memory for test driver data\n");
1044 drv->bss = os_zalloc(sizeof(*drv->bss));
1045 if (drv->bss == NULL) {
1046 printf("Could not allocate memory for test driver BSS data\n");
1053 /* Generate a MAC address to help testing with multiple APs */
1054 hapd->own_addr[0] = 0x02; /* locally administered */
1055 sha1_prf((const u8 *) hapd->conf->iface, strlen(hapd->conf->iface),
1056 "hostapd test bssid generation",
1057 (const u8 *) hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len,
1058 hapd->own_addr + 1, ETH_ALEN - 1);
1060 os_strlcpy(drv->bss->ifname, hapd->conf->iface, IFNAMSIZ);
1061 memcpy(drv->bss->bssid, hapd->own_addr, ETH_ALEN);
1063 if (hapd->conf->test_socket) {
1064 if (strlen(hapd->conf->test_socket) >= sizeof(addr.sun_path)) {
1065 printf("Too long test_socket path\n");
1066 test_driver_free_priv(drv);
1069 if (strncmp(hapd->conf->test_socket, "DIR:", 4) == 0) {
1070 size_t len = strlen(hapd->conf->test_socket) + 30;
1071 drv->socket_dir = strdup(hapd->conf->test_socket + 4);
1072 drv->own_socket_path = malloc(len);
1073 if (drv->own_socket_path) {
1074 snprintf(drv->own_socket_path, len,
1076 hapd->conf->test_socket + 4,
1077 MAC2STR(hapd->own_addr));
1080 drv->own_socket_path = strdup(hapd->conf->test_socket);
1082 if (drv->own_socket_path == NULL) {
1083 test_driver_free_priv(drv);
1087 drv->test_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
1088 if (drv->test_socket < 0) {
1089 perror("socket(PF_UNIX)");
1090 test_driver_free_priv(drv);
1094 memset(&addr, 0, sizeof(addr));
1095 addr.sun_family = AF_UNIX;
1096 os_strlcpy(addr.sun_path, drv->own_socket_path,
1097 sizeof(addr.sun_path));
1098 if (bind(drv->test_socket, (struct sockaddr *) &addr,
1099 sizeof(addr)) < 0) {
1100 perror("bind(PF_UNIX)");
1101 close(drv->test_socket);
1102 unlink(drv->own_socket_path);
1103 test_driver_free_priv(drv);
1106 eloop_register_read_sock(drv->test_socket,
1107 test_driver_receive_unix, drv, NULL);
1109 drv->test_socket = -1;
1115 static void test_driver_deinit(void *priv)
1117 struct test_driver_data *drv = priv;
1118 struct test_client_socket *cli, *prev;
1127 if (drv->test_socket >= 0) {
1128 eloop_unregister_read_sock(drv->test_socket);
1129 close(drv->test_socket);
1130 unlink(drv->own_socket_path);
1133 /* There should be only one BSS remaining at this point. */
1134 if (drv->bss == NULL)
1135 wpa_printf(MSG_ERROR, "%s: drv->bss == NULL", __func__);
1136 else if (drv->bss->next)
1137 wpa_printf(MSG_ERROR, "%s: drv->bss->next != NULL", __func__);
1139 test_driver_free_priv(drv);
1143 const struct wpa_driver_ops wpa_driver_test_ops = {
1145 .init = test_driver_init,
1146 .deinit = test_driver_deinit,
1147 .send_eapol = test_driver_send_eapol,
1148 .send_mgmt_frame = test_driver_send_mgmt_frame,
1149 .set_generic_elem = test_driver_set_generic_elem,
1150 .sta_deauth = test_driver_sta_deauth,
1151 .sta_disassoc = test_driver_sta_disassoc,
1152 .get_hw_feature_data = test_driver_get_hw_feature_data,
1153 .bss_add = test_driver_bss_add,
1154 .bss_remove = test_driver_bss_remove,
1155 .if_add = test_driver_if_add,
1156 .if_update = test_driver_if_update,
1157 .if_remove = test_driver_if_remove,
1158 .valid_bss_mask = test_driver_valid_bss_mask,
1159 .set_ssid = test_driver_set_ssid,
1160 .set_privacy = test_driver_set_privacy,
1161 .set_encryption = test_driver_set_encryption,
1162 .set_sta_vlan = test_driver_set_sta_vlan,
1163 .sta_add = test_driver_sta_add,
1164 .send_ether = test_driver_send_ether,