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