Home | History | Annotate | Download | only in drivers
      1 /*
      2  * Testing driver interface for a simulated network driver
      3  * Copyright (c) 2004-2010, Jouni Malinen <j (at) w1.fi>
      4  *
      5  * This program is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU General Public License version 2 as
      7  * published by the Free Software Foundation.
      8  *
      9  * Alternatively, this software may be distributed under the terms of BSD
     10  * license.
     11  *
     12  * See README and COPYING for more details.
     13  */
     14 
     15 /* Make sure we get winsock2.h for Windows build to get sockaddr_storage */
     16 #include "build_config.h"
     17 #ifdef CONFIG_NATIVE_WINDOWS
     18 #include <winsock2.h>
     19 #endif /* CONFIG_NATIVE_WINDOWS */
     20 
     21 #include "utils/includes.h"
     22 
     23 #ifndef CONFIG_NATIVE_WINDOWS
     24 #include <sys/un.h>
     25 #include <dirent.h>
     26 #include <sys/stat.h>
     27 #define DRIVER_TEST_UNIX
     28 #endif /* CONFIG_NATIVE_WINDOWS */
     29 
     30 #include "utils/common.h"
     31 #include "utils/eloop.h"
     32 #include "utils/list.h"
     33 #include "utils/trace.h"
     34 #include "common/ieee802_11_defs.h"
     35 #include "crypto/sha1.h"
     36 #include "l2_packet/l2_packet.h"
     37 #include "p2p/p2p.h"
     38 #include "wps/wps.h"
     39 #include "driver.h"
     40 
     41 
     42 struct test_client_socket {
     43 	struct test_client_socket *next;
     44 	u8 addr[ETH_ALEN];
     45 	struct sockaddr_un un;
     46 	socklen_t unlen;
     47 	struct test_driver_bss *bss;
     48 };
     49 
     50 struct test_driver_bss {
     51 	struct wpa_driver_test_data *drv;
     52 	struct dl_list list;
     53 	void *bss_ctx;
     54 	char ifname[IFNAMSIZ];
     55 	u8 bssid[ETH_ALEN];
     56 	u8 *ie;
     57 	size_t ielen;
     58 	u8 *wps_beacon_ie;
     59 	size_t wps_beacon_ie_len;
     60 	u8 *wps_probe_resp_ie;
     61 	size_t wps_probe_resp_ie_len;
     62 	u8 ssid[32];
     63 	size_t ssid_len;
     64 	int privacy;
     65 };
     66 
     67 struct wpa_driver_test_global {
     68 	int bss_add_used;
     69 	u8 req_addr[ETH_ALEN];
     70 };
     71 
     72 struct wpa_driver_test_data {
     73 	struct wpa_driver_test_global *global;
     74 	void *ctx;
     75 	WPA_TRACE_REF(ctx);
     76 	u8 own_addr[ETH_ALEN];
     77 	int test_socket;
     78 #ifdef DRIVER_TEST_UNIX
     79 	struct sockaddr_un hostapd_addr;
     80 #endif /* DRIVER_TEST_UNIX */
     81 	int hostapd_addr_set;
     82 	struct sockaddr_in hostapd_addr_udp;
     83 	int hostapd_addr_udp_set;
     84 	char *own_socket_path;
     85 	char *test_dir;
     86 #define MAX_SCAN_RESULTS 30
     87 	struct wpa_scan_res *scanres[MAX_SCAN_RESULTS];
     88 	size_t num_scanres;
     89 	int use_associnfo;
     90 	u8 assoc_wpa_ie[80];
     91 	size_t assoc_wpa_ie_len;
     92 	int use_mlme;
     93 	int associated;
     94 	u8 *probe_req_ie;
     95 	size_t probe_req_ie_len;
     96 	u8 probe_req_ssid[32];
     97 	size_t probe_req_ssid_len;
     98 	int ibss;
     99 	int ap;
    100 
    101 	struct test_client_socket *cli;
    102 	struct dl_list bss;
    103 	int udp_port;
    104 
    105 	int alloc_iface_idx;
    106 
    107 	int probe_req_report;
    108 	unsigned int remain_on_channel_freq;
    109 	unsigned int remain_on_channel_duration;
    110 
    111 	int current_freq;
    112 
    113 	struct p2p_data *p2p;
    114 	unsigned int off_channel_freq;
    115 	struct wpabuf *pending_action_tx;
    116 	u8 pending_action_src[ETH_ALEN];
    117 	u8 pending_action_dst[ETH_ALEN];
    118 	u8 pending_action_bssid[ETH_ALEN];
    119 	unsigned int pending_action_freq;
    120 	unsigned int pending_listen_freq;
    121 	unsigned int pending_listen_duration;
    122 	int pending_p2p_scan;
    123 	struct sockaddr *probe_from;
    124 	socklen_t probe_from_len;
    125 };
    126 
    127 
    128 static void wpa_driver_test_deinit(void *priv);
    129 static int wpa_driver_test_attach(struct wpa_driver_test_data *drv,
    130 				  const char *dir, int ap);
    131 static void wpa_driver_test_close_test_socket(
    132 	struct wpa_driver_test_data *drv);
    133 static void test_remain_on_channel_timeout(void *eloop_ctx, void *timeout_ctx);
    134 static int wpa_driver_test_init_p2p(struct wpa_driver_test_data *drv);
    135 
    136 
    137 static void test_driver_free_bss(struct test_driver_bss *bss)
    138 {
    139 	os_free(bss->ie);
    140 	os_free(bss->wps_beacon_ie);
    141 	os_free(bss->wps_probe_resp_ie);
    142 	os_free(bss);
    143 }
    144 
    145 
    146 static void test_driver_free_bsses(struct wpa_driver_test_data *drv)
    147 {
    148 	struct test_driver_bss *bss, *tmp;
    149 
    150 	dl_list_for_each_safe(bss, tmp, &drv->bss, struct test_driver_bss,
    151 			      list) {
    152 		dl_list_del(&bss->list);
    153 		test_driver_free_bss(bss);
    154 	}
    155 }
    156 
    157 
    158 static struct test_client_socket *
    159 test_driver_get_cli(struct wpa_driver_test_data *drv, struct sockaddr_un *from,
    160 		    socklen_t fromlen)
    161 {
    162 	struct test_client_socket *cli = drv->cli;
    163 
    164 	while (cli) {
    165 		if (cli->unlen == fromlen &&
    166 		    strncmp(cli->un.sun_path, from->sun_path,
    167 			    fromlen - sizeof(cli->un.sun_family)) == 0)
    168 			return cli;
    169 		cli = cli->next;
    170 	}
    171 
    172 	return NULL;
    173 }
    174 
    175 
    176 static int test_driver_send_eapol(void *priv, const u8 *addr, const u8 *data,
    177 				  size_t data_len, int encrypt,
    178 				  const u8 *own_addr, u32 flags)
    179 {
    180 	struct test_driver_bss *dbss = priv;
    181 	struct wpa_driver_test_data *drv = dbss->drv;
    182 	struct test_client_socket *cli;
    183 	struct msghdr msg;
    184 	struct iovec io[3];
    185 	struct l2_ethhdr eth;
    186 
    187 	if (drv->test_socket < 0)
    188 		return -1;
    189 
    190 	cli = drv->cli;
    191 	while (cli) {
    192 		if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
    193 			break;
    194 		cli = cli->next;
    195 	}
    196 
    197 	if (!cli) {
    198 		wpa_printf(MSG_DEBUG, "%s: no destination client entry",
    199 			   __func__);
    200 		return -1;
    201 	}
    202 
    203 	memcpy(eth.h_dest, addr, ETH_ALEN);
    204 	memcpy(eth.h_source, own_addr, ETH_ALEN);
    205 	eth.h_proto = host_to_be16(ETH_P_EAPOL);
    206 
    207 	io[0].iov_base = "EAPOL ";
    208 	io[0].iov_len = 6;
    209 	io[1].iov_base = &eth;
    210 	io[1].iov_len = sizeof(eth);
    211 	io[2].iov_base = (u8 *) data;
    212 	io[2].iov_len = data_len;
    213 
    214 	memset(&msg, 0, sizeof(msg));
    215 	msg.msg_iov = io;
    216 	msg.msg_iovlen = 3;
    217 	msg.msg_name = &cli->un;
    218 	msg.msg_namelen = cli->unlen;
    219 	return sendmsg(drv->test_socket, &msg, 0);
    220 }
    221 
    222 
    223 static int test_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
    224 				  u16 proto, const u8 *data, size_t data_len)
    225 {
    226 	struct test_driver_bss *dbss = priv;
    227 	struct wpa_driver_test_data *drv = dbss->drv;
    228 	struct msghdr msg;
    229 	struct iovec io[3];
    230 	struct l2_ethhdr eth;
    231 	char desttxt[30];
    232 	struct sockaddr_un addr;
    233 	struct dirent *dent;
    234 	DIR *dir;
    235 	int ret = 0, broadcast = 0, count = 0;
    236 
    237 	if (drv->test_socket < 0 || drv->test_dir == NULL) {
    238 		wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d "
    239 			   "test_dir=%p)",
    240 			   __func__, drv->test_socket, drv->test_dir);
    241 		return -1;
    242 	}
    243 
    244 	broadcast = memcmp(dst, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
    245 	snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dst));
    246 
    247 	memcpy(eth.h_dest, dst, ETH_ALEN);
    248 	memcpy(eth.h_source, src, ETH_ALEN);
    249 	eth.h_proto = host_to_be16(proto);
    250 
    251 	io[0].iov_base = "ETHER ";
    252 	io[0].iov_len = 6;
    253 	io[1].iov_base = &eth;
    254 	io[1].iov_len = sizeof(eth);
    255 	io[2].iov_base = (u8 *) data;
    256 	io[2].iov_len = data_len;
    257 
    258 	memset(&msg, 0, sizeof(msg));
    259 	msg.msg_iov = io;
    260 	msg.msg_iovlen = 3;
    261 
    262 	dir = opendir(drv->test_dir);
    263 	if (dir == NULL) {
    264 		perror("test_driver: opendir");
    265 		return -1;
    266 	}
    267 	while ((dent = readdir(dir))) {
    268 #ifdef _DIRENT_HAVE_D_TYPE
    269 		/* Skip the file if it is not a socket. Also accept
    270 		 * DT_UNKNOWN (0) in case the C library or underlying file
    271 		 * system does not support d_type. */
    272 		if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
    273 			continue;
    274 #endif /* _DIRENT_HAVE_D_TYPE */
    275 		if (strcmp(dent->d_name, ".") == 0 ||
    276 		    strcmp(dent->d_name, "..") == 0)
    277 			continue;
    278 
    279 		memset(&addr, 0, sizeof(addr));
    280 		addr.sun_family = AF_UNIX;
    281 		snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
    282 			 drv->test_dir, dent->d_name);
    283 
    284 		if (strcmp(addr.sun_path, drv->own_socket_path) == 0)
    285 			continue;
    286 		if (!broadcast && strstr(dent->d_name, desttxt) == NULL)
    287 			continue;
    288 
    289 		wpa_printf(MSG_DEBUG, "%s: Send ether frame to %s",
    290 			   __func__, dent->d_name);
    291 
    292 		msg.msg_name = &addr;
    293 		msg.msg_namelen = sizeof(addr);
    294 		ret = sendmsg(drv->test_socket, &msg, 0);
    295 		if (ret < 0)
    296 			perror("driver_test: sendmsg");
    297 		count++;
    298 	}
    299 	closedir(dir);
    300 
    301 	if (!broadcast && count == 0) {
    302 		wpa_printf(MSG_DEBUG, "%s: Destination " MACSTR " not found",
    303 			   __func__, MAC2STR(dst));
    304 		return -1;
    305 	}
    306 
    307 	return ret;
    308 }
    309 
    310 
    311 static int wpa_driver_test_send_mlme(void *priv, const u8 *data,
    312 				     size_t data_len)
    313 {
    314 	struct test_driver_bss *dbss = priv;
    315 	struct wpa_driver_test_data *drv = dbss->drv;
    316 	struct msghdr msg;
    317 	struct iovec io[2];
    318 	const u8 *dest;
    319 	struct sockaddr_un addr;
    320 	struct dirent *dent;
    321 	DIR *dir;
    322 	int broadcast;
    323 	int ret = 0;
    324 	struct ieee80211_hdr *hdr;
    325 	u16 fc;
    326 	char cmd[50];
    327 	int freq;
    328 #ifdef HOSTAPD
    329 	char desttxt[30];
    330 #endif /* HOSTAPD */
    331 	union wpa_event_data event;
    332 
    333 	wpa_hexdump(MSG_MSGDUMP, "test_send_mlme", data, data_len);
    334 	if (drv->test_socket < 0 || data_len < 10) {
    335 		wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d len=%lu"
    336 			   " test_dir=%p)",
    337 			   __func__, drv->test_socket,
    338 			   (unsigned long) data_len,
    339 			   drv->test_dir);
    340 		return -1;
    341 	}
    342 
    343 	dest = data + 4;
    344 	broadcast = os_memcmp(dest, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
    345 
    346 #ifdef HOSTAPD
    347 	snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dest));
    348 #endif /* HOSTAPD */
    349 
    350 	if (drv->remain_on_channel_freq)
    351 		freq = drv->remain_on_channel_freq;
    352 	else
    353 		freq = drv->current_freq;
    354 	wpa_printf(MSG_DEBUG, "test_driver(%s): MLME TX on freq %d MHz",
    355 		   dbss->ifname, freq);
    356 	os_snprintf(cmd, sizeof(cmd), "MLME freq=%d ", freq);
    357 	io[0].iov_base = cmd;
    358 	io[0].iov_len = os_strlen(cmd);
    359 	io[1].iov_base = (void *) data;
    360 	io[1].iov_len = data_len;
    361 
    362 	os_memset(&msg, 0, sizeof(msg));
    363 	msg.msg_iov = io;
    364 	msg.msg_iovlen = 2;
    365 
    366 #ifdef HOSTAPD
    367 	if (drv->test_dir == NULL) {
    368 		wpa_printf(MSG_DEBUG, "%s: test_dir == NULL", __func__);
    369 		return -1;
    370 	}
    371 
    372 	dir = opendir(drv->test_dir);
    373 	if (dir == NULL) {
    374 		perror("test_driver: opendir");
    375 		return -1;
    376 	}
    377 	while ((dent = readdir(dir))) {
    378 #ifdef _DIRENT_HAVE_D_TYPE
    379 		/* Skip the file if it is not a socket. Also accept
    380 		 * DT_UNKNOWN (0) in case the C library or underlying file
    381 		 * system does not support d_type. */
    382 		if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
    383 			continue;
    384 #endif /* _DIRENT_HAVE_D_TYPE */
    385 		if (os_strcmp(dent->d_name, ".") == 0 ||
    386 		    os_strcmp(dent->d_name, "..") == 0)
    387 			continue;
    388 
    389 		os_memset(&addr, 0, sizeof(addr));
    390 		addr.sun_family = AF_UNIX;
    391 		os_snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
    392 			    drv->test_dir, dent->d_name);
    393 
    394 		if (os_strcmp(addr.sun_path, drv->own_socket_path) == 0)
    395 			continue;
    396 		if (!broadcast && os_strstr(dent->d_name, desttxt) == NULL)
    397 			continue;
    398 
    399 		wpa_printf(MSG_DEBUG, "%s: Send management frame to %s",
    400 			   __func__, dent->d_name);
    401 
    402 		msg.msg_name = &addr;
    403 		msg.msg_namelen = sizeof(addr);
    404 		ret = sendmsg(drv->test_socket, &msg, 0);
    405 		if (ret < 0)
    406 			perror("driver_test: sendmsg(test_socket)");
    407 	}
    408 	closedir(dir);
    409 #else /* HOSTAPD */
    410 
    411 	if (os_memcmp(dest, dbss->bssid, ETH_ALEN) == 0 ||
    412 	    drv->test_dir == NULL) {
    413 		if (drv->hostapd_addr_udp_set) {
    414 			msg.msg_name = &drv->hostapd_addr_udp;
    415 			msg.msg_namelen = sizeof(drv->hostapd_addr_udp);
    416 		} else {
    417 #ifdef DRIVER_TEST_UNIX
    418 			msg.msg_name = &drv->hostapd_addr;
    419 			msg.msg_namelen = sizeof(drv->hostapd_addr);
    420 #endif /* DRIVER_TEST_UNIX */
    421 		}
    422 	} else if (broadcast) {
    423 		dir = opendir(drv->test_dir);
    424 		if (dir == NULL)
    425 			return -1;
    426 		while ((dent = readdir(dir))) {
    427 #ifdef _DIRENT_HAVE_D_TYPE
    428 			/* Skip the file if it is not a socket.
    429 			 * Also accept DT_UNKNOWN (0) in case
    430 			 * the C library or underlying file
    431 			 * system does not support d_type. */
    432 			if (dent->d_type != DT_SOCK &&
    433 			    dent->d_type != DT_UNKNOWN)
    434 				continue;
    435 #endif /* _DIRENT_HAVE_D_TYPE */
    436 			if (os_strcmp(dent->d_name, ".") == 0 ||
    437 			    os_strcmp(dent->d_name, "..") == 0)
    438 				continue;
    439 			wpa_printf(MSG_DEBUG, "%s: Send broadcast MLME to %s",
    440 				   __func__, dent->d_name);
    441 			os_memset(&addr, 0, sizeof(addr));
    442 			addr.sun_family = AF_UNIX;
    443 			os_snprintf(addr.sun_path, sizeof(addr.sun_path),
    444 				    "%s/%s", drv->test_dir, dent->d_name);
    445 
    446 			msg.msg_name = &addr;
    447 			msg.msg_namelen = sizeof(addr);
    448 
    449 			ret = sendmsg(drv->test_socket, &msg, 0);
    450 			if (ret < 0)
    451 				perror("driver_test: sendmsg(test_socket)");
    452 		}
    453 		closedir(dir);
    454 		return ret;
    455 	} else {
    456 		struct stat st;
    457 		os_memset(&addr, 0, sizeof(addr));
    458 		addr.sun_family = AF_UNIX;
    459 		os_snprintf(addr.sun_path, sizeof(addr.sun_path),
    460 			    "%s/AP-" MACSTR, drv->test_dir, MAC2STR(dest));
    461 		if (stat(addr.sun_path, &st) < 0) {
    462 			os_snprintf(addr.sun_path, sizeof(addr.sun_path),
    463 				    "%s/STA-" MACSTR,
    464 				    drv->test_dir, MAC2STR(dest));
    465 		}
    466 		msg.msg_name = &addr;
    467 		msg.msg_namelen = sizeof(addr);
    468 	}
    469 
    470 	if (sendmsg(drv->test_socket, &msg, 0) < 0) {
    471 		perror("sendmsg(test_socket)");
    472 		return -1;
    473 	}
    474 #endif /* HOSTAPD */
    475 
    476 	hdr = (struct ieee80211_hdr *) data;
    477 	fc = le_to_host16(hdr->frame_control);
    478 
    479 	os_memset(&event, 0, sizeof(event));
    480 	event.tx_status.type = WLAN_FC_GET_TYPE(fc);
    481 	event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
    482 	event.tx_status.dst = hdr->addr1;
    483 	event.tx_status.data = data;
    484 	event.tx_status.data_len = data_len;
    485 	event.tx_status.ack = ret >= 0;
    486 	wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
    487 
    488 #ifdef CONFIG_P2P
    489 	if (drv->p2p &&
    490 	    WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
    491 	    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
    492 		if (drv->pending_action_tx == NULL) {
    493 			wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - "
    494 				   "no pending operation");
    495 			return ret;
    496 		}
    497 
    498 		if (os_memcmp(hdr->addr1, drv->pending_action_dst, ETH_ALEN) !=
    499 		    0) {
    500 			wpa_printf(MSG_DEBUG, "P2P: Ignore Action TX status - "
    501 				   "unknown destination address");
    502 			return ret;
    503 		}
    504 
    505 		wpabuf_free(drv->pending_action_tx);
    506 		drv->pending_action_tx = NULL;
    507 
    508 		p2p_send_action_cb(drv->p2p, drv->pending_action_freq,
    509 				   drv->pending_action_dst,
    510 				   drv->pending_action_src,
    511 				   drv->pending_action_bssid,
    512 				   ret >= 0);
    513 	}
    514 #endif /* CONFIG_P2P */
    515 
    516 	return ret;
    517 }
    518 
    519 
    520 static void test_driver_scan(struct wpa_driver_test_data *drv,
    521 			     struct sockaddr_un *from, socklen_t fromlen,
    522 			     char *data)
    523 {
    524 	char buf[512], *pos, *end;
    525 	int ret;
    526 	struct test_driver_bss *bss;
    527 	u8 sa[ETH_ALEN];
    528 	u8 ie[512];
    529 	size_t ielen;
    530 	union wpa_event_data event;
    531 
    532 	/* data: optional [ ' ' | STA-addr | ' ' | IEs(hex) ] */
    533 
    534 	wpa_printf(MSG_DEBUG, "test_driver: SCAN");
    535 
    536 	if (*data) {
    537 		if (*data != ' ' ||
    538 		    hwaddr_aton(data + 1, sa)) {
    539 			wpa_printf(MSG_DEBUG, "test_driver: Unexpected SCAN "
    540 				   "command format");
    541 			return;
    542 		}
    543 
    544 		data += 18;
    545 		while (*data == ' ')
    546 			data++;
    547 		ielen = os_strlen(data) / 2;
    548 		if (ielen > sizeof(ie))
    549 			ielen = sizeof(ie);
    550 		if (hexstr2bin(data, ie, ielen) < 0)
    551 			ielen = 0;
    552 
    553 		wpa_printf(MSG_DEBUG, "test_driver: Scan from " MACSTR,
    554 			   MAC2STR(sa));
    555 		wpa_hexdump(MSG_MSGDUMP, "test_driver: scan IEs", ie, ielen);
    556 
    557 		os_memset(&event, 0, sizeof(event));
    558 		event.rx_probe_req.sa = sa;
    559 		event.rx_probe_req.ie = ie;
    560 		event.rx_probe_req.ie_len = ielen;
    561 		wpa_supplicant_event(drv->ctx, EVENT_RX_PROBE_REQ, &event);
    562 #ifdef CONFIG_P2P
    563 		if (drv->p2p)
    564 			p2p_probe_req_rx(drv->p2p, sa, ie, ielen);
    565 #endif /* CONFIG_P2P */
    566 	}
    567 
    568 	dl_list_for_each(bss, &drv->bss, struct test_driver_bss, list) {
    569 		pos = buf;
    570 		end = buf + sizeof(buf);
    571 
    572 		/* reply: SCANRESP BSSID SSID IEs */
    573 		ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
    574 			       MAC2STR(bss->bssid));
    575 		if (ret < 0 || ret >= end - pos)
    576 			return;
    577 		pos += ret;
    578 		pos += wpa_snprintf_hex(pos, end - pos,
    579 					bss->ssid, bss->ssid_len);
    580 		ret = snprintf(pos, end - pos, " ");
    581 		if (ret < 0 || ret >= end - pos)
    582 			return;
    583 		pos += ret;
    584 		pos += wpa_snprintf_hex(pos, end - pos, bss->ie, bss->ielen);
    585 		pos += wpa_snprintf_hex(pos, end - pos, bss->wps_probe_resp_ie,
    586 					bss->wps_probe_resp_ie_len);
    587 
    588 		if (bss->privacy) {
    589 			ret = snprintf(pos, end - pos, " PRIVACY");
    590 			if (ret < 0 || ret >= end - pos)
    591 				return;
    592 			pos += ret;
    593 		}
    594 
    595 		sendto(drv->test_socket, buf, pos - buf, 0,
    596 		       (struct sockaddr *) from, fromlen);
    597 	}
    598 }
    599 
    600 
    601 static void test_driver_assoc(struct wpa_driver_test_data *drv,
    602 			      struct sockaddr_un *from, socklen_t fromlen,
    603 			      char *data)
    604 {
    605 	struct test_client_socket *cli;
    606 	u8 ie[256], ssid[32];
    607 	size_t ielen, ssid_len = 0;
    608 	char *pos, *pos2, cmd[50];
    609 	struct test_driver_bss *bss, *tmp;
    610 
    611 	/* data: STA-addr SSID(hex) IEs(hex) */
    612 
    613 	cli = os_zalloc(sizeof(*cli));
    614 	if (cli == NULL)
    615 		return;
    616 
    617 	if (hwaddr_aton(data, cli->addr)) {
    618 		printf("test_socket: Invalid MAC address '%s' in ASSOC\n",
    619 		       data);
    620 		os_free(cli);
    621 		return;
    622 	}
    623 	pos = data + 17;
    624 	while (*pos == ' ')
    625 		pos++;
    626 	pos2 = strchr(pos, ' ');
    627 	ielen = 0;
    628 	if (pos2) {
    629 		ssid_len = (pos2 - pos) / 2;
    630 		if (hexstr2bin(pos, ssid, ssid_len) < 0) {
    631 			wpa_printf(MSG_DEBUG, "%s: Invalid SSID", __func__);
    632 			os_free(cli);
    633 			return;
    634 		}
    635 		wpa_hexdump_ascii(MSG_DEBUG, "test_driver_assoc: SSID",
    636 				  ssid, ssid_len);
    637 
    638 		pos = pos2 + 1;
    639 		ielen = strlen(pos) / 2;
    640 		if (ielen > sizeof(ie))
    641 			ielen = sizeof(ie);
    642 		if (hexstr2bin(pos, ie, ielen) < 0)
    643 			ielen = 0;
    644 	}
    645 
    646 	bss = NULL;
    647 	dl_list_for_each(tmp, &drv->bss, struct test_driver_bss, list) {
    648 		if (tmp->ssid_len == ssid_len &&
    649 		    os_memcmp(tmp->ssid, ssid, ssid_len) == 0) {
    650 			bss = tmp;
    651 			break;
    652 		}
    653 	}
    654 	if (bss == NULL) {
    655 		wpa_printf(MSG_DEBUG, "%s: No matching SSID found from "
    656 			   "configured BSSes", __func__);
    657 		os_free(cli);
    658 		return;
    659 	}
    660 
    661 	cli->bss = bss;
    662 	memcpy(&cli->un, from, sizeof(cli->un));
    663 	cli->unlen = fromlen;
    664 	cli->next = drv->cli;
    665 	drv->cli = cli;
    666 	wpa_hexdump_ascii(MSG_DEBUG, "test_socket: ASSOC sun_path",
    667 			  (const u8 *) cli->un.sun_path,
    668 			  cli->unlen - sizeof(cli->un.sun_family));
    669 
    670 	snprintf(cmd, sizeof(cmd), "ASSOCRESP " MACSTR " 0",
    671 		 MAC2STR(bss->bssid));
    672 	sendto(drv->test_socket, cmd, strlen(cmd), 0,
    673 	       (struct sockaddr *) from, fromlen);
    674 
    675 	drv_event_assoc(bss->bss_ctx, cli->addr, ie, ielen, 0);
    676 }
    677 
    678 
    679 static void test_driver_disassoc(struct wpa_driver_test_data *drv,
    680 				 struct sockaddr_un *from, socklen_t fromlen)
    681 {
    682 	struct test_client_socket *cli;
    683 
    684 	cli = test_driver_get_cli(drv, from, fromlen);
    685 	if (!cli)
    686 		return;
    687 
    688 	drv_event_disassoc(drv->ctx, cli->addr);
    689 }
    690 
    691 
    692 static void test_driver_eapol(struct wpa_driver_test_data *drv,
    693 			      struct sockaddr_un *from, socklen_t fromlen,
    694 			      u8 *data, size_t datalen)
    695 {
    696 #ifdef HOSTAPD
    697 	struct test_client_socket *cli;
    698 #endif /* HOSTAPD */
    699 	const u8 *src = NULL;
    700 
    701 	if (datalen > 14) {
    702 		/* Skip Ethernet header */
    703 		src = data + ETH_ALEN;
    704 		wpa_printf(MSG_DEBUG, "test_driver: dst=" MACSTR " src="
    705 			   MACSTR " proto=%04x",
    706 			   MAC2STR(data), MAC2STR(src),
    707 			   WPA_GET_BE16(data + 2 * ETH_ALEN));
    708 		data += 14;
    709 		datalen -= 14;
    710 	}
    711 
    712 #ifdef HOSTAPD
    713 	cli = test_driver_get_cli(drv, from, fromlen);
    714 	if (cli) {
    715 		drv_event_eapol_rx(cli->bss->bss_ctx, cli->addr, data,
    716 				   datalen);
    717 	} else {
    718 		wpa_printf(MSG_DEBUG, "test_socket: EAPOL from unknown "
    719 			   "client");
    720 	}
    721 #else /* HOSTAPD */
    722 	if (src)
    723 		drv_event_eapol_rx(drv->ctx, src, data, datalen);
    724 #endif /* HOSTAPD */
    725 }
    726 
    727 
    728 static void test_driver_ether(struct wpa_driver_test_data *drv,
    729 			      struct sockaddr_un *from, socklen_t fromlen,
    730 			      u8 *data, size_t datalen)
    731 {
    732 	struct l2_ethhdr *eth;
    733 
    734 	if (datalen < sizeof(*eth))
    735 		return;
    736 
    737 	eth = (struct l2_ethhdr *) data;
    738 	wpa_printf(MSG_DEBUG, "test_driver: RX ETHER dst=" MACSTR " src="
    739 		   MACSTR " proto=%04x",
    740 		   MAC2STR(eth->h_dest), MAC2STR(eth->h_source),
    741 		   be_to_host16(eth->h_proto));
    742 
    743 #ifdef CONFIG_IEEE80211R
    744 	if (be_to_host16(eth->h_proto) == ETH_P_RRB) {
    745 		union wpa_event_data ev;
    746 		os_memset(&ev, 0, sizeof(ev));
    747 		ev.ft_rrb_rx.src = eth->h_source;
    748 		ev.ft_rrb_rx.data = data + sizeof(*eth);
    749 		ev.ft_rrb_rx.data_len = datalen - sizeof(*eth);
    750 	}
    751 #endif /* CONFIG_IEEE80211R */
    752 }
    753 
    754 
    755 static void test_driver_mlme(struct wpa_driver_test_data *drv,
    756 			     struct sockaddr_un *from, socklen_t fromlen,
    757 			     u8 *data, size_t datalen)
    758 {
    759 	struct ieee80211_hdr *hdr;
    760 	u16 fc;
    761 	union wpa_event_data event;
    762 	int freq = 0, own_freq;
    763 	struct test_driver_bss *bss;
    764 
    765 	bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
    766 
    767 	if (datalen > 6 && os_memcmp(data, "freq=", 5) == 0) {
    768 		size_t pos;
    769 		for (pos = 5; pos < datalen; pos++) {
    770 			if (data[pos] == ' ')
    771 				break;
    772 		}
    773 		if (pos < datalen) {
    774 			freq = atoi((const char *) &data[5]);
    775 			wpa_printf(MSG_DEBUG, "test_driver(%s): MLME RX on "
    776 				   "freq %d MHz", bss->ifname, freq);
    777 			pos++;
    778 			data += pos;
    779 			datalen -= pos;
    780 		}
    781 	}
    782 
    783 	if (drv->remain_on_channel_freq)
    784 		own_freq = drv->remain_on_channel_freq;
    785 	else
    786 		own_freq = drv->current_freq;
    787 
    788 	if (freq && own_freq && freq != own_freq) {
    789 		wpa_printf(MSG_DEBUG, "test_driver(%s): Ignore MLME RX on "
    790 			   "another frequency %d MHz (own %d MHz)",
    791 			   bss->ifname, freq, own_freq);
    792 		return;
    793 	}
    794 
    795 	hdr = (struct ieee80211_hdr *) data;
    796 
    797 	if (test_driver_get_cli(drv, from, fromlen) == NULL && datalen >= 16) {
    798 		struct test_client_socket *cli;
    799 		cli = os_zalloc(sizeof(*cli));
    800 		if (cli == NULL)
    801 			return;
    802 		wpa_printf(MSG_DEBUG, "Adding client entry for " MACSTR,
    803 			   MAC2STR(hdr->addr2));
    804 		memcpy(cli->addr, hdr->addr2, ETH_ALEN);
    805 		memcpy(&cli->un, from, sizeof(cli->un));
    806 		cli->unlen = fromlen;
    807 		cli->next = drv->cli;
    808 		drv->cli = cli;
    809 	}
    810 
    811 	wpa_hexdump(MSG_MSGDUMP, "test_driver_mlme: received frame",
    812 		    data, datalen);
    813 	fc = le_to_host16(hdr->frame_control);
    814 	if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT) {
    815 		wpa_printf(MSG_ERROR, "%s: received non-mgmt frame",
    816 			   __func__);
    817 		return;
    818 	}
    819 
    820 	os_memset(&event, 0, sizeof(event));
    821 	event.rx_mgmt.frame = data;
    822 	event.rx_mgmt.frame_len = datalen;
    823 	wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
    824 }
    825 
    826 
    827 static void test_driver_receive_unix(int sock, void *eloop_ctx, void *sock_ctx)
    828 {
    829 	struct wpa_driver_test_data *drv = eloop_ctx;
    830 	char buf[2000];
    831 	int res;
    832 	struct sockaddr_un from;
    833 	socklen_t fromlen = sizeof(from);
    834 
    835 	res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
    836 		       (struct sockaddr *) &from, &fromlen);
    837 	if (res < 0) {
    838 		perror("recvfrom(test_socket)");
    839 		return;
    840 	}
    841 	buf[res] = '\0';
    842 
    843 	wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
    844 
    845 	if (strncmp(buf, "SCAN", 4) == 0) {
    846 		test_driver_scan(drv, &from, fromlen, buf + 4);
    847 	} else if (strncmp(buf, "ASSOC ", 6) == 0) {
    848 		test_driver_assoc(drv, &from, fromlen, buf + 6);
    849 	} else if (strcmp(buf, "DISASSOC") == 0) {
    850 		test_driver_disassoc(drv, &from, fromlen);
    851 	} else if (strncmp(buf, "EAPOL ", 6) == 0) {
    852 		test_driver_eapol(drv, &from, fromlen, (u8 *) buf + 6,
    853 				  res - 6);
    854 	} else if (strncmp(buf, "ETHER ", 6) == 0) {
    855 		test_driver_ether(drv, &from, fromlen, (u8 *) buf + 6,
    856 				  res - 6);
    857 	} else if (strncmp(buf, "MLME ", 5) == 0) {
    858 		test_driver_mlme(drv, &from, fromlen, (u8 *) buf + 5, res - 5);
    859 	} else {
    860 		wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
    861 				  (u8 *) buf, res);
    862 	}
    863 }
    864 
    865 
    866 static int test_driver_set_generic_elem(void *priv,
    867 					const u8 *elem, size_t elem_len)
    868 {
    869 	struct test_driver_bss *bss = priv;
    870 
    871 	os_free(bss->ie);
    872 
    873 	if (elem == NULL) {
    874 		bss->ie = NULL;
    875 		bss->ielen = 0;
    876 		return 0;
    877 	}
    878 
    879 	bss->ie = os_malloc(elem_len);
    880 	if (bss->ie == NULL) {
    881 		bss->ielen = 0;
    882 		return -1;
    883 	}
    884 
    885 	memcpy(bss->ie, elem, elem_len);
    886 	bss->ielen = elem_len;
    887 	return 0;
    888 }
    889 
    890 
    891 static int test_driver_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
    892 				     const struct wpabuf *proberesp,
    893 				     const struct wpabuf *assocresp)
    894 {
    895 	struct test_driver_bss *bss = priv;
    896 
    897 	if (beacon == NULL)
    898 		wpa_printf(MSG_DEBUG, "test_driver: Clear Beacon WPS IE");
    899 	else
    900 		wpa_hexdump_buf(MSG_DEBUG, "test_driver: Beacon WPS IE",
    901 				beacon);
    902 
    903 	os_free(bss->wps_beacon_ie);
    904 
    905 	if (beacon == NULL) {
    906 		bss->wps_beacon_ie = NULL;
    907 		bss->wps_beacon_ie_len = 0;
    908 	} else {
    909 		bss->wps_beacon_ie = os_malloc(wpabuf_len(beacon));
    910 		if (bss->wps_beacon_ie == NULL) {
    911 			bss->wps_beacon_ie_len = 0;
    912 			return -1;
    913 		}
    914 
    915 		os_memcpy(bss->wps_beacon_ie, wpabuf_head(beacon),
    916 			  wpabuf_len(beacon));
    917 		bss->wps_beacon_ie_len = wpabuf_len(beacon);
    918 	}
    919 
    920 	if (proberesp == NULL)
    921 		wpa_printf(MSG_DEBUG, "test_driver: Clear Probe Response WPS "
    922 			   "IE");
    923 	else
    924 		wpa_hexdump_buf(MSG_DEBUG, "test_driver: Probe Response WPS "
    925 				"IE", proberesp);
    926 
    927 	os_free(bss->wps_probe_resp_ie);
    928 
    929 	if (proberesp == NULL) {
    930 		bss->wps_probe_resp_ie = NULL;
    931 		bss->wps_probe_resp_ie_len = 0;
    932 	} else {
    933 		bss->wps_probe_resp_ie = os_malloc(wpabuf_len(proberesp));
    934 		if (bss->wps_probe_resp_ie == NULL) {
    935 			bss->wps_probe_resp_ie_len = 0;
    936 			return -1;
    937 		}
    938 
    939 		os_memcpy(bss->wps_probe_resp_ie, wpabuf_head(proberesp),
    940 			  wpabuf_len(proberesp));
    941 		bss->wps_probe_resp_ie_len = wpabuf_len(proberesp);
    942 	}
    943 
    944 	return 0;
    945 }
    946 
    947 
    948 static int test_driver_sta_deauth(void *priv, const u8 *own_addr,
    949 				  const u8 *addr, int reason)
    950 {
    951 	struct test_driver_bss *dbss = priv;
    952 	struct wpa_driver_test_data *drv = dbss->drv;
    953 	struct test_client_socket *cli;
    954 
    955 	if (drv->test_socket < 0)
    956 		return -1;
    957 
    958 	cli = drv->cli;
    959 	while (cli) {
    960 		if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
    961 			break;
    962 		cli = cli->next;
    963 	}
    964 
    965 	if (!cli)
    966 		return -1;
    967 
    968 	return sendto(drv->test_socket, "DEAUTH", 6, 0,
    969 		      (struct sockaddr *) &cli->un, cli->unlen);
    970 }
    971 
    972 
    973 static int test_driver_sta_disassoc(void *priv, const u8 *own_addr,
    974 				    const u8 *addr, int reason)
    975 {
    976 	struct test_driver_bss *dbss = priv;
    977 	struct wpa_driver_test_data *drv = dbss->drv;
    978 	struct test_client_socket *cli;
    979 
    980 	if (drv->test_socket < 0)
    981 		return -1;
    982 
    983 	cli = drv->cli;
    984 	while (cli) {
    985 		if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
    986 			break;
    987 		cli = cli->next;
    988 	}
    989 
    990 	if (!cli)
    991 		return -1;
    992 
    993 	return sendto(drv->test_socket, "DISASSOC", 8, 0,
    994 		      (struct sockaddr *) &cli->un, cli->unlen);
    995 }
    996 
    997 
    998 static int test_driver_bss_add(void *priv, const char *ifname, const u8 *bssid,
    999 			       void *bss_ctx, void **drv_priv)
   1000 {
   1001 	struct test_driver_bss *dbss = priv;
   1002 	struct wpa_driver_test_data *drv = dbss->drv;
   1003 	struct test_driver_bss *bss;
   1004 
   1005 	wpa_printf(MSG_DEBUG, "%s(ifname=%s bssid=" MACSTR ")",
   1006 		   __func__, ifname, MAC2STR(bssid));
   1007 
   1008 	bss = os_zalloc(sizeof(*bss));
   1009 	if (bss == NULL)
   1010 		return -1;
   1011 
   1012 	bss->bss_ctx = bss_ctx;
   1013 	bss->drv = drv;
   1014 	os_strlcpy(bss->ifname, ifname, IFNAMSIZ);
   1015 	os_memcpy(bss->bssid, bssid, ETH_ALEN);
   1016 
   1017 	dl_list_add(&drv->bss, &bss->list);
   1018 	if (drv->global) {
   1019 		drv->global->bss_add_used = 1;
   1020 		os_memcpy(drv->global->req_addr, bssid, ETH_ALEN);
   1021 	}
   1022 
   1023 	if (drv_priv)
   1024 		*drv_priv = bss;
   1025 
   1026 	return 0;
   1027 }
   1028 
   1029 
   1030 static int test_driver_bss_remove(void *priv, const char *ifname)
   1031 {
   1032 	struct test_driver_bss *dbss = priv;
   1033 	struct wpa_driver_test_data *drv = dbss->drv;
   1034 	struct test_driver_bss *bss;
   1035 	struct test_client_socket *cli, *prev_c;
   1036 
   1037 	wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
   1038 
   1039 	dl_list_for_each(bss, &drv->bss, struct test_driver_bss, list) {
   1040 		if (strcmp(bss->ifname, ifname) != 0)
   1041 			continue;
   1042 
   1043 		for (prev_c = NULL, cli = drv->cli; cli;
   1044 		     prev_c = cli, cli = cli->next) {
   1045 			if (cli->bss != bss)
   1046 				continue;
   1047 			if (prev_c)
   1048 				prev_c->next = cli->next;
   1049 			else
   1050 				drv->cli = cli->next;
   1051 			os_free(cli);
   1052 			break;
   1053 		}
   1054 
   1055 		dl_list_del(&bss->list);
   1056 		test_driver_free_bss(bss);
   1057 		return 0;
   1058 	}
   1059 
   1060 	return -1;
   1061 }
   1062 
   1063 
   1064 static int test_driver_if_add(void *priv, enum wpa_driver_if_type type,
   1065 			      const char *ifname, const u8 *addr,
   1066 			      void *bss_ctx, void **drv_priv,
   1067 			      char *force_ifname, u8 *if_addr,
   1068 			      const char *bridge)
   1069 {
   1070 	struct test_driver_bss *dbss = priv;
   1071 	struct wpa_driver_test_data *drv = dbss->drv;
   1072 
   1073 	wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s bss_ctx=%p)",
   1074 		   __func__, type, ifname, bss_ctx);
   1075 	if (addr)
   1076 		os_memcpy(if_addr, addr, ETH_ALEN);
   1077 	else {
   1078 		drv->alloc_iface_idx++;
   1079 		if_addr[0] = 0x02; /* locally administered */
   1080 		sha1_prf(drv->own_addr, ETH_ALEN,
   1081 			 "hostapd test addr generation",
   1082 			 (const u8 *) &drv->alloc_iface_idx,
   1083 			 sizeof(drv->alloc_iface_idx),
   1084 			 if_addr + 1, ETH_ALEN - 1);
   1085 	}
   1086 	if (type == WPA_IF_AP_BSS || type == WPA_IF_P2P_GO ||
   1087 	    type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP)
   1088 		return test_driver_bss_add(priv, ifname, if_addr, bss_ctx,
   1089 					   drv_priv);
   1090 	return 0;
   1091 }
   1092 
   1093 
   1094 static int test_driver_if_remove(void *priv, enum wpa_driver_if_type type,
   1095 				 const char *ifname)
   1096 {
   1097 	wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
   1098 	if (type == WPA_IF_AP_BSS || type == WPA_IF_P2P_GO ||
   1099 	    type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP)
   1100 		return test_driver_bss_remove(priv, ifname);
   1101 	return 0;
   1102 }
   1103 
   1104 
   1105 static int test_driver_valid_bss_mask(void *priv, const u8 *addr,
   1106 				      const u8 *mask)
   1107 {
   1108 	return 0;
   1109 }
   1110 
   1111 
   1112 static int test_driver_set_ssid(void *priv, const u8 *buf, int len)
   1113 {
   1114 	struct test_driver_bss *bss = priv;
   1115 
   1116 	wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, bss->ifname);
   1117 	if (len < 0)
   1118 		return -1;
   1119 	wpa_hexdump_ascii(MSG_DEBUG, "test_driver_set_ssid: SSID", buf, len);
   1120 
   1121 	if ((size_t) len > sizeof(bss->ssid))
   1122 		return -1;
   1123 
   1124 	os_memcpy(bss->ssid, buf, len);
   1125 	bss->ssid_len = len;
   1126 
   1127 	return 0;
   1128 }
   1129 
   1130 
   1131 static int test_driver_set_privacy(void *priv, int enabled)
   1132 {
   1133 	struct test_driver_bss *dbss = priv;
   1134 
   1135 	wpa_printf(MSG_DEBUG, "%s(enabled=%d)",  __func__, enabled);
   1136 	dbss->privacy = enabled;
   1137 
   1138 	return 0;
   1139 }
   1140 
   1141 
   1142 static int test_driver_set_sta_vlan(void *priv, const u8 *addr,
   1143 				    const char *ifname, int vlan_id)
   1144 {
   1145 	wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " ifname=%s vlan_id=%d)",
   1146 		   __func__, MAC2STR(addr), ifname, vlan_id);
   1147 	return 0;
   1148 }
   1149 
   1150 
   1151 static int test_driver_sta_add(void *priv,
   1152 			       struct hostapd_sta_add_params *params)
   1153 {
   1154 	struct test_driver_bss *bss = priv;
   1155 	struct wpa_driver_test_data *drv = bss->drv;
   1156 	struct test_client_socket *cli;
   1157 
   1158 	wpa_printf(MSG_DEBUG, "%s(ifname=%s addr=" MACSTR " aid=%d "
   1159 		   "capability=0x%x listen_interval=%d)",
   1160 		   __func__, bss->ifname, MAC2STR(params->addr), params->aid,
   1161 		   params->capability, params->listen_interval);
   1162 	wpa_hexdump(MSG_DEBUG, "test_driver_sta_add - supp_rates",
   1163 		    params->supp_rates, params->supp_rates_len);
   1164 
   1165 	cli = drv->cli;
   1166 	while (cli) {
   1167 		if (os_memcmp(cli->addr, params->addr, ETH_ALEN) == 0)
   1168 			break;
   1169 		cli = cli->next;
   1170 	}
   1171 	if (!cli) {
   1172 		wpa_printf(MSG_DEBUG, "%s: no matching client entry",
   1173 			   __func__);
   1174 		return -1;
   1175 	}
   1176 
   1177 	cli->bss = bss;
   1178 
   1179 	return 0;
   1180 }
   1181 
   1182 
   1183 static struct wpa_driver_test_data * test_alloc_data(void *ctx,
   1184 						     const char *ifname)
   1185 {
   1186 	struct wpa_driver_test_data *drv;
   1187 	struct test_driver_bss *bss;
   1188 
   1189 	drv = os_zalloc(sizeof(struct wpa_driver_test_data));
   1190 	if (drv == NULL) {
   1191 		wpa_printf(MSG_ERROR, "Could not allocate memory for test "
   1192 			   "driver data");
   1193 		return NULL;
   1194 	}
   1195 
   1196 	bss = os_zalloc(sizeof(struct test_driver_bss));
   1197 	if (bss == NULL) {
   1198 		os_free(drv);
   1199 		return NULL;
   1200 	}
   1201 
   1202 	drv->ctx = ctx;
   1203 	wpa_trace_add_ref(drv, ctx, ctx);
   1204 	dl_list_init(&drv->bss);
   1205 	dl_list_add(&drv->bss, &bss->list);
   1206 	os_strlcpy(bss->ifname, ifname, IFNAMSIZ);
   1207 	bss->bss_ctx = ctx;
   1208 	bss->drv = drv;
   1209 
   1210 	/* Generate a MAC address to help testing with multiple STAs */
   1211 	drv->own_addr[0] = 0x02; /* locally administered */
   1212 	sha1_prf((const u8 *) ifname, os_strlen(ifname),
   1213 		 "test mac addr generation",
   1214 		 NULL, 0, drv->own_addr + 1, ETH_ALEN - 1);
   1215 
   1216 	return drv;
   1217 }
   1218 
   1219 
   1220 static void * test_driver_init(struct hostapd_data *hapd,
   1221 			       struct wpa_init_params *params)
   1222 {
   1223 	struct wpa_driver_test_data *drv;
   1224 	struct sockaddr_un addr_un;
   1225 	struct sockaddr_in addr_in;
   1226 	struct sockaddr *addr;
   1227 	socklen_t alen;
   1228 	struct test_driver_bss *bss;
   1229 
   1230 	drv = test_alloc_data(hapd, params->ifname);
   1231 	if (drv == NULL)
   1232 		return NULL;
   1233 	drv->ap = 1;
   1234 	bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
   1235 
   1236 	bss->bss_ctx = hapd;
   1237 	os_memcpy(bss->bssid, drv->own_addr, ETH_ALEN);
   1238 	os_memcpy(params->own_addr, drv->own_addr, ETH_ALEN);
   1239 
   1240 	if (params->test_socket) {
   1241 		if (os_strlen(params->test_socket) >=
   1242 		    sizeof(addr_un.sun_path)) {
   1243 			printf("Too long test_socket path\n");
   1244 			wpa_driver_test_deinit(bss);
   1245 			return NULL;
   1246 		}
   1247 		if (strncmp(params->test_socket, "DIR:", 4) == 0) {
   1248 			size_t len = strlen(params->test_socket) + 30;
   1249 			drv->test_dir = os_strdup(params->test_socket + 4);
   1250 			drv->own_socket_path = os_malloc(len);
   1251 			if (drv->own_socket_path) {
   1252 				snprintf(drv->own_socket_path, len,
   1253 					 "%s/AP-" MACSTR,
   1254 					 params->test_socket + 4,
   1255 					 MAC2STR(params->own_addr));
   1256 			}
   1257 		} else if (strncmp(params->test_socket, "UDP:", 4) == 0) {
   1258 			drv->udp_port = atoi(params->test_socket + 4);
   1259 		} else {
   1260 			drv->own_socket_path = os_strdup(params->test_socket);
   1261 		}
   1262 		if (drv->own_socket_path == NULL && drv->udp_port == 0) {
   1263 			wpa_driver_test_deinit(bss);
   1264 			return NULL;
   1265 		}
   1266 
   1267 		drv->test_socket = socket(drv->udp_port ? PF_INET : PF_UNIX,
   1268 					  SOCK_DGRAM, 0);
   1269 		if (drv->test_socket < 0) {
   1270 			perror("socket");
   1271 			wpa_driver_test_deinit(bss);
   1272 			return NULL;
   1273 		}
   1274 
   1275 		if (drv->udp_port) {
   1276 			os_memset(&addr_in, 0, sizeof(addr_in));
   1277 			addr_in.sin_family = AF_INET;
   1278 			addr_in.sin_port = htons(drv->udp_port);
   1279 			addr = (struct sockaddr *) &addr_in;
   1280 			alen = sizeof(addr_in);
   1281 		} else {
   1282 			os_memset(&addr_un, 0, sizeof(addr_un));
   1283 			addr_un.sun_family = AF_UNIX;
   1284 			os_strlcpy(addr_un.sun_path, drv->own_socket_path,
   1285 				   sizeof(addr_un.sun_path));
   1286 			addr = (struct sockaddr *) &addr_un;
   1287 			alen = sizeof(addr_un);
   1288 		}
   1289 		if (bind(drv->test_socket, addr, alen) < 0) {
   1290 			perror("bind(PF_UNIX)");
   1291 			close(drv->test_socket);
   1292 			if (drv->own_socket_path)
   1293 				unlink(drv->own_socket_path);
   1294 			wpa_driver_test_deinit(bss);
   1295 			return NULL;
   1296 		}
   1297 		eloop_register_read_sock(drv->test_socket,
   1298 					 test_driver_receive_unix, drv, NULL);
   1299 	} else
   1300 		drv->test_socket = -1;
   1301 
   1302 	return bss;
   1303 }
   1304 
   1305 
   1306 static void wpa_driver_test_poll(void *eloop_ctx, void *timeout_ctx)
   1307 {
   1308 	struct wpa_driver_test_data *drv = eloop_ctx;
   1309 
   1310 #ifdef DRIVER_TEST_UNIX
   1311 	if (drv->associated && drv->hostapd_addr_set) {
   1312 		struct stat st;
   1313 		if (stat(drv->hostapd_addr.sun_path, &st) < 0) {
   1314 			wpa_printf(MSG_DEBUG, "%s: lost connection to AP: %s",
   1315 				   __func__, strerror(errno));
   1316 			drv->associated = 0;
   1317 			wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
   1318 		}
   1319 	}
   1320 #endif /* DRIVER_TEST_UNIX */
   1321 
   1322 	eloop_register_timeout(1, 0, wpa_driver_test_poll, drv, NULL);
   1323 }
   1324 
   1325 
   1326 static void wpa_driver_test_scan_timeout(void *eloop_ctx, void *timeout_ctx)
   1327 {
   1328 	struct wpa_driver_test_data *drv = eloop_ctx;
   1329 	wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
   1330 	if (drv->pending_p2p_scan && drv->p2p) {
   1331 #ifdef CONFIG_P2P
   1332 		size_t i;
   1333 		for (i = 0; i < drv->num_scanres; i++) {
   1334 			struct wpa_scan_res *bss = drv->scanres[i];
   1335 			if (p2p_scan_res_handler(drv->p2p, bss->bssid,
   1336 						 bss->freq, bss->level,
   1337 						 (const u8 *) (bss + 1),
   1338 						 bss->ie_len) > 0)
   1339 				return;
   1340 		}
   1341 		p2p_scan_res_handled(drv->p2p);
   1342 #endif /* CONFIG_P2P */
   1343 		return;
   1344 	}
   1345 	wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
   1346 }
   1347 
   1348 
   1349 #ifdef DRIVER_TEST_UNIX
   1350 static void wpa_driver_scan_dir(struct wpa_driver_test_data *drv,
   1351 				const char *path)
   1352 {
   1353 	struct dirent *dent;
   1354 	DIR *dir;
   1355 	struct sockaddr_un addr;
   1356 	char cmd[512], *pos, *end;
   1357 	int ret;
   1358 
   1359 	dir = opendir(path);
   1360 	if (dir == NULL)
   1361 		return;
   1362 
   1363 	end = cmd + sizeof(cmd);
   1364 	pos = cmd;
   1365 	ret = os_snprintf(pos, end - pos, "SCAN " MACSTR,
   1366 			  MAC2STR(drv->own_addr));
   1367 	if (ret >= 0 && ret < end - pos)
   1368 		pos += ret;
   1369 	if (drv->probe_req_ie) {
   1370 		ret = os_snprintf(pos, end - pos, " ");
   1371 		if (ret >= 0 && ret < end - pos)
   1372 			pos += ret;
   1373 		pos += wpa_snprintf_hex(pos, end - pos, drv->probe_req_ie,
   1374 					drv->probe_req_ie_len);
   1375 	}
   1376 	if (drv->probe_req_ssid_len) {
   1377 		/* Add SSID IE */
   1378 		ret = os_snprintf(pos, end - pos, "%02x%02x",
   1379 				  WLAN_EID_SSID,
   1380 				  (unsigned int) drv->probe_req_ssid_len);
   1381 		if (ret >= 0 && ret < end - pos)
   1382 			pos += ret;
   1383 		pos += wpa_snprintf_hex(pos, end - pos, drv->probe_req_ssid,
   1384 					drv->probe_req_ssid_len);
   1385 	}
   1386 	end[-1] = '\0';
   1387 
   1388 	while ((dent = readdir(dir))) {
   1389 		if (os_strncmp(dent->d_name, "AP-", 3) != 0 &&
   1390 		    os_strncmp(dent->d_name, "STA-", 4) != 0)
   1391 			continue;
   1392 		if (drv->own_socket_path) {
   1393 			size_t olen, dlen;
   1394 			olen = os_strlen(drv->own_socket_path);
   1395 			dlen = os_strlen(dent->d_name);
   1396 			if (olen >= dlen &&
   1397 			    os_strcmp(dent->d_name,
   1398 				      drv->own_socket_path + olen - dlen) == 0)
   1399 				continue;
   1400 		}
   1401 		wpa_printf(MSG_DEBUG, "%s: SCAN %s", __func__, dent->d_name);
   1402 
   1403 		os_memset(&addr, 0, sizeof(addr));
   1404 		addr.sun_family = AF_UNIX;
   1405 		os_snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
   1406 			    path, dent->d_name);
   1407 
   1408 		if (sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
   1409 			   (struct sockaddr *) &addr, sizeof(addr)) < 0) {
   1410 			perror("sendto(test_socket)");
   1411 		}
   1412 	}
   1413 	closedir(dir);
   1414 }
   1415 #endif /* DRIVER_TEST_UNIX */
   1416 
   1417 
   1418 static int wpa_driver_test_scan(void *priv,
   1419 				struct wpa_driver_scan_params *params)
   1420 {
   1421 	struct test_driver_bss *dbss = priv;
   1422 	struct wpa_driver_test_data *drv = dbss->drv;
   1423 	size_t i;
   1424 
   1425 	wpa_printf(MSG_DEBUG, "%s: priv=%p", __func__, priv);
   1426 
   1427 	os_free(drv->probe_req_ie);
   1428 	if (params->extra_ies) {
   1429 		drv->probe_req_ie = os_malloc(params->extra_ies_len);
   1430 		if (drv->probe_req_ie == NULL) {
   1431 			drv->probe_req_ie_len = 0;
   1432 			return -1;
   1433 		}
   1434 		os_memcpy(drv->probe_req_ie, params->extra_ies,
   1435 			  params->extra_ies_len);
   1436 		drv->probe_req_ie_len = params->extra_ies_len;
   1437 	} else {
   1438 		drv->probe_req_ie = NULL;
   1439 		drv->probe_req_ie_len = 0;
   1440 	}
   1441 
   1442 	for (i = 0; i < params->num_ssids; i++)
   1443 		wpa_hexdump(MSG_DEBUG, "Scan SSID",
   1444 			    params->ssids[i].ssid, params->ssids[i].ssid_len);
   1445 	drv->probe_req_ssid_len = 0;
   1446 	if (params->num_ssids) {
   1447 		os_memcpy(drv->probe_req_ssid, params->ssids[0].ssid,
   1448 			  params->ssids[0].ssid_len);
   1449 		drv->probe_req_ssid_len = params->ssids[0].ssid_len;
   1450 	}
   1451 	wpa_hexdump(MSG_DEBUG, "Scan extra IE(s)",
   1452 		    params->extra_ies, params->extra_ies_len);
   1453 
   1454 	drv->num_scanres = 0;
   1455 
   1456 #ifdef DRIVER_TEST_UNIX
   1457 	if (drv->test_socket >= 0 && drv->test_dir)
   1458 		wpa_driver_scan_dir(drv, drv->test_dir);
   1459 
   1460 	if (drv->test_socket >= 0 && drv->hostapd_addr_set &&
   1461 	    sendto(drv->test_socket, "SCAN", 4, 0,
   1462 		   (struct sockaddr *) &drv->hostapd_addr,
   1463 		   sizeof(drv->hostapd_addr)) < 0) {
   1464 		perror("sendto(test_socket)");
   1465 	}
   1466 #endif /* DRIVER_TEST_UNIX */
   1467 
   1468 	if (drv->test_socket >= 0 && drv->hostapd_addr_udp_set &&
   1469 	    sendto(drv->test_socket, "SCAN", 4, 0,
   1470 		   (struct sockaddr *) &drv->hostapd_addr_udp,
   1471 		   sizeof(drv->hostapd_addr_udp)) < 0) {
   1472 		perror("sendto(test_socket)");
   1473 	}
   1474 
   1475 	eloop_cancel_timeout(wpa_driver_test_scan_timeout, drv, drv->ctx);
   1476 	eloop_register_timeout(1, 0, wpa_driver_test_scan_timeout, drv,
   1477 			       drv->ctx);
   1478 	return 0;
   1479 }
   1480 
   1481 
   1482 static struct wpa_scan_results * wpa_driver_test_get_scan_results2(void *priv)
   1483 {
   1484 	struct test_driver_bss *dbss = priv;
   1485 	struct wpa_driver_test_data *drv = dbss->drv;
   1486 	struct wpa_scan_results *res;
   1487 	size_t i;
   1488 
   1489 	res = os_zalloc(sizeof(*res));
   1490 	if (res == NULL)
   1491 		return NULL;
   1492 
   1493 	res->res = os_zalloc(drv->num_scanres * sizeof(struct wpa_scan_res *));
   1494 	if (res->res == NULL) {
   1495 		os_free(res);
   1496 		return NULL;
   1497 	}
   1498 
   1499 	for (i = 0; i < drv->num_scanres; i++) {
   1500 		struct wpa_scan_res *r;
   1501 		if (drv->scanres[i] == NULL)
   1502 			continue;
   1503 		r = os_malloc(sizeof(*r) + drv->scanres[i]->ie_len);
   1504 		if (r == NULL)
   1505 			break;
   1506 		os_memcpy(r, drv->scanres[i],
   1507 			  sizeof(*r) + drv->scanres[i]->ie_len);
   1508 		res->res[res->num++] = r;
   1509 	}
   1510 
   1511 	return res;
   1512 }
   1513 
   1514 
   1515 static int wpa_driver_test_set_key(const char *ifname, void *priv,
   1516 				   enum wpa_alg alg, const u8 *addr,
   1517 				   int key_idx, int set_tx,
   1518 				   const u8 *seq, size_t seq_len,
   1519 				   const u8 *key, size_t key_len)
   1520 {
   1521 	wpa_printf(MSG_DEBUG, "%s: ifname=%s priv=%p alg=%d key_idx=%d "
   1522 		   "set_tx=%d",
   1523 		   __func__, ifname, priv, alg, key_idx, set_tx);
   1524 	if (addr)
   1525 		wpa_printf(MSG_DEBUG, "   addr=" MACSTR, MAC2STR(addr));
   1526 	if (seq)
   1527 		wpa_hexdump(MSG_DEBUG, "   seq", seq, seq_len);
   1528 	if (key)
   1529 		wpa_hexdump_key(MSG_DEBUG, "   key", key, key_len);
   1530 	return 0;
   1531 }
   1532 
   1533 
   1534 static int wpa_driver_update_mode(struct wpa_driver_test_data *drv, int ap)
   1535 {
   1536 	if (ap && !drv->ap) {
   1537 		wpa_driver_test_close_test_socket(drv);
   1538 		wpa_driver_test_attach(drv, drv->test_dir, 1);
   1539 		drv->ap = 1;
   1540 	} else if (!ap && drv->ap) {
   1541 		wpa_driver_test_close_test_socket(drv);
   1542 		wpa_driver_test_attach(drv, drv->test_dir, 0);
   1543 		drv->ap = 0;
   1544 	}
   1545 
   1546 	return 0;
   1547 }
   1548 
   1549 
   1550 static int wpa_driver_test_associate(
   1551 	void *priv, struct wpa_driver_associate_params *params)
   1552 {
   1553 	struct test_driver_bss *dbss = priv;
   1554 	struct wpa_driver_test_data *drv = dbss->drv;
   1555 	wpa_printf(MSG_DEBUG, "%s: priv=%p freq=%d pairwise_suite=%d "
   1556 		   "group_suite=%d key_mgmt_suite=%d auth_alg=%d mode=%d",
   1557 		   __func__, priv, params->freq, params->pairwise_suite,
   1558 		   params->group_suite, params->key_mgmt_suite,
   1559 		   params->auth_alg, params->mode);
   1560 	wpa_driver_update_mode(drv, params->mode == IEEE80211_MODE_AP);
   1561 	if (params->bssid) {
   1562 		wpa_printf(MSG_DEBUG, "   bssid=" MACSTR,
   1563 			   MAC2STR(params->bssid));
   1564 	}
   1565 	if (params->ssid) {
   1566 		wpa_hexdump_ascii(MSG_DEBUG, "   ssid",
   1567 				  params->ssid, params->ssid_len);
   1568 	}
   1569 	if (params->wpa_ie) {
   1570 		wpa_hexdump(MSG_DEBUG, "   wpa_ie",
   1571 			    params->wpa_ie, params->wpa_ie_len);
   1572 		drv->assoc_wpa_ie_len = params->wpa_ie_len;
   1573 		if (drv->assoc_wpa_ie_len > sizeof(drv->assoc_wpa_ie))
   1574 			drv->assoc_wpa_ie_len = sizeof(drv->assoc_wpa_ie);
   1575 		os_memcpy(drv->assoc_wpa_ie, params->wpa_ie,
   1576 			  drv->assoc_wpa_ie_len);
   1577 	} else
   1578 		drv->assoc_wpa_ie_len = 0;
   1579 
   1580 	wpa_driver_update_mode(drv, params->mode == IEEE80211_MODE_AP);
   1581 
   1582 	drv->ibss = params->mode == IEEE80211_MODE_IBSS;
   1583 	dbss->privacy = params->key_mgmt_suite &
   1584 		(WPA_KEY_MGMT_IEEE8021X |
   1585 		 WPA_KEY_MGMT_PSK |
   1586 		 WPA_KEY_MGMT_WPA_NONE |
   1587 		 WPA_KEY_MGMT_FT_IEEE8021X |
   1588 		 WPA_KEY_MGMT_FT_PSK |
   1589 		 WPA_KEY_MGMT_IEEE8021X_SHA256 |
   1590 		 WPA_KEY_MGMT_PSK_SHA256);
   1591 	if (params->wep_key_len[params->wep_tx_keyidx])
   1592 		dbss->privacy = 1;
   1593 
   1594 #ifdef DRIVER_TEST_UNIX
   1595 	if (drv->test_dir && params->bssid &&
   1596 	    params->mode != IEEE80211_MODE_IBSS) {
   1597 		os_memset(&drv->hostapd_addr, 0, sizeof(drv->hostapd_addr));
   1598 		drv->hostapd_addr.sun_family = AF_UNIX;
   1599 		os_snprintf(drv->hostapd_addr.sun_path,
   1600 			    sizeof(drv->hostapd_addr.sun_path),
   1601 			    "%s/AP-" MACSTR,
   1602 			    drv->test_dir, MAC2STR(params->bssid));
   1603 		drv->hostapd_addr_set = 1;
   1604 	}
   1605 #endif /* DRIVER_TEST_UNIX */
   1606 
   1607 	if (params->mode == IEEE80211_MODE_AP) {
   1608 		os_memcpy(dbss->ssid, params->ssid, params->ssid_len);
   1609 		dbss->ssid_len = params->ssid_len;
   1610 		os_memcpy(dbss->bssid, drv->own_addr, ETH_ALEN);
   1611 		if (params->wpa_ie && params->wpa_ie_len) {
   1612 			dbss->ie = os_malloc(params->wpa_ie_len);
   1613 			if (dbss->ie) {
   1614 				os_memcpy(dbss->ie, params->wpa_ie,
   1615 					  params->wpa_ie_len);
   1616 				dbss->ielen = params->wpa_ie_len;
   1617 			}
   1618 		}
   1619 	} else if (drv->test_socket >= 0 &&
   1620 		   (drv->hostapd_addr_set || drv->hostapd_addr_udp_set)) {
   1621 		char cmd[200], *pos, *end;
   1622 		int ret;
   1623 		end = cmd + sizeof(cmd);
   1624 		pos = cmd;
   1625 		ret = os_snprintf(pos, end - pos, "ASSOC " MACSTR " ",
   1626 				  MAC2STR(drv->own_addr));
   1627 		if (ret >= 0 && ret < end - pos)
   1628 			pos += ret;
   1629 		pos += wpa_snprintf_hex(pos, end - pos, params->ssid,
   1630 					params->ssid_len);
   1631 		ret = os_snprintf(pos, end - pos, " ");
   1632 		if (ret >= 0 && ret < end - pos)
   1633 			pos += ret;
   1634 		pos += wpa_snprintf_hex(pos, end - pos, params->wpa_ie,
   1635 					params->wpa_ie_len);
   1636 		end[-1] = '\0';
   1637 #ifdef DRIVER_TEST_UNIX
   1638 		if (drv->hostapd_addr_set &&
   1639 		    sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
   1640 			   (struct sockaddr *) &drv->hostapd_addr,
   1641 			   sizeof(drv->hostapd_addr)) < 0) {
   1642 			perror("sendto(test_socket)");
   1643 			return -1;
   1644 		}
   1645 #endif /* DRIVER_TEST_UNIX */
   1646 		if (drv->hostapd_addr_udp_set &&
   1647 		    sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
   1648 			   (struct sockaddr *) &drv->hostapd_addr_udp,
   1649 			   sizeof(drv->hostapd_addr_udp)) < 0) {
   1650 			perror("sendto(test_socket)");
   1651 			return -1;
   1652 		}
   1653 
   1654 		os_memcpy(dbss->ssid, params->ssid, params->ssid_len);
   1655 		dbss->ssid_len = params->ssid_len;
   1656 	} else {
   1657 		drv->associated = 1;
   1658 		if (params->mode == IEEE80211_MODE_IBSS) {
   1659 			os_memcpy(dbss->ssid, params->ssid, params->ssid_len);
   1660 			dbss->ssid_len = params->ssid_len;
   1661 			if (params->bssid)
   1662 				os_memcpy(dbss->bssid, params->bssid,
   1663 					  ETH_ALEN);
   1664 			else {
   1665 				os_get_random(dbss->bssid, ETH_ALEN);
   1666 				dbss->bssid[0] &= ~0x01;
   1667 				dbss->bssid[0] |= 0x02;
   1668 			}
   1669 		}
   1670 		wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
   1671 	}
   1672 
   1673 	return 0;
   1674 }
   1675 
   1676 
   1677 static int wpa_driver_test_get_bssid(void *priv, u8 *bssid)
   1678 {
   1679 	struct test_driver_bss *dbss = priv;
   1680 	os_memcpy(bssid, dbss->bssid, ETH_ALEN);
   1681 	return 0;
   1682 }
   1683 
   1684 
   1685 static int wpa_driver_test_get_ssid(void *priv, u8 *ssid)
   1686 {
   1687 	struct test_driver_bss *dbss = priv;
   1688 	os_memcpy(ssid, dbss->ssid, 32);
   1689 	return dbss->ssid_len;
   1690 }
   1691 
   1692 
   1693 static int wpa_driver_test_send_disassoc(struct wpa_driver_test_data *drv)
   1694 {
   1695 #ifdef DRIVER_TEST_UNIX
   1696 	if (drv->test_socket >= 0 &&
   1697 	    sendto(drv->test_socket, "DISASSOC", 8, 0,
   1698 		   (struct sockaddr *) &drv->hostapd_addr,
   1699 		   sizeof(drv->hostapd_addr)) < 0) {
   1700 		perror("sendto(test_socket)");
   1701 		return -1;
   1702 	}
   1703 #endif /* DRIVER_TEST_UNIX */
   1704 	if (drv->test_socket >= 0 && drv->hostapd_addr_udp_set &&
   1705 	    sendto(drv->test_socket, "DISASSOC", 8, 0,
   1706 		   (struct sockaddr *) &drv->hostapd_addr_udp,
   1707 		   sizeof(drv->hostapd_addr_udp)) < 0) {
   1708 		perror("sendto(test_socket)");
   1709 		return -1;
   1710 	}
   1711 	return 0;
   1712 }
   1713 
   1714 
   1715 static int wpa_driver_test_deauthenticate(void *priv, const u8 *addr,
   1716 					  int reason_code)
   1717 {
   1718 	struct test_driver_bss *dbss = priv;
   1719 	struct wpa_driver_test_data *drv = dbss->drv;
   1720 	wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d",
   1721 		   __func__, MAC2STR(addr), reason_code);
   1722 	os_memset(dbss->bssid, 0, ETH_ALEN);
   1723 	drv->associated = 0;
   1724 	wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
   1725 	return wpa_driver_test_send_disassoc(drv);
   1726 }
   1727 
   1728 
   1729 static int wpa_driver_test_disassociate(void *priv, const u8 *addr,
   1730 					int reason_code)
   1731 {
   1732 	struct test_driver_bss *dbss = priv;
   1733 	struct wpa_driver_test_data *drv = dbss->drv;
   1734 	wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d",
   1735 		   __func__, MAC2STR(addr), reason_code);
   1736 	os_memset(dbss->bssid, 0, ETH_ALEN);
   1737 	drv->associated = 0;
   1738 	wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
   1739 	return wpa_driver_test_send_disassoc(drv);
   1740 }
   1741 
   1742 
   1743 static const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
   1744 {
   1745 	const u8 *end, *pos;
   1746 
   1747 	pos = (const u8 *) (res + 1);
   1748 	end = pos + res->ie_len;
   1749 
   1750 	while (pos + 1 < end) {
   1751 		if (pos + 2 + pos[1] > end)
   1752 			break;
   1753 		if (pos[0] == ie)
   1754 			return pos;
   1755 		pos += 2 + pos[1];
   1756 	}
   1757 
   1758 	return NULL;
   1759 }
   1760 
   1761 
   1762 static void wpa_driver_test_scanresp(struct wpa_driver_test_data *drv,
   1763 				     struct sockaddr *from,
   1764 				     socklen_t fromlen,
   1765 				     const char *data)
   1766 {
   1767 	struct wpa_scan_res *res;
   1768 	const char *pos, *pos2;
   1769 	size_t len;
   1770 	u8 *ie_pos, *ie_start, *ie_end;
   1771 #define MAX_IE_LEN 1000
   1772 	const u8 *ds_params;
   1773 
   1774 	wpa_printf(MSG_DEBUG, "test_driver: SCANRESP %s", data);
   1775 	if (drv->num_scanres >= MAX_SCAN_RESULTS) {
   1776 		wpa_printf(MSG_DEBUG, "test_driver: No room for the new scan "
   1777 			   "result");
   1778 		return;
   1779 	}
   1780 
   1781 	/* SCANRESP BSSID SSID IEs */
   1782 
   1783 	res = os_zalloc(sizeof(*res) + MAX_IE_LEN);
   1784 	if (res == NULL)
   1785 		return;
   1786 	ie_start = ie_pos = (u8 *) (res + 1);
   1787 	ie_end = ie_pos + MAX_IE_LEN;
   1788 
   1789 	if (hwaddr_aton(data, res->bssid)) {
   1790 		wpa_printf(MSG_DEBUG, "test_driver: invalid BSSID in scanres");
   1791 		os_free(res);
   1792 		return;
   1793 	}
   1794 
   1795 	pos = data + 17;
   1796 	while (*pos == ' ')
   1797 		pos++;
   1798 	pos2 = os_strchr(pos, ' ');
   1799 	if (pos2 == NULL) {
   1800 		wpa_printf(MSG_DEBUG, "test_driver: invalid SSID termination "
   1801 			   "in scanres");
   1802 		os_free(res);
   1803 		return;
   1804 	}
   1805 	len = (pos2 - pos) / 2;
   1806 	if (len > 32)
   1807 		len = 32;
   1808 	/*
   1809 	 * Generate SSID IE from the SSID field since this IE is not included
   1810 	 * in the main IE field.
   1811 	 */
   1812 	*ie_pos++ = WLAN_EID_SSID;
   1813 	*ie_pos++ = len;
   1814 	if (hexstr2bin(pos, ie_pos, len) < 0) {
   1815 		wpa_printf(MSG_DEBUG, "test_driver: invalid SSID in scanres");
   1816 		os_free(res);
   1817 		return;
   1818 	}
   1819 	ie_pos += len;
   1820 
   1821 	pos = pos2 + 1;
   1822 	pos2 = os_strchr(pos, ' ');
   1823 	if (pos2 == NULL)
   1824 		len = os_strlen(pos) / 2;
   1825 	else
   1826 		len = (pos2 - pos) / 2;
   1827 	if ((int) len > ie_end - ie_pos)
   1828 		len = ie_end - ie_pos;
   1829 	if (hexstr2bin(pos, ie_pos, len) < 0) {
   1830 		wpa_printf(MSG_DEBUG, "test_driver: invalid IEs in scanres");
   1831 		os_free(res);
   1832 		return;
   1833 	}
   1834 	ie_pos += len;
   1835 	res->ie_len = ie_pos - ie_start;
   1836 
   1837 	if (pos2) {
   1838 		pos = pos2 + 1;
   1839 		while (*pos == ' ')
   1840 			pos++;
   1841 		if (os_strstr(pos, "PRIVACY"))
   1842 			res->caps |= IEEE80211_CAP_PRIVACY;
   1843 		if (os_strstr(pos, "IBSS"))
   1844 			res->caps |= IEEE80211_CAP_IBSS;
   1845 	}
   1846 
   1847 	ds_params = wpa_scan_get_ie(res, WLAN_EID_DS_PARAMS);
   1848 	if (ds_params && ds_params[1] > 0) {
   1849 		if (ds_params[2] >= 1 && ds_params[2] <= 13)
   1850 			res->freq = 2407 + ds_params[2] * 5;
   1851 	}
   1852 
   1853 	os_free(drv->scanres[drv->num_scanres]);
   1854 	drv->scanres[drv->num_scanres++] = res;
   1855 }
   1856 
   1857 
   1858 static void wpa_driver_test_assocresp(struct wpa_driver_test_data *drv,
   1859 				      struct sockaddr *from,
   1860 				      socklen_t fromlen,
   1861 				      const char *data)
   1862 {
   1863 	struct test_driver_bss *bss;
   1864 
   1865 	bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
   1866 
   1867 	/* ASSOCRESP BSSID <res> */
   1868 	if (hwaddr_aton(data, bss->bssid)) {
   1869 		wpa_printf(MSG_DEBUG, "test_driver: invalid BSSID in "
   1870 			   "assocresp");
   1871 	}
   1872 	if (drv->use_associnfo) {
   1873 		union wpa_event_data event;
   1874 		os_memset(&event, 0, sizeof(event));
   1875 		event.assoc_info.req_ies = drv->assoc_wpa_ie;
   1876 		event.assoc_info.req_ies_len = drv->assoc_wpa_ie_len;
   1877 		wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &event);
   1878 	}
   1879 	drv->associated = 1;
   1880 	wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
   1881 }
   1882 
   1883 
   1884 static void wpa_driver_test_disassoc(struct wpa_driver_test_data *drv,
   1885 				     struct sockaddr *from,
   1886 				     socklen_t fromlen)
   1887 {
   1888 	drv->associated = 0;
   1889 	wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
   1890 }
   1891 
   1892 
   1893 static void wpa_driver_test_eapol(struct wpa_driver_test_data *drv,
   1894 				  struct sockaddr *from,
   1895 				  socklen_t fromlen,
   1896 				  const u8 *data, size_t data_len)
   1897 {
   1898 	const u8 *src;
   1899 	struct test_driver_bss *bss;
   1900 
   1901 	bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
   1902 
   1903 	if (data_len > 14) {
   1904 		/* Skip Ethernet header */
   1905 		src = data + ETH_ALEN;
   1906 		data += 14;
   1907 		data_len -= 14;
   1908 	} else
   1909 		src = bss->bssid;
   1910 
   1911 	drv_event_eapol_rx(drv->ctx, src, data, data_len);
   1912 }
   1913 
   1914 
   1915 static void wpa_driver_test_mlme(struct wpa_driver_test_data *drv,
   1916 				 struct sockaddr *from,
   1917 				 socklen_t fromlen,
   1918 				 const u8 *data, size_t data_len)
   1919 {
   1920 	int freq = 0, own_freq;
   1921 	union wpa_event_data event;
   1922 	const struct ieee80211_mgmt *mgmt;
   1923 	u16 fc;
   1924 	struct test_driver_bss *bss;
   1925 
   1926 	bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
   1927 	if (data_len > 6 && os_memcmp(data, "freq=", 5) == 0) {
   1928 		size_t pos;
   1929 		for (pos = 5; pos < data_len; pos++) {
   1930 			if (data[pos] == ' ')
   1931 				break;
   1932 		}
   1933 		if (pos < data_len) {
   1934 			freq = atoi((const char *) &data[5]);
   1935 			wpa_printf(MSG_DEBUG, "test_driver(%s): MLME RX on "
   1936 				   "freq %d MHz", bss->ifname, freq);
   1937 			pos++;
   1938 			data += pos;
   1939 			data_len -= pos;
   1940 		}
   1941 	}
   1942 
   1943 	if (drv->remain_on_channel_freq)
   1944 		own_freq = drv->remain_on_channel_freq;
   1945 	else
   1946 		own_freq = drv->current_freq;
   1947 
   1948 	if (freq && own_freq && freq != own_freq) {
   1949 		wpa_printf(MSG_DEBUG, "test_driver(%s): Ignore MLME RX on "
   1950 			   "another frequency %d MHz (own %d MHz)",
   1951 			   bss->ifname, freq, own_freq);
   1952 		return;
   1953 	}
   1954 
   1955 	os_memset(&event, 0, sizeof(event));
   1956 	event.mlme_rx.buf = data;
   1957 	event.mlme_rx.len = data_len;
   1958 	event.mlme_rx.freq = freq;
   1959 	wpa_supplicant_event(drv->ctx, EVENT_MLME_RX, &event);
   1960 
   1961 	mgmt = (const struct ieee80211_mgmt *) data;
   1962 	fc = le_to_host16(mgmt->frame_control);
   1963 
   1964 	if (drv->probe_req_report && data_len >= 24) {
   1965 		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
   1966 		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ) {
   1967 			os_memset(&event, 0, sizeof(event));
   1968 			event.rx_probe_req.sa = mgmt->sa;
   1969 			event.rx_probe_req.ie = mgmt->u.probe_req.variable;
   1970 			event.rx_probe_req.ie_len =
   1971 				data_len - (mgmt->u.probe_req.variable - data);
   1972 			wpa_supplicant_event(drv->ctx, EVENT_RX_PROBE_REQ,
   1973 					     &event);
   1974 #ifdef CONFIG_P2P
   1975 			if (drv->p2p)
   1976 				p2p_probe_req_rx(drv->p2p, mgmt->sa,
   1977 						 event.rx_probe_req.ie,
   1978 						 event.rx_probe_req.ie_len);
   1979 #endif /* CONFIG_P2P */
   1980 		}
   1981 	}
   1982 
   1983 #ifdef CONFIG_P2P
   1984 	if (drv->p2p &&
   1985 	    WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
   1986 	    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
   1987 		size_t hdr_len;
   1988 		hdr_len = (const u8 *)
   1989 			&mgmt->u.action.u.vs_public_action.action - data;
   1990 		p2p_rx_action(drv->p2p, mgmt->da, mgmt->sa, mgmt->bssid,
   1991 			      mgmt->u.action.category,
   1992 			      &mgmt->u.action.u.vs_public_action.action,
   1993 			      data_len - hdr_len, freq);
   1994 	}
   1995 #endif /* CONFIG_P2P */
   1996 
   1997 }
   1998 
   1999 
   2000 static void wpa_driver_test_scan_cmd(struct wpa_driver_test_data *drv,
   2001 				     struct sockaddr *from,
   2002 				     socklen_t fromlen,
   2003 				     const u8 *data, size_t data_len)
   2004 {
   2005 	char buf[512], *pos, *end;
   2006 	int ret;
   2007 	struct test_driver_bss *bss;
   2008 
   2009 	bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
   2010 
   2011 	/* data: optional [ STA-addr | ' ' | IEs(hex) ] */
   2012 #ifdef CONFIG_P2P
   2013 	if (drv->probe_req_report && drv->p2p && data_len) {
   2014 		const char *d = (const char *) data;
   2015 		u8 sa[ETH_ALEN];
   2016 		u8 ie[512];
   2017 		size_t ielen;
   2018 
   2019 		if (hwaddr_aton(d, sa))
   2020 			return;
   2021 		d += 18;
   2022 		while (*d == ' ')
   2023 			d++;
   2024 		ielen = os_strlen(d) / 2;
   2025 		if (ielen > sizeof(ie))
   2026 			ielen = sizeof(ie);
   2027 		if (hexstr2bin(d, ie, ielen) < 0)
   2028 			ielen = 0;
   2029 		drv->probe_from = from;
   2030 		drv->probe_from_len = fromlen;
   2031 		p2p_probe_req_rx(drv->p2p, sa, ie, ielen);
   2032 		drv->probe_from = NULL;
   2033 	}
   2034 #endif /* CONFIG_P2P */
   2035 
   2036 	if (!drv->ibss)
   2037 		return;
   2038 
   2039 	pos = buf;
   2040 	end = buf + sizeof(buf);
   2041 
   2042 	/* reply: SCANRESP BSSID SSID IEs */
   2043 	ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
   2044 		       MAC2STR(bss->bssid));
   2045 	if (ret < 0 || ret >= end - pos)
   2046 		return;
   2047 	pos += ret;
   2048 	pos += wpa_snprintf_hex(pos, end - pos,
   2049 				bss->ssid, bss->ssid_len);
   2050 	ret = snprintf(pos, end - pos, " ");
   2051 	if (ret < 0 || ret >= end - pos)
   2052 		return;
   2053 	pos += ret;
   2054 	pos += wpa_snprintf_hex(pos, end - pos, drv->assoc_wpa_ie,
   2055 				drv->assoc_wpa_ie_len);
   2056 
   2057 	if (bss->privacy) {
   2058 		ret = snprintf(pos, end - pos, " PRIVACY");
   2059 		if (ret < 0 || ret >= end - pos)
   2060 			return;
   2061 		pos += ret;
   2062 	}
   2063 
   2064 	ret = snprintf(pos, end - pos, " IBSS");
   2065 	if (ret < 0 || ret >= end - pos)
   2066 		return;
   2067 	pos += ret;
   2068 
   2069 	sendto(drv->test_socket, buf, pos - buf, 0,
   2070 	       (struct sockaddr *) from, fromlen);
   2071 }
   2072 
   2073 
   2074 static void wpa_driver_test_receive_unix(int sock, void *eloop_ctx,
   2075 					 void *sock_ctx)
   2076 {
   2077 	struct wpa_driver_test_data *drv = eloop_ctx;
   2078 	char *buf;
   2079 	int res;
   2080 	struct sockaddr_storage from;
   2081 	socklen_t fromlen = sizeof(from);
   2082 	const size_t buflen = 2000;
   2083 
   2084 	if (drv->ap) {
   2085 		test_driver_receive_unix(sock, eloop_ctx, sock_ctx);
   2086 		return;
   2087 	}
   2088 
   2089 	buf = os_malloc(buflen);
   2090 	if (buf == NULL)
   2091 		return;
   2092 	res = recvfrom(sock, buf, buflen - 1, 0,
   2093 		       (struct sockaddr *) &from, &fromlen);
   2094 	if (res < 0) {
   2095 		perror("recvfrom(test_socket)");
   2096 		os_free(buf);
   2097 		return;
   2098 	}
   2099 	buf[res] = '\0';
   2100 
   2101 	wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
   2102 
   2103 	if (os_strncmp(buf, "SCANRESP ", 9) == 0) {
   2104 		wpa_driver_test_scanresp(drv, (struct sockaddr *) &from,
   2105 					 fromlen, buf + 9);
   2106 	} else if (os_strncmp(buf, "ASSOCRESP ", 10) == 0) {
   2107 		wpa_driver_test_assocresp(drv, (struct sockaddr *) &from,
   2108 					  fromlen, buf + 10);
   2109 	} else if (os_strcmp(buf, "DISASSOC") == 0) {
   2110 		wpa_driver_test_disassoc(drv, (struct sockaddr *) &from,
   2111 					 fromlen);
   2112 	} else if (os_strcmp(buf, "DEAUTH") == 0) {
   2113 		wpa_driver_test_disassoc(drv, (struct sockaddr *) &from,
   2114 					 fromlen);
   2115 	} else if (os_strncmp(buf, "EAPOL ", 6) == 0) {
   2116 		wpa_driver_test_eapol(drv, (struct sockaddr *) &from, fromlen,
   2117 				      (const u8 *) buf + 6, res - 6);
   2118 	} else if (os_strncmp(buf, "MLME ", 5) == 0) {
   2119 		wpa_driver_test_mlme(drv, (struct sockaddr *) &from, fromlen,
   2120 				     (const u8 *) buf + 5, res - 5);
   2121 	} else if (os_strncmp(buf, "SCAN ", 5) == 0) {
   2122 		wpa_driver_test_scan_cmd(drv, (struct sockaddr *) &from,
   2123 					 fromlen,
   2124 					 (const u8 *) buf + 5, res - 5);
   2125 	} else {
   2126 		wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
   2127 				  (u8 *) buf, res);
   2128 	}
   2129 	os_free(buf);
   2130 }
   2131 
   2132 
   2133 static void * wpa_driver_test_init2(void *ctx, const char *ifname,
   2134 				    void *global_priv)
   2135 {
   2136 	struct wpa_driver_test_data *drv;
   2137 	struct wpa_driver_test_global *global = global_priv;
   2138 	struct test_driver_bss *bss;
   2139 
   2140 	drv = test_alloc_data(ctx, ifname);
   2141 	if (drv == NULL)
   2142 		return NULL;
   2143 	bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
   2144 	drv->global = global_priv;
   2145 	drv->test_socket = -1;
   2146 
   2147 	/* Set dummy BSSID and SSID for testing. */
   2148 	bss->bssid[0] = 0x02;
   2149 	bss->bssid[1] = 0x00;
   2150 	bss->bssid[2] = 0x00;
   2151 	bss->bssid[3] = 0x00;
   2152 	bss->bssid[4] = 0x00;
   2153 	bss->bssid[5] = 0x01;
   2154 	os_memcpy(bss->ssid, "test", 5);
   2155 	bss->ssid_len = 4;
   2156 
   2157 	if (global->bss_add_used) {
   2158 		os_memcpy(drv->own_addr, global->req_addr, ETH_ALEN);
   2159 		global->bss_add_used = 0;
   2160 	}
   2161 
   2162 	eloop_register_timeout(1, 0, wpa_driver_test_poll, drv, NULL);
   2163 
   2164 	return bss;
   2165 }
   2166 
   2167 
   2168 static void wpa_driver_test_close_test_socket(struct wpa_driver_test_data *drv)
   2169 {
   2170 	if (drv->test_socket >= 0) {
   2171 		eloop_unregister_read_sock(drv->test_socket);
   2172 		close(drv->test_socket);
   2173 		drv->test_socket = -1;
   2174 	}
   2175 
   2176 	if (drv->own_socket_path) {
   2177 		unlink(drv->own_socket_path);
   2178 		os_free(drv->own_socket_path);
   2179 		drv->own_socket_path = NULL;
   2180 	}
   2181 }
   2182 
   2183 
   2184 static void wpa_driver_test_deinit(void *priv)
   2185 {
   2186 	struct test_driver_bss *dbss = priv;
   2187 	struct wpa_driver_test_data *drv = dbss->drv;
   2188 	struct test_client_socket *cli, *prev;
   2189 	int i;
   2190 
   2191 #ifdef CONFIG_P2P
   2192 	if (drv->p2p)
   2193 		p2p_deinit(drv->p2p);
   2194 	wpabuf_free(drv->pending_action_tx);
   2195 #endif /* CONFIG_P2P */
   2196 
   2197 	cli = drv->cli;
   2198 	while (cli) {
   2199 		prev = cli;
   2200 		cli = cli->next;
   2201 		os_free(prev);
   2202 	}
   2203 
   2204 #ifdef HOSTAPD
   2205 	/* There should be only one BSS remaining at this point. */
   2206 	if (dl_list_len(&drv->bss) != 1)
   2207 		wpa_printf(MSG_ERROR, "%s: %u remaining BSS entries",
   2208 			   __func__, dl_list_len(&drv->bss));
   2209 #endif /* HOSTAPD */
   2210 
   2211 	test_driver_free_bsses(drv);
   2212 
   2213 	wpa_driver_test_close_test_socket(drv);
   2214 	eloop_cancel_timeout(wpa_driver_test_scan_timeout, drv, drv->ctx);
   2215 	eloop_cancel_timeout(wpa_driver_test_poll, drv, NULL);
   2216 	eloop_cancel_timeout(test_remain_on_channel_timeout, drv, NULL);
   2217 	os_free(drv->test_dir);
   2218 	for (i = 0; i < MAX_SCAN_RESULTS; i++)
   2219 		os_free(drv->scanres[i]);
   2220 	os_free(drv->probe_req_ie);
   2221 	wpa_trace_remove_ref(drv, ctx, drv->ctx);
   2222 	os_free(drv);
   2223 }
   2224 
   2225 
   2226 static int wpa_driver_test_attach(struct wpa_driver_test_data *drv,
   2227 				  const char *dir, int ap)
   2228 {
   2229 #ifdef DRIVER_TEST_UNIX
   2230 	static unsigned int counter = 0;
   2231 	struct sockaddr_un addr;
   2232 	size_t len;
   2233 
   2234 	os_free(drv->own_socket_path);
   2235 	if (dir) {
   2236 		len = os_strlen(dir) + 30;
   2237 		drv->own_socket_path = os_malloc(len);
   2238 		if (drv->own_socket_path == NULL)
   2239 			return -1;
   2240 		os_snprintf(drv->own_socket_path, len, "%s/%s-" MACSTR,
   2241 			    dir, ap ? "AP" : "STA", MAC2STR(drv->own_addr));
   2242 	} else {
   2243 		drv->own_socket_path = os_malloc(100);
   2244 		if (drv->own_socket_path == NULL)
   2245 			return -1;
   2246 		os_snprintf(drv->own_socket_path, 100,
   2247 			    "/tmp/wpa_supplicant_test-%d-%d",
   2248 			    getpid(), counter++);
   2249 	}
   2250 
   2251 	drv->test_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
   2252 	if (drv->test_socket < 0) {
   2253 		perror("socket(PF_UNIX)");
   2254 		os_free(drv->own_socket_path);
   2255 		drv->own_socket_path = NULL;
   2256 		return -1;
   2257 	}
   2258 
   2259 	os_memset(&addr, 0, sizeof(addr));
   2260 	addr.sun_family = AF_UNIX;
   2261 	os_strlcpy(addr.sun_path, drv->own_socket_path, sizeof(addr.sun_path));
   2262 	if (bind(drv->test_socket, (struct sockaddr *) &addr,
   2263 		 sizeof(addr)) < 0) {
   2264 		perror("bind(PF_UNIX)");
   2265 		close(drv->test_socket);
   2266 		unlink(drv->own_socket_path);
   2267 		os_free(drv->own_socket_path);
   2268 		drv->own_socket_path = NULL;
   2269 		return -1;
   2270 	}
   2271 
   2272 	eloop_register_read_sock(drv->test_socket,
   2273 				 wpa_driver_test_receive_unix, drv, NULL);
   2274 
   2275 	return 0;
   2276 #else /* DRIVER_TEST_UNIX */
   2277 	return -1;
   2278 #endif /* DRIVER_TEST_UNIX */
   2279 }
   2280 
   2281 
   2282 static int wpa_driver_test_attach_udp(struct wpa_driver_test_data *drv,
   2283 				      char *dst)
   2284 {
   2285 	char *pos;
   2286 
   2287 	pos = os_strchr(dst, ':');
   2288 	if (pos == NULL)
   2289 		return -1;
   2290 	*pos++ = '\0';
   2291 	wpa_printf(MSG_DEBUG, "%s: addr=%s port=%s", __func__, dst, pos);
   2292 
   2293 	drv->test_socket = socket(PF_INET, SOCK_DGRAM, 0);
   2294 	if (drv->test_socket < 0) {
   2295 		perror("socket(PF_INET)");
   2296 		return -1;
   2297 	}
   2298 
   2299 	os_memset(&drv->hostapd_addr_udp, 0, sizeof(drv->hostapd_addr_udp));
   2300 	drv->hostapd_addr_udp.sin_family = AF_INET;
   2301 #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
   2302 	{
   2303 		int a[4];
   2304 		u8 *pos;
   2305 		sscanf(dst, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
   2306 		pos = (u8 *) &drv->hostapd_addr_udp.sin_addr;
   2307 		*pos++ = a[0];
   2308 		*pos++ = a[1];
   2309 		*pos++ = a[2];
   2310 		*pos++ = a[3];
   2311 	}
   2312 #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
   2313 	inet_aton(dst, &drv->hostapd_addr_udp.sin_addr);
   2314 #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
   2315 	drv->hostapd_addr_udp.sin_port = htons(atoi(pos));
   2316 
   2317 	drv->hostapd_addr_udp_set = 1;
   2318 
   2319 	eloop_register_read_sock(drv->test_socket,
   2320 				 wpa_driver_test_receive_unix, drv, NULL);
   2321 
   2322 	return 0;
   2323 }
   2324 
   2325 
   2326 static int wpa_driver_test_set_param(void *priv, const char *param)
   2327 {
   2328 	struct test_driver_bss *dbss = priv;
   2329 	struct wpa_driver_test_data *drv = dbss->drv;
   2330 	const char *pos;
   2331 
   2332 	wpa_printf(MSG_DEBUG, "%s: param='%s'", __func__, param);
   2333 	if (param == NULL)
   2334 		return 0;
   2335 
   2336 	wpa_driver_test_close_test_socket(drv);
   2337 
   2338 #ifdef DRIVER_TEST_UNIX
   2339 	pos = os_strstr(param, "test_socket=");
   2340 	if (pos) {
   2341 		const char *pos2;
   2342 		size_t len;
   2343 
   2344 		pos += 12;
   2345 		pos2 = os_strchr(pos, ' ');
   2346 		if (pos2)
   2347 			len = pos2 - pos;
   2348 		else
   2349 			len = os_strlen(pos);
   2350 		if (len > sizeof(drv->hostapd_addr.sun_path))
   2351 			return -1;
   2352 		os_memset(&drv->hostapd_addr, 0, sizeof(drv->hostapd_addr));
   2353 		drv->hostapd_addr.sun_family = AF_UNIX;
   2354 		os_memcpy(drv->hostapd_addr.sun_path, pos, len);
   2355 		drv->hostapd_addr_set = 1;
   2356 	}
   2357 #endif /* DRIVER_TEST_UNIX */
   2358 
   2359 	pos = os_strstr(param, "test_dir=");
   2360 	if (pos) {
   2361 		char *end;
   2362 		os_free(drv->test_dir);
   2363 		drv->test_dir = os_strdup(pos + 9);
   2364 		if (drv->test_dir == NULL)
   2365 			return -1;
   2366 		end = os_strchr(drv->test_dir, ' ');
   2367 		if (end)
   2368 			*end = '\0';
   2369 		if (wpa_driver_test_attach(drv, drv->test_dir, 0))
   2370 			return -1;
   2371 	} else {
   2372 		pos = os_strstr(param, "test_udp=");
   2373 		if (pos) {
   2374 			char *dst, *epos;
   2375 			dst = os_strdup(pos + 9);
   2376 			if (dst == NULL)
   2377 				return -1;
   2378 			epos = os_strchr(dst, ' ');
   2379 			if (epos)
   2380 				*epos = '\0';
   2381 			if (wpa_driver_test_attach_udp(drv, dst))
   2382 				return -1;
   2383 			os_free(dst);
   2384 		} else if (wpa_driver_test_attach(drv, NULL, 0))
   2385 			return -1;
   2386 	}
   2387 
   2388 	if (os_strstr(param, "use_associnfo=1")) {
   2389 		wpa_printf(MSG_DEBUG, "test_driver: Use AssocInfo events");
   2390 		drv->use_associnfo = 1;
   2391 	}
   2392 
   2393 #ifdef CONFIG_CLIENT_MLME
   2394 	if (os_strstr(param, "use_mlme=1")) {
   2395 		wpa_printf(MSG_DEBUG, "test_driver: Use internal MLME");
   2396 		drv->use_mlme = 1;
   2397 	}
   2398 #endif /* CONFIG_CLIENT_MLME */
   2399 
   2400 	if (os_strstr(param, "p2p_mgmt=1")) {
   2401 		wpa_printf(MSG_DEBUG, "test_driver: Use internal P2P "
   2402 			   "management");
   2403 		if (wpa_driver_test_init_p2p(drv) < 0)
   2404 			return -1;
   2405 	}
   2406 
   2407 	return 0;
   2408 }
   2409 
   2410 
   2411 static const u8 * wpa_driver_test_get_mac_addr(void *priv)
   2412 {
   2413 	struct test_driver_bss *dbss = priv;
   2414 	struct wpa_driver_test_data *drv = dbss->drv;
   2415 	wpa_printf(MSG_DEBUG, "%s", __func__);
   2416 	return drv->own_addr;
   2417 }
   2418 
   2419 
   2420 static int wpa_driver_test_send_eapol(void *priv, const u8 *dest, u16 proto,
   2421 				      const u8 *data, size_t data_len)
   2422 {
   2423 	struct test_driver_bss *dbss = priv;
   2424 	struct wpa_driver_test_data *drv = dbss->drv;
   2425 	char *msg;
   2426 	size_t msg_len;
   2427 	struct l2_ethhdr eth;
   2428 	struct sockaddr *addr;
   2429 	socklen_t alen;
   2430 #ifdef DRIVER_TEST_UNIX
   2431 	struct sockaddr_un addr_un;
   2432 #endif /* DRIVER_TEST_UNIX */
   2433 
   2434 	wpa_hexdump(MSG_MSGDUMP, "test_send_eapol TX frame", data, data_len);
   2435 
   2436 	os_memset(&eth, 0, sizeof(eth));
   2437 	os_memcpy(eth.h_dest, dest, ETH_ALEN);
   2438 	os_memcpy(eth.h_source, drv->own_addr, ETH_ALEN);
   2439 	eth.h_proto = host_to_be16(proto);
   2440 
   2441 	msg_len = 6 + sizeof(eth) + data_len;
   2442 	msg = os_malloc(msg_len);
   2443 	if (msg == NULL)
   2444 		return -1;
   2445 	os_memcpy(msg, "EAPOL ", 6);
   2446 	os_memcpy(msg + 6, &eth, sizeof(eth));
   2447 	os_memcpy(msg + 6 + sizeof(eth), data, data_len);
   2448 
   2449 	if (os_memcmp(dest, dbss->bssid, ETH_ALEN) == 0 ||
   2450 	    drv->test_dir == NULL) {
   2451 		if (drv->hostapd_addr_udp_set) {
   2452 			addr = (struct sockaddr *) &drv->hostapd_addr_udp;
   2453 			alen = sizeof(drv->hostapd_addr_udp);
   2454 		} else {
   2455 #ifdef DRIVER_TEST_UNIX
   2456 			addr = (struct sockaddr *) &drv->hostapd_addr;
   2457 			alen = sizeof(drv->hostapd_addr);
   2458 #else /* DRIVER_TEST_UNIX */
   2459 			os_free(msg);
   2460 			return -1;
   2461 #endif /* DRIVER_TEST_UNIX */
   2462 		}
   2463 	} else {
   2464 #ifdef DRIVER_TEST_UNIX
   2465 		struct stat st;
   2466 		os_memset(&addr_un, 0, sizeof(addr_un));
   2467 		addr_un.sun_family = AF_UNIX;
   2468 		os_snprintf(addr_un.sun_path, sizeof(addr_un.sun_path),
   2469 			    "%s/STA-" MACSTR, drv->test_dir, MAC2STR(dest));
   2470 		if (stat(addr_un.sun_path, &st) < 0) {
   2471 			os_snprintf(addr_un.sun_path, sizeof(addr_un.sun_path),
   2472 				    "%s/AP-" MACSTR,
   2473 				    drv->test_dir, MAC2STR(dest));
   2474 		}
   2475 		addr = (struct sockaddr *) &addr_un;
   2476 		alen = sizeof(addr_un);
   2477 #else /* DRIVER_TEST_UNIX */
   2478 		os_free(msg);
   2479 		return -1;
   2480 #endif /* DRIVER_TEST_UNIX */
   2481 	}
   2482 
   2483 	if (sendto(drv->test_socket, msg, msg_len, 0, addr, alen) < 0) {
   2484 		perror("sendmsg(test_socket)");
   2485 		os_free(msg);
   2486 		return -1;
   2487 	}
   2488 
   2489 	os_free(msg);
   2490 	return 0;
   2491 }
   2492 
   2493 
   2494 static int wpa_driver_test_get_capa(void *priv, struct wpa_driver_capa *capa)
   2495 {
   2496 	struct test_driver_bss *dbss = priv;
   2497 	struct wpa_driver_test_data *drv = dbss->drv;
   2498 	os_memset(capa, 0, sizeof(*capa));
   2499 	capa->key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
   2500 		WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
   2501 		WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
   2502 		WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK |
   2503 		WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE |
   2504 		WPA_DRIVER_CAPA_KEY_MGMT_FT |
   2505 		WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
   2506 	capa->enc = WPA_DRIVER_CAPA_ENC_WEP40 |
   2507 		WPA_DRIVER_CAPA_ENC_WEP104 |
   2508 		WPA_DRIVER_CAPA_ENC_TKIP |
   2509 		WPA_DRIVER_CAPA_ENC_CCMP;
   2510 	capa->auth = WPA_DRIVER_AUTH_OPEN |
   2511 		WPA_DRIVER_AUTH_SHARED |
   2512 		WPA_DRIVER_AUTH_LEAP;
   2513 	if (drv->use_mlme)
   2514 		capa->flags |= WPA_DRIVER_FLAGS_USER_SPACE_MLME;
   2515 	if (drv->p2p)
   2516 		capa->flags |= WPA_DRIVER_FLAGS_P2P_MGMT;
   2517 	capa->flags |= WPA_DRIVER_FLAGS_AP;
   2518 	capa->flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
   2519 	capa->flags |= WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE;
   2520 	capa->flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
   2521 	capa->max_scan_ssids = 2;
   2522 	capa->max_remain_on_chan = 60000;
   2523 
   2524 	return 0;
   2525 }
   2526 
   2527 
   2528 static int wpa_driver_test_mlme_setprotection(void *priv, const u8 *addr,
   2529 					      int protect_type,
   2530 					      int key_type)
   2531 {
   2532 	wpa_printf(MSG_DEBUG, "%s: protect_type=%d key_type=%d",
   2533 		   __func__, protect_type, key_type);
   2534 
   2535 	if (addr) {
   2536 		wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR,
   2537 			   __func__, MAC2STR(addr));
   2538 	}
   2539 
   2540 	return 0;
   2541 }
   2542 
   2543 
   2544 static int wpa_driver_test_set_channel(void *priv,
   2545 				       enum hostapd_hw_mode phymode,
   2546 				       int chan, int freq)
   2547 {
   2548 	struct test_driver_bss *dbss = priv;
   2549 	struct wpa_driver_test_data *drv = dbss->drv;
   2550 	wpa_printf(MSG_DEBUG, "%s: phymode=%d chan=%d freq=%d",
   2551 		   __func__, phymode, chan, freq);
   2552 	drv->current_freq = freq;
   2553 	return 0;
   2554 }
   2555 
   2556 
   2557 static int wpa_driver_test_mlme_add_sta(void *priv, const u8 *addr,
   2558 					const u8 *supp_rates,
   2559 					size_t supp_rates_len)
   2560 {
   2561 	wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, MAC2STR(addr));
   2562 	return 0;
   2563 }
   2564 
   2565 
   2566 static int wpa_driver_test_mlme_remove_sta(void *priv, const u8 *addr)
   2567 {
   2568 	wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, MAC2STR(addr));
   2569 	return 0;
   2570 }
   2571 
   2572 
   2573 static int wpa_driver_test_set_ssid(void *priv, const u8 *ssid,
   2574 				    size_t ssid_len)
   2575 {
   2576 	wpa_printf(MSG_DEBUG, "%s", __func__);
   2577 	return 0;
   2578 }
   2579 
   2580 
   2581 static int wpa_driver_test_set_bssid(void *priv, const u8 *bssid)
   2582 {
   2583 	wpa_printf(MSG_DEBUG, "%s: bssid=" MACSTR, __func__, MAC2STR(bssid));
   2584 	return 0;
   2585 }
   2586 
   2587 
   2588 static void * wpa_driver_test_global_init(void)
   2589 {
   2590 	struct wpa_driver_test_global *global;
   2591 
   2592 	global = os_zalloc(sizeof(*global));
   2593 	return global;
   2594 }
   2595 
   2596 
   2597 static void wpa_driver_test_global_deinit(void *priv)
   2598 {
   2599 	struct wpa_driver_test_global *global = priv;
   2600 	os_free(global);
   2601 }
   2602 
   2603 
   2604 static struct wpa_interface_info *
   2605 wpa_driver_test_get_interfaces(void *global_priv)
   2606 {
   2607 	/* struct wpa_driver_test_global *global = priv; */
   2608 	struct wpa_interface_info *iface;
   2609 
   2610 	iface = os_zalloc(sizeof(*iface));
   2611 	if (iface == NULL)
   2612 		return iface;
   2613 	iface->ifname = os_strdup("sta0");
   2614 	iface->desc = os_strdup("test interface 0");
   2615 	iface->drv_name = "test";
   2616 	iface->next = os_zalloc(sizeof(*iface));
   2617 	if (iface->next) {
   2618 		iface->next->ifname = os_strdup("sta1");
   2619 		iface->next->desc = os_strdup("test interface 1");
   2620 		iface->next->drv_name = "test";
   2621 	}
   2622 
   2623 	return iface;
   2624 }
   2625 
   2626 
   2627 static struct hostapd_hw_modes *
   2628 wpa_driver_test_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
   2629 {
   2630 	struct hostapd_hw_modes *modes;
   2631 	size_t i;
   2632 
   2633 	*num_modes = 3;
   2634 	*flags = 0;
   2635 	modes = os_zalloc(*num_modes * sizeof(struct hostapd_hw_modes));
   2636 	if (modes == NULL)
   2637 		return NULL;
   2638 	modes[0].mode = HOSTAPD_MODE_IEEE80211G;
   2639 	modes[0].num_channels = 11;
   2640 	modes[0].num_rates = 12;
   2641 	modes[0].channels =
   2642 		os_zalloc(11 * sizeof(struct hostapd_channel_data));
   2643 	modes[0].rates = os_zalloc(modes[0].num_rates * sizeof(int));
   2644 	if (modes[0].channels == NULL || modes[0].rates == NULL)
   2645 		goto fail;
   2646 	for (i = 0; i < 11; i++) {
   2647 		modes[0].channels[i].chan = i + 1;
   2648 		modes[0].channels[i].freq = 2412 + 5 * i;
   2649 		modes[0].channels[i].flag = 0;
   2650 	}
   2651 	modes[0].rates[0] = 10;
   2652 	modes[0].rates[1] = 20;
   2653 	modes[0].rates[2] = 55;
   2654 	modes[0].rates[3] = 110;
   2655 	modes[0].rates[4] = 60;
   2656 	modes[0].rates[5] = 90;
   2657 	modes[0].rates[6] = 120;
   2658 	modes[0].rates[7] = 180;
   2659 	modes[0].rates[8] = 240;
   2660 	modes[0].rates[9] = 360;
   2661 	modes[0].rates[10] = 480;
   2662 	modes[0].rates[11] = 540;
   2663 
   2664 	modes[1].mode = HOSTAPD_MODE_IEEE80211B;
   2665 	modes[1].num_channels = 11;
   2666 	modes[1].num_rates = 4;
   2667 	modes[1].channels =
   2668 		os_zalloc(11 * sizeof(struct hostapd_channel_data));
   2669 	modes[1].rates = os_zalloc(modes[1].num_rates * sizeof(int));
   2670 	if (modes[1].channels == NULL || modes[1].rates == NULL)
   2671 		goto fail;
   2672 	for (i = 0; i < 11; i++) {
   2673 		modes[1].channels[i].chan = i + 1;
   2674 		modes[1].channels[i].freq = 2412 + 5 * i;
   2675 		modes[1].channels[i].flag = 0;
   2676 	}
   2677 	modes[1].rates[0] = 10;
   2678 	modes[1].rates[1] = 20;
   2679 	modes[1].rates[2] = 55;
   2680 	modes[1].rates[3] = 110;
   2681 
   2682 	modes[2].mode = HOSTAPD_MODE_IEEE80211A;
   2683 	modes[2].num_channels = 1;
   2684 	modes[2].num_rates = 8;
   2685 	modes[2].channels = os_zalloc(sizeof(struct hostapd_channel_data));
   2686 	modes[2].rates = os_zalloc(modes[2].num_rates * sizeof(int));
   2687 	if (modes[2].channels == NULL || modes[2].rates == NULL)
   2688 		goto fail;
   2689 	modes[2].channels[0].chan = 60;
   2690 	modes[2].channels[0].freq = 5300;
   2691 	modes[2].channels[0].flag = 0;
   2692 	modes[2].rates[0] = 60;
   2693 	modes[2].rates[1] = 90;
   2694 	modes[2].rates[2] = 120;
   2695 	modes[2].rates[3] = 180;
   2696 	modes[2].rates[4] = 240;
   2697 	modes[2].rates[5] = 360;
   2698 	modes[2].rates[6] = 480;
   2699 	modes[2].rates[7] = 540;
   2700 
   2701 	return modes;
   2702 
   2703 fail:
   2704 	if (modes) {
   2705 		for (i = 0; i < *num_modes; i++) {
   2706 			os_free(modes[i].channels);
   2707 			os_free(modes[i].rates);
   2708 		}
   2709 		os_free(modes);
   2710 	}
   2711 	return NULL;
   2712 }
   2713 
   2714 
   2715 static int wpa_driver_test_set_freq(void *priv,
   2716 				    struct hostapd_freq_params *freq)
   2717 {
   2718 	struct test_driver_bss *dbss = priv;
   2719 	struct wpa_driver_test_data *drv = dbss->drv;
   2720 	wpa_printf(MSG_DEBUG, "test: set_freq %u MHz", freq->freq);
   2721 	drv->current_freq = freq->freq;
   2722 	return 0;
   2723 }
   2724 
   2725 
   2726 static int wpa_driver_test_send_action(void *priv, unsigned int freq,
   2727 				       unsigned int wait,
   2728 				       const u8 *dst, const u8 *src,
   2729 				       const u8 *bssid,
   2730 				       const u8 *data, size_t data_len)
   2731 {
   2732 	struct test_driver_bss *dbss = priv;
   2733 	struct wpa_driver_test_data *drv = dbss->drv;
   2734 	int ret = -1;
   2735 	u8 *buf;
   2736 	struct ieee80211_hdr *hdr;
   2737 
   2738 	wpa_printf(MSG_DEBUG, "test: Send Action frame");
   2739 
   2740 	if ((drv->remain_on_channel_freq &&
   2741 	     freq != drv->remain_on_channel_freq) ||
   2742 	    (drv->remain_on_channel_freq == 0 &&
   2743 	     freq != (unsigned int) drv->current_freq)) {
   2744 		wpa_printf(MSG_DEBUG, "test: Reject Action frame TX on "
   2745 			   "unexpected channel: freq=%u MHz (current_freq=%u "
   2746 			   "MHz, remain-on-channel freq=%u MHz)",
   2747 			   freq, drv->current_freq,
   2748 			   drv->remain_on_channel_freq);
   2749 		return -1;
   2750 	}
   2751 
   2752 	buf = os_zalloc(24 + data_len);
   2753 	if (buf == NULL)
   2754 		return ret;
   2755 	os_memcpy(buf + 24, data, data_len);
   2756 	hdr = (struct ieee80211_hdr *) buf;
   2757 	hdr->frame_control =
   2758 		IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
   2759 	os_memcpy(hdr->addr1, dst, ETH_ALEN);
   2760 	os_memcpy(hdr->addr2, src, ETH_ALEN);
   2761 	os_memcpy(hdr->addr3, bssid, ETH_ALEN);
   2762 
   2763 	ret = wpa_driver_test_send_mlme(priv, buf, 24 + data_len);
   2764 	os_free(buf);
   2765 	return ret;
   2766 }
   2767 
   2768 
   2769 #ifdef CONFIG_P2P
   2770 static void test_send_action_cb(void *eloop_ctx, void *timeout_ctx)
   2771 {
   2772 	struct wpa_driver_test_data *drv = eloop_ctx;
   2773 	int res;
   2774 
   2775 	if (drv->pending_action_tx == NULL)
   2776 		return;
   2777 
   2778 	if (drv->off_channel_freq != drv->pending_action_freq) {
   2779 		wpa_printf(MSG_DEBUG, "P2P: Pending Action frame TX "
   2780 			   "waiting for another freq=%u",
   2781 			   drv->pending_action_freq);
   2782 		return;
   2783 	}
   2784 	wpa_printf(MSG_DEBUG, "P2P: Sending pending Action frame to "
   2785 		   MACSTR, MAC2STR(drv->pending_action_dst));
   2786 	res = wpa_driver_test_send_action(drv, drv->pending_action_freq, 0,
   2787 					  drv->pending_action_dst,
   2788 					  drv->pending_action_src,
   2789 					  drv->pending_action_bssid,
   2790 					  wpabuf_head(drv->pending_action_tx),
   2791 					  wpabuf_len(drv->pending_action_tx));
   2792 }
   2793 #endif /* CONFIG_P2P */
   2794 
   2795 
   2796 static void test_remain_on_channel_timeout(void *eloop_ctx, void *timeout_ctx)
   2797 {
   2798 	struct wpa_driver_test_data *drv = eloop_ctx;
   2799 	union wpa_event_data data;
   2800 
   2801 	wpa_printf(MSG_DEBUG, "test: Remain-on-channel timeout");
   2802 
   2803 	os_memset(&data, 0, sizeof(data));
   2804 	data.remain_on_channel.freq = drv->remain_on_channel_freq;
   2805 	data.remain_on_channel.duration = drv->remain_on_channel_duration;
   2806 
   2807 	if (drv->p2p)
   2808 		drv->off_channel_freq = 0;
   2809 
   2810 	drv->remain_on_channel_freq = 0;
   2811 
   2812 	wpa_supplicant_event(drv->ctx, EVENT_CANCEL_REMAIN_ON_CHANNEL, &data);
   2813 }
   2814 
   2815 
   2816 static int wpa_driver_test_remain_on_channel(void *priv, unsigned int freq,
   2817 					     unsigned int duration)
   2818 {
   2819 	struct test_driver_bss *dbss = priv;
   2820 	struct wpa_driver_test_data *drv = dbss->drv;
   2821 	union wpa_event_data data;
   2822 
   2823 	wpa_printf(MSG_DEBUG, "%s(freq=%u, duration=%u)",
   2824 		   __func__, freq, duration);
   2825 	if (drv->remain_on_channel_freq &&
   2826 	    drv->remain_on_channel_freq != freq) {
   2827 		wpa_printf(MSG_DEBUG, "test: Refuse concurrent "
   2828 			   "remain_on_channel request");
   2829 		return -1;
   2830 	}
   2831 
   2832 	drv->remain_on_channel_freq = freq;
   2833 	drv->remain_on_channel_duration = duration;
   2834 	eloop_cancel_timeout(test_remain_on_channel_timeout, drv, NULL);
   2835 	eloop_register_timeout(duration / 1000, (duration % 1000) * 1000,
   2836 			       test_remain_on_channel_timeout, drv, NULL);
   2837 
   2838 	os_memset(&data, 0, sizeof(data));
   2839 	data.remain_on_channel.freq = freq;
   2840 	data.remain_on_channel.duration = duration;
   2841 	wpa_supplicant_event(drv->ctx, EVENT_REMAIN_ON_CHANNEL, &data);
   2842 
   2843 #ifdef CONFIG_P2P
   2844 	if (drv->p2p) {
   2845 		drv->off_channel_freq = drv->remain_on_channel_freq;
   2846 		test_send_action_cb(drv, NULL);
   2847 		if (drv->off_channel_freq == drv->pending_listen_freq) {
   2848 			p2p_listen_cb(drv->p2p, drv->pending_listen_freq,
   2849 				      drv->pending_listen_duration);
   2850 			drv->pending_listen_freq = 0;
   2851 		}
   2852 	}
   2853 #endif /* CONFIG_P2P */
   2854 
   2855 	return 0;
   2856 }
   2857 
   2858 
   2859 static int wpa_driver_test_cancel_remain_on_channel(void *priv)
   2860 {
   2861 	struct test_driver_bss *dbss = priv;
   2862 	struct wpa_driver_test_data *drv = dbss->drv;
   2863 	wpa_printf(MSG_DEBUG, "%s", __func__);
   2864 	if (!drv->remain_on_channel_freq)
   2865 		return -1;
   2866 	drv->remain_on_channel_freq = 0;
   2867 	eloop_cancel_timeout(test_remain_on_channel_timeout, drv, NULL);
   2868 	return 0;
   2869 }
   2870 
   2871 
   2872 static int wpa_driver_test_probe_req_report(void *priv, int report)
   2873 {
   2874 	struct test_driver_bss *dbss = priv;
   2875 	struct wpa_driver_test_data *drv = dbss->drv;
   2876 	wpa_printf(MSG_DEBUG, "%s(report=%d)", __func__, report);
   2877 	drv->probe_req_report = report;
   2878 	return 0;
   2879 }
   2880 
   2881 
   2882 #ifdef CONFIG_P2P
   2883 
   2884 static int wpa_driver_test_p2p_find(void *priv, unsigned int timeout, int type)
   2885 {
   2886 	struct wpa_driver_test_data *drv = priv;
   2887 	wpa_printf(MSG_DEBUG, "%s(timeout=%u)", __func__, timeout);
   2888 	if (!drv->p2p)
   2889 		return -1;
   2890 	return p2p_find(drv->p2p, timeout, type, 0, NULL);
   2891 }
   2892 
   2893 
   2894 static int wpa_driver_test_p2p_stop_find(void *priv)
   2895 {
   2896 	struct wpa_driver_test_data *drv = priv;
   2897 	wpa_printf(MSG_DEBUG, "%s", __func__);
   2898 	if (!drv->p2p)
   2899 		return -1;
   2900 	p2p_stop_find(drv->p2p);
   2901 	return 0;
   2902 }
   2903 
   2904 
   2905 static int wpa_driver_test_p2p_listen(void *priv, unsigned int timeout)
   2906 {
   2907 	struct wpa_driver_test_data *drv = priv;
   2908 	wpa_printf(MSG_DEBUG, "%s(timeout=%u)", __func__, timeout);
   2909 	if (!drv->p2p)
   2910 		return -1;
   2911 	return p2p_listen(drv->p2p, timeout);
   2912 }
   2913 
   2914 
   2915 static int wpa_driver_test_p2p_connect(void *priv, const u8 *peer_addr,
   2916 				       int wps_method, int go_intent,
   2917 				       const u8 *own_interface_addr,
   2918 				       unsigned int force_freq,
   2919 				       int persistent_group)
   2920 {
   2921 	struct wpa_driver_test_data *drv = priv;
   2922 	wpa_printf(MSG_DEBUG, "%s(peer_addr=" MACSTR " wps_method=%d "
   2923 		   "go_intent=%d "
   2924 		   "own_interface_addr=" MACSTR " force_freq=%u "
   2925 		   "persistent_group=%d)",
   2926 		   __func__, MAC2STR(peer_addr), wps_method, go_intent,
   2927 		   MAC2STR(own_interface_addr), force_freq, persistent_group);
   2928 	if (!drv->p2p)
   2929 		return -1;
   2930 	return p2p_connect(drv->p2p, peer_addr, wps_method, go_intent,
   2931 			   own_interface_addr, force_freq, persistent_group);
   2932 }
   2933 
   2934 
   2935 static int wpa_driver_test_wps_success_cb(void *priv, const u8 *peer_addr)
   2936 {
   2937 	struct wpa_driver_test_data *drv = priv;
   2938 	wpa_printf(MSG_DEBUG, "%s(peer_addr=" MACSTR ")",
   2939 		   __func__, MAC2STR(peer_addr));
   2940 	if (!drv->p2p)
   2941 		return -1;
   2942 	p2p_wps_success_cb(drv->p2p, peer_addr);
   2943 	return 0;
   2944 }
   2945 
   2946 
   2947 static int wpa_driver_test_p2p_group_formation_failed(void *priv)
   2948 {
   2949 	struct wpa_driver_test_data *drv = priv;
   2950 	wpa_printf(MSG_DEBUG, "%s", __func__);
   2951 	if (!drv->p2p)
   2952 		return -1;
   2953 	p2p_group_formation_failed(drv->p2p);
   2954 	return 0;
   2955 }
   2956 
   2957 
   2958 static int wpa_driver_test_p2p_set_params(void *priv,
   2959 					  const struct p2p_params *params)
   2960 {
   2961 	struct wpa_driver_test_data *drv = priv;
   2962 	wpa_printf(MSG_DEBUG, "%s", __func__);
   2963 	if (!drv->p2p)
   2964 		return -1;
   2965 	if (p2p_set_dev_name(drv->p2p, params->dev_name) < 0 ||
   2966 	    p2p_set_pri_dev_type(drv->p2p, params->pri_dev_type) < 0 ||
   2967 	    p2p_set_sec_dev_types(drv->p2p, params->sec_dev_type,
   2968 				  params->num_sec_dev_types) < 0)
   2969 		return -1;
   2970 	return 0;
   2971 }
   2972 
   2973 
   2974 static int test_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
   2975 			 unsigned int num_req_dev_types,
   2976 			 const u8 *req_dev_types)
   2977 {
   2978 	struct wpa_driver_test_data *drv = ctx;
   2979 	struct wpa_driver_scan_params params;
   2980 	int ret;
   2981 	struct wpabuf *wps_ie, *ies;
   2982 	int social_channels[] = { 2412, 2437, 2462, 0, 0 };
   2983 
   2984 	wpa_printf(MSG_DEBUG, "%s(type=%d freq=%d)",
   2985 		   __func__, type, freq);
   2986 
   2987 	os_memset(&params, 0, sizeof(params));
   2988 
   2989 	/* P2P Wildcard SSID */
   2990 	params.num_ssids = 1;
   2991 	params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
   2992 	params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
   2993 
   2994 #if 0 /* TODO: WPS IE */
   2995 	wpa_s->wps->dev.p2p = 1;
   2996 	wps_ie = wps_build_probe_req_ie(0, &wpa_s->wps->dev, wpa_s->wps->uuid,
   2997 					WPS_REQ_ENROLLEE);
   2998 #else
   2999 	wps_ie = wpabuf_alloc(1);
   3000 #endif
   3001 	if (wps_ie == NULL)
   3002 		return -1;
   3003 
   3004 	ies = wpabuf_alloc(wpabuf_len(wps_ie) + 100);
   3005 	if (ies == NULL) {
   3006 		wpabuf_free(wps_ie);
   3007 		return -1;
   3008 	}
   3009 	wpabuf_put_buf(ies, wps_ie);
   3010 	wpabuf_free(wps_ie);
   3011 
   3012 	p2p_scan_ie(drv->p2p, ies);
   3013 
   3014 	params.extra_ies = wpabuf_head(ies);
   3015 	params.extra_ies_len = wpabuf_len(ies);
   3016 
   3017 	switch (type) {
   3018 	case P2P_SCAN_SOCIAL:
   3019 		params.freqs = social_channels;
   3020 		break;
   3021 	case P2P_SCAN_FULL:
   3022 		break;
   3023 	case P2P_SCAN_SPECIFIC:
   3024 		social_channels[0] = freq;
   3025 		social_channels[1] = 0;
   3026 		params.freqs = social_channels;
   3027 		break;
   3028 	case P2P_SCAN_SOCIAL_PLUS_ONE:
   3029 		social_channels[3] = freq;
   3030 		params.freqs = social_channels;
   3031 		break;
   3032 	}
   3033 
   3034 	drv->pending_p2p_scan = 1;
   3035 	ret = wpa_driver_test_scan(drv, &params);
   3036 
   3037 	wpabuf_free(ies);
   3038 
   3039 	return ret;
   3040 }
   3041 
   3042 
   3043 static int test_send_action(void *ctx, unsigned int freq, const u8 *dst,
   3044 			    const u8 *src, const u8 *bssid, const u8 *buf,
   3045 			    size_t len, unsigned int wait_time)
   3046 {
   3047 	struct wpa_driver_test_data *drv = ctx;
   3048 
   3049 	wpa_printf(MSG_DEBUG, "%s(freq=%u dst=" MACSTR " src=" MACSTR
   3050 		   " bssid=" MACSTR " len=%d",
   3051 		   __func__, freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
   3052 		   (int) len);
   3053 	if (freq <= 0) {
   3054 		wpa_printf(MSG_WARNING, "P2P: No frequency specified for "
   3055 			   "action frame TX");
   3056 		return -1;
   3057 	}
   3058 
   3059 	if (drv->pending_action_tx) {
   3060 		wpa_printf(MSG_DEBUG, "P2P: Dropped pending Action frame TX "
   3061 			   "to " MACSTR, MAC2STR(drv->pending_action_dst));
   3062 		wpabuf_free(drv->pending_action_tx);
   3063 	}
   3064 	drv->pending_action_tx = wpabuf_alloc(len);
   3065 	if (drv->pending_action_tx == NULL)
   3066 		return -1;
   3067 	wpabuf_put_data(drv->pending_action_tx, buf, len);
   3068 	os_memcpy(drv->pending_action_src, src, ETH_ALEN);
   3069 	os_memcpy(drv->pending_action_dst, dst, ETH_ALEN);
   3070 	os_memcpy(drv->pending_action_bssid, bssid, ETH_ALEN);
   3071 	drv->pending_action_freq = freq;
   3072 
   3073 	if (drv->off_channel_freq == freq) {
   3074 		/* Already on requested channel; send immediately */
   3075 		/* TODO: Would there ever be need to extend the current
   3076 		 * duration on the channel? */
   3077 		eloop_cancel_timeout(test_send_action_cb, drv, NULL);
   3078 		eloop_register_timeout(0, 0, test_send_action_cb, drv, NULL);
   3079 		return 0;
   3080 	}
   3081 
   3082 	wpa_printf(MSG_DEBUG, "P2P: Schedule Action frame to be transmitted "
   3083 		   "once the driver gets to the requested channel");
   3084 	if (wpa_driver_test_remain_on_channel(drv, freq, wait_time) < 0) {
   3085 		wpa_printf(MSG_DEBUG, "P2P: Failed to request driver "
   3086 			   "to remain on channel (%u MHz) for Action "
   3087 			   "Frame TX", freq);
   3088 		return -1;
   3089 	}
   3090 
   3091 	return 0;
   3092 }
   3093 
   3094 
   3095 static void test_send_action_done(void *ctx)
   3096 {
   3097 	wpa_printf(MSG_DEBUG, "%s", __func__);
   3098 	/* TODO */
   3099 }
   3100 
   3101 
   3102 static void test_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
   3103 {
   3104 	struct wpa_driver_test_data *drv = ctx;
   3105 	union wpa_event_data event;
   3106 	wpa_printf(MSG_DEBUG, "%s", __func__);
   3107 	os_memset(&event, 0, sizeof(event));
   3108 	event.p2p_go_neg_completed.res = res;
   3109 	wpa_supplicant_event(drv->ctx, EVENT_P2P_GO_NEG_COMPLETED, &event);
   3110 }
   3111 
   3112 
   3113 static void test_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
   3114 {
   3115 	struct wpa_driver_test_data *drv = ctx;
   3116 	union wpa_event_data event;
   3117 	wpa_printf(MSG_DEBUG, "%s(src=" MACSTR ")", __func__, MAC2STR(src));
   3118 	os_memset(&event, 0, sizeof(event));
   3119 	event.p2p_go_neg_req_rx.src = src;
   3120 	event.p2p_go_neg_req_rx.dev_passwd_id = dev_passwd_id;
   3121 	wpa_supplicant_event(drv->ctx, EVENT_P2P_GO_NEG_REQ_RX, &event);
   3122 }
   3123 
   3124 
   3125 static void test_dev_found(void *ctx, const u8 *addr,
   3126 			   const struct p2p_peer_info *info, int new_device)
   3127 {
   3128 	struct wpa_driver_test_data *drv = ctx;
   3129 	union wpa_event_data event;
   3130 	char devtype[WPS_DEV_TYPE_BUFSIZE];
   3131 	wpa_printf(MSG_DEBUG, "%s(" MACSTR " p2p_dev_addr=" MACSTR
   3132 		   " pri_dev_type=%s name='%s' config_methods=0x%x "
   3133 		   "dev_capab=0x%x group_capab=0x%x)",
   3134 		   __func__, MAC2STR(addr), MAC2STR(info->p2p_device_addr),
   3135 		   wps_dev_type_bin2str(info->pri_dev_type, devtype,
   3136 					sizeof(devtype)),
   3137 		   info->device_name, info->config_methods, info->dev_capab,
   3138 		   info->group_capab);
   3139 
   3140 	os_memset(&event, 0, sizeof(event));
   3141 	event.p2p_dev_found.addr = addr;
   3142 	event.p2p_dev_found.dev_addr = info->p2p_device_addr;
   3143 	event.p2p_dev_found.pri_dev_type = info->pri_dev_type;
   3144 	event.p2p_dev_found.dev_name = info->device_name;
   3145 	event.p2p_dev_found.config_methods = info->config_methods;
   3146 	event.p2p_dev_found.dev_capab = info->dev_capab;
   3147 	event.p2p_dev_found.group_capab = info->group_capab;
   3148 	wpa_supplicant_event(drv->ctx, EVENT_P2P_DEV_FOUND, &event);
   3149 }
   3150 
   3151 
   3152 static int test_start_listen(void *ctx, unsigned int freq,
   3153 			     unsigned int duration,
   3154 			     const struct wpabuf *probe_resp_ie)
   3155 {
   3156 	struct wpa_driver_test_data *drv = ctx;
   3157 
   3158 	wpa_printf(MSG_DEBUG, "%s(freq=%u duration=%u)",
   3159 		   __func__, freq, duration);
   3160 
   3161 	if (wpa_driver_test_probe_req_report(drv, 1) < 0)
   3162 		return -1;
   3163 
   3164 	drv->pending_listen_freq = freq;
   3165 	drv->pending_listen_duration = duration;
   3166 
   3167 	if (wpa_driver_test_remain_on_channel(drv, freq, duration) < 0) {
   3168 		drv->pending_listen_freq = 0;
   3169 		return -1;
   3170 	}
   3171 
   3172 	return 0;
   3173 }
   3174 
   3175 
   3176 static void test_stop_listen(void *ctx)
   3177 {
   3178 	wpa_printf(MSG_DEBUG, "%s", __func__);
   3179 	/* TODO */
   3180 }
   3181 
   3182 
   3183 static int test_send_probe_resp(void *ctx, const struct wpabuf *buf)
   3184 {
   3185 	struct wpa_driver_test_data *drv = ctx;
   3186 	char resp[512], *pos, *end;
   3187 	int ret;
   3188 	const struct ieee80211_mgmt *mgmt;
   3189 	const u8 *ie, *ie_end;
   3190 
   3191 	wpa_printf(MSG_DEBUG, "%s", __func__);
   3192 	wpa_hexdump_buf(MSG_MSGDUMP, "Probe Response", buf);
   3193 	if (wpabuf_len(buf) < 24)
   3194 		return -1;
   3195 	if (!drv->probe_from) {
   3196 		wpa_printf(MSG_DEBUG, "%s: probe_from not set", __func__);
   3197 		return -1;
   3198 	}
   3199 
   3200 	pos = resp;
   3201 	end = resp + sizeof(resp);
   3202 
   3203 	mgmt = wpabuf_head(buf);
   3204 
   3205 	/* reply: SCANRESP BSSID SSID IEs */
   3206 	ret = os_snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
   3207 			  MAC2STR(mgmt->bssid));
   3208 	if (ret < 0 || ret >= end - pos)
   3209 		return -1;
   3210 	pos += ret;
   3211 
   3212 	ie = mgmt->u.probe_resp.variable;
   3213 	ie_end = wpabuf_head_u8(buf) + wpabuf_len(buf);
   3214 	if (ie_end - ie < 2 || ie[0] != WLAN_EID_SSID ||
   3215 	    ie + 2 + ie[1] > ie_end)
   3216 		return -1;
   3217 	pos += wpa_snprintf_hex(pos, end - pos, ie + 2, ie[1]);
   3218 
   3219 	ret = os_snprintf(pos, end - pos, " ");
   3220 	if (ret < 0 || ret >= end - pos)
   3221 		return -1;
   3222 	pos += ret;
   3223 	pos += wpa_snprintf_hex(pos, end - pos, ie, ie_end - ie);
   3224 
   3225 	sendto(drv->test_socket, resp, pos - resp, 0,
   3226 	       drv->probe_from, drv->probe_from_len);
   3227 
   3228 	return 0;
   3229 }
   3230 
   3231 
   3232 static void test_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
   3233 			    u16 update_indic, const u8 *tlvs, size_t tlvs_len)
   3234 {
   3235 	wpa_printf(MSG_DEBUG, "%s", __func__);
   3236 	/* TODO */
   3237 }
   3238 
   3239 
   3240 static void test_sd_response(void *ctx, const u8 *sa, u16 update_indic,
   3241 			     const u8 *tlvs, size_t tlvs_len)
   3242 {
   3243 	wpa_printf(MSG_DEBUG, "%s", __func__);
   3244 	/* TODO */
   3245 }
   3246 
   3247 
   3248 static void test_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
   3249 			       const u8 *dev_addr, const u8 *pri_dev_type,
   3250 			       const char *dev_name, u16 supp_config_methods,
   3251 			       u8 dev_capab, u8 group_capab)
   3252 {
   3253 	wpa_printf(MSG_DEBUG, "%s(peer=" MACSTR " config_methods=0x%x)",
   3254 		   __func__, MAC2STR(peer), config_methods);
   3255 	/* TODO */
   3256 }
   3257 
   3258 
   3259 static void test_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
   3260 {
   3261 	wpa_printf(MSG_DEBUG, "%s(peer=" MACSTR " config_methods=0x%x)",
   3262 		   __func__, MAC2STR(peer), config_methods);
   3263 	/* TODO */
   3264 }
   3265 
   3266 #endif /* CONFIG_P2P */
   3267 
   3268 
   3269 static int wpa_driver_test_init_p2p(struct wpa_driver_test_data *drv)
   3270 {
   3271 #ifdef CONFIG_P2P
   3272 	struct p2p_config p2p;
   3273 	unsigned int r;
   3274 	int i;
   3275 
   3276 	os_memset(&p2p, 0, sizeof(p2p));
   3277 	p2p.msg_ctx = drv->ctx;
   3278 	p2p.cb_ctx = drv;
   3279 	p2p.p2p_scan = test_p2p_scan;
   3280 	p2p.send_action = test_send_action;
   3281 	p2p.send_action_done = test_send_action_done;
   3282 	p2p.go_neg_completed = test_go_neg_completed;
   3283 	p2p.go_neg_req_rx = test_go_neg_req_rx;
   3284 	p2p.dev_found = test_dev_found;
   3285 	p2p.start_listen = test_start_listen;
   3286 	p2p.stop_listen = test_stop_listen;
   3287 	p2p.send_probe_resp = test_send_probe_resp;
   3288 	p2p.sd_request = test_sd_request;
   3289 	p2p.sd_response = test_sd_response;
   3290 	p2p.prov_disc_req = test_prov_disc_req;
   3291 	p2p.prov_disc_resp = test_prov_disc_resp;
   3292 
   3293 	os_memcpy(p2p.dev_addr, drv->own_addr, ETH_ALEN);
   3294 
   3295 	p2p.reg_class = 12; /* TODO: change depending on location */
   3296 	/*
   3297 	 * Pick one of the social channels randomly as the listen
   3298 	 * channel.
   3299 	 */
   3300 	os_get_random((u8 *) &r, sizeof(r));
   3301 	p2p.channel = 1 + (r % 3) * 5;
   3302 
   3303 	/* TODO: change depending on location */
   3304 	p2p.op_reg_class = 12;
   3305 	/*
   3306 	 * For initial tests, pick the operation channel randomly.
   3307 	 * TODO: Use scan results (etc.) to select the best channel.
   3308 	 */
   3309 	p2p.op_channel = 1 + r % 11;
   3310 
   3311 	os_memcpy(p2p.country, "US ", 3);
   3312 
   3313 	/* FIX: fetch available channels from the driver */
   3314 	p2p.channels.reg_classes = 1;
   3315 	p2p.channels.reg_class[0].reg_class = 12; /* US/12 = 2.4 GHz band */
   3316 	p2p.channels.reg_class[0].channels = 11;
   3317 	for (i = 0; i < 11; i++)
   3318 		p2p.channels.reg_class[0].channel[i] = i + 1;
   3319 
   3320 	p2p.max_peers = 100;
   3321 
   3322 	drv->p2p = p2p_init(&p2p);
   3323 	if (drv->p2p == NULL)
   3324 		return -1;
   3325 	return 0;
   3326 #else /* CONFIG_P2P */
   3327 	wpa_printf(MSG_INFO, "driver_test: P2P support not included");
   3328 	return -1;
   3329 #endif /* CONFIG_P2P */
   3330 }
   3331 
   3332 
   3333 const struct wpa_driver_ops wpa_driver_test_ops = {
   3334 	"test",
   3335 	"wpa_supplicant test driver",
   3336 	.hapd_init = test_driver_init,
   3337 	.hapd_deinit = wpa_driver_test_deinit,
   3338 	.hapd_send_eapol = test_driver_send_eapol,
   3339 	.send_mlme = wpa_driver_test_send_mlme,
   3340 	.set_generic_elem = test_driver_set_generic_elem,
   3341 	.sta_deauth = test_driver_sta_deauth,
   3342 	.sta_disassoc = test_driver_sta_disassoc,
   3343 	.get_hw_feature_data = wpa_driver_test_get_hw_feature_data,
   3344 	.if_add = test_driver_if_add,
   3345 	.if_remove = test_driver_if_remove,
   3346 	.valid_bss_mask = test_driver_valid_bss_mask,
   3347 	.hapd_set_ssid = test_driver_set_ssid,
   3348 	.set_privacy = test_driver_set_privacy,
   3349 	.set_sta_vlan = test_driver_set_sta_vlan,
   3350 	.sta_add = test_driver_sta_add,
   3351 	.send_ether = test_driver_send_ether,
   3352 	.set_ap_wps_ie = test_driver_set_ap_wps_ie,
   3353 	.get_bssid = wpa_driver_test_get_bssid,
   3354 	.get_ssid = wpa_driver_test_get_ssid,
   3355 	.set_key = wpa_driver_test_set_key,
   3356 	.deinit = wpa_driver_test_deinit,
   3357 	.set_param = wpa_driver_test_set_param,
   3358 	.deauthenticate = wpa_driver_test_deauthenticate,
   3359 	.disassociate = wpa_driver_test_disassociate,
   3360 	.associate = wpa_driver_test_associate,
   3361 	.get_capa = wpa_driver_test_get_capa,
   3362 	.get_mac_addr = wpa_driver_test_get_mac_addr,
   3363 	.send_eapol = wpa_driver_test_send_eapol,
   3364 	.mlme_setprotection = wpa_driver_test_mlme_setprotection,
   3365 	.set_channel = wpa_driver_test_set_channel,
   3366 	.set_ssid = wpa_driver_test_set_ssid,
   3367 	.set_bssid = wpa_driver_test_set_bssid,
   3368 	.mlme_add_sta = wpa_driver_test_mlme_add_sta,
   3369 	.mlme_remove_sta = wpa_driver_test_mlme_remove_sta,
   3370 	.get_scan_results2 = wpa_driver_test_get_scan_results2,
   3371 	.global_init = wpa_driver_test_global_init,
   3372 	.global_deinit = wpa_driver_test_global_deinit,
   3373 	.init2 = wpa_driver_test_init2,
   3374 	.get_interfaces = wpa_driver_test_get_interfaces,
   3375 	.scan2 = wpa_driver_test_scan,
   3376 	.set_freq = wpa_driver_test_set_freq,
   3377 	.send_action = wpa_driver_test_send_action,
   3378 	.remain_on_channel = wpa_driver_test_remain_on_channel,
   3379 	.cancel_remain_on_channel = wpa_driver_test_cancel_remain_on_channel,
   3380 	.probe_req_report = wpa_driver_test_probe_req_report,
   3381 #ifdef CONFIG_P2P
   3382 	.p2p_find = wpa_driver_test_p2p_find,
   3383 	.p2p_stop_find = wpa_driver_test_p2p_stop_find,
   3384 	.p2p_listen = wpa_driver_test_p2p_listen,
   3385 	.p2p_connect = wpa_driver_test_p2p_connect,
   3386 	.wps_success_cb = wpa_driver_test_wps_success_cb,
   3387 	.p2p_group_formation_failed =
   3388 	wpa_driver_test_p2p_group_formation_failed,
   3389 	.p2p_set_params = wpa_driver_test_p2p_set_params,
   3390 #endif /* CONFIG_P2P */
   3391 };
   3392