Sync usage with man page.
[netbsd-mini2440.git] / dist / wpa / hostapd / driver_nl80211.c
blob836ed42e200b0997eef36404b97cbbc4da76ad79
1 /*
2 * hostapd / Kernel driver communication via nl80211
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * Alternatively, this software may be distributed under the terms of BSD
13 * license.
15 * See README and COPYING for more details.
18 #include "includes.h"
20 #include <sys/ioctl.h>
21 #include <netlink/genl/genl.h>
22 #include <netlink/genl/family.h>
23 #include <netlink/genl/ctrl.h>
24 #include <netlink/msg.h>
25 #include <netlink/attr.h>
26 #include <linux/nl80211.h>
27 #include <net/if.h>
28 #include <linux/if_packet.h>
29 #include <linux/if_ether.h> /* The L2 protocols */
30 #include "wireless_copy.h"
31 #include <net/if_arp.h>
33 #include "hostapd.h"
34 #include "driver.h"
35 #include "ieee802_1x.h"
36 #include "eloop.h"
37 #include "ieee802_11.h"
38 #include "sta_info.h"
39 #include "hw_features.h"
40 #include "mlme.h"
41 #include "radiotap.h"
42 #include "radiotap_iter.h"
44 enum ieee80211_msg_type {
45 ieee80211_msg_normal = 0,
46 ieee80211_msg_tx_callback_ack = 1,
47 ieee80211_msg_tx_callback_fail = 2,
50 struct i802_driver_data {
51 struct hostapd_data *hapd;
53 char iface[IFNAMSIZ + 1];
54 int bridge;
55 int ioctl_sock; /* socket for ioctl() use */
56 int wext_sock; /* socket for wireless events */
57 int eapol_sock; /* socket for EAPOL frames */
58 int monitor_sock; /* socket for monitor */
59 int monitor_ifidx;
61 int default_if_indices[16];
62 int *if_indices;
63 int num_if_indices;
65 int we_version;
66 struct nl_handle *nl_handle;
67 struct nl_cache *nl_cache;
68 struct nl_cb *nl_cb;
69 struct genl_family *nl80211;
70 int dtim_period, beacon_int;
71 unsigned int beacon_set:1;
72 unsigned int ieee802_1x_active:1;
76 static void add_ifidx(struct i802_driver_data *drv, int ifidx)
78 int i;
79 int *old;
81 for (i = 0; i < drv->num_if_indices; i++) {
82 if (drv->if_indices[i] == 0) {
83 drv->if_indices[i] = ifidx;
84 return;
88 if (drv->if_indices != drv->default_if_indices)
89 old = drv->if_indices;
90 else
91 old = NULL;
93 drv->if_indices = realloc(old,
94 sizeof(int) * (drv->num_if_indices + 1));
95 if (!drv->if_indices) {
96 if (!old)
97 drv->if_indices = drv->default_if_indices;
98 else
99 drv->if_indices = old;
100 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
101 "interfaces");
102 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
103 return;
105 drv->if_indices[drv->num_if_indices] = ifidx;
106 drv->num_if_indices++;
110 static void del_ifidx(struct i802_driver_data *drv, int ifidx)
112 int i;
114 for (i = 0; i < drv->num_if_indices; i++) {
115 if (drv->if_indices[i] == ifidx) {
116 drv->if_indices[i] = 0;
117 break;
123 static int have_ifidx(struct i802_driver_data *drv, int ifidx)
125 int i;
127 if (ifidx == drv->bridge)
128 return 1;
130 for (i = 0; i < drv->num_if_indices; i++)
131 if (drv->if_indices[i] == ifidx)
132 return 1;
134 return 0;
138 /* helper for netlink get routines */
139 static int ack_wait_handler(struct nl_msg *msg, void *arg)
141 int *finished = arg;
143 *finished = 1;
144 return NL_STOP;
148 static int hostapd_set_iface_flags(struct i802_driver_data *drv,
149 const char *ifname, int dev_up)
151 struct ifreq ifr;
153 if (drv->ioctl_sock < 0)
154 return -1;
156 memset(&ifr, 0, sizeof(ifr));
157 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
159 if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
160 perror("ioctl[SIOCGIFFLAGS]");
161 wpa_printf(MSG_DEBUG, "Could not read interface flags (%s)",
162 drv->iface);
163 return -1;
166 if (dev_up)
167 ifr.ifr_flags |= IFF_UP;
168 else
169 ifr.ifr_flags &= ~IFF_UP;
171 if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
172 perror("ioctl[SIOCSIFFLAGS]");
173 return -1;
176 return 0;
180 static int nl_set_encr(int ifindex, struct i802_driver_data *drv,
181 const char *alg, const u8 *addr, int idx, const u8 *key,
182 size_t key_len, int txkey)
184 struct nl_msg *msg;
185 int ret = -1;
186 int err = 0;
188 msg = nlmsg_alloc();
189 if (!msg)
190 goto out;
192 if (strcmp(alg, "none") == 0) {
193 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
194 0, NL80211_CMD_DEL_KEY, 0);
195 } else {
196 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
197 0, NL80211_CMD_NEW_KEY, 0);
198 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
199 if (strcmp(alg, "WEP") == 0) {
200 if (key_len == 5)
201 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
202 0x000FAC01);
203 else
204 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
205 0x000FAC05);
206 } else if (strcmp(alg, "TKIP") == 0)
207 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC02);
208 else if (strcmp(alg, "CCMP") == 0)
209 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC04);
210 else if (strcmp(alg, "IGTK") == 0)
211 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC06);
212 else {
213 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
214 "algorithm '%s'", __func__, alg);
215 goto out;
219 if (addr)
220 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
221 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
222 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
224 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
225 (err = nl_wait_for_ack(drv->nl_handle)) < 0) {
226 if (err != -ENOENT) {
227 ret = 0;
228 goto out;
233 * If we need to set the default TX key we do that below,
234 * otherwise we're done here.
236 if (!txkey || addr) {
237 ret = 0;
238 goto out;
241 nlmsg_free(msg);
243 msg = nlmsg_alloc();
244 if (!msg)
245 goto out;
247 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
248 0, NL80211_CMD_SET_KEY, 0);
249 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
250 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
251 #ifdef NL80211_MFP_PENDING
252 if (strcmp(alg, "IGTK") == 0)
253 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
254 else
255 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
256 #else /* NL80211_MFP_PENDING */
257 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
258 #endif /* NL80211_MFP_PENDING */
260 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
261 (err = nl_wait_for_ack(drv->nl_handle)) < 0) {
262 if (err != -ENOENT) {
263 ret = 0;
264 goto out;
268 ret = 0;
270 out:
271 nla_put_failure:
272 nlmsg_free(msg);
273 return ret;
277 static int i802_set_encryption(const char *iface, void *priv, const char *alg,
278 const u8 *addr, int idx, const u8 *key,
279 size_t key_len, int txkey)
281 struct i802_driver_data *drv = priv;
282 int ret;
284 ret = nl_set_encr(if_nametoindex(iface), drv, alg, addr, idx, key,
285 key_len, txkey);
286 if (ret < 0)
287 return ret;
289 if (strcmp(alg, "IGTK") == 0) {
290 ret = nl_set_encr(drv->monitor_ifidx, drv, alg, addr, idx, key,
291 key_len, txkey);
294 return ret;
298 static inline int min_int(int a, int b)
300 if (a < b)
301 return a;
302 return b;
306 static int get_key_handler(struct nl_msg *msg, void *arg)
308 struct nlattr *tb[NL80211_ATTR_MAX + 1];
309 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
311 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
312 genlmsg_attrlen(gnlh, 0), NULL);
315 * TODO: validate the key index and mac address!
316 * Otherwise, there's a race condition as soon as
317 * the kernel starts sending key notifications.
320 if (tb[NL80211_ATTR_KEY_SEQ])
321 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
322 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
323 return NL_SKIP;
327 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
328 int idx, u8 *seq)
330 struct i802_driver_data *drv = priv;
331 struct nl_msg *msg;
332 struct nl_cb *cb = NULL;
333 int ret = -1;
334 int err = 0;
335 int finished = 0;
337 msg = nlmsg_alloc();
338 if (!msg)
339 goto out;
341 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
342 0, NL80211_CMD_GET_KEY, 0);
344 if (addr)
345 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
346 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
347 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
349 cb = nl_cb_clone(drv->nl_cb);
350 if (!cb)
351 goto out;
353 memset(seq, 0, 6);
355 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
356 goto out;
358 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, get_key_handler, seq);
359 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
361 err = nl_recvmsgs(drv->nl_handle, cb);
363 if (!finished)
364 err = nl_wait_for_ack(drv->nl_handle);
366 if (err < 0)
367 goto out;
369 ret = 0;
371 out:
372 nl_cb_put(cb);
373 nla_put_failure:
374 nlmsg_free(msg);
375 return ret;
379 static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
380 int mode)
382 return -1;
386 static int i802_set_ssid(const char *ifname, void *priv, const u8 *buf,
387 int len)
389 struct i802_driver_data *drv = priv;
390 struct iwreq iwr;
392 memset(&iwr, 0, sizeof(iwr));
393 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
394 iwr.u.essid.flags = 1; /* SSID active */
395 iwr.u.essid.pointer = (caddr_t) buf;
396 iwr.u.essid.length = len;
398 if (ioctl(drv->ioctl_sock, SIOCSIWESSID, &iwr) < 0) {
399 perror("ioctl[SIOCSIWESSID]");
400 printf("len=%d\n", len);
401 return -1;
404 return 0;
408 static int i802_send_mgmt_frame(void *priv, const void *data, size_t len,
409 int flags)
411 struct ieee80211_hdr *hdr = (void*) data;
412 __u8 rtap_hdr[] = {
413 0x00, 0x00, /* radiotap version */
414 0x0e, 0x00, /* radiotap length */
415 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
416 0x0c, /* F_WEP | F_FRAG (encrypt/fragment if required) */
417 0x00, /* padding */
418 0x00, 0x00, /* RX and TX flags to indicate that */
419 0x00, 0x00, /* this is the injected frame directly */
421 struct i802_driver_data *drv = priv;
422 struct iovec iov[2] = {
424 .iov_base = &rtap_hdr,
425 .iov_len = sizeof(rtap_hdr),
428 .iov_base = (void*)data,
429 .iov_len = len,
432 struct msghdr msg = {
433 .msg_name = NULL,
434 .msg_namelen = 0,
435 .msg_iov = iov,
436 .msg_iovlen = 2,
437 .msg_control = NULL,
438 .msg_controllen = 0,
439 .msg_flags = 0,
443 * ugh, guess what, the generic code sets one of the version
444 * bits to request tx callback
446 hdr->frame_control &= ~host_to_le16(BIT(1));
447 return sendmsg(drv->monitor_sock, &msg, flags);
451 /* Set kernel driver on given frequency (MHz) */
452 static int i802_set_freq(void *priv, int mode, int freq)
454 struct i802_driver_data *drv = priv;
455 struct iwreq iwr;
457 memset(&iwr, 0, sizeof(iwr));
458 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
459 iwr.u.freq.m = freq;
460 iwr.u.freq.e = 6;
462 if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
463 perror("ioctl[SIOCSIWFREQ]");
464 return -1;
467 return 0;
471 static int i802_set_rts(void *priv, int rts)
473 struct i802_driver_data *drv = priv;
474 struct iwreq iwr;
476 memset(&iwr, 0, sizeof(iwr));
477 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
478 iwr.u.rts.value = rts;
479 iwr.u.rts.fixed = 1;
481 if (ioctl(drv->ioctl_sock, SIOCSIWRTS, &iwr) < 0) {
482 perror("ioctl[SIOCSIWRTS]");
483 return -1;
486 return 0;
490 static int i802_get_rts(void *priv, int *rts)
492 struct i802_driver_data *drv = priv;
493 struct iwreq iwr;
495 memset(&iwr, 0, sizeof(iwr));
496 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
498 if (ioctl(drv->ioctl_sock, SIOCGIWRTS, &iwr) < 0) {
499 perror("ioctl[SIOCGIWRTS]");
500 return -1;
503 *rts = iwr.u.rts.value;
505 return 0;
509 static int i802_set_frag(void *priv, int frag)
511 struct i802_driver_data *drv = priv;
512 struct iwreq iwr;
514 memset(&iwr, 0, sizeof(iwr));
515 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
516 iwr.u.frag.value = frag;
517 iwr.u.frag.fixed = 1;
519 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
520 perror("ioctl[SIOCSIWFRAG]");
521 return -1;
524 return 0;
528 static int i802_get_frag(void *priv, int *frag)
530 struct i802_driver_data *drv = priv;
531 struct iwreq iwr;
533 memset(&iwr, 0, sizeof(iwr));
534 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
536 if (ioctl(drv->ioctl_sock, SIOCGIWFRAG, &iwr) < 0) {
537 perror("ioctl[SIOCGIWFRAG]");
538 return -1;
541 *frag = iwr.u.frag.value;
543 return 0;
547 static int i802_set_retry(void *priv, int short_retry, int long_retry)
549 struct i802_driver_data *drv = priv;
550 struct iwreq iwr;
552 memset(&iwr, 0, sizeof(iwr));
553 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
555 iwr.u.retry.value = short_retry;
556 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
557 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
558 perror("ioctl[SIOCSIWRETRY(short)]");
559 return -1;
562 iwr.u.retry.value = long_retry;
563 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
564 if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
565 perror("ioctl[SIOCSIWRETRY(long)]");
566 return -1;
569 return 0;
573 static int i802_get_retry(void *priv, int *short_retry, int *long_retry)
575 struct i802_driver_data *drv = priv;
576 struct iwreq iwr;
578 memset(&iwr, 0, sizeof(iwr));
579 os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
581 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
582 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
583 perror("ioctl[SIOCGIWFRAG(short)]");
584 return -1;
586 *short_retry = iwr.u.retry.value;
588 iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
589 if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
590 perror("ioctl[SIOCGIWFRAG(long)]");
591 return -1;
593 *long_retry = iwr.u.retry.value;
595 return 0;
599 static int i802_flush(void *priv)
601 struct i802_driver_data *drv = priv;
602 struct nl_msg *msg;
603 int ret = -1;
605 msg = nlmsg_alloc();
606 if (!msg)
607 goto out;
609 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
610 0, NL80211_CMD_DEL_STATION, 0);
613 * XXX: FIX! this needs to flush all VLANs too
615 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
616 if_nametoindex(drv->iface));
618 ret = 0;
620 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
621 nl_wait_for_ack(drv->nl_handle) < 0) {
622 ret = -1;
625 nla_put_failure:
626 nlmsg_free(msg);
628 out:
629 return ret;
633 static int get_sta_handler(struct nl_msg *msg, void *arg)
635 struct nlattr *tb[NL80211_ATTR_MAX + 1];
636 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
637 struct hostap_sta_driver_data *data = arg;
638 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
639 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
640 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
641 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
642 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
645 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
646 genlmsg_attrlen(gnlh, 0), NULL);
649 * TODO: validate the interface and mac address!
650 * Otherwise, there's a race condition as soon as
651 * the kernel starts sending station notifications.
654 if (!tb[NL80211_ATTR_STA_INFO]) {
655 wpa_printf(MSG_DEBUG, "sta stats missing!");
656 return NL_SKIP;
658 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
659 tb[NL80211_ATTR_STA_INFO],
660 stats_policy)) {
661 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
662 return NL_SKIP;
665 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
666 data->inactive_msec =
667 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
668 if (stats[NL80211_STA_INFO_RX_BYTES])
669 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
670 if (stats[NL80211_STA_INFO_TX_BYTES])
671 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
673 return NL_SKIP;
676 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
677 const u8 *addr)
679 struct i802_driver_data *drv = priv;
680 struct nl_msg *msg;
681 struct nl_cb *cb = NULL;
682 int ret = -1;
683 int err = 0;
684 int finished = 0;
686 msg = nlmsg_alloc();
687 if (!msg)
688 goto out;
690 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
691 0, NL80211_CMD_GET_STATION, 0);
693 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
694 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
696 cb = nl_cb_clone(drv->nl_cb);
697 if (!cb)
698 goto out;
700 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
701 goto out;
703 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, get_sta_handler, data);
704 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
706 err = nl_recvmsgs(drv->nl_handle, cb);
708 if (!finished)
709 err = nl_wait_for_ack(drv->nl_handle);
711 if (err < 0)
712 goto out;
714 ret = 0;
716 out:
717 nl_cb_put(cb);
718 nla_put_failure:
719 nlmsg_free(msg);
720 return ret;
725 static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
726 size_t data_len, int encrypt, const u8 *own_addr)
728 struct i802_driver_data *drv = priv;
729 struct ieee80211_hdr *hdr;
730 size_t len;
731 u8 *pos;
732 int res;
733 #if 0 /* FIX */
734 int qos = sta->flags & WLAN_STA_WME;
735 #else
736 int qos = 0;
737 #endif
739 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
740 data_len;
741 hdr = os_zalloc(len);
742 if (hdr == NULL) {
743 printf("malloc() failed for i802_send_data(len=%lu)\n",
744 (unsigned long) len);
745 return -1;
748 hdr->frame_control =
749 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
750 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
751 if (encrypt)
752 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
753 #if 0 /* To be enabled if qos determination is added above */
754 if (qos) {
755 hdr->frame_control |=
756 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
758 #endif
760 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
761 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
762 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
763 pos = (u8 *) (hdr + 1);
765 #if 0 /* To be enabled if qos determination is added above */
766 if (qos) {
767 /* add an empty QoS header if needed */
768 pos[0] = 0;
769 pos[1] = 0;
770 pos += 2;
772 #endif
774 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
775 pos += sizeof(rfc1042_header);
776 WPA_PUT_BE16(pos, ETH_P_PAE);
777 pos += 2;
778 memcpy(pos, data, data_len);
780 res = i802_send_mgmt_frame(drv, (u8 *) hdr, len, 0);
781 free(hdr);
783 if (res < 0) {
784 perror("i802_send_eapol: send");
785 printf("i802_send_eapol - packet len: %lu - failed\n",
786 (unsigned long) len);
789 return res;
793 static int i802_sta_add(const char *ifname, void *priv, const u8 *addr,
794 u16 aid, u16 capability, u8 *supp_rates,
795 size_t supp_rates_len, int flags, u16 listen_interval)
797 struct i802_driver_data *drv = priv;
798 struct nl_msg *msg;
799 int ret = -1;
801 msg = nlmsg_alloc();
802 if (!msg)
803 goto out;
805 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
806 0, NL80211_CMD_NEW_STATION, 0);
808 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
809 if_nametoindex(drv->iface));
810 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
811 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, aid);
812 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, supp_rates_len,
813 supp_rates);
814 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL, listen_interval);
816 ret = nl_send_auto_complete(drv->nl_handle, msg);
817 if (ret < 0)
818 goto nla_put_failure;
820 ret = nl_wait_for_ack(drv->nl_handle);
821 /* ignore EEXIST, this happens if a STA associates while associated */
822 if (ret == -EEXIST || ret >= 0)
823 ret = 0;
825 nla_put_failure:
826 nlmsg_free(msg);
828 out:
829 return ret;
833 static int i802_sta_remove(void *priv, const u8 *addr)
835 struct i802_driver_data *drv = priv;
836 struct nl_msg *msg;
837 int ret = -1;
839 msg = nlmsg_alloc();
840 if (!msg)
841 goto out;
843 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
844 0, NL80211_CMD_DEL_STATION, 0);
846 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
847 if_nametoindex(drv->iface));
848 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
850 ret = 0;
852 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
853 nl_wait_for_ack(drv->nl_handle) < 0) {
854 ret = -1;
857 nla_put_failure:
858 nlmsg_free(msg);
860 out:
861 return ret;
865 static int i802_sta_set_flags(void *priv, const u8 *addr,
866 int total_flags, int flags_or, int flags_and)
868 struct i802_driver_data *drv = priv;
869 struct nl_msg *msg, *flags = NULL;
870 int ret = -1;
872 msg = nlmsg_alloc();
873 if (!msg)
874 goto out;
876 flags = nlmsg_alloc();
877 if (!flags)
878 goto free_msg;
880 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
881 0, NL80211_CMD_SET_STATION, 0);
883 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
884 if_nametoindex(drv->iface));
885 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
887 if (total_flags & WLAN_STA_AUTHORIZED || !drv->ieee802_1x_active)
888 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
890 if (total_flags & WLAN_STA_WME)
891 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
893 if (total_flags & WLAN_STA_SHORT_PREAMBLE)
894 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
896 #ifdef NL80211_MFP_PENDING
897 if (total_flags & WLAN_STA_MFP)
898 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
899 #endif /* NL80211_MFP_PENDING */
901 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
902 goto nla_put_failure;
904 ret = 0;
906 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
907 nl_wait_for_ack(drv->nl_handle) < 0) {
908 ret = -1;
911 nla_put_failure:
912 nlmsg_free(flags);
914 free_msg:
915 nlmsg_free(msg);
917 out:
918 return ret;
922 static int i802_set_channel_flag(void *priv, int mode, int chan, int flag,
923 unsigned char power_level,
924 unsigned char antenna_max)
926 return -1;
930 static int i802_set_regulatory_domain(void *priv, unsigned int rd)
932 return -1;
936 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
937 int cw_min, int cw_max, int burst_time)
939 return -1;
943 static void nl80211_remove_iface(struct i802_driver_data *drv, int ifidx)
945 struct nl_msg *msg;
947 /* stop listening for EAPOL on this interface */
948 del_ifidx(drv, ifidx);
950 msg = nlmsg_alloc();
951 if (!msg)
952 goto nla_put_failure;
954 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
955 0, NL80211_CMD_DEL_INTERFACE, 0);
956 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
957 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
958 nl_wait_for_ack(drv->nl_handle) < 0)
959 nla_put_failure:
960 printf("Failed to remove interface.\n");
961 nlmsg_free(msg);
965 static int nl80211_create_iface(struct i802_driver_data *drv,
966 const char *ifname,
967 enum nl80211_iftype iftype,
968 const u8 *addr)
970 struct nl_msg *msg, *flags = NULL;
971 int ifidx;
972 struct ifreq ifreq;
973 struct iwreq iwr;
975 msg = nlmsg_alloc();
976 if (!msg)
977 return -1;
979 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
980 0, NL80211_CMD_NEW_INTERFACE, 0);
981 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
982 if_nametoindex(drv->hapd->conf->iface));
983 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
984 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
986 if (iftype == NL80211_IFTYPE_MONITOR) {
987 int err;
989 flags = nlmsg_alloc();
990 if (!flags)
991 goto nla_put_failure;
993 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
995 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
997 nlmsg_free(flags);
999 if (err)
1000 goto nla_put_failure;
1003 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1004 nl_wait_for_ack(drv->nl_handle) < 0) {
1005 nla_put_failure:
1006 printf("Failed to create interface %s.\n", ifname);
1007 nlmsg_free(msg);
1008 return -1;
1011 nlmsg_free(msg);
1013 ifidx = if_nametoindex(ifname);
1015 if (ifidx <= 0)
1016 return -1;
1018 /* start listening for EAPOL on this interface */
1019 add_ifidx(drv, ifidx);
1021 if (addr) {
1022 switch (iftype) {
1023 case NL80211_IFTYPE_AP:
1024 os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
1025 memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
1026 ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1028 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
1029 nl80211_remove_iface(drv, ifidx);
1030 return -1;
1032 break;
1033 case NL80211_IFTYPE_WDS:
1034 memset(&iwr, 0, sizeof(iwr));
1035 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1036 iwr.u.addr.sa_family = ARPHRD_ETHER;
1037 memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
1038 if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
1039 return -1;
1040 break;
1041 default:
1042 /* nothing */
1043 break;
1047 return ifidx;
1051 static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
1053 int ifidx;
1056 * The kernel supports that when the low-level driver does,
1057 * but we currently don't because we need per-BSS data that
1058 * currently we can't handle easily.
1060 return -1;
1062 ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
1063 if (ifidx < 0)
1064 return -1;
1065 if (hostapd_set_iface_flags(priv, ifname, 1)) {
1066 nl80211_remove_iface(priv, ifidx);
1067 return -1;
1069 return 0;
1073 static int i802_bss_remove(void *priv, const char *ifname)
1075 nl80211_remove_iface(priv, if_nametoindex(ifname));
1076 return 0;
1080 static int i802_set_beacon(const char *iface, void *priv,
1081 u8 *head, size_t head_len,
1082 u8 *tail, size_t tail_len)
1084 struct i802_driver_data *drv = priv;
1085 struct nl_msg *msg;
1086 u8 cmd = NL80211_CMD_NEW_BEACON;
1087 int ret = -1;
1089 msg = nlmsg_alloc();
1090 if (!msg)
1091 goto out;
1093 if (drv->beacon_set)
1094 cmd = NL80211_CMD_SET_BEACON;
1096 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1097 0, cmd, 0);
1098 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
1099 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
1100 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1101 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, drv->beacon_int);
1103 if (!drv->dtim_period)
1104 drv->dtim_period = 2;
1105 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1107 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1108 nl_wait_for_ack(drv->nl_handle) < 0)
1109 goto out;
1111 ret = 0;
1113 drv->beacon_set = 1;
1115 out:
1116 nla_put_failure:
1117 nlmsg_free(msg);
1118 return ret;
1122 static int i802_del_beacon(struct i802_driver_data *drv)
1124 struct nl_msg *msg;
1125 int ret = -1;
1127 msg = nlmsg_alloc();
1128 if (!msg)
1129 goto out;
1131 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1132 0, NL80211_CMD_DEL_BEACON, 0);
1133 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1135 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1136 nl_wait_for_ack(drv->nl_handle) < 0)
1137 goto out;
1139 ret = 0;
1141 out:
1142 nla_put_failure:
1143 nlmsg_free(msg);
1144 return ret;
1148 static int i802_set_ieee8021x(const char *ifname, void *priv, int enabled)
1150 struct i802_driver_data *drv = priv;
1153 * FIXME: This needs to be per interface (BSS)
1155 drv->ieee802_1x_active = enabled;
1156 return 0;
1160 static int i802_set_privacy(const char *ifname, void *priv, int enabled)
1162 struct i802_driver_data *drv = priv;
1163 struct iwreq iwr;
1165 memset(&iwr, 0, sizeof(iwr));
1167 os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
1168 iwr.u.param.flags = IW_AUTH_PRIVACY_INVOKED;
1169 iwr.u.param.value = enabled;
1171 ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr);
1173 /* ignore errors, the kernel/driver might not care */
1174 return 0;
1178 static int i802_set_internal_bridge(void *priv, int value)
1180 return -1;
1184 static int i802_set_beacon_int(void *priv, int value)
1186 struct i802_driver_data *drv = priv;
1187 struct nl_msg *msg;
1188 int ret = -1;
1190 drv->beacon_int = value;
1192 if (!drv->beacon_set)
1193 return 0;
1195 msg = nlmsg_alloc();
1196 if (!msg)
1197 goto out;
1199 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1200 0, NL80211_CMD_SET_BEACON, 0);
1201 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1203 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, value);
1205 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1206 nl_wait_for_ack(drv->nl_handle) < 0)
1207 goto out;
1209 ret = 0;
1211 out:
1212 nla_put_failure:
1213 nlmsg_free(msg);
1214 return ret;
1218 static int i802_set_dtim_period(const char *iface, void *priv, int value)
1220 struct i802_driver_data *drv = priv;
1221 struct nl_msg *msg;
1222 int ret = -1;
1224 msg = nlmsg_alloc();
1225 if (!msg)
1226 goto out;
1228 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1229 0, NL80211_CMD_SET_BEACON, 0);
1230 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
1232 drv->dtim_period = value;
1233 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, drv->dtim_period);
1235 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1236 nl_wait_for_ack(drv->nl_handle) < 0)
1237 goto out;
1239 ret = 0;
1241 out:
1242 nla_put_failure:
1243 nlmsg_free(msg);
1244 return ret;
1248 static int i802_set_bss(void *priv, int cts, int preamble, int slot)
1250 #ifdef NL80211_CMD_SET_BSS
1251 struct i802_driver_data *drv = priv;
1252 struct nl_msg *msg;
1253 int ret = -1;
1255 msg = nlmsg_alloc();
1256 if (!msg)
1257 goto out;
1259 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1260 NL80211_CMD_SET_BSS, 0);
1262 if (cts >= 0)
1263 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
1264 if (preamble >= 0)
1265 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
1266 if (slot >= 0)
1267 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
1269 ret = 0;
1271 /* TODO: multi-BSS support */
1272 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1274 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1275 nl_wait_for_ack(drv->nl_handle) < 0) {
1276 ret = -1;
1279 nla_put_failure:
1280 nlmsg_free(msg);
1282 out:
1283 return ret;
1284 #else /* NL80211_CMD_SET_BSS */
1285 return -1;
1286 #endif /* NL80211_CMD_SET_BSS */
1290 static int i802_set_cts_protect(void *priv, int value)
1292 return i802_set_bss(priv, value, -1, -1);
1296 static int i802_set_preamble(void *priv, int value)
1298 return i802_set_bss(priv, -1, value, -1);
1302 static int i802_set_short_slot_time(void *priv, int value)
1304 return i802_set_bss(priv, -1, -1, value);
1308 static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
1310 switch (type) {
1311 case HOSTAPD_IF_VLAN:
1312 return NL80211_IFTYPE_AP_VLAN;
1313 case HOSTAPD_IF_WDS:
1314 return NL80211_IFTYPE_WDS;
1316 return -1;
1320 static int i802_if_add(const char *iface, void *priv,
1321 enum hostapd_driver_if_type type, char *ifname,
1322 const u8 *addr)
1324 if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
1325 return -1;
1326 return 0;
1330 static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
1331 char *ifname, const u8 *addr)
1333 /* unused at the moment */
1334 return -1;
1338 static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
1339 const char *ifname, const u8 *addr)
1341 nl80211_remove_iface(priv, if_nametoindex(ifname));
1342 return 0;
1346 struct phy_info_arg {
1347 u16 *num_modes;
1348 struct hostapd_hw_modes *modes;
1349 int error;
1352 static int phy_info_handler(struct nl_msg *msg, void *arg)
1354 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1355 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1356 struct phy_info_arg *phy_info = arg;
1358 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
1360 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
1361 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
1362 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
1363 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
1364 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
1365 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
1366 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
1369 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
1370 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
1371 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
1372 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
1375 struct nlattr *nl_band;
1376 struct nlattr *nl_freq;
1377 struct nlattr *nl_rate;
1378 int rem_band, rem_freq, rem_rate;
1379 struct hostapd_hw_modes *mode;
1380 int idx, mode_is_set;
1382 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1383 genlmsg_attrlen(gnlh, 0), NULL);
1385 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
1386 return NL_SKIP;
1388 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
1389 mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
1390 if (!mode)
1391 return NL_SKIP;
1392 phy_info->modes = mode;
1394 mode_is_set = 0;
1396 mode = &phy_info->modes[*(phy_info->num_modes)];
1397 memset(mode, 0, sizeof(*mode));
1398 *(phy_info->num_modes) += 1;
1400 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
1401 nla_len(nl_band), NULL);
1403 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1404 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1405 nla_len(nl_freq), freq_policy);
1406 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1407 continue;
1408 mode->num_channels++;
1411 mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
1412 if (!mode->channels)
1413 return NL_SKIP;
1415 idx = 0;
1417 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
1418 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
1419 nla_len(nl_freq), freq_policy);
1420 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
1421 continue;
1423 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
1424 mode->channels[idx].flag |= HOSTAPD_CHAN_W_SCAN |
1425 HOSTAPD_CHAN_W_ACTIVE_SCAN |
1426 HOSTAPD_CHAN_W_IBSS;
1428 if (!mode_is_set) {
1429 /* crude heuristic */
1430 if (mode->channels[idx].freq < 4000)
1431 mode->mode = HOSTAPD_MODE_IEEE80211B;
1432 else
1433 mode->mode = HOSTAPD_MODE_IEEE80211A;
1434 mode_is_set = 1;
1437 /* crude heuristic */
1438 if (mode->channels[idx].freq < 4000)
1439 if (mode->channels[idx].freq == 2848)
1440 mode->channels[idx].chan = 14;
1441 else
1442 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
1443 else
1444 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
1446 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
1447 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_SCAN;
1448 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
1449 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_ACTIVE_SCAN;
1450 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
1451 mode->channels[idx].flag &= ~HOSTAPD_CHAN_W_IBSS;
1452 idx++;
1455 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1456 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1457 nla_len(nl_rate), rate_policy);
1458 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1459 continue;
1460 mode->num_rates++;
1463 mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
1464 if (!mode->rates)
1465 return NL_SKIP;
1467 idx = 0;
1469 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
1470 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
1471 nla_len(nl_rate), rate_policy);
1472 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
1473 continue;
1474 mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
1476 /* crude heuristic */
1477 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
1478 mode->rates[idx].rate > 200)
1479 mode->mode = HOSTAPD_MODE_IEEE80211G;
1481 if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
1482 mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
1484 idx++;
1488 phy_info->error = 0;
1490 return NL_SKIP;
1493 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
1494 u16 *num_modes,
1495 u16 *flags)
1497 struct i802_driver_data *drv = priv;
1498 struct nl_msg *msg;
1499 int err = -1;
1500 struct nl_cb *cb = NULL;
1501 int finished = 0;
1502 struct phy_info_arg result = {
1503 .num_modes = num_modes,
1504 .modes = NULL,
1505 .error = 1,
1508 *num_modes = 0;
1509 *flags = 0;
1511 msg = nlmsg_alloc();
1512 if (!msg)
1513 return NULL;
1515 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1516 0, NL80211_CMD_GET_WIPHY, 0);
1518 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
1520 cb = nl_cb_clone(drv->nl_cb);
1521 if (!cb)
1522 goto out;
1524 if (nl_send_auto_complete(drv->nl_handle, msg) < 0)
1525 goto out;
1527 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, phy_info_handler, &result);
1528 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, &finished);
1530 err = nl_recvmsgs(drv->nl_handle, cb);
1532 if (!finished)
1533 err = nl_wait_for_ack(drv->nl_handle);
1535 if (err < 0 || result.error) {
1536 hostapd_free_hw_features(result.modes, *num_modes);
1537 result.modes = NULL;
1540 out:
1541 nl_cb_put(cb);
1542 nla_put_failure:
1543 if (err)
1544 fprintf(stderr, "failed to get information: %d\n", err);
1545 nlmsg_free(msg);
1546 return result.modes;
1550 static int i802_set_sta_vlan(void *priv, const u8 *addr,
1551 const char *ifname, int vlan_id)
1553 struct i802_driver_data *drv = priv;
1554 struct nl_msg *msg;
1555 int ret = -1;
1557 msg = nlmsg_alloc();
1558 if (!msg)
1559 goto out;
1561 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1562 0, NL80211_CMD_SET_STATION, 0);
1564 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1565 if_nametoindex(drv->iface));
1566 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1567 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1568 if_nametoindex(ifname));
1570 ret = 0;
1572 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1573 (errno = nl_wait_for_ack(drv->nl_handle) < 0)) {
1574 ret = -1;
1577 nla_put_failure:
1578 nlmsg_free(msg);
1580 out:
1581 return ret;
1585 static void handle_unknown_sta(struct hostapd_data *hapd, u8 *ta)
1587 struct sta_info *sta;
1589 sta = ap_get_sta(hapd, ta);
1590 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
1591 printf("Data/PS-poll frame from not associated STA "
1592 MACSTR "\n", MAC2STR(ta));
1593 if (sta && (sta->flags & WLAN_STA_AUTH))
1594 hostapd_sta_disassoc(
1595 hapd, ta,
1596 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1597 else
1598 hostapd_sta_deauth(
1599 hapd, ta,
1600 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1605 static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
1606 int ok)
1608 struct ieee80211_hdr *hdr;
1609 u16 fc, type, stype;
1610 struct sta_info *sta;
1612 hdr = (struct ieee80211_hdr *) buf;
1613 fc = le_to_host16(hdr->frame_control);
1615 type = WLAN_FC_GET_TYPE(fc);
1616 stype = WLAN_FC_GET_STYPE(fc);
1618 switch (type) {
1619 case WLAN_FC_TYPE_MGMT:
1620 wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
1621 ok ? "ACK" : "fail");
1622 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
1623 break;
1624 case WLAN_FC_TYPE_CTRL:
1625 wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
1626 ok ? "ACK" : "fail");
1627 break;
1628 case WLAN_FC_TYPE_DATA:
1629 wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
1630 ok ? "ACK" : "fail");
1631 sta = ap_get_sta(hapd, hdr->addr1);
1632 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
1633 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1634 "activity poll", MAC2STR(sta->addr),
1635 ok ? "ACKed" : "did not ACK");
1636 if (ok)
1637 sta->flags &= ~WLAN_STA_PENDING_POLL;
1639 if (sta)
1640 ieee802_1x_tx_status(hapd, sta, buf, len, ok);
1641 break;
1642 default:
1643 printf("unknown TX callback frame type %d\n", type);
1644 break;
1649 static void handle_frame(struct hostapd_iface *iface, u8 *buf, size_t len,
1650 struct hostapd_frame_info *hfi,
1651 enum ieee80211_msg_type msg_type)
1653 struct ieee80211_hdr *hdr;
1654 u16 fc, type, stype;
1655 size_t data_len = len;
1656 struct hostapd_data *hapd = NULL;
1657 int broadcast_bssid = 0;
1658 size_t i;
1659 u8 *bssid;
1662 * PS-Poll frames are 16 bytes. All other frames are
1663 * 24 bytes or longer.
1665 if (len < 16)
1666 return;
1668 hdr = (struct ieee80211_hdr *) buf;
1669 fc = le_to_host16(hdr->frame_control);
1671 type = WLAN_FC_GET_TYPE(fc);
1672 stype = WLAN_FC_GET_STYPE(fc);
1674 switch (type) {
1675 case WLAN_FC_TYPE_DATA:
1676 if (len < 24)
1677 return;
1678 switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
1679 case WLAN_FC_TODS:
1680 bssid = hdr->addr1;
1681 break;
1682 default:
1683 /* discard */
1684 return;
1686 break;
1687 case WLAN_FC_TYPE_CTRL:
1688 /* discard non-ps-poll frames */
1689 if (stype != WLAN_FC_STYPE_PSPOLL)
1690 return;
1691 bssid = hdr->addr1;
1692 break;
1693 case WLAN_FC_TYPE_MGMT:
1694 bssid = hdr->addr3;
1695 break;
1696 default:
1697 /* discard */
1698 return;
1701 /* find interface frame belongs to */
1702 for (i = 0; i < iface->num_bss; i++) {
1703 if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
1704 hapd = iface->bss[i];
1705 break;
1709 if (hapd == NULL) {
1710 hapd = iface->bss[0];
1712 if (bssid[0] != 0xff || bssid[1] != 0xff ||
1713 bssid[2] != 0xff || bssid[3] != 0xff ||
1714 bssid[4] != 0xff || bssid[5] != 0xff) {
1716 * Unknown BSSID - drop frame if this is not from
1717 * passive scanning or a beacon (at least ProbeReq
1718 * frames to other APs may be allowed through RX
1719 * filtering in the wlan hw/driver)
1721 if ((type != WLAN_FC_TYPE_MGMT ||
1722 stype != WLAN_FC_STYPE_BEACON))
1723 return;
1724 } else
1725 broadcast_bssid = 1;
1728 switch (msg_type) {
1729 case ieee80211_msg_normal:
1730 /* continue processing */
1731 break;
1732 case ieee80211_msg_tx_callback_ack:
1733 handle_tx_callback(hapd, buf, data_len, 1);
1734 return;
1735 case ieee80211_msg_tx_callback_fail:
1736 handle_tx_callback(hapd, buf, data_len, 0);
1737 return;
1740 switch (type) {
1741 case WLAN_FC_TYPE_MGMT:
1742 if (stype != WLAN_FC_STYPE_BEACON &&
1743 stype != WLAN_FC_STYPE_PROBE_REQ)
1744 wpa_printf(MSG_MSGDUMP, "MGMT");
1745 if (broadcast_bssid) {
1746 for (i = 0; i < iface->num_bss; i++)
1747 ieee802_11_mgmt(iface->bss[i], buf, data_len,
1748 stype, hfi);
1749 } else
1750 ieee802_11_mgmt(hapd, buf, data_len, stype, hfi);
1751 break;
1752 case WLAN_FC_TYPE_CTRL:
1753 /* can only get here with PS-Poll frames */
1754 wpa_printf(MSG_DEBUG, "CTRL");
1755 handle_unknown_sta(hapd, hdr->addr2);
1756 break;
1757 case WLAN_FC_TYPE_DATA:
1758 wpa_printf(MSG_DEBUG, "DATA");
1759 handle_unknown_sta(hapd, hdr->addr2);
1760 break;
1765 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
1767 struct i802_driver_data *drv = eloop_ctx;
1768 struct hostapd_data *hapd = drv->hapd;
1769 struct sockaddr_ll lladdr;
1770 unsigned char buf[3000];
1771 int len;
1772 socklen_t fromlen = sizeof(lladdr);
1774 len = recvfrom(sock, buf, sizeof(buf), 0,
1775 (struct sockaddr *)&lladdr, &fromlen);
1776 if (len < 0) {
1777 perror("recv");
1778 return;
1781 if (have_ifidx(drv, lladdr.sll_ifindex))
1782 ieee802_1x_receive(hapd, lladdr.sll_addr, buf, len);
1786 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
1788 struct i802_driver_data *drv = eloop_ctx;
1789 int len;
1790 unsigned char buf[3000];
1791 struct hostapd_data *hapd = drv->hapd;
1792 struct ieee80211_radiotap_iterator iter;
1793 int ret;
1794 struct hostapd_frame_info hfi;
1795 int injected = 0, failed = 0, msg_type, rxflags = 0;
1797 len = recv(sock, buf, sizeof(buf), 0);
1798 if (len < 0) {
1799 perror("recv");
1800 return;
1803 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
1804 printf("received invalid radiotap frame\n");
1805 return;
1808 memset(&hfi, 0, sizeof(hfi));
1810 while (1) {
1811 ret = ieee80211_radiotap_iterator_next(&iter);
1812 if (ret == -ENOENT)
1813 break;
1814 if (ret) {
1815 printf("received invalid radiotap frame (%d)\n", ret);
1816 return;
1818 switch (iter.this_arg_index) {
1819 case IEEE80211_RADIOTAP_FLAGS:
1820 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
1821 len -= 4;
1822 break;
1823 case IEEE80211_RADIOTAP_RX_FLAGS:
1824 rxflags = 1;
1825 break;
1826 case IEEE80211_RADIOTAP_TX_FLAGS:
1827 injected = 1;
1828 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
1829 IEEE80211_RADIOTAP_F_TX_FAIL;
1830 break;
1831 case IEEE80211_RADIOTAP_DATA_RETRIES:
1832 break;
1833 case IEEE80211_RADIOTAP_CHANNEL:
1834 /* TODO convert from freq/flags to channel number
1835 hfi.channel = XXX;
1836 hfi.phytype = XXX;
1838 break;
1839 case IEEE80211_RADIOTAP_RATE:
1840 hfi.datarate = *iter.this_arg * 5;
1841 break;
1842 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
1843 hfi.ssi_signal = *iter.this_arg;
1844 break;
1848 if (rxflags && injected)
1849 return;
1851 if (!injected)
1852 msg_type = ieee80211_msg_normal;
1853 else if (failed)
1854 msg_type = ieee80211_msg_tx_callback_fail;
1855 else
1856 msg_type = ieee80211_msg_tx_callback_ack;
1858 handle_frame(hapd->iface, buf + iter.max_length,
1859 len - iter.max_length, &hfi, msg_type);
1863 static int nl80211_create_monitor_interface(struct i802_driver_data *drv)
1865 char buf[IFNAMSIZ];
1866 struct sockaddr_ll ll;
1867 int optval;
1868 socklen_t optlen;
1870 snprintf(buf, IFNAMSIZ, "mon.%s", drv->iface);
1871 buf[IFNAMSIZ - 1] = '\0';
1873 drv->monitor_ifidx =
1874 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
1876 if (drv->monitor_ifidx < 0)
1877 return -1;
1879 if (hostapd_set_iface_flags(drv, buf, 1))
1880 goto error;
1882 memset(&ll, 0, sizeof(ll));
1883 ll.sll_family = AF_PACKET;
1884 ll.sll_ifindex = drv->monitor_ifidx;
1885 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
1886 if (drv->monitor_sock < 0) {
1887 perror("socket[PF_PACKET,SOCK_RAW]");
1888 goto error;
1891 if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
1892 sizeof(ll)) < 0) {
1893 perror("monitor socket bind");
1894 goto error;
1897 optlen = sizeof(optval);
1898 optval = 20;
1899 if (setsockopt
1900 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
1901 perror("Failed to set socket priority");
1902 goto error;
1905 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
1906 drv, NULL)) {
1907 printf("Could not register monitor read socket\n");
1908 goto error;
1911 return 0;
1912 error:
1913 nl80211_remove_iface(drv, drv->monitor_ifidx);
1914 return -1;
1918 static int nl80211_set_master_mode(struct i802_driver_data *drv,
1919 const char *ifname)
1921 struct nl_msg *msg;
1923 msg = nlmsg_alloc();
1924 if (!msg)
1925 return -1;
1927 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1928 0, NL80211_CMD_SET_INTERFACE, 0);
1929 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
1930 if_nametoindex(ifname));
1931 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, NL80211_IFTYPE_AP);
1933 if (nl_send_auto_complete(drv->nl_handle, msg) < 0 ||
1934 nl_wait_for_ack(drv->nl_handle) < 0) {
1935 nla_put_failure:
1936 wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
1937 "mode.", ifname);
1938 nlmsg_free(msg);
1939 return -1;
1942 nlmsg_free(msg);
1944 return 0;
1948 static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
1950 struct ifreq ifr;
1951 struct sockaddr_ll addr;
1953 drv->ioctl_sock = -1;
1955 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1956 if (drv->ioctl_sock < 0) {
1957 perror("socket[PF_INET,SOCK_DGRAM]");
1958 return -1;
1961 /* start listening for EAPOL on the default AP interface */
1962 add_ifidx(drv, if_nametoindex(drv->iface));
1964 if (hostapd_set_iface_flags(drv, drv->iface, 0))
1965 return -1;
1967 if (bssid) {
1968 os_strlcpy(ifr.ifr_name, drv->iface, IFNAMSIZ);
1969 memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
1970 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1972 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
1973 perror("ioctl(SIOCSIFHWADDR)");
1974 return -1;
1979 * initialise generic netlink and nl80211
1981 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1982 if (!drv->nl_cb) {
1983 printf("Failed to allocate netlink callbacks.\n");
1984 return -1;
1987 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
1988 if (!drv->nl_handle) {
1989 printf("Failed to allocate netlink handle.\n");
1990 return -1;
1993 if (genl_connect(drv->nl_handle)) {
1994 printf("Failed to connect to generic netlink.\n");
1995 return -1;
1998 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
1999 if (!drv->nl_cache) {
2000 printf("Failed to allocate generic netlink cache.\n");
2001 return -1;
2004 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
2005 if (!drv->nl80211) {
2006 printf("nl80211 not found.\n");
2007 return -1;
2010 /* Initialise a monitor interface */
2011 if (nl80211_create_monitor_interface(drv))
2012 return -1;
2014 if (nl80211_set_master_mode(drv, drv->iface))
2015 return -1;
2017 if (hostapd_set_iface_flags(drv, drv->iface, 1))
2018 return -1;
2020 memset(&addr, 0, sizeof(addr));
2021 addr.sll_family = AF_PACKET;
2022 addr.sll_ifindex = ifr.ifr_ifindex;
2023 wpa_printf(MSG_DEBUG, "Opening raw packet socket for ifindex %d",
2024 addr.sll_ifindex);
2026 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
2027 if (drv->eapol_sock < 0) {
2028 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
2029 return -1;
2032 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
2034 printf("Could not register read socket for eapol\n");
2035 return -1;
2038 memset(&ifr, 0, sizeof(ifr));
2039 os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
2040 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
2041 perror("ioctl(SIOCGIFHWADDR)");
2042 return -1;
2045 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
2046 printf("Invalid HW-addr family 0x%04x\n",
2047 ifr.ifr_hwaddr.sa_family);
2048 return -1;
2050 memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
2052 return 0;
2056 static int i802_get_inact_sec(void *priv, const u8 *addr)
2058 struct hostap_sta_driver_data data;
2059 int ret;
2061 data.inactive_msec = (unsigned long) -1;
2062 ret = i802_read_sta_data(priv, &data, addr);
2063 if (ret || data.inactive_msec == (unsigned long) -1)
2064 return -1;
2065 return data.inactive_msec / 1000;
2069 static int i802_sta_clear_stats(void *priv, const u8 *addr)
2071 #if 0
2072 /* TODO */
2073 #endif
2074 return 0;
2078 static void
2079 hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
2080 char *custom)
2082 wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
2084 if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
2085 char *pos;
2086 u8 addr[ETH_ALEN];
2087 pos = strstr(custom, "addr=");
2088 if (pos == NULL) {
2089 wpa_printf(MSG_DEBUG,
2090 "MLME-MICHAELMICFAILURE.indication "
2091 "without sender address ignored");
2092 return;
2094 pos += 5;
2095 if (hwaddr_aton(pos, addr) == 0) {
2096 ieee80211_michael_mic_failure(drv->hapd, addr, 1);
2097 } else {
2098 wpa_printf(MSG_DEBUG,
2099 "MLME-MICHAELMICFAILURE.indication "
2100 "with invalid MAC address");
2106 static void hostapd_wireless_event_wireless(struct i802_driver_data *drv,
2107 char *data, int len)
2109 struct iw_event iwe_buf, *iwe = &iwe_buf;
2110 char *pos, *end, *custom, *buf;
2112 pos = data;
2113 end = data + len;
2115 while (pos + IW_EV_LCP_LEN <= end) {
2116 /* Event data may be unaligned, so make a local, aligned copy
2117 * before processing. */
2118 memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
2119 wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
2120 iwe->cmd, iwe->len);
2121 if (iwe->len <= IW_EV_LCP_LEN)
2122 return;
2124 custom = pos + IW_EV_POINT_LEN;
2125 if (drv->we_version > 18 &&
2126 (iwe->cmd == IWEVMICHAELMICFAILURE ||
2127 iwe->cmd == IWEVCUSTOM)) {
2128 /* WE-19 removed the pointer from struct iw_point */
2129 char *dpos = (char *) &iwe_buf.u.data.length;
2130 int dlen = dpos - (char *) &iwe_buf;
2131 memcpy(dpos, pos + IW_EV_LCP_LEN,
2132 sizeof(struct iw_event) - dlen);
2133 } else {
2134 memcpy(&iwe_buf, pos, sizeof(struct iw_event));
2135 custom += IW_EV_POINT_OFF;
2138 switch (iwe->cmd) {
2139 case IWEVCUSTOM:
2140 if (custom + iwe->u.data.length > end)
2141 return;
2142 buf = malloc(iwe->u.data.length + 1);
2143 if (buf == NULL)
2144 return;
2145 memcpy(buf, custom, iwe->u.data.length);
2146 buf[iwe->u.data.length] = '\0';
2147 hostapd_wireless_event_wireless_custom(drv, buf);
2148 free(buf);
2149 break;
2152 pos += iwe->len;
2157 static void hostapd_wireless_event_rtm_newlink(struct i802_driver_data *drv,
2158 struct nlmsghdr *h, int len)
2160 struct ifinfomsg *ifi;
2161 int attrlen, nlmsg_len, rta_len;
2162 struct rtattr *attr;
2164 if (len < (int) sizeof(*ifi))
2165 return;
2167 ifi = NLMSG_DATA(h);
2169 /* TODO: use ifi->ifi_index to filter out wireless events from other
2170 * interfaces */
2172 nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
2174 attrlen = h->nlmsg_len - nlmsg_len;
2175 if (attrlen < 0)
2176 return;
2178 attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
2180 rta_len = RTA_ALIGN(sizeof(struct rtattr));
2181 while (RTA_OK(attr, attrlen)) {
2182 if (attr->rta_type == IFLA_WIRELESS) {
2183 hostapd_wireless_event_wireless(
2184 drv, ((char *) attr) + rta_len,
2185 attr->rta_len - rta_len);
2187 attr = RTA_NEXT(attr, attrlen);
2192 static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
2193 void *sock_ctx)
2195 char buf[256];
2196 int left;
2197 struct sockaddr_nl from;
2198 socklen_t fromlen;
2199 struct nlmsghdr *h;
2200 struct i802_driver_data *drv = eloop_ctx;
2202 fromlen = sizeof(from);
2203 left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
2204 (struct sockaddr *) &from, &fromlen);
2205 if (left < 0) {
2206 if (errno != EINTR && errno != EAGAIN)
2207 perror("recvfrom(netlink)");
2208 return;
2211 h = (struct nlmsghdr *) buf;
2212 while (left >= (int) sizeof(*h)) {
2213 int len, plen;
2215 len = h->nlmsg_len;
2216 plen = len - sizeof(*h);
2217 if (len > left || plen < 0) {
2218 printf("Malformed netlink message: "
2219 "len=%d left=%d plen=%d\n",
2220 len, left, plen);
2221 break;
2224 switch (h->nlmsg_type) {
2225 case RTM_NEWLINK:
2226 hostapd_wireless_event_rtm_newlink(drv, h, plen);
2227 break;
2230 len = NLMSG_ALIGN(len);
2231 left -= len;
2232 h = (struct nlmsghdr *) ((char *) h + len);
2235 if (left > 0) {
2236 printf("%d extra bytes in the end of netlink message\n", left);
2241 static int hostap_get_we_version(struct i802_driver_data *drv)
2243 struct iw_range *range;
2244 struct iwreq iwr;
2245 int minlen;
2246 size_t buflen;
2248 drv->we_version = 0;
2251 * Use larger buffer than struct iw_range in order to allow the
2252 * structure to grow in the future.
2254 buflen = sizeof(struct iw_range) + 500;
2255 range = os_zalloc(buflen);
2256 if (range == NULL)
2257 return -1;
2259 memset(&iwr, 0, sizeof(iwr));
2260 os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
2261 iwr.u.data.pointer = (caddr_t) range;
2262 iwr.u.data.length = buflen;
2264 minlen = ((char *) &range->enc_capa) - (char *) range +
2265 sizeof(range->enc_capa);
2267 if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
2268 perror("ioctl[SIOCGIWRANGE]");
2269 free(range);
2270 return -1;
2271 } else if (iwr.u.data.length >= minlen &&
2272 range->we_version_compiled >= 18) {
2273 wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
2274 "WE(source)=%d enc_capa=0x%x",
2275 range->we_version_compiled,
2276 range->we_version_source,
2277 range->enc_capa);
2278 drv->we_version = range->we_version_compiled;
2281 free(range);
2282 return 0;
2286 static int i802_wireless_event_init(void *priv)
2288 struct i802_driver_data *drv = priv;
2289 int s;
2290 struct sockaddr_nl local;
2292 hostap_get_we_version(drv);
2294 drv->wext_sock = -1;
2296 s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
2297 if (s < 0) {
2298 perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
2299 return -1;
2302 memset(&local, 0, sizeof(local));
2303 local.nl_family = AF_NETLINK;
2304 local.nl_groups = RTMGRP_LINK;
2305 if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
2306 perror("bind(netlink)");
2307 close(s);
2308 return -1;
2311 eloop_register_read_sock(s, hostapd_wireless_event_receive, drv,
2312 NULL);
2313 drv->wext_sock = s;
2315 return 0;
2319 static void i802_wireless_event_deinit(void *priv)
2321 struct i802_driver_data *drv = priv;
2322 if (drv->wext_sock < 0)
2323 return;
2324 eloop_unregister_read_sock(drv->wext_sock);
2325 close(drv->wext_sock);
2329 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
2331 struct i802_driver_data *drv = priv;
2332 struct ieee80211_mgmt mgmt;
2334 memset(&mgmt, 0, sizeof(mgmt));
2335 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2336 WLAN_FC_STYPE_DEAUTH);
2337 memcpy(mgmt.da, addr, ETH_ALEN);
2338 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2339 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2340 mgmt.u.deauth.reason_code = host_to_le16(reason);
2341 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2342 sizeof(mgmt.u.deauth), 0);
2346 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
2348 struct i802_driver_data *drv = priv;
2349 struct ieee80211_mgmt mgmt;
2351 memset(&mgmt, 0, sizeof(mgmt));
2352 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
2353 WLAN_FC_STYPE_DISASSOC);
2354 memcpy(mgmt.da, addr, ETH_ALEN);
2355 memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
2356 memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
2357 mgmt.u.disassoc.reason_code = host_to_le16(reason);
2358 return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
2359 sizeof(mgmt.u.disassoc), 0);
2363 static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
2365 struct i802_driver_data *drv;
2367 drv = os_zalloc(sizeof(struct i802_driver_data));
2368 if (drv == NULL) {
2369 printf("Could not allocate memory for i802 driver data\n");
2370 return NULL;
2373 drv->hapd = hapd;
2374 memcpy(drv->iface, hapd->conf->iface, sizeof(drv->iface));
2376 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
2377 drv->if_indices = drv->default_if_indices;
2378 drv->bridge = if_nametoindex(hapd->conf->bridge);
2380 if (i802_init_sockets(drv, bssid))
2381 goto failed;
2383 return drv;
2385 failed:
2386 free(drv);
2387 return NULL;
2391 static void *i802_init(struct hostapd_data *hapd)
2393 return i802_init_bssid(hapd, NULL);
2397 static void i802_deinit(void *priv)
2399 struct i802_driver_data *drv = priv;
2401 i802_del_beacon(drv);
2403 /* remove monitor interface */
2404 nl80211_remove_iface(drv, drv->monitor_ifidx);
2406 (void) hostapd_set_iface_flags(drv, drv->iface, 0);
2408 if (drv->monitor_sock >= 0) {
2409 eloop_unregister_read_sock(drv->monitor_sock);
2410 close(drv->monitor_sock);
2412 if (drv->ioctl_sock >= 0)
2413 close(drv->ioctl_sock);
2414 if (drv->eapol_sock >= 0) {
2415 eloop_unregister_read_sock(drv->eapol_sock);
2416 close(drv->eapol_sock);
2419 genl_family_put(drv->nl80211);
2420 nl_cache_free(drv->nl_cache);
2421 nl_handle_destroy(drv->nl_handle);
2422 nl_cb_put(drv->nl_cb);
2424 if (drv->if_indices != drv->default_if_indices)
2425 free(drv->if_indices);
2427 free(drv);
2431 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
2432 .name = "nl80211",
2433 .init = i802_init,
2434 .init_bssid = i802_init_bssid,
2435 .deinit = i802_deinit,
2436 .wireless_event_init = i802_wireless_event_init,
2437 .wireless_event_deinit = i802_wireless_event_deinit,
2438 .set_ieee8021x = i802_set_ieee8021x,
2439 .set_privacy = i802_set_privacy,
2440 .set_encryption = i802_set_encryption,
2441 .get_seqnum = i802_get_seqnum,
2442 .flush = i802_flush,
2443 .read_sta_data = i802_read_sta_data,
2444 .send_eapol = i802_send_eapol,
2445 .sta_set_flags = i802_sta_set_flags,
2446 .sta_deauth = i802_sta_deauth,
2447 .sta_disassoc = i802_sta_disassoc,
2448 .sta_remove = i802_sta_remove,
2449 .set_ssid = i802_set_ssid,
2450 .send_mgmt_frame = i802_send_mgmt_frame,
2451 .sta_add = i802_sta_add,
2452 .get_inact_sec = i802_get_inact_sec,
2453 .sta_clear_stats = i802_sta_clear_stats,
2454 .set_freq = i802_set_freq,
2455 .set_rts = i802_set_rts,
2456 .get_rts = i802_get_rts,
2457 .set_frag = i802_set_frag,
2458 .get_frag = i802_get_frag,
2459 .set_retry = i802_set_retry,
2460 .get_retry = i802_get_retry,
2461 .set_rate_sets = i802_set_rate_sets,
2462 .set_channel_flag = i802_set_channel_flag,
2463 .set_regulatory_domain = i802_set_regulatory_domain,
2464 .set_beacon = i802_set_beacon,
2465 .set_internal_bridge = i802_set_internal_bridge,
2466 .set_beacon_int = i802_set_beacon_int,
2467 .set_dtim_period = i802_set_dtim_period,
2468 .set_cts_protect = i802_set_cts_protect,
2469 .set_preamble = i802_set_preamble,
2470 .set_short_slot_time = i802_set_short_slot_time,
2471 .set_tx_queue_params = i802_set_tx_queue_params,
2472 .bss_add = i802_bss_add,
2473 .bss_remove = i802_bss_remove,
2474 .if_add = i802_if_add,
2475 .if_update = i802_if_update,
2476 .if_remove = i802_if_remove,
2477 .get_hw_feature_data = i802_get_hw_feature_data,
2478 .set_sta_vlan = i802_set_sta_vlan,