blob: 8fc0efd4eee584fd42aebdb7ae733bade28530ac [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Driver interaction with Linux Host AP driver
3 * Copyright (c) 2003-2005, 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/ioctl.h>
17
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080018#include "linux_wext.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019#include "common.h"
20#include "driver.h"
21#include "driver_wext.h"
22#include "eloop.h"
23#include "driver_hostap.h"
24
25
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include <net/if_arp.h>
27#include <netpacket/packet.h>
28
29#include "priv_netlink.h"
30#include "netlink.h"
31#include "linux_ioctl.h"
32#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080033#include "common/ieee802_11_common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034
35
36/* MTU to be set for the wlan#ap device; this is mainly needed for IEEE 802.1X
37 * frames that might be longer than normal default MTU and they are not
38 * fragmented */
39#define HOSTAPD_MTU 2290
40
41static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
42
43struct hostap_driver_data {
44 struct hostapd_data *hapd;
45
46 char iface[IFNAMSIZ + 1];
47 int sock; /* raw packet socket for driver access */
48 int ioctl_sock; /* socket for ioctl() use */
49 struct netlink_data *netlink;
50
51 int we_version;
52
53 u8 *generic_ie;
54 size_t generic_ie_len;
55 u8 *wps_ie;
56 size_t wps_ie_len;
57};
58
59
60static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
61 int len);
62static int hostap_set_iface_flags(void *priv, int dev_up);
63
64static void handle_data(struct hostap_driver_data *drv, u8 *buf, size_t len,
65 u16 stype)
66{
67 struct ieee80211_hdr *hdr;
68 u16 fc, ethertype;
69 u8 *pos, *sa;
70 size_t left;
71 union wpa_event_data event;
72
73 if (len < sizeof(struct ieee80211_hdr))
74 return;
75
76 hdr = (struct ieee80211_hdr *) buf;
77 fc = le_to_host16(hdr->frame_control);
78
79 if ((fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) != WLAN_FC_TODS) {
80 printf("Not ToDS data frame (fc=0x%04x)\n", fc);
81 return;
82 }
83
84 sa = hdr->addr2;
85 os_memset(&event, 0, sizeof(event));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080086 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
87 event.rx_from_unknown.addr = sa;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088 wpa_supplicant_event(drv->hapd, EVENT_RX_FROM_UNKNOWN, &event);
89
90 pos = (u8 *) (hdr + 1);
91 left = len - sizeof(*hdr);
92
93 if (left < sizeof(rfc1042_header)) {
94 printf("Too short data frame\n");
95 return;
96 }
97
98 if (memcmp(pos, rfc1042_header, sizeof(rfc1042_header)) != 0) {
99 printf("Data frame with no RFC1042 header\n");
100 return;
101 }
102 pos += sizeof(rfc1042_header);
103 left -= sizeof(rfc1042_header);
104
105 if (left < 2) {
106 printf("No ethertype in data frame\n");
107 return;
108 }
109
110 ethertype = WPA_GET_BE16(pos);
111 pos += 2;
112 left -= 2;
113 switch (ethertype) {
114 case ETH_P_PAE:
115 drv_event_eapol_rx(drv->hapd, sa, pos, left);
116 break;
117
118 default:
119 printf("Unknown ethertype 0x%04x in data frame\n", ethertype);
120 break;
121 }
122}
123
124
125static void handle_tx_callback(struct hostap_driver_data *drv, u8 *buf,
126 size_t len, int ok)
127{
128 struct ieee80211_hdr *hdr;
129 u16 fc;
130 union wpa_event_data event;
131
132 hdr = (struct ieee80211_hdr *) buf;
133 fc = le_to_host16(hdr->frame_control);
134
135 os_memset(&event, 0, sizeof(event));
136 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
137 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
138 event.tx_status.dst = hdr->addr1;
139 event.tx_status.data = buf;
140 event.tx_status.data_len = len;
141 event.tx_status.ack = ok;
142 wpa_supplicant_event(drv->hapd, EVENT_TX_STATUS, &event);
143}
144
145
146static void handle_frame(struct hostap_driver_data *drv, u8 *buf, size_t len)
147{
148 struct ieee80211_hdr *hdr;
149 u16 fc, extra_len, type, stype;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150 size_t data_len = len;
151 int ver;
152 union wpa_event_data event;
153
154 /* PSPOLL is only 16 bytes, but driver does not (at least yet) pass
155 * these to user space */
156 if (len < 24) {
157 wpa_printf(MSG_MSGDUMP, "handle_frame: too short (%lu)",
158 (unsigned long) len);
159 return;
160 }
161
162 hdr = (struct ieee80211_hdr *) buf;
163 fc = le_to_host16(hdr->frame_control);
164 type = WLAN_FC_GET_TYPE(fc);
165 stype = WLAN_FC_GET_STYPE(fc);
166
167 if (type != WLAN_FC_TYPE_MGMT || stype != WLAN_FC_STYPE_BEACON) {
168 wpa_hexdump(MSG_MSGDUMP, "Received management frame",
169 buf, len);
170 }
171
172 ver = fc & WLAN_FC_PVER;
173
174 /* protocol version 3 is reserved for indicating extra data after the
175 * payload, version 2 for indicating ACKed frame (TX callbacks), and
176 * version 1 for indicating failed frame (no ACK, TX callbacks) */
177 if (ver == 3) {
178 u8 *pos = buf + len - 2;
179 extra_len = WPA_GET_LE16(pos);
180 printf("extra data in frame (elen=%d)\n", extra_len);
181 if ((size_t) extra_len + 2 > len) {
182 printf(" extra data overflow\n");
183 return;
184 }
185 len -= extra_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700186 } else if (ver == 1 || ver == 2) {
187 handle_tx_callback(drv, buf, data_len, ver == 2 ? 1 : 0);
188 return;
189 } else if (ver != 0) {
190 printf("unknown protocol version %d\n", ver);
191 return;
192 }
193
194 switch (type) {
195 case WLAN_FC_TYPE_MGMT:
196 os_memset(&event, 0, sizeof(event));
197 event.rx_mgmt.frame = buf;
198 event.rx_mgmt.frame_len = data_len;
199 wpa_supplicant_event(drv->hapd, EVENT_RX_MGMT, &event);
200 break;
201 case WLAN_FC_TYPE_CTRL:
202 wpa_printf(MSG_DEBUG, "CTRL");
203 break;
204 case WLAN_FC_TYPE_DATA:
205 wpa_printf(MSG_DEBUG, "DATA");
206 handle_data(drv, buf, data_len, stype);
207 break;
208 default:
209 wpa_printf(MSG_DEBUG, "unknown frame type %d", type);
210 break;
211 }
212}
213
214
215static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
216{
217 struct hostap_driver_data *drv = eloop_ctx;
218 int len;
219 unsigned char buf[3000];
220
221 len = recv(sock, buf, sizeof(buf), 0);
222 if (len < 0) {
223 perror("recv");
224 return;
225 }
226
227 handle_frame(drv, buf, len);
228}
229
230
231static int hostap_init_sockets(struct hostap_driver_data *drv, u8 *own_addr)
232{
233 struct ifreq ifr;
234 struct sockaddr_ll addr;
235
236 drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
237 if (drv->sock < 0) {
238 perror("socket[PF_PACKET,SOCK_RAW]");
239 return -1;
240 }
241
242 if (eloop_register_read_sock(drv->sock, handle_read, drv, NULL)) {
243 printf("Could not register read socket\n");
244 return -1;
245 }
246
247 memset(&ifr, 0, sizeof(ifr));
248 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
249 if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
250 perror("ioctl(SIOCGIFINDEX)");
251 return -1;
252 }
253
254 if (hostap_set_iface_flags(drv, 1)) {
255 return -1;
256 }
257
258 memset(&addr, 0, sizeof(addr));
259 addr.sll_family = AF_PACKET;
260 addr.sll_ifindex = ifr.ifr_ifindex;
261 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
262 addr.sll_ifindex);
263
264 if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
265 perror("bind");
266 return -1;
267 }
268
269 return linux_get_ifhwaddr(drv->sock, drv->iface, own_addr);
270}
271
272
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800273static int hostap_send_mlme(void *priv, const u8 *msg, size_t len, int noack)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274{
275 struct hostap_driver_data *drv = priv;
276 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) msg;
277 int res;
278
279 /* Request TX callback */
280 hdr->frame_control |= host_to_le16(BIT(1));
281 res = send(drv->sock, msg, len, 0);
282 hdr->frame_control &= ~host_to_le16(BIT(1));
283
284 return res;
285}
286
287
288static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
289 size_t data_len, int encrypt, const u8 *own_addr,
290 u32 flags)
291{
292 struct hostap_driver_data *drv = priv;
293 struct ieee80211_hdr *hdr;
294 size_t len;
295 u8 *pos;
296 int res;
297
298 len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
299 hdr = os_zalloc(len);
300 if (hdr == NULL) {
301 printf("malloc() failed for hostapd_send_data(len=%lu)\n",
302 (unsigned long) len);
303 return -1;
304 }
305
306 hdr->frame_control =
307 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
308 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
309 if (encrypt)
310 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
311 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
312 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
313 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
314
315 pos = (u8 *) (hdr + 1);
316 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
317 pos += sizeof(rfc1042_header);
318 *((u16 *) pos) = htons(ETH_P_PAE);
319 pos += 2;
320 memcpy(pos, data, data_len);
321
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800322 res = hostap_send_mlme(drv, (u8 *) hdr, len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 if (res < 0) {
324 wpa_printf(MSG_ERROR, "hostap_send_eapol - packet len: %lu - "
325 "failed: %d (%s)",
326 (unsigned long) len, errno, strerror(errno));
327 }
328 free(hdr);
329
330 return res;
331}
332
333
334static int hostap_sta_set_flags(void *priv, const u8 *addr,
335 int total_flags, int flags_or, int flags_and)
336{
337 struct hostap_driver_data *drv = priv;
338 struct prism2_hostapd_param param;
339
340 if (flags_or & WPA_STA_AUTHORIZED)
341 flags_or = BIT(5); /* WLAN_STA_AUTHORIZED */
342 if (!(flags_and & WPA_STA_AUTHORIZED))
343 flags_and = ~BIT(5);
344 else
345 flags_and = ~0;
346 memset(&param, 0, sizeof(param));
347 param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
348 memcpy(param.sta_addr, addr, ETH_ALEN);
349 param.u.set_flags_sta.flags_or = flags_or;
350 param.u.set_flags_sta.flags_and = flags_and;
351 return hostapd_ioctl(drv, &param, sizeof(param));
352}
353
354
355static int hostap_set_iface_flags(void *priv, int dev_up)
356{
357 struct hostap_driver_data *drv = priv;
358 struct ifreq ifr;
359 char ifname[IFNAMSIZ];
360
361 os_snprintf(ifname, IFNAMSIZ, "%sap", drv->iface);
362 if (linux_set_iface_flags(drv->ioctl_sock, ifname, dev_up) < 0)
363 return -1;
364
365 if (dev_up) {
366 memset(&ifr, 0, sizeof(ifr));
367 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
368 ifr.ifr_mtu = HOSTAPD_MTU;
369 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
370 perror("ioctl[SIOCSIFMTU]");
371 printf("Setting MTU failed - trying to survive with "
372 "current value\n");
373 }
374 }
375
376 return 0;
377}
378
379
380static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
381 int len)
382{
383 struct hostap_driver_data *drv = priv;
384 struct iwreq iwr;
385
386 memset(&iwr, 0, sizeof(iwr));
387 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
388 iwr.u.data.pointer = (caddr_t) param;
389 iwr.u.data.length = len;
390
391 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
392 perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
393 return -1;
394 }
395
396 return 0;
397}
398
399
400static int wpa_driver_hostap_set_key(const char *ifname, void *priv,
401 enum wpa_alg alg, const u8 *addr,
402 int key_idx, int set_tx,
403 const u8 *seq, size_t seq_len,
404 const u8 *key, size_t key_len)
405{
406 struct hostap_driver_data *drv = priv;
407 struct prism2_hostapd_param *param;
408 u8 *buf;
409 size_t blen;
410 int ret = 0;
411
412 blen = sizeof(*param) + key_len;
413 buf = os_zalloc(blen);
414 if (buf == NULL)
415 return -1;
416
417 param = (struct prism2_hostapd_param *) buf;
418 param->cmd = PRISM2_SET_ENCRYPTION;
419 if (addr == NULL)
420 memset(param->sta_addr, 0xff, ETH_ALEN);
421 else
422 memcpy(param->sta_addr, addr, ETH_ALEN);
423 switch (alg) {
424 case WPA_ALG_NONE:
425 os_strlcpy((char *) param->u.crypt.alg, "NONE",
426 HOSTAP_CRYPT_ALG_NAME_LEN);
427 break;
428 case WPA_ALG_WEP:
429 os_strlcpy((char *) param->u.crypt.alg, "WEP",
430 HOSTAP_CRYPT_ALG_NAME_LEN);
431 break;
432 case WPA_ALG_TKIP:
433 os_strlcpy((char *) param->u.crypt.alg, "TKIP",
434 HOSTAP_CRYPT_ALG_NAME_LEN);
435 break;
436 case WPA_ALG_CCMP:
437 os_strlcpy((char *) param->u.crypt.alg, "CCMP",
438 HOSTAP_CRYPT_ALG_NAME_LEN);
439 break;
440 default:
441 os_free(buf);
442 return -1;
443 }
444 param->u.crypt.flags = set_tx ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
445 param->u.crypt.idx = key_idx;
446 param->u.crypt.key_len = key_len;
447 memcpy((u8 *) (param + 1), key, key_len);
448
449 if (hostapd_ioctl(drv, param, blen)) {
450 printf("Failed to set encryption.\n");
451 ret = -1;
452 }
453 free(buf);
454
455 return ret;
456}
457
458
459static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
460 int idx, u8 *seq)
461{
462 struct hostap_driver_data *drv = priv;
463 struct prism2_hostapd_param *param;
464 u8 *buf;
465 size_t blen;
466 int ret = 0;
467
468 blen = sizeof(*param) + 32;
469 buf = os_zalloc(blen);
470 if (buf == NULL)
471 return -1;
472
473 param = (struct prism2_hostapd_param *) buf;
474 param->cmd = PRISM2_GET_ENCRYPTION;
475 if (addr == NULL)
476 memset(param->sta_addr, 0xff, ETH_ALEN);
477 else
478 memcpy(param->sta_addr, addr, ETH_ALEN);
479 param->u.crypt.idx = idx;
480
481 if (hostapd_ioctl(drv, param, blen)) {
482 printf("Failed to get encryption.\n");
483 ret = -1;
484 } else {
485 memcpy(seq, param->u.crypt.seq, 8);
486 }
487 free(buf);
488
489 return ret;
490}
491
492
493static int hostap_ioctl_prism2param(void *priv, int param, int value)
494{
495 struct hostap_driver_data *drv = priv;
496 struct iwreq iwr;
497 int *i;
498
499 memset(&iwr, 0, sizeof(iwr));
500 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
501 i = (int *) iwr.u.name;
502 *i++ = param;
503 *i++ = value;
504
505 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
506 perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]");
507 return -1;
508 }
509
510 return 0;
511}
512
513
514static int hostap_set_ieee8021x(void *priv, struct wpa_bss_params *params)
515{
516 struct hostap_driver_data *drv = priv;
517 int enabled = params->enabled;
518
519 /* enable kernel driver support for IEEE 802.1X */
520 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
521 printf("Could not setup IEEE 802.1X support in kernel driver."
522 "\n");
523 return -1;
524 }
525
526 if (!enabled)
527 return 0;
528
529 /* use host driver implementation of encryption to allow
530 * individual keys and passing plaintext EAPOL frames */
531 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
532 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
533 printf("Could not setup host-based encryption in kernel "
534 "driver.\n");
535 return -1;
536 }
537
538 return 0;
539}
540
541
542static int hostap_set_privacy(void *priv, int enabled)
543{
544 struct hostap_drvier_data *drv = priv;
545
546 return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
547 enabled);
548}
549
550
551static int hostap_set_ssid(void *priv, const u8 *buf, int len)
552{
553 struct hostap_driver_data *drv = priv;
554 struct iwreq iwr;
555
556 memset(&iwr, 0, sizeof(iwr));
557 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
558 iwr.u.essid.flags = 1; /* SSID active */
559 iwr.u.essid.pointer = (caddr_t) buf;
560 iwr.u.essid.length = len + 1;
561
562 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
563 perror("ioctl[SIOCSIWESSID]");
564 printf("len=%d\n", len);
565 return -1;
566 }
567
568 return 0;
569}
570
571
572static int hostap_flush(void *priv)
573{
574 struct hostap_driver_data *drv = priv;
575 struct prism2_hostapd_param param;
576
577 memset(&param, 0, sizeof(param));
578 param.cmd = PRISM2_HOSTAPD_FLUSH;
579 return hostapd_ioctl(drv, &param, sizeof(param));
580}
581
582
583static int hostap_read_sta_data(void *priv,
584 struct hostap_sta_driver_data *data,
585 const u8 *addr)
586{
587 struct hostap_driver_data *drv = priv;
588 char buf[1024], line[128], *pos;
589 FILE *f;
590 unsigned long val;
591
592 memset(data, 0, sizeof(*data));
593 snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
594 drv->iface, MAC2STR(addr));
595
596 f = fopen(buf, "r");
597 if (!f)
598 return -1;
599 /* Need to read proc file with in one piece, so use large enough
600 * buffer. */
601 setbuffer(f, buf, sizeof(buf));
602
603 while (fgets(line, sizeof(line), f)) {
604 pos = strchr(line, '=');
605 if (!pos)
606 continue;
607 *pos++ = '\0';
608 val = strtoul(pos, NULL, 10);
609 if (strcmp(line, "rx_packets") == 0)
610 data->rx_packets = val;
611 else if (strcmp(line, "tx_packets") == 0)
612 data->tx_packets = val;
613 else if (strcmp(line, "rx_bytes") == 0)
614 data->rx_bytes = val;
615 else if (strcmp(line, "tx_bytes") == 0)
616 data->tx_bytes = val;
617 }
618
619 fclose(f);
620
621 return 0;
622}
623
624
625static int hostap_sta_add(void *priv, struct hostapd_sta_add_params *params)
626{
627 struct hostap_driver_data *drv = priv;
628 struct prism2_hostapd_param param;
629 int tx_supp_rates = 0;
630 size_t i;
631
632#define WLAN_RATE_1M BIT(0)
633#define WLAN_RATE_2M BIT(1)
634#define WLAN_RATE_5M5 BIT(2)
635#define WLAN_RATE_11M BIT(3)
636
637 for (i = 0; i < params->supp_rates_len; i++) {
638 if ((params->supp_rates[i] & 0x7f) == 2)
639 tx_supp_rates |= WLAN_RATE_1M;
640 if ((params->supp_rates[i] & 0x7f) == 4)
641 tx_supp_rates |= WLAN_RATE_2M;
642 if ((params->supp_rates[i] & 0x7f) == 11)
643 tx_supp_rates |= WLAN_RATE_5M5;
644 if ((params->supp_rates[i] & 0x7f) == 22)
645 tx_supp_rates |= WLAN_RATE_11M;
646 }
647
648 memset(&param, 0, sizeof(param));
649 param.cmd = PRISM2_HOSTAPD_ADD_STA;
650 memcpy(param.sta_addr, params->addr, ETH_ALEN);
651 param.u.add_sta.aid = params->aid;
652 param.u.add_sta.capability = params->capability;
653 param.u.add_sta.tx_supp_rates = tx_supp_rates;
654 return hostapd_ioctl(drv, &param, sizeof(param));
655}
656
657
658static int hostap_sta_remove(void *priv, const u8 *addr)
659{
660 struct hostap_driver_data *drv = priv;
661 struct prism2_hostapd_param param;
662
663 hostap_sta_set_flags(drv, addr, 0, 0, ~WPA_STA_AUTHORIZED);
664
665 memset(&param, 0, sizeof(param));
666 param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
667 memcpy(param.sta_addr, addr, ETH_ALEN);
668 if (hostapd_ioctl(drv, &param, sizeof(param))) {
669 printf("Could not remove station from kernel driver.\n");
670 return -1;
671 }
672 return 0;
673}
674
675
676static int hostap_get_inact_sec(void *priv, const u8 *addr)
677{
678 struct hostap_driver_data *drv = priv;
679 struct prism2_hostapd_param param;
680
681 memset(&param, 0, sizeof(param));
682 param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
683 memcpy(param.sta_addr, addr, ETH_ALEN);
684 if (hostapd_ioctl(drv, &param, sizeof(param))) {
685 return -1;
686 }
687
688 return param.u.get_info_sta.inactive_sec;
689}
690
691
692static int hostap_sta_clear_stats(void *priv, const u8 *addr)
693{
694 struct hostap_driver_data *drv = priv;
695 struct prism2_hostapd_param param;
696
697 memset(&param, 0, sizeof(param));
698 param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
699 memcpy(param.sta_addr, addr, ETH_ALEN);
700 if (hostapd_ioctl(drv, &param, sizeof(param))) {
701 return -1;
702 }
703
704 return 0;
705}
706
707
708static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
709{
710 struct prism2_hostapd_param *param;
711 int res;
712 size_t blen, elem_len;
713
714 elem_len = drv->generic_ie_len + drv->wps_ie_len;
715 blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
716 if (blen < sizeof(*param))
717 blen = sizeof(*param);
718
719 param = os_zalloc(blen);
720 if (param == NULL)
721 return -1;
722
723 param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
724 param->u.generic_elem.len = elem_len;
725 if (drv->generic_ie) {
726 os_memcpy(param->u.generic_elem.data, drv->generic_ie,
727 drv->generic_ie_len);
728 }
729 if (drv->wps_ie) {
730 os_memcpy(&param->u.generic_elem.data[drv->generic_ie_len],
731 drv->wps_ie, drv->wps_ie_len);
732 }
733 wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
734 param->u.generic_elem.data, elem_len);
735 res = hostapd_ioctl(drv, param, blen);
736
737 os_free(param);
738
739 return res;
740}
741
742
743static int hostap_set_generic_elem(void *priv,
744 const u8 *elem, size_t elem_len)
745{
746 struct hostap_driver_data *drv = priv;
747
748 os_free(drv->generic_ie);
749 drv->generic_ie = NULL;
750 drv->generic_ie_len = 0;
751 if (elem) {
752 drv->generic_ie = os_malloc(elem_len);
753 if (drv->generic_ie == NULL)
754 return -1;
755 os_memcpy(drv->generic_ie, elem, elem_len);
756 drv->generic_ie_len = elem_len;
757 }
758
759 return hostapd_ioctl_set_generic_elem(drv);
760}
761
762
763static int hostap_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
764 const struct wpabuf *proberesp,
765 const struct wpabuf *assocresp)
766{
767 struct hostap_driver_data *drv = priv;
768
769 /*
770 * Host AP driver supports only one set of extra IEs, so we need to
771 * use the Probe Response IEs also for Beacon frames since they include
772 * more information.
773 */
774
775 os_free(drv->wps_ie);
776 drv->wps_ie = NULL;
777 drv->wps_ie_len = 0;
778 if (proberesp) {
779 drv->wps_ie = os_malloc(wpabuf_len(proberesp));
780 if (drv->wps_ie == NULL)
781 return -1;
782 os_memcpy(drv->wps_ie, wpabuf_head(proberesp),
783 wpabuf_len(proberesp));
784 drv->wps_ie_len = wpabuf_len(proberesp);
785 }
786
787 return hostapd_ioctl_set_generic_elem(drv);
788}
789
790
791static void
792hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
793 char *custom)
794{
795 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
796
797 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
798 char *pos;
799 u8 addr[ETH_ALEN];
800 pos = strstr(custom, "addr=");
801 if (pos == NULL) {
802 wpa_printf(MSG_DEBUG,
803 "MLME-MICHAELMICFAILURE.indication "
804 "without sender address ignored");
805 return;
806 }
807 pos += 5;
808 if (hwaddr_aton(pos, addr) == 0) {
809 union wpa_event_data data;
810 os_memset(&data, 0, sizeof(data));
811 data.michael_mic_failure.unicast = 1;
812 data.michael_mic_failure.src = addr;
813 wpa_supplicant_event(drv->hapd,
814 EVENT_MICHAEL_MIC_FAILURE, &data);
815 } else {
816 wpa_printf(MSG_DEBUG,
817 "MLME-MICHAELMICFAILURE.indication "
818 "with invalid MAC address");
819 }
820 }
821}
822
823
824static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
825 char *data, int len)
826{
827 struct iw_event iwe_buf, *iwe = &iwe_buf;
828 char *pos, *end, *custom, *buf;
829
830 pos = data;
831 end = data + len;
832
833 while (pos + IW_EV_LCP_LEN <= end) {
834 /* Event data may be unaligned, so make a local, aligned copy
835 * before processing. */
836 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
837 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
838 iwe->cmd, iwe->len);
839 if (iwe->len <= IW_EV_LCP_LEN)
840 return;
841
842 custom = pos + IW_EV_POINT_LEN;
843 if (drv->we_version > 18 &&
844 (iwe->cmd == IWEVMICHAELMICFAILURE ||
845 iwe->cmd == IWEVCUSTOM)) {
846 /* WE-19 removed the pointer from struct iw_point */
847 char *dpos = (char *) &iwe_buf.u.data.length;
848 int dlen = dpos - (char *) &iwe_buf;
849 memcpy(dpos, pos + IW_EV_LCP_LEN,
850 sizeof(struct iw_event) - dlen);
851 } else {
852 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
853 custom += IW_EV_POINT_OFF;
854 }
855
856 switch (iwe->cmd) {
857 case IWEVCUSTOM:
858 if (custom + iwe->u.data.length > end)
859 return;
860 buf = malloc(iwe->u.data.length + 1);
861 if (buf == NULL)
862 return;
863 memcpy(buf, custom, iwe->u.data.length);
864 buf[iwe->u.data.length] = '\0';
865 hostapd_wireless_event_wireless_custom(drv, buf);
866 free(buf);
867 break;
868 }
869
870 pos += iwe->len;
871 }
872}
873
874
875static void hostapd_wireless_event_rtm_newlink(void *ctx,
876 struct ifinfomsg *ifi,
877 u8 *buf, size_t len)
878{
879 struct hostap_driver_data *drv = ctx;
880 int attrlen, rta_len;
881 struct rtattr *attr;
882
883 /* TODO: use ifi->ifi_index to filter out wireless events from other
884 * interfaces */
885
886 attrlen = len;
887 attr = (struct rtattr *) buf;
888
889 rta_len = RTA_ALIGN(sizeof(struct rtattr));
890 while (RTA_OK(attr, attrlen)) {
891 if (attr->rta_type == IFLA_WIRELESS) {
892 hostapd_wireless_event_wireless(
893 drv, ((char *) attr) + rta_len,
894 attr->rta_len - rta_len);
895 }
896 attr = RTA_NEXT(attr, attrlen);
897 }
898}
899
900
901static int hostap_get_we_version(struct hostap_driver_data *drv)
902{
903 struct iw_range *range;
904 struct iwreq iwr;
905 int minlen;
906 size_t buflen;
907
908 drv->we_version = 0;
909
910 /*
911 * Use larger buffer than struct iw_range in order to allow the
912 * structure to grow in the future.
913 */
914 buflen = sizeof(struct iw_range) + 500;
915 range = os_zalloc(buflen);
916 if (range == NULL)
917 return -1;
918
919 memset(&iwr, 0, sizeof(iwr));
920 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
921 iwr.u.data.pointer = (caddr_t) range;
922 iwr.u.data.length = buflen;
923
924 minlen = ((char *) &range->enc_capa) - (char *) range +
925 sizeof(range->enc_capa);
926
927 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
928 perror("ioctl[SIOCGIWRANGE]");
929 free(range);
930 return -1;
931 } else if (iwr.u.data.length >= minlen &&
932 range->we_version_compiled >= 18) {
933 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
934 "WE(source)=%d enc_capa=0x%x",
935 range->we_version_compiled,
936 range->we_version_source,
937 range->enc_capa);
938 drv->we_version = range->we_version_compiled;
939 }
940
941 free(range);
942 return 0;
943}
944
945
946static int hostap_wireless_event_init(struct hostap_driver_data *drv)
947{
948 struct netlink_config *cfg;
949
950 hostap_get_we_version(drv);
951
952 cfg = os_zalloc(sizeof(*cfg));
953 if (cfg == NULL)
954 return -1;
955 cfg->ctx = drv;
956 cfg->newlink_cb = hostapd_wireless_event_rtm_newlink;
957 drv->netlink = netlink_init(cfg);
958 if (drv->netlink == NULL) {
959 os_free(cfg);
960 return -1;
961 }
962
963 return 0;
964}
965
966
967static void * hostap_init(struct hostapd_data *hapd,
968 struct wpa_init_params *params)
969{
970 struct hostap_driver_data *drv;
971
972 drv = os_zalloc(sizeof(struct hostap_driver_data));
973 if (drv == NULL) {
974 printf("Could not allocate memory for hostapd driver data\n");
975 return NULL;
976 }
977
978 drv->hapd = hapd;
979 drv->ioctl_sock = drv->sock = -1;
980 memcpy(drv->iface, params->ifname, sizeof(drv->iface));
981
982 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
983 if (drv->ioctl_sock < 0) {
984 perror("socket[PF_INET,SOCK_DGRAM]");
985 free(drv);
986 return NULL;
987 }
988
989 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
990 printf("Could not enable hostapd mode for interface %s\n",
991 drv->iface);
992 close(drv->ioctl_sock);
993 free(drv);
994 return NULL;
995 }
996
997 if (hostap_init_sockets(drv, params->own_addr) ||
998 hostap_wireless_event_init(drv)) {
999 close(drv->ioctl_sock);
1000 free(drv);
1001 return NULL;
1002 }
1003
1004 return drv;
1005}
1006
1007
1008static void hostap_driver_deinit(void *priv)
1009{
1010 struct hostap_driver_data *drv = priv;
1011
1012 netlink_deinit(drv->netlink);
1013 (void) hostap_set_iface_flags(drv, 0);
1014 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
1015 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
1016
1017 if (drv->ioctl_sock >= 0)
1018 close(drv->ioctl_sock);
1019
1020 if (drv->sock >= 0)
1021 close(drv->sock);
1022
1023 os_free(drv->generic_ie);
1024 os_free(drv->wps_ie);
1025
1026 free(drv);
1027}
1028
1029
1030static int hostap_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
1031 int reason)
1032{
1033 struct hostap_driver_data *drv = priv;
1034 struct ieee80211_mgmt mgmt;
1035
1036 if (is_broadcast_ether_addr(addr)) {
1037 /*
1038 * New Prism2.5/3 STA firmware versions seem to have issues
1039 * with this broadcast deauth frame. This gets the firmware in
1040 * odd state where nothing works correctly, so let's skip
1041 * sending this for the hostap driver.
1042 */
1043 return 0;
1044 }
1045
1046 memset(&mgmt, 0, sizeof(mgmt));
1047 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1048 WLAN_FC_STYPE_DEAUTH);
1049 memcpy(mgmt.da, addr, ETH_ALEN);
1050 memcpy(mgmt.sa, own_addr, ETH_ALEN);
1051 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
1052 mgmt.u.deauth.reason_code = host_to_le16(reason);
1053 return hostap_send_mlme(drv, (u8 *) &mgmt, IEEE80211_HDRLEN +
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001054 sizeof(mgmt.u.deauth), 0);
1055}
1056
1057
1058static int hostap_set_freq(void *priv, struct hostapd_freq_params *freq)
1059{
1060 struct hostap_driver_data *drv = priv;
1061 struct iwreq iwr;
1062
1063 os_memset(&iwr, 0, sizeof(iwr));
1064 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1065 iwr.u.freq.m = freq->channel;
1066 iwr.u.freq.e = 0;
1067
1068 if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
1069 perror("ioctl[SIOCSIWFREQ]");
1070 return -1;
1071 }
1072
1073 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074}
1075
1076
1077static int hostap_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
1078 int reason)
1079{
1080 struct hostap_driver_data *drv = priv;
1081 struct ieee80211_mgmt mgmt;
1082
1083 memset(&mgmt, 0, sizeof(mgmt));
1084 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1085 WLAN_FC_STYPE_DISASSOC);
1086 memcpy(mgmt.da, addr, ETH_ALEN);
1087 memcpy(mgmt.sa, own_addr, ETH_ALEN);
1088 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
1089 mgmt.u.disassoc.reason_code = host_to_le16(reason);
1090 return hostap_send_mlme(drv, (u8 *) &mgmt, IEEE80211_HDRLEN +
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001091 sizeof(mgmt.u.disassoc), 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092}
1093
1094
1095static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
1096 u16 *num_modes,
1097 u16 *flags)
1098{
1099 struct hostapd_hw_modes *mode;
1100 int i, clen, rlen;
1101 const short chan2freq[14] = {
1102 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1103 2447, 2452, 2457, 2462, 2467, 2472, 2484
1104 };
1105
1106 mode = os_zalloc(sizeof(struct hostapd_hw_modes));
1107 if (mode == NULL)
1108 return NULL;
1109
1110 *num_modes = 1;
1111 *flags = 0;
1112
1113 mode->mode = HOSTAPD_MODE_IEEE80211B;
1114 mode->num_channels = 14;
1115 mode->num_rates = 4;
1116
1117 clen = mode->num_channels * sizeof(struct hostapd_channel_data);
1118 rlen = mode->num_rates * sizeof(int);
1119
1120 mode->channels = os_zalloc(clen);
1121 mode->rates = os_zalloc(rlen);
1122 if (mode->channels == NULL || mode->rates == NULL) {
1123 os_free(mode->channels);
1124 os_free(mode->rates);
1125 os_free(mode);
1126 return NULL;
1127 }
1128
1129 for (i = 0; i < 14; i++) {
1130 mode->channels[i].chan = i + 1;
1131 mode->channels[i].freq = chan2freq[i];
1132 /* TODO: Get allowed channel list from the driver */
1133 if (i >= 11)
1134 mode->channels[i].flag = HOSTAPD_CHAN_DISABLED;
1135 }
1136
1137 mode->rates[0] = 10;
1138 mode->rates[1] = 20;
1139 mode->rates[2] = 55;
1140 mode->rates[3] = 110;
1141
1142 return mode;
1143}
1144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001146static void wpa_driver_hostap_poll_client(void *priv, const u8 *own_addr,
1147 const u8 *addr, int qos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001149 struct ieee80211_hdr hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001151 os_memset(&hdr, 0, sizeof(hdr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001153 /*
1154 * WLAN_FC_STYPE_NULLFUNC would be more appropriate,
1155 * but it is apparently not retried so TX Exc events
1156 * are not received for it.
1157 * This is the reason the driver overrides the default
1158 * handling.
1159 */
1160 hdr.frame_control = IEEE80211_FC(WLAN_FC_TYPE_DATA,
1161 WLAN_FC_STYPE_DATA);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001163 hdr.frame_control |=
1164 host_to_le16(WLAN_FC_FROMDS);
1165 os_memcpy(hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
1166 os_memcpy(hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
1167 os_memcpy(hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
1168
1169 hostap_send_mlme(priv, (u8 *)&hdr, sizeof(hdr), 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001170}
1171
1172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173const struct wpa_driver_ops wpa_driver_hostap_ops = {
1174 .name = "hostap",
1175 .desc = "Host AP driver (Intersil Prism2/2.5/3)",
1176 .set_key = wpa_driver_hostap_set_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 .hapd_init = hostap_init,
1178 .hapd_deinit = hostap_driver_deinit,
1179 .set_ieee8021x = hostap_set_ieee8021x,
1180 .set_privacy = hostap_set_privacy,
1181 .get_seqnum = hostap_get_seqnum,
1182 .flush = hostap_flush,
1183 .set_generic_elem = hostap_set_generic_elem,
1184 .read_sta_data = hostap_read_sta_data,
1185 .hapd_send_eapol = hostap_send_eapol,
1186 .sta_set_flags = hostap_sta_set_flags,
1187 .sta_deauth = hostap_sta_deauth,
1188 .sta_disassoc = hostap_sta_disassoc,
1189 .sta_remove = hostap_sta_remove,
1190 .hapd_set_ssid = hostap_set_ssid,
1191 .send_mlme = hostap_send_mlme,
1192 .sta_add = hostap_sta_add,
1193 .get_inact_sec = hostap_get_inact_sec,
1194 .sta_clear_stats = hostap_sta_clear_stats,
1195 .get_hw_feature_data = hostap_get_hw_feature_data,
1196 .set_ap_wps_ie = hostap_set_ap_wps_ie,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001197 .set_freq = hostap_set_freq,
1198 .poll_client = wpa_driver_hostap_poll_client,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199};