Replace the nologin shell script with a tiny c version. Just for the sake
[dragonfly/netmp.git] / contrib / hostapd-0.5.8 / driver.c
blob077a5f72aa32f9da93e603f9d38d7b3c4d0091b3
1 /*
2 * hostapd / Kernel driver communication with Linux Host AP driver
3 * Copyright (c) 2002-2005, 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 #include <asm/types.h>
20 #include <linux/if_packet.h>
21 #include <linux/if_ether.h> /* The L2 protocols */
22 #include <linux/if_arp.h>
23 #include <linux/wireless.h>
24 #else /* USE_KERNEL_HEADERS */
25 #include <net/if_arp.h>
26 #include <netpacket/packet.h>
27 #include "wireless_copy.h"
28 #endif /* USE_KERNEL_HEADERS */
30 #include "hostapd.h"
31 #include "driver.h"
32 #include "ieee802_1x.h"
33 #include "eloop.h"
34 #include "priv_netlink.h"
35 #include "ieee802_11.h"
36 #include "sta_info.h"
37 #include "hostap_common.h"
38 #include "hw_features.h"
41 struct hostap_driver_data {
42 struct driver_ops ops;
43 struct hostapd_data *hapd;
45 char iface[IFNAMSIZ + 1];
46 int sock; /* raw packet socket for driver access */
47 int ioctl_sock; /* socket for ioctl() use */
48 int wext_sock; /* socket for wireless events */
50 int we_version;
53 static const struct driver_ops hostap_driver_ops;
56 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
57 int len);
58 static int hostap_set_iface_flags(void *priv, int dev_up);
60 static void handle_data(struct hostapd_data *hapd, u8 *buf, size_t len,
61 u16 stype)
63 struct ieee80211_hdr *hdr;
64 u16 fc, ethertype;
65 u8 *pos, *sa;
66 size_t left;
67 struct sta_info *sta;
69 if (len < sizeof(struct ieee80211_hdr))
70 return;
72 hdr = (struct ieee80211_hdr *) buf;
73 fc = le_to_host16(hdr->frame_control);
75 if ((fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) != WLAN_FC_TODS) {
76 printf("Not ToDS data frame (fc=0x%04x)\n", fc);
77 return;
80 sa = hdr->addr2;
81 sta = ap_get_sta(hapd, sa);
82 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
83 printf("Data frame from not associated STA " MACSTR "\n",
84 MAC2STR(sa));
85 if (sta && (sta->flags & WLAN_STA_AUTH))
86 hostapd_sta_disassoc(
87 hapd, sa,
88 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
89 else
90 hostapd_sta_deauth(
91 hapd, sa,
92 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
93 return;
96 pos = (u8 *) (hdr + 1);
97 left = len - sizeof(*hdr);
99 if (left < sizeof(rfc1042_header)) {
100 printf("Too short data frame\n");
101 return;
104 if (memcmp(pos, rfc1042_header, sizeof(rfc1042_header)) != 0) {
105 printf("Data frame with no RFC1042 header\n");
106 return;
108 pos += sizeof(rfc1042_header);
109 left -= sizeof(rfc1042_header);
111 if (left < 2) {
112 printf("No ethertype in data frame\n");
113 return;
116 ethertype = *pos++ << 8;
117 ethertype |= *pos++;
118 left -= 2;
119 switch (ethertype) {
120 case ETH_P_PAE:
121 ieee802_1x_receive(hapd, sa, pos, left);
122 break;
124 default:
125 printf("Unknown ethertype 0x%04x in data frame\n", ethertype);
126 break;
131 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
132 int ok)
134 struct ieee80211_hdr *hdr;
135 u16 fc, type, stype;
136 struct sta_info *sta;
138 hdr = (struct ieee80211_hdr *) buf;
139 fc = le_to_host16(hdr->frame_control);
141 type = WLAN_FC_GET_TYPE(fc);
142 stype = WLAN_FC_GET_STYPE(fc);
144 switch (type) {
145 case WLAN_FC_TYPE_MGMT:
146 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
147 "MGMT (TX callback) %s\n", ok ? "ACK" : "fail");
148 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
149 break;
150 case WLAN_FC_TYPE_CTRL:
151 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
152 "CTRL (TX callback) %s\n", ok ? "ACK" : "fail");
153 break;
154 case WLAN_FC_TYPE_DATA:
155 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
156 "DATA (TX callback) %s\n", ok ? "ACK" : "fail");
157 sta = ap_get_sta(hapd, hdr->addr1);
158 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
159 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "STA " MACSTR
160 " %s pending activity poll\n",
161 MAC2STR(sta->addr),
162 ok ? "ACKed" : "did not ACK");
163 if (ok)
164 sta->flags &= ~WLAN_STA_PENDING_POLL;
166 if (sta)
167 ieee802_1x_tx_status(hapd, sta, buf, len, ok);
168 break;
169 default:
170 printf("unknown TX callback frame type %d\n", type);
171 break;
176 static void handle_frame(struct hostapd_data *hapd, u8 *buf, size_t len)
178 struct ieee80211_hdr *hdr;
179 u16 fc, extra_len, type, stype;
180 unsigned char *extra = NULL;
181 size_t data_len = len;
182 int ver;
184 /* PSPOLL is only 16 bytes, but driver does not (at least yet) pass
185 * these to user space */
186 if (len < 24) {
187 HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE, "handle_frame: too short "
188 "(%lu)\n", (unsigned long) len);
189 return;
192 hdr = (struct ieee80211_hdr *) buf;
193 fc = le_to_host16(hdr->frame_control);
194 type = WLAN_FC_GET_TYPE(fc);
195 stype = WLAN_FC_GET_STYPE(fc);
197 if (type != WLAN_FC_TYPE_MGMT || stype != WLAN_FC_STYPE_BEACON ||
198 hapd->conf->debug >= HOSTAPD_DEBUG_EXCESSIVE) {
199 wpa_hexdump(MSG_MSGDUMP, "Received management frrame",
200 buf, len);
203 ver = fc & WLAN_FC_PVER;
205 /* protocol version 3 is reserved for indicating extra data after the
206 * payload, version 2 for indicating ACKed frame (TX callbacks), and
207 * version 1 for indicating failed frame (no ACK, TX callbacks) */
208 if (ver == 3) {
209 u8 *pos = buf + len - 2;
210 extra_len = (u16) pos[1] << 8 | pos[0];
211 printf("extra data in frame (elen=%d)\n", extra_len);
212 if ((size_t) extra_len + 2 > len) {
213 printf(" extra data overflow\n");
214 return;
216 len -= extra_len + 2;
217 extra = buf + len;
218 } else if (ver == 1 || ver == 2) {
219 handle_tx_callback(hapd, buf, data_len, ver == 2 ? 1 : 0);
220 return;
221 } else if (ver != 0) {
222 printf("unknown protocol version %d\n", ver);
223 return;
226 switch (type) {
227 case WLAN_FC_TYPE_MGMT:
228 HOSTAPD_DEBUG(stype == WLAN_FC_STYPE_BEACON ?
229 HOSTAPD_DEBUG_EXCESSIVE : HOSTAPD_DEBUG_VERBOSE,
230 "MGMT\n");
231 ieee802_11_mgmt(hapd, buf, data_len, stype, NULL);
232 break;
233 case WLAN_FC_TYPE_CTRL:
234 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "CTRL\n");
235 break;
236 case WLAN_FC_TYPE_DATA:
237 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "DATA\n");
238 handle_data(hapd, buf, data_len, stype);
239 break;
240 default:
241 printf("unknown frame type %d\n", type);
242 break;
247 static void handle_read(int sock, void *eloop_ctx, void *sock_ctx)
249 struct hostapd_data *hapd = (struct hostapd_data *) eloop_ctx;
250 int len;
251 unsigned char buf[3000];
253 len = recv(sock, buf, sizeof(buf), 0);
254 if (len < 0) {
255 perror("recv");
256 return;
259 handle_frame(hapd, buf, len);
263 static int hostap_init_sockets(struct hostap_driver_data *drv)
265 struct hostapd_data *hapd = drv->hapd;
266 struct ifreq ifr;
267 struct sockaddr_ll addr;
269 drv->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
270 if (drv->sock < 0) {
271 perror("socket[PF_PACKET,SOCK_RAW]");
272 return -1;
275 if (eloop_register_read_sock(drv->sock, handle_read, drv->hapd, NULL))
277 printf("Could not register read socket\n");
278 return -1;
281 memset(&ifr, 0, sizeof(ifr));
282 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%sap", drv->iface);
283 if (ioctl(drv->sock, SIOCGIFINDEX, &ifr) != 0) {
284 perror("ioctl(SIOCGIFINDEX)");
285 return -1;
288 if (hostap_set_iface_flags(drv, 1)) {
289 return -1;
292 memset(&addr, 0, sizeof(addr));
293 addr.sll_family = AF_PACKET;
294 addr.sll_ifindex = ifr.ifr_ifindex;
295 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
296 "Opening raw packet socket for ifindex %d\n",
297 addr.sll_ifindex);
299 if (bind(drv->sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
300 perror("bind");
301 return -1;
304 memset(&ifr, 0, sizeof(ifr));
305 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", drv->iface);
306 if (ioctl(drv->sock, SIOCGIFHWADDR, &ifr) != 0) {
307 perror("ioctl(SIOCGIFHWADDR)");
308 return -1;
311 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
312 printf("Invalid HW-addr family 0x%04x\n",
313 ifr.ifr_hwaddr.sa_family);
314 return -1;
316 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
318 return 0;
322 static int hostap_send_mgmt_frame(void *priv, const void *msg, size_t len,
323 int flags)
325 struct hostap_driver_data *drv = priv;
327 return send(drv->sock, msg, len, flags);
331 static int hostap_send_eapol(void *priv, const u8 *addr, const u8 *data,
332 size_t data_len, int encrypt, const u8 *own_addr)
334 struct hostap_driver_data *drv = priv;
335 struct ieee80211_hdr *hdr;
336 size_t len;
337 u8 *pos;
338 int res;
340 len = sizeof(*hdr) + sizeof(rfc1042_header) + 2 + data_len;
341 hdr = wpa_zalloc(len);
342 if (hdr == NULL) {
343 printf("malloc() failed for hostapd_send_data(len=%lu)\n",
344 (unsigned long) len);
345 return -1;
348 hdr->frame_control =
349 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
350 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
351 /* Request TX callback */
352 hdr->frame_control |= host_to_le16(BIT(1));
353 if (encrypt)
354 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
355 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
356 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
357 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
359 pos = (u8 *) (hdr + 1);
360 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
361 pos += sizeof(rfc1042_header);
362 *((u16 *) pos) = htons(ETH_P_PAE);
363 pos += 2;
364 memcpy(pos, data, data_len);
366 res = hostap_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
367 free(hdr);
369 if (res < 0) {
370 perror("hostapd_send_eapol: send");
371 printf("hostapd_send_eapol - packet len: %lu - failed\n",
372 (unsigned long) len);
375 return res;
379 static int hostap_sta_set_flags(void *priv, const u8 *addr,
380 int flags_or, int flags_and)
382 struct hostap_driver_data *drv = priv;
383 struct prism2_hostapd_param param;
385 memset(&param, 0, sizeof(param));
386 param.cmd = PRISM2_HOSTAPD_SET_FLAGS_STA;
387 memcpy(param.sta_addr, addr, ETH_ALEN);
388 param.u.set_flags_sta.flags_or = flags_or;
389 param.u.set_flags_sta.flags_and = flags_and;
390 return hostapd_ioctl(drv, &param, sizeof(param));
394 static int hostap_set_iface_flags(void *priv, int dev_up)
396 struct hostap_driver_data *drv = priv;
397 struct ifreq ifr;
399 if (drv->ioctl_sock < 0)
400 return -1;
402 memset(&ifr, 0, sizeof(ifr));
403 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
405 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
406 perror("ioctl[SIOCGIFFLAGS]");
407 return -1;
410 if (dev_up)
411 ifr.ifr_flags |= IFF_UP;
412 else
413 ifr.ifr_flags &= ~IFF_UP;
415 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
416 perror("ioctl[SIOCSIFFLAGS]");
417 return -1;
420 if (dev_up) {
421 memset(&ifr, 0, sizeof(ifr));
422 snprintf(ifr.ifr_name, IFNAMSIZ, "%sap", drv->iface);
423 ifr.ifr_mtu = HOSTAPD_MTU;
424 if (ioctl(drv->ioctl_sock, SIOCSIFMTU, &ifr) != 0) {
425 perror("ioctl[SIOCSIFMTU]");
426 printf("Setting MTU failed - trying to survive with "
427 "current value\n");
431 return 0;
435 static int hostapd_ioctl(void *priv, struct prism2_hostapd_param *param,
436 int len)
438 struct hostap_driver_data *drv = priv;
439 struct iwreq iwr;
441 memset(&iwr, 0, sizeof(iwr));
442 strncpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
443 iwr.u.data.pointer = (caddr_t) param;
444 iwr.u.data.length = len;
446 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_HOSTAPD, &iwr) < 0) {
447 perror("ioctl[PRISM2_IOCTL_HOSTAPD]");
448 return -1;
451 return 0;
455 static int hostap_set_encryption(const char *ifname, void *priv,
456 const char *alg, const u8 *addr,
457 int idx, const u8 *key, size_t key_len,
458 int txkey)
460 struct hostap_driver_data *drv = priv;
461 struct prism2_hostapd_param *param;
462 u8 *buf;
463 size_t blen;
464 int ret = 0;
466 blen = sizeof(*param) + key_len;
467 buf = wpa_zalloc(blen);
468 if (buf == NULL)
469 return -1;
471 param = (struct prism2_hostapd_param *) buf;
472 param->cmd = PRISM2_SET_ENCRYPTION;
473 if (addr == NULL)
474 memset(param->sta_addr, 0xff, ETH_ALEN);
475 else
476 memcpy(param->sta_addr, addr, ETH_ALEN);
477 strncpy((char *) param->u.crypt.alg, alg, HOSTAP_CRYPT_ALG_NAME_LEN);
478 param->u.crypt.flags = txkey ? HOSTAP_CRYPT_FLAG_SET_TX_KEY : 0;
479 param->u.crypt.idx = idx;
480 param->u.crypt.key_len = key_len;
481 memcpy((u8 *) (param + 1), key, key_len);
483 if (hostapd_ioctl(drv, param, blen)) {
484 printf("Failed to set encryption.\n");
485 ret = -1;
487 free(buf);
489 return ret;
493 static int hostap_get_seqnum(const char *ifname, void *priv, const u8 *addr,
494 int idx, u8 *seq)
496 struct hostap_driver_data *drv = priv;
497 struct prism2_hostapd_param *param;
498 u8 *buf;
499 size_t blen;
500 int ret = 0;
502 blen = sizeof(*param) + 32;
503 buf = wpa_zalloc(blen);
504 if (buf == NULL)
505 return -1;
507 param = (struct prism2_hostapd_param *) buf;
508 param->cmd = PRISM2_GET_ENCRYPTION;
509 if (addr == NULL)
510 memset(param->sta_addr, 0xff, ETH_ALEN);
511 else
512 memcpy(param->sta_addr, addr, ETH_ALEN);
513 param->u.crypt.idx = idx;
515 if (hostapd_ioctl(drv, param, blen)) {
516 printf("Failed to get encryption.\n");
517 ret = -1;
518 } else {
519 memcpy(seq, param->u.crypt.seq, 8);
521 free(buf);
523 return ret;
527 static int hostap_ioctl_prism2param(void *priv, int param, int value)
529 struct hostap_driver_data *drv = priv;
530 struct iwreq iwr;
531 int *i;
533 memset(&iwr, 0, sizeof(iwr));
534 strncpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
535 i = (int *) iwr.u.name;
536 *i++ = param;
537 *i++ = value;
539 if (ioctl(drv->ioctl_sock, PRISM2_IOCTL_PRISM2_PARAM, &iwr) < 0) {
540 perror("ioctl[PRISM2_IOCTL_PRISM2_PARAM]");
541 return -1;
544 return 0;
548 static int hostap_set_ieee8021x(const char *ifname, void *priv, int enabled)
550 struct hostap_driver_data *drv = priv;
552 /* enable kernel driver support for IEEE 802.1X */
553 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_IEEE_802_1X, enabled)) {
554 printf("Could not setup IEEE 802.1X support in kernel driver."
555 "\n");
556 return -1;
559 if (!enabled)
560 return 0;
562 /* use host driver implementation of encryption to allow
563 * individual keys and passing plaintext EAPOL frames */
564 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_DECRYPT, 1) ||
565 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOST_ENCRYPT, 1)) {
566 printf("Could not setup host-based encryption in kernel "
567 "driver.\n");
568 return -1;
571 return 0;
575 static int hostap_set_privacy(const char *ifname, void *priv, int enabled)
577 struct hostap_drvier_data *drv = priv;
579 return hostap_ioctl_prism2param(drv, PRISM2_PARAM_PRIVACY_INVOKED,
580 enabled);
584 static int hostap_set_ssid(const char *ifname, void *priv, const u8 *buf,
585 int len)
587 struct hostap_driver_data *drv = priv;
588 struct iwreq iwr;
590 memset(&iwr, 0, sizeof(iwr));
591 strncpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
592 iwr.u.essid.flags = 1; /* SSID active */
593 iwr.u.essid.pointer = (caddr_t) buf;
594 iwr.u.essid.length = len + 1;
596 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
597 perror("ioctl[SIOCSIWESSID]");
598 printf("len=%d\n", len);
599 return -1;
602 return 0;
606 static int hostap_flush(void *priv)
608 struct hostap_driver_data *drv = priv;
609 struct prism2_hostapd_param param;
611 memset(&param, 0, sizeof(param));
612 param.cmd = PRISM2_HOSTAPD_FLUSH;
613 return hostapd_ioctl(drv, &param, sizeof(param));
617 static int hostap_read_sta_data(void *priv,
618 struct hostap_sta_driver_data *data,
619 const u8 *addr)
621 struct hostap_driver_data *drv = priv;
622 char buf[1024], line[128], *pos;
623 FILE *f;
624 unsigned long val;
626 memset(data, 0, sizeof(*data));
627 snprintf(buf, sizeof(buf), "/proc/net/hostap/%s/" MACSTR,
628 drv->iface, MAC2STR(addr));
630 f = fopen(buf, "r");
631 if (!f)
632 return -1;
633 /* Need to read proc file with in one piece, so use large enough
634 * buffer. */
635 setbuffer(f, buf, sizeof(buf));
637 while (fgets(line, sizeof(line), f)) {
638 pos = strchr(line, '=');
639 if (!pos)
640 continue;
641 *pos++ = '\0';
642 val = strtoul(pos, NULL, 10);
643 if (strcmp(line, "rx_packets") == 0)
644 data->rx_packets = val;
645 else if (strcmp(line, "tx_packets") == 0)
646 data->tx_packets = val;
647 else if (strcmp(line, "rx_bytes") == 0)
648 data->rx_bytes = val;
649 else if (strcmp(line, "tx_bytes") == 0)
650 data->tx_bytes = val;
653 fclose(f);
655 return 0;
659 static int hostap_sta_add(const char *ifname, void *priv, const u8 *addr,
660 u16 aid, u16 capability, u8 *supp_rates,
661 size_t supp_rates_len, int flags)
663 struct hostap_driver_data *drv = priv;
664 struct prism2_hostapd_param param;
665 int tx_supp_rates = 0;
666 size_t i;
668 #define WLAN_RATE_1M BIT(0)
669 #define WLAN_RATE_2M BIT(1)
670 #define WLAN_RATE_5M5 BIT(2)
671 #define WLAN_RATE_11M BIT(3)
673 for (i = 0; i < supp_rates_len; i++) {
674 if ((supp_rates[i] & 0x7f) == 2)
675 tx_supp_rates |= WLAN_RATE_1M;
676 if ((supp_rates[i] & 0x7f) == 4)
677 tx_supp_rates |= WLAN_RATE_2M;
678 if ((supp_rates[i] & 0x7f) == 11)
679 tx_supp_rates |= WLAN_RATE_5M5;
680 if ((supp_rates[i] & 0x7f) == 22)
681 tx_supp_rates |= WLAN_RATE_11M;
684 memset(&param, 0, sizeof(param));
685 param.cmd = PRISM2_HOSTAPD_ADD_STA;
686 memcpy(param.sta_addr, addr, ETH_ALEN);
687 param.u.add_sta.aid = aid;
688 param.u.add_sta.capability = capability;
689 param.u.add_sta.tx_supp_rates = tx_supp_rates;
690 return hostapd_ioctl(drv, &param, sizeof(param));
694 static int hostap_sta_remove(void *priv, const u8 *addr)
696 struct hostap_driver_data *drv = priv;
697 struct prism2_hostapd_param param;
699 hostap_sta_set_flags(drv, addr, 0, ~WLAN_STA_AUTHORIZED);
701 memset(&param, 0, sizeof(param));
702 param.cmd = PRISM2_HOSTAPD_REMOVE_STA;
703 memcpy(param.sta_addr, addr, ETH_ALEN);
704 if (hostapd_ioctl(drv, &param, sizeof(param))) {
705 printf("Could not remove station from kernel driver.\n");
706 return -1;
708 return 0;
712 static int hostap_get_inact_sec(void *priv, const u8 *addr)
714 struct hostap_driver_data *drv = priv;
715 struct prism2_hostapd_param param;
717 memset(&param, 0, sizeof(param));
718 param.cmd = PRISM2_HOSTAPD_GET_INFO_STA;
719 memcpy(param.sta_addr, addr, ETH_ALEN);
720 if (hostapd_ioctl(drv, &param, sizeof(param))) {
721 return -1;
724 return param.u.get_info_sta.inactive_sec;
728 static int hostap_sta_clear_stats(void *priv, const u8 *addr)
730 struct hostap_driver_data *drv = priv;
731 struct prism2_hostapd_param param;
733 memset(&param, 0, sizeof(param));
734 param.cmd = PRISM2_HOSTAPD_STA_CLEAR_STATS;
735 memcpy(param.sta_addr, addr, ETH_ALEN);
736 if (hostapd_ioctl(drv, &param, sizeof(param))) {
737 return -1;
740 return 0;
744 static int hostap_set_assoc_ap(void *priv, const u8 *addr)
746 struct hostap_driver_data *drv = priv;
747 struct prism2_hostapd_param param;
749 memset(&param, 0, sizeof(param));
750 param.cmd = PRISM2_HOSTAPD_SET_ASSOC_AP_ADDR;
751 memcpy(param.sta_addr, addr, ETH_ALEN);
752 if (hostapd_ioctl(drv, &param, sizeof(param)))
753 return -1;
755 return 0;
759 static int hostap_set_generic_elem(const char *ifname, void *priv,
760 const u8 *elem, size_t elem_len)
762 struct hostap_driver_data *drv = priv;
763 struct prism2_hostapd_param *param;
764 int res;
765 size_t blen = PRISM2_HOSTAPD_GENERIC_ELEMENT_HDR_LEN + elem_len;
766 if (blen < sizeof(*param))
767 blen = sizeof(*param);
769 param = wpa_zalloc(blen);
770 if (param == NULL)
771 return -1;
773 param->cmd = PRISM2_HOSTAPD_SET_GENERIC_ELEMENT;
774 param->u.generic_elem.len = elem_len;
775 memcpy(param->u.generic_elem.data, elem, elem_len);
776 res = hostapd_ioctl(drv, param, blen);
778 free(param);
780 return res;
784 static void
785 hostapd_wireless_event_wireless_custom(struct hostap_driver_data *drv,
786 char *custom)
788 struct hostapd_data *hapd = drv->hapd;
790 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "Custom wireless event: '%s'\n",
791 custom);
793 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
794 char *pos;
795 u8 addr[ETH_ALEN];
796 pos = strstr(custom, "addr=");
797 if (pos == NULL) {
798 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
799 "MLME-MICHAELMICFAILURE.indication "
800 "without sender address ignored\n");
801 return;
803 pos += 5;
804 if (hwaddr_aton(pos, addr) == 0) {
805 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
806 } else {
807 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
808 "MLME-MICHAELMICFAILURE.indication "
809 "with invalid MAC address");
815 static void hostapd_wireless_event_wireless(struct hostap_driver_data *drv,
816 char *data, int len)
818 struct hostapd_data *hapd = drv->hapd;
819 struct iw_event iwe_buf, *iwe = &iwe_buf;
820 char *pos, *end, *custom, *buf;
822 pos = data;
823 end = data + len;
825 while (pos + IW_EV_LCP_LEN <= end) {
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 HOSTAPD_DEBUG(HOSTAPD_DEBUG_VERBOSE, "Wireless event: "
830 "cmd=0x%x len=%d\n", iwe->cmd, iwe->len);
831 if (iwe->len <= IW_EV_LCP_LEN)
832 return;
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;
848 switch (iwe->cmd) {
849 case IWEVCUSTOM:
850 if (custom + iwe->u.data.length > end)
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;
862 pos += iwe->len;
867 static void hostapd_wireless_event_rtm_newlink(struct hostap_driver_data *drv,
868 struct nlmsghdr *h, int len)
870 struct ifinfomsg *ifi;
871 int attrlen, nlmsg_len, rta_len;
872 struct rtattr * attr;
874 if (len < (int) sizeof(*ifi))
875 return;
877 ifi = NLMSG_DATA(h);
879 /* TODO: use ifi->ifi_index to filter out wireless events from other
880 * interfaces */
882 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
884 attrlen = h->nlmsg_len - nlmsg_len;
885 if (attrlen < 0)
886 return;
888 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
890 rta_len = RTA_ALIGN(sizeof(struct rtattr));
891 while (RTA_OK(attr, attrlen)) {
892 if (attr->rta_type == IFLA_WIRELESS) {
893 hostapd_wireless_event_wireless(
894 drv, ((char *) attr) + rta_len,
895 attr->rta_len - rta_len);
897 attr = RTA_NEXT(attr, attrlen);
902 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
903 void *sock_ctx)
905 char buf[256];
906 int left;
907 struct sockaddr_nl from;
908 socklen_t fromlen;
909 struct nlmsghdr *h;
910 struct hostap_driver_data *drv = eloop_ctx;
912 fromlen = sizeof(from);
913 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
914 (struct sockaddr *) &from, &fromlen);
915 if (left < 0) {
916 if (errno != EINTR && errno != EAGAIN)
917 perror("recvfrom(netlink)");
918 return;
921 h = (struct nlmsghdr *) buf;
922 while (left >= (int) sizeof(*h)) {
923 int len, plen;
925 len = h->nlmsg_len;
926 plen = len - sizeof(*h);
927 if (len > left || plen < 0) {
928 printf("Malformed netlink message: "
929 "len=%d left=%d plen=%d\n",
930 len, left, plen);
931 break;
934 switch (h->nlmsg_type) {
935 case RTM_NEWLINK:
936 hostapd_wireless_event_rtm_newlink(drv, h, plen);
937 break;
940 len = NLMSG_ALIGN(len);
941 left -= len;
942 h = (struct nlmsghdr *) ((char *) h + len);
945 if (left > 0) {
946 printf("%d extra bytes in the end of netlink message\n", left);
951 static int hostap_get_we_version(struct hostap_driver_data *drv)
953 struct iw_range *range;
954 struct iwreq iwr;
955 int minlen;
956 size_t buflen;
958 drv->we_version = 0;
961 * Use larger buffer than struct iw_range in order to allow the
962 * structure to grow in the future.
964 buflen = sizeof(struct iw_range) + 500;
965 range = wpa_zalloc(buflen);
966 if (range == NULL)
967 return -1;
969 memset(&iwr, 0, sizeof(iwr));
970 strncpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
971 iwr.u.data.pointer = (caddr_t) range;
972 iwr.u.data.length = buflen;
974 minlen = ((char *) &range->enc_capa) - (char *) range +
975 sizeof(range->enc_capa);
977 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
978 perror("ioctl[SIOCGIWRANGE]");
979 free(range);
980 return -1;
981 } else if (iwr.u.data.length >= minlen &&
982 range->we_version_compiled >= 18) {
983 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
984 "WE(source)=%d enc_capa=0x%x",
985 range->we_version_compiled,
986 range->we_version_source,
987 range->enc_capa);
988 drv->we_version = range->we_version_compiled;
991 free(range);
992 return 0;
996 static int hostap_wireless_event_init(void *priv)
998 struct hostap_driver_data *drv = priv;
999 int s;
1000 struct sockaddr_nl local;
1002 hostap_get_we_version(drv);
1004 drv->wext_sock = -1;
1006 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
1007 if (s < 0) {
1008 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
1009 return -1;
1012 memset(&local, 0, sizeof(local));
1013 local.nl_family = AF_NETLINK;
1014 local.nl_groups = RTMGRP_LINK;
1015 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
1016 perror("bind(netlink)");
1017 close(s);
1018 return -1;
1021 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
1022 NULL);
1023 drv->wext_sock = s;
1025 return 0;
1029 static void hostap_wireless_event_deinit(void *priv)
1031 struct hostap_driver_data *drv = priv;
1032 if (drv->wext_sock < 0)
1033 return;
1034 eloop_unregister_read_sock(drv->wext_sock);
1035 close(drv->wext_sock);
1039 static int hostap_init(struct hostapd_data *hapd)
1041 struct hostap_driver_data *drv;
1043 drv = wpa_zalloc(sizeof(struct hostap_driver_data));
1044 if (drv == NULL) {
1045 printf("Could not allocate memory for hostapd driver data\n");
1046 return -1;
1049 drv->ops = hostap_driver_ops;
1050 drv->hapd = hapd;
1051 drv->ioctl_sock = drv->sock = -1;
1052 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
1054 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1055 if (drv->ioctl_sock < 0) {
1056 perror("socket[PF_INET,SOCK_DGRAM]");
1057 free(drv);
1058 return -1;
1061 if (hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 1)) {
1062 printf("Could not enable hostapd mode for interface %s\n",
1063 drv->iface);
1064 close(drv->ioctl_sock);
1065 free(drv);
1066 return -1;
1069 if (hapd->conf->assoc_ap &&
1070 hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 1)) {
1071 printf("Could not enable hostapd STA mode for interface %s\n",
1072 drv->iface);
1073 close(drv->ioctl_sock);
1074 free(drv);
1075 return -1;
1078 if (hostap_init_sockets(drv)) {
1079 close(drv->ioctl_sock);
1080 free(drv);
1081 return -1;
1084 hapd->driver = &drv->ops;
1085 return 0;
1089 static void hostap_driver_deinit(void *priv)
1091 struct hostap_driver_data *drv = priv;
1093 drv->hapd->driver = NULL;
1095 (void) hostap_set_iface_flags(drv, 0);
1096 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD, 0);
1097 (void) hostap_ioctl_prism2param(drv, PRISM2_PARAM_HOSTAPD_STA, 0);
1099 if (drv->ioctl_sock >= 0)
1100 close(drv->ioctl_sock);
1102 if (drv->sock >= 0)
1103 close(drv->sock);
1105 free(drv);
1109 static int hostap_sta_deauth(void *priv, const u8 *addr, int reason)
1111 struct hostap_driver_data *drv = priv;
1112 struct ieee80211_mgmt mgmt;
1114 memset(&mgmt, 0, sizeof(mgmt));
1115 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1116 WLAN_FC_STYPE_DEAUTH);
1117 memcpy(mgmt.da, addr, ETH_ALEN);
1118 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1119 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1120 mgmt.u.deauth.reason_code = host_to_le16(reason);
1121 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1122 sizeof(mgmt.u.deauth), 0);
1126 static int hostap_sta_disassoc(void *priv, const u8 *addr, int reason)
1128 struct hostap_driver_data *drv = priv;
1129 struct ieee80211_mgmt mgmt;
1131 memset(&mgmt, 0, sizeof(mgmt));
1132 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1133 WLAN_FC_STYPE_DISASSOC);
1134 memcpy(mgmt.da, addr, ETH_ALEN);
1135 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
1136 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
1137 mgmt.u.disassoc.reason_code = host_to_le16(reason);
1138 return hostap_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
1139 sizeof(mgmt.u.disassoc), 0);
1143 static struct hostapd_hw_modes * hostap_get_hw_feature_data(void *priv,
1144 u16 *num_modes,
1145 u16 *flags)
1147 struct hostapd_hw_modes *mode;
1148 int i, clen, rlen;
1149 const short chan2freq[14] = {
1150 2412, 2417, 2422, 2427, 2432, 2437, 2442,
1151 2447, 2452, 2457, 2462, 2467, 2472, 2484
1154 mode = wpa_zalloc(sizeof(struct hostapd_hw_modes));
1155 if (mode == NULL)
1156 return NULL;
1158 *num_modes = 1;
1159 *flags = 0;
1161 mode->mode = HOSTAPD_MODE_IEEE80211B;
1162 mode->num_channels = 14;
1163 mode->num_rates = 4;
1165 clen = mode->num_channels * sizeof(struct hostapd_channel_data);
1166 rlen = mode->num_rates * sizeof(struct hostapd_rate_data);
1168 mode->channels = wpa_zalloc(clen);
1169 mode->rates = wpa_zalloc(rlen);
1170 if (mode->channels == NULL || mode->rates == NULL) {
1171 hostapd_free_hw_features(mode, *num_modes);
1172 return NULL;
1175 for (i = 0; i < 14; i++) {
1176 mode->channels[i].chan = i + 1;
1177 mode->channels[i].freq = chan2freq[i];
1180 mode->rates[0].rate = 10;
1181 mode->rates[0].flags = HOSTAPD_RATE_CCK;
1182 mode->rates[1].rate = 20;
1183 mode->rates[1].flags = HOSTAPD_RATE_CCK;
1184 mode->rates[2].rate = 55;
1185 mode->rates[2].flags = HOSTAPD_RATE_CCK;
1186 mode->rates[3].rate = 110;
1187 mode->rates[3].flags = HOSTAPD_RATE_CCK;
1189 return mode;
1193 static const struct driver_ops hostap_driver_ops = {
1194 .name = "hostap",
1195 .init = hostap_init,
1196 .deinit = hostap_driver_deinit,
1197 .wireless_event_init = hostap_wireless_event_init,
1198 .wireless_event_deinit = hostap_wireless_event_deinit,
1199 .set_ieee8021x = hostap_set_ieee8021x,
1200 .set_privacy = hostap_set_privacy,
1201 .set_encryption = hostap_set_encryption,
1202 .get_seqnum = hostap_get_seqnum,
1203 .flush = hostap_flush,
1204 .set_generic_elem = hostap_set_generic_elem,
1205 .read_sta_data = hostap_read_sta_data,
1206 .send_eapol = hostap_send_eapol,
1207 .sta_set_flags = hostap_sta_set_flags,
1208 .sta_deauth = hostap_sta_deauth,
1209 .sta_disassoc = hostap_sta_disassoc,
1210 .sta_remove = hostap_sta_remove,
1211 .set_ssid = hostap_set_ssid,
1212 .send_mgmt_frame = hostap_send_mgmt_frame,
1213 .set_assoc_ap = hostap_set_assoc_ap,
1214 .sta_add = hostap_sta_add,
1215 .get_inact_sec = hostap_get_inact_sec,
1216 .sta_clear_stats = hostap_sta_clear_stats,
1217 .get_hw_feature_data = hostap_get_hw_feature_data,
1221 void hostap_driver_register(void)
1223 driver_register(hostap_driver_ops.name, &hostap_driver_ops);