blob: 14c52d2ca837eff0f82f38e48d621b2a10462560 [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 Shmidta3dc3092015-06-23 11:21:28 -0700269static int hostap_send_mlme(void *priv, const u8 *msg, size_t len, int noack,
270 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271{
272 struct hostap_driver_data *drv = priv;
273 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) msg;
274 int res;
275
276 /* Request TX callback */
277 hdr->frame_control |= host_to_le16(BIT(1));
278 res = send(drv->sock, msg, len, 0);
279 hdr->frame_control &= ~host_to_le16(BIT(1));
280
281 return res;
282}
283
284
285static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
286 size_t data_len, int encrypt, const u8 *own_addr,
287 u32 flags)
288{
289 struct hostap_driver_data *drv = priv;
290 struct ieee80211_hdr *hdr;
291 size_t len;
292 u8 *pos;
293 int res;
294
295 len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
296 hdr = os_zalloc(len);
297 if (hdr == NULL) {
298 printf("malloc() failed for hostapd_send_data(len=%lu)\n",
299 (unsigned long) len);
300 return -1;
301 }
302
303 hdr->frame_control =
304 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
305 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
306 if (encrypt)
307 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
308 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
309 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
310 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
311
312 pos = (u8 *) (hdr + 1);
313 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
314 pos += sizeof(rfc1042_header);
315 *((u16 *) pos) = htons(ETH_P_PAE);
316 pos += 2;
317 memcpy(pos, data, data_len);
318
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800319 res = hostap_send_mlme(drv, (u8 *) hdr, len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320 if (res < 0) {
321 wpa_printf(MSG_ERROR, "hostap_send_eapol - packet len: %lu - "
322 "failed: %d (%s)",
323 (unsigned long) len, errno, strerror(errno));
324 }
325 free(hdr);
326
327 return res;
328}
329
330
331static int hostap_sta_set_flags(void *priv, const u8 *addr,
332 int total_flags, int flags_or, int flags_and)
333{
334 struct hostap_driver_data *drv = priv;
335 struct prism2_hostapd_param param;
336
337 if (flags_or & WPA_STA_AUTHORIZED)
338 flags_or = BIT(5); /* WLAN_STA_AUTHORIZED */
339 if (!(flags_and & WPA_STA_AUTHORIZED))
340 flags_and = ~BIT(5);
341 else
342 flags_and = ~0;
343 memset(&param, 0, sizeof(param));
344 param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
345 memcpy(param.sta_addr, addr, ETH_ALEN);
346 param.u.set_flags_sta.flags_or = flags_or;
347 param.u.set_flags_sta.flags_and = flags_and;
348 return hostapd_ioctl(drv, &param, sizeof(param));
349}
350
351
352static int hostap_set_iface_flags(void *priv, int dev_up)
353{
354 struct hostap_driver_data *drv = priv;
355 struct ifreq ifr;
356 char ifname[IFNAMSIZ];
357
358 os_snprintf(ifname, IFNAMSIZ, "%sap", drv->iface);
359 if (linux_set_iface_flags(drv->ioctl_sock, ifname, dev_up) < 0)
360 return -1;
361
362 if (dev_up) {
363 memset(&ifr, 0, sizeof(ifr));
364 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
365 ifr.ifr_mtu = HOSTAPD_MTU;
366 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800367 wpa_printf(MSG_INFO,
368 "Setting MTU failed - trying to survive with current value: ioctl[SIOCSIFMTU]: %s",
369 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370 }
371 }
372
373 return 0;
374}
375
376
377static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
378 int len)
379{
380 struct hostap_driver_data *drv = priv;
381 struct iwreq iwr;
382
383 memset(&iwr, 0, sizeof(iwr));
384 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
385 iwr.u.data.pointer = (caddr_t) param;
386 iwr.u.data.length = len;
387
388 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800389 wpa_printf(MSG_ERROR, "ioctl[PRISM2_IOCTL_HOSTAPD]: %s",
390 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391 return -1;
392 }
393
394 return 0;
395}
396
397
398static int wpa_driver_hostap_set_key(const char *ifname, void *priv,
399 enum wpa_alg alg, const u8 *addr,
400 int key_idx, int set_tx,
401 const u8 *seq, size_t seq_len,
402 const u8 *key, size_t key_len)
403{
404 struct hostap_driver_data *drv = priv;
405 struct prism2_hostapd_param *param;
406 u8 *buf;
407 size_t blen;
408 int ret = 0;
409
410 blen = sizeof(*param) + key_len;
411 buf = os_zalloc(blen);
412 if (buf == NULL)
413 return -1;
414
415 param = (struct prism2_hostapd_param *) buf;
416 param->cmd = PRISM2_SET_ENCRYPTION;
417 if (addr == NULL)
418 memset(param->sta_addr, 0xff, ETH_ALEN);
419 else
420 memcpy(param->sta_addr, addr, ETH_ALEN);
421 switch (alg) {
422 case WPA_ALG_NONE:
423 os_strlcpy((char *) param->u.crypt.alg, "NONE",
424 HOSTAP_CRYPT_ALG_NAME_LEN);
425 break;
426 case WPA_ALG_WEP:
427 os_strlcpy((char *) param->u.crypt.alg, "WEP",
428 HOSTAP_CRYPT_ALG_NAME_LEN);
429 break;
430 case WPA_ALG_TKIP:
431 os_strlcpy((char *) param->u.crypt.alg, "TKIP",
432 HOSTAP_CRYPT_ALG_NAME_LEN);
433 break;
434 case WPA_ALG_CCMP:
435 os_strlcpy((char *) param->u.crypt.alg, "CCMP",
436 HOSTAP_CRYPT_ALG_NAME_LEN);
437 break;
438 default:
439 os_free(buf);
440 return -1;
441 }
442 param->u.crypt.flags = set_tx ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
443 param->u.crypt.idx = key_idx;
444 param->u.crypt.key_len = key_len;
445 memcpy((u8 *) (param + 1), key, key_len);
446
447 if (hostapd_ioctl(drv, param, blen)) {
448 printf("Failed to set encryption.\n");
449 ret = -1;
450 }
451 free(buf);
452
453 return ret;
454}
455
456
457static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
458 int idx, u8 *seq)
459{
460 struct hostap_driver_data *drv = priv;
461 struct prism2_hostapd_param *param;
462 u8 *buf;
463 size_t blen;
464 int ret = 0;
465
466 blen = sizeof(*param) + 32;
467 buf = os_zalloc(blen);
468 if (buf == NULL)
469 return -1;
470
471 param = (struct prism2_hostapd_param *) buf;
472 param->cmd = PRISM2_GET_ENCRYPTION;
473 if (addr == NULL)
474 memset(param->sta_addr, 0xff, ETH_ALEN);
475 else
476 memcpy(param->sta_addr, addr, ETH_ALEN);
477 param->u.crypt.idx = idx;
478
479 if (hostapd_ioctl(drv, param, blen)) {
480 printf("Failed to get encryption.\n");
481 ret = -1;
482 } else {
483 memcpy(seq, param->u.crypt.seq, 8);
484 }
485 free(buf);
486
487 return ret;
488}
489
490
491static int hostap_ioctl_prism2param(void *priv, int param, int value)
492{
493 struct hostap_driver_data *drv = priv;
494 struct iwreq iwr;
495 int *i;
496
497 memset(&iwr, 0, sizeof(iwr));
498 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
499 i = (int *) iwr.u.name;
500 *i++ = param;
501 *i++ = value;
502
503 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800504 wpa_printf(MSG_ERROR, "ioctl[PRISM2_IOCTL_PRISM2_PARAM]: %s",
505 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506 return -1;
507 }
508
509 return 0;
510}
511
512
513static int hostap_set_ieee8021x(void *priv, struct wpa_bss_params *params)
514{
515 struct hostap_driver_data *drv = priv;
516 int enabled = params->enabled;
517
518 /* enable kernel driver support for IEEE 802.1X */
519 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
520 printf("Could not setup IEEE 802.1X support in kernel driver."
521 "\n");
522 return -1;
523 }
524
525 if (!enabled)
526 return 0;
527
528 /* use host driver implementation of encryption to allow
529 * individual keys and passing plaintext EAPOL frames */
530 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
531 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
532 printf("Could not setup host-based encryption in kernel "
533 "driver.\n");
534 return -1;
535 }
536
537 return 0;
538}
539
540
541static int hostap_set_privacy(void *priv, int enabled)
542{
543 struct hostap_drvier_data *drv = priv;
544
545 return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
546 enabled);
547}
548
549
550static int hostap_set_ssid(void *priv, const u8 *buf, int len)
551{
552 struct hostap_driver_data *drv = priv;
553 struct iwreq iwr;
554
555 memset(&iwr, 0, sizeof(iwr));
556 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
557 iwr.u.essid.flags = 1; /* SSID active */
558 iwr.u.essid.pointer = (caddr_t) buf;
559 iwr.u.essid.length = len + 1;
560
561 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800562 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWESSID,len=%d]: %s",
563 len, strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564 return -1;
565 }
566
567 return 0;
568}
569
570
571static int hostap_flush(void *priv)
572{
573 struct hostap_driver_data *drv = priv;
574 struct prism2_hostapd_param param;
575
576 memset(&param, 0, sizeof(param));
577 param.cmd = PRISM2_HOSTAPD_FLUSH;
578 return hostapd_ioctl(drv, &param, sizeof(param));
579}
580
581
582static int hostap_read_sta_data(void *priv,
583 struct hostap_sta_driver_data *data,
584 const u8 *addr)
585{
586 struct hostap_driver_data *drv = priv;
587 char buf[1024], line[128], *pos;
588 FILE *f;
589 unsigned long val;
590
591 memset(data, 0, sizeof(*data));
592 snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
593 drv->iface, MAC2STR(addr));
594
595 f = fopen(buf, "r");
596 if (!f)
597 return -1;
598 /* Need to read proc file with in one piece, so use large enough
599 * buffer. */
600 setbuffer(f, buf, sizeof(buf));
601
602 while (fgets(line, sizeof(line), f)) {
603 pos = strchr(line, '=');
604 if (!pos)
605 continue;
606 *pos++ = '\0';
607 val = strtoul(pos, NULL, 10);
608 if (strcmp(line, "rx_packets") == 0)
609 data->rx_packets = val;
610 else if (strcmp(line, "tx_packets") == 0)
611 data->tx_packets = val;
612 else if (strcmp(line, "rx_bytes") == 0)
613 data->rx_bytes = val;
614 else if (strcmp(line, "tx_bytes") == 0)
615 data->tx_bytes = val;
616 }
617
618 fclose(f);
619
620 return 0;
621}
622
623
624static int hostap_sta_add(void *priv, struct hostapd_sta_add_params *params)
625{
626 struct hostap_driver_data *drv = priv;
627 struct prism2_hostapd_param param;
628 int tx_supp_rates = 0;
629 size_t i;
630
631#define WLAN_RATE_1M BIT(0)
632#define WLAN_RATE_2M BIT(1)
633#define WLAN_RATE_5M5 BIT(2)
634#define WLAN_RATE_11M BIT(3)
635
636 for (i = 0; i < params->supp_rates_len; i++) {
637 if ((params->supp_rates[i] & 0x7f) == 2)
638 tx_supp_rates |= WLAN_RATE_1M;
639 if ((params->supp_rates[i] & 0x7f) == 4)
640 tx_supp_rates |= WLAN_RATE_2M;
641 if ((params->supp_rates[i] & 0x7f) == 11)
642 tx_supp_rates |= WLAN_RATE_5M5;
643 if ((params->supp_rates[i] & 0x7f) == 22)
644 tx_supp_rates |= WLAN_RATE_11M;
645 }
646
647 memset(&param, 0, sizeof(param));
648 param.cmd = PRISM2_HOSTAPD_ADD_STA;
649 memcpy(param.sta_addr, params->addr, ETH_ALEN);
650 param.u.add_sta.aid = params->aid;
651 param.u.add_sta.capability = params->capability;
652 param.u.add_sta.tx_supp_rates = tx_supp_rates;
653 return hostapd_ioctl(drv, &param, sizeof(param));
654}
655
656
657static int hostap_sta_remove(void *priv, const u8 *addr)
658{
659 struct hostap_driver_data *drv = priv;
660 struct prism2_hostapd_param param;
661
662 hostap_sta_set_flags(drv, addr, 0, 0, ~WPA_STA_AUTHORIZED);
663
664 memset(&param, 0, sizeof(param));
665 param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
666 memcpy(param.sta_addr, addr, ETH_ALEN);
667 if (hostapd_ioctl(drv, &param, sizeof(param))) {
668 printf("Could not remove station from kernel driver.\n");
669 return -1;
670 }
671 return 0;
672}
673
674
675static int hostap_get_inact_sec(void *priv, const u8 *addr)
676{
677 struct hostap_driver_data *drv = priv;
678 struct prism2_hostapd_param param;
679
680 memset(&param, 0, sizeof(param));
681 param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
682 memcpy(param.sta_addr, addr, ETH_ALEN);
683 if (hostapd_ioctl(drv, &param, sizeof(param))) {
684 return -1;
685 }
686
687 return param.u.get_info_sta.inactive_sec;
688}
689
690
691static int hostap_sta_clear_stats(void *priv, const u8 *addr)
692{
693 struct hostap_driver_data *drv = priv;
694 struct prism2_hostapd_param param;
695
696 memset(&param, 0, sizeof(param));
697 param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
698 memcpy(param.sta_addr, addr, ETH_ALEN);
699 if (hostapd_ioctl(drv, &param, sizeof(param))) {
700 return -1;
701 }
702
703 return 0;
704}
705
706
707static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
708{
709 struct prism2_hostapd_param *param;
710 int res;
711 size_t blen, elem_len;
712
713 elem_len = drv->generic_ie_len + drv->wps_ie_len;
714 blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
715 if (blen < sizeof(*param))
716 blen = sizeof(*param);
717
718 param = os_zalloc(blen);
719 if (param == NULL)
720 return -1;
721
722 param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
723 param->u.generic_elem.len = elem_len;
724 if (drv->generic_ie) {
725 os_memcpy(param->u.generic_elem.data, drv->generic_ie,
726 drv->generic_ie_len);
727 }
728 if (drv->wps_ie) {
729 os_memcpy(&param->u.generic_elem.data[drv->generic_ie_len],
730 drv->wps_ie, drv->wps_ie_len);
731 }
732 wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
733 param->u.generic_elem.data, elem_len);
734 res = hostapd_ioctl(drv, param, blen);
735
736 os_free(param);
737
738 return res;
739}
740
741
742static int hostap_set_generic_elem(void *priv,
743 const u8 *elem, size_t elem_len)
744{
745 struct hostap_driver_data *drv = priv;
746
747 os_free(drv->generic_ie);
748 drv->generic_ie = NULL;
749 drv->generic_ie_len = 0;
750 if (elem) {
751 drv->generic_ie = os_malloc(elem_len);
752 if (drv->generic_ie == NULL)
753 return -1;
754 os_memcpy(drv->generic_ie, elem, elem_len);
755 drv->generic_ie_len = elem_len;
756 }
757
758 return hostapd_ioctl_set_generic_elem(drv);
759}
760
761
762static int hostap_set_ap_wps_ie(void *priv, const struct wpabuf *beacon,
763 const struct wpabuf *proberesp,
764 const struct wpabuf *assocresp)
765{
766 struct hostap_driver_data *drv = priv;
767
768 /*
769 * Host AP driver supports only one set of extra IEs, so we need to
770 * use the Probe Response IEs also for Beacon frames since they include
771 * more information.
772 */
773
774 os_free(drv->wps_ie);
775 drv->wps_ie = NULL;
776 drv->wps_ie_len = 0;
777 if (proberesp) {
778 drv->wps_ie = os_malloc(wpabuf_len(proberesp));
779 if (drv->wps_ie == NULL)
780 return -1;
781 os_memcpy(drv->wps_ie, wpabuf_head(proberesp),
782 wpabuf_len(proberesp));
783 drv->wps_ie_len = wpabuf_len(proberesp);
784 }
785
786 return hostapd_ioctl_set_generic_elem(drv);
787}
788
789
790static void
791hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
792 char *custom)
793{
794 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
795
796 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
797 char *pos;
798 u8 addr[ETH_ALEN];
799 pos = strstr(custom, "addr=");
800 if (pos == NULL) {
801 wpa_printf(MSG_DEBUG,
802 "MLME-MICHAELMICFAILURE.indication "
803 "without sender address ignored");
804 return;
805 }
806 pos += 5;
807 if (hwaddr_aton(pos, addr) == 0) {
808 union wpa_event_data data;
809 os_memset(&data, 0, sizeof(data));
810 data.michael_mic_failure.unicast = 1;
811 data.michael_mic_failure.src = addr;
812 wpa_supplicant_event(drv->hapd,
813 EVENT_MICHAEL_MIC_FAILURE, &data);
814 } else {
815 wpa_printf(MSG_DEBUG,
816 "MLME-MICHAELMICFAILURE.indication "
817 "with invalid MAC address");
818 }
819 }
820}
821
822
823static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
824 char *data, int len)
825{
826 struct iw_event iwe_buf, *iwe = &iwe_buf;
827 char *pos, *end, *custom, *buf;
828
829 pos = data;
830 end = data + len;
831
832 while (pos + IW_EV_LCP_LEN <= end) {
833 /* Event data may be unaligned, so make a local, aligned copy
834 * before processing. */
835 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
836 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
837 iwe->cmd, iwe->len);
838 if (iwe->len <= IW_EV_LCP_LEN)
839 return;
840
841 custom = pos + IW_EV_POINT_LEN;
842 if (drv->we_version > 18 &&
843 (iwe->cmd == IWEVMICHAELMICFAILURE ||
844 iwe->cmd == IWEVCUSTOM)) {
845 /* WE-19 removed the pointer from struct iw_point */
846 char *dpos = (char *) &iwe_buf.u.data.length;
847 int dlen = dpos - (char *) &iwe_buf;
848 memcpy(dpos, pos + IW_EV_LCP_LEN,
849 sizeof(struct iw_event) - dlen);
850 } else {
851 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
852 custom += IW_EV_POINT_OFF;
853 }
854
855 switch (iwe->cmd) {
856 case IWEVCUSTOM:
857 if (custom + iwe->u.data.length > end)
858 return;
859 buf = malloc(iwe->u.data.length + 1);
860 if (buf == NULL)
861 return;
862 memcpy(buf, custom, iwe->u.data.length);
863 buf[iwe->u.data.length] = '\0';
864 hostapd_wireless_event_wireless_custom(drv, buf);
865 free(buf);
866 break;
867 }
868
869 pos += iwe->len;
870 }
871}
872
873
874static void hostapd_wireless_event_rtm_newlink(void *ctx,
875 struct ifinfomsg *ifi,
876 u8 *buf, size_t len)
877{
878 struct hostap_driver_data *drv = ctx;
879 int attrlen, rta_len;
880 struct rtattr *attr;
881
882 /* TODO: use ifi->ifi_index to filter out wireless events from other
883 * interfaces */
884
885 attrlen = len;
886 attr = (struct rtattr *) buf;
887
888 rta_len = RTA_ALIGN(sizeof(struct rtattr));
889 while (RTA_OK(attr, attrlen)) {
890 if (attr->rta_type == IFLA_WIRELESS) {
891 hostapd_wireless_event_wireless(
892 drv, ((char *) attr) + rta_len,
893 attr->rta_len - rta_len);
894 }
895 attr = RTA_NEXT(attr, attrlen);
896 }
897}
898
899
900static int hostap_get_we_version(struct hostap_driver_data *drv)
901{
902 struct iw_range *range;
903 struct iwreq iwr;
904 int minlen;
905 size_t buflen;
906
907 drv->we_version = 0;
908
909 /*
910 * Use larger buffer than struct iw_range in order to allow the
911 * structure to grow in the future.
912 */
913 buflen = sizeof(struct iw_range) + 500;
914 range = os_zalloc(buflen);
915 if (range == NULL)
916 return -1;
917
918 memset(&iwr, 0, sizeof(iwr));
919 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
920 iwr.u.data.pointer = (caddr_t) range;
921 iwr.u.data.length = buflen;
922
923 minlen = ((char *) &range->enc_capa) - (char *) range +
924 sizeof(range->enc_capa);
925
926 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800927 wpa_printf(MSG_ERROR, "ioctl[SIOCGIWRANGE]: %s",
928 strerror(errno));
929 os_free(range);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 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) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800984 wpa_printf(MSG_ERROR, "socket[PF_INET,SOCK_DGRAM]: %s",
985 strerror(errno));
986 os_free(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 return NULL;
988 }
989
990 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800991 wpa_printf(MSG_ERROR,
992 "Could not enable hostapd mode for interface %s",
993 drv->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 close(drv->ioctl_sock);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800995 os_free(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700996 return NULL;
997 }
998
999 if (hostap_init_sockets(drv, params->own_addr) ||
1000 hostap_wireless_event_init(drv)) {
1001 close(drv->ioctl_sock);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001002 os_free(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001003 return NULL;
1004 }
1005
1006 return drv;
1007}
1008
1009
1010static void hostap_driver_deinit(void *priv)
1011{
1012 struct hostap_driver_data *drv = priv;
1013
1014 netlink_deinit(drv->netlink);
1015 (void) hostap_set_iface_flags(drv, 0);
1016 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
1017 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
1018
1019 if (drv->ioctl_sock >= 0)
1020 close(drv->ioctl_sock);
1021
1022 if (drv->sock >= 0)
1023 close(drv->sock);
1024
1025 os_free(drv->generic_ie);
1026 os_free(drv->wps_ie);
1027
1028 free(drv);
1029}
1030
1031
1032static int hostap_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
1033 int reason)
1034{
1035 struct hostap_driver_data *drv = priv;
1036 struct ieee80211_mgmt mgmt;
1037
1038 if (is_broadcast_ether_addr(addr)) {
1039 /*
1040 * New Prism2.5/3 STA firmware versions seem to have issues
1041 * with this broadcast deauth frame. This gets the firmware in
1042 * odd state where nothing works correctly, so let's skip
1043 * sending this for the hostap driver.
1044 */
1045 return 0;
1046 }
1047
1048 memset(&mgmt, 0, sizeof(mgmt));
1049 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1050 WLAN_FC_STYPE_DEAUTH);
1051 memcpy(mgmt.da, addr, ETH_ALEN);
1052 memcpy(mgmt.sa, own_addr, ETH_ALEN);
1053 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
1054 mgmt.u.deauth.reason_code = host_to_le16(reason);
1055 return hostap_send_mlme(drv, (u8 *) &mgmt, IEEE80211_HDRLEN +
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001056 sizeof(mgmt.u.deauth), 0);
1057}
1058
1059
1060static int hostap_set_freq(void *priv, struct hostapd_freq_params *freq)
1061{
1062 struct hostap_driver_data *drv = priv;
1063 struct iwreq iwr;
1064
1065 os_memset(&iwr, 0, sizeof(iwr));
1066 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
1067 iwr.u.freq.m = freq->channel;
1068 iwr.u.freq.e = 0;
1069
1070 if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001071 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWFREQ]: %s",
1072 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001073 return -1;
1074 }
1075
1076 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001077}
1078
1079
1080static int hostap_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
1081 int reason)
1082{
1083 struct hostap_driver_data *drv = priv;
1084 struct ieee80211_mgmt mgmt;
1085
1086 memset(&mgmt, 0, sizeof(mgmt));
1087 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1088 WLAN_FC_STYPE_DISASSOC);
1089 memcpy(mgmt.da, addr, ETH_ALEN);
1090 memcpy(mgmt.sa, own_addr, ETH_ALEN);
1091 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
1092 mgmt.u.disassoc.reason_code = host_to_le16(reason);
1093 return hostap_send_mlme(drv, (u8 *) &mgmt, IEEE80211_HDRLEN +
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001094 sizeof(mgmt.u.disassoc), 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095}
1096
1097
1098static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
1099 u16 *num_modes,
1100 u16 *flags)
1101{
1102 struct hostapd_hw_modes *mode;
1103 int i, clen, rlen;
1104 const short chan2freq[14] = {
1105 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1106 2447, 2452, 2457, 2462, 2467, 2472, 2484
1107 };
1108
1109 mode = os_zalloc(sizeof(struct hostapd_hw_modes));
1110 if (mode == NULL)
1111 return NULL;
1112
1113 *num_modes = 1;
1114 *flags = 0;
1115
1116 mode->mode = HOSTAPD_MODE_IEEE80211B;
1117 mode->num_channels = 14;
1118 mode->num_rates = 4;
1119
1120 clen = mode->num_channels * sizeof(struct hostapd_channel_data);
1121 rlen = mode->num_rates * sizeof(int);
1122
1123 mode->channels = os_zalloc(clen);
1124 mode->rates = os_zalloc(rlen);
1125 if (mode->channels == NULL || mode->rates == NULL) {
1126 os_free(mode->channels);
1127 os_free(mode->rates);
1128 os_free(mode);
1129 return NULL;
1130 }
1131
1132 for (i = 0; i < 14; i++) {
1133 mode->channels[i].chan = i + 1;
1134 mode->channels[i].freq = chan2freq[i];
1135 /* TODO: Get allowed channel list from the driver */
1136 if (i >= 11)
1137 mode->channels[i].flag = HOSTAPD_CHAN_DISABLED;
1138 }
1139
1140 mode->rates[0] = 10;
1141 mode->rates[1] = 20;
1142 mode->rates[2] = 55;
1143 mode->rates[3] = 110;
1144
1145 return mode;
1146}
1147
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001149static void wpa_driver_hostap_poll_client(void *priv, const u8 *own_addr,
1150 const u8 *addr, int qos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001152 struct ieee80211_hdr hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001154 os_memset(&hdr, 0, sizeof(hdr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001156 /*
1157 * WLAN_FC_STYPE_NULLFUNC would be more appropriate,
1158 * but it is apparently not retried so TX Exc events
1159 * are not received for it.
1160 * This is the reason the driver overrides the default
1161 * handling.
1162 */
1163 hdr.frame_control = IEEE80211_FC(WLAN_FC_TYPE_DATA,
1164 WLAN_FC_STYPE_DATA);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001166 hdr.frame_control |=
1167 host_to_le16(WLAN_FC_FROMDS);
1168 os_memcpy(hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
1169 os_memcpy(hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
1170 os_memcpy(hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
1171
1172 hostap_send_mlme(priv, (u8 *)&hdr, sizeof(hdr), 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173}
1174
1175
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176const struct wpa_driver_ops wpa_driver_hostap_ops = {
1177 .name = "hostap",
1178 .desc = "Host AP driver (Intersil Prism2/2.5/3)",
1179 .set_key = wpa_driver_hostap_set_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180 .hapd_init = hostap_init,
1181 .hapd_deinit = hostap_driver_deinit,
1182 .set_ieee8021x = hostap_set_ieee8021x,
1183 .set_privacy = hostap_set_privacy,
1184 .get_seqnum = hostap_get_seqnum,
1185 .flush = hostap_flush,
1186 .set_generic_elem = hostap_set_generic_elem,
1187 .read_sta_data = hostap_read_sta_data,
1188 .hapd_send_eapol = hostap_send_eapol,
1189 .sta_set_flags = hostap_sta_set_flags,
1190 .sta_deauth = hostap_sta_deauth,
1191 .sta_disassoc = hostap_sta_disassoc,
1192 .sta_remove = hostap_sta_remove,
1193 .hapd_set_ssid = hostap_set_ssid,
1194 .send_mlme = hostap_send_mlme,
1195 .sta_add = hostap_sta_add,
1196 .get_inact_sec = hostap_get_inact_sec,
1197 .sta_clear_stats = hostap_sta_clear_stats,
1198 .get_hw_feature_data = hostap_get_hw_feature_data,
1199 .set_ap_wps_ie = hostap_set_ap_wps_ie,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001200 .set_freq = hostap_set_freq,
1201 .poll_client = wpa_driver_hostap_poll_client,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202};