Line data Source code
1 : /*
2 : * Testing driver interface for a simulated network driver
3 : * Copyright (c) 2004-2010, Jouni Malinen <j@w1.fi>
4 : *
5 : * This software may be distributed under the terms of the BSD license.
6 : * See README for more details.
7 : */
8 :
9 : /* Make sure we get winsock2.h for Windows build to get sockaddr_storage */
10 : #include "build_config.h"
11 : #ifdef CONFIG_NATIVE_WINDOWS
12 : #include <winsock2.h>
13 : #endif /* CONFIG_NATIVE_WINDOWS */
14 :
15 : #include "utils/includes.h"
16 :
17 : #ifndef CONFIG_NATIVE_WINDOWS
18 : #include <sys/un.h>
19 : #include <dirent.h>
20 : #include <sys/stat.h>
21 : #define DRIVER_TEST_UNIX
22 : #endif /* CONFIG_NATIVE_WINDOWS */
23 :
24 : #include "utils/common.h"
25 : #include "utils/eloop.h"
26 : #include "utils/list.h"
27 : #include "utils/trace.h"
28 : #include "common/ieee802_11_defs.h"
29 : #include "crypto/sha1.h"
30 : #include "l2_packet/l2_packet.h"
31 : #include "wps/wps.h"
32 : #include "driver.h"
33 :
34 :
35 : struct test_client_socket {
36 : struct test_client_socket *next;
37 : u8 addr[ETH_ALEN];
38 : struct sockaddr_un un;
39 : socklen_t unlen;
40 : struct test_driver_bss *bss;
41 : };
42 :
43 : struct test_driver_bss {
44 : struct wpa_driver_test_data *drv;
45 : struct dl_list list;
46 : void *bss_ctx;
47 : char ifname[IFNAMSIZ];
48 : u8 bssid[ETH_ALEN];
49 : u8 *ie;
50 : size_t ielen;
51 : u8 *wps_beacon_ie;
52 : size_t wps_beacon_ie_len;
53 : u8 *wps_probe_resp_ie;
54 : size_t wps_probe_resp_ie_len;
55 : u8 ssid[32];
56 : size_t ssid_len;
57 : int privacy;
58 : };
59 :
60 : struct wpa_driver_test_global {
61 : int bss_add_used;
62 : u8 req_addr[ETH_ALEN];
63 : };
64 :
65 : struct wpa_driver_test_data {
66 : struct wpa_driver_test_global *global;
67 : void *ctx;
68 : WPA_TRACE_REF(ctx);
69 : u8 own_addr[ETH_ALEN];
70 : int test_socket;
71 : #ifdef DRIVER_TEST_UNIX
72 : struct sockaddr_un hostapd_addr;
73 : #endif /* DRIVER_TEST_UNIX */
74 : int hostapd_addr_set;
75 : struct sockaddr_in hostapd_addr_udp;
76 : int hostapd_addr_udp_set;
77 : char *own_socket_path;
78 : char *test_dir;
79 : #define MAX_SCAN_RESULTS 30
80 : struct wpa_scan_res *scanres[MAX_SCAN_RESULTS];
81 : size_t num_scanres;
82 : int use_associnfo;
83 : u8 assoc_wpa_ie[80];
84 : size_t assoc_wpa_ie_len;
85 : int associated;
86 : u8 *probe_req_ie;
87 : size_t probe_req_ie_len;
88 : u8 probe_req_ssid[32];
89 : size_t probe_req_ssid_len;
90 : int ibss;
91 : int ap;
92 :
93 : struct test_client_socket *cli;
94 : struct dl_list bss;
95 : int udp_port;
96 :
97 : int alloc_iface_idx;
98 :
99 : int probe_req_report;
100 : unsigned int remain_on_channel_freq;
101 : unsigned int remain_on_channel_duration;
102 :
103 : int current_freq;
104 : };
105 :
106 :
107 : static void wpa_driver_test_deinit(void *priv);
108 : static int wpa_driver_test_attach(struct wpa_driver_test_data *drv,
109 : const char *dir, int ap);
110 : static void wpa_driver_test_close_test_socket(
111 : struct wpa_driver_test_data *drv);
112 : static void test_remain_on_channel_timeout(void *eloop_ctx, void *timeout_ctx);
113 :
114 :
115 0 : static void test_driver_free_bss(struct test_driver_bss *bss)
116 : {
117 0 : os_free(bss->ie);
118 0 : os_free(bss->wps_beacon_ie);
119 0 : os_free(bss->wps_probe_resp_ie);
120 0 : os_free(bss);
121 0 : }
122 :
123 :
124 0 : static void test_driver_free_bsses(struct wpa_driver_test_data *drv)
125 : {
126 : struct test_driver_bss *bss, *tmp;
127 :
128 0 : dl_list_for_each_safe(bss, tmp, &drv->bss, struct test_driver_bss,
129 : list) {
130 0 : dl_list_del(&bss->list);
131 0 : test_driver_free_bss(bss);
132 : }
133 0 : }
134 :
135 :
136 : static struct test_client_socket *
137 0 : test_driver_get_cli(struct wpa_driver_test_data *drv, struct sockaddr_un *from,
138 : socklen_t fromlen)
139 : {
140 0 : struct test_client_socket *cli = drv->cli;
141 :
142 0 : while (cli) {
143 0 : if (cli->unlen == fromlen &&
144 0 : strncmp(cli->un.sun_path, from->sun_path,
145 : fromlen - sizeof(cli->un.sun_family)) == 0)
146 0 : return cli;
147 0 : cli = cli->next;
148 : }
149 :
150 0 : return NULL;
151 : }
152 :
153 :
154 0 : static int test_driver_send_eapol(void *priv, const u8 *addr, const u8 *data,
155 : size_t data_len, int encrypt,
156 : const u8 *own_addr, u32 flags)
157 : {
158 0 : struct test_driver_bss *dbss = priv;
159 0 : struct wpa_driver_test_data *drv = dbss->drv;
160 : struct test_client_socket *cli;
161 : struct msghdr msg;
162 : struct iovec io[3];
163 : struct l2_ethhdr eth;
164 :
165 0 : if (drv->test_socket < 0)
166 0 : return -1;
167 :
168 0 : cli = drv->cli;
169 0 : while (cli) {
170 0 : if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
171 0 : break;
172 0 : cli = cli->next;
173 : }
174 :
175 0 : if (!cli) {
176 0 : wpa_printf(MSG_DEBUG, "%s: no destination client entry",
177 : __func__);
178 0 : return -1;
179 : }
180 :
181 0 : memcpy(eth.h_dest, addr, ETH_ALEN);
182 0 : memcpy(eth.h_source, own_addr, ETH_ALEN);
183 0 : eth.h_proto = host_to_be16(ETH_P_EAPOL);
184 :
185 0 : io[0].iov_base = "EAPOL ";
186 0 : io[0].iov_len = 6;
187 0 : io[1].iov_base = ð
188 0 : io[1].iov_len = sizeof(eth);
189 0 : io[2].iov_base = (u8 *) data;
190 0 : io[2].iov_len = data_len;
191 :
192 0 : memset(&msg, 0, sizeof(msg));
193 0 : msg.msg_iov = io;
194 0 : msg.msg_iovlen = 3;
195 0 : msg.msg_name = &cli->un;
196 0 : msg.msg_namelen = cli->unlen;
197 0 : return sendmsg(drv->test_socket, &msg, 0);
198 : }
199 :
200 :
201 0 : static int test_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
202 : u16 proto, const u8 *data, size_t data_len)
203 : {
204 0 : struct test_driver_bss *dbss = priv;
205 0 : struct wpa_driver_test_data *drv = dbss->drv;
206 : struct msghdr msg;
207 : struct iovec io[3];
208 : struct l2_ethhdr eth;
209 : char desttxt[30];
210 : struct sockaddr_un addr;
211 : struct dirent *dent;
212 : DIR *dir;
213 0 : int ret = 0, broadcast = 0, count = 0;
214 :
215 0 : if (drv->test_socket < 0 || drv->test_dir == NULL) {
216 0 : wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d "
217 : "test_dir=%p)",
218 : __func__, drv->test_socket, drv->test_dir);
219 0 : return -1;
220 : }
221 :
222 0 : broadcast = memcmp(dst, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
223 0 : snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dst));
224 :
225 0 : memcpy(eth.h_dest, dst, ETH_ALEN);
226 0 : memcpy(eth.h_source, src, ETH_ALEN);
227 0 : eth.h_proto = host_to_be16(proto);
228 :
229 0 : io[0].iov_base = "ETHER ";
230 0 : io[0].iov_len = 6;
231 0 : io[1].iov_base = ð
232 0 : io[1].iov_len = sizeof(eth);
233 0 : io[2].iov_base = (u8 *) data;
234 0 : io[2].iov_len = data_len;
235 :
236 0 : memset(&msg, 0, sizeof(msg));
237 0 : msg.msg_iov = io;
238 0 : msg.msg_iovlen = 3;
239 :
240 0 : dir = opendir(drv->test_dir);
241 0 : if (dir == NULL) {
242 0 : perror("test_driver: opendir");
243 0 : return -1;
244 : }
245 0 : while ((dent = readdir(dir))) {
246 : #ifdef _DIRENT_HAVE_D_TYPE
247 : /* Skip the file if it is not a socket. Also accept
248 : * DT_UNKNOWN (0) in case the C library or underlying file
249 : * system does not support d_type. */
250 0 : if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
251 0 : continue;
252 : #endif /* _DIRENT_HAVE_D_TYPE */
253 0 : if (strcmp(dent->d_name, ".") == 0 ||
254 0 : strcmp(dent->d_name, "..") == 0)
255 0 : continue;
256 :
257 0 : memset(&addr, 0, sizeof(addr));
258 0 : addr.sun_family = AF_UNIX;
259 0 : snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
260 0 : drv->test_dir, dent->d_name);
261 :
262 0 : if (strcmp(addr.sun_path, drv->own_socket_path) == 0)
263 0 : continue;
264 0 : if (!broadcast && strstr(dent->d_name, desttxt) == NULL)
265 0 : continue;
266 :
267 0 : wpa_printf(MSG_DEBUG, "%s: Send ether frame to %s",
268 0 : __func__, dent->d_name);
269 :
270 0 : msg.msg_name = &addr;
271 0 : msg.msg_namelen = sizeof(addr);
272 0 : ret = sendmsg(drv->test_socket, &msg, 0);
273 0 : if (ret < 0)
274 0 : perror("driver_test: sendmsg");
275 0 : count++;
276 : }
277 0 : closedir(dir);
278 :
279 0 : if (!broadcast && count == 0) {
280 0 : wpa_printf(MSG_DEBUG, "%s: Destination " MACSTR " not found",
281 0 : __func__, MAC2STR(dst));
282 0 : return -1;
283 : }
284 :
285 0 : return ret;
286 : }
287 :
288 :
289 0 : static int wpa_driver_test_send_mlme(void *priv, const u8 *data,
290 : size_t data_len, int noack)
291 : {
292 0 : struct test_driver_bss *dbss = priv;
293 0 : struct wpa_driver_test_data *drv = dbss->drv;
294 : struct msghdr msg;
295 : struct iovec io[2];
296 : const u8 *dest;
297 : struct sockaddr_un addr;
298 : struct dirent *dent;
299 : DIR *dir;
300 : int broadcast;
301 0 : int ret = 0;
302 : struct ieee80211_hdr *hdr;
303 : u16 fc;
304 : char cmd[50];
305 : int freq;
306 : #ifdef HOSTAPD
307 : char desttxt[30];
308 : #endif /* HOSTAPD */
309 : union wpa_event_data event;
310 :
311 0 : wpa_hexdump(MSG_MSGDUMP, "test_send_mlme", data, data_len);
312 0 : if (drv->test_socket < 0 || data_len < 10) {
313 0 : wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d len=%lu"
314 : " test_dir=%p)",
315 : __func__, drv->test_socket,
316 : (unsigned long) data_len,
317 : drv->test_dir);
318 0 : return -1;
319 : }
320 :
321 0 : dest = data + 4;
322 0 : broadcast = os_memcmp(dest, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
323 :
324 : #ifdef HOSTAPD
325 0 : snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dest));
326 : #endif /* HOSTAPD */
327 :
328 0 : if (drv->remain_on_channel_freq)
329 0 : freq = drv->remain_on_channel_freq;
330 : else
331 0 : freq = drv->current_freq;
332 0 : wpa_printf(MSG_DEBUG, "test_driver(%s): MLME TX on freq %d MHz",
333 0 : dbss->ifname, freq);
334 0 : os_snprintf(cmd, sizeof(cmd), "MLME freq=%d ", freq);
335 0 : io[0].iov_base = cmd;
336 0 : io[0].iov_len = os_strlen(cmd);
337 0 : io[1].iov_base = (void *) data;
338 0 : io[1].iov_len = data_len;
339 :
340 0 : os_memset(&msg, 0, sizeof(msg));
341 0 : msg.msg_iov = io;
342 0 : msg.msg_iovlen = 2;
343 :
344 : #ifdef HOSTAPD
345 0 : if (drv->test_dir == NULL) {
346 0 : wpa_printf(MSG_DEBUG, "%s: test_dir == NULL", __func__);
347 0 : return -1;
348 : }
349 :
350 0 : dir = opendir(drv->test_dir);
351 0 : if (dir == NULL) {
352 0 : perror("test_driver: opendir");
353 0 : return -1;
354 : }
355 0 : while ((dent = readdir(dir))) {
356 : #ifdef _DIRENT_HAVE_D_TYPE
357 : /* Skip the file if it is not a socket. Also accept
358 : * DT_UNKNOWN (0) in case the C library or underlying file
359 : * system does not support d_type. */
360 0 : if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
361 0 : continue;
362 : #endif /* _DIRENT_HAVE_D_TYPE */
363 0 : if (os_strcmp(dent->d_name, ".") == 0 ||
364 0 : os_strcmp(dent->d_name, "..") == 0)
365 0 : continue;
366 :
367 0 : os_memset(&addr, 0, sizeof(addr));
368 0 : addr.sun_family = AF_UNIX;
369 0 : os_snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
370 0 : drv->test_dir, dent->d_name);
371 :
372 0 : if (os_strcmp(addr.sun_path, drv->own_socket_path) == 0)
373 0 : continue;
374 0 : if (!broadcast && os_strstr(dent->d_name, desttxt) == NULL)
375 0 : continue;
376 :
377 0 : wpa_printf(MSG_DEBUG, "%s: Send management frame to %s",
378 0 : __func__, dent->d_name);
379 :
380 0 : msg.msg_name = &addr;
381 0 : msg.msg_namelen = sizeof(addr);
382 0 : ret = sendmsg(drv->test_socket, &msg, 0);
383 0 : if (ret < 0)
384 0 : perror("driver_test: sendmsg(test_socket)");
385 : }
386 0 : closedir(dir);
387 : #else /* HOSTAPD */
388 :
389 : if (os_memcmp(dest, dbss->bssid, ETH_ALEN) == 0 ||
390 : drv->test_dir == NULL) {
391 : if (drv->hostapd_addr_udp_set) {
392 : msg.msg_name = &drv->hostapd_addr_udp;
393 : msg.msg_namelen = sizeof(drv->hostapd_addr_udp);
394 : } else {
395 : #ifdef DRIVER_TEST_UNIX
396 : msg.msg_name = &drv->hostapd_addr;
397 : msg.msg_namelen = sizeof(drv->hostapd_addr);
398 : #endif /* DRIVER_TEST_UNIX */
399 : }
400 : } else if (broadcast) {
401 : dir = opendir(drv->test_dir);
402 : if (dir == NULL)
403 : return -1;
404 : while ((dent = readdir(dir))) {
405 : #ifdef _DIRENT_HAVE_D_TYPE
406 : /* Skip the file if it is not a socket.
407 : * Also accept DT_UNKNOWN (0) in case
408 : * the C library or underlying file
409 : * system does not support d_type. */
410 : if (dent->d_type != DT_SOCK &&
411 : dent->d_type != DT_UNKNOWN)
412 : continue;
413 : #endif /* _DIRENT_HAVE_D_TYPE */
414 : if (os_strcmp(dent->d_name, ".") == 0 ||
415 : os_strcmp(dent->d_name, "..") == 0)
416 : continue;
417 : wpa_printf(MSG_DEBUG, "%s: Send broadcast MLME to %s",
418 : __func__, dent->d_name);
419 : os_memset(&addr, 0, sizeof(addr));
420 : addr.sun_family = AF_UNIX;
421 : os_snprintf(addr.sun_path, sizeof(addr.sun_path),
422 : "%s/%s", drv->test_dir, dent->d_name);
423 :
424 : msg.msg_name = &addr;
425 : msg.msg_namelen = sizeof(addr);
426 :
427 : ret = sendmsg(drv->test_socket, &msg, 0);
428 : if (ret < 0)
429 : perror("driver_test: sendmsg(test_socket)");
430 : }
431 : closedir(dir);
432 : return ret;
433 : } else {
434 : struct stat st;
435 : os_memset(&addr, 0, sizeof(addr));
436 : addr.sun_family = AF_UNIX;
437 : os_snprintf(addr.sun_path, sizeof(addr.sun_path),
438 : "%s/AP-" MACSTR, drv->test_dir, MAC2STR(dest));
439 : if (stat(addr.sun_path, &st) < 0) {
440 : os_snprintf(addr.sun_path, sizeof(addr.sun_path),
441 : "%s/STA-" MACSTR,
442 : drv->test_dir, MAC2STR(dest));
443 : }
444 : msg.msg_name = &addr;
445 : msg.msg_namelen = sizeof(addr);
446 : }
447 :
448 : if (sendmsg(drv->test_socket, &msg, 0) < 0) {
449 : perror("sendmsg(test_socket)");
450 : return -1;
451 : }
452 : #endif /* HOSTAPD */
453 :
454 0 : hdr = (struct ieee80211_hdr *) data;
455 0 : fc = le_to_host16(hdr->frame_control);
456 :
457 0 : os_memset(&event, 0, sizeof(event));
458 0 : event.tx_status.type = WLAN_FC_GET_TYPE(fc);
459 0 : event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
460 0 : event.tx_status.dst = hdr->addr1;
461 0 : event.tx_status.data = data;
462 0 : event.tx_status.data_len = data_len;
463 0 : event.tx_status.ack = ret >= 0;
464 0 : wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
465 :
466 0 : return ret;
467 : }
468 :
469 :
470 0 : static void test_driver_scan(struct wpa_driver_test_data *drv,
471 : struct sockaddr_un *from, socklen_t fromlen,
472 : char *data)
473 : {
474 : char buf[512], *pos, *end;
475 : int ret;
476 : struct test_driver_bss *bss;
477 : u8 sa[ETH_ALEN];
478 : u8 ie[512];
479 : size_t ielen;
480 : union wpa_event_data event;
481 :
482 : /* data: optional [ ' ' | STA-addr | ' ' | IEs(hex) ] */
483 :
484 0 : wpa_printf(MSG_DEBUG, "test_driver: SCAN");
485 :
486 0 : if (*data) {
487 0 : if (*data != ' ' ||
488 0 : hwaddr_aton(data + 1, sa)) {
489 0 : wpa_printf(MSG_DEBUG, "test_driver: Unexpected SCAN "
490 : "command format");
491 0 : return;
492 : }
493 :
494 0 : data += 18;
495 0 : while (*data == ' ')
496 0 : data++;
497 0 : ielen = os_strlen(data) / 2;
498 0 : if (ielen > sizeof(ie))
499 0 : ielen = sizeof(ie);
500 0 : if (hexstr2bin(data, ie, ielen) < 0)
501 0 : ielen = 0;
502 :
503 0 : wpa_printf(MSG_DEBUG, "test_driver: Scan from " MACSTR,
504 0 : MAC2STR(sa));
505 0 : wpa_hexdump(MSG_MSGDUMP, "test_driver: scan IEs", ie, ielen);
506 :
507 0 : os_memset(&event, 0, sizeof(event));
508 0 : event.rx_probe_req.sa = sa;
509 0 : event.rx_probe_req.ie = ie;
510 0 : event.rx_probe_req.ie_len = ielen;
511 0 : wpa_supplicant_event(drv->ctx, EVENT_RX_PROBE_REQ, &event);
512 : }
513 :
514 0 : dl_list_for_each(bss, &drv->bss, struct test_driver_bss, list) {
515 0 : pos = buf;
516 0 : end = buf + sizeof(buf);
517 :
518 : /* reply: SCANRESP BSSID SSID IEs */
519 0 : ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
520 0 : MAC2STR(bss->bssid));
521 0 : if (ret < 0 || ret >= end - pos)
522 0 : return;
523 0 : pos += ret;
524 0 : pos += wpa_snprintf_hex(pos, end - pos,
525 0 : bss->ssid, bss->ssid_len);
526 0 : ret = snprintf(pos, end - pos, " ");
527 0 : if (ret < 0 || ret >= end - pos)
528 0 : return;
529 0 : pos += ret;
530 0 : pos += wpa_snprintf_hex(pos, end - pos, bss->ie, bss->ielen);
531 0 : pos += wpa_snprintf_hex(pos, end - pos, bss->wps_probe_resp_ie,
532 : bss->wps_probe_resp_ie_len);
533 :
534 0 : if (bss->privacy) {
535 0 : ret = snprintf(pos, end - pos, " PRIVACY");
536 0 : if (ret < 0 || ret >= end - pos)
537 0 : return;
538 0 : pos += ret;
539 : }
540 :
541 0 : sendto(drv->test_socket, buf, pos - buf, 0,
542 : (struct sockaddr *) from, fromlen);
543 : }
544 : }
545 :
546 :
547 0 : static void test_driver_assoc(struct wpa_driver_test_data *drv,
548 : struct sockaddr_un *from, socklen_t fromlen,
549 : char *data)
550 : {
551 : struct test_client_socket *cli;
552 : u8 ie[256], ssid[32];
553 0 : size_t ielen, ssid_len = 0;
554 : char *pos, *pos2, cmd[50];
555 : struct test_driver_bss *bss, *tmp;
556 :
557 : /* data: STA-addr SSID(hex) IEs(hex) */
558 :
559 0 : cli = os_zalloc(sizeof(*cli));
560 0 : if (cli == NULL)
561 0 : return;
562 :
563 0 : if (hwaddr_aton(data, cli->addr)) {
564 0 : printf("test_socket: Invalid MAC address '%s' in ASSOC\n",
565 : data);
566 0 : os_free(cli);
567 0 : return;
568 : }
569 0 : pos = data + 17;
570 0 : while (*pos == ' ')
571 0 : pos++;
572 0 : pos2 = strchr(pos, ' ');
573 0 : ielen = 0;
574 0 : if (pos2) {
575 0 : ssid_len = (pos2 - pos) / 2;
576 0 : if (hexstr2bin(pos, ssid, ssid_len) < 0) {
577 0 : wpa_printf(MSG_DEBUG, "%s: Invalid SSID", __func__);
578 0 : os_free(cli);
579 0 : return;
580 : }
581 0 : wpa_hexdump_ascii(MSG_DEBUG, "test_driver_assoc: SSID",
582 : ssid, ssid_len);
583 :
584 0 : pos = pos2 + 1;
585 0 : ielen = strlen(pos) / 2;
586 0 : if (ielen > sizeof(ie))
587 0 : ielen = sizeof(ie);
588 0 : if (hexstr2bin(pos, ie, ielen) < 0)
589 0 : ielen = 0;
590 : }
591 :
592 0 : bss = NULL;
593 0 : dl_list_for_each(tmp, &drv->bss, struct test_driver_bss, list) {
594 0 : if (tmp->ssid_len == ssid_len &&
595 0 : os_memcmp(tmp->ssid, ssid, ssid_len) == 0) {
596 0 : bss = tmp;
597 0 : break;
598 : }
599 : }
600 0 : if (bss == NULL) {
601 0 : wpa_printf(MSG_DEBUG, "%s: No matching SSID found from "
602 : "configured BSSes", __func__);
603 0 : os_free(cli);
604 0 : return;
605 : }
606 :
607 0 : cli->bss = bss;
608 0 : memcpy(&cli->un, from, sizeof(cli->un));
609 0 : cli->unlen = fromlen;
610 0 : cli->next = drv->cli;
611 0 : drv->cli = cli;
612 0 : wpa_hexdump_ascii(MSG_DEBUG, "test_socket: ASSOC sun_path",
613 0 : (const u8 *) cli->un.sun_path,
614 0 : cli->unlen - sizeof(cli->un.sun_family));
615 :
616 0 : snprintf(cmd, sizeof(cmd), "ASSOCRESP " MACSTR " 0",
617 0 : MAC2STR(bss->bssid));
618 0 : sendto(drv->test_socket, cmd, strlen(cmd), 0,
619 : (struct sockaddr *) from, fromlen);
620 :
621 0 : drv_event_assoc(bss->bss_ctx, cli->addr, ie, ielen, 0);
622 : }
623 :
624 :
625 0 : static void test_driver_disassoc(struct wpa_driver_test_data *drv,
626 : struct sockaddr_un *from, socklen_t fromlen)
627 : {
628 : struct test_client_socket *cli;
629 :
630 0 : cli = test_driver_get_cli(drv, from, fromlen);
631 0 : if (!cli)
632 0 : return;
633 :
634 0 : drv_event_disassoc(drv->ctx, cli->addr);
635 : }
636 :
637 :
638 0 : static void test_driver_eapol(struct wpa_driver_test_data *drv,
639 : struct sockaddr_un *from, socklen_t fromlen,
640 : u8 *data, size_t datalen)
641 : {
642 : #ifdef HOSTAPD
643 : struct test_client_socket *cli;
644 : #endif /* HOSTAPD */
645 0 : const u8 *src = NULL;
646 :
647 0 : if (datalen > 14) {
648 : /* Skip Ethernet header */
649 0 : src = data + ETH_ALEN;
650 0 : wpa_printf(MSG_DEBUG, "test_driver: dst=" MACSTR " src="
651 : MACSTR " proto=%04x",
652 0 : MAC2STR(data), MAC2STR(src),
653 0 : WPA_GET_BE16(data + 2 * ETH_ALEN));
654 0 : data += 14;
655 0 : datalen -= 14;
656 : }
657 :
658 : #ifdef HOSTAPD
659 0 : cli = test_driver_get_cli(drv, from, fromlen);
660 0 : if (cli) {
661 0 : drv_event_eapol_rx(cli->bss->bss_ctx, cli->addr, data,
662 : datalen);
663 : } else {
664 0 : wpa_printf(MSG_DEBUG, "test_socket: EAPOL from unknown "
665 : "client");
666 : }
667 : #else /* HOSTAPD */
668 : if (src)
669 : drv_event_eapol_rx(drv->ctx, src, data, datalen);
670 : #endif /* HOSTAPD */
671 0 : }
672 :
673 :
674 0 : static void test_driver_ether(struct wpa_driver_test_data *drv,
675 : struct sockaddr_un *from, socklen_t fromlen,
676 : u8 *data, size_t datalen)
677 : {
678 : struct l2_ethhdr *eth;
679 :
680 0 : if (datalen < sizeof(*eth))
681 0 : return;
682 :
683 0 : eth = (struct l2_ethhdr *) data;
684 0 : wpa_printf(MSG_DEBUG, "test_driver: RX ETHER dst=" MACSTR " src="
685 : MACSTR " proto=%04x",
686 0 : MAC2STR(eth->h_dest), MAC2STR(eth->h_source),
687 0 : be_to_host16(eth->h_proto));
688 :
689 : #ifdef CONFIG_IEEE80211R
690 0 : if (be_to_host16(eth->h_proto) == ETH_P_RRB) {
691 : union wpa_event_data ev;
692 0 : os_memset(&ev, 0, sizeof(ev));
693 0 : ev.ft_rrb_rx.src = eth->h_source;
694 0 : ev.ft_rrb_rx.data = data + sizeof(*eth);
695 0 : ev.ft_rrb_rx.data_len = datalen - sizeof(*eth);
696 : }
697 : #endif /* CONFIG_IEEE80211R */
698 : }
699 :
700 :
701 0 : static void test_driver_mlme(struct wpa_driver_test_data *drv,
702 : struct sockaddr_un *from, socklen_t fromlen,
703 : u8 *data, size_t datalen)
704 : {
705 : struct ieee80211_hdr *hdr;
706 : u16 fc;
707 : union wpa_event_data event;
708 0 : int freq = 0, own_freq;
709 : struct test_driver_bss *bss;
710 :
711 0 : bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
712 :
713 0 : if (datalen > 6 && os_memcmp(data, "freq=", 5) == 0) {
714 : size_t pos;
715 0 : for (pos = 5; pos < datalen; pos++) {
716 0 : if (data[pos] == ' ')
717 0 : break;
718 : }
719 0 : if (pos < datalen) {
720 0 : freq = atoi((const char *) &data[5]);
721 0 : wpa_printf(MSG_DEBUG, "test_driver(%s): MLME RX on "
722 0 : "freq %d MHz", bss->ifname, freq);
723 0 : pos++;
724 0 : data += pos;
725 0 : datalen -= pos;
726 : }
727 : }
728 :
729 0 : if (drv->remain_on_channel_freq)
730 0 : own_freq = drv->remain_on_channel_freq;
731 : else
732 0 : own_freq = drv->current_freq;
733 :
734 0 : if (freq && own_freq && freq != own_freq) {
735 0 : wpa_printf(MSG_DEBUG, "test_driver(%s): Ignore MLME RX on "
736 : "another frequency %d MHz (own %d MHz)",
737 0 : bss->ifname, freq, own_freq);
738 0 : return;
739 : }
740 :
741 0 : hdr = (struct ieee80211_hdr *) data;
742 :
743 0 : if (test_driver_get_cli(drv, from, fromlen) == NULL && datalen >= 16) {
744 : struct test_client_socket *cli;
745 0 : cli = os_zalloc(sizeof(*cli));
746 0 : if (cli == NULL)
747 0 : return;
748 0 : wpa_printf(MSG_DEBUG, "Adding client entry for " MACSTR,
749 0 : MAC2STR(hdr->addr2));
750 0 : memcpy(cli->addr, hdr->addr2, ETH_ALEN);
751 0 : memcpy(&cli->un, from, sizeof(cli->un));
752 0 : cli->unlen = fromlen;
753 0 : cli->next = drv->cli;
754 0 : drv->cli = cli;
755 : }
756 :
757 0 : wpa_hexdump(MSG_MSGDUMP, "test_driver_mlme: received frame",
758 : data, datalen);
759 0 : fc = le_to_host16(hdr->frame_control);
760 0 : if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT) {
761 0 : wpa_printf(MSG_ERROR, "%s: received non-mgmt frame",
762 : __func__);
763 0 : return;
764 : }
765 :
766 0 : os_memset(&event, 0, sizeof(event));
767 0 : event.rx_mgmt.frame = data;
768 0 : event.rx_mgmt.frame_len = datalen;
769 0 : wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
770 : }
771 :
772 :
773 0 : static void test_driver_receive_unix(int sock, void *eloop_ctx, void *sock_ctx)
774 : {
775 0 : struct wpa_driver_test_data *drv = eloop_ctx;
776 : char buf[2000];
777 : int res;
778 : struct sockaddr_un from;
779 0 : socklen_t fromlen = sizeof(from);
780 :
781 0 : res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
782 : (struct sockaddr *) &from, &fromlen);
783 0 : if (res < 0) {
784 0 : perror("recvfrom(test_socket)");
785 0 : return;
786 : }
787 0 : buf[res] = '\0';
788 :
789 0 : wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
790 :
791 0 : if (strncmp(buf, "SCAN", 4) == 0) {
792 0 : test_driver_scan(drv, &from, fromlen, buf + 4);
793 0 : } else if (strncmp(buf, "ASSOC ", 6) == 0) {
794 0 : test_driver_assoc(drv, &from, fromlen, buf + 6);
795 0 : } else if (strcmp(buf, "DISASSOC") == 0) {
796 0 : test_driver_disassoc(drv, &from, fromlen);
797 0 : } else if (strncmp(buf, "EAPOL ", 6) == 0) {
798 0 : test_driver_eapol(drv, &from, fromlen, (u8 *) buf + 6,
799 0 : res - 6);
800 0 : } else if (strncmp(buf, "ETHER ", 6) == 0) {
801 0 : test_driver_ether(drv, &from, fromlen, (u8 *) buf + 6,
802 0 : res - 6);
803 0 : } else if (strncmp(buf, "MLME ", 5) == 0) {
804 0 : test_driver_mlme(drv, &from, fromlen, (u8 *) buf + 5, res - 5);
805 : } else {
806 0 : wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
807 : (u8 *) buf, res);
808 : }
809 : }
810 :
811 :
812 0 : static int test_driver_set_generic_elem(void *priv,
813 : const u8 *elem, size_t elem_len)
814 : {
815 0 : struct test_driver_bss *bss = priv;
816 :
817 0 : os_free(bss->ie);
818 :
819 0 : if (elem == NULL) {
820 0 : bss->ie = NULL;
821 0 : bss->ielen = 0;
822 0 : return 0;
823 : }
824 :
825 0 : bss->ie = os_malloc(elem_len);
826 0 : if (bss->ie == NULL) {
827 0 : bss->ielen = 0;
828 0 : return -1;
829 : }
830 :
831 0 : memcpy(bss->ie, elem, elem_len);
832 0 : bss->ielen = elem_len;
833 0 : return 0;
834 : }
835 :
836 :
837 0 : static int test_driver_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
838 : const struct wpabuf *proberesp,
839 : const struct wpabuf *assocresp)
840 : {
841 0 : struct test_driver_bss *bss = priv;
842 :
843 0 : if (beacon == NULL)
844 0 : wpa_printf(MSG_DEBUG, "test_driver: Clear Beacon WPS IE");
845 : else
846 0 : wpa_hexdump_buf(MSG_DEBUG, "test_driver: Beacon WPS IE",
847 : beacon);
848 :
849 0 : os_free(bss->wps_beacon_ie);
850 :
851 0 : if (beacon == NULL) {
852 0 : bss->wps_beacon_ie = NULL;
853 0 : bss->wps_beacon_ie_len = 0;
854 : } else {
855 0 : bss->wps_beacon_ie = os_malloc(wpabuf_len(beacon));
856 0 : if (bss->wps_beacon_ie == NULL) {
857 0 : bss->wps_beacon_ie_len = 0;
858 0 : return -1;
859 : }
860 :
861 0 : os_memcpy(bss->wps_beacon_ie, wpabuf_head(beacon),
862 : wpabuf_len(beacon));
863 0 : bss->wps_beacon_ie_len = wpabuf_len(beacon);
864 : }
865 :
866 0 : if (proberesp == NULL)
867 0 : wpa_printf(MSG_DEBUG, "test_driver: Clear Probe Response WPS "
868 : "IE");
869 : else
870 0 : wpa_hexdump_buf(MSG_DEBUG, "test_driver: Probe Response WPS "
871 : "IE", proberesp);
872 :
873 0 : os_free(bss->wps_probe_resp_ie);
874 :
875 0 : if (proberesp == NULL) {
876 0 : bss->wps_probe_resp_ie = NULL;
877 0 : bss->wps_probe_resp_ie_len = 0;
878 : } else {
879 0 : bss->wps_probe_resp_ie = os_malloc(wpabuf_len(proberesp));
880 0 : if (bss->wps_probe_resp_ie == NULL) {
881 0 : bss->wps_probe_resp_ie_len = 0;
882 0 : return -1;
883 : }
884 :
885 0 : os_memcpy(bss->wps_probe_resp_ie, wpabuf_head(proberesp),
886 : wpabuf_len(proberesp));
887 0 : bss->wps_probe_resp_ie_len = wpabuf_len(proberesp);
888 : }
889 :
890 0 : return 0;
891 : }
892 :
893 :
894 0 : static int test_driver_sta_deauth(void *priv, const u8 *own_addr,
895 : const u8 *addr, int reason)
896 : {
897 0 : struct test_driver_bss *dbss = priv;
898 0 : struct wpa_driver_test_data *drv = dbss->drv;
899 : struct test_client_socket *cli;
900 :
901 0 : if (drv->test_socket < 0)
902 0 : return -1;
903 :
904 0 : cli = drv->cli;
905 0 : while (cli) {
906 0 : if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
907 0 : break;
908 0 : cli = cli->next;
909 : }
910 :
911 0 : if (!cli)
912 0 : return -1;
913 :
914 0 : return sendto(drv->test_socket, "DEAUTH", 6, 0,
915 0 : (struct sockaddr *) &cli->un, cli->unlen);
916 : }
917 :
918 :
919 0 : static int test_driver_sta_disassoc(void *priv, const u8 *own_addr,
920 : const u8 *addr, int reason)
921 : {
922 0 : struct test_driver_bss *dbss = priv;
923 0 : struct wpa_driver_test_data *drv = dbss->drv;
924 : struct test_client_socket *cli;
925 :
926 0 : if (drv->test_socket < 0)
927 0 : return -1;
928 :
929 0 : cli = drv->cli;
930 0 : while (cli) {
931 0 : if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
932 0 : break;
933 0 : cli = cli->next;
934 : }
935 :
936 0 : if (!cli)
937 0 : return -1;
938 :
939 0 : return sendto(drv->test_socket, "DISASSOC", 8, 0,
940 0 : (struct sockaddr *) &cli->un, cli->unlen);
941 : }
942 :
943 :
944 0 : static int test_driver_bss_add(void *priv, const char *ifname, const u8 *bssid,
945 : void *bss_ctx, void **drv_priv)
946 : {
947 0 : struct test_driver_bss *dbss = priv;
948 0 : struct wpa_driver_test_data *drv = dbss->drv;
949 : struct test_driver_bss *bss;
950 :
951 0 : wpa_printf(MSG_DEBUG, "%s(ifname=%s bssid=" MACSTR ")",
952 0 : __func__, ifname, MAC2STR(bssid));
953 :
954 0 : bss = os_zalloc(sizeof(*bss));
955 0 : if (bss == NULL)
956 0 : return -1;
957 :
958 0 : bss->bss_ctx = bss_ctx;
959 0 : bss->drv = drv;
960 0 : os_strlcpy(bss->ifname, ifname, IFNAMSIZ);
961 0 : os_memcpy(bss->bssid, bssid, ETH_ALEN);
962 :
963 0 : dl_list_add(&drv->bss, &bss->list);
964 0 : if (drv->global) {
965 0 : drv->global->bss_add_used = 1;
966 0 : os_memcpy(drv->global->req_addr, bssid, ETH_ALEN);
967 : }
968 :
969 0 : if (drv_priv)
970 0 : *drv_priv = bss;
971 :
972 0 : return 0;
973 : }
974 :
975 :
976 0 : static int test_driver_bss_remove(void *priv, const char *ifname)
977 : {
978 0 : struct test_driver_bss *dbss = priv;
979 0 : struct wpa_driver_test_data *drv = dbss->drv;
980 : struct test_driver_bss *bss;
981 : struct test_client_socket *cli, *prev_c;
982 :
983 0 : wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
984 :
985 0 : dl_list_for_each(bss, &drv->bss, struct test_driver_bss, list) {
986 0 : if (strcmp(bss->ifname, ifname) != 0)
987 0 : continue;
988 :
989 0 : for (prev_c = NULL, cli = drv->cli; cli;
990 0 : prev_c = cli, cli = cli->next) {
991 0 : if (cli->bss != bss)
992 0 : continue;
993 0 : if (prev_c)
994 0 : prev_c->next = cli->next;
995 : else
996 0 : drv->cli = cli->next;
997 0 : os_free(cli);
998 0 : break;
999 : }
1000 :
1001 0 : dl_list_del(&bss->list);
1002 0 : test_driver_free_bss(bss);
1003 0 : return 0;
1004 : }
1005 :
1006 0 : return -1;
1007 : }
1008 :
1009 :
1010 0 : static int test_driver_if_add(void *priv, enum wpa_driver_if_type type,
1011 : const char *ifname, const u8 *addr,
1012 : void *bss_ctx, void **drv_priv,
1013 : char *force_ifname, u8 *if_addr,
1014 : const char *bridge, int use_existing)
1015 : {
1016 0 : struct test_driver_bss *dbss = priv;
1017 0 : struct wpa_driver_test_data *drv = dbss->drv;
1018 :
1019 0 : wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s bss_ctx=%p)",
1020 : __func__, type, ifname, bss_ctx);
1021 0 : if (addr)
1022 0 : os_memcpy(if_addr, addr, ETH_ALEN);
1023 : else {
1024 0 : drv->alloc_iface_idx++;
1025 0 : if_addr[0] = 0x02; /* locally administered */
1026 0 : sha1_prf(drv->own_addr, ETH_ALEN,
1027 : "hostapd test addr generation",
1028 0 : (const u8 *) &drv->alloc_iface_idx,
1029 : sizeof(drv->alloc_iface_idx),
1030 : if_addr + 1, ETH_ALEN - 1);
1031 : }
1032 0 : if (type == WPA_IF_AP_BSS || type == WPA_IF_P2P_GO ||
1033 0 : type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP)
1034 0 : return test_driver_bss_add(priv, ifname, if_addr, bss_ctx,
1035 : drv_priv);
1036 0 : return 0;
1037 : }
1038 :
1039 :
1040 0 : static int test_driver_if_remove(void *priv, enum wpa_driver_if_type type,
1041 : const char *ifname)
1042 : {
1043 0 : wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
1044 0 : if (type == WPA_IF_AP_BSS || type == WPA_IF_P2P_GO ||
1045 0 : type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP)
1046 0 : return test_driver_bss_remove(priv, ifname);
1047 0 : return 0;
1048 : }
1049 :
1050 :
1051 0 : static int test_driver_set_ssid(void *priv, const u8 *buf, int len)
1052 : {
1053 0 : struct test_driver_bss *bss = priv;
1054 :
1055 0 : wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, bss->ifname);
1056 0 : if (len < 0)
1057 0 : return -1;
1058 0 : wpa_hexdump_ascii(MSG_DEBUG, "test_driver_set_ssid: SSID", buf, len);
1059 :
1060 0 : if ((size_t) len > sizeof(bss->ssid))
1061 0 : return -1;
1062 :
1063 0 : os_memcpy(bss->ssid, buf, len);
1064 0 : bss->ssid_len = len;
1065 :
1066 0 : return 0;
1067 : }
1068 :
1069 :
1070 0 : static int test_driver_set_privacy(void *priv, int enabled)
1071 : {
1072 0 : struct test_driver_bss *dbss = priv;
1073 :
1074 0 : wpa_printf(MSG_DEBUG, "%s(enabled=%d)", __func__, enabled);
1075 0 : dbss->privacy = enabled;
1076 :
1077 0 : return 0;
1078 : }
1079 :
1080 :
1081 0 : static int test_driver_set_sta_vlan(void *priv, const u8 *addr,
1082 : const char *ifname, int vlan_id)
1083 : {
1084 0 : wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " ifname=%s vlan_id=%d)",
1085 0 : __func__, MAC2STR(addr), ifname, vlan_id);
1086 0 : return 0;
1087 : }
1088 :
1089 :
1090 0 : static int test_driver_sta_add(void *priv,
1091 : struct hostapd_sta_add_params *params)
1092 : {
1093 0 : struct test_driver_bss *bss = priv;
1094 0 : struct wpa_driver_test_data *drv = bss->drv;
1095 : struct test_client_socket *cli;
1096 :
1097 0 : wpa_printf(MSG_DEBUG, "%s(ifname=%s addr=" MACSTR " aid=%d "
1098 : "capability=0x%x listen_interval=%d)",
1099 0 : __func__, bss->ifname, MAC2STR(params->addr), params->aid,
1100 0 : params->capability, params->listen_interval);
1101 0 : wpa_hexdump(MSG_DEBUG, "test_driver_sta_add - supp_rates",
1102 0 : params->supp_rates, params->supp_rates_len);
1103 :
1104 0 : cli = drv->cli;
1105 0 : while (cli) {
1106 0 : if (os_memcmp(cli->addr, params->addr, ETH_ALEN) == 0)
1107 0 : break;
1108 0 : cli = cli->next;
1109 : }
1110 0 : if (!cli) {
1111 0 : wpa_printf(MSG_DEBUG, "%s: no matching client entry",
1112 : __func__);
1113 0 : return -1;
1114 : }
1115 :
1116 0 : cli->bss = bss;
1117 :
1118 0 : return 0;
1119 : }
1120 :
1121 :
1122 0 : static struct wpa_driver_test_data * test_alloc_data(void *ctx,
1123 : const char *ifname)
1124 : {
1125 : struct wpa_driver_test_data *drv;
1126 : struct test_driver_bss *bss;
1127 :
1128 0 : drv = os_zalloc(sizeof(struct wpa_driver_test_data));
1129 0 : if (drv == NULL) {
1130 0 : wpa_printf(MSG_ERROR, "Could not allocate memory for test "
1131 : "driver data");
1132 0 : return NULL;
1133 : }
1134 :
1135 0 : bss = os_zalloc(sizeof(struct test_driver_bss));
1136 0 : if (bss == NULL) {
1137 0 : os_free(drv);
1138 0 : return NULL;
1139 : }
1140 :
1141 0 : drv->ctx = ctx;
1142 0 : wpa_trace_add_ref(drv, ctx, ctx);
1143 0 : dl_list_init(&drv->bss);
1144 0 : dl_list_add(&drv->bss, &bss->list);
1145 0 : os_strlcpy(bss->ifname, ifname, IFNAMSIZ);
1146 0 : bss->bss_ctx = ctx;
1147 0 : bss->drv = drv;
1148 :
1149 : /* Generate a MAC address to help testing with multiple STAs */
1150 0 : drv->own_addr[0] = 0x02; /* locally administered */
1151 0 : sha1_prf((const u8 *) ifname, os_strlen(ifname),
1152 : "test mac addr generation",
1153 : NULL, 0, drv->own_addr + 1, ETH_ALEN - 1);
1154 :
1155 0 : return drv;
1156 : }
1157 :
1158 :
1159 0 : static void * test_driver_init(struct hostapd_data *hapd,
1160 : struct wpa_init_params *params)
1161 : {
1162 : struct wpa_driver_test_data *drv;
1163 : struct sockaddr_un addr_un;
1164 : struct sockaddr_in addr_in;
1165 : struct sockaddr *addr;
1166 : socklen_t alen;
1167 : struct test_driver_bss *bss;
1168 :
1169 0 : drv = test_alloc_data(hapd, params->ifname);
1170 0 : if (drv == NULL)
1171 0 : return NULL;
1172 0 : drv->ap = 1;
1173 0 : bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
1174 0 : drv->global = params->global_priv;
1175 :
1176 0 : bss->bss_ctx = hapd;
1177 0 : os_memcpy(bss->bssid, drv->own_addr, ETH_ALEN);
1178 0 : os_memcpy(params->own_addr, drv->own_addr, ETH_ALEN);
1179 :
1180 0 : if (params->test_socket) {
1181 0 : if (os_strlen(params->test_socket) >=
1182 : sizeof(addr_un.sun_path)) {
1183 0 : printf("Too long test_socket path\n");
1184 0 : wpa_driver_test_deinit(bss);
1185 0 : return NULL;
1186 : }
1187 0 : if (strncmp(params->test_socket, "DIR:", 4) == 0) {
1188 0 : size_t len = strlen(params->test_socket) + 30;
1189 0 : drv->test_dir = os_strdup(params->test_socket + 4);
1190 0 : drv->own_socket_path = os_malloc(len);
1191 0 : if (drv->own_socket_path) {
1192 0 : snprintf(drv->own_socket_path, len,
1193 : "%s/AP-" MACSTR,
1194 0 : params->test_socket + 4,
1195 0 : MAC2STR(params->own_addr));
1196 : }
1197 0 : } else if (strncmp(params->test_socket, "UDP:", 4) == 0) {
1198 0 : drv->udp_port = atoi(params->test_socket + 4);
1199 : } else {
1200 0 : drv->own_socket_path = os_strdup(params->test_socket);
1201 : }
1202 0 : if (drv->own_socket_path == NULL && drv->udp_port == 0) {
1203 0 : wpa_driver_test_deinit(bss);
1204 0 : return NULL;
1205 : }
1206 :
1207 0 : drv->test_socket = socket(drv->udp_port ? PF_INET : PF_UNIX,
1208 : SOCK_DGRAM, 0);
1209 0 : if (drv->test_socket < 0) {
1210 0 : perror("socket");
1211 0 : wpa_driver_test_deinit(bss);
1212 0 : return NULL;
1213 : }
1214 :
1215 0 : if (drv->udp_port) {
1216 0 : os_memset(&addr_in, 0, sizeof(addr_in));
1217 0 : addr_in.sin_family = AF_INET;
1218 0 : addr_in.sin_port = htons(drv->udp_port);
1219 0 : addr = (struct sockaddr *) &addr_in;
1220 0 : alen = sizeof(addr_in);
1221 : } else {
1222 0 : os_memset(&addr_un, 0, sizeof(addr_un));
1223 0 : addr_un.sun_family = AF_UNIX;
1224 0 : os_strlcpy(addr_un.sun_path, drv->own_socket_path,
1225 : sizeof(addr_un.sun_path));
1226 0 : addr = (struct sockaddr *) &addr_un;
1227 0 : alen = sizeof(addr_un);
1228 : }
1229 0 : if (bind(drv->test_socket, addr, alen) < 0) {
1230 0 : perror("test-driver-init: bind(PF_UNIX)");
1231 0 : close(drv->test_socket);
1232 0 : if (drv->own_socket_path)
1233 0 : unlink(drv->own_socket_path);
1234 0 : wpa_driver_test_deinit(bss);
1235 0 : return NULL;
1236 : }
1237 0 : eloop_register_read_sock(drv->test_socket,
1238 : test_driver_receive_unix, drv, NULL);
1239 : } else
1240 0 : drv->test_socket = -1;
1241 :
1242 0 : return bss;
1243 : }
1244 :
1245 :
1246 0 : static void wpa_driver_test_poll(void *eloop_ctx, void *timeout_ctx)
1247 : {
1248 0 : struct wpa_driver_test_data *drv = eloop_ctx;
1249 :
1250 : #ifdef DRIVER_TEST_UNIX
1251 0 : if (drv->associated && drv->hostapd_addr_set) {
1252 : struct stat st;
1253 0 : if (stat(drv->hostapd_addr.sun_path, &st) < 0) {
1254 0 : wpa_printf(MSG_DEBUG, "%s: lost connection to AP: %s",
1255 0 : __func__, strerror(errno));
1256 0 : drv->associated = 0;
1257 0 : wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1258 : }
1259 : }
1260 : #endif /* DRIVER_TEST_UNIX */
1261 :
1262 0 : eloop_register_timeout(1, 0, wpa_driver_test_poll, drv, NULL);
1263 0 : }
1264 :
1265 :
1266 0 : static void wpa_driver_test_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1267 : {
1268 0 : wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
1269 0 : wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
1270 0 : }
1271 :
1272 :
1273 : #ifdef DRIVER_TEST_UNIX
1274 0 : static void wpa_driver_scan_dir(struct wpa_driver_test_data *drv,
1275 : const char *path)
1276 : {
1277 : struct dirent *dent;
1278 : DIR *dir;
1279 : struct sockaddr_un addr;
1280 : char cmd[512], *pos, *end;
1281 : int ret;
1282 :
1283 0 : dir = opendir(path);
1284 0 : if (dir == NULL)
1285 0 : return;
1286 :
1287 0 : end = cmd + sizeof(cmd);
1288 0 : pos = cmd;
1289 0 : ret = os_snprintf(pos, end - pos, "SCAN " MACSTR,
1290 0 : MAC2STR(drv->own_addr));
1291 0 : if (ret >= 0 && ret < end - pos)
1292 0 : pos += ret;
1293 0 : if (drv->probe_req_ie) {
1294 0 : ret = os_snprintf(pos, end - pos, " ");
1295 0 : if (ret >= 0 && ret < end - pos)
1296 0 : pos += ret;
1297 0 : pos += wpa_snprintf_hex(pos, end - pos, drv->probe_req_ie,
1298 : drv->probe_req_ie_len);
1299 : }
1300 0 : if (drv->probe_req_ssid_len) {
1301 : /* Add SSID IE */
1302 0 : ret = os_snprintf(pos, end - pos, "%02x%02x",
1303 : WLAN_EID_SSID,
1304 0 : (unsigned int) drv->probe_req_ssid_len);
1305 0 : if (ret >= 0 && ret < end - pos)
1306 0 : pos += ret;
1307 0 : pos += wpa_snprintf_hex(pos, end - pos, drv->probe_req_ssid,
1308 : drv->probe_req_ssid_len);
1309 : }
1310 0 : end[-1] = '\0';
1311 :
1312 0 : while ((dent = readdir(dir))) {
1313 0 : if (os_strncmp(dent->d_name, "AP-", 3) != 0 &&
1314 0 : os_strncmp(dent->d_name, "STA-", 4) != 0)
1315 0 : continue;
1316 0 : if (drv->own_socket_path) {
1317 : size_t olen, dlen;
1318 0 : olen = os_strlen(drv->own_socket_path);
1319 0 : dlen = os_strlen(dent->d_name);
1320 0 : if (olen >= dlen &&
1321 0 : os_strcmp(dent->d_name,
1322 : drv->own_socket_path + olen - dlen) == 0)
1323 0 : continue;
1324 : }
1325 0 : wpa_printf(MSG_DEBUG, "%s: SCAN %s", __func__, dent->d_name);
1326 :
1327 0 : os_memset(&addr, 0, sizeof(addr));
1328 0 : addr.sun_family = AF_UNIX;
1329 0 : os_snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
1330 0 : path, dent->d_name);
1331 :
1332 0 : if (sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
1333 : (struct sockaddr *) &addr, sizeof(addr)) < 0) {
1334 0 : perror("sendto(test_socket)");
1335 : }
1336 : }
1337 0 : closedir(dir);
1338 : }
1339 : #endif /* DRIVER_TEST_UNIX */
1340 :
1341 :
1342 0 : static int wpa_driver_test_scan(void *priv,
1343 : struct wpa_driver_scan_params *params)
1344 : {
1345 0 : struct test_driver_bss *dbss = priv;
1346 0 : struct wpa_driver_test_data *drv = dbss->drv;
1347 : size_t i;
1348 :
1349 0 : wpa_printf(MSG_DEBUG, "%s: priv=%p", __func__, priv);
1350 :
1351 0 : os_free(drv->probe_req_ie);
1352 0 : if (params->extra_ies) {
1353 0 : drv->probe_req_ie = os_malloc(params->extra_ies_len);
1354 0 : if (drv->probe_req_ie == NULL) {
1355 0 : drv->probe_req_ie_len = 0;
1356 0 : return -1;
1357 : }
1358 0 : os_memcpy(drv->probe_req_ie, params->extra_ies,
1359 : params->extra_ies_len);
1360 0 : drv->probe_req_ie_len = params->extra_ies_len;
1361 : } else {
1362 0 : drv->probe_req_ie = NULL;
1363 0 : drv->probe_req_ie_len = 0;
1364 : }
1365 :
1366 0 : for (i = 0; i < params->num_ssids; i++)
1367 0 : wpa_hexdump(MSG_DEBUG, "Scan SSID",
1368 0 : params->ssids[i].ssid, params->ssids[i].ssid_len);
1369 0 : drv->probe_req_ssid_len = 0;
1370 0 : if (params->num_ssids) {
1371 0 : os_memcpy(drv->probe_req_ssid, params->ssids[0].ssid,
1372 : params->ssids[0].ssid_len);
1373 0 : drv->probe_req_ssid_len = params->ssids[0].ssid_len;
1374 : }
1375 0 : wpa_hexdump(MSG_DEBUG, "Scan extra IE(s)",
1376 0 : params->extra_ies, params->extra_ies_len);
1377 :
1378 0 : drv->num_scanres = 0;
1379 :
1380 : #ifdef DRIVER_TEST_UNIX
1381 0 : if (drv->test_socket >= 0 && drv->test_dir)
1382 0 : wpa_driver_scan_dir(drv, drv->test_dir);
1383 :
1384 0 : if (drv->test_socket >= 0 && drv->hostapd_addr_set &&
1385 0 : sendto(drv->test_socket, "SCAN", 4, 0,
1386 0 : (struct sockaddr *) &drv->hostapd_addr,
1387 : sizeof(drv->hostapd_addr)) < 0) {
1388 0 : perror("sendto(test_socket)");
1389 : }
1390 : #endif /* DRIVER_TEST_UNIX */
1391 :
1392 0 : if (drv->test_socket >= 0 && drv->hostapd_addr_udp_set &&
1393 0 : sendto(drv->test_socket, "SCAN", 4, 0,
1394 0 : (struct sockaddr *) &drv->hostapd_addr_udp,
1395 : sizeof(drv->hostapd_addr_udp)) < 0) {
1396 0 : perror("sendto(test_socket)");
1397 : }
1398 :
1399 0 : eloop_cancel_timeout(wpa_driver_test_scan_timeout, drv, drv->ctx);
1400 0 : eloop_register_timeout(1, 0, wpa_driver_test_scan_timeout, drv,
1401 : drv->ctx);
1402 0 : return 0;
1403 : }
1404 :
1405 :
1406 0 : static struct wpa_scan_results * wpa_driver_test_get_scan_results2(void *priv)
1407 : {
1408 0 : struct test_driver_bss *dbss = priv;
1409 0 : struct wpa_driver_test_data *drv = dbss->drv;
1410 : struct wpa_scan_results *res;
1411 : size_t i;
1412 :
1413 0 : res = os_zalloc(sizeof(*res));
1414 0 : if (res == NULL)
1415 0 : return NULL;
1416 :
1417 0 : res->res = os_calloc(drv->num_scanres, sizeof(struct wpa_scan_res *));
1418 0 : if (res->res == NULL) {
1419 0 : os_free(res);
1420 0 : return NULL;
1421 : }
1422 :
1423 0 : for (i = 0; i < drv->num_scanres; i++) {
1424 : struct wpa_scan_res *r;
1425 0 : if (drv->scanres[i] == NULL)
1426 0 : continue;
1427 0 : r = os_malloc(sizeof(*r) + drv->scanres[i]->ie_len);
1428 0 : if (r == NULL)
1429 0 : break;
1430 0 : os_memcpy(r, drv->scanres[i],
1431 : sizeof(*r) + drv->scanres[i]->ie_len);
1432 0 : res->res[res->num++] = r;
1433 : }
1434 :
1435 0 : return res;
1436 : }
1437 :
1438 :
1439 0 : static int wpa_driver_test_set_key(const char *ifname, void *priv,
1440 : enum wpa_alg alg, const u8 *addr,
1441 : int key_idx, int set_tx,
1442 : const u8 *seq, size_t seq_len,
1443 : const u8 *key, size_t key_len)
1444 : {
1445 0 : wpa_printf(MSG_DEBUG, "%s: ifname=%s priv=%p alg=%d key_idx=%d "
1446 : "set_tx=%d",
1447 : __func__, ifname, priv, alg, key_idx, set_tx);
1448 0 : if (addr)
1449 0 : wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
1450 0 : if (seq)
1451 0 : wpa_hexdump(MSG_DEBUG, " seq", seq, seq_len);
1452 0 : if (key)
1453 0 : wpa_hexdump_key(MSG_DEBUG, " key", key, key_len);
1454 0 : return 0;
1455 : }
1456 :
1457 :
1458 0 : static int wpa_driver_update_mode(struct wpa_driver_test_data *drv, int ap)
1459 : {
1460 0 : if (ap && !drv->ap) {
1461 0 : wpa_driver_test_close_test_socket(drv);
1462 0 : wpa_driver_test_attach(drv, drv->test_dir, 1);
1463 0 : drv->ap = 1;
1464 0 : } else if (!ap && drv->ap) {
1465 0 : wpa_driver_test_close_test_socket(drv);
1466 0 : wpa_driver_test_attach(drv, drv->test_dir, 0);
1467 0 : drv->ap = 0;
1468 : }
1469 :
1470 0 : return 0;
1471 : }
1472 :
1473 :
1474 0 : static int wpa_driver_test_associate(
1475 : void *priv, struct wpa_driver_associate_params *params)
1476 : {
1477 0 : struct test_driver_bss *dbss = priv;
1478 0 : struct wpa_driver_test_data *drv = dbss->drv;
1479 0 : wpa_printf(MSG_DEBUG, "%s: priv=%p freq=%d pairwise_suite=%d "
1480 : "group_suite=%d key_mgmt_suite=%d auth_alg=%d mode=%d",
1481 : __func__, priv, params->freq, params->pairwise_suite,
1482 : params->group_suite, params->key_mgmt_suite,
1483 : params->auth_alg, params->mode);
1484 0 : wpa_driver_update_mode(drv, params->mode == IEEE80211_MODE_AP);
1485 0 : if (params->bssid) {
1486 0 : wpa_printf(MSG_DEBUG, " bssid=" MACSTR,
1487 0 : MAC2STR(params->bssid));
1488 : }
1489 0 : if (params->ssid) {
1490 0 : wpa_hexdump_ascii(MSG_DEBUG, " ssid",
1491 0 : params->ssid, params->ssid_len);
1492 : }
1493 0 : if (params->wpa_ie) {
1494 0 : wpa_hexdump(MSG_DEBUG, " wpa_ie",
1495 0 : params->wpa_ie, params->wpa_ie_len);
1496 0 : drv->assoc_wpa_ie_len = params->wpa_ie_len;
1497 0 : if (drv->assoc_wpa_ie_len > sizeof(drv->assoc_wpa_ie))
1498 0 : drv->assoc_wpa_ie_len = sizeof(drv->assoc_wpa_ie);
1499 0 : os_memcpy(drv->assoc_wpa_ie, params->wpa_ie,
1500 : drv->assoc_wpa_ie_len);
1501 : } else
1502 0 : drv->assoc_wpa_ie_len = 0;
1503 :
1504 0 : wpa_driver_update_mode(drv, params->mode == IEEE80211_MODE_AP);
1505 :
1506 0 : drv->ibss = params->mode == IEEE80211_MODE_IBSS;
1507 0 : dbss->privacy = params->key_mgmt_suite &
1508 : (WPA_KEY_MGMT_IEEE8021X |
1509 : WPA_KEY_MGMT_PSK |
1510 : WPA_KEY_MGMT_WPA_NONE |
1511 : WPA_KEY_MGMT_FT_IEEE8021X |
1512 : WPA_KEY_MGMT_FT_PSK |
1513 : WPA_KEY_MGMT_IEEE8021X_SHA256 |
1514 : WPA_KEY_MGMT_PSK_SHA256);
1515 0 : if (params->wep_key_len[params->wep_tx_keyidx])
1516 0 : dbss->privacy = 1;
1517 :
1518 : #ifdef DRIVER_TEST_UNIX
1519 0 : if (drv->test_dir && params->bssid &&
1520 0 : params->mode != IEEE80211_MODE_IBSS) {
1521 0 : os_memset(&drv->hostapd_addr, 0, sizeof(drv->hostapd_addr));
1522 0 : drv->hostapd_addr.sun_family = AF_UNIX;
1523 0 : os_snprintf(drv->hostapd_addr.sun_path,
1524 : sizeof(drv->hostapd_addr.sun_path),
1525 : "%s/AP-" MACSTR,
1526 0 : drv->test_dir, MAC2STR(params->bssid));
1527 0 : drv->hostapd_addr_set = 1;
1528 : }
1529 : #endif /* DRIVER_TEST_UNIX */
1530 :
1531 0 : if (params->mode == IEEE80211_MODE_AP) {
1532 0 : if (params->ssid)
1533 0 : os_memcpy(dbss->ssid, params->ssid, params->ssid_len);
1534 0 : dbss->ssid_len = params->ssid_len;
1535 0 : os_memcpy(dbss->bssid, drv->own_addr, ETH_ALEN);
1536 0 : if (params->wpa_ie && params->wpa_ie_len) {
1537 0 : dbss->ie = os_malloc(params->wpa_ie_len);
1538 0 : if (dbss->ie) {
1539 0 : os_memcpy(dbss->ie, params->wpa_ie,
1540 : params->wpa_ie_len);
1541 0 : dbss->ielen = params->wpa_ie_len;
1542 : }
1543 : }
1544 0 : } else if (drv->test_socket >= 0 &&
1545 0 : (drv->hostapd_addr_set || drv->hostapd_addr_udp_set)) {
1546 : char cmd[200], *pos, *end;
1547 : int ret;
1548 0 : end = cmd + sizeof(cmd);
1549 0 : pos = cmd;
1550 0 : ret = os_snprintf(pos, end - pos, "ASSOC " MACSTR " ",
1551 0 : MAC2STR(drv->own_addr));
1552 0 : if (ret >= 0 && ret < end - pos)
1553 0 : pos += ret;
1554 0 : if (params->ssid)
1555 0 : pos += wpa_snprintf_hex(pos, end - pos, params->ssid,
1556 : params->ssid_len);
1557 0 : ret = os_snprintf(pos, end - pos, " ");
1558 0 : if (ret >= 0 && ret < end - pos)
1559 0 : pos += ret;
1560 0 : pos += wpa_snprintf_hex(pos, end - pos, params->wpa_ie,
1561 : params->wpa_ie_len);
1562 0 : end[-1] = '\0';
1563 : #ifdef DRIVER_TEST_UNIX
1564 0 : if (drv->hostapd_addr_set &&
1565 0 : sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
1566 0 : (struct sockaddr *) &drv->hostapd_addr,
1567 : sizeof(drv->hostapd_addr)) < 0) {
1568 0 : perror("sendto(test_socket)");
1569 0 : return -1;
1570 : }
1571 : #endif /* DRIVER_TEST_UNIX */
1572 0 : if (drv->hostapd_addr_udp_set &&
1573 0 : sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
1574 0 : (struct sockaddr *) &drv->hostapd_addr_udp,
1575 : sizeof(drv->hostapd_addr_udp)) < 0) {
1576 0 : perror("sendto(test_socket)");
1577 0 : return -1;
1578 : }
1579 :
1580 0 : if (params->ssid)
1581 0 : os_memcpy(dbss->ssid, params->ssid, params->ssid_len);
1582 0 : dbss->ssid_len = params->ssid_len;
1583 : } else {
1584 0 : drv->associated = 1;
1585 0 : if (params->mode == IEEE80211_MODE_IBSS) {
1586 0 : if (params->ssid)
1587 0 : os_memcpy(dbss->ssid, params->ssid,
1588 : params->ssid_len);
1589 0 : dbss->ssid_len = params->ssid_len;
1590 0 : if (params->bssid)
1591 0 : os_memcpy(dbss->bssid, params->bssid,
1592 : ETH_ALEN);
1593 : else {
1594 0 : os_get_random(dbss->bssid, ETH_ALEN);
1595 0 : dbss->bssid[0] &= ~0x01;
1596 0 : dbss->bssid[0] |= 0x02;
1597 : }
1598 : }
1599 0 : wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1600 : }
1601 :
1602 0 : return 0;
1603 : }
1604 :
1605 :
1606 0 : static int wpa_driver_test_get_bssid(void *priv, u8 *bssid)
1607 : {
1608 0 : struct test_driver_bss *dbss = priv;
1609 0 : os_memcpy(bssid, dbss->bssid, ETH_ALEN);
1610 0 : return 0;
1611 : }
1612 :
1613 :
1614 0 : static int wpa_driver_test_get_ssid(void *priv, u8 *ssid)
1615 : {
1616 0 : struct test_driver_bss *dbss = priv;
1617 0 : os_memcpy(ssid, dbss->ssid, 32);
1618 0 : return dbss->ssid_len;
1619 : }
1620 :
1621 :
1622 0 : static int wpa_driver_test_send_disassoc(struct wpa_driver_test_data *drv)
1623 : {
1624 : #ifdef DRIVER_TEST_UNIX
1625 0 : if (drv->test_socket >= 0 &&
1626 0 : sendto(drv->test_socket, "DISASSOC", 8, 0,
1627 0 : (struct sockaddr *) &drv->hostapd_addr,
1628 : sizeof(drv->hostapd_addr)) < 0) {
1629 0 : perror("sendto(test_socket)");
1630 0 : return -1;
1631 : }
1632 : #endif /* DRIVER_TEST_UNIX */
1633 0 : if (drv->test_socket >= 0 && drv->hostapd_addr_udp_set &&
1634 0 : sendto(drv->test_socket, "DISASSOC", 8, 0,
1635 0 : (struct sockaddr *) &drv->hostapd_addr_udp,
1636 : sizeof(drv->hostapd_addr_udp)) < 0) {
1637 0 : perror("sendto(test_socket)");
1638 0 : return -1;
1639 : }
1640 0 : return 0;
1641 : }
1642 :
1643 :
1644 0 : static int wpa_driver_test_deauthenticate(void *priv, const u8 *addr,
1645 : int reason_code)
1646 : {
1647 0 : struct test_driver_bss *dbss = priv;
1648 0 : struct wpa_driver_test_data *drv = dbss->drv;
1649 0 : wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d",
1650 0 : __func__, MAC2STR(addr), reason_code);
1651 0 : os_memset(dbss->bssid, 0, ETH_ALEN);
1652 0 : drv->associated = 0;
1653 0 : wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1654 0 : return wpa_driver_test_send_disassoc(drv);
1655 : }
1656 :
1657 :
1658 0 : static const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1659 : {
1660 : const u8 *end, *pos;
1661 :
1662 0 : pos = (const u8 *) (res + 1);
1663 0 : end = pos + res->ie_len;
1664 :
1665 0 : while (pos + 1 < end) {
1666 0 : if (pos + 2 + pos[1] > end)
1667 0 : break;
1668 0 : if (pos[0] == ie)
1669 0 : return pos;
1670 0 : pos += 2 + pos[1];
1671 : }
1672 :
1673 0 : return NULL;
1674 : }
1675 :
1676 :
1677 0 : static void wpa_driver_test_scanresp(struct wpa_driver_test_data *drv,
1678 : struct sockaddr *from,
1679 : socklen_t fromlen,
1680 : const char *data)
1681 : {
1682 : struct wpa_scan_res *res;
1683 : const char *pos, *pos2;
1684 : size_t len;
1685 : u8 *ie_pos, *ie_start, *ie_end;
1686 : #define MAX_IE_LEN 1000
1687 : const u8 *ds_params;
1688 :
1689 0 : wpa_printf(MSG_DEBUG, "test_driver: SCANRESP %s", data);
1690 0 : if (drv->num_scanres >= MAX_SCAN_RESULTS) {
1691 0 : wpa_printf(MSG_DEBUG, "test_driver: No room for the new scan "
1692 : "result");
1693 0 : return;
1694 : }
1695 :
1696 : /* SCANRESP BSSID SSID IEs */
1697 :
1698 0 : res = os_zalloc(sizeof(*res) + MAX_IE_LEN);
1699 0 : if (res == NULL)
1700 0 : return;
1701 0 : ie_start = ie_pos = (u8 *) (res + 1);
1702 0 : ie_end = ie_pos + MAX_IE_LEN;
1703 :
1704 0 : if (hwaddr_aton(data, res->bssid)) {
1705 0 : wpa_printf(MSG_DEBUG, "test_driver: invalid BSSID in scanres");
1706 0 : os_free(res);
1707 0 : return;
1708 : }
1709 :
1710 0 : pos = data + 17;
1711 0 : while (*pos == ' ')
1712 0 : pos++;
1713 0 : pos2 = os_strchr(pos, ' ');
1714 0 : if (pos2 == NULL) {
1715 0 : wpa_printf(MSG_DEBUG, "test_driver: invalid SSID termination "
1716 : "in scanres");
1717 0 : os_free(res);
1718 0 : return;
1719 : }
1720 0 : len = (pos2 - pos) / 2;
1721 0 : if (len > 32)
1722 0 : len = 32;
1723 : /*
1724 : * Generate SSID IE from the SSID field since this IE is not included
1725 : * in the main IE field.
1726 : */
1727 0 : *ie_pos++ = WLAN_EID_SSID;
1728 0 : *ie_pos++ = len;
1729 0 : if (hexstr2bin(pos, ie_pos, len) < 0) {
1730 0 : wpa_printf(MSG_DEBUG, "test_driver: invalid SSID in scanres");
1731 0 : os_free(res);
1732 0 : return;
1733 : }
1734 0 : ie_pos += len;
1735 :
1736 0 : pos = pos2 + 1;
1737 0 : pos2 = os_strchr(pos, ' ');
1738 0 : if (pos2 == NULL)
1739 0 : len = os_strlen(pos) / 2;
1740 : else
1741 0 : len = (pos2 - pos) / 2;
1742 0 : if ((int) len > ie_end - ie_pos)
1743 0 : len = ie_end - ie_pos;
1744 0 : if (hexstr2bin(pos, ie_pos, len) < 0) {
1745 0 : wpa_printf(MSG_DEBUG, "test_driver: invalid IEs in scanres");
1746 0 : os_free(res);
1747 0 : return;
1748 : }
1749 0 : ie_pos += len;
1750 0 : res->ie_len = ie_pos - ie_start;
1751 :
1752 0 : if (pos2) {
1753 0 : pos = pos2 + 1;
1754 0 : while (*pos == ' ')
1755 0 : pos++;
1756 0 : if (os_strstr(pos, "PRIVACY"))
1757 0 : res->caps |= IEEE80211_CAP_PRIVACY;
1758 0 : if (os_strstr(pos, "IBSS"))
1759 0 : res->caps |= IEEE80211_CAP_IBSS;
1760 : }
1761 :
1762 0 : ds_params = wpa_scan_get_ie(res, WLAN_EID_DS_PARAMS);
1763 0 : if (ds_params && ds_params[1] > 0) {
1764 0 : if (ds_params[2] >= 1 && ds_params[2] <= 13)
1765 0 : res->freq = 2407 + ds_params[2] * 5;
1766 : }
1767 :
1768 0 : os_free(drv->scanres[drv->num_scanres]);
1769 0 : drv->scanres[drv->num_scanres++] = res;
1770 : }
1771 :
1772 :
1773 0 : static void wpa_driver_test_assocresp(struct wpa_driver_test_data *drv,
1774 : struct sockaddr *from,
1775 : socklen_t fromlen,
1776 : const char *data)
1777 : {
1778 : struct test_driver_bss *bss;
1779 :
1780 0 : bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
1781 :
1782 : /* ASSOCRESP BSSID <res> */
1783 0 : if (hwaddr_aton(data, bss->bssid)) {
1784 0 : wpa_printf(MSG_DEBUG, "test_driver: invalid BSSID in "
1785 : "assocresp");
1786 : }
1787 0 : if (drv->use_associnfo) {
1788 : union wpa_event_data event;
1789 0 : os_memset(&event, 0, sizeof(event));
1790 0 : event.assoc_info.req_ies = drv->assoc_wpa_ie;
1791 0 : event.assoc_info.req_ies_len = drv->assoc_wpa_ie_len;
1792 0 : wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &event);
1793 : }
1794 0 : drv->associated = 1;
1795 0 : wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1796 0 : }
1797 :
1798 :
1799 0 : static void wpa_driver_test_disassoc(struct wpa_driver_test_data *drv,
1800 : struct sockaddr *from,
1801 : socklen_t fromlen)
1802 : {
1803 0 : drv->associated = 0;
1804 0 : wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
1805 0 : }
1806 :
1807 :
1808 0 : static void wpa_driver_test_eapol(struct wpa_driver_test_data *drv,
1809 : struct sockaddr *from,
1810 : socklen_t fromlen,
1811 : const u8 *data, size_t data_len)
1812 : {
1813 : const u8 *src;
1814 : struct test_driver_bss *bss;
1815 :
1816 0 : bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
1817 :
1818 0 : if (data_len > 14) {
1819 : /* Skip Ethernet header */
1820 0 : src = data + ETH_ALEN;
1821 0 : data += 14;
1822 0 : data_len -= 14;
1823 : } else
1824 0 : src = bss->bssid;
1825 :
1826 0 : drv_event_eapol_rx(drv->ctx, src, data, data_len);
1827 0 : }
1828 :
1829 :
1830 0 : static void wpa_driver_test_mlme(struct wpa_driver_test_data *drv,
1831 : struct sockaddr *from,
1832 : socklen_t fromlen,
1833 : const u8 *data, size_t data_len)
1834 : {
1835 0 : int freq = 0, own_freq;
1836 : union wpa_event_data event;
1837 : const struct ieee80211_mgmt *mgmt;
1838 : u16 fc;
1839 : struct test_driver_bss *bss;
1840 :
1841 0 : bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
1842 0 : if (data_len > 6 && os_memcmp(data, "freq=", 5) == 0) {
1843 : size_t pos;
1844 0 : for (pos = 5; pos < data_len; pos++) {
1845 0 : if (data[pos] == ' ')
1846 0 : break;
1847 : }
1848 0 : if (pos < data_len) {
1849 0 : freq = atoi((const char *) &data[5]);
1850 0 : wpa_printf(MSG_DEBUG, "test_driver(%s): MLME RX on "
1851 0 : "freq %d MHz", bss->ifname, freq);
1852 0 : pos++;
1853 0 : data += pos;
1854 0 : data_len -= pos;
1855 : }
1856 : }
1857 :
1858 0 : if (drv->remain_on_channel_freq)
1859 0 : own_freq = drv->remain_on_channel_freq;
1860 : else
1861 0 : own_freq = drv->current_freq;
1862 :
1863 0 : if (freq && own_freq && freq != own_freq) {
1864 0 : wpa_printf(MSG_DEBUG, "test_driver(%s): Ignore MLME RX on "
1865 : "another frequency %d MHz (own %d MHz)",
1866 0 : bss->ifname, freq, own_freq);
1867 0 : return;
1868 : }
1869 :
1870 0 : os_memset(&event, 0, sizeof(event));
1871 0 : event.mlme_rx.buf = data;
1872 0 : event.mlme_rx.len = data_len;
1873 0 : event.mlme_rx.freq = freq;
1874 0 : wpa_supplicant_event(drv->ctx, EVENT_MLME_RX, &event);
1875 :
1876 0 : mgmt = (const struct ieee80211_mgmt *) data;
1877 0 : fc = le_to_host16(mgmt->frame_control);
1878 :
1879 0 : if (drv->probe_req_report && data_len >= 24) {
1880 0 : if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1881 0 : WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ) {
1882 0 : os_memset(&event, 0, sizeof(event));
1883 0 : event.rx_probe_req.sa = mgmt->sa;
1884 0 : event.rx_probe_req.da = mgmt->da;
1885 0 : event.rx_probe_req.bssid = mgmt->bssid;
1886 0 : event.rx_probe_req.ie = mgmt->u.probe_req.variable;
1887 0 : event.rx_probe_req.ie_len =
1888 0 : data_len - (mgmt->u.probe_req.variable - data);
1889 0 : wpa_supplicant_event(drv->ctx, EVENT_RX_PROBE_REQ,
1890 : &event);
1891 : }
1892 : }
1893 : }
1894 :
1895 :
1896 0 : static void wpa_driver_test_scan_cmd(struct wpa_driver_test_data *drv,
1897 : struct sockaddr *from,
1898 : socklen_t fromlen,
1899 : const u8 *data, size_t data_len)
1900 : {
1901 : char buf[512], *pos, *end;
1902 : int ret;
1903 : struct test_driver_bss *bss;
1904 :
1905 0 : bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
1906 :
1907 : /* data: optional [ STA-addr | ' ' | IEs(hex) ] */
1908 :
1909 0 : if (bss == NULL || !drv->ibss)
1910 0 : return;
1911 :
1912 0 : pos = buf;
1913 0 : end = buf + sizeof(buf);
1914 :
1915 : /* reply: SCANRESP BSSID SSID IEs */
1916 0 : ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
1917 0 : MAC2STR(bss->bssid));
1918 0 : if (ret < 0 || ret >= end - pos)
1919 0 : return;
1920 0 : pos += ret;
1921 0 : pos += wpa_snprintf_hex(pos, end - pos,
1922 0 : bss->ssid, bss->ssid_len);
1923 0 : ret = snprintf(pos, end - pos, " ");
1924 0 : if (ret < 0 || ret >= end - pos)
1925 0 : return;
1926 0 : pos += ret;
1927 0 : pos += wpa_snprintf_hex(pos, end - pos, drv->assoc_wpa_ie,
1928 : drv->assoc_wpa_ie_len);
1929 :
1930 0 : if (bss->privacy) {
1931 0 : ret = snprintf(pos, end - pos, " PRIVACY");
1932 0 : if (ret < 0 || ret >= end - pos)
1933 0 : return;
1934 0 : pos += ret;
1935 : }
1936 :
1937 0 : ret = snprintf(pos, end - pos, " IBSS");
1938 0 : if (ret < 0 || ret >= end - pos)
1939 0 : return;
1940 0 : pos += ret;
1941 :
1942 0 : sendto(drv->test_socket, buf, pos - buf, 0,
1943 : (struct sockaddr *) from, fromlen);
1944 : }
1945 :
1946 :
1947 0 : static void wpa_driver_test_receive_unix(int sock, void *eloop_ctx,
1948 : void *sock_ctx)
1949 : {
1950 0 : struct wpa_driver_test_data *drv = eloop_ctx;
1951 : char *buf;
1952 : int res;
1953 : struct sockaddr_storage from;
1954 0 : socklen_t fromlen = sizeof(from);
1955 0 : const size_t buflen = 2000;
1956 :
1957 0 : if (drv->ap) {
1958 0 : test_driver_receive_unix(sock, eloop_ctx, sock_ctx);
1959 0 : return;
1960 : }
1961 :
1962 0 : buf = os_malloc(buflen);
1963 0 : if (buf == NULL)
1964 0 : return;
1965 0 : res = recvfrom(sock, buf, buflen - 1, 0,
1966 : (struct sockaddr *) &from, &fromlen);
1967 0 : if (res < 0) {
1968 0 : perror("recvfrom(test_socket)");
1969 0 : os_free(buf);
1970 0 : return;
1971 : }
1972 0 : buf[res] = '\0';
1973 :
1974 0 : wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
1975 :
1976 0 : if (os_strncmp(buf, "SCANRESP ", 9) == 0) {
1977 0 : wpa_driver_test_scanresp(drv, (struct sockaddr *) &from,
1978 : fromlen, buf + 9);
1979 0 : } else if (os_strncmp(buf, "ASSOCRESP ", 10) == 0) {
1980 0 : wpa_driver_test_assocresp(drv, (struct sockaddr *) &from,
1981 : fromlen, buf + 10);
1982 0 : } else if (os_strcmp(buf, "DISASSOC") == 0) {
1983 0 : wpa_driver_test_disassoc(drv, (struct sockaddr *) &from,
1984 : fromlen);
1985 0 : } else if (os_strcmp(buf, "DEAUTH") == 0) {
1986 0 : wpa_driver_test_disassoc(drv, (struct sockaddr *) &from,
1987 : fromlen);
1988 0 : } else if (os_strncmp(buf, "EAPOL ", 6) == 0) {
1989 0 : wpa_driver_test_eapol(drv, (struct sockaddr *) &from, fromlen,
1990 0 : (const u8 *) buf + 6, res - 6);
1991 0 : } else if (os_strncmp(buf, "MLME ", 5) == 0) {
1992 0 : wpa_driver_test_mlme(drv, (struct sockaddr *) &from, fromlen,
1993 0 : (const u8 *) buf + 5, res - 5);
1994 0 : } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
1995 0 : wpa_driver_test_scan_cmd(drv, (struct sockaddr *) &from,
1996 : fromlen,
1997 0 : (const u8 *) buf + 5, res - 5);
1998 : } else {
1999 0 : wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
2000 : (u8 *) buf, res);
2001 : }
2002 0 : os_free(buf);
2003 : }
2004 :
2005 :
2006 0 : static void * wpa_driver_test_init2(void *ctx, const char *ifname,
2007 : void *global_priv)
2008 : {
2009 : struct wpa_driver_test_data *drv;
2010 0 : struct wpa_driver_test_global *global = global_priv;
2011 : struct test_driver_bss *bss;
2012 :
2013 0 : drv = test_alloc_data(ctx, ifname);
2014 0 : if (drv == NULL)
2015 0 : return NULL;
2016 0 : bss = dl_list_first(&drv->bss, struct test_driver_bss, list);
2017 0 : drv->global = global_priv;
2018 0 : drv->test_socket = -1;
2019 :
2020 : /* Set dummy BSSID and SSID for testing. */
2021 0 : bss->bssid[0] = 0x02;
2022 0 : bss->bssid[1] = 0x00;
2023 0 : bss->bssid[2] = 0x00;
2024 0 : bss->bssid[3] = 0x00;
2025 0 : bss->bssid[4] = 0x00;
2026 0 : bss->bssid[5] = 0x01;
2027 0 : os_memcpy(bss->ssid, "test", 5);
2028 0 : bss->ssid_len = 4;
2029 :
2030 0 : if (global->bss_add_used) {
2031 0 : os_memcpy(drv->own_addr, global->req_addr, ETH_ALEN);
2032 0 : global->bss_add_used = 0;
2033 : }
2034 :
2035 0 : eloop_register_timeout(1, 0, wpa_driver_test_poll, drv, NULL);
2036 :
2037 0 : return bss;
2038 : }
2039 :
2040 :
2041 0 : static void wpa_driver_test_close_test_socket(struct wpa_driver_test_data *drv)
2042 : {
2043 0 : if (drv->test_socket >= 0) {
2044 0 : eloop_unregister_read_sock(drv->test_socket);
2045 0 : close(drv->test_socket);
2046 0 : drv->test_socket = -1;
2047 : }
2048 :
2049 0 : if (drv->own_socket_path) {
2050 0 : unlink(drv->own_socket_path);
2051 0 : os_free(drv->own_socket_path);
2052 0 : drv->own_socket_path = NULL;
2053 : }
2054 0 : }
2055 :
2056 :
2057 0 : static void wpa_driver_test_deinit(void *priv)
2058 : {
2059 0 : struct test_driver_bss *dbss = priv;
2060 0 : struct wpa_driver_test_data *drv = dbss->drv;
2061 : struct test_client_socket *cli, *prev;
2062 : int i;
2063 :
2064 0 : cli = drv->cli;
2065 0 : while (cli) {
2066 0 : prev = cli;
2067 0 : cli = cli->next;
2068 0 : os_free(prev);
2069 : }
2070 :
2071 : #ifdef HOSTAPD
2072 : /* There should be only one BSS remaining at this point. */
2073 0 : if (dl_list_len(&drv->bss) != 1)
2074 0 : wpa_printf(MSG_ERROR, "%s: %u remaining BSS entries",
2075 : __func__, dl_list_len(&drv->bss));
2076 : #endif /* HOSTAPD */
2077 :
2078 0 : test_driver_free_bsses(drv);
2079 :
2080 0 : wpa_driver_test_close_test_socket(drv);
2081 0 : eloop_cancel_timeout(wpa_driver_test_scan_timeout, drv, drv->ctx);
2082 0 : eloop_cancel_timeout(wpa_driver_test_poll, drv, NULL);
2083 0 : eloop_cancel_timeout(test_remain_on_channel_timeout, drv, NULL);
2084 0 : os_free(drv->test_dir);
2085 0 : for (i = 0; i < MAX_SCAN_RESULTS; i++)
2086 0 : os_free(drv->scanres[i]);
2087 0 : os_free(drv->probe_req_ie);
2088 0 : wpa_trace_remove_ref(drv, ctx, drv->ctx);
2089 0 : os_free(drv);
2090 0 : }
2091 :
2092 :
2093 0 : static int wpa_driver_test_attach(struct wpa_driver_test_data *drv,
2094 : const char *dir, int ap)
2095 : {
2096 : #ifdef DRIVER_TEST_UNIX
2097 : static unsigned int counter = 0;
2098 : struct sockaddr_un addr;
2099 : size_t len;
2100 :
2101 0 : os_free(drv->own_socket_path);
2102 0 : if (dir) {
2103 0 : len = os_strlen(dir) + 30;
2104 0 : drv->own_socket_path = os_malloc(len);
2105 0 : if (drv->own_socket_path == NULL)
2106 0 : return -1;
2107 0 : os_snprintf(drv->own_socket_path, len, "%s/%s-" MACSTR,
2108 0 : dir, ap ? "AP" : "STA", MAC2STR(drv->own_addr));
2109 : } else {
2110 0 : drv->own_socket_path = os_malloc(100);
2111 0 : if (drv->own_socket_path == NULL)
2112 0 : return -1;
2113 0 : os_snprintf(drv->own_socket_path, 100,
2114 : "/tmp/wpa_supplicant_test-%d-%d",
2115 : getpid(), counter++);
2116 : }
2117 :
2118 0 : drv->test_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
2119 0 : if (drv->test_socket < 0) {
2120 0 : perror("socket(PF_UNIX)");
2121 0 : os_free(drv->own_socket_path);
2122 0 : drv->own_socket_path = NULL;
2123 0 : return -1;
2124 : }
2125 :
2126 0 : os_memset(&addr, 0, sizeof(addr));
2127 0 : addr.sun_family = AF_UNIX;
2128 0 : os_strlcpy(addr.sun_path, drv->own_socket_path, sizeof(addr.sun_path));
2129 0 : if (bind(drv->test_socket, (struct sockaddr *) &addr,
2130 : sizeof(addr)) < 0) {
2131 0 : perror("test-driver-attach: bind(PF_UNIX)");
2132 0 : close(drv->test_socket);
2133 0 : unlink(drv->own_socket_path);
2134 0 : os_free(drv->own_socket_path);
2135 0 : drv->own_socket_path = NULL;
2136 0 : return -1;
2137 : }
2138 :
2139 0 : eloop_register_read_sock(drv->test_socket,
2140 : wpa_driver_test_receive_unix, drv, NULL);
2141 :
2142 0 : return 0;
2143 : #else /* DRIVER_TEST_UNIX */
2144 : return -1;
2145 : #endif /* DRIVER_TEST_UNIX */
2146 : }
2147 :
2148 :
2149 0 : static int wpa_driver_test_attach_udp(struct wpa_driver_test_data *drv,
2150 : char *dst)
2151 : {
2152 : char *pos;
2153 :
2154 0 : pos = os_strchr(dst, ':');
2155 0 : if (pos == NULL)
2156 0 : return -1;
2157 0 : *pos++ = '\0';
2158 0 : wpa_printf(MSG_DEBUG, "%s: addr=%s port=%s", __func__, dst, pos);
2159 :
2160 0 : drv->test_socket = socket(PF_INET, SOCK_DGRAM, 0);
2161 0 : if (drv->test_socket < 0) {
2162 0 : perror("socket(PF_INET)");
2163 0 : return -1;
2164 : }
2165 :
2166 0 : os_memset(&drv->hostapd_addr_udp, 0, sizeof(drv->hostapd_addr_udp));
2167 0 : drv->hostapd_addr_udp.sin_family = AF_INET;
2168 : #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
2169 : {
2170 : int a[4];
2171 : u8 *pos;
2172 : sscanf(dst, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
2173 : pos = (u8 *) &drv->hostapd_addr_udp.sin_addr;
2174 : *pos++ = a[0];
2175 : *pos++ = a[1];
2176 : *pos++ = a[2];
2177 : *pos++ = a[3];
2178 : }
2179 : #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
2180 0 : inet_aton(dst, &drv->hostapd_addr_udp.sin_addr);
2181 : #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
2182 0 : drv->hostapd_addr_udp.sin_port = htons(atoi(pos));
2183 :
2184 0 : drv->hostapd_addr_udp_set = 1;
2185 :
2186 0 : eloop_register_read_sock(drv->test_socket,
2187 : wpa_driver_test_receive_unix, drv, NULL);
2188 :
2189 0 : return 0;
2190 : }
2191 :
2192 :
2193 0 : static int wpa_driver_test_set_param(void *priv, const char *param)
2194 : {
2195 0 : struct test_driver_bss *dbss = priv;
2196 0 : struct wpa_driver_test_data *drv = dbss->drv;
2197 : const char *pos;
2198 :
2199 0 : wpa_printf(MSG_DEBUG, "%s: param='%s'", __func__, param);
2200 0 : if (param == NULL)
2201 0 : return 0;
2202 :
2203 0 : wpa_driver_test_close_test_socket(drv);
2204 :
2205 : #ifdef DRIVER_TEST_UNIX
2206 0 : pos = os_strstr(param, "test_socket=");
2207 0 : if (pos) {
2208 : const char *pos2;
2209 : size_t len;
2210 :
2211 0 : pos += 12;
2212 0 : pos2 = os_strchr(pos, ' ');
2213 0 : if (pos2)
2214 0 : len = pos2 - pos;
2215 : else
2216 0 : len = os_strlen(pos);
2217 0 : if (len > sizeof(drv->hostapd_addr.sun_path))
2218 0 : return -1;
2219 0 : os_memset(&drv->hostapd_addr, 0, sizeof(drv->hostapd_addr));
2220 0 : drv->hostapd_addr.sun_family = AF_UNIX;
2221 0 : os_memcpy(drv->hostapd_addr.sun_path, pos, len);
2222 0 : drv->hostapd_addr_set = 1;
2223 : }
2224 : #endif /* DRIVER_TEST_UNIX */
2225 :
2226 0 : pos = os_strstr(param, "test_dir=");
2227 0 : if (pos) {
2228 : char *end;
2229 0 : os_free(drv->test_dir);
2230 0 : drv->test_dir = os_strdup(pos + 9);
2231 0 : if (drv->test_dir == NULL)
2232 0 : return -1;
2233 0 : end = os_strchr(drv->test_dir, ' ');
2234 0 : if (end)
2235 0 : *end = '\0';
2236 0 : if (wpa_driver_test_attach(drv, drv->test_dir, 0))
2237 0 : return -1;
2238 : } else {
2239 0 : pos = os_strstr(param, "test_udp=");
2240 0 : if (pos) {
2241 : char *dst, *epos;
2242 0 : dst = os_strdup(pos + 9);
2243 0 : if (dst == NULL)
2244 0 : return -1;
2245 0 : epos = os_strchr(dst, ' ');
2246 0 : if (epos)
2247 0 : *epos = '\0';
2248 0 : if (wpa_driver_test_attach_udp(drv, dst))
2249 0 : return -1;
2250 0 : os_free(dst);
2251 0 : } else if (wpa_driver_test_attach(drv, NULL, 0))
2252 0 : return -1;
2253 : }
2254 :
2255 0 : if (os_strstr(param, "use_associnfo=1")) {
2256 0 : wpa_printf(MSG_DEBUG, "test_driver: Use AssocInfo events");
2257 0 : drv->use_associnfo = 1;
2258 : }
2259 :
2260 0 : return 0;
2261 : }
2262 :
2263 :
2264 0 : static const u8 * wpa_driver_test_get_mac_addr(void *priv)
2265 : {
2266 0 : struct test_driver_bss *dbss = priv;
2267 0 : struct wpa_driver_test_data *drv = dbss->drv;
2268 0 : wpa_printf(MSG_DEBUG, "%s", __func__);
2269 0 : return drv->own_addr;
2270 : }
2271 :
2272 :
2273 0 : static int wpa_driver_test_send_eapol(void *priv, const u8 *dest, u16 proto,
2274 : const u8 *data, size_t data_len)
2275 : {
2276 0 : struct test_driver_bss *dbss = priv;
2277 0 : struct wpa_driver_test_data *drv = dbss->drv;
2278 : char *msg;
2279 : size_t msg_len;
2280 : struct l2_ethhdr eth;
2281 : struct sockaddr *addr;
2282 : socklen_t alen;
2283 : #ifdef DRIVER_TEST_UNIX
2284 : struct sockaddr_un addr_un;
2285 : #endif /* DRIVER_TEST_UNIX */
2286 :
2287 0 : wpa_hexdump(MSG_MSGDUMP, "test_send_eapol TX frame", data, data_len);
2288 :
2289 0 : os_memset(ð, 0, sizeof(eth));
2290 0 : os_memcpy(eth.h_dest, dest, ETH_ALEN);
2291 0 : os_memcpy(eth.h_source, drv->own_addr, ETH_ALEN);
2292 0 : eth.h_proto = host_to_be16(proto);
2293 :
2294 0 : msg_len = 6 + sizeof(eth) + data_len;
2295 0 : msg = os_malloc(msg_len);
2296 0 : if (msg == NULL)
2297 0 : return -1;
2298 0 : os_memcpy(msg, "EAPOL ", 6);
2299 0 : os_memcpy(msg + 6, ð, sizeof(eth));
2300 0 : os_memcpy(msg + 6 + sizeof(eth), data, data_len);
2301 :
2302 0 : if (os_memcmp(dest, dbss->bssid, ETH_ALEN) == 0 ||
2303 0 : drv->test_dir == NULL) {
2304 0 : if (drv->hostapd_addr_udp_set) {
2305 0 : addr = (struct sockaddr *) &drv->hostapd_addr_udp;
2306 0 : alen = sizeof(drv->hostapd_addr_udp);
2307 : } else {
2308 : #ifdef DRIVER_TEST_UNIX
2309 0 : addr = (struct sockaddr *) &drv->hostapd_addr;
2310 0 : alen = sizeof(drv->hostapd_addr);
2311 : #else /* DRIVER_TEST_UNIX */
2312 : os_free(msg);
2313 : return -1;
2314 : #endif /* DRIVER_TEST_UNIX */
2315 : }
2316 : } else {
2317 : #ifdef DRIVER_TEST_UNIX
2318 : struct stat st;
2319 0 : os_memset(&addr_un, 0, sizeof(addr_un));
2320 0 : addr_un.sun_family = AF_UNIX;
2321 0 : os_snprintf(addr_un.sun_path, sizeof(addr_un.sun_path),
2322 0 : "%s/STA-" MACSTR, drv->test_dir, MAC2STR(dest));
2323 0 : if (stat(addr_un.sun_path, &st) < 0) {
2324 0 : os_snprintf(addr_un.sun_path, sizeof(addr_un.sun_path),
2325 : "%s/AP-" MACSTR,
2326 0 : drv->test_dir, MAC2STR(dest));
2327 : }
2328 0 : addr = (struct sockaddr *) &addr_un;
2329 0 : alen = sizeof(addr_un);
2330 : #else /* DRIVER_TEST_UNIX */
2331 : os_free(msg);
2332 : return -1;
2333 : #endif /* DRIVER_TEST_UNIX */
2334 : }
2335 :
2336 0 : if (sendto(drv->test_socket, msg, msg_len, 0, addr, alen) < 0) {
2337 0 : perror("sendmsg(test_socket)");
2338 0 : os_free(msg);
2339 0 : return -1;
2340 : }
2341 :
2342 0 : os_free(msg);
2343 0 : return 0;
2344 : }
2345 :
2346 :
2347 0 : static int wpa_driver_test_get_capa(void *priv, struct wpa_driver_capa *capa)
2348 : {
2349 0 : os_memset(capa, 0, sizeof(*capa));
2350 0 : capa->key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2351 : WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2352 : WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2353 : WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK |
2354 : WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE |
2355 : WPA_DRIVER_CAPA_KEY_MGMT_FT |
2356 : WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2357 0 : capa->enc = WPA_DRIVER_CAPA_ENC_WEP40 |
2358 : WPA_DRIVER_CAPA_ENC_WEP104 |
2359 : WPA_DRIVER_CAPA_ENC_TKIP |
2360 : WPA_DRIVER_CAPA_ENC_CCMP;
2361 0 : capa->auth = WPA_DRIVER_AUTH_OPEN |
2362 : WPA_DRIVER_AUTH_SHARED |
2363 : WPA_DRIVER_AUTH_LEAP;
2364 0 : capa->flags |= WPA_DRIVER_FLAGS_AP;
2365 0 : capa->flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
2366 0 : capa->flags |= WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE;
2367 0 : capa->flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
2368 0 : capa->max_scan_ssids = 2;
2369 0 : capa->max_remain_on_chan = 60000;
2370 :
2371 0 : return 0;
2372 : }
2373 :
2374 :
2375 0 : static int wpa_driver_test_mlme_setprotection(void *priv, const u8 *addr,
2376 : int protect_type,
2377 : int key_type)
2378 : {
2379 0 : wpa_printf(MSG_DEBUG, "%s: protect_type=%d key_type=%d",
2380 : __func__, protect_type, key_type);
2381 :
2382 0 : if (addr) {
2383 0 : wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR,
2384 0 : __func__, MAC2STR(addr));
2385 : }
2386 :
2387 0 : return 0;
2388 : }
2389 :
2390 :
2391 0 : static void * wpa_driver_test_global_init(void)
2392 : {
2393 : struct wpa_driver_test_global *global;
2394 :
2395 0 : global = os_zalloc(sizeof(*global));
2396 0 : return global;
2397 : }
2398 :
2399 :
2400 0 : static void wpa_driver_test_global_deinit(void *priv)
2401 : {
2402 0 : struct wpa_driver_test_global *global = priv;
2403 0 : os_free(global);
2404 0 : }
2405 :
2406 :
2407 : static struct wpa_interface_info *
2408 0 : wpa_driver_test_get_interfaces(void *global_priv)
2409 : {
2410 : /* struct wpa_driver_test_global *global = priv; */
2411 : struct wpa_interface_info *iface;
2412 :
2413 0 : iface = os_zalloc(sizeof(*iface));
2414 0 : if (iface == NULL)
2415 0 : return iface;
2416 0 : iface->ifname = os_strdup("sta0");
2417 0 : iface->desc = os_strdup("test interface 0");
2418 0 : iface->drv_name = "test";
2419 0 : iface->next = os_zalloc(sizeof(*iface));
2420 0 : if (iface->next) {
2421 0 : iface->next->ifname = os_strdup("sta1");
2422 0 : iface->next->desc = os_strdup("test interface 1");
2423 0 : iface->next->drv_name = "test";
2424 : }
2425 :
2426 0 : return iface;
2427 : }
2428 :
2429 :
2430 : static struct hostapd_hw_modes *
2431 0 : wpa_driver_test_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
2432 : {
2433 : struct hostapd_hw_modes *modes;
2434 : size_t i;
2435 :
2436 0 : *num_modes = 3;
2437 0 : *flags = 0;
2438 0 : modes = os_calloc(*num_modes, sizeof(struct hostapd_hw_modes));
2439 0 : if (modes == NULL)
2440 0 : return NULL;
2441 0 : modes[0].mode = HOSTAPD_MODE_IEEE80211G;
2442 0 : modes[0].num_channels = 11;
2443 0 : modes[0].num_rates = 12;
2444 0 : modes[0].channels = os_calloc(11, sizeof(struct hostapd_channel_data));
2445 0 : modes[0].rates = os_calloc(modes[0].num_rates, sizeof(int));
2446 0 : if (modes[0].channels == NULL || modes[0].rates == NULL)
2447 : goto fail;
2448 0 : for (i = 0; i < 11; i++) {
2449 0 : modes[0].channels[i].chan = i + 1;
2450 0 : modes[0].channels[i].freq = 2412 + 5 * i;
2451 0 : modes[0].channels[i].flag = 0;
2452 : }
2453 0 : modes[0].rates[0] = 10;
2454 0 : modes[0].rates[1] = 20;
2455 0 : modes[0].rates[2] = 55;
2456 0 : modes[0].rates[3] = 110;
2457 0 : modes[0].rates[4] = 60;
2458 0 : modes[0].rates[5] = 90;
2459 0 : modes[0].rates[6] = 120;
2460 0 : modes[0].rates[7] = 180;
2461 0 : modes[0].rates[8] = 240;
2462 0 : modes[0].rates[9] = 360;
2463 0 : modes[0].rates[10] = 480;
2464 0 : modes[0].rates[11] = 540;
2465 :
2466 0 : modes[1].mode = HOSTAPD_MODE_IEEE80211B;
2467 0 : modes[1].num_channels = 11;
2468 0 : modes[1].num_rates = 4;
2469 0 : modes[1].channels = os_calloc(11, sizeof(struct hostapd_channel_data));
2470 0 : modes[1].rates = os_calloc(modes[1].num_rates, sizeof(int));
2471 0 : if (modes[1].channels == NULL || modes[1].rates == NULL)
2472 : goto fail;
2473 0 : for (i = 0; i < 11; i++) {
2474 0 : modes[1].channels[i].chan = i + 1;
2475 0 : modes[1].channels[i].freq = 2412 + 5 * i;
2476 0 : modes[1].channels[i].flag = 0;
2477 : }
2478 0 : modes[1].rates[0] = 10;
2479 0 : modes[1].rates[1] = 20;
2480 0 : modes[1].rates[2] = 55;
2481 0 : modes[1].rates[3] = 110;
2482 :
2483 0 : modes[2].mode = HOSTAPD_MODE_IEEE80211A;
2484 0 : modes[2].num_channels = 1;
2485 0 : modes[2].num_rates = 8;
2486 0 : modes[2].channels = os_calloc(1, sizeof(struct hostapd_channel_data));
2487 0 : modes[2].rates = os_calloc(modes[2].num_rates, sizeof(int));
2488 0 : if (modes[2].channels == NULL || modes[2].rates == NULL)
2489 : goto fail;
2490 0 : modes[2].channels[0].chan = 60;
2491 0 : modes[2].channels[0].freq = 5300;
2492 0 : modes[2].channels[0].flag = 0;
2493 0 : modes[2].rates[0] = 60;
2494 0 : modes[2].rates[1] = 90;
2495 0 : modes[2].rates[2] = 120;
2496 0 : modes[2].rates[3] = 180;
2497 0 : modes[2].rates[4] = 240;
2498 0 : modes[2].rates[5] = 360;
2499 0 : modes[2].rates[6] = 480;
2500 0 : modes[2].rates[7] = 540;
2501 :
2502 0 : return modes;
2503 :
2504 : fail:
2505 0 : if (modes) {
2506 0 : for (i = 0; i < *num_modes; i++) {
2507 0 : os_free(modes[i].channels);
2508 0 : os_free(modes[i].rates);
2509 : }
2510 0 : os_free(modes);
2511 : }
2512 0 : return NULL;
2513 : }
2514 :
2515 :
2516 0 : static int wpa_driver_test_set_freq(void *priv,
2517 : struct hostapd_freq_params *freq)
2518 : {
2519 0 : struct test_driver_bss *dbss = priv;
2520 0 : struct wpa_driver_test_data *drv = dbss->drv;
2521 0 : wpa_printf(MSG_DEBUG, "test: set_freq %u MHz", freq->freq);
2522 0 : drv->current_freq = freq->freq;
2523 0 : return 0;
2524 : }
2525 :
2526 :
2527 0 : static int wpa_driver_test_send_action(void *priv, unsigned int freq,
2528 : unsigned int wait,
2529 : const u8 *dst, const u8 *src,
2530 : const u8 *bssid,
2531 : const u8 *data, size_t data_len,
2532 : int no_cck)
2533 : {
2534 0 : struct test_driver_bss *dbss = priv;
2535 0 : struct wpa_driver_test_data *drv = dbss->drv;
2536 0 : int ret = -1;
2537 : u8 *buf;
2538 : struct ieee80211_hdr *hdr;
2539 :
2540 0 : wpa_printf(MSG_DEBUG, "test: Send Action frame");
2541 :
2542 0 : if ((drv->remain_on_channel_freq &&
2543 0 : freq != drv->remain_on_channel_freq) ||
2544 0 : (drv->remain_on_channel_freq == 0 &&
2545 0 : freq != (unsigned int) drv->current_freq)) {
2546 0 : wpa_printf(MSG_DEBUG, "test: Reject Action frame TX on "
2547 : "unexpected channel: freq=%u MHz (current_freq=%u "
2548 : "MHz, remain-on-channel freq=%u MHz)",
2549 : freq, drv->current_freq,
2550 : drv->remain_on_channel_freq);
2551 0 : return -1;
2552 : }
2553 :
2554 0 : buf = os_zalloc(24 + data_len);
2555 0 : if (buf == NULL)
2556 0 : return ret;
2557 0 : os_memcpy(buf + 24, data, data_len);
2558 0 : hdr = (struct ieee80211_hdr *) buf;
2559 0 : hdr->frame_control =
2560 : IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
2561 0 : os_memcpy(hdr->addr1, dst, ETH_ALEN);
2562 0 : os_memcpy(hdr->addr2, src, ETH_ALEN);
2563 0 : os_memcpy(hdr->addr3, bssid, ETH_ALEN);
2564 :
2565 0 : ret = wpa_driver_test_send_mlme(priv, buf, 24 + data_len, 0);
2566 0 : os_free(buf);
2567 0 : return ret;
2568 : }
2569 :
2570 :
2571 0 : static void test_remain_on_channel_timeout(void *eloop_ctx, void *timeout_ctx)
2572 : {
2573 0 : struct wpa_driver_test_data *drv = eloop_ctx;
2574 : union wpa_event_data data;
2575 :
2576 0 : wpa_printf(MSG_DEBUG, "test: Remain-on-channel timeout");
2577 :
2578 0 : os_memset(&data, 0, sizeof(data));
2579 0 : data.remain_on_channel.freq = drv->remain_on_channel_freq;
2580 0 : data.remain_on_channel.duration = drv->remain_on_channel_duration;
2581 :
2582 0 : drv->remain_on_channel_freq = 0;
2583 :
2584 0 : wpa_supplicant_event(drv->ctx, EVENT_CANCEL_REMAIN_ON_CHANNEL, &data);
2585 0 : }
2586 :
2587 :
2588 0 : static int wpa_driver_test_remain_on_channel(void *priv, unsigned int freq,
2589 : unsigned int duration)
2590 : {
2591 0 : struct test_driver_bss *dbss = priv;
2592 0 : struct wpa_driver_test_data *drv = dbss->drv;
2593 : union wpa_event_data data;
2594 :
2595 0 : wpa_printf(MSG_DEBUG, "%s(freq=%u, duration=%u)",
2596 : __func__, freq, duration);
2597 0 : if (drv->remain_on_channel_freq &&
2598 0 : drv->remain_on_channel_freq != freq) {
2599 0 : wpa_printf(MSG_DEBUG, "test: Refuse concurrent "
2600 : "remain_on_channel request");
2601 0 : return -1;
2602 : }
2603 :
2604 0 : drv->remain_on_channel_freq = freq;
2605 0 : drv->remain_on_channel_duration = duration;
2606 0 : eloop_cancel_timeout(test_remain_on_channel_timeout, drv, NULL);
2607 0 : eloop_register_timeout(duration / 1000, (duration % 1000) * 1000,
2608 : test_remain_on_channel_timeout, drv, NULL);
2609 :
2610 0 : os_memset(&data, 0, sizeof(data));
2611 0 : data.remain_on_channel.freq = freq;
2612 0 : data.remain_on_channel.duration = duration;
2613 0 : wpa_supplicant_event(drv->ctx, EVENT_REMAIN_ON_CHANNEL, &data);
2614 :
2615 0 : return 0;
2616 : }
2617 :
2618 :
2619 0 : static int wpa_driver_test_cancel_remain_on_channel(void *priv)
2620 : {
2621 0 : struct test_driver_bss *dbss = priv;
2622 0 : struct wpa_driver_test_data *drv = dbss->drv;
2623 0 : wpa_printf(MSG_DEBUG, "%s", __func__);
2624 0 : if (!drv->remain_on_channel_freq)
2625 0 : return -1;
2626 0 : drv->remain_on_channel_freq = 0;
2627 0 : eloop_cancel_timeout(test_remain_on_channel_timeout, drv, NULL);
2628 0 : return 0;
2629 : }
2630 :
2631 :
2632 0 : static int wpa_driver_test_probe_req_report(void *priv, int report)
2633 : {
2634 0 : struct test_driver_bss *dbss = priv;
2635 0 : struct wpa_driver_test_data *drv = dbss->drv;
2636 0 : wpa_printf(MSG_DEBUG, "%s(report=%d)", __func__, report);
2637 0 : drv->probe_req_report = report;
2638 0 : return 0;
2639 : }
2640 :
2641 :
2642 : const struct wpa_driver_ops wpa_driver_test_ops = {
2643 : "test",
2644 : "wpa_supplicant test driver",
2645 : .hapd_init = test_driver_init,
2646 : .hapd_deinit = wpa_driver_test_deinit,
2647 : .hapd_send_eapol = test_driver_send_eapol,
2648 : .send_mlme = wpa_driver_test_send_mlme,
2649 : .set_generic_elem = test_driver_set_generic_elem,
2650 : .sta_deauth = test_driver_sta_deauth,
2651 : .sta_disassoc = test_driver_sta_disassoc,
2652 : .get_hw_feature_data = wpa_driver_test_get_hw_feature_data,
2653 : .if_add = test_driver_if_add,
2654 : .if_remove = test_driver_if_remove,
2655 : .hapd_set_ssid = test_driver_set_ssid,
2656 : .set_privacy = test_driver_set_privacy,
2657 : .set_sta_vlan = test_driver_set_sta_vlan,
2658 : .sta_add = test_driver_sta_add,
2659 : .send_ether = test_driver_send_ether,
2660 : .set_ap_wps_ie = test_driver_set_ap_wps_ie,
2661 : .get_bssid = wpa_driver_test_get_bssid,
2662 : .get_ssid = wpa_driver_test_get_ssid,
2663 : .set_key = wpa_driver_test_set_key,
2664 : .deinit = wpa_driver_test_deinit,
2665 : .set_param = wpa_driver_test_set_param,
2666 : .deauthenticate = wpa_driver_test_deauthenticate,
2667 : .associate = wpa_driver_test_associate,
2668 : .get_capa = wpa_driver_test_get_capa,
2669 : .get_mac_addr = wpa_driver_test_get_mac_addr,
2670 : .send_eapol = wpa_driver_test_send_eapol,
2671 : .mlme_setprotection = wpa_driver_test_mlme_setprotection,
2672 : .get_scan_results2 = wpa_driver_test_get_scan_results2,
2673 : .global_init = wpa_driver_test_global_init,
2674 : .global_deinit = wpa_driver_test_global_deinit,
2675 : .init2 = wpa_driver_test_init2,
2676 : .get_interfaces = wpa_driver_test_get_interfaces,
2677 : .scan2 = wpa_driver_test_scan,
2678 : .set_freq = wpa_driver_test_set_freq,
2679 : .send_action = wpa_driver_test_send_action,
2680 : .remain_on_channel = wpa_driver_test_remain_on_channel,
2681 : .cancel_remain_on_channel = wpa_driver_test_cancel_remain_on_channel,
2682 : .probe_req_report = wpa_driver_test_probe_req_report,
2683 : };
|