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