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