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