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