Sync usage with man page.
[netbsd-mini2440.git] / dist / wpa / hostapd / driver_hostap.c
blob013e63116530489377554daa93df55387b54a605
1 /*
2 * hostapd / Kernel driver communication with Linux Host AP driver
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
16 #include <sys/ioctl.h>
18 #ifdef USE_KERNEL_HEADERS
19 /* compat-wireless does not include linux/compiler.h to define __user, so
20 * define it here */
21 #ifndef __user
22 #define __user
23 #endif /* __user */
24 #include <asm/types.h>
25 #include <linux/if_packet.h>
26 #include <linux/if_ether.h> /* The L2 protocols */
27 #include <linux/if_arp.h>
28 #include <linux/wireless.h>
29 #else /* USE_KERNEL_HEADERS */
30 #include <net/if_arp.h>
31 #include <netpacket/packet.h>
32 #include "wireless_copy.h"
33 #endif /* USE_KERNEL_HEADERS */
35 #include "hostapd.h"
36 #include "driver.h"
37 #include "ieee802_1x.h"
38 #include "eloop.h"
39 #include "priv_netlink.h"
40 #include "ieee802_11.h"
41 #include "sta_info.h"
42 #include "hostap_common.h"
43 #include "hw_features.h"
46 struct hostap_driver_data {
47 struct hostapd_data *hapd;
49 char iface[IFNAMSIZ + 1];
50 int sock; /* raw packet socket for driver access */
51 int ioctl_sock; /* socket for ioctl() use */
52 int wext_sock; /* socket for wireless events */
54 int we_version;
56 u8 *generic_ie;
57 size_t generic_ie_len;
61 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
62 int len);
63 static int hostap_set_iface_flags(void *priv, int dev_up);
65 static void handle_data(struct hostapd_data *hapd, u8 *buf, size_t len,
66 u16 stype)
68 struct ieee80211_hdr *hdr;
69 u16 fc, ethertype;
70 u8 *pos, *sa;
71 size_t left;
72 struct sta_info *sta;
74 if (len < sizeof(struct ieee80211_hdr))
75 return;
77 hdr = (struct ieee80211_hdr *) buf;
78 fc = le_to_host16(hdr->frame_control);
80 if ((fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) != WLAN_FC_TODS) {
81 printf("Not ToDS data frame (fc=0x%04x)\n", fc);
82 return;
85 sa = hdr->addr2;
86 sta = ap_get_sta(hapd, sa);
87 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
88 printf("Data frame from not associated STA " MACSTR "\n",
89 MAC2STR(sa));
90 if (sta && (sta->flags & WLAN_STA_AUTH))
91 hostapd_sta_disassoc(
92 hapd, sa,
93 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
94 else
95 hostapd_sta_deauth(
96 hapd, sa,
97 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
98 return;
101 pos = (u8 *) (hdr + 1);
102 left = len - sizeof(*hdr);
104 if (left < sizeof(rfc1042_header)) {
105 printf("Too short data frame\n");
106 return;
109 if (memcmp(pos, rfc1042_header, sizeof(rfc1042_header)) != 0) {
110 printf("Data frame with no RFC1042 header\n");
111 return;
113 pos += sizeof(rfc1042_header);
114 left -= sizeof(rfc1042_header);
116 if (left < 2) {
117 printf("No ethertype in data frame\n");
118 return;
121 ethertype = WPA_GET_BE16(pos);
122 pos += 2;
123 left -= 2;
124 switch (ethertype) {
125 case ETH_P_PAE:
126 ieee802_1x_receive(hapd, sa, pos, left);
127 break;
129 default:
130 printf("Unknown ethertype 0x%04x in data frame\n", ethertype);
131 break;
136 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
137 int ok)
139 struct ieee80211_hdr *hdr;
140 u16 fc, type, stype;
141 struct sta_info *sta;
143 hdr = (struct ieee80211_hdr *) buf;
144 fc = le_to_host16(hdr->frame_control);
146 type = WLAN_FC_GET_TYPE(fc);
147 stype = WLAN_FC_GET_STYPE(fc);
149 switch (type) {
150 case WLAN_FC_TYPE_MGMT:
151 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
152 ok ? "ACK" : "fail");
153 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
154 break;
155 case WLAN_FC_TYPE_CTRL:
156 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
157 ok ? "ACK" : "fail");
158 break;
159 case WLAN_FC_TYPE_DATA:
160 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
161 ok ? "ACK" : "fail");
162 sta = ap_get_sta(hapd, hdr->addr1);
163 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
164 wpa_printf(MSG_DEBUG, "STA " MACSTR
165 " %s pending activity poll",
166 MAC2STR(sta->addr),
167 ok ? "ACKed" : "did not ACK");
168 if (ok)
169 sta->flags &= ~WLAN_STA_PENDING_POLL;
171 if (sta)
172 ieee802_1x_tx_status(hapd, sta, buf, len, ok);
173 break;
174 default:
175 printf("unknown TX callback frame type %d\n", type);
176 break;
181 static void handle_frame(struct hostapd_data *hapd, u8 *buf, size_t len)
183 struct ieee80211_hdr *hdr;
184 u16 fc, extra_len, type, stype;
185 unsigned char *extra = NULL;
186 size_t data_len = len;
187 int ver;
189 /* PSPOLL is only 16 bytes, but driver does not (at least yet) pass
190 * these to user space */
191 if (len < 24) {
192 wpa_printf(MSG_MSGDUMP, "handle_frame: too short (%lu)",
193 (unsigned long) len);
194 return;
197 hdr = (struct ieee80211_hdr *) buf;
198 fc = le_to_host16(hdr->frame_control);
199 type = WLAN_FC_GET_TYPE(fc);
200 stype = WLAN_FC_GET_STYPE(fc);
202 if (type != WLAN_FC_TYPE_MGMT || stype != WLAN_FC_STYPE_BEACON) {
203 wpa_hexdump(MSG_MSGDUMP, "Received management frame",
204 buf, len);
207 ver = fc & WLAN_FC_PVER;
209 /* protocol version 3 is reserved for indicating extra data after the
210 * payload, version 2 for indicating ACKed frame (TX callbacks), and
211 * version 1 for indicating failed frame (no ACK, TX callbacks) */
212 if (ver == 3) {
213 u8 *pos = buf + len - 2;
214 extra_len = WPA_GET_LE16(pos);
215 printf("extra data in frame (elen=%d)\n", extra_len);
216 if ((size_t) extra_len + 2 > len) {
217 printf(" extra data overflow\n");
218 return;
220 len -= extra_len + 2;
221 extra = buf + len;
222 } else if (ver == 1 || ver == 2) {
223 handle_tx_callback(hapd, buf, data_len, ver == 2 ? 1 : 0);
224 return;
225 } else if (ver != 0) {
226 printf("unknown protocol version %d\n", ver);
227 return;
230 switch (type) {
231 case WLAN_FC_TYPE_MGMT:
232 if (stype != WLAN_FC_STYPE_BEACON)
233 wpa_printf(MSG_MSGDUMP, "MGMT");
234 ieee802_11_mgmt(hapd, buf, data_len, stype, NULL);
235 break;
236 case WLAN_FC_TYPE_CTRL:
237 wpa_printf(MSG_DEBUG, "CTRL");
238 break;
239 case WLAN_FC_TYPE_DATA:
240 wpa_printf(MSG_DEBUG, "DATA");
241 handle_data(hapd, buf, data_len, stype);
242 break;
243 default:
244 wpa_printf(MSG_DEBUG, "unknown frame type %d", type);
245 break;
250 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
252 struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
253 int len;
254 unsigned char buf[3000];
256 len = recv(sock, buf, sizeof(buf), 0);
257 if (len < 0) {
258 perror("recv");
259 return;
262 handle_frame(hapd, buf, len);
266 static int hostap_init_sockets(struct hostap_driver_data *drv)
268 struct ifreq ifr;
269 struct sockaddr_ll addr;
271 drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
272 if (drv->sock < 0) {
273 perror("socket[PF_PACKET,SOCK_RAW]");
274 return -1;
277 if (eloop_register_read_sock(drv->sock, handle_read, drv->hapd, NULL))
279 printf("Could not register read socket\n");
280 return -1;
283 memset(&ifr, 0, sizeof(ifr));
284 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
285 if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
286 perror("ioctl(SIOCGIFINDEX)");
287 return -1;
290 if (hostap_set_iface_flags(drv, 1)) {
291 return -1;
294 memset(&addr, 0, sizeof(addr));
295 addr.sll_family = AF_PACKET;
296 addr.sll_ifindex = ifr.ifr_ifindex;
297 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
298 addr.sll_ifindex);
300 if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
301 perror("bind");
302 return -1;
305 memset(&ifr, 0, sizeof(ifr));
306 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
307 if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
308 perror("ioctl(SIOCGIFHWADDR)");
309 return -1;
312 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
313 printf("Invalid HW-addr family 0x%04x\n",
314 ifr.ifr_hwaddr.sa_family);
315 return -1;
317 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
319 return 0;
323 static int hostap_send_mgmt_frame(void *priv, const void *msg, size_t len,
324 int flags)
326 struct hostap_driver_data *drv = priv;
328 return send(drv->sock, msg, len, flags);
332 static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
333 size_t data_len, int encrypt, const u8 *own_addr)
335 struct hostap_driver_data *drv = priv;
336 struct ieee80211_hdr *hdr;
337 size_t len;
338 u8 *pos;
339 int res;
341 len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
342 hdr = os_zalloc(len);
343 if (hdr == NULL) {
344 printf("malloc() failed for hostapd_send_data(len=%lu)\n",
345 (unsigned long) len);
346 return -1;
349 hdr->frame_control =
350 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
351 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
352 /* Request TX callback */
353 hdr->frame_control |= host_to_le16(BIT(1));
354 if (encrypt)
355 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
356 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
357 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
358 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
360 pos = (u8 *) (hdr + 1);
361 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
362 pos += sizeof(rfc1042_header);
363 *((u16 *) pos) = htons(ETH_P_PAE);
364 pos += 2;
365 memcpy(pos, data, data_len);
367 res = hostap_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
368 free(hdr);
370 if (res < 0) {
371 perror("hostapd_send_eapol: send");
372 printf("hostapd_send_eapol - packet len: %lu - failed\n",
373 (unsigned long) len);
376 return res;
380 static int hostap_sta_set_flags(void *priv, const u8 *addr,
381 int total_flags, int flags_or, int flags_and)
383 struct hostap_driver_data *drv = priv;
384 struct prism2_hostapd_param param;
386 memset(&param, 0, sizeof(param));
387 param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
388 memcpy(param.sta_addr, addr, ETH_ALEN);
389 param.u.set_flags_sta.flags_or = flags_or;
390 param.u.set_flags_sta.flags_and = flags_and;
391 return hostapd_ioctl(drv, &param, sizeof(param));
395 static int hostap_set_iface_flags(void *priv, int dev_up)
397 struct hostap_driver_data *drv = priv;
398 struct ifreq ifr;
400 if (drv->ioctl_sock < 0)
401 return -1;
403 memset(&ifr, 0, sizeof(ifr));
404 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
406 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
407 perror("ioctl[SIOCGIFFLAGS]");
408 return -1;
411 if (dev_up)
412 ifr.ifr_flags |= IFF_UP;
413 else
414 ifr.ifr_flags &= ~IFF_UP;
416 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
417 perror("ioctl[SIOCSIFFLAGS]");
418 return -1;
421 if (dev_up) {
422 memset(&ifr, 0, sizeof(ifr));
423 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
424 ifr.ifr_mtu = HOSTAPD_MTU;
425 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
426 perror("ioctl[SIOCSIFMTU]");
427 printf("Setting MTU failed - trying to survive with "
428 "current value\n");
432 return 0;
436 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
437 int len)
439 struct hostap_driver_data *drv = priv;
440 struct iwreq iwr;
442 memset(&iwr, 0, sizeof(iwr));
443 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
444 iwr.u.data.pointer = (caddr_t) param;
445 iwr.u.data.length = len;
447 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
448 perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
449 return -1;
452 return 0;
456 static int hostap_set_encryption(const char *ifname, void *priv,
457 const char *alg, const u8 *addr,
458 int idx, const u8 *key, size_t key_len,
459 int txkey)
461 struct hostap_driver_data *drv = priv;
462 struct prism2_hostapd_param *param;
463 u8 *buf;
464 size_t blen;
465 int ret = 0;
467 blen = sizeof(*param) + key_len;
468 buf = os_zalloc(blen);
469 if (buf == NULL)
470 return -1;
472 param = (struct prism2_hostapd_param *) buf;
473 param->cmd = PRISM2_SET_ENCRYPTION;
474 if (addr == NULL)
475 memset(param->sta_addr, 0xff, ETH_ALEN);
476 else
477 memcpy(param->sta_addr, addr, ETH_ALEN);
478 os_strlcpy((char *) param->u.crypt.alg, alg,
479 HOSTAP_CRYPT_ALG_NAME_LEN);
480 param->u.crypt.flags = txkey ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
481 param->u.crypt.idx = idx;
482 param->u.crypt.key_len = key_len;
483 memcpy((u8 *) (param + 1), key, key_len);
485 if (hostapd_ioctl(drv, param, blen)) {
486 printf("Failed to set encryption.\n");
487 ret = -1;
489 free(buf);
491 return ret;
495 static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
496 int idx, u8 *seq)
498 struct hostap_driver_data *drv = priv;
499 struct prism2_hostapd_param *param;
500 u8 *buf;
501 size_t blen;
502 int ret = 0;
504 blen = sizeof(*param) + 32;
505 buf = os_zalloc(blen);
506 if (buf == NULL)
507 return -1;
509 param = (struct prism2_hostapd_param *) buf;
510 param->cmd = PRISM2_GET_ENCRYPTION;
511 if (addr == NULL)
512 memset(param->sta_addr, 0xff, ETH_ALEN);
513 else
514 memcpy(param->sta_addr, addr, ETH_ALEN);
515 param->u.crypt.idx = idx;
517 if (hostapd_ioctl(drv, param, blen)) {
518 printf("Failed to get encryption.\n");
519 ret = -1;
520 } else {
521 memcpy(seq, param->u.crypt.seq, 8);
523 free(buf);
525 return ret;
529 static int hostap_ioctl_prism2param(void *priv, int param, int value)
531 struct hostap_driver_data *drv = priv;
532 struct iwreq iwr;
533 int *i;
535 memset(&iwr, 0, sizeof(iwr));
536 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
537 i = (int *) iwr.u.name;
538 *i++ = param;
539 *i++ = value;
541 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
542 perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]");
543 return -1;
546 return 0;
550 static int hostap_set_ieee8021x(const char *ifname, void *priv, int enabled)
552 struct hostap_driver_data *drv = priv;
554 /* enable kernel driver support for IEEE 802.1X */
555 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
556 printf("Could not setup IEEE 802.1X support in kernel driver."
557 "\n");
558 return -1;
561 if (!enabled)
562 return 0;
564 /* use host driver implementation of encryption to allow
565 * individual keys and passing plaintext EAPOL frames */
566 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
567 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
568 printf("Could not setup host-based encryption in kernel "
569 "driver.\n");
570 return -1;
573 return 0;
577 static int hostap_set_privacy(const char *ifname, void *priv, int enabled)
579 struct hostap_drvier_data *drv = priv;
581 return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
582 enabled);
586 static int hostap_set_ssid(const char *ifname, void *priv, const u8 *buf,
587 int len)
589 struct hostap_driver_data *drv = priv;
590 struct iwreq iwr;
592 memset(&iwr, 0, sizeof(iwr));
593 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
594 iwr.u.essid.flags = 1; /* SSID active */
595 iwr.u.essid.pointer = (caddr_t) buf;
596 iwr.u.essid.length = len + 1;
598 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
599 perror("ioctl[SIOCSIWESSID]");
600 printf("len=%d\n", len);
601 return -1;
604 return 0;
608 static int hostap_flush(void *priv)
610 struct hostap_driver_data *drv = priv;
611 struct prism2_hostapd_param param;
613 memset(&param, 0, sizeof(param));
614 param.cmd = PRISM2_HOSTAPD_FLUSH;
615 return hostapd_ioctl(drv, &param, sizeof(param));
619 static int hostap_read_sta_data(void *priv,
620 struct hostap_sta_driver_data *data,
621 const u8 *addr)
623 struct hostap_driver_data *drv = priv;
624 char buf[1024], line[128], *pos;
625 FILE *f;
626 unsigned long val;
628 memset(data, 0, sizeof(*data));
629 snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
630 drv->iface, MAC2STR(addr));
632 f = fopen(buf, "r");
633 if (!f)
634 return -1;
635 /* Need to read proc file with in one piece, so use large enough
636 * buffer. */
637 setbuffer(f, buf, sizeof(buf));
639 while (fgets(line, sizeof(line), f)) {
640 pos = strchr(line, '=');
641 if (!pos)
642 continue;
643 *pos++ = '\0';
644 val = strtoul(pos, NULL, 10);
645 if (strcmp(line, "rx_packets") == 0)
646 data->rx_packets = val;
647 else if (strcmp(line, "tx_packets") == 0)
648 data->tx_packets = val;
649 else if (strcmp(line, "rx_bytes") == 0)
650 data->rx_bytes = val;
651 else if (strcmp(line, "tx_bytes") == 0)
652 data->tx_bytes = val;
655 fclose(f);
657 return 0;
661 static int hostap_sta_add(const char *ifname, void *priv, const u8 *addr,
662 u16 aid, u16 capability, u8 *supp_rates,
663 size_t supp_rates_len, int flags,
664 u16 listen_interval)
666 struct hostap_driver_data *drv = priv;
667 struct prism2_hostapd_param param;
668 int tx_supp_rates = 0;
669 size_t i;
671 #define WLAN_RATE_1M BIT(0)
672 #define WLAN_RATE_2M BIT(1)
673 #define WLAN_RATE_5M5 BIT(2)
674 #define WLAN_RATE_11M BIT(3)
676 for (i = 0; i < supp_rates_len; i++) {
677 if ((supp_rates[i] & 0x7f) == 2)
678 tx_supp_rates |= WLAN_RATE_1M;
679 if ((supp_rates[i] & 0x7f) == 4)
680 tx_supp_rates |= WLAN_RATE_2M;
681 if ((supp_rates[i] & 0x7f) == 11)
682 tx_supp_rates |= WLAN_RATE_5M5;
683 if ((supp_rates[i] & 0x7f) == 22)
684 tx_supp_rates |= WLAN_RATE_11M;
687 memset(&param, 0, sizeof(param));
688 param.cmd = PRISM2_HOSTAPD_ADD_STA;
689 memcpy(param.sta_addr, addr, ETH_ALEN);
690 param.u.add_sta.aid = aid;
691 param.u.add_sta.capability = capability;
692 param.u.add_sta.tx_supp_rates = tx_supp_rates;
693 return hostapd_ioctl(drv, &param, sizeof(param));
697 static int hostap_sta_remove(void *priv, const u8 *addr)
699 struct hostap_driver_data *drv = priv;
700 struct prism2_hostapd_param param;
702 hostap_sta_set_flags(drv, addr, 0, 0, ~WLAN_STA_AUTHORIZED);
704 memset(&param, 0, sizeof(param));
705 param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
706 memcpy(param.sta_addr, addr, ETH_ALEN);
707 if (hostapd_ioctl(drv, &param, sizeof(param))) {
708 printf("Could not remove station from kernel driver.\n");
709 return -1;
711 return 0;
715 static int hostap_get_inact_sec(void *priv, const u8 *addr)
717 struct hostap_driver_data *drv = priv;
718 struct prism2_hostapd_param param;
720 memset(&param, 0, sizeof(param));
721 param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
722 memcpy(param.sta_addr, addr, ETH_ALEN);
723 if (hostapd_ioctl(drv, &param, sizeof(param))) {
724 return -1;
727 return param.u.get_info_sta.inactive_sec;
731 static int hostap_sta_clear_stats(void *priv, const u8 *addr)
733 struct hostap_driver_data *drv = priv;
734 struct prism2_hostapd_param param;
736 memset(&param, 0, sizeof(param));
737 param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
738 memcpy(param.sta_addr, addr, ETH_ALEN);
739 if (hostapd_ioctl(drv, &param, sizeof(param))) {
740 return -1;
743 return 0;
747 static int hostap_set_assoc_ap(void *priv, const u8 *addr)
749 struct hostap_driver_data *drv = priv;
750 struct prism2_hostapd_param param;
752 memset(&param, 0, sizeof(param));
753 param.cmd = PRISM2_HOSTAPD_SET_ASSOC_AP_ADDR;
754 memcpy(param.sta_addr, addr, ETH_ALEN);
755 if (hostapd_ioctl(drv, &param, sizeof(param)))
756 return -1;
758 return 0;
762 static int hostapd_ioctl_set_generic_elem(struct hostap_driver_data *drv)
764 struct prism2_hostapd_param *param;
765 int res;
766 size_t blen, elem_len;
768 elem_len = drv->generic_ie_len;
769 blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
770 if (blen < sizeof(*param))
771 blen = sizeof(*param);
773 param = os_zalloc(blen);
774 if (param == NULL)
775 return -1;
777 param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
778 param->u.generic_elem.len = elem_len;
779 if (drv->generic_ie) {
780 os_memcpy(param->u.generic_elem.data, drv->generic_ie,
781 drv->generic_ie_len);
783 wpa_hexdump(MSG_DEBUG, "hostap: Set generic IE",
784 param->u.generic_elem.data, elem_len);
785 res = hostapd_ioctl(drv, param, blen);
787 os_free(param);
789 return res;
793 static int hostap_set_generic_elem(const char *ifname, void *priv,
794 const u8 *elem, size_t elem_len)
796 struct hostap_driver_data *drv = priv;
798 os_free(drv->generic_ie);
799 drv->generic_ie = NULL;
800 drv->generic_ie_len = 0;
801 if (elem) {
802 drv->generic_ie = os_malloc(elem_len);
803 if (drv->generic_ie == NULL)
804 return -1;
805 os_memcpy(drv->generic_ie, elem, elem_len);
806 drv->generic_ie_len = elem_len;
809 return hostapd_ioctl_set_generic_elem(drv);
813 static void
814 hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
815 char *custom)
817 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
819 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
820 char *pos;
821 u8 addr[ETH_ALEN];
822 pos = strstr(custom, "addr=");
823 if (pos == NULL) {
824 wpa_printf(MSG_DEBUG,
825 "MLME-MICHAELMICFAILURE.indication "
826 "without sender address ignored");
827 return;
829 pos += 5;
830 if (hwaddr_aton(pos, addr) == 0) {
831 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
832 } else {
833 wpa_printf(MSG_DEBUG,
834 "MLME-MICHAELMICFAILURE.indication "
835 "with invalid MAC address");
841 static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
842 char *data, int len)
844 struct iw_event iwe_buf, *iwe = &iwe_buf;
845 char *pos, *end, *custom, *buf;
847 pos = data;
848 end = data + len;
850 while (pos + IW_EV_LCP_LEN <= end) {
851 /* Event data may be unaligned, so make a local, aligned copy
852 * before processing. */
853 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
854 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
855 iwe->cmd, iwe->len);
856 if (iwe->len <= IW_EV_LCP_LEN)
857 return;
859 custom = pos + IW_EV_POINT_LEN;
860 if (drv->we_version > 18 &&
861 (iwe->cmd == IWEVMICHAELMICFAILURE ||
862 iwe->cmd == IWEVCUSTOM)) {
863 /* WE-19 removed the pointer from struct iw_point */
864 char *dpos = (char *) &iwe_buf.u.data.length;
865 int dlen = dpos - (char *) &iwe_buf;
866 memcpy(dpos, pos + IW_EV_LCP_LEN,
867 sizeof(struct iw_event) - dlen);
868 } else {
869 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
870 custom += IW_EV_POINT_OFF;
873 switch (iwe->cmd) {
874 case IWEVCUSTOM:
875 if (custom + iwe->u.data.length > end)
876 return;
877 buf = malloc(iwe->u.data.length + 1);
878 if (buf == NULL)
879 return;
880 memcpy(buf, custom, iwe->u.data.length);
881 buf[iwe->u.data.length] = '\0';
882 hostapd_wireless_event_wireless_custom(drv, buf);
883 free(buf);
884 break;
887 pos += iwe->len;
892 static void hostapd_wireless_event_rtm_newlink(struct hostap_driver_data *drv,
893 struct nlmsghdr *h, int len)
895 struct ifinfomsg *ifi;
896 int attrlen, nlmsg_len, rta_len;
897 struct rtattr * attr;
899 if (len < (int) sizeof(*ifi))
900 return;
902 ifi = NLMSG_DATA(h);
904 /* TODO: use ifi->ifi_index to filter out wireless events from other
905 * interfaces */
907 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
909 attrlen = h->nlmsg_len - nlmsg_len;
910 if (attrlen < 0)
911 return;
913 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
915 rta_len = RTA_ALIGN(sizeof(struct rtattr));
916 while (RTA_OK(attr, attrlen)) {
917 if (attr->rta_type == IFLA_WIRELESS) {
918 hostapd_wireless_event_wireless(
919 drv, ((char *) attr) + rta_len,
920 attr->rta_len - rta_len);
922 attr = RTA_NEXT(attr, attrlen);
927 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
928 void *sock_ctx)
930 char buf[256];
931 int left;
932 struct sockaddr_nl from;
933 socklen_t fromlen;
934 struct nlmsghdr *h;
935 struct hostap_driver_data *drv = eloop_ctx;
937 fromlen = sizeof(from);
938 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
939 (struct sockaddr *) &from, &fromlen);
940 if (left < 0) {
941 if (errno != EINTR && errno != EAGAIN)
942 perror("recvfrom(netlink)");
943 return;
946 h = (struct nlmsghdr *) buf;
947 while (left >= (int) sizeof(*h)) {
948 int len, plen;
950 len = h->nlmsg_len;
951 plen = len - sizeof(*h);
952 if (len > left || plen < 0) {
953 printf("Malformed netlink message: "
954 "len=%d left=%d plen=%d\n",
955 len, left, plen);
956 break;
959 switch (h->nlmsg_type) {
960 case RTM_NEWLINK:
961 hostapd_wireless_event_rtm_newlink(drv, h, plen);
962 break;
965 len = NLMSG_ALIGN(len);
966 left -= len;
967 h = (struct nlmsghdr *) ((char *) h + len);
970 if (left > 0) {
971 printf("%d extra bytes in the end of netlink message\n", left);
976 static int hostap_get_we_version(struct hostap_driver_data *drv)
978 struct iw_range *range;
979 struct iwreq iwr;
980 int minlen;
981 size_t buflen;
983 drv->we_version = 0;
986 * Use larger buffer than struct iw_range in order to allow the
987 * structure to grow in the future.
989 buflen = sizeof(struct iw_range) + 500;
990 range = os_zalloc(buflen);
991 if (range == NULL)
992 return -1;
994 memset(&iwr, 0, sizeof(iwr));
995 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
996 iwr.u.data.pointer = (caddr_t) range;
997 iwr.u.data.length = buflen;
999 minlen = ((char *) &range->enc_capa) - (char *) range +
1000 sizeof(range->enc_capa);
1002 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
1003 perror("ioctl[SIOCGIWRANGE]");
1004 free(range);
1005 return -1;
1006 } else if (iwr.u.data.length >= minlen &&
1007 range->we_version_compiled >= 18) {
1008 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
1009 "WE(source)=%d enc_capa=0x%x",
1010 range->we_version_compiled,
1011 range->we_version_source,
1012 range->enc_capa);
1013 drv->we_version = range->we_version_compiled;
1016 free(range);
1017 return 0;
1021 static int hostap_wireless_event_init(void *priv)
1023 struct hostap_driver_data *drv = priv;
1024 int s;
1025 struct sockaddr_nl local;
1027 hostap_get_we_version(drv);
1029 drv->wext_sock = -1;
1031 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1032 if (s < 0) {
1033 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1034 return -1;
1037 memset(&local, 0, sizeof(local));
1038 local.nl_family = AF_NETLINK;
1039 local.nl_groups = RTMGRP_LINK;
1040 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
1041 perror("bind(netlink)");
1042 close(s);
1043 return -1;
1046 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
1047 NULL);
1048 drv->wext_sock = s;
1050 return 0;
1054 static void hostap_wireless_event_deinit(void *priv)
1056 struct hostap_driver_data *drv = priv;
1057 if (drv->wext_sock < 0)
1058 return;
1059 eloop_unregister_read_sock(drv->wext_sock);
1060 close(drv->wext_sock);
1064 static void * hostap_init(struct hostapd_data *hapd)
1066 struct hostap_driver_data *drv;
1068 drv = os_zalloc(sizeof(struct hostap_driver_data));
1069 if (drv == NULL) {
1070 printf("Could not allocate memory for hostapd driver data\n");
1071 return NULL;
1074 drv->hapd = hapd;
1075 drv->ioctl_sock = drv->sock = -1;
1076 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
1078 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1079 if (drv->ioctl_sock < 0) {
1080 perror("socket[PF_INET,SOCK_DGRAM]");
1081 free(drv);
1082 return NULL;
1085 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
1086 printf("Could not enable hostapd mode for interface %s\n",
1087 drv->iface);
1088 close(drv->ioctl_sock);
1089 free(drv);
1090 return NULL;
1093 if (hapd->conf->assoc_ap &&
1094 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 1)) {
1095 printf("Could not enable hostapd STA mode for interface %s\n",
1096 drv->iface);
1097 close(drv->ioctl_sock);
1098 free(drv);
1099 return NULL;
1102 if (hostap_init_sockets(drv)) {
1103 close(drv->ioctl_sock);
1104 free(drv);
1105 return NULL;
1108 return drv;
1112 static void hostap_driver_deinit(void *priv)
1114 struct hostap_driver_data *drv = priv;
1116 (void) hostap_set_iface_flags(drv, 0);
1117 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
1118 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
1120 if (drv->ioctl_sock >= 0)
1121 close(drv->ioctl_sock);
1123 if (drv->sock >= 0)
1124 close(drv->sock);
1126 os_free(drv->generic_ie);
1128 free(drv);
1132 static int hostap_sta_deauth(void *priv, const u8 *addr, int reason)
1134 struct hostap_driver_data *drv = priv;
1135 struct ieee80211_mgmt mgmt;
1137 memset(&mgmt, 0, sizeof(mgmt));
1138 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1139 WLAN_FC_STYPE_DEAUTH);
1140 memcpy(mgmt.da, addr, ETH_ALEN);
1141 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1142 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1143 mgmt.u.deauth.reason_code = host_to_le16(reason);
1144 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1145 sizeof(mgmt.u.deauth), 0);
1149 static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason)
1151 struct hostap_driver_data *drv = priv;
1152 struct ieee80211_mgmt mgmt;
1154 memset(&mgmt, 0, sizeof(mgmt));
1155 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1156 WLAN_FC_STYPE_DISASSOC);
1157 memcpy(mgmt.da, addr, ETH_ALEN);
1158 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1159 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1160 mgmt.u.disassoc.reason_code = host_to_le16(reason);
1161 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1162 sizeof(mgmt.u.disassoc), 0);
1166 static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
1167 u16 *num_modes,
1168 u16 *flags)
1170 struct hostapd_hw_modes *mode;
1171 int i, clen, rlen;
1172 const short chan2freq[14] = {
1173 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1174 2447, 2452, 2457, 2462, 2467, 2472, 2484
1177 mode = os_zalloc(sizeof(struct hostapd_hw_modes));
1178 if (mode == NULL)
1179 return NULL;
1181 *num_modes = 1;
1182 *flags = 0;
1184 mode->mode = HOSTAPD_MODE_IEEE80211B;
1185 mode->num_channels = 14;
1186 mode->num_rates = 4;
1188 clen = mode->num_channels * sizeof(struct hostapd_channel_data);
1189 rlen = mode->num_rates * sizeof(struct hostapd_rate_data);
1191 mode->channels = os_zalloc(clen);
1192 mode->rates = os_zalloc(rlen);
1193 if (mode->channels == NULL || mode->rates == NULL) {
1194 hostapd_free_hw_features(mode, *num_modes);
1195 return NULL;
1198 for (i = 0; i < 14; i++) {
1199 mode->channels[i].chan = i + 1;
1200 mode->channels[i].freq = chan2freq[i];
1203 mode->rates[0].rate = 10;
1204 mode->rates[0].flags = HOSTAPD_RATE_CCK;
1205 mode->rates[1].rate = 20;
1206 mode->rates[1].flags = HOSTAPD_RATE_CCK;
1207 mode->rates[2].rate = 55;
1208 mode->rates[2].flags = HOSTAPD_RATE_CCK;
1209 mode->rates[3].rate = 110;
1210 mode->rates[3].flags = HOSTAPD_RATE_CCK;
1212 return mode;
1216 const struct wpa_driver_ops wpa_driver_hostap_ops = {
1217 .name = "hostap",
1218 .init = hostap_init,
1219 .deinit = hostap_driver_deinit,
1220 .wireless_event_init = hostap_wireless_event_init,
1221 .wireless_event_deinit = hostap_wireless_event_deinit,
1222 .set_ieee8021x = hostap_set_ieee8021x,
1223 .set_privacy = hostap_set_privacy,
1224 .set_encryption = hostap_set_encryption,
1225 .get_seqnum = hostap_get_seqnum,
1226 .flush = hostap_flush,
1227 .set_generic_elem = hostap_set_generic_elem,
1228 .read_sta_data = hostap_read_sta_data,
1229 .send_eapol = hostap_send_eapol,
1230 .sta_set_flags = hostap_sta_set_flags,
1231 .sta_deauth = hostap_sta_deauth,
1232 .sta_disassoc = hostap_sta_disassoc,
1233 .sta_remove = hostap_sta_remove,
1234 .set_ssid = hostap_set_ssid,
1235 .send_mgmt_frame = hostap_send_mgmt_frame,
1236 .set_assoc_ap = hostap_set_assoc_ap,
1237 .sta_add = hostap_sta_add,
1238 .get_inact_sec = hostap_get_inact_sec,
1239 .sta_clear_stats = hostap_sta_clear_stats,
1240 .get_hw_feature_data = hostap_get_hw_feature_data,