Added preliminary Wi-Fi Protected Setup (WPS) implementation
[mech_eap.git] / hostapd / driver_test.c
1 /*
2  * hostapd / Driver interface for development testing
3  * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16 #include <sys/un.h>
17 #include <dirent.h>
18
19 #include "hostapd.h"
20 #include "driver.h"
21 #include "sha1.h"
22 #include "eloop.h"
23 #include "ieee802_1x.h"
24 #include "sta_info.h"
25 #include "wpa.h"
26 #include "accounting.h"
27 #include "radius/radius.h"
28 #include "l2_packet/l2_packet.h"
29 #include "ieee802_11.h"
30 #include "hw_features.h"
31 #include "wps_hostapd.h"
32
33
34 struct test_client_socket {
35         struct test_client_socket *next;
36         u8 addr[ETH_ALEN];
37         struct sockaddr_un un;
38         socklen_t unlen;
39         struct test_driver_bss *bss;
40 };
41
42 struct test_driver_bss {
43         struct test_driver_bss *next;
44         char ifname[IFNAMSIZ + 1];
45         u8 bssid[ETH_ALEN];
46         u8 *ie;
47         size_t ielen;
48         u8 *wps_beacon_ie;
49         size_t wps_beacon_ie_len;
50         u8 *wps_probe_resp_ie;
51         size_t wps_probe_resp_ie_len;
52         u8 ssid[32];
53         size_t ssid_len;
54         int privacy;
55 };
56
57 struct test_driver_data {
58         struct hostapd_data *hapd;
59         struct test_client_socket *cli;
60         int test_socket;
61         struct test_driver_bss *bss;
62         char *socket_dir;
63         char *own_socket_path;
64 };
65
66
67 static void test_driver_free_bss(struct test_driver_bss *bss)
68 {
69         free(bss->ie);
70         free(bss->wps_beacon_ie);
71         free(bss->wps_probe_resp_ie);
72         free(bss);
73 }
74
75
76 static void test_driver_free_priv(struct test_driver_data *drv)
77 {
78         struct test_driver_bss *bss, *prev;
79
80         if (drv == NULL)
81                 return;
82
83         bss = drv->bss;
84         while (bss) {
85                 prev = bss;
86                 bss = bss->next;
87                 test_driver_free_bss(prev);
88         }
89         free(drv->own_socket_path);
90         free(drv->socket_dir);
91         free(drv);
92 }
93
94
95 static struct test_client_socket *
96 test_driver_get_cli(struct test_driver_data *drv, struct sockaddr_un *from,
97                     socklen_t fromlen)
98 {
99         struct test_client_socket *cli = drv->cli;
100
101         while (cli) {
102                 if (cli->unlen == fromlen &&
103                     strncmp(cli->un.sun_path, from->sun_path,
104                             fromlen - sizeof(cli->un.sun_family)) == 0)
105                         return cli;
106                 cli = cli->next;
107         }
108
109         return NULL;
110 }
111
112
113 static int test_driver_send_eapol(void *priv, const u8 *addr, const u8 *data,
114                                   size_t data_len, int encrypt,
115                                   const u8 *own_addr)
116 {
117         struct test_driver_data *drv = priv;
118         struct test_client_socket *cli;
119         struct msghdr msg;
120         struct iovec io[3];
121         struct l2_ethhdr eth;
122
123         if (drv->test_socket < 0)
124                 return -1;
125
126         cli = drv->cli;
127         while (cli) {
128                 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
129                         break;
130                 cli = cli->next;
131         }
132
133         if (!cli) {
134                 wpa_printf(MSG_DEBUG, "%s: no destination client entry",
135                            __func__);
136                 return -1;
137         }
138
139         memcpy(eth.h_dest, addr, ETH_ALEN);
140         memcpy(eth.h_source, own_addr, ETH_ALEN);
141         eth.h_proto = htons(ETH_P_EAPOL);
142
143         io[0].iov_base = "EAPOL ";
144         io[0].iov_len = 6;
145         io[1].iov_base = &eth;
146         io[1].iov_len = sizeof(eth);
147         io[2].iov_base = (u8 *) data;
148         io[2].iov_len = data_len;
149
150         memset(&msg, 0, sizeof(msg));
151         msg.msg_iov = io;
152         msg.msg_iovlen = 3;
153         msg.msg_name = &cli->un;
154         msg.msg_namelen = cli->unlen;
155         return sendmsg(drv->test_socket, &msg, 0);
156 }
157
158
159 static int test_driver_send_ether(void *priv, const u8 *dst, const u8 *src,
160                                   u16 proto, const u8 *data, size_t data_len)
161 {
162         struct test_driver_data *drv = priv;
163         struct msghdr msg;
164         struct iovec io[3];
165         struct l2_ethhdr eth;
166         char desttxt[30];
167         struct sockaddr_un addr;
168         struct dirent *dent;
169         DIR *dir;
170         int ret = 0, broadcast = 0, count = 0;
171
172         if (drv->test_socket < 0 || drv->socket_dir == NULL) {
173                 wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d "
174                            "socket_dir=%p)",
175                            __func__, drv->test_socket, drv->socket_dir);
176                 return -1;
177         }
178
179         broadcast = memcmp(dst, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
180         snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dst));
181
182         memcpy(eth.h_dest, dst, ETH_ALEN);
183         memcpy(eth.h_source, src, ETH_ALEN);
184         eth.h_proto = htons(proto);
185
186         io[0].iov_base = "ETHER ";
187         io[0].iov_len = 6;
188         io[1].iov_base = &eth;
189         io[1].iov_len = sizeof(eth);
190         io[2].iov_base = (u8 *) data;
191         io[2].iov_len = data_len;
192
193         memset(&msg, 0, sizeof(msg));
194         msg.msg_iov = io;
195         msg.msg_iovlen = 3;
196
197         dir = opendir(drv->socket_dir);
198         if (dir == NULL) {
199                 perror("test_driver: opendir");
200                 return -1;
201         }
202         while ((dent = readdir(dir))) {
203 #ifdef _DIRENT_HAVE_D_TYPE
204                 /* Skip the file if it is not a socket. Also accept
205                  * DT_UNKNOWN (0) in case the C library or underlying file
206                  * system does not support d_type. */
207                 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
208                         continue;
209 #endif /* _DIRENT_HAVE_D_TYPE */
210                 if (strcmp(dent->d_name, ".") == 0 ||
211                     strcmp(dent->d_name, "..") == 0)
212                         continue;
213
214                 memset(&addr, 0, sizeof(addr));
215                 addr.sun_family = AF_UNIX;
216                 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
217                          drv->socket_dir, dent->d_name);
218
219                 if (strcmp(addr.sun_path, drv->own_socket_path) == 0)
220                         continue;
221                 if (!broadcast && strstr(dent->d_name, desttxt) == NULL)
222                         continue;
223
224                 wpa_printf(MSG_DEBUG, "%s: Send ether frame to %s",
225                            __func__, dent->d_name);
226
227                 msg.msg_name = &addr;
228                 msg.msg_namelen = sizeof(addr);
229                 ret = sendmsg(drv->test_socket, &msg, 0);
230                 if (ret < 0)
231                         perror("driver_test: sendmsg");
232                 count++;
233         }
234         closedir(dir);
235
236         if (!broadcast && count == 0) {
237                 wpa_printf(MSG_DEBUG, "%s: Destination " MACSTR " not found",
238                            __func__, MAC2STR(dst));
239                 return -1;
240         }
241
242         return ret;
243 }
244
245
246 static int test_driver_send_mgmt_frame(void *priv, const void *buf,
247                                        size_t len, int flags)
248 {
249         struct test_driver_data *drv = priv;
250         struct msghdr msg;
251         struct iovec io[2];
252         const u8 *dest;
253         int ret = 0, broadcast = 0;
254         char desttxt[30];
255         struct sockaddr_un addr;
256         struct dirent *dent;
257         DIR *dir;
258         struct ieee80211_hdr *hdr;
259         u16 fc;
260
261         if (drv->test_socket < 0 || len < 10 || drv->socket_dir == NULL) {
262                 wpa_printf(MSG_DEBUG, "%s: invalid parameters (sock=%d len=%lu"
263                            " socket_dir=%p)",
264                            __func__, drv->test_socket, (unsigned long) len,
265                            drv->socket_dir);
266                 return -1;
267         }
268
269         dest = buf;
270         dest += 4;
271         broadcast = memcmp(dest, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0;
272         snprintf(desttxt, sizeof(desttxt), MACSTR, MAC2STR(dest));
273
274         io[0].iov_base = "MLME ";
275         io[0].iov_len = 5;
276         io[1].iov_base = (void *) buf;
277         io[1].iov_len = len;
278
279         memset(&msg, 0, sizeof(msg));
280         msg.msg_iov = io;
281         msg.msg_iovlen = 2;
282
283         dir = opendir(drv->socket_dir);
284         if (dir == NULL) {
285                 perror("test_driver: opendir");
286                 return -1;
287         }
288         while ((dent = readdir(dir))) {
289 #ifdef _DIRENT_HAVE_D_TYPE
290                 /* Skip the file if it is not a socket. Also accept
291                  * DT_UNKNOWN (0) in case the C library or underlying file
292                  * system does not support d_type. */
293                 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
294                         continue;
295 #endif /* _DIRENT_HAVE_D_TYPE */
296                 if (strcmp(dent->d_name, ".") == 0 ||
297                     strcmp(dent->d_name, "..") == 0)
298                         continue;
299
300                 memset(&addr, 0, sizeof(addr));
301                 addr.sun_family = AF_UNIX;
302                 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s/%s",
303                          drv->socket_dir, dent->d_name);
304
305                 if (strcmp(addr.sun_path, drv->own_socket_path) == 0)
306                         continue;
307                 if (!broadcast && strstr(dent->d_name, desttxt) == NULL)
308                         continue;
309
310                 wpa_printf(MSG_DEBUG, "%s: Send management frame to %s",
311                            __func__, dent->d_name);
312
313                 msg.msg_name = &addr;
314                 msg.msg_namelen = sizeof(addr);
315                 ret = sendmsg(drv->test_socket, &msg, 0);
316                 if (ret < 0)
317                         perror("driver_test: sendmsg");
318         }
319         closedir(dir);
320
321         hdr = (struct ieee80211_hdr *) buf;
322         fc = le_to_host16(hdr->frame_control);
323         ieee802_11_mgmt_cb(drv->hapd, (u8 *) buf, len, WLAN_FC_GET_STYPE(fc),
324                            ret >= 0);
325
326         return ret;
327 }
328
329
330 static void test_driver_scan(struct test_driver_data *drv,
331                              struct sockaddr_un *from, socklen_t fromlen,
332                              char *data)
333 {
334         char buf[512], *pos, *end;
335         int ret;
336         struct test_driver_bss *bss;
337         u8 sa[ETH_ALEN];
338         u8 ie[512];
339         size_t ielen;
340
341         /* data: optional [ ' ' | STA-addr | ' ' | IEs(hex) ] */
342
343         wpa_printf(MSG_DEBUG, "test_driver: SCAN");
344
345         if (*data) {
346                 if (*data != ' ' ||
347                     hwaddr_aton(data + 1, sa)) {
348                         wpa_printf(MSG_DEBUG, "test_driver: Unexpected SCAN "
349                                    "command format");
350                         return;
351                 }
352
353                 data += 18;
354                 while (*data == ' ')
355                         data++;
356                 ielen = os_strlen(data) / 2;
357                 if (ielen > sizeof(ie))
358                         ielen = sizeof(ie);
359                 if (hexstr2bin(data, ie, ielen) < 0)
360                         ielen = 0;
361
362                 wpa_printf(MSG_DEBUG, "test_driver: Scan from " MACSTR,
363                            MAC2STR(sa));
364                 wpa_hexdump(MSG_MSGDUMP, "test_driver: scan IEs", ie, ielen);
365
366                 hostapd_wps_probe_req_rx(drv->hapd, sa, ie, ielen);
367         }
368
369         for (bss = drv->bss; bss; bss = bss->next) {
370                 pos = buf;
371                 end = buf + sizeof(buf);
372
373                 /* reply: SCANRESP BSSID SSID IEs */
374                 ret = snprintf(pos, end - pos, "SCANRESP " MACSTR " ",
375                                MAC2STR(bss->bssid));
376                 if (ret < 0 || ret >= end - pos)
377                         return;
378                 pos += ret;
379                 pos += wpa_snprintf_hex(pos, end - pos,
380                                         bss->ssid, bss->ssid_len);
381                 ret = snprintf(pos, end - pos, " ");
382                 if (ret < 0 || ret >= end - pos)
383                         return;
384                 pos += ret;
385                 pos += wpa_snprintf_hex(pos, end - pos, bss->ie, bss->ielen);
386                 pos += wpa_snprintf_hex(pos, end - pos, bss->wps_probe_resp_ie,
387                                         bss->wps_probe_resp_ie_len);
388
389                 if (bss->privacy) {
390                         ret = snprintf(pos, end - pos, " PRIVACY");
391                         if (ret < 0 || ret >= end - pos)
392                                 return;
393                         pos += ret;
394                 }
395
396                 sendto(drv->test_socket, buf, pos - buf, 0,
397                        (struct sockaddr *) from, fromlen);
398         }
399 }
400
401
402 static struct hostapd_data * test_driver_get_hapd(struct test_driver_data *drv,
403                                                   struct test_driver_bss *bss)
404 {
405         struct hostapd_iface *iface = drv->hapd->iface;
406         struct hostapd_data *hapd = NULL;
407         size_t i;
408
409         if (bss == NULL) {
410                 wpa_printf(MSG_DEBUG, "%s: bss == NULL", __func__);
411                 return NULL;
412         }
413
414         for (i = 0; i < iface->num_bss; i++) {
415                 hapd = iface->bss[i];
416                 if (memcmp(hapd->own_addr, bss->bssid, ETH_ALEN) == 0)
417                         break;
418         }
419         if (i == iface->num_bss) {
420                 wpa_printf(MSG_DEBUG, "%s: no matching interface entry found "
421                            "for BSSID " MACSTR, __func__, MAC2STR(bss->bssid));
422                 return NULL;
423         }
424
425         return hapd;
426 }
427
428
429 static int test_driver_new_sta(struct test_driver_data *drv,
430                                struct test_driver_bss *bss, const u8 *addr,
431                                const u8 *ie, size_t ielen)
432 {
433         struct hostapd_data *hapd;
434         struct sta_info *sta;
435         int new_assoc, res;
436
437         hapd = test_driver_get_hapd(drv, bss);
438         if (hapd == NULL)
439                 return -1;
440
441         hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
442                 HOSTAPD_LEVEL_INFO, "associated");
443
444         sta = ap_get_sta(hapd, addr);
445         if (sta) {
446                 accounting_sta_stop(hapd, sta);
447         } else {
448                 sta = ap_sta_add(hapd, addr);
449                 if (sta == NULL)
450                         return -1;
451         }
452
453         if (hapd->conf->wpa) {
454                 if (ie == NULL || ielen == 0) {
455                         printf("test_driver: no IE from STA\n");
456                         return -1;
457                 }
458                 if (sta->wpa_sm == NULL)
459                         sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
460                                                         sta->addr);
461                 if (sta->wpa_sm == NULL) {
462                         printf("test_driver: Failed to initialize WPA state "
463                                "machine\n");
464                         return -1;
465                 }
466                 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
467                                           ie, ielen, NULL, 0);
468                 if (res != WPA_IE_OK) {
469                         printf("WPA/RSN information element rejected? "
470                                "(res %u)\n", res);
471                         return -1;
472                 }
473         }
474
475         new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
476         sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
477         wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
478
479         hostapd_new_assoc_sta(hapd, sta, !new_assoc);
480
481         ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
482
483         return 0;
484 }
485
486
487 static void test_driver_assoc(struct test_driver_data *drv,
488                               struct sockaddr_un *from, socklen_t fromlen,
489                               char *data)
490 {
491         struct test_client_socket *cli;
492         u8 ie[256], ssid[32];
493         size_t ielen, ssid_len = 0;
494         char *pos, *pos2, cmd[50];
495         struct test_driver_bss *bss;
496
497         /* data: STA-addr SSID(hex) IEs(hex) */
498
499         cli = os_zalloc(sizeof(*cli));
500         if (cli == NULL)
501                 return;
502
503         if (hwaddr_aton(data, cli->addr)) {
504                 printf("test_socket: Invalid MAC address '%s' in ASSOC\n",
505                        data);
506                 free(cli);
507                 return;
508         }
509         pos = data + 17;
510         while (*pos == ' ')
511                 pos++;
512         pos2 = strchr(pos, ' ');
513         ielen = 0;
514         if (pos2) {
515                 ssid_len = (pos2 - pos) / 2;
516                 if (hexstr2bin(pos, ssid, ssid_len) < 0) {
517                         wpa_printf(MSG_DEBUG, "%s: Invalid SSID", __func__);
518                         free(cli);
519                         return;
520                 }
521                 wpa_hexdump_ascii(MSG_DEBUG, "test_driver_assoc: SSID",
522                                   ssid, ssid_len);
523
524                 pos = pos2 + 1;
525                 ielen = strlen(pos) / 2;
526                 if (ielen > sizeof(ie))
527                         ielen = sizeof(ie);
528                 if (hexstr2bin(pos, ie, ielen) < 0)
529                         ielen = 0;
530         }
531
532         for (bss = drv->bss; bss; bss = bss->next) {
533                 if (bss->ssid_len == ssid_len &&
534                     memcmp(bss->ssid, ssid, ssid_len) == 0)
535                         break;
536         }
537         if (bss == NULL) {
538                 wpa_printf(MSG_DEBUG, "%s: No matching SSID found from "
539                            "configured BSSes", __func__);
540                 free(cli);
541                 return;
542         }
543
544         cli->bss = bss;
545         memcpy(&cli->un, from, sizeof(cli->un));
546         cli->unlen = fromlen;
547         cli->next = drv->cli;
548         drv->cli = cli;
549         wpa_hexdump_ascii(MSG_DEBUG, "test_socket: ASSOC sun_path",
550                           (const u8 *) cli->un.sun_path,
551                           cli->unlen - sizeof(cli->un.sun_family));
552
553         snprintf(cmd, sizeof(cmd), "ASSOCRESP " MACSTR " 0",
554                  MAC2STR(bss->bssid));
555         sendto(drv->test_socket, cmd, strlen(cmd), 0,
556                (struct sockaddr *) from, fromlen);
557
558         if (test_driver_new_sta(drv, bss, cli->addr, ie, ielen) < 0) {
559                 wpa_printf(MSG_DEBUG, "test_driver: failed to add new STA");
560         }
561 }
562
563
564 static void test_driver_disassoc(struct test_driver_data *drv,
565                                  struct sockaddr_un *from, socklen_t fromlen)
566 {
567         struct test_client_socket *cli;
568         struct sta_info *sta;
569
570         cli = test_driver_get_cli(drv, from, fromlen);
571         if (!cli)
572                 return;
573
574         hostapd_logger(drv->hapd, cli->addr, HOSTAPD_MODULE_IEEE80211,
575                        HOSTAPD_LEVEL_INFO, "disassociated");
576
577         sta = ap_get_sta(drv->hapd, cli->addr);
578         if (sta != NULL) {
579                 sta->flags &= ~WLAN_STA_ASSOC;
580                 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
581                 sta->acct_terminate_cause =
582                         RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
583                 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
584                 ap_free_sta(drv->hapd, sta);
585         }
586 }
587
588
589 static void test_driver_eapol(struct test_driver_data *drv,
590                               struct sockaddr_un *from, socklen_t fromlen,
591                               u8 *data, size_t datalen)
592 {
593         struct test_client_socket *cli;
594         if (datalen > 14) {
595                 u8 *proto = data + 2 * ETH_ALEN;
596                 /* Skip Ethernet header */
597                 wpa_printf(MSG_DEBUG, "test_driver: dst=" MACSTR " src="
598                            MACSTR " proto=%04x",
599                            MAC2STR(data), MAC2STR(data + ETH_ALEN),
600                            WPA_GET_BE16(proto));
601                 data += 14;
602                 datalen -= 14;
603         }
604         cli = test_driver_get_cli(drv, from, fromlen);
605         if (cli) {
606                 struct hostapd_data *hapd;
607                 hapd = test_driver_get_hapd(drv, cli->bss);
608                 if (hapd == NULL)
609                         return;
610                 ieee802_1x_receive(hapd, cli->addr, data, datalen);
611         } else {
612                 wpa_printf(MSG_DEBUG, "test_socket: EAPOL from unknown "
613                            "client");
614         }
615 }
616
617
618 static void test_driver_ether(struct test_driver_data *drv,
619                               struct sockaddr_un *from, socklen_t fromlen,
620                               u8 *data, size_t datalen)
621 {
622         struct l2_ethhdr *eth;
623
624         if (datalen < sizeof(*eth))
625                 return;
626
627         eth = (struct l2_ethhdr *) data;
628         wpa_printf(MSG_DEBUG, "test_driver: RX ETHER dst=" MACSTR " src="
629                    MACSTR " proto=%04x",
630                    MAC2STR(eth->h_dest), MAC2STR(eth->h_source),
631                    be_to_host16(eth->h_proto));
632
633 #ifdef CONFIG_IEEE80211R
634         if (be_to_host16(eth->h_proto) == ETH_P_RRB) {
635                 wpa_ft_rrb_rx(drv->hapd->wpa_auth, eth->h_source,
636                               data + sizeof(*eth), datalen - sizeof(*eth));
637         }
638 #endif /* CONFIG_IEEE80211R */
639 }
640
641
642 static void test_driver_mlme(struct test_driver_data *drv,
643                              struct sockaddr_un *from, socklen_t fromlen,
644                              u8 *data, size_t datalen)
645 {
646         struct ieee80211_hdr *hdr;
647         u16 fc;
648
649         hdr = (struct ieee80211_hdr *) data;
650
651         if (test_driver_get_cli(drv, from, fromlen) == NULL && datalen >= 16) {
652                 struct test_client_socket *cli;
653                 cli = os_zalloc(sizeof(*cli));
654                 if (cli == NULL)
655                         return;
656                 wpa_printf(MSG_DEBUG, "Adding client entry for " MACSTR,
657                            MAC2STR(hdr->addr2));
658                 memcpy(cli->addr, hdr->addr2, ETH_ALEN);
659                 memcpy(&cli->un, from, sizeof(cli->un));
660                 cli->unlen = fromlen;
661                 cli->next = drv->cli;
662                 drv->cli = cli;
663         }
664
665         wpa_hexdump(MSG_MSGDUMP, "test_driver_mlme: received frame",
666                     data, datalen);
667         fc = le_to_host16(hdr->frame_control);
668         if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT) {
669                 wpa_printf(MSG_ERROR, "%s: received non-mgmt frame",
670                            __func__);
671                 return;
672         }
673         ieee802_11_mgmt(drv->hapd, data, datalen, WLAN_FC_GET_STYPE(fc), NULL);
674 }
675
676
677 static void test_driver_receive_unix(int sock, void *eloop_ctx, void *sock_ctx)
678 {
679         struct test_driver_data *drv = eloop_ctx;
680         char buf[2000];
681         int res;
682         struct sockaddr_un from;
683         socklen_t fromlen = sizeof(from);
684
685         res = recvfrom(sock, buf, sizeof(buf) - 1, 0,
686                        (struct sockaddr *) &from, &fromlen);
687         if (res < 0) {
688                 perror("recvfrom(test_socket)");
689                 return;
690         }
691         buf[res] = '\0';
692
693         wpa_printf(MSG_DEBUG, "test_driver: received %u bytes", res);
694
695         if (strncmp(buf, "SCAN", 4) == 0) {
696                 test_driver_scan(drv, &from, fromlen, buf + 4);
697         } else if (strncmp(buf, "ASSOC ", 6) == 0) {
698                 test_driver_assoc(drv, &from, fromlen, buf + 6);
699         } else if (strcmp(buf, "DISASSOC") == 0) {
700                 test_driver_disassoc(drv, &from, fromlen);
701         } else if (strncmp(buf, "EAPOL ", 6) == 0) {
702                 test_driver_eapol(drv, &from, fromlen, (u8 *) buf + 6,
703                                   res - 6);
704         } else if (strncmp(buf, "ETHER ", 6) == 0) {
705                 test_driver_ether(drv, &from, fromlen, (u8 *) buf + 6,
706                                   res - 6);
707         } else if (strncmp(buf, "MLME ", 5) == 0) {
708                 test_driver_mlme(drv, &from, fromlen, (u8 *) buf + 5, res - 5);
709         } else {
710                 wpa_hexdump_ascii(MSG_DEBUG, "Unknown test_socket command",
711                                   (u8 *) buf, res);
712         }
713 }
714
715
716 static struct test_driver_bss *
717 test_driver_get_bss(struct test_driver_data *drv, const char *ifname)
718 {
719         struct test_driver_bss *bss;
720
721         for (bss = drv->bss; bss; bss = bss->next) {
722                 if (strcmp(bss->ifname, ifname) == 0)
723                         return bss;
724         }
725         return NULL;
726 }
727
728
729 static int test_driver_set_generic_elem(const char *ifname, void *priv,
730                                         const u8 *elem, size_t elem_len)
731 {
732         struct test_driver_data *drv = priv;
733         struct test_driver_bss *bss;
734
735         bss = test_driver_get_bss(drv, ifname);
736         if (bss == NULL)
737                 return -1;
738
739         free(bss->ie);
740
741         if (elem == NULL) {
742                 bss->ie = NULL;
743                 bss->ielen = 0;
744                 return 0;
745         }
746
747         bss->ie = malloc(elem_len);
748         if (bss->ie == NULL) {
749                 bss->ielen = 0;
750                 return -1;
751         }
752
753         memcpy(bss->ie, elem, elem_len);
754         bss->ielen = elem_len;
755         return 0;
756 }
757
758
759 static int test_driver_set_wps_beacon_ie(const char *ifname, void *priv,
760                                          const u8 *ie, size_t len)
761 {
762         struct test_driver_data *drv = priv;
763         struct test_driver_bss *bss;
764
765         bss = test_driver_get_bss(drv, ifname);
766         if (bss == NULL)
767                 return -1;
768
769         free(bss->wps_beacon_ie);
770
771         if (ie == NULL) {
772                 bss->wps_beacon_ie = NULL;
773                 bss->wps_beacon_ie_len = 0;
774                 return 0;
775         }
776
777         bss->wps_beacon_ie = malloc(len);
778         if (bss->wps_beacon_ie == NULL) {
779                 bss->wps_beacon_ie_len = 0;
780                 return -1;
781         }
782
783         memcpy(bss->wps_beacon_ie, ie, len);
784         bss->wps_beacon_ie_len = len;
785         return 0;
786 }
787
788
789 static int test_driver_set_wps_probe_resp_ie(const char *ifname, void *priv,
790                                              const u8 *ie, size_t len)
791 {
792         struct test_driver_data *drv = priv;
793         struct test_driver_bss *bss;
794
795         bss = test_driver_get_bss(drv, ifname);
796         if (bss == NULL)
797                 return -1;
798
799         free(bss->wps_probe_resp_ie);
800
801         if (ie == NULL) {
802                 bss->wps_probe_resp_ie = NULL;
803                 bss->wps_probe_resp_ie_len = 0;
804                 return 0;
805         }
806
807         bss->wps_probe_resp_ie = malloc(len);
808         if (bss->wps_probe_resp_ie == NULL) {
809                 bss->wps_probe_resp_ie_len = 0;
810                 return -1;
811         }
812
813         memcpy(bss->wps_probe_resp_ie, ie, len);
814         bss->wps_probe_resp_ie_len = len;
815         return 0;
816 }
817
818
819 static int test_driver_sta_deauth(void *priv, const u8 *addr, int reason)
820 {
821         struct test_driver_data *drv = priv;
822         struct test_client_socket *cli;
823
824         if (drv->test_socket < 0)
825                 return -1;
826
827         cli = drv->cli;
828         while (cli) {
829                 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
830                         break;
831                 cli = cli->next;
832         }
833
834         if (!cli)
835                 return -1;
836
837         return sendto(drv->test_socket, "DEAUTH", 6, 0,
838                       (struct sockaddr *) &cli->un, cli->unlen);
839 }
840
841
842 static int test_driver_sta_disassoc(void *priv, const u8 *addr, int reason)
843 {
844         struct test_driver_data *drv = priv;
845         struct test_client_socket *cli;
846
847         if (drv->test_socket < 0)
848                 return -1;
849
850         cli = drv->cli;
851         while (cli) {
852                 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
853                         break;
854                 cli = cli->next;
855         }
856
857         if (!cli)
858                 return -1;
859
860         return sendto(drv->test_socket, "DISASSOC", 8, 0,
861                       (struct sockaddr *) &cli->un, cli->unlen);
862 }
863
864
865 static struct hostapd_hw_modes *
866 test_driver_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
867 {
868         struct hostapd_hw_modes *modes;
869
870         *num_modes = 3;
871         *flags = 0;
872         modes = os_zalloc(*num_modes * sizeof(struct hostapd_hw_modes));
873         if (modes == NULL)
874                 return NULL;
875         modes[0].mode = HOSTAPD_MODE_IEEE80211G;
876         modes[0].num_channels = 1;
877         modes[0].num_rates = 1;
878         modes[0].channels = os_zalloc(sizeof(struct hostapd_channel_data));
879         modes[0].rates = os_zalloc(sizeof(struct hostapd_rate_data));
880         if (modes[0].channels == NULL || modes[0].rates == NULL) {
881                 hostapd_free_hw_features(modes, *num_modes);
882                 return NULL;
883         }
884         modes[0].channels[0].chan = 1;
885         modes[0].channels[0].freq = 2412;
886         modes[0].channels[0].flag = 0;
887         modes[0].rates[0].rate = 10;
888         modes[0].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
889                 HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
890
891         modes[1].mode = HOSTAPD_MODE_IEEE80211B;
892         modes[1].num_channels = 1;
893         modes[1].num_rates = 1;
894         modes[1].channels = os_zalloc(sizeof(struct hostapd_channel_data));
895         modes[1].rates = os_zalloc(sizeof(struct hostapd_rate_data));
896         if (modes[1].channels == NULL || modes[1].rates == NULL) {
897                 hostapd_free_hw_features(modes, *num_modes);
898                 return NULL;
899         }
900         modes[1].channels[0].chan = 1;
901         modes[1].channels[0].freq = 2412;
902         modes[1].channels[0].flag = 0;
903         modes[1].rates[0].rate = 10;
904         modes[1].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
905                 HOSTAPD_RATE_CCK | HOSTAPD_RATE_MANDATORY;
906
907         modes[2].mode = HOSTAPD_MODE_IEEE80211A;
908         modes[2].num_channels = 1;
909         modes[2].num_rates = 1;
910         modes[2].channels = os_zalloc(sizeof(struct hostapd_channel_data));
911         modes[2].rates = os_zalloc(sizeof(struct hostapd_rate_data));
912         if (modes[2].channels == NULL || modes[2].rates == NULL) {
913                 hostapd_free_hw_features(modes, *num_modes);
914                 return NULL;
915         }
916         modes[2].channels[0].chan = 60;
917         modes[2].channels[0].freq = 5300;
918         modes[2].channels[0].flag = 0;
919         modes[2].rates[0].rate = 60;
920         modes[2].rates[0].flags = HOSTAPD_RATE_BASIC | HOSTAPD_RATE_SUPPORTED |
921                 HOSTAPD_RATE_MANDATORY;
922
923         return modes;
924 }
925
926
927 static int test_driver_bss_add(void *priv, const char *ifname, const u8 *bssid)
928 {
929         struct test_driver_data *drv = priv;
930         struct test_driver_bss *bss;
931
932         wpa_printf(MSG_DEBUG, "%s(ifname=%s bssid=" MACSTR ")",
933                    __func__, ifname, MAC2STR(bssid));
934
935         bss = os_zalloc(sizeof(*bss));
936         if (bss == NULL)
937                 return -1;
938
939         os_strlcpy(bss->ifname, ifname, IFNAMSIZ);
940         memcpy(bss->bssid, bssid, ETH_ALEN);
941
942         bss->next = drv->bss;
943         drv->bss = bss;
944
945         return 0;
946 }
947
948
949 static int test_driver_bss_remove(void *priv, const char *ifname)
950 {
951         struct test_driver_data *drv = priv;
952         struct test_driver_bss *bss, *prev;
953         struct test_client_socket *cli, *prev_c;
954
955         wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
956
957         for (prev = NULL, bss = drv->bss; bss; prev = bss, bss = bss->next) {
958                 if (strcmp(bss->ifname, ifname) != 0)
959                         continue;
960
961                 if (prev)
962                         prev->next = bss->next;
963                 else
964                         drv->bss = bss->next;
965
966                 for (prev_c = NULL, cli = drv->cli; cli;
967                      prev_c = cli, cli = cli->next) {
968                         if (cli->bss != bss)
969                                 continue;
970                         if (prev_c)
971                                 prev_c->next = cli->next;
972                         else
973                                 drv->cli = cli->next;
974                         free(cli);
975                         break;
976                 }
977
978                 test_driver_free_bss(bss);
979                 return 0;
980         }
981
982         return -1;
983 }
984
985
986 static int test_driver_if_add(const char *iface, void *priv,
987                               enum hostapd_driver_if_type type, char *ifname,
988                               const u8 *addr)
989 {
990         wpa_printf(MSG_DEBUG, "%s(iface=%s type=%d ifname=%s)",
991                    __func__, iface, type, ifname);
992         return 0;
993 }
994
995
996 static int test_driver_if_update(void *priv, enum hostapd_driver_if_type type,
997                                  char *ifname, const u8 *addr)
998 {
999         wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
1000         return 0;
1001 }
1002
1003
1004 static int test_driver_if_remove(void *priv, enum hostapd_driver_if_type type,
1005                                  const char *ifname, const u8 *addr)
1006 {
1007         wpa_printf(MSG_DEBUG, "%s(type=%d ifname=%s)", __func__, type, ifname);
1008         return 0;
1009 }
1010
1011
1012 static int test_driver_valid_bss_mask(void *priv, const u8 *addr,
1013                                       const u8 *mask)
1014 {
1015         return 0;
1016 }
1017
1018
1019 static int test_driver_set_ssid(const char *ifname, void *priv, const u8 *buf,
1020                                 int len)
1021 {
1022         struct test_driver_data *drv = priv;
1023         struct test_driver_bss *bss;
1024
1025         wpa_printf(MSG_DEBUG, "%s(ifname=%s)", __func__, ifname);
1026         wpa_hexdump_ascii(MSG_DEBUG, "test_driver_set_ssid: SSID", buf, len);
1027
1028         for (bss = drv->bss; bss; bss = bss->next) {
1029                 if (strcmp(bss->ifname, ifname) != 0)
1030                         continue;
1031
1032                 if (len < 0 || (size_t) len > sizeof(bss->ssid))
1033                         return -1;
1034
1035                 memcpy(bss->ssid, buf, len);
1036                 bss->ssid_len = len;
1037
1038                 return 0;
1039         }
1040
1041         return -1;
1042 }
1043
1044
1045 static int test_driver_set_privacy(const char *ifname, void *priv, int enabled)
1046 {
1047         struct test_driver_data *drv = priv;
1048         struct test_driver_bss *bss;
1049
1050         wpa_printf(MSG_DEBUG, "%s(ifname=%s enabled=%d)",
1051                    __func__, ifname, enabled);
1052
1053         for (bss = drv->bss; bss; bss = bss->next) {
1054                 if (strcmp(bss->ifname, ifname) != 0)
1055                         continue;
1056
1057                 bss->privacy = enabled;
1058
1059                 return 0;
1060         }
1061
1062         return -1;
1063 }
1064
1065
1066 static int test_driver_set_encryption(const char *iface, void *priv,
1067                                       const char *alg, const u8 *addr, int idx,
1068                                       const u8 *key, size_t key_len, int txkey)
1069 {
1070         wpa_printf(MSG_DEBUG, "%s(iface=%s alg=%s idx=%d txkey=%d)",
1071                    __func__, iface, alg, idx, txkey);
1072         if (addr)
1073                 wpa_printf(MSG_DEBUG, "   addr=" MACSTR, MAC2STR(addr));
1074         if (key)
1075                 wpa_hexdump_key(MSG_DEBUG, "   key", key, key_len);
1076         return 0;
1077 }
1078
1079
1080 static int test_driver_set_sta_vlan(void *priv, const u8 *addr,
1081                                     const char *ifname, int vlan_id)
1082 {
1083         wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " ifname=%s vlan_id=%d)",
1084                    __func__, MAC2STR(addr), ifname, vlan_id);
1085         return 0;
1086 }
1087
1088
1089 static int test_driver_sta_add(const char *ifname, void *priv, const u8 *addr,
1090                                u16 aid, u16 capability, u8 *supp_rates,
1091                                size_t supp_rates_len, int flags,
1092                                u16 listen_interval)
1093 {
1094         struct test_driver_data *drv = priv;
1095         struct test_client_socket *cli;
1096         struct test_driver_bss *bss;
1097
1098         wpa_printf(MSG_DEBUG, "%s(ifname=%s addr=" MACSTR " aid=%d "
1099                    "capability=0x%x flags=0x%x listen_interval=%d)",
1100                    __func__, ifname, MAC2STR(addr), aid, capability, flags,
1101                    listen_interval);
1102         wpa_hexdump(MSG_DEBUG, "test_driver_sta_add - supp_rates",
1103                     supp_rates, supp_rates_len);
1104
1105         cli = drv->cli;
1106         while (cli) {
1107                 if (memcmp(cli->addr, addr, ETH_ALEN) == 0)
1108                         break;
1109                 cli = cli->next;
1110         }
1111         if (!cli) {
1112                 wpa_printf(MSG_DEBUG, "%s: no matching client entry",
1113                            __func__);
1114                 return -1;
1115         }
1116
1117         for (bss = drv->bss; bss; bss = bss->next) {
1118                 if (strcmp(ifname, bss->ifname) == 0)
1119                         break;
1120         }
1121         if (bss == NULL) {
1122                 wpa_printf(MSG_DEBUG, "%s: No matching interface found from "
1123                            "configured BSSes", __func__);
1124                 return -1;
1125         }
1126
1127         cli->bss = bss;
1128
1129         return 0;
1130 }
1131
1132
1133 static void * test_driver_init(struct hostapd_data *hapd)
1134 {
1135         struct test_driver_data *drv;
1136         struct sockaddr_un addr;
1137
1138         drv = os_zalloc(sizeof(struct test_driver_data));
1139         if (drv == NULL) {
1140                 printf("Could not allocate memory for test driver data\n");
1141                 return NULL;
1142         }
1143         drv->bss = os_zalloc(sizeof(*drv->bss));
1144         if (drv->bss == NULL) {
1145                 printf("Could not allocate memory for test driver BSS data\n");
1146                 free(drv);
1147                 return NULL;
1148         }
1149
1150         drv->hapd = hapd;
1151
1152         /* Generate a MAC address to help testing with multiple APs */
1153         hapd->own_addr[0] = 0x02; /* locally administered */
1154         sha1_prf((const u8 *) hapd->conf->iface, strlen(hapd->conf->iface),
1155                  "hostapd test bssid generation",
1156                  (const u8 *) hapd->conf->ssid.ssid, hapd->conf->ssid.ssid_len,
1157                  hapd->own_addr + 1, ETH_ALEN - 1);
1158
1159         os_strlcpy(drv->bss->ifname, hapd->conf->iface, IFNAMSIZ);
1160         memcpy(drv->bss->bssid, hapd->own_addr, ETH_ALEN);
1161
1162         if (hapd->conf->test_socket) {
1163                 if (strlen(hapd->conf->test_socket) >= sizeof(addr.sun_path)) {
1164                         printf("Too long test_socket path\n");
1165                         test_driver_free_priv(drv);
1166                         return NULL;
1167                 }
1168                 if (strncmp(hapd->conf->test_socket, "DIR:", 4) == 0) {
1169                         size_t len = strlen(hapd->conf->test_socket) + 30;
1170                         drv->socket_dir = strdup(hapd->conf->test_socket + 4);
1171                         drv->own_socket_path = malloc(len);
1172                         if (drv->own_socket_path) {
1173                                 snprintf(drv->own_socket_path, len,
1174                                          "%s/AP-" MACSTR,
1175                                          hapd->conf->test_socket + 4,
1176                                          MAC2STR(hapd->own_addr));
1177                         }
1178                 } else {
1179                         drv->own_socket_path = strdup(hapd->conf->test_socket);
1180                 }
1181                 if (drv->own_socket_path == NULL) {
1182                         test_driver_free_priv(drv);
1183                         return NULL;
1184                 }
1185
1186                 drv->test_socket = socket(PF_UNIX, SOCK_DGRAM, 0);
1187                 if (drv->test_socket < 0) {
1188                         perror("socket(PF_UNIX)");
1189                         test_driver_free_priv(drv);
1190                         return NULL;
1191                 }
1192
1193                 memset(&addr, 0, sizeof(addr));
1194                 addr.sun_family = AF_UNIX;
1195                 os_strlcpy(addr.sun_path, drv->own_socket_path,
1196                            sizeof(addr.sun_path));
1197                 if (bind(drv->test_socket, (struct sockaddr *) &addr,
1198                          sizeof(addr)) < 0) {
1199                         perror("bind(PF_UNIX)");
1200                         close(drv->test_socket);
1201                         unlink(drv->own_socket_path);
1202                         test_driver_free_priv(drv);
1203                         return NULL;
1204                 }
1205                 eloop_register_read_sock(drv->test_socket,
1206                                          test_driver_receive_unix, drv, NULL);
1207         } else
1208                 drv->test_socket = -1;
1209
1210         return drv;
1211 }
1212
1213
1214 static void test_driver_deinit(void *priv)
1215 {
1216         struct test_driver_data *drv = priv;
1217         struct test_client_socket *cli, *prev;
1218
1219         cli = drv->cli;
1220         while (cli) {
1221                 prev = cli;
1222                 cli = cli->next;
1223                 free(prev);
1224         }
1225
1226         if (drv->test_socket >= 0) {
1227                 eloop_unregister_read_sock(drv->test_socket);
1228                 close(drv->test_socket);
1229                 unlink(drv->own_socket_path);
1230         }
1231
1232         /* There should be only one BSS remaining at this point. */
1233         if (drv->bss == NULL)
1234                 wpa_printf(MSG_ERROR, "%s: drv->bss == NULL", __func__);
1235         else if (drv->bss->next)
1236                 wpa_printf(MSG_ERROR, "%s: drv->bss->next != NULL", __func__);
1237
1238         test_driver_free_priv(drv);
1239 }
1240
1241
1242 const struct wpa_driver_ops wpa_driver_test_ops = {
1243         .name = "test",
1244         .init = test_driver_init,
1245         .deinit = test_driver_deinit,
1246         .send_eapol = test_driver_send_eapol,
1247         .send_mgmt_frame = test_driver_send_mgmt_frame,
1248         .set_generic_elem = test_driver_set_generic_elem,
1249         .sta_deauth = test_driver_sta_deauth,
1250         .sta_disassoc = test_driver_sta_disassoc,
1251         .get_hw_feature_data = test_driver_get_hw_feature_data,
1252         .bss_add = test_driver_bss_add,
1253         .bss_remove = test_driver_bss_remove,
1254         .if_add = test_driver_if_add,
1255         .if_update = test_driver_if_update,
1256         .if_remove = test_driver_if_remove,
1257         .valid_bss_mask = test_driver_valid_bss_mask,
1258         .set_ssid = test_driver_set_ssid,
1259         .set_privacy = test_driver_set_privacy,
1260         .set_encryption = test_driver_set_encryption,
1261         .set_sta_vlan = test_driver_set_sta_vlan,
1262         .sta_add = test_driver_sta_add,
1263         .send_ether = test_driver_send_ether,
1264         .set_wps_beacon_ie = test_driver_set_wps_beacon_ie,
1265         .set_wps_probe_resp_ie = test_driver_set_wps_probe_resp_ie,
1266 };