Sync usage with man page.
[netbsd-mini2440.git] / dist / wpa / src / drivers / driver_test.c
blob5c6e6f1cc7c82b5868c02fbfb71e9ec366ce6eb0
1 /*
2 * WPA Supplicant - testing driver interface
3 * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
16 #include <sys/un.h>
17 #include <dirent.h>
18 #include <sys/stat.h>
20 #include "common.h"
21 #include "driver.h"
22 #include "l2_packet/l2_packet.h"
23 #include "eloop.h"
24 #include "sha1.h"
25 #include "ieee802_11_defs.h"
28 struct wpa_driver_test_data {
29 void *ctx;
30 u8 own_addr[ETH_ALEN];
31 int test_socket;
32 struct sockaddr_un hostapd_addr;
33 int hostapd_addr_set;
34 char *own_socket_path;
35 char *test_dir;
36 u8 bssid[ETH_ALEN];
37 u8 ssid[32];
38 size_t ssid_len;
39 #define MAX_SCAN_RESULTS 30
40 struct wpa_scan_res *scanres[MAX_SCAN_RESULTS];
41 size_t num_scanres;
42 int use_associnfo;
43 u8 assoc_wpa_ie[80];
44 size_t assoc_wpa_ie_len;
45 int use_mlme;
46 int associated;
50 static void wpa_driver_test_poll(void *eloop_ctx, void *timeout_ctx)
52 struct wpa_driver_test_data *drv = eloop_ctx;
54 if (drv->associated && drv->hostapd_addr_set) {
55 struct stat st;
56 if (stat(drv->hostapd_addr.sun_path, &st) < 0) {
57 wpa_printf(MSG_DEBUG, "%s: lost connection to AP: %s",
58 __func__, strerror(errno));
59 drv->associated = 0;
60 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
64 eloop_register_timeout(1, 0, wpa_driver_test_poll, drv, NULL);
68 static int wpa_driver_test_set_wpa(void *priv, int enabled)
70 wpa_printf(MSG_DEBUG, "%s: enabled=%d", __func__, enabled);
71 return 0;
75 static void wpa_driver_test_scan_timeout(void *eloop_ctx, void *timeout_ctx)
77 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
78 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
82 static void wpa_driver_scan_dir(struct wpa_driver_test_data *drv,
83 const char *path)
85 struct dirent *dent;
86 DIR *dir;
87 struct sockaddr_un addr;
89 dir = opendir(path);
90 if (dir == NULL)
91 return;
93 while ((dent = readdir(dir))) {
94 if (os_strncmp(dent->d_name, "AP-", 3) != 0)
95 continue;
96 wpa_printf(MSG_DEBUG, "%s: SCAN %s", __func__, dent->d_name);
98 os_memset(&addr, 0, sizeof(addr));
99 addr.sun_family = AF_UNIX;
100 os_snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
101 path, dent->d_name);
103 if (sendto(drv->test_socket, "SCAN", 4, 0,
104 (struct sockaddr *) &addr, sizeof(addr)) < 0) {
105 perror("sendto(test_socket)");
108 closedir(dir);
112 static int wpa_driver_test_scan(void *priv, const u8 *ssid, size_t ssid_len)
114 struct wpa_driver_test_data *drv = priv;
115 wpa_printf(MSG_DEBUG, "%s: priv=%p", __func__, priv);
117 drv->num_scanres = 0;
119 if (drv->test_socket >= 0 && drv->test_dir)
120 wpa_driver_scan_dir(drv, drv->test_dir);
122 if (drv->test_socket >= 0 && drv->hostapd_addr_set &&
123 sendto(drv->test_socket, "SCAN", 4, 0,
124 (struct sockaddr *) &drv->hostapd_addr,
125 sizeof(drv->hostapd_addr)) < 0) {
126 perror("sendto(test_socket)");
129 eloop_cancel_timeout(wpa_driver_test_scan_timeout, drv, drv->ctx);
130 eloop_register_timeout(1, 0, wpa_driver_test_scan_timeout, drv,
131 drv->ctx);
132 return 0;
136 static struct wpa_scan_results * wpa_driver_test_get_scan_results2(void *priv)
138 struct wpa_driver_test_data *drv = priv;
139 struct wpa_scan_results *res;
140 size_t i;
142 res = os_zalloc(sizeof(*res));
143 if (res == NULL)
144 return NULL;
146 res->res = os_zalloc(drv->num_scanres * sizeof(struct wpa_scan_res *));
147 if (res->res == NULL) {
148 os_free(res);
149 return NULL;
152 for (i = 0; i < drv->num_scanres; i++) {
153 struct wpa_scan_res *r;
154 if (drv->scanres[i] == NULL)
155 continue;
156 r = os_malloc(sizeof(*r) + drv->scanres[i]->ie_len);
157 if (r == NULL)
158 break;
159 os_memcpy(r, drv->scanres[i],
160 sizeof(*r) + drv->scanres[i]->ie_len);
161 res->res[res->num++] = r;
164 return res;
168 static int wpa_driver_test_set_key(void *priv, wpa_alg alg, const u8 *addr,
169 int key_idx, int set_tx,
170 const u8 *seq, size_t seq_len,
171 const u8 *key, size_t key_len)
173 wpa_printf(MSG_DEBUG, "%s: priv=%p alg=%d key_idx=%d set_tx=%d",
174 __func__, priv, alg, key_idx, set_tx);
175 if (addr) {
176 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
178 if (seq) {
179 wpa_hexdump(MSG_DEBUG, " seq", seq, seq_len);
181 if (key) {
182 wpa_hexdump(MSG_DEBUG, " key", key, key_len);
184 return 0;
188 static int wpa_driver_test_associate(
189 void *priv, struct wpa_driver_associate_params *params)
191 struct wpa_driver_test_data *drv = priv;
192 wpa_printf(MSG_DEBUG, "%s: priv=%p freq=%d pairwise_suite=%d "
193 "group_suite=%d key_mgmt_suite=%d auth_alg=%d mode=%d",
194 __func__, priv, params->freq, params->pairwise_suite,
195 params->group_suite, params->key_mgmt_suite,
196 params->auth_alg, params->mode);
197 if (params->bssid) {
198 wpa_printf(MSG_DEBUG, " bssid=" MACSTR,
199 MAC2STR(params->bssid));
201 if (params->ssid) {
202 wpa_hexdump_ascii(MSG_DEBUG, " ssid",
203 params->ssid, params->ssid_len);
205 if (params->wpa_ie) {
206 wpa_hexdump(MSG_DEBUG, " wpa_ie",
207 params->wpa_ie, params->wpa_ie_len);
208 drv->assoc_wpa_ie_len = params->wpa_ie_len;
209 if (drv->assoc_wpa_ie_len > sizeof(drv->assoc_wpa_ie))
210 drv->assoc_wpa_ie_len = sizeof(drv->assoc_wpa_ie);
211 os_memcpy(drv->assoc_wpa_ie, params->wpa_ie,
212 drv->assoc_wpa_ie_len);
213 } else
214 drv->assoc_wpa_ie_len = 0;
216 if (drv->test_dir && params->bssid) {
217 os_memset(&drv->hostapd_addr, 0, sizeof(drv->hostapd_addr));
218 drv->hostapd_addr.sun_family = AF_UNIX;
219 os_snprintf(drv->hostapd_addr.sun_path,
220 sizeof(drv->hostapd_addr.sun_path),
221 "%s/AP-" MACSTR,
222 drv->test_dir, MAC2STR(params->bssid));
223 drv->hostapd_addr_set = 1;
226 if (drv->test_socket >= 0 && drv->hostapd_addr_set) {
227 char cmd[200], *pos, *end;
228 int ret;
229 end = cmd + sizeof(cmd);
230 pos = cmd;
231 ret = os_snprintf(pos, end - pos, "ASSOC " MACSTR " ",
232 MAC2STR(drv->own_addr));
233 if (ret >= 0 && ret < end - pos)
234 pos += ret;
235 pos += wpa_snprintf_hex(pos, end - pos, params->ssid,
236 params->ssid_len);
237 ret = os_snprintf(pos, end - pos, " ");
238 if (ret >= 0 && ret < end - pos)
239 pos += ret;
240 pos += wpa_snprintf_hex(pos, end - pos, params->wpa_ie,
241 params->wpa_ie_len);
242 end[-1] = '\0';
243 if (sendto(drv->test_socket, cmd, os_strlen(cmd), 0,
244 (struct sockaddr *) &drv->hostapd_addr,
245 sizeof(drv->hostapd_addr)) < 0) {
246 perror("sendto(test_socket)");
247 return -1;
250 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
251 drv->ssid_len = params->ssid_len;
252 } else {
253 drv->associated = 1;
254 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
257 return 0;
261 static int wpa_driver_test_get_bssid(void *priv, u8 *bssid)
263 struct wpa_driver_test_data *drv = priv;
264 os_memcpy(bssid, drv->bssid, ETH_ALEN);
265 return 0;
269 static int wpa_driver_test_get_ssid(void *priv, u8 *ssid)
271 struct wpa_driver_test_data *drv = priv;
272 os_memcpy(ssid, drv->ssid, 32);
273 return drv->ssid_len;
277 static int wpa_driver_test_send_disassoc(struct wpa_driver_test_data *drv)
279 if (drv->test_socket >= 0 &&
280 sendto(drv->test_socket, "DISASSOC", 8, 0,
281 (struct sockaddr *) &drv->hostapd_addr,
282 sizeof(drv->hostapd_addr)) < 0) {
283 perror("sendto(test_socket)");
284 return -1;
286 return 0;
290 static int wpa_driver_test_deauthenticate(void *priv, const u8 *addr,
291 int reason_code)
293 struct wpa_driver_test_data *drv = priv;
294 wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d",
295 __func__, MAC2STR(addr), reason_code);
296 os_memset(drv->bssid, 0, ETH_ALEN);
297 drv->associated = 0;
298 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
299 return wpa_driver_test_send_disassoc(drv);
303 static int wpa_driver_test_disassociate(void *priv, const u8 *addr,
304 int reason_code)
306 struct wpa_driver_test_data *drv = priv;
307 wpa_printf(MSG_DEBUG, "%s addr=" MACSTR " reason_code=%d",
308 __func__, MAC2STR(addr), reason_code);
309 os_memset(drv->bssid, 0, ETH_ALEN);
310 drv->associated = 0;
311 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
312 return wpa_driver_test_send_disassoc(drv);
316 static void wpa_driver_test_scanresp(struct wpa_driver_test_data *drv,
317 struct sockaddr_un *from,
318 socklen_t fromlen,
319 const char *data)
321 struct wpa_scan_res *res;
322 const char *pos, *pos2;
323 size_t len;
324 u8 *ie_pos, *ie_start, *ie_end;
325 #define MAX_IE_LEN 1000
327 wpa_printf(MSG_DEBUG, "test_driver: SCANRESP %s", data);
328 if (drv->num_scanres >= MAX_SCAN_RESULTS) {
329 wpa_printf(MSG_DEBUG, "test_driver: No room for the new scan "
330 "result");
331 return;
334 /* SCANRESP BSSID SSID IEs */
336 res = os_zalloc(sizeof(*res) + MAX_IE_LEN);
337 if (res == NULL)
338 return;
339 ie_start = ie_pos = (u8 *) (res + 1);
340 ie_end = ie_pos + MAX_IE_LEN;
342 if (hwaddr_aton(data, res->bssid)) {
343 wpa_printf(MSG_DEBUG, "test_driver: invalid BSSID in scanres");
344 os_free(res);
345 return;
348 pos = data + 17;
349 while (*pos == ' ')
350 pos++;
351 pos2 = os_strchr(pos, ' ');
352 if (pos2 == NULL) {
353 wpa_printf(MSG_DEBUG, "test_driver: invalid SSID termination "
354 "in scanres");
355 os_free(res);
356 return;
358 len = (pos2 - pos) / 2;
359 if (len > 32)
360 len = 32;
362 * Generate SSID IE from the SSID field since this IE is not included
363 * in the main IE field.
365 *ie_pos++ = WLAN_EID_SSID;
366 *ie_pos++ = len;
367 if (hexstr2bin(pos, ie_pos, len) < 0) {
368 wpa_printf(MSG_DEBUG, "test_driver: invalid SSID in scanres");
369 os_free(res);
370 return;
372 ie_pos += len;
374 pos = pos2 + 1;
375 pos2 = os_strchr(pos, ' ');
376 if (pos2 == NULL)
377 len = os_strlen(pos) / 2;
378 else
379 len = (pos2 - pos) / 2;
380 if ((int) len > ie_end - ie_pos)
381 len = ie_end - ie_pos;
382 if (hexstr2bin(pos, ie_pos, len) < 0) {
383 wpa_printf(MSG_DEBUG, "test_driver: invalid IEs in scanres");
384 os_free(res);
385 return;
387 ie_pos += len;
388 res->ie_len = ie_pos - ie_start;
390 if (pos2) {
391 pos = pos2 + 1;
392 while (*pos == ' ')
393 pos++;
394 if (os_strncmp(pos, "PRIVACY", 7) == 0)
395 res->caps |= IEEE80211_CAP_PRIVACY;
398 os_free(drv->scanres[drv->num_scanres]);
399 drv->scanres[drv->num_scanres++] = res;
403 static void wpa_driver_test_assocresp(struct wpa_driver_test_data *drv,
404 struct sockaddr_un *from,
405 socklen_t fromlen,
406 const char *data)
408 /* ASSOCRESP BSSID <res> */
409 if (hwaddr_aton(data, drv->bssid)) {
410 wpa_printf(MSG_DEBUG, "test_driver: invalid BSSID in "
411 "assocresp");
413 if (drv->use_associnfo) {
414 union wpa_event_data event;
415 os_memset(&event, 0, sizeof(event));
416 event.assoc_info.req_ies = drv->assoc_wpa_ie;
417 event.assoc_info.req_ies_len = drv->assoc_wpa_ie_len;
418 wpa_supplicant_event(drv->ctx, EVENT_ASSOCINFO, &event);
420 drv->associated = 1;
421 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
425 static void wpa_driver_test_disassoc(struct wpa_driver_test_data *drv,
426 struct sockaddr_un *from,
427 socklen_t fromlen)
429 drv->associated = 0;
430 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
434 static void wpa_driver_test_eapol(struct wpa_driver_test_data *drv,
435 struct sockaddr_un *from,
436 socklen_t fromlen,
437 const u8 *data, size_t data_len)
439 const u8 *src = drv->bssid;
441 if (data_len > 14) {
442 /* Skip Ethernet header */
443 src = data + ETH_ALEN;
444 data += 14;
445 data_len -= 14;
447 wpa_supplicant_rx_eapol(drv->ctx, src, data, data_len);
451 static void wpa_driver_test_mlme(struct wpa_driver_test_data *drv,
452 struct sockaddr_un *from,
453 socklen_t fromlen,
454 const u8 *data, size_t data_len)
456 struct ieee80211_rx_status rx_status;
457 os_memset(&rx_status, 0, sizeof(rx_status));
458 wpa_supplicant_sta_rx(drv->ctx, data, data_len, &rx_status);
462 static void wpa_driver_test_receive_unix(int sock, void *eloop_ctx,
463 void *sock_ctx)
465 struct wpa_driver_test_data *drv = eloop_ctx;
466 char *buf;
467 int res;
468 struct sockaddr_un from;
469 socklen_t fromlen = sizeof(from);
470 const size_t buflen = 2000;
472 buf = os_malloc(buflen);
473 if (buf == NULL)
474 return;
475 res = recvfrom(sock, buf, buflen - 1, 0,
476 (struct sockaddr *) &from, &fromlen);
477 if (res < 0) {
478 perror("recvfrom(test_socket)");
479 os_free(buf);
480 return;
482 buf[res] = '\0';
484 wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
486 if (os_strncmp(buf, "SCANRESP ", 9) == 0) {
487 wpa_driver_test_scanresp(drv, &from, fromlen, buf + 9);
488 } else if (os_strncmp(buf, "ASSOCRESP ", 10) == 0) {
489 wpa_driver_test_assocresp(drv, &from, fromlen, buf + 10);
490 } else if (os_strcmp(buf, "DISASSOC") == 0) {
491 wpa_driver_test_disassoc(drv, &from, fromlen);
492 } else if (os_strcmp(buf, "DEAUTH") == 0) {
493 wpa_driver_test_disassoc(drv, &from, fromlen);
494 } else if (os_strncmp(buf, "EAPOL ", 6) == 0) {
495 wpa_driver_test_eapol(drv, &from, fromlen,
496 (const u8 *) buf + 6, res - 6);
497 } else if (os_strncmp(buf, "MLME ", 5) == 0) {
498 wpa_driver_test_mlme(drv, &from, fromlen,
499 (const u8 *) buf + 5, res - 5);
500 } else {
501 wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
502 (u8 *) buf, res);
504 os_free(buf);
508 static void * wpa_driver_test_init(void *ctx, const char *ifname)
510 struct wpa_driver_test_data *drv;
512 drv = os_zalloc(sizeof(*drv));
513 if (drv == NULL)
514 return NULL;
515 drv->ctx = ctx;
516 drv->test_socket = -1;
518 /* Set dummy BSSID and SSID for testing. */
519 drv->bssid[0] = 0x02;
520 drv->bssid[1] = 0x00;
521 drv->bssid[2] = 0x00;
522 drv->bssid[3] = 0x00;
523 drv->bssid[4] = 0x00;
524 drv->bssid[5] = 0x01;
525 os_memcpy(drv->ssid, "test", 5);
526 drv->ssid_len = 4;
528 /* Generate a MAC address to help testing with multiple STAs */
529 drv->own_addr[0] = 0x02; /* locally administered */
530 sha1_prf((const u8 *) ifname, os_strlen(ifname),
531 "wpa_supplicant test mac addr generation",
532 NULL, 0, drv->own_addr + 1, ETH_ALEN - 1);
533 eloop_register_timeout(1, 0, wpa_driver_test_poll, drv, NULL);
535 return drv;
539 static void wpa_driver_test_close_test_socket(struct wpa_driver_test_data *drv)
541 if (drv->test_socket >= 0) {
542 eloop_unregister_read_sock(drv->test_socket);
543 close(drv->test_socket);
544 drv->test_socket = -1;
547 if (drv->own_socket_path) {
548 unlink(drv->own_socket_path);
549 os_free(drv->own_socket_path);
550 drv->own_socket_path = NULL;
555 static void wpa_driver_test_deinit(void *priv)
557 struct wpa_driver_test_data *drv = priv;
558 int i;
559 wpa_driver_test_close_test_socket(drv);
560 eloop_cancel_timeout(wpa_driver_test_scan_timeout, drv, drv->ctx);
561 eloop_cancel_timeout(wpa_driver_test_poll, drv, NULL);
562 os_free(drv->test_dir);
563 for (i = 0; i < MAX_SCAN_RESULTS; i++)
564 os_free(drv->scanres[i]);
565 os_free(drv);
569 static int wpa_driver_test_attach(struct wpa_driver_test_data *drv,
570 const char *dir)
572 static unsigned int counter = 0;
573 struct sockaddr_un addr;
574 size_t len;
576 os_free(drv->own_socket_path);
577 if (dir) {
578 len = os_strlen(dir) + 30;
579 drv->own_socket_path = os_malloc(len);
580 if (drv->own_socket_path == NULL)
581 return -1;
582 os_snprintf(drv->own_socket_path, len, "%s/STA-" MACSTR,
583 dir, MAC2STR(drv->own_addr));
584 } else {
585 drv->own_socket_path = os_malloc(100);
586 if (drv->own_socket_path == NULL)
587 return -1;
588 os_snprintf(drv->own_socket_path, 100,
589 "/tmp/wpa_supplicant_test-%d-%d",
590 getpid(), counter++);
593 drv->test_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
594 if (drv->test_socket < 0) {
595 perror("socket(PF_UNIX)");
596 os_free(drv->own_socket_path);
597 drv->own_socket_path = NULL;
598 return -1;
601 os_memset(&addr, 0, sizeof(addr));
602 addr.sun_family = AF_UNIX;
603 os_strlcpy(addr.sun_path, drv->own_socket_path, sizeof(addr.sun_path));
604 if (bind(drv->test_socket, (struct sockaddr *) &addr,
605 sizeof(addr)) < 0) {
606 perror("bind(PF_UNIX)");
607 close(drv->test_socket);
608 unlink(drv->own_socket_path);
609 os_free(drv->own_socket_path);
610 drv->own_socket_path = NULL;
611 return -1;
614 eloop_register_read_sock(drv->test_socket,
615 wpa_driver_test_receive_unix, drv, NULL);
617 return 0;
621 static int wpa_driver_test_set_param(void *priv, const char *param)
623 struct wpa_driver_test_data *drv = priv;
624 const char *pos, *pos2;
625 size_t len;
627 wpa_printf(MSG_DEBUG, "%s: param='%s'", __func__, param);
628 if (param == NULL)
629 return 0;
631 wpa_driver_test_close_test_socket(drv);
632 pos = os_strstr(param, "test_socket=");
633 if (pos) {
634 pos += 12;
635 pos2 = os_strchr(pos, ' ');
636 if (pos2)
637 len = pos2 - pos;
638 else
639 len = os_strlen(pos);
640 if (len > sizeof(drv->hostapd_addr.sun_path))
641 return -1;
642 os_memset(&drv->hostapd_addr, 0, sizeof(drv->hostapd_addr));
643 drv->hostapd_addr.sun_family = AF_UNIX;
644 os_memcpy(drv->hostapd_addr.sun_path, pos, len);
645 drv->hostapd_addr_set = 1;
648 pos = os_strstr(param, "test_dir=");
649 if (pos) {
650 char *end;
651 os_free(drv->test_dir);
652 drv->test_dir = os_strdup(pos + 9);
653 if (drv->test_dir == NULL)
654 return -1;
655 end = os_strchr(drv->test_dir, ' ');
656 if (end)
657 *end = '\0';
658 wpa_driver_test_attach(drv, drv->test_dir);
659 } else
660 wpa_driver_test_attach(drv, NULL);
662 if (os_strstr(param, "use_associnfo=1")) {
663 wpa_printf(MSG_DEBUG, "test_driver: Use AssocInfo events");
664 drv->use_associnfo = 1;
667 #ifdef CONFIG_CLIENT_MLME
668 if (os_strstr(param, "use_mlme=1")) {
669 wpa_printf(MSG_DEBUG, "test_driver: Use internal MLME");
670 drv->use_mlme = 1;
672 #endif /* CONFIG_CLIENT_MLME */
674 return 0;
678 static const u8 * wpa_driver_test_get_mac_addr(void *priv)
680 struct wpa_driver_test_data *drv = priv;
681 wpa_printf(MSG_DEBUG, "%s", __func__);
682 return drv->own_addr;
686 static int wpa_driver_test_send_eapol(void *priv, const u8 *dest, u16 proto,
687 const u8 *data, size_t data_len)
689 struct wpa_driver_test_data *drv = priv;
690 struct msghdr msg;
691 struct iovec io[3];
692 struct l2_ethhdr eth;
693 struct sockaddr_un addr;
695 wpa_hexdump(MSG_MSGDUMP, "test_send_eapol TX frame", data, data_len);
697 os_memset(&eth, 0, sizeof(eth));
698 os_memcpy(eth.h_dest, dest, ETH_ALEN);
699 os_memcpy(eth.h_source, drv->own_addr, ETH_ALEN);
700 eth.h_proto = host_to_be16(proto);
702 io[0].iov_base = "EAPOL ";
703 io[0].iov_len = 6;
704 io[1].iov_base = (u8 *) &eth;
705 io[1].iov_len = sizeof(eth);
706 io[2].iov_base = (u8 *) data;
707 io[2].iov_len = data_len;
709 os_memset(&msg, 0, sizeof(msg));
710 msg.msg_iov = io;
711 msg.msg_iovlen = 3;
712 if (os_memcmp(dest, drv->bssid, ETH_ALEN) == 0 ||
713 drv->test_dir == NULL) {
714 msg.msg_name = &drv->hostapd_addr;
715 msg.msg_namelen = sizeof(drv->hostapd_addr);
716 } else {
717 struct stat st;
718 os_memset(&addr, 0, sizeof(addr));
719 addr.sun_family = AF_UNIX;
720 os_snprintf(addr.sun_path, sizeof(addr.sun_path),
721 "%s/STA-" MACSTR, drv->test_dir, MAC2STR(dest));
722 if (stat(addr.sun_path, &st) < 0) {
723 os_snprintf(addr.sun_path, sizeof(addr.sun_path),
724 "%s/AP-" MACSTR,
725 drv->test_dir, MAC2STR(dest));
727 msg.msg_name = &addr;
728 msg.msg_namelen = sizeof(addr);
731 if (sendmsg(drv->test_socket, &msg, 0) < 0) {
732 perror("sendmsg(test_socket)");
733 return -1;
736 return 0;
740 static int wpa_driver_test_get_capa(void *priv, struct wpa_driver_capa *capa)
742 struct wpa_driver_test_data *drv = priv;
743 os_memset(capa, 0, sizeof(*capa));
744 capa->key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
745 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
746 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
747 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK |
748 WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE |
749 WPA_DRIVER_CAPA_KEY_MGMT_FT |
750 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
751 capa->enc = WPA_DRIVER_CAPA_ENC_WEP40 |
752 WPA_DRIVER_CAPA_ENC_WEP104 |
753 WPA_DRIVER_CAPA_ENC_TKIP |
754 WPA_DRIVER_CAPA_ENC_CCMP;
755 capa->auth = WPA_DRIVER_AUTH_OPEN |
756 WPA_DRIVER_AUTH_SHARED |
757 WPA_DRIVER_AUTH_LEAP;
758 if (drv->use_mlme)
759 capa->flags |= WPA_DRIVER_FLAGS_USER_SPACE_MLME;
761 return 0;
765 static int wpa_driver_test_mlme_setprotection(void *priv, const u8 *addr,
766 int protect_type,
767 int key_type)
769 wpa_printf(MSG_DEBUG, "%s: protect_type=%d key_type=%d",
770 __func__, protect_type, key_type);
772 if (addr) {
773 wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR,
774 __func__, MAC2STR(addr));
777 return 0;
781 #ifdef CONFIG_CLIENT_MLME
782 static struct wpa_hw_modes *
783 wpa_driver_test_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
785 struct wpa_hw_modes *modes;
787 *num_modes = 1;
788 *flags = 0;
789 modes = os_zalloc(*num_modes * sizeof(struct wpa_hw_modes));
790 if (modes == NULL)
791 return NULL;
792 modes[0].mode = WPA_MODE_IEEE80211G;
793 modes[0].num_channels = 1;
794 modes[0].num_rates = 1;
795 modes[0].channels = os_zalloc(sizeof(struct wpa_channel_data));
796 modes[0].rates = os_zalloc(sizeof(struct wpa_rate_data));
797 if (modes[0].channels == NULL || modes[0].rates == NULL) {
798 wpa_supplicant_sta_free_hw_features(modes, *num_modes);
799 return NULL;
801 modes[0].channels[0].chan = 1;
802 modes[0].channels[0].freq = 2412;
803 modes[0].channels[0].flag = WPA_CHAN_W_SCAN | WPA_CHAN_W_ACTIVE_SCAN;
804 modes[0].rates[0].rate = 10;
805 modes[0].rates[0].flags = WPA_RATE_BASIC | WPA_RATE_SUPPORTED |
806 WPA_RATE_CCK | WPA_RATE_MANDATORY;
808 return modes;
812 int wpa_driver_test_set_channel(void *priv, wpa_hw_mode phymode, int chan,
813 int freq)
815 wpa_printf(MSG_DEBUG, "%s: phymode=%d chan=%d freq=%d",
816 __func__, phymode, chan, freq);
817 return 0;
821 static int wpa_driver_test_send_mlme(void *priv, const u8 *data,
822 size_t data_len)
824 struct wpa_driver_test_data *drv = priv;
825 struct msghdr msg;
826 struct iovec io[2];
827 struct sockaddr_un addr;
828 const u8 *dest;
829 struct dirent *dent;
830 DIR *dir;
832 wpa_hexdump(MSG_MSGDUMP, "test_send_mlme", data, data_len);
833 if (data_len < 10)
834 return -1;
835 dest = data + 4;
837 io[0].iov_base = "MLME ";
838 io[0].iov_len = 5;
839 io[1].iov_base = (u8 *) data;
840 io[1].iov_len = data_len;
842 os_memset(&msg, 0, sizeof(msg));
843 msg.msg_iov = io;
844 msg.msg_iovlen = 2;
845 if (os_memcmp(dest, drv->bssid, ETH_ALEN) == 0 ||
846 drv->test_dir == NULL) {
847 msg.msg_name = &drv->hostapd_addr;
848 msg.msg_namelen = sizeof(drv->hostapd_addr);
849 } else if (os_memcmp(dest, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0)
851 dir = opendir(drv->test_dir);
852 if (dir == NULL)
853 return -1;
854 while ((dent = readdir(dir))) {
855 #ifdef _DIRENT_HAVE_D_TYPE
856 /* Skip the file if it is not a socket.
857 * Also accept DT_UNKNOWN (0) in case
858 * the C library or underlying file
859 * system does not support d_type. */
860 if (dent->d_type != DT_SOCK &&
861 dent->d_type != DT_UNKNOWN)
862 continue;
863 #endif /* _DIRENT_HAVE_D_TYPE */
864 if (os_strcmp(dent->d_name, ".") == 0 ||
865 os_strcmp(dent->d_name, "..") == 0)
866 continue;
867 wpa_printf(MSG_DEBUG, "%s: Send broadcast MLME to %s",
868 __func__, dent->d_name);
869 os_memset(&addr, 0, sizeof(addr));
870 addr.sun_family = AF_UNIX;
871 os_snprintf(addr.sun_path, sizeof(addr.sun_path),
872 "%s/%s", drv->test_dir, dent->d_name);
874 msg.msg_name = &addr;
875 msg.msg_namelen = sizeof(addr);
877 if (sendmsg(drv->test_socket, &msg, 0) < 0)
878 perror("sendmsg(test_socket)");
880 closedir(dir);
881 return 0;
882 } else {
883 struct stat st;
884 os_memset(&addr, 0, sizeof(addr));
885 addr.sun_family = AF_UNIX;
886 os_snprintf(addr.sun_path, sizeof(addr.sun_path),
887 "%s/AP-" MACSTR, drv->test_dir, MAC2STR(dest));
888 if (stat(addr.sun_path, &st) < 0) {
889 os_snprintf(addr.sun_path, sizeof(addr.sun_path),
890 "%s/STA-" MACSTR,
891 drv->test_dir, MAC2STR(dest));
893 msg.msg_name = &addr;
894 msg.msg_namelen = sizeof(addr);
897 if (sendmsg(drv->test_socket, &msg, 0) < 0) {
898 perror("sendmsg(test_socket)");
899 return -1;
902 return 0;
906 static int wpa_driver_test_mlme_add_sta(void *priv, const u8 *addr,
907 const u8 *supp_rates,
908 size_t supp_rates_len)
910 wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, MAC2STR(addr));
911 return 0;
915 static int wpa_driver_test_mlme_remove_sta(void *priv, const u8 *addr)
917 wpa_printf(MSG_DEBUG, "%s: addr=" MACSTR, __func__, MAC2STR(addr));
918 return 0;
922 int wpa_driver_test_set_ssid(void *priv, const u8 *ssid, size_t ssid_len)
924 wpa_printf(MSG_DEBUG, "%s", __func__);
925 return 0;
929 int wpa_driver_test_set_bssid(void *priv, const u8 *bssid)
931 wpa_printf(MSG_DEBUG, "%s: bssid=" MACSTR, __func__, MAC2STR(bssid));
932 return 0;
934 #endif /* CONFIG_CLIENT_MLME */
937 const struct wpa_driver_ops wpa_driver_test_ops = {
938 "test",
939 "wpa_supplicant test driver",
940 wpa_driver_test_get_bssid,
941 wpa_driver_test_get_ssid,
942 wpa_driver_test_set_wpa,
943 wpa_driver_test_set_key,
944 wpa_driver_test_init,
945 wpa_driver_test_deinit,
946 wpa_driver_test_set_param,
947 NULL /* set_countermeasures */,
948 NULL /* set_drop_unencrypted */,
949 wpa_driver_test_scan,
950 NULL /* get_scan_results */,
951 wpa_driver_test_deauthenticate,
952 wpa_driver_test_disassociate,
953 wpa_driver_test_associate,
954 NULL /* set_auth_alg */,
955 NULL /* add_pmkid */,
956 NULL /* remove_pmkid */,
957 NULL /* flush_pmkid */,
958 wpa_driver_test_get_capa,
959 NULL /* poll */,
960 NULL /* get_ifname */,
961 wpa_driver_test_get_mac_addr,
962 wpa_driver_test_send_eapol,
963 NULL /* set_operstate */,
964 wpa_driver_test_mlme_setprotection,
965 #ifdef CONFIG_CLIENT_MLME
966 wpa_driver_test_get_hw_feature_data,
967 wpa_driver_test_set_channel,
968 wpa_driver_test_set_ssid,
969 wpa_driver_test_set_bssid,
970 wpa_driver_test_send_mlme,
971 wpa_driver_test_mlme_add_sta,
972 wpa_driver_test_mlme_remove_sta,
973 #else /* CONFIG_CLIENT_MLME */
974 NULL /* get_hw_feature_data */,
975 NULL /* set_channel */,
976 NULL /* set_ssid */,
977 NULL /* set_bssid */,
978 NULL /* send_mlme */,
979 NULL /* mlme_add_sta */,
980 NULL /* mlme_remove_sta */,
981 #endif /* CONFIG_CLIENT_MLME */
982 NULL /* update_ft_ies */,
983 NULL /* send_ft_action */,
984 wpa_driver_test_get_scan_results2,
985 NULL /* set_probe_req_ie */