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