1 /*
2 * Driver interaction with Linux nl80211/cfg80211
3 * Copyright (c) 2002-2012, 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>
7 * Copyright (c) 2009-2010, Atheros Communications
8 *
9 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
11 */
13 #include "includes.h"
14 #include <sys/ioctl.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <net/if.h>
19 #include <netlink/genl/genl.h>
20 #include <netlink/genl/family.h>
21 #include <netlink/genl/ctrl.h>
22 #include <linux/rtnetlink.h>
23 #include <netpacket/packet.h>
24 #include <linux/filter.h>
25 #include <linux/errqueue.h>
26 #include "nl80211_copy.h"
28 #include "common.h"
29 #include "eloop.h"
30 #include "utils/list.h"
31 #include "common/ieee802_11_defs.h"
32 #include "common/ieee802_11_common.h"
33 #include "l2_packet/l2_packet.h"
34 #include "netlink.h"
35 #include "linux_ioctl.h"
36 #include "radiotap.h"
37 #include "radiotap_iter.h"
38 #include "rfkill.h"
39 #include "driver.h"
41 #ifndef SO_WIFI_STATUS
42 # if defined(__sparc__)
43 # define SO_WIFI_STATUS 0x0025
44 # elif defined(__parisc__)
45 # define SO_WIFI_STATUS 0x4022
46 # else
47 # define SO_WIFI_STATUS 41
48 # endif
50 # define SCM_WIFI_STATUS SO_WIFI_STATUS
51 #endif
53 #ifndef SO_EE_ORIGIN_TXSTATUS
54 #define SO_EE_ORIGIN_TXSTATUS 4
55 #endif
57 #ifndef PACKET_TX_TIMESTAMP
58 #define PACKET_TX_TIMESTAMP 16
59 #endif
61 #ifdef ANDROID
62 #include "android_drv.h"
64 static int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
65 size_t buf_len);
66 #endif /* ANDROID */
67 #ifdef CONFIG_LIBNL20
68 /* libnl 2.0 compatibility code */
69 #define nl_handle nl_sock
70 #define nl80211_handle_alloc nl_socket_alloc_cb
71 #define nl80211_handle_destroy nl_socket_free
72 #else
73 /*
74 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
75 * but when you free a socket again it will mess up its bitmap and
76 * and use the wrong number the next time it needs a socket ID.
77 * Therefore, we wrap the handle alloc/destroy and add our own pid
78 * accounting.
79 */
80 static uint32_t port_bitmap[32] = { 0 };
82 static struct nl_handle *nl80211_handle_alloc(void *cb)
83 {
84 struct nl_handle *handle;
85 uint32_t pid = getpid() & 0x3FFFFF;
86 int i;
88 handle = nl_handle_alloc_cb(cb);
90 for (i = 0; i < 1024; i++) {
91 if (port_bitmap[i / 32] & (1 << (i % 32)))
92 continue;
93 port_bitmap[i / 32] |= 1 << (i % 32);
94 pid += i << 22;
95 break;
96 }
98 nl_socket_set_local_port(handle, pid);
100 return handle;
101 }
103 static void nl80211_handle_destroy(struct nl_handle *handle)
104 {
105 uint32_t port = nl_socket_get_local_port(handle);
107 port >>= 22;
108 port_bitmap[port / 32] &= ~(1 << (port % 32));
110 nl_handle_destroy(handle);
111 }
112 #endif /* CONFIG_LIBNL20 */
115 static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
116 {
117 struct nl_handle *handle;
119 handle = nl80211_handle_alloc(cb);
120 if (handle == NULL) {
121 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
122 "callbacks (%s)", dbg);
123 return NULL;
124 }
126 if (genl_connect(handle)) {
127 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
128 "netlink (%s)", dbg);
129 nl80211_handle_destroy(handle);
130 return NULL;
131 }
133 return handle;
134 }
137 static void nl_destroy_handles(struct nl_handle **handle)
138 {
139 if (*handle == NULL)
140 return;
141 nl80211_handle_destroy(*handle);
142 *handle = NULL;
143 }
146 #ifndef IFF_LOWER_UP
147 #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
148 #endif
149 #ifndef IFF_DORMANT
150 #define IFF_DORMANT 0x20000 /* driver signals dormant */
151 #endif
153 #ifndef IF_OPER_DORMANT
154 #define IF_OPER_DORMANT 5
155 #endif
156 #ifndef IF_OPER_UP
157 #define IF_OPER_UP 6
158 #endif
160 struct nl80211_global {
161 struct dl_list interfaces;
162 int if_add_ifindex;
163 struct netlink_data *netlink;
164 struct nl_cb *nl_cb;
165 struct nl_handle *nl;
166 int nl80211_id;
167 int ioctl_sock; /* socket for ioctl() use */
169 struct nl_handle *nl_event;
170 };
172 struct nl80211_wiphy_data {
173 struct dl_list list;
174 struct dl_list bsss;
175 struct dl_list drvs;
177 struct nl_handle *nl_beacons;
178 struct nl_cb *nl_cb;
180 int wiphy_idx;
181 };
183 static void nl80211_global_deinit(void *priv);
184 static void wpa_driver_nl80211_deinit(void *priv);
186 struct i802_bss {
187 struct wpa_driver_nl80211_data *drv;
188 struct i802_bss *next;
189 int ifindex;
190 char ifname[IFNAMSIZ + 1];
191 char brname[IFNAMSIZ];
192 unsigned int beacon_set:1;
193 unsigned int added_if_into_bridge:1;
194 unsigned int added_bridge:1;
196 u8 addr[ETH_ALEN];
198 int freq;
200 struct nl_handle *nl_preq, *nl_mgmt;
201 struct nl_cb *nl_cb;
203 struct nl80211_wiphy_data *wiphy_data;
204 struct dl_list wiphy_list;
205 };
207 struct wpa_driver_nl80211_data {
208 struct nl80211_global *global;
209 struct dl_list list;
210 struct dl_list wiphy_list;
211 char phyname[32];
212 void *ctx;
213 int ifindex;
214 int if_removed;
215 int if_disabled;
216 int ignore_if_down_event;
217 struct rfkill_data *rfkill;
218 struct wpa_driver_capa capa;
219 int has_capability;
221 int operstate;
223 int scan_complete_events;
225 struct nl_cb *nl_cb;
227 u8 auth_bssid[ETH_ALEN];
228 u8 bssid[ETH_ALEN];
229 int associated;
230 u8 ssid[32];
231 size_t ssid_len;
232 enum nl80211_iftype nlmode;
233 enum nl80211_iftype ap_scan_as_station;
234 unsigned int assoc_freq;
236 int monitor_sock;
237 int monitor_ifidx;
238 int monitor_refcount;
240 unsigned int disabled_11b_rates:1;
241 unsigned int pending_remain_on_chan:1;
242 unsigned int in_interface_list:1;
243 unsigned int device_ap_sme:1;
244 unsigned int poll_command_supported:1;
245 unsigned int data_tx_status:1;
246 unsigned int scan_for_auth:1;
247 unsigned int retry_auth:1;
248 unsigned int use_monitor:1;
250 u64 remain_on_chan_cookie;
251 u64 send_action_cookie;
253 unsigned int last_mgmt_freq;
255 struct wpa_driver_scan_filter *filter_ssids;
256 size_t num_filter_ssids;
258 struct i802_bss first_bss;
260 int eapol_tx_sock;
262 #ifdef HOSTAPD
263 int eapol_sock; /* socket for EAPOL frames */
265 int default_if_indices[16];
266 int *if_indices;
267 int num_if_indices;
269 int last_freq;
270 int last_freq_ht;
271 #endif /* HOSTAPD */
273 /* From failed authentication command */
274 int auth_freq;
275 u8 auth_bssid_[ETH_ALEN];
276 u8 auth_ssid[32];
277 size_t auth_ssid_len;
278 int auth_alg;
279 u8 *auth_ie;
280 size_t auth_ie_len;
281 u8 auth_wep_key[4][16];
282 size_t auth_wep_key_len[4];
283 int auth_wep_tx_keyidx;
284 int auth_local_state_change;
285 int auth_p2p;
286 #ifdef ANDROID
287 u8 wowlan_triggers;
288 u8 wowlan_enabled;
289 #endif
290 };
293 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
294 void *timeout_ctx);
295 static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
296 enum nl80211_iftype nlmode);
297 static int
298 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
299 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
300 const u8 *addr, int cmd, u16 reason_code,
301 int local_state_change);
302 static void nl80211_remove_monitor_interface(
303 struct wpa_driver_nl80211_data *drv);
304 static int nl80211_send_frame_cmd(struct i802_bss *bss,
305 unsigned int freq, unsigned int wait,
306 const u8 *buf, size_t buf_len, u64 *cookie,
307 int no_cck, int no_ack, int offchanok);
308 static int wpa_driver_nl80211_probe_req_report(void *priv, int report);
309 #ifdef ANDROID
310 static int android_pno_start(struct i802_bss *bss,
311 struct wpa_driver_scan_params *params);
312 static int android_pno_stop(struct i802_bss *bss);
313 #endif /* ANDROID */
314 #ifdef ANDROID_BRCM_P2P_PATCH
315 static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
316 enum wpa_event_type type,
317 const u8 *frame, size_t len);
318 #endif /* ANDROID_BRCM_P2P_PATCH */
320 #ifdef HOSTAPD
321 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
322 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
323 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
324 static int wpa_driver_nl80211_if_remove(void *priv,
325 enum wpa_driver_if_type type,
326 const char *ifname);
327 #else /* HOSTAPD */
328 static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
329 {
330 }
332 static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
333 {
334 }
336 static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
337 {
338 return 0;
339 }
340 #endif /* HOSTAPD */
342 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
343 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
344 int ifindex, int disabled);
346 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
347 static int wpa_driver_nl80211_authenticate_retry(
348 struct wpa_driver_nl80211_data *drv);
351 static int is_ap_interface(enum nl80211_iftype nlmode)
352 {
353 return (nlmode == NL80211_IFTYPE_AP ||
354 nlmode == NL80211_IFTYPE_P2P_GO);
355 }
358 static int is_sta_interface(enum nl80211_iftype nlmode)
359 {
360 return (nlmode == NL80211_IFTYPE_STATION ||
361 nlmode == NL80211_IFTYPE_P2P_CLIENT);
362 }
365 static int is_p2p_interface(enum nl80211_iftype nlmode)
366 {
367 return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
368 nlmode == NL80211_IFTYPE_P2P_GO);
369 }
372 struct nl80211_bss_info_arg {
373 struct wpa_driver_nl80211_data *drv;
374 struct wpa_scan_results *res;
375 unsigned int assoc_freq;
376 u8 assoc_bssid[ETH_ALEN];
377 };
379 static int bss_info_handler(struct nl_msg *msg, void *arg);
382 /* nl80211 code */
383 static int ack_handler(struct nl_msg *msg, void *arg)
384 {
385 int *err = arg;
386 *err = 0;
387 return NL_STOP;
388 }
390 static int finish_handler(struct nl_msg *msg, void *arg)
391 {
392 int *ret = arg;
393 *ret = 0;
394 return NL_SKIP;
395 }
397 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
398 void *arg)
399 {
400 int *ret = arg;
401 *ret = err->error;
402 return NL_SKIP;
403 }
406 static int no_seq_check(struct nl_msg *msg, void *arg)
407 {
408 return NL_OK;
409 }
412 static int send_and_recv(struct nl80211_global *global,
413 struct nl_handle *nl_handle, struct nl_msg *msg,
414 int (*valid_handler)(struct nl_msg *, void *),
415 void *valid_data)
416 {
417 struct nl_cb *cb;
418 int err = -ENOMEM;
420 cb = nl_cb_clone(global->nl_cb);
421 if (!cb)
422 goto out;
424 err = nl_send_auto_complete(nl_handle, msg);
425 if (err < 0)
426 goto out;
428 err = 1;
430 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
431 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
432 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
434 if (valid_handler)
435 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
436 valid_handler, valid_data);
438 while (err > 0)
439 nl_recvmsgs(nl_handle, cb);
440 out:
441 nl_cb_put(cb);
442 nlmsg_free(msg);
443 return err;
444 }
447 static int send_and_recv_msgs_global(struct nl80211_global *global,
448 struct nl_msg *msg,
449 int (*valid_handler)(struct nl_msg *, void *),
450 void *valid_data)
451 {
452 return send_and_recv(global, global->nl, msg, valid_handler,
453 valid_data);
454 }
457 static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
458 struct nl_msg *msg,
459 int (*valid_handler)(struct nl_msg *, void *),
460 void *valid_data)
461 {
462 return send_and_recv(drv->global, drv->global->nl, msg,
463 valid_handler, valid_data);
464 }
467 struct family_data {
468 const char *group;
469 int id;
470 };
473 static int family_handler(struct nl_msg *msg, void *arg)
474 {
475 struct family_data *res = arg;
476 struct nlattr *tb[CTRL_ATTR_MAX + 1];
477 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
478 struct nlattr *mcgrp;
479 int i;
481 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
482 genlmsg_attrlen(gnlh, 0), NULL);
483 if (!tb[CTRL_ATTR_MCAST_GROUPS])
484 return NL_SKIP;
486 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
487 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
488 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
489 nla_len(mcgrp), NULL);
490 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
491 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
492 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
493 res->group,
494 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
495 continue;
496 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
497 break;
498 };
500 return NL_SKIP;
501 }
504 static int nl_get_multicast_id(struct nl80211_global *global,
505 const char *family, const char *group)
506 {
507 struct nl_msg *msg;
508 int ret = -1;
509 struct family_data res = { group, -ENOENT };
511 msg = nlmsg_alloc();
512 if (!msg)
513 return -ENOMEM;
514 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
515 0, 0, CTRL_CMD_GETFAMILY, 0);
516 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
518 ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
519 msg = NULL;
520 if (ret == 0)
521 ret = res.id;
523 nla_put_failure:
524 nlmsg_free(msg);
525 return ret;
526 }
529 static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
530 struct nl_msg *msg, int flags, uint8_t cmd)
531 {
532 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
533 0, flags, cmd, 0);
534 }
537 struct wiphy_idx_data {
538 int wiphy_idx;
539 };
542 static int netdev_info_handler(struct nl_msg *msg, void *arg)
543 {
544 struct nlattr *tb[NL80211_ATTR_MAX + 1];
545 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
546 struct wiphy_idx_data *info = arg;
548 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
549 genlmsg_attrlen(gnlh, 0), NULL);
551 if (tb[NL80211_ATTR_WIPHY])
552 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
554 return NL_SKIP;
555 }
558 static int nl80211_get_wiphy_index(struct i802_bss *bss)
559 {
560 struct nl_msg *msg;
561 struct wiphy_idx_data data = {
562 .wiphy_idx = -1,
563 };
565 msg = nlmsg_alloc();
566 if (!msg)
567 return -1;
569 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
571 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
573 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
574 return data.wiphy_idx;
575 msg = NULL;
576 nla_put_failure:
577 nlmsg_free(msg);
578 return -1;
579 }
582 static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
583 struct nl80211_wiphy_data *w)
584 {
585 struct nl_msg *msg;
586 int ret = -1;
588 msg = nlmsg_alloc();
589 if (!msg)
590 return -1;
592 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
594 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
596 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
597 msg = NULL;
598 if (ret) {
599 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
600 "failed: ret=%d (%s)",
601 ret, strerror(-ret));
602 goto nla_put_failure;
603 }
604 ret = 0;
605 nla_put_failure:
606 nlmsg_free(msg);
607 return ret;
608 }
611 static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
612 {
613 struct nl80211_wiphy_data *w = eloop_ctx;
615 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
617 nl_recvmsgs(handle, w->nl_cb);
618 }
621 static int process_beacon_event(struct nl_msg *msg, void *arg)
622 {
623 struct nl80211_wiphy_data *w = arg;
624 struct wpa_driver_nl80211_data *drv;
625 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
626 struct nlattr *tb[NL80211_ATTR_MAX + 1];
627 union wpa_event_data event;
629 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
630 genlmsg_attrlen(gnlh, 0), NULL);
632 if (gnlh->cmd != NL80211_CMD_FRAME) {
633 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
634 gnlh->cmd);
635 return NL_SKIP;
636 }
638 if (!tb[NL80211_ATTR_FRAME])
639 return NL_SKIP;
641 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
642 wiphy_list) {
643 os_memset(&event, 0, sizeof(event));
644 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
645 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
646 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
647 }
649 return NL_SKIP;
650 }
653 static struct nl80211_wiphy_data *
654 nl80211_get_wiphy_data_ap(struct i802_bss *bss)
655 {
656 static DEFINE_DL_LIST(nl80211_wiphys);
657 struct nl80211_wiphy_data *w;
658 int wiphy_idx, found = 0;
659 struct i802_bss *tmp_bss;
661 if (bss->wiphy_data != NULL)
662 return bss->wiphy_data;
664 wiphy_idx = nl80211_get_wiphy_index(bss);
666 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
667 if (w->wiphy_idx == wiphy_idx)
668 goto add;
669 }
671 /* alloc new one */
672 w = os_zalloc(sizeof(*w));
673 if (w == NULL)
674 return NULL;
675 w->wiphy_idx = wiphy_idx;
676 dl_list_init(&w->bsss);
677 dl_list_init(&w->drvs);
679 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
680 if (!w->nl_cb) {
681 os_free(w);
682 return NULL;
683 }
684 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
685 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
686 w);
688 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
689 "wiphy beacons");
690 if (w->nl_beacons == NULL) {
691 os_free(w);
692 return NULL;
693 }
695 if (nl80211_register_beacons(bss->drv, w)) {
696 nl_destroy_handles(&w->nl_beacons);
697 os_free(w);
698 return NULL;
699 }
701 eloop_register_read_sock(nl_socket_get_fd(w->nl_beacons),
702 nl80211_recv_beacons, w, w->nl_beacons);
704 dl_list_add(&nl80211_wiphys, &w->list);
706 add:
707 /* drv entry for this bss already there? */
708 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
709 if (tmp_bss->drv == bss->drv) {
710 found = 1;
711 break;
712 }
713 }
714 /* if not add it */
715 if (!found)
716 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
718 dl_list_add(&w->bsss, &bss->wiphy_list);
719 bss->wiphy_data = w;
720 return w;
721 }
724 static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
725 {
726 struct nl80211_wiphy_data *w = bss->wiphy_data;
727 struct i802_bss *tmp_bss;
728 int found = 0;
730 if (w == NULL)
731 return;
732 bss->wiphy_data = NULL;
733 dl_list_del(&bss->wiphy_list);
735 /* still any for this drv present? */
736 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
737 if (tmp_bss->drv == bss->drv) {
738 found = 1;
739 break;
740 }
741 }
742 /* if not remove it */
743 if (!found)
744 dl_list_del(&bss->drv->wiphy_list);
746 if (!dl_list_empty(&w->bsss))
747 return;
749 eloop_unregister_read_sock(nl_socket_get_fd(w->nl_beacons));
751 nl_cb_put(w->nl_cb);
752 nl_destroy_handles(&w->nl_beacons);
753 dl_list_del(&w->list);
754 os_free(w);
755 }
758 static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
759 {
760 struct i802_bss *bss = priv;
761 struct wpa_driver_nl80211_data *drv = bss->drv;
762 if (!drv->associated)
763 return -1;
764 os_memcpy(bssid, drv->bssid, ETH_ALEN);
765 return 0;
766 }
769 static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
770 {
771 struct i802_bss *bss = priv;
772 struct wpa_driver_nl80211_data *drv = bss->drv;
773 if (!drv->associated)
774 return -1;
775 os_memcpy(ssid, drv->ssid, drv->ssid_len);
776 return drv->ssid_len;
777 }
780 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
781 char *buf, size_t len, int del)
782 {
783 union wpa_event_data event;
785 os_memset(&event, 0, sizeof(event));
786 if (len > sizeof(event.interface_status.ifname))
787 len = sizeof(event.interface_status.ifname) - 1;
788 os_memcpy(event.interface_status.ifname, buf, len);
789 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
790 EVENT_INTERFACE_ADDED;
792 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
793 del ? "DEL" : "NEW",
794 event.interface_status.ifname,
795 del ? "removed" : "added");
797 if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
798 if (del) {
799 if (drv->if_removed) {
800 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
801 "already set - ignore event");
802 return;
803 }
804 drv->if_removed = 1;
805 } else {
806 if (if_nametoindex(drv->first_bss.ifname) == 0) {
807 wpa_printf(MSG_DEBUG, "nl80211: Interface %s "
808 "does not exist - ignore "
809 "RTM_NEWLINK",
810 drv->first_bss.ifname);
811 return;
812 }
813 if (!drv->if_removed) {
814 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
815 "already cleared - ignore event");
816 return;
817 }
818 drv->if_removed = 0;
819 }
820 }
822 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
823 }
826 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
827 u8 *buf, size_t len)
828 {
829 int attrlen, rta_len;
830 struct rtattr *attr;
832 attrlen = len;
833 attr = (struct rtattr *) buf;
835 rta_len = RTA_ALIGN(sizeof(struct rtattr));
836 while (RTA_OK(attr, attrlen)) {
837 if (attr->rta_type == IFLA_IFNAME) {
838 if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
839 == 0)
840 return 1;
841 else
842 break;
843 }
844 attr = RTA_NEXT(attr, attrlen);
845 }
847 return 0;
848 }
851 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
852 int ifindex, u8 *buf, size_t len)
853 {
854 if (drv->ifindex == ifindex)
855 return 1;
857 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
858 drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
859 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
860 "interface");
861 wpa_driver_nl80211_finish_drv_init(drv);
862 return 1;
863 }
865 return 0;
866 }
869 static struct wpa_driver_nl80211_data *
870 nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
871 {
872 struct wpa_driver_nl80211_data *drv;
873 dl_list_for_each(drv, &global->interfaces,
874 struct wpa_driver_nl80211_data, list) {
875 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
876 have_ifidx(drv, idx))
877 return drv;
878 }
879 return NULL;
880 }
883 static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
884 struct ifinfomsg *ifi,
885 u8 *buf, size_t len)
886 {
887 struct nl80211_global *global = ctx;
888 struct wpa_driver_nl80211_data *drv;
889 int attrlen, rta_len;
890 struct rtattr *attr;
891 u32 brid = 0;
892 char namebuf[IFNAMSIZ];
894 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
895 if (!drv) {
896 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
897 "ifindex %d", ifi->ifi_index);
898 return;
899 }
901 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
902 "(%s%s%s%s)",
903 drv->operstate, ifi->ifi_flags,
904 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
905 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
906 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
907 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
909 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
910 if (if_indextoname(ifi->ifi_index, namebuf) &&
911 linux_iface_up(drv->global->ioctl_sock,
912 drv->first_bss.ifname) > 0) {
913 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
914 "event since interface %s is up", namebuf);
915 return;
916 }
917 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
918 if (drv->ignore_if_down_event) {
919 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
920 "event generated by mode change");
921 drv->ignore_if_down_event = 0;
922 } else {
923 drv->if_disabled = 1;
924 wpa_supplicant_event(drv->ctx,
925 EVENT_INTERFACE_DISABLED, NULL);
926 }
927 }
929 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
930 if (if_indextoname(ifi->ifi_index, namebuf) &&
931 linux_iface_up(drv->global->ioctl_sock,
932 drv->first_bss.ifname) == 0) {
933 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
934 "event since interface %s is down",
935 namebuf);
936 } else if (if_nametoindex(drv->first_bss.ifname) == 0) {
937 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
938 "event since interface %s does not exist",
939 drv->first_bss.ifname);
940 } else if (drv->if_removed) {
941 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
942 "event since interface %s is marked "
943 "removed", drv->first_bss.ifname);
944 } else {
945 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
946 drv->if_disabled = 0;
947 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
948 NULL);
949 }
950 }
952 /*
953 * Some drivers send the association event before the operup event--in
954 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
955 * fails. This will hit us when wpa_supplicant does not need to do
956 * IEEE 802.1X authentication
957 */
958 if (drv->operstate == 1 &&
959 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
960 !(ifi->ifi_flags & IFF_RUNNING))
961 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
962 -1, IF_OPER_UP);
964 attrlen = len;
965 attr = (struct rtattr *) buf;
966 rta_len = RTA_ALIGN(sizeof(struct rtattr));
967 while (RTA_OK(attr, attrlen)) {
968 if (attr->rta_type == IFLA_IFNAME) {
969 wpa_driver_nl80211_event_link(
970 drv,
971 ((char *) attr) + rta_len,
972 attr->rta_len - rta_len, 0);
973 } else if (attr->rta_type == IFLA_MASTER)
974 brid = nla_get_u32((struct nlattr *) attr);
975 attr = RTA_NEXT(attr, attrlen);
976 }
978 if (ifi->ifi_family == AF_BRIDGE && brid) {
979 /* device has been added to bridge */
980 if_indextoname(brid, namebuf);
981 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
982 brid, namebuf);
983 add_ifidx(drv, brid);
984 }
985 }
988 static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
989 struct ifinfomsg *ifi,
990 u8 *buf, size_t len)
991 {
992 struct nl80211_global *global = ctx;
993 struct wpa_driver_nl80211_data *drv;
994 int attrlen, rta_len;
995 struct rtattr *attr;
996 u32 brid = 0;
998 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
999 if (!drv) {
1000 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
1001 "foreign ifindex %d", ifi->ifi_index);
1002 return;
1003 }
1005 attrlen = len;
1006 attr = (struct rtattr *) buf;
1008 rta_len = RTA_ALIGN(sizeof(struct rtattr));
1009 while (RTA_OK(attr, attrlen)) {
1010 if (attr->rta_type == IFLA_IFNAME) {
1011 wpa_driver_nl80211_event_link(
1012 drv,
1013 ((char *) attr) + rta_len,
1014 attr->rta_len - rta_len, 1);
1015 } else if (attr->rta_type == IFLA_MASTER)
1016 brid = nla_get_u32((struct nlattr *) attr);
1017 attr = RTA_NEXT(attr, attrlen);
1018 }
1020 if (ifi->ifi_family == AF_BRIDGE && brid) {
1021 /* device has been removed from bridge */
1022 char namebuf[IFNAMSIZ];
1023 if_indextoname(brid, namebuf);
1024 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1025 "%s", brid, namebuf);
1026 del_ifidx(drv, brid);
1027 }
1028 }
1031 static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1032 const u8 *frame, size_t len)
1033 {
1034 const struct ieee80211_mgmt *mgmt;
1035 union wpa_event_data event;
1037 mgmt = (const struct ieee80211_mgmt *) frame;
1038 if (len < 24 + sizeof(mgmt->u.auth)) {
1039 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1040 "frame");
1041 return;
1042 }
1044 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
1045 os_memset(&event, 0, sizeof(event));
1046 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1047 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
1048 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1049 if (len > 24 + sizeof(mgmt->u.auth)) {
1050 event.auth.ies = mgmt->u.auth.variable;
1051 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1052 }
1054 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1055 }
1058 static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1059 {
1060 struct nl_msg *msg;
1061 int ret;
1062 struct nl80211_bss_info_arg arg;
1064 os_memset(&arg, 0, sizeof(arg));
1065 msg = nlmsg_alloc();
1066 if (!msg)
1067 goto nla_put_failure;
1069 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1070 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1072 arg.drv = drv;
1073 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1074 msg = NULL;
1075 if (ret == 0) {
1076 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1077 "associated BSS from scan results: %u MHz",
1078 arg.assoc_freq);
1079 return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq;
1080 }
1081 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1082 "(%s)", ret, strerror(-ret));
1083 nla_put_failure:
1084 nlmsg_free(msg);
1085 return drv->assoc_freq;
1086 }
1089 static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1090 const u8 *frame, size_t len)
1091 {
1092 const struct ieee80211_mgmt *mgmt;
1093 union wpa_event_data event;
1094 u16 status;
1096 mgmt = (const struct ieee80211_mgmt *) frame;
1097 #if (defined (CONFIG_AP) || defined (HOSTAPD) ) && defined (ANDROID_BRCM_P2P_PATCH)
1098 if (drv->nlmode == NL80211_IFTYPE_AP || drv->nlmode == NL80211_IFTYPE_P2P_GO) {
1099 if (len < 24 + sizeof(mgmt->u.assoc_req)) {
1100 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1101 "frame");
1102 return;
1103 }
1104 os_memset(&event, 0, sizeof(event));
1105 event.assoc_info.freq = drv->assoc_freq;
1106 event.assoc_info.req_ies = (u8 *) mgmt->u.assoc_req.variable;
1107 event.assoc_info.req_ies_len = len - 24 - sizeof(mgmt->u.assoc_req);
1108 event.assoc_info.addr = mgmt->sa;
1109 } else {
1110 #endif
1111 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1112 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1113 "frame");
1114 return;
1115 }
1117 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1118 if (status != WLAN_STATUS_SUCCESS) {
1119 os_memset(&event, 0, sizeof(event));
1120 event.assoc_reject.bssid = mgmt->bssid;
1121 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1122 event.assoc_reject.resp_ies =
1123 (u8 *) mgmt->u.assoc_resp.variable;
1124 event.assoc_reject.resp_ies_len =
1125 len - 24 - sizeof(mgmt->u.assoc_resp);
1126 }
1127 event.assoc_reject.status_code = status;
1129 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1130 return;
1131 }
1133 drv->associated = 1;
1134 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
1136 os_memset(&event, 0, sizeof(event));
1137 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1138 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1139 event.assoc_info.resp_ies_len =
1140 len - 24 - sizeof(mgmt->u.assoc_resp);
1141 }
1143 event.assoc_info.freq = drv->assoc_freq;
1144 #if (defined (CONFIG_AP) || defined(HOSTAPD)) && defined (ANDROID_BRCM_P2P_PATCH)
1145 }
1146 #endif
1148 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1149 }
1152 static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1153 enum nl80211_commands cmd, struct nlattr *status,
1154 struct nlattr *addr, struct nlattr *req_ie,
1155 struct nlattr *resp_ie)
1156 {
1157 union wpa_event_data event;
1159 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1160 /*
1161 * Avoid reporting two association events that would confuse
1162 * the core code.
1163 */
1164 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1165 "when using userspace SME", cmd);
1166 return;
1167 }
1169 os_memset(&event, 0, sizeof(event));
1170 if (cmd == NL80211_CMD_CONNECT &&
1171 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1172 if (addr)
1173 event.assoc_reject.bssid = nla_data(addr);
1174 if (resp_ie) {
1175 event.assoc_reject.resp_ies = nla_data(resp_ie);
1176 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1177 }
1178 event.assoc_reject.status_code = nla_get_u16(status);
1179 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1180 return;
1181 }
1183 drv->associated = 1;
1184 if (addr)
1185 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
1187 if (req_ie) {
1188 event.assoc_info.req_ies = nla_data(req_ie);
1189 event.assoc_info.req_ies_len = nla_len(req_ie);
1190 }
1191 if (resp_ie) {
1192 event.assoc_info.resp_ies = nla_data(resp_ie);
1193 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1194 }
1196 event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1198 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1199 }
1202 static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
1203 struct nlattr *reason, struct nlattr *addr,
1204 struct nlattr *by_ap)
1205 {
1206 union wpa_event_data data;
1208 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1209 /*
1210 * Avoid reporting two disassociation events that could
1211 * confuse the core code.
1212 */
1213 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1214 "event when using userspace SME");
1215 return;
1216 }
1218 drv->associated = 0;
1219 os_memset(&data, 0, sizeof(data));
1220 if (reason)
1221 data.disassoc_info.reason_code = nla_get_u16(reason);
1222 data.disassoc_info.locally_generated = by_ap == NULL;
1223 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, &data);
1224 }
1227 static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1228 enum nl80211_commands cmd, struct nlattr *addr)
1229 {
1230 union wpa_event_data event;
1231 enum wpa_event_type ev;
1233 if (nla_len(addr) != ETH_ALEN)
1234 return;
1236 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1237 cmd, MAC2STR((u8 *) nla_data(addr)));
1239 if (cmd == NL80211_CMD_AUTHENTICATE)
1240 ev = EVENT_AUTH_TIMED_OUT;
1241 else if (cmd == NL80211_CMD_ASSOCIATE)
1242 ev = EVENT_ASSOC_TIMED_OUT;
1243 else
1244 return;
1246 os_memset(&event, 0, sizeof(event));
1247 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1248 wpa_supplicant_event(drv->ctx, ev, &event);
1249 }
1252 static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
1253 struct nlattr *freq, const u8 *frame, size_t len)
1254 {
1255 const struct ieee80211_mgmt *mgmt;
1256 union wpa_event_data event;
1257 u16 fc, stype;
1259 mgmt = (const struct ieee80211_mgmt *) frame;
1260 if (len < 24) {
1261 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
1262 return;
1263 }
1265 fc = le_to_host16(mgmt->frame_control);
1266 stype = WLAN_FC_GET_STYPE(fc);
1268 os_memset(&event, 0, sizeof(event));
1269 if (freq) {
1270 event.rx_action.freq = nla_get_u32(freq);
1271 drv->last_mgmt_freq = event.rx_action.freq;
1272 }
1273 if (stype == WLAN_FC_STYPE_ACTION) {
1274 event.rx_action.da = mgmt->da;
1275 event.rx_action.sa = mgmt->sa;
1276 event.rx_action.bssid = mgmt->bssid;
1277 event.rx_action.category = mgmt->u.action.category;
1278 event.rx_action.data = &mgmt->u.action.category + 1;
1279 event.rx_action.len = frame + len - event.rx_action.data;
1280 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
1281 #ifdef ANDROID_BRCM_P2P_PATCH
1282 } else if (stype == WLAN_FC_STYPE_ASSOC_REQ) {
1283 mlme_event_assoc(drv, frame, len);
1284 } else if (stype == WLAN_FC_STYPE_DISASSOC) {
1285 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC, frame, len);
1286 } else if (stype == WLAN_FC_STYPE_DEAUTH) {
1287 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH, frame, len);
1288 #endif /* ANDROID_BRCM_P2P_PATCH */
1289 } else {
1290 event.rx_mgmt.frame = frame;
1291 event.rx_mgmt.frame_len = len;
1292 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
1293 }
1294 }
1297 static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1298 struct nlattr *cookie, const u8 *frame,
1299 size_t len, struct nlattr *ack)
1300 {
1301 union wpa_event_data event;
1302 const struct ieee80211_hdr *hdr;
1303 u16 fc;
1305 if (!is_ap_interface(drv->nlmode)) {
1306 u64 cookie_val;
1308 if (!cookie)
1309 return;
1311 cookie_val = nla_get_u64(cookie);
1312 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1313 " cookie=0%llx%s (ack=%d)",
1314 (long long unsigned int) cookie_val,
1315 cookie_val == drv->send_action_cookie ?
1316 " (match)" : " (unknown)", ack != NULL);
1317 if (cookie_val != drv->send_action_cookie)
1318 return;
1319 }
1321 hdr = (const struct ieee80211_hdr *) frame;
1322 fc = le_to_host16(hdr->frame_control);
1324 os_memset(&event, 0, sizeof(event));
1325 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1326 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1327 event.tx_status.dst = hdr->addr1;
1328 event.tx_status.data = frame;
1329 event.tx_status.data_len = len;
1330 event.tx_status.ack = ack != NULL;
1331 wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1332 }
1335 static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1336 enum wpa_event_type type,
1337 const u8 *frame, size_t len)
1338 {
1339 const struct ieee80211_mgmt *mgmt;
1340 union wpa_event_data event;
1341 const u8 *bssid = NULL;
1342 u16 reason_code = 0;
1344 mgmt = (const struct ieee80211_mgmt *) frame;
1345 if (len >= 24) {
1346 bssid = mgmt->bssid;
1348 if (drv->associated != 0 &&
1349 os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1350 os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1351 /*
1352 * We have presumably received this deauth as a
1353 * response to a clear_state_mismatch() outgoing
1354 * deauth. Don't let it take us offline!
1355 */
1356 wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1357 "from Unknown BSSID " MACSTR " -- ignoring",
1358 MAC2STR(bssid));
1359 return;
1360 }
1361 }
1363 drv->associated = 0;
1364 os_memset(&event, 0, sizeof(event));
1366 /* Note: Same offset for Reason Code in both frame subtypes */
1367 if (len >= 24 + sizeof(mgmt->u.deauth))
1368 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1370 if (type == EVENT_DISASSOC) {
1371 event.disassoc_info.locally_generated =
1372 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
1373 #ifdef ANDROID_BRCM_P2P_PATCH
1374 if (is_ap_interface(drv->nlmode)) {
1375 event.disassoc_info.addr = mgmt->sa;
1376 } else
1377 #endif /* ANDROID_BRCM_P2P_PATCH */
1378 event.disassoc_info.addr = bssid;
1379 event.disassoc_info.reason_code = reason_code;
1380 if (frame + len > mgmt->u.disassoc.variable) {
1381 event.disassoc_info.ie = mgmt->u.disassoc.variable;
1382 event.disassoc_info.ie_len = frame + len -
1383 mgmt->u.disassoc.variable;
1384 }
1385 } else {
1386 event.deauth_info.locally_generated =
1387 !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
1388 #ifdef ANDROID_BRCM_P2P_PATCH
1389 if (is_ap_interface(drv->nlmode)) {
1390 event.deauth_info.addr = mgmt->sa;
1391 } else
1392 #endif /* ANDROID_BRCM_P2P_PATCH */
1393 event.deauth_info.addr = bssid;
1394 event.deauth_info.reason_code = reason_code;
1395 if (frame + len > mgmt->u.deauth.variable) {
1396 event.deauth_info.ie = mgmt->u.deauth.variable;
1397 event.deauth_info.ie_len = frame + len -
1398 mgmt->u.deauth.variable;
1399 }
1400 }
1402 wpa_supplicant_event(drv->ctx, type, &event);
1403 }
1406 static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1407 enum wpa_event_type type,
1408 const u8 *frame, size_t len)
1409 {
1410 const struct ieee80211_mgmt *mgmt;
1411 union wpa_event_data event;
1412 u16 reason_code = 0;
1414 if (len < 24)
1415 return;
1417 mgmt = (const struct ieee80211_mgmt *) frame;
1419 os_memset(&event, 0, sizeof(event));
1420 /* Note: Same offset for Reason Code in both frame subtypes */
1421 if (len >= 24 + sizeof(mgmt->u.deauth))
1422 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1424 if (type == EVENT_UNPROT_DISASSOC) {
1425 event.unprot_disassoc.sa = mgmt->sa;
1426 event.unprot_disassoc.da = mgmt->da;
1427 event.unprot_disassoc.reason_code = reason_code;
1428 } else {
1429 event.unprot_deauth.sa = mgmt->sa;
1430 event.unprot_deauth.da = mgmt->da;
1431 event.unprot_deauth.reason_code = reason_code;
1432 }
1434 wpa_supplicant_event(drv->ctx, type, &event);
1435 }
1438 static void mlme_event(struct wpa_driver_nl80211_data *drv,
1439 enum nl80211_commands cmd, struct nlattr *frame,
1440 struct nlattr *addr, struct nlattr *timed_out,
1441 struct nlattr *freq, struct nlattr *ack,
1442 struct nlattr *cookie)
1443 {
1444 if (timed_out && addr) {
1445 mlme_timeout_event(drv, cmd, addr);
1446 return;
1447 }
1449 if (frame == NULL) {
1450 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
1451 "data", cmd);
1452 return;
1453 }
1455 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
1456 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1457 nla_data(frame), nla_len(frame));
1459 switch (cmd) {
1460 case NL80211_CMD_AUTHENTICATE:
1461 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1462 break;
1463 case NL80211_CMD_ASSOCIATE:
1464 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1465 break;
1466 case NL80211_CMD_DEAUTHENTICATE:
1467 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1468 nla_data(frame), nla_len(frame));
1469 break;
1470 case NL80211_CMD_DISASSOCIATE:
1471 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1472 nla_data(frame), nla_len(frame));
1473 break;
1474 case NL80211_CMD_FRAME:
1475 mlme_event_mgmt(drv, freq, nla_data(frame), nla_len(frame));
1476 break;
1477 case NL80211_CMD_FRAME_TX_STATUS:
1478 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1479 nla_len(frame), ack);
1480 break;
1481 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1482 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1483 nla_data(frame), nla_len(frame));
1484 break;
1485 case NL80211_CMD_UNPROT_DISASSOCIATE:
1486 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1487 nla_data(frame), nla_len(frame));
1488 break;
1489 default:
1490 break;
1491 }
1492 }
1495 static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
1496 struct nlattr *tb[])
1497 {
1498 union wpa_event_data data;
1500 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1501 os_memset(&data, 0, sizeof(data));
1502 if (tb[NL80211_ATTR_MAC]) {
1503 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1504 nla_data(tb[NL80211_ATTR_MAC]),
1505 nla_len(tb[NL80211_ATTR_MAC]));
1506 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1507 }
1508 if (tb[NL80211_ATTR_KEY_SEQ]) {
1509 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1510 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1511 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1512 }
1513 if (tb[NL80211_ATTR_KEY_TYPE]) {
1514 enum nl80211_key_type key_type =
1515 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1516 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1517 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1518 data.michael_mic_failure.unicast = 1;
1519 } else
1520 data.michael_mic_failure.unicast = 1;
1522 if (tb[NL80211_ATTR_KEY_IDX]) {
1523 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1524 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1525 }
1527 wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
1528 }
1531 static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1532 struct nlattr *tb[])
1533 {
1534 if (tb[NL80211_ATTR_MAC] == NULL) {
1535 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1536 "event");
1537 return;
1538 }
1539 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1540 drv->associated = 1;
1541 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1542 MAC2STR(drv->bssid));
1544 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1545 }
1548 static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1549 int cancel_event, struct nlattr *tb[])
1550 {
1551 unsigned int freq, chan_type, duration;
1552 union wpa_event_data data;
1553 u64 cookie;
1555 if (tb[NL80211_ATTR_WIPHY_FREQ])
1556 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1557 else
1558 freq = 0;
1560 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1561 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1562 else
1563 chan_type = 0;
1565 if (tb[NL80211_ATTR_DURATION])
1566 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1567 else
1568 duration = 0;
1570 if (tb[NL80211_ATTR_COOKIE])
1571 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1572 else
1573 cookie = 0;
1575 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1576 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1577 cancel_event, freq, chan_type, duration,
1578 (long long unsigned int) cookie,
1579 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
1581 if (cookie != drv->remain_on_chan_cookie)
1582 return; /* not for us */
1584 if (cancel_event)
1585 drv->pending_remain_on_chan = 0;
1587 os_memset(&data, 0, sizeof(data));
1588 data.remain_on_channel.freq = freq;
1589 data.remain_on_channel.duration = duration;
1590 wpa_supplicant_event(drv->ctx, cancel_event ?
1591 EVENT_CANCEL_REMAIN_ON_CHANNEL :
1592 EVENT_REMAIN_ON_CHANNEL, &data);
1593 }
1596 static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
1597 struct nlattr *tb[])
1598 {
1599 union wpa_event_data event;
1600 struct nlattr *nl;
1601 int rem;
1602 struct scan_info *info;
1603 #define MAX_REPORT_FREQS 50
1604 int freqs[MAX_REPORT_FREQS];
1605 int num_freqs = 0;
1607 if (drv->scan_for_auth) {
1608 drv->scan_for_auth = 0;
1609 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
1610 "cfg80211 BSS entry");
1611 wpa_driver_nl80211_authenticate_retry(drv);
1612 return;
1613 }
1615 os_memset(&event, 0, sizeof(event));
1616 info = &event.scan_info;
1617 info->aborted = aborted;
1619 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
1620 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
1621 struct wpa_driver_scan_ssid *s =
1622 &info->ssids[info->num_ssids];
1623 s->ssid = nla_data(nl);
1624 s->ssid_len = nla_len(nl);
1625 info->num_ssids++;
1626 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
1627 break;
1628 }
1629 }
1630 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
1631 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
1632 {
1633 freqs[num_freqs] = nla_get_u32(nl);
1634 num_freqs++;
1635 if (num_freqs == MAX_REPORT_FREQS - 1)
1636 break;
1637 }
1638 info->freqs = freqs;
1639 info->num_freqs = num_freqs;
1640 }
1641 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
1642 }
1645 static int get_link_signal(struct nl_msg *msg, void *arg)
1646 {
1647 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1648 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1649 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1650 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1651 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1652 };
1653 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1654 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1655 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1656 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1657 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1658 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1659 };
1660 struct wpa_signal_info *sig_change = arg;
1662 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1663 genlmsg_attrlen(gnlh, 0), NULL);
1664 if (!tb[NL80211_ATTR_STA_INFO] ||
1665 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1666 tb[NL80211_ATTR_STA_INFO], policy))
1667 return NL_SKIP;
1668 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1669 return NL_SKIP;
1671 sig_change->current_signal =
1672 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1674 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1675 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1676 sinfo[NL80211_STA_INFO_TX_BITRATE],
1677 rate_policy)) {
1678 sig_change->current_txrate = 0;
1679 } else {
1680 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1681 sig_change->current_txrate =
1682 nla_get_u16(rinfo[
1683 NL80211_RATE_INFO_BITRATE]) * 100;
1684 }
1685 }
1686 }
1688 return NL_SKIP;
1689 }
1692 static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1693 struct wpa_signal_info *sig)
1694 {
1695 struct nl_msg *msg;
1697 sig->current_signal = -9999;
1698 sig->current_txrate = 0;
1700 msg = nlmsg_alloc();
1701 if (!msg)
1702 return -ENOMEM;
1704 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
1706 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1707 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
1709 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
1710 nla_put_failure:
1711 nlmsg_free(msg);
1712 return -ENOBUFS;
1713 }
1716 static int get_link_noise(struct nl_msg *msg, void *arg)
1717 {
1718 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1719 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1720 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1721 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1722 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1723 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1724 };
1725 struct wpa_signal_info *sig_change = arg;
1727 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1728 genlmsg_attrlen(gnlh, 0), NULL);
1730 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1731 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1732 return NL_SKIP;
1733 }
1735 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1736 tb[NL80211_ATTR_SURVEY_INFO],
1737 survey_policy)) {
1738 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1739 "attributes!");
1740 return NL_SKIP;
1741 }
1743 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1744 return NL_SKIP;
1746 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1747 sig_change->frequency)
1748 return NL_SKIP;
1750 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1751 return NL_SKIP;
1753 sig_change->current_noise =
1754 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1756 return NL_SKIP;
1757 }
1760 static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1761 struct wpa_signal_info *sig_change)
1762 {
1763 struct nl_msg *msg;
1765 sig_change->current_noise = 9999;
1766 sig_change->frequency = drv->assoc_freq;
1768 msg = nlmsg_alloc();
1769 if (!msg)
1770 return -ENOMEM;
1772 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1774 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1776 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
1777 nla_put_failure:
1778 nlmsg_free(msg);
1779 return -ENOBUFS;
1780 }
1783 static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
1784 {
1785 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1786 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1787 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1788 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1789 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1790 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1791 };
1792 struct wpa_scan_results *scan_results = arg;
1793 struct wpa_scan_res *scan_res;
1794 size_t i;
1796 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1797 genlmsg_attrlen(gnlh, 0), NULL);
1799 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1800 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
1801 return NL_SKIP;
1802 }
1804 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1805 tb[NL80211_ATTR_SURVEY_INFO],
1806 survey_policy)) {
1807 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
1808 "attributes");
1809 return NL_SKIP;
1810 }
1812 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1813 return NL_SKIP;
1815 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1816 return NL_SKIP;
1818 for (i = 0; i < scan_results->num; ++i) {
1819 scan_res = scan_results->res[i];
1820 if (!scan_res)
1821 continue;
1822 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1823 scan_res->freq)
1824 continue;
1825 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
1826 continue;
1827 scan_res->noise = (s8)
1828 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1829 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
1830 }
1832 return NL_SKIP;
1833 }
1836 static int nl80211_get_noise_for_scan_results(
1837 struct wpa_driver_nl80211_data *drv,
1838 struct wpa_scan_results *scan_res)
1839 {
1840 struct nl_msg *msg;
1842 msg = nlmsg_alloc();
1843 if (!msg)
1844 return -ENOMEM;
1846 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1848 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1850 return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
1851 scan_res);
1852 nla_put_failure:
1853 nlmsg_free(msg);
1854 return -ENOBUFS;
1855 }
1858 static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
1859 struct nlattr *tb[])
1860 {
1861 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
1862 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
1863 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
1864 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
1865 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
1866 };
1867 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
1868 enum nl80211_cqm_rssi_threshold_event event;
1869 union wpa_event_data ed;
1870 struct wpa_signal_info sig;
1871 int res;
1873 if (tb[NL80211_ATTR_CQM] == NULL ||
1874 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
1875 cqm_policy)) {
1876 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
1877 return;
1878 }
1880 os_memset(&ed, 0, sizeof(ed));
1882 if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
1883 if (!tb[NL80211_ATTR_MAC])
1884 return;
1885 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
1886 ETH_ALEN);
1887 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
1888 return;
1889 }
1891 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
1892 return;
1893 event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
1895 if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
1896 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1897 "event: RSSI high");
1898 ed.signal_change.above_threshold = 1;
1899 } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
1900 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1901 "event: RSSI low");
1902 ed.signal_change.above_threshold = 0;
1903 } else if (event == NL80211_CQM_RSSI_BEACON_LOSS) {
1904 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1905 "event: beacon loss!");
1906 wpa_supplicant_event(drv->ctx, EVENT_START_ROAMING, &ed);
1907 } else
1908 return;
1910 res = nl80211_get_link_signal(drv, &sig);
1911 if (res == 0) {
1912 ed.signal_change.current_signal = sig.current_signal;
1913 ed.signal_change.current_txrate = sig.current_txrate;
1914 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm txrate: %d",
1915 sig.current_signal, sig.current_txrate);
1916 }
1918 res = nl80211_get_link_noise(drv, &sig);
1919 if (res == 0) {
1920 ed.signal_change.current_noise = sig.current_noise;
1921 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
1922 sig.current_noise);
1923 }
1925 wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
1926 }
1929 static void nl80211_roaming_support_event(struct wpa_driver_nl80211_data *drv,
1930 struct nlattr *tb[])
1931 {
1932 int enabled;
1933 enum wpa_event_type event;
1935 enabled = (tb[NL80211_ATTR_ROAMING_DISABLED] == NULL);
1937 if (enabled)
1938 event = EVENT_ROAMING_ENABLED;
1939 else
1940 event = EVENT_ROAMING_DISABLED;
1942 wpa_printf(MSG_DEBUG, "nl80211: roaming %s",
1943 enabled ? "enabled" : "disabled");
1945 wpa_supplicant_event(drv->ctx, event, NULL);
1946 }
1948 static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
1949 struct nlattr **tb)
1950 {
1951 u8 *addr;
1952 union wpa_event_data data;
1954 if (tb[NL80211_ATTR_MAC] == NULL)
1955 return;
1956 addr = nla_data(tb[NL80211_ATTR_MAC]);
1957 wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
1959 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
1960 u8 *ies = NULL;
1961 size_t ies_len = 0;
1962 if (tb[NL80211_ATTR_IE]) {
1963 ies = nla_data(tb[NL80211_ATTR_IE]);
1964 ies_len = nla_len(tb[NL80211_ATTR_IE]);
1965 }
1966 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
1967 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
1968 return;
1969 }
1971 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1972 return;
1974 os_memset(&data, 0, sizeof(data));
1975 os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
1976 wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
1977 }
1980 static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
1981 struct nlattr **tb)
1982 {
1983 u8 *addr;
1984 union wpa_event_data data;
1986 if (tb[NL80211_ATTR_MAC] == NULL)
1987 return;
1988 addr = nla_data(tb[NL80211_ATTR_MAC]);
1989 wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
1990 MAC2STR(addr));
1992 if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
1993 drv_event_disassoc(drv->ctx, addr);
1994 return;
1995 }
1997 if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1998 return;
2000 os_memset(&data, 0, sizeof(data));
2001 os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
2002 wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
2003 }
2006 static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
2007 struct nlattr **tb)
2008 {
2009 struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
2010 static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
2011 [NL80211_REKEY_DATA_KEK] = {
2012 .minlen = NL80211_KEK_LEN,
2013 .maxlen = NL80211_KEK_LEN,
2014 },
2015 [NL80211_REKEY_DATA_KCK] = {
2016 .minlen = NL80211_KCK_LEN,
2017 .maxlen = NL80211_KCK_LEN,
2018 },
2019 [NL80211_REKEY_DATA_REPLAY_CTR] = {
2020 .minlen = NL80211_REPLAY_CTR_LEN,
2021 .maxlen = NL80211_REPLAY_CTR_LEN,
2022 },
2023 };
2024 union wpa_event_data data;
2026 if (!tb[NL80211_ATTR_MAC])
2027 return;
2028 if (!tb[NL80211_ATTR_REKEY_DATA])
2029 return;
2030 if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
2031 tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
2032 return;
2033 if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
2034 return;
2036 os_memset(&data, 0, sizeof(data));
2037 data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
2038 wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
2039 MAC2STR(data.driver_gtk_rekey.bssid));
2040 data.driver_gtk_rekey.replay_ctr =
2041 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
2042 wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
2043 data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
2044 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
2045 }
2048 static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
2049 struct nlattr **tb)
2050 {
2051 struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
2052 static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
2053 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
2054 [NL80211_PMKSA_CANDIDATE_BSSID] = {
2055 .minlen = ETH_ALEN,
2056 .maxlen = ETH_ALEN,
2057 },
2058 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
2059 };
2060 union wpa_event_data data;
2062 if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
2063 return;
2064 if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
2065 tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
2066 return;
2067 if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
2068 !cand[NL80211_PMKSA_CANDIDATE_BSSID])
2069 return;
2071 os_memset(&data, 0, sizeof(data));
2072 os_memcpy(data.pmkid_candidate.bssid,
2073 nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2074 data.pmkid_candidate.index =
2075 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2076 data.pmkid_candidate.preauth =
2077 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2078 wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2079 }
2082 static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2083 struct nlattr **tb)
2084 {
2085 union wpa_event_data data;
2087 if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2088 return;
2090 os_memset(&data, 0, sizeof(data));
2091 os_memcpy(data.client_poll.addr,
2092 nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2094 wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2095 }
2098 static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2099 int wds)
2100 {
2101 struct wpa_driver_nl80211_data *drv = bss->drv;
2102 union wpa_event_data event;
2104 if (!tb[NL80211_ATTR_MAC])
2105 return;
2107 os_memset(&event, 0, sizeof(event));
2108 event.rx_from_unknown.bssid = bss->addr;
2109 event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2110 event.rx_from_unknown.wds = wds;
2112 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2113 }
2116 static void do_process_drv_event(struct wpa_driver_nl80211_data *drv,
2117 int cmd, struct nlattr **tb)
2118 {
2119 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2120 (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2121 cmd == NL80211_CMD_SCAN_ABORTED)) {
2122 wpa_driver_nl80211_set_mode(&drv->first_bss,
2123 drv->ap_scan_as_station);
2124 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
2125 }
2127 switch (cmd) {
2128 case NL80211_CMD_TRIGGER_SCAN:
2129 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
2130 break;
2131 case NL80211_CMD_START_SCHED_SCAN:
2132 wpa_printf(MSG_DEBUG, "nl80211: Sched scan started");
2133 break;
2134 case NL80211_CMD_SCHED_SCAN_STOPPED:
2135 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stopped");
2136 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2137 break;
2138 case NL80211_CMD_NEW_SCAN_RESULTS:
2139 wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
2140 drv->scan_complete_events = 1;
2141 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2142 drv->ctx);
2143 send_scan_event(drv, 0, tb);
2144 break;
2145 case NL80211_CMD_SCHED_SCAN_RESULTS:
2146 wpa_printf(MSG_DEBUG,
2147 "nl80211: New sched scan results available");
2148 send_scan_event(drv, 0, tb);
2149 break;
2150 case NL80211_CMD_SCAN_ABORTED:
2151 wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
2152 /*
2153 * Need to indicate that scan results are available in order
2154 * not to make wpa_supplicant stop its scanning.
2155 */
2156 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2157 drv->ctx);
2158 send_scan_event(drv, 1, tb);
2159 break;
2160 case NL80211_CMD_AUTHENTICATE:
2161 case NL80211_CMD_ASSOCIATE:
2162 case NL80211_CMD_DEAUTHENTICATE:
2163 case NL80211_CMD_DISASSOCIATE:
2164 case NL80211_CMD_FRAME_TX_STATUS:
2165 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2166 case NL80211_CMD_UNPROT_DISASSOCIATE:
2167 mlme_event(drv, cmd, tb[NL80211_ATTR_FRAME],
2168 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2169 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
2170 tb[NL80211_ATTR_COOKIE]);
2171 break;
2172 case NL80211_CMD_CONNECT:
2173 case NL80211_CMD_ROAM:
2174 mlme_event_connect(drv, cmd,
2175 tb[NL80211_ATTR_STATUS_CODE],
2176 tb[NL80211_ATTR_MAC],
2177 tb[NL80211_ATTR_REQ_IE],
2178 tb[NL80211_ATTR_RESP_IE]);
2179 break;
2180 case NL80211_CMD_DISCONNECT:
2181 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
2182 tb[NL80211_ATTR_MAC],
2183 tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
2184 break;
2185 case NL80211_CMD_MICHAEL_MIC_FAILURE:
2186 mlme_event_michael_mic_failure(drv, tb);
2187 break;
2188 case NL80211_CMD_JOIN_IBSS:
2189 mlme_event_join_ibss(drv, tb);
2190 break;
2191 case NL80211_CMD_REMAIN_ON_CHANNEL:
2192 mlme_event_remain_on_channel(drv, 0, tb);
2193 break;
2194 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2195 mlme_event_remain_on_channel(drv, 1, tb);
2196 break;
2197 case NL80211_CMD_NOTIFY_CQM:
2198 nl80211_cqm_event(drv, tb);
2199 break;
2200 case NL80211_CMD_ROAMING_SUPPORT:
2201 nl80211_roaming_support_event(drv, tb);
2202 break;
2203 case NL80211_CMD_REG_CHANGE:
2204 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2205 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2206 NULL);
2207 break;
2208 case NL80211_CMD_REG_BEACON_HINT:
2209 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
2210 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2211 NULL);
2212 break;
2213 case NL80211_CMD_NEW_STATION:
2214 nl80211_new_station_event(drv, tb);
2215 break;
2216 case NL80211_CMD_DEL_STATION:
2217 nl80211_del_station_event(drv, tb);
2218 break;
2219 case NL80211_CMD_SET_REKEY_OFFLOAD:
2220 nl80211_rekey_offload_event(drv, tb);
2221 break;
2222 case NL80211_CMD_PMKSA_CANDIDATE:
2223 nl80211_pmksa_candidate_event(drv, tb);
2224 break;
2225 case NL80211_CMD_PROBE_CLIENT:
2226 nl80211_client_probe_event(drv, tb);
2227 break;
2228 default:
2229 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
2230 "(cmd=%d)", cmd);
2231 break;
2232 }
2233 }
2236 static int process_drv_event(struct nl_msg *msg, void *arg)
2237 {
2238 struct wpa_driver_nl80211_data *drv = arg;
2239 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2240 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2242 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2243 genlmsg_attrlen(gnlh, 0), NULL);
2245 if (tb[NL80211_ATTR_IFINDEX]) {
2246 int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2247 if (ifindex != drv->ifindex && !have_ifidx(drv, ifindex)) {
2248 wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)"
2249 " for foreign interface (ifindex %d)",
2250 gnlh->cmd, ifindex);
2251 return NL_SKIP;
2252 }
2253 }
2255 do_process_drv_event(drv, gnlh->cmd, tb);
2256 return NL_SKIP;
2257 }
2260 static int process_global_event(struct nl_msg *msg, void *arg)
2261 {
2262 struct nl80211_global *global = arg;
2263 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2264 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2265 struct wpa_driver_nl80211_data *drv;
2266 int ifidx = -1;
2268 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2269 genlmsg_attrlen(gnlh, 0), NULL);
2271 if (tb[NL80211_ATTR_IFINDEX])
2272 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2274 dl_list_for_each(drv, &global->interfaces,
2275 struct wpa_driver_nl80211_data, list) {
2276 if (ifidx == -1 || ifidx == drv->ifindex ||
2277 have_ifidx(drv, ifidx))
2278 do_process_drv_event(drv, gnlh->cmd, tb);
2279 }
2281 return NL_SKIP;
2282 }
2285 static int process_bss_event(struct nl_msg *msg, void *arg)
2286 {
2287 struct i802_bss *bss = arg;
2288 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2289 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2291 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2292 genlmsg_attrlen(gnlh, 0), NULL);
2294 switch (gnlh->cmd) {
2295 case NL80211_CMD_FRAME:
2296 case NL80211_CMD_FRAME_TX_STATUS:
2297 mlme_event(bss->drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
2298 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2299 tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
2300 tb[NL80211_ATTR_COOKIE]);
2301 break;
2302 case NL80211_CMD_UNEXPECTED_FRAME:
2303 nl80211_spurious_frame(bss, tb, 0);
2304 break;
2305 case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
2306 nl80211_spurious_frame(bss, tb, 1);
2307 break;
2308 default:
2309 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
2310 "(cmd=%d)", gnlh->cmd);
2311 break;
2312 }
2314 return NL_SKIP;
2315 }
2318 static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
2319 void *handle)
2320 {
2321 struct nl_cb *cb = eloop_ctx;
2323 wpa_printf(MSG_DEBUG, "nl80211: Event message available");
2325 nl_recvmsgs(handle, cb);
2326 }
2329 /**
2330 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
2331 * @priv: driver_nl80211 private data
2332 * @alpha2_arg: country to which to switch to
2333 * Returns: 0 on success, -1 on failure
2334 *
2335 * This asks nl80211 to set the regulatory domain for given
2336 * country ISO / IEC alpha2.
2337 */
2338 static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
2339 {
2340 struct i802_bss *bss = priv;
2341 struct wpa_driver_nl80211_data *drv = bss->drv;
2342 char alpha2[3];
2343 struct nl_msg *msg;
2345 msg = nlmsg_alloc();
2346 if (!msg)
2347 return -ENOMEM;
2349 alpha2[0] = alpha2_arg[0];
2350 alpha2[1] = alpha2_arg[1];
2351 alpha2[2] = '\0';
2353 nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
2355 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
2356 if (send_and_recv_msgs(drv, msg, NULL, NULL))
2357 return -EINVAL;
2358 return 0;
2359 nla_put_failure:
2360 nlmsg_free(msg);
2361 return -EINVAL;
2362 }
2365 struct wiphy_info_data {
2366 struct wpa_driver_capa *capa;
2368 unsigned int error:1;
2369 unsigned int device_ap_sme:1;
2370 unsigned int poll_command_supported:1;
2371 unsigned int data_tx_status:1;
2372 unsigned int monitor_supported:1;
2373 };
2376 static unsigned int probe_resp_offload_support(int supp_protocols)
2377 {
2378 unsigned int prot = 0;
2380 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
2381 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
2382 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
2383 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
2384 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
2385 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
2386 if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
2387 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
2389 return prot;
2390 }
2393 static int wiphy_info_handler(struct nl_msg *msg, void *arg)
2394 {
2395 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2396 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2397 struct wiphy_info_data *info = arg;
2398 int p2p_go_supported = 0, p2p_client_supported = 0;
2399 int p2p_concurrent = 0;
2400 int auth_supported = 0, connect_supported = 0;
2401 struct wpa_driver_capa *capa = info->capa;
2402 static struct nla_policy
2403 iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
2404 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
2405 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
2406 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
2407 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
2408 },
2409 iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
2410 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
2411 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
2412 };
2414 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2415 genlmsg_attrlen(gnlh, 0), NULL);
2417 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
2418 capa->max_scan_ssids =
2419 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
2421 if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
2422 capa->max_sched_scan_ssids =
2423 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
2425 if (tb[NL80211_ATTR_MAX_MATCH_SETS])
2426 capa->max_match_sets =
2427 nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
2429 if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
2430 struct nlattr *nl_mode;
2431 int i;
2432 nla_for_each_nested(nl_mode,
2433 tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
2434 switch (nla_type(nl_mode)) {
2435 case NL80211_IFTYPE_AP:
2436 capa->flags |= WPA_DRIVER_FLAGS_AP;
2437 break;
2438 case NL80211_IFTYPE_P2P_GO:
2439 p2p_go_supported = 1;
2440 break;
2441 case NL80211_IFTYPE_P2P_CLIENT:
2442 p2p_client_supported = 1;
2443 break;
2444 case NL80211_IFTYPE_MONITOR:
2445 info->monitor_supported = 1;
2446 break;
2447 }
2448 }
2449 }
2451 if (tb[NL80211_ATTR_INTERFACE_COMBINATIONS]) {
2452 struct nlattr *nl_combi;
2453 int rem_combi;
2455 nla_for_each_nested(nl_combi,
2456 tb[NL80211_ATTR_INTERFACE_COMBINATIONS],
2457 rem_combi) {
2458 struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
2459 struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
2460 struct nlattr *nl_limit, *nl_mode;
2461 int err, rem_limit, rem_mode;
2462 int combination_has_p2p = 0, combination_has_mgd = 0;
2464 err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
2465 nl_combi,
2466 iface_combination_policy);
2467 if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
2468 !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
2469 !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
2470 goto broken_combination;
2472 nla_for_each_nested(nl_limit,
2473 tb_comb[NL80211_IFACE_COMB_LIMITS],
2474 rem_limit) {
2475 err = nla_parse_nested(tb_limit,
2476 MAX_NL80211_IFACE_LIMIT,
2477 nl_limit,
2478 iface_limit_policy);
2479 if (err ||
2480 !tb_limit[NL80211_IFACE_LIMIT_TYPES])
2481 goto broken_combination;
2483 nla_for_each_nested(
2484 nl_mode,
2485 tb_limit[NL80211_IFACE_LIMIT_TYPES],
2486 rem_mode) {
2487 int ift = nla_type(nl_mode);
2488 if (ift == NL80211_IFTYPE_P2P_GO ||
2489 ift == NL80211_IFTYPE_P2P_CLIENT)
2490 combination_has_p2p = 1;
2491 if (ift == NL80211_IFTYPE_STATION)
2492 combination_has_mgd = 1;
2493 }
2494 if (combination_has_p2p && combination_has_mgd)
2495 break;
2496 }
2498 if (combination_has_p2p && combination_has_mgd) {
2499 p2p_concurrent = 1;
2500 break;
2501 }
2503 broken_combination:
2504 ;
2505 }
2506 }
2508 if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
2509 struct nlattr *nl_cmd;
2510 int i;
2512 nla_for_each_nested(nl_cmd,
2513 tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
2514 switch (nla_get_u32(nl_cmd)) {
2515 case NL80211_CMD_AUTHENTICATE:
2516 auth_supported = 1;
2517 break;
2518 case NL80211_CMD_CONNECT:
2519 connect_supported = 1;
2520 break;
2521 case NL80211_CMD_START_SCHED_SCAN:
2522 capa->sched_scan_supported = 1;
2523 break;
2524 case NL80211_CMD_PROBE_CLIENT:
2525 info->poll_command_supported = 1;
2526 break;
2527 }
2528 }
2529 }
2531 if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
2532 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
2533 "off-channel TX");
2534 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
2535 }
2537 if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
2538 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
2539 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
2540 }
2542 /* default to 5000 since early versions of mac80211 don't set it */
2543 capa->max_remain_on_chan = 5000;
2545 if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
2546 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
2548 if (tb[NL80211_ATTR_FEATURE_FLAGS]) {
2549 int features = nla_get_u32(tb[NL80211_ATTR_FEATURE_FLAGS]);
2550 if (features & NL80211_FEATURE_SCHED_SCAN_INTERVALS)
2551 capa->sched_scan_intervals_supported = 1;
2552 }
2554 if (tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION])
2555 capa->max_remain_on_chan =
2556 nla_get_u32(tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
2558 if (auth_supported)
2559 capa->flags |= WPA_DRIVER_FLAGS_SME;
2560 else if (!connect_supported) {
2561 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
2562 "authentication/association or connect commands");
2563 info->error = 1;
2564 }
2566 if (p2p_go_supported && p2p_client_supported)
2567 capa->flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
2568 if (p2p_concurrent) {
2569 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
2570 "interface (driver advertised support)");
2571 capa->flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
2572 capa->flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
2573 }
2575 if (tb[NL80211_ATTR_TDLS_SUPPORT]) {
2576 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
2577 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
2579 if (tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]) {
2580 wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
2581 capa->flags |=
2582 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
2583 }
2584 }
2586 if (tb[NL80211_ATTR_DEVICE_AP_SME])
2587 info->device_ap_sme = 1;
2589 if (tb[NL80211_ATTR_FEATURE_FLAGS]) {
2590 u32 flags = nla_get_u32(tb[NL80211_ATTR_FEATURE_FLAGS]);
2592 if (flags & NL80211_FEATURE_SK_TX_STATUS)
2593 info->data_tx_status = 1;
2594 }
2596 if (tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]) {
2597 int protocols =
2598 nla_get_u32(tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
2599 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response "
2600 "offload in AP mode");
2601 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
2602 capa->probe_resp_offloads =
2603 probe_resp_offload_support(protocols);
2604 }
2606 return NL_SKIP;
2607 }
2610 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
2611 struct wiphy_info_data *info)
2612 {
2613 struct nl_msg *msg;
2615 os_memset(info, 0, sizeof(*info));
2616 info->capa = &drv->capa;
2618 msg = nlmsg_alloc();
2619 if (!msg)
2620 return -1;
2622 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
2624 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
2626 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0)
2627 return 0;
2628 msg = NULL;
2629 nla_put_failure:
2630 nlmsg_free(msg);
2631 return -1;
2632 }
2635 static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
2636 {
2637 struct wiphy_info_data info;
2638 if (wpa_driver_nl80211_get_info(drv, &info))
2639 return -1;
2641 if (info.error)
2642 return -1;
2644 drv->has_capability = 1;
2645 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
2646 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2647 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2648 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2649 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
2650 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
2651 WPA_DRIVER_CAPA_ENC_WEP104 |
2652 WPA_DRIVER_CAPA_ENC_TKIP |
2653 WPA_DRIVER_CAPA_ENC_CCMP;
2654 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
2655 WPA_DRIVER_AUTH_SHARED |
2656 WPA_DRIVER_AUTH_LEAP;
2658 drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
2659 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
2660 drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
2661 drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
2663 drv->device_ap_sme = info.device_ap_sme;
2664 drv->poll_command_supported = info.poll_command_supported;
2665 drv->data_tx_status = info.data_tx_status;
2667 /*
2668 * If poll command is supported mac80211 is new enough to
2669 * have everything we need to not need monitor interfaces.
2670 */
2671 drv->use_monitor = !info.poll_command_supported;
2673 if (drv->device_ap_sme && drv->use_monitor) {
2674 /*
2675 * Non-mac80211 drivers may not support monitor interface.
2676 * Make sure we do not get stuck with incorrect capability here
2677 * by explicitly testing this.
2678 */
2679 if (!info.monitor_supported) {
2680 wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
2681 "with device_ap_sme since no monitor mode "
2682 "support detected");
2683 drv->use_monitor = 0;
2684 }
2685 }
2687 /*
2688 * If we aren't going to use monitor interfaces, but the
2689 * driver doesn't support data TX status, we won't get TX
2690 * status for EAPOL frames.
2691 */
2692 if (!drv->use_monitor && !info.data_tx_status)
2693 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
2695 return 0;
2696 }
2699 #ifdef ANDROID
2700 static int android_genl_ctrl_resolve(struct nl_handle *handle,
2701 const char *name)
2702 {
2703 /*
2704 * Android ICS has very minimal genl_ctrl_resolve() implementation, so
2705 * need to work around that.
2706 */
2707 struct nl_cache *cache = NULL;
2708 struct genl_family *nl80211 = NULL;
2709 int id = -1;
2711 if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
2712 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
2713 "netlink cache");
2714 goto fail;
2715 }
2717 nl80211 = genl_ctrl_search_by_name(cache, name);
2718 if (nl80211 == NULL)
2719 goto fail;
2721 id = genl_family_get_id(nl80211);
2723 fail:
2724 if (nl80211)
2725 genl_family_put(nl80211);
2726 if (cache)
2727 nl_cache_free(cache);
2729 return id;
2730 }
2731 #define genl_ctrl_resolve android_genl_ctrl_resolve
2732 #endif /* ANDROID */
2735 static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
2736 {
2737 int ret;
2739 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2740 if (global->nl_cb == NULL) {
2741 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
2742 "callbacks");
2743 return -1;
2744 }
2746 global->nl = nl_create_handle(global->nl_cb, "nl");
2747 if (global->nl == NULL)
2748 goto err;
2750 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
2751 if (global->nl80211_id < 0) {
2752 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
2753 "found");
2754 goto err;
2755 }
2757 global->nl_event = nl_create_handle(global->nl_cb, "event");
2758 if (global->nl_event == NULL)
2759 goto err;
2761 ret = nl_get_multicast_id(global, "nl80211", "scan");
2762 if (ret >= 0)
2763 ret = nl_socket_add_membership(global->nl_event, ret);
2764 if (ret < 0) {
2765 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
2766 "membership for scan events: %d (%s)",
2767 ret, strerror(-ret));
2768 goto err;
2769 }
2771 ret = nl_get_multicast_id(global, "nl80211", "mlme");
2772 if (ret >= 0)
2773 ret = nl_socket_add_membership(global->nl_event, ret);
2774 if (ret < 0) {
2775 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
2776 "membership for mlme events: %d (%s)",
2777 ret, strerror(-ret));
2778 goto err;
2779 }
2781 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
2782 if (ret >= 0)
2783 ret = nl_socket_add_membership(global->nl_event, ret);
2784 if (ret < 0) {
2785 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
2786 "membership for regulatory events: %d (%s)",
2787 ret, strerror(-ret));
2788 /* Continue without regulatory events */
2789 }
2791 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2792 no_seq_check, NULL);
2793 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2794 process_global_event, global);
2796 eloop_register_read_sock(nl_socket_get_fd(global->nl_event),
2797 wpa_driver_nl80211_event_receive,
2798 global->nl_cb, global->nl_event);
2800 return 0;
2802 err:
2803 nl_destroy_handles(&global->nl_event);
2804 nl_destroy_handles(&global->nl);
2805 nl_cb_put(global->nl_cb);
2806 global->nl_cb = NULL;
2807 return -1;
2808 }
2811 static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
2812 {
2813 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2814 if (!drv->nl_cb) {
2815 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
2816 return -1;
2817 }
2819 nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2820 no_seq_check, NULL);
2821 nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2822 process_drv_event, drv);
2824 return 0;
2825 }
2828 static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
2829 {
2830 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
2831 /*
2832 * This may be for any interface; use ifdown event to disable
2833 * interface.
2834 */
2835 }
2838 static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
2839 {
2840 struct wpa_driver_nl80211_data *drv = ctx;
2841 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
2842 if (linux_set_iface_flags(drv->global->ioctl_sock,
2843 drv->first_bss.ifname, 1)) {
2844 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
2845 "after rfkill unblock");
2846 return;
2847 }
2848 /* rtnetlink ifup handler will report interface as enabled */
2849 }
2852 static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
2853 {
2854 /* Find phy (radio) to which this interface belongs */
2855 char buf[90], *pos;
2856 int f, rv;
2858 drv->phyname[0] = '\0';
2859 snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
2860 drv->first_bss.ifname);
2861 f = open(buf, O_RDONLY);
2862 if (f < 0) {
2863 wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
2864 buf, strerror(errno));
2865 return;
2866 }
2868 rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
2869 close(f);
2870 if (rv < 0) {
2871 wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
2872 buf, strerror(errno));
2873 return;
2874 }
2876 drv->phyname[rv] = '\0';
2877 pos = os_strchr(drv->phyname, '\n');
2878 if (pos)
2879 *pos = '\0';
2880 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2881 drv->first_bss.ifname, drv->phyname);
2882 }
2885 static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
2886 void *eloop_ctx,
2887 void *handle)
2888 {
2889 struct wpa_driver_nl80211_data *drv = eloop_ctx;
2890 u8 data[2048];
2891 struct msghdr msg;
2892 struct iovec entry;
2893 struct {
2894 struct cmsghdr cm;
2895 char control[512];
2896 } control;
2897 struct cmsghdr *cmsg;
2898 int res, found_ee = 0, found_wifi = 0, acked = 0;
2899 union wpa_event_data event;
2901 memset(&msg, 0, sizeof(msg));
2902 msg.msg_iov = &entry;
2903 msg.msg_iovlen = 1;
2904 entry.iov_base = data;
2905 entry.iov_len = sizeof(data);
2906 msg.msg_control = &control;
2907 msg.msg_controllen = sizeof(control);
2909 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
2910 /* if error or not fitting 802.3 header, return */
2911 if (res < 14)
2912 return;
2914 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
2915 {
2916 if (cmsg->cmsg_level == SOL_SOCKET &&
2917 cmsg->cmsg_type == SCM_WIFI_STATUS) {
2918 int *ack;
2920 found_wifi = 1;
2921 ack = (void *)CMSG_DATA(cmsg);
2922 acked = *ack;
2923 }
2925 if (cmsg->cmsg_level == SOL_PACKET &&
2926 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
2927 struct sock_extended_err *err =
2928 (struct sock_extended_err *)CMSG_DATA(cmsg);
2930 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
2931 found_ee = 1;
2932 }
2933 }
2935 if (!found_ee || !found_wifi)
2936 return;
2938 memset(&event, 0, sizeof(event));
2939 event.eapol_tx_status.dst = data;
2940 event.eapol_tx_status.data = data + 14;
2941 event.eapol_tx_status.data_len = res - 14;
2942 event.eapol_tx_status.ack = acked;
2943 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
2944 }
2947 static int nl80211_init_bss(struct i802_bss *bss)
2948 {
2949 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2950 if (!bss->nl_cb)
2951 return -1;
2953 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2954 no_seq_check, NULL);
2955 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2956 process_bss_event, bss);
2958 return 0;
2959 }
2962 static void nl80211_destroy_bss(struct i802_bss *bss)
2963 {
2964 nl_cb_put(bss->nl_cb);
2965 bss->nl_cb = NULL;
2966 }
2969 /**
2970 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
2971 * @ctx: context to be used when calling wpa_supplicant functions,
2972 * e.g., wpa_supplicant_event()
2973 * @ifname: interface name, e.g., wlan0
2974 * @global_priv: private driver global data from global_init()
2975 * Returns: Pointer to private data, %NULL on failure
2976 */
2977 static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
2978 void *global_priv)
2979 {
2980 struct wpa_driver_nl80211_data *drv;
2981 struct rfkill_config *rcfg;
2982 struct i802_bss *bss;
2984 if (global_priv == NULL)
2985 return NULL;
2986 drv = os_zalloc(sizeof(*drv));
2987 if (drv == NULL)
2988 return NULL;
2989 drv->global = global_priv;
2990 drv->ctx = ctx;
2991 bss = &drv->first_bss;
2992 bss->drv = drv;
2993 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
2994 drv->monitor_ifidx = -1;
2995 drv->monitor_sock = -1;
2996 drv->eapol_tx_sock = -1;
2997 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
2999 if (wpa_driver_nl80211_init_nl(drv)) {
3000 os_free(drv);
3001 return NULL;
3002 }
3004 if (nl80211_init_bss(bss))
3005 goto failed;
3007 nl80211_get_phy_name(drv);
3009 rcfg = os_zalloc(sizeof(*rcfg));
3010 if (rcfg == NULL)
3011 goto failed;
3012 rcfg->ctx = drv;
3013 os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
3014 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
3015 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
3016 drv->rfkill = rfkill_init(rcfg);
3017 if (drv->rfkill == NULL) {
3018 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
3019 os_free(rcfg);
3020 }
3022 if (wpa_driver_nl80211_finish_drv_init(drv))
3023 goto failed;
3025 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
3026 if (drv->eapol_tx_sock < 0)
3027 goto failed;
3029 if (drv->data_tx_status) {
3030 int enabled = 1;
3032 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
3033 &enabled, sizeof(enabled)) < 0) {
3034 wpa_printf(MSG_DEBUG,
3035 "nl80211: wifi status sockopt failed\n");
3036 drv->data_tx_status = 0;
3037 if (!drv->use_monitor)
3038 drv->capa.flags &=
3039 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
3040 } else {
3041 eloop_register_read_sock(drv->eapol_tx_sock,
3042 wpa_driver_nl80211_handle_eapol_tx_status,
3043 drv, NULL);
3044 }
3045 }
3047 if (drv->global) {
3048 dl_list_add(&drv->global->interfaces, &drv->list);
3049 drv->in_interface_list = 1;
3050 }
3052 return bss;
3054 failed:
3055 wpa_driver_nl80211_deinit(bss);
3056 return NULL;
3057 }
3060 static int nl80211_register_frame(struct i802_bss *bss,
3061 struct nl_handle *nl_handle,
3062 u16 type, const u8 *match, size_t match_len)
3063 {
3064 struct wpa_driver_nl80211_data *drv = bss->drv;
3065 struct nl_msg *msg;
3066 int ret = -1;
3068 msg = nlmsg_alloc();
3069 if (!msg)
3070 return -1;
3072 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p",
3073 type, nl_handle);
3074 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3075 match, match_len);
3077 nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
3079 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
3080 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
3081 NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
3083 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
3084 msg = NULL;
3085 if (ret) {
3086 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
3087 "failed (type=%u): ret=%d (%s)",
3088 type, ret, strerror(-ret));
3089 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3090 match, match_len);
3091 goto nla_put_failure;
3092 }
3093 ret = 0;
3094 nla_put_failure:
3095 nlmsg_free(msg);
3096 return ret;
3097 }
3100 static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
3101 {
3102 struct wpa_driver_nl80211_data *drv = bss->drv;
3104 if (bss->nl_mgmt) {
3105 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
3106 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
3107 return -1;
3108 }
3110 bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
3111 if (bss->nl_mgmt == NULL)
3112 return -1;
3114 eloop_register_read_sock(nl_socket_get_fd(bss->nl_mgmt),
3115 wpa_driver_nl80211_event_receive, bss->nl_cb,
3116 bss->nl_mgmt);
3118 return 0;
3119 }
3122 static int nl80211_register_action_frame(struct i802_bss *bss,
3123 const u8 *match, size_t match_len)
3124 {
3125 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
3126 return nl80211_register_frame(bss, bss->nl_mgmt,
3127 type, match, match_len);
3128 }
3131 static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
3132 {
3133 struct wpa_driver_nl80211_data *drv = bss->drv;
3135 if (nl80211_alloc_mgmt_handle(bss))
3136 return -1;
3137 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
3138 "handle %p", bss->nl_mgmt);
3140 #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
3141 /* GAS Initial Request */
3142 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
3143 return -1;
3144 /* GAS Initial Response */
3145 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
3146 return -1;
3147 /* GAS Comeback Request */
3148 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
3149 return -1;
3150 /* GAS Comeback Response */
3151 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
3152 return -1;
3153 #endif /* CONFIG_P2P || CONFIG_INTERWORKING */
3154 #ifdef CONFIG_P2P
3155 /* P2P Public Action */
3156 if (nl80211_register_action_frame(bss,
3157 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
3158 6) < 0)
3159 return -1;
3160 /* P2P Action */
3161 if (nl80211_register_action_frame(bss,
3162 (u8 *) "\x7f\x50\x6f\x9a\x09",
3163 5) < 0)
3164 return -1;
3165 #endif /* CONFIG_P2P */
3166 #ifdef CONFIG_IEEE80211W
3167 /* SA Query Response */
3168 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
3169 return -1;
3170 #endif /* CONFIG_IEEE80211W */
3171 #ifdef CONFIG_TDLS
3172 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
3173 /* TDLS Discovery Response */
3174 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
3175 0)
3176 return -1;
3177 }
3178 #endif /* CONFIG_TDLS */
3180 /* FT Action frames */
3181 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
3182 return -1;
3183 else
3184 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
3185 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
3187 /* WNM - BSS Transition Management Request */
3188 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
3189 return -1;
3191 return 0;
3192 }
3195 static int nl80211_register_spurious_class3(struct i802_bss *bss)
3196 {
3197 struct wpa_driver_nl80211_data *drv = bss->drv;
3198 struct nl_msg *msg;
3199 int ret = -1;
3201 msg = nlmsg_alloc();
3202 if (!msg)
3203 return -1;
3205 nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
3207 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
3209 ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
3210 msg = NULL;
3211 if (ret) {
3212 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
3213 "failed: ret=%d (%s)",
3214 ret, strerror(-ret));
3215 goto nla_put_failure;
3216 }
3217 ret = 0;
3218 nla_put_failure:
3219 nlmsg_free(msg);
3220 return ret;
3221 }
3224 static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
3225 {
3226 static const int stypes[] = {
3227 WLAN_FC_STYPE_AUTH,
3228 WLAN_FC_STYPE_ASSOC_REQ,
3229 WLAN_FC_STYPE_REASSOC_REQ,
3230 WLAN_FC_STYPE_DISASSOC,
3231 WLAN_FC_STYPE_DEAUTH,
3232 WLAN_FC_STYPE_ACTION,
3233 WLAN_FC_STYPE_PROBE_REQ,
3234 /* Beacon doesn't work as mac80211 doesn't currently allow
3235 * it, but it wouldn't really be the right thing anyway as
3236 * it isn't per interface ... maybe just dump the scan
3237 * results periodically for OLBC?
3238 */
3239 // WLAN_FC_STYPE_BEACON,
3240 };
3241 unsigned int i;
3243 if (nl80211_alloc_mgmt_handle(bss))
3244 return -1;
3245 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3246 "handle %p", bss->nl_mgmt);
3248 for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) {
3249 if (nl80211_register_frame(bss, bss->nl_mgmt,
3250 (WLAN_FC_TYPE_MGMT << 2) |
3251 (stypes[i] << 4),
3252 NULL, 0) < 0) {
3253 goto out_err;
3254 }
3255 }
3257 if (nl80211_register_spurious_class3(bss))
3258 goto out_err;
3260 if (nl80211_get_wiphy_data_ap(bss) == NULL)
3261 goto out_err;
3263 return 0;
3265 out_err:
3266 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3267 nl_destroy_handles(&bss->nl_mgmt);
3268 return -1;
3269 }
3272 static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
3273 {
3274 if (nl80211_alloc_mgmt_handle(bss))
3275 return -1;
3276 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3277 "handle %p (device SME)", bss->nl_mgmt);
3279 if (nl80211_register_frame(bss, bss->nl_mgmt,
3280 (WLAN_FC_TYPE_MGMT << 2) |
3281 (WLAN_FC_STYPE_ACTION << 4),
3282 NULL, 0) < 0)
3283 goto out_err;
3285 return 0;
3287 out_err:
3288 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3289 nl_destroy_handles(&bss->nl_mgmt);
3290 return -1;
3291 }
3294 static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
3295 {
3296 if (bss->nl_mgmt == NULL)
3297 return;
3298 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
3299 "(%s)", bss->nl_mgmt, reason);
3300 eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3301 nl_destroy_handles(&bss->nl_mgmt);
3303 nl80211_put_wiphy_data_ap(bss);
3304 }
3307 static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
3308 {
3309 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
3310 }
3313 static int
3314 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
3315 {
3316 struct i802_bss *bss = &drv->first_bss;
3317 int send_rfkill_event = 0;
3319 drv->ifindex = if_nametoindex(bss->ifname);
3320 drv->first_bss.ifindex = drv->ifindex;
3322 #ifndef HOSTAPD
3323 /*
3324 * Make sure the interface starts up in station mode unless this is a
3325 * dynamically added interface (e.g., P2P) that was already configured
3326 * with proper iftype.
3327 */
3328 if (drv->ifindex != drv->global->if_add_ifindex &&
3329 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION) < 0) {
3330 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to "
3331 "use managed mode");
3332 return -1;
3333 }
3335 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
3336 if (rfkill_is_blocked(drv->rfkill)) {
3337 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
3338 "interface '%s' due to rfkill",
3339 bss->ifname);
3340 drv->if_disabled = 1;
3341 send_rfkill_event = 1;
3342 } else {
3343 wpa_printf(MSG_ERROR, "nl80211: Could not set "
3344 "interface '%s' UP", bss->ifname);
3345 return -1;
3346 }
3347 }
3349 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
3350 1, IF_OPER_DORMANT);
3351 #endif /* HOSTAPD */
3353 if (wpa_driver_nl80211_capa(drv))
3354 return -1;
3356 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3357 bss->addr))
3358 return -1;
3360 if (send_rfkill_event) {
3361 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
3362 drv, drv->ctx);
3363 }
3365 return 0;
3366 }
3369 static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
3370 {
3371 struct nl_msg *msg;
3373 msg = nlmsg_alloc();
3374 if (!msg)
3375 return -ENOMEM;
3377 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
3378 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3380 return send_and_recv_msgs(drv, msg, NULL, NULL);
3381 nla_put_failure:
3382 nlmsg_free(msg);
3383 return -ENOBUFS;
3384 }
3387 /**
3388 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
3389 * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
3390 *
3391 * Shut down driver interface and processing of driver events. Free
3392 * private data buffer if one was allocated in wpa_driver_nl80211_init().
3393 */
3394 static void wpa_driver_nl80211_deinit(void *priv)
3395 {
3396 struct i802_bss *bss = priv;
3397 struct wpa_driver_nl80211_data *drv = bss->drv;
3399 if (drv->data_tx_status)
3400 eloop_unregister_read_sock(drv->eapol_tx_sock);
3401 if (drv->eapol_tx_sock >= 0)
3402 close(drv->eapol_tx_sock);
3404 if (bss->nl_preq)
3405 wpa_driver_nl80211_probe_req_report(bss, 0);
3406 if (bss->added_if_into_bridge) {
3407 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
3408 bss->ifname) < 0)
3409 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3410 "interface %s from bridge %s: %s",
3411 bss->ifname, bss->brname, strerror(errno));
3412 }
3413 if (bss->added_bridge) {
3414 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
3415 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3416 "bridge %s: %s",
3417 bss->brname, strerror(errno));
3418 }
3420 nl80211_remove_monitor_interface(drv);
3422 if (is_ap_interface(drv->nlmode))
3423 wpa_driver_nl80211_del_beacon(drv);
3425 #ifdef HOSTAPD
3426 if (drv->last_freq_ht) {
3427 /* Clear HT flags from the driver */
3428 struct hostapd_freq_params freq;
3429 os_memset(&freq, 0, sizeof(freq));
3430 freq.freq = drv->last_freq;
3431 i802_set_freq(priv, &freq);
3432 }
3434 if (drv->eapol_sock >= 0) {
3435 eloop_unregister_read_sock(drv->eapol_sock);
3436 close(drv->eapol_sock);
3437 }
3439 if (drv->if_indices != drv->default_if_indices)
3440 os_free(drv->if_indices);
3441 #endif /* HOSTAPD */
3443 if (drv->disabled_11b_rates)
3444 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3446 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
3447 IF_OPER_UP);
3448 rfkill_deinit(drv->rfkill);
3450 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3452 (void) linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
3453 wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
3454 nl80211_mgmt_unsubscribe(bss, "deinit");
3456 nl_cb_put(drv->nl_cb);
3458 nl80211_destroy_bss(&drv->first_bss);
3460 os_free(drv->filter_ssids);
3462 os_free(drv->auth_ie);
3464 if (drv->in_interface_list)
3465 dl_list_del(&drv->list);
3467 os_free(drv);
3468 }
3471 /**
3472 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
3473 * @eloop_ctx: Driver private data
3474 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
3475 *
3476 * This function can be used as registered timeout when starting a scan to
3477 * generate a scan completed event if the driver does not report this.
3478 */
3479 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
3480 {
3481 struct wpa_driver_nl80211_data *drv = eloop_ctx;
3482 if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
3483 wpa_driver_nl80211_set_mode(&drv->first_bss,
3484 drv->ap_scan_as_station);
3485 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
3486 }
3487 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
3488 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
3489 }
3492 /**
3493 * wpa_driver_nl80211_scan - Request the driver to initiate scan
3494 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3495 * @params: Scan parameters
3496 * Returns: 0 on success, -1 on failure
3497 */
3498 static int wpa_driver_nl80211_scan(void *priv,
3499 struct wpa_driver_scan_params *params)
3500 {
3501 struct i802_bss *bss = priv;
3502 struct wpa_driver_nl80211_data *drv = bss->drv;
3503 int ret = 0, timeout;
3504 struct nl_msg *msg, *ssids, *freqs, *rates;
3505 size_t i;
3507 drv->scan_for_auth = 0;
3509 msg = nlmsg_alloc();
3510 ssids = nlmsg_alloc();
3511 freqs = nlmsg_alloc();
3512 rates = nlmsg_alloc();
3513 if (!msg || !ssids || !freqs || !rates) {
3514 nlmsg_free(msg);
3515 nlmsg_free(ssids);
3516 nlmsg_free(freqs);
3517 nlmsg_free(rates);
3518 return -1;
3519 }
3521 os_free(drv->filter_ssids);
3522 drv->filter_ssids = params->filter_ssids;
3523 params->filter_ssids = NULL;
3524 drv->num_filter_ssids = params->num_filter_ssids;
3526 nl80211_cmd(drv, msg, 0, NL80211_CMD_TRIGGER_SCAN);
3528 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3530 for (i = 0; i < params->num_ssids; i++) {
3531 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
3532 params->ssids[i].ssid,
3533 params->ssids[i].ssid_len);
3534 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
3535 params->ssids[i].ssid);
3536 }
3537 if (params->num_ssids)
3538 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
3540 if (params->extra_ies) {
3541 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
3542 params->extra_ies, params->extra_ies_len);
3543 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
3544 params->extra_ies);
3545 }
3547 if (params->freqs) {
3548 for (i = 0; params->freqs[i]; i++) {
3549 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
3550 "MHz", params->freqs[i]);
3551 NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
3552 }
3553 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
3554 }
3556 if (params->p2p_probe) {
3557 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
3559 /*
3560 * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
3561 * by masking out everything else apart from the OFDM rates 6,
3562 * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
3563 * rates are left enabled.
3564 */
3565 NLA_PUT(rates, NL80211_BAND_2GHZ, 8,
3566 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
3567 nla_put_nested(msg, NL80211_ATTR_SCAN_SUPP_RATES, rates);
3569 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
3570 }
3572 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3573 msg = NULL;
3574 if (ret) {
3575 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
3576 "(%s)", ret, strerror(-ret));
3577 #ifdef HOSTAPD
3578 if (is_ap_interface(drv->nlmode)) {
3579 /*
3580 * mac80211 does not allow scan requests in AP mode, so
3581 * try to do this in station mode.
3582 */
3583 if (wpa_driver_nl80211_set_mode(
3584 bss, NL80211_IFTYPE_STATION))
3585 goto nla_put_failure;
3587 if (wpa_driver_nl80211_scan(drv, params)) {
3588 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
3589 goto nla_put_failure;
3590 }
3592 /* Restore AP mode when processing scan results */
3593 drv->ap_scan_as_station = drv->nlmode;
3594 ret = 0;
3595 } else
3596 goto nla_put_failure;
3597 #else /* HOSTAPD */
3598 goto nla_put_failure;
3599 #endif /* HOSTAPD */
3600 }
3602 /* Not all drivers generate "scan completed" wireless event, so try to
3603 * read results after a timeout. */
3604 timeout = 10;
3605 if (drv->scan_complete_events) {
3606 /*
3607 * The driver seems to deliver events to notify when scan is
3608 * complete, so use longer timeout to avoid race conditions
3609 * with scanning and following association request.
3610 */
3611 timeout = 30;
3612 }
3613 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
3614 "seconds", ret, timeout);
3615 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3616 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
3617 drv, drv->ctx);
3619 nla_put_failure:
3620 nlmsg_free(ssids);
3621 nlmsg_free(msg);
3622 nlmsg_free(freqs);
3623 nlmsg_free(rates);
3624 return ret;
3625 }
3628 /**
3629 * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
3630 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3631 * @params: Scan parameters
3632 * @long_interval: interval between scan cycles after end of short cycles
3633 * @short_interval: interval between initial short scan cycles
3634 * @num_short_intervals: number of interval short scan intervals
3635 * Returns: 0 on success, -1 on failure or if not supported
3636 */
3637 static int wpa_driver_nl80211_sched_scan(void *priv,
3638 struct wpa_driver_scan_params *params,
3639 u32 long_interval,
3640 u32 short_interval,
3641 u8 num_short_intervals)
3642 {
3643 struct i802_bss *bss = priv;
3644 struct wpa_driver_nl80211_data *drv = bss->drv;
3645 int ret = 0;
3646 struct nl_msg *msg, *ssids, *freqs, *match_set_ssid, *match_sets;
3647 size_t i;
3649 #ifdef ANDROID
3650 if (!drv->capa.sched_scan_supported)
3651 return android_pno_start(bss, params);
3652 #endif /* ANDROID */
3654 msg = nlmsg_alloc();
3655 ssids = nlmsg_alloc();
3656 freqs = nlmsg_alloc();
3657 if (!msg || !ssids || !freqs) {
3658 nlmsg_free(msg);
3659 nlmsg_free(ssids);
3660 nlmsg_free(freqs);
3661 return -1;
3662 }
3664 os_free(drv->filter_ssids);
3665 drv->filter_ssids = params->filter_ssids;
3666 params->filter_ssids = NULL;
3667 drv->num_filter_ssids = params->num_filter_ssids;
3669 nl80211_cmd(drv, msg, 0, NL80211_CMD_START_SCHED_SCAN);
3671 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3673 NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, long_interval);
3675 if (drv->capa.sched_scan_intervals_supported) {
3676 NLA_PUT_U32(msg,
3677 NL80211_ATTR_SCHED_SCAN_SHORT_INTERVAL,
3678 short_interval);
3680 NLA_PUT_U8(msg,
3681 NL80211_ATTR_SCHED_SCAN_NUM_SHORT_INTERVALS,
3682 num_short_intervals);
3683 }
3685 if (drv->num_filter_ssids &&
3686 (int) drv->num_filter_ssids <= drv->capa.max_match_sets) {
3687 match_sets = nlmsg_alloc();
3689 for (i = 0; i < drv->num_filter_ssids; i++) {
3690 wpa_hexdump_ascii(MSG_MSGDUMP,
3691 "nl80211: Sched scan filter SSID",
3692 drv->filter_ssids[i].ssid,
3693 drv->filter_ssids[i].ssid_len);
3695 match_set_ssid = nlmsg_alloc();
3696 nla_put(match_set_ssid,
3697 NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
3698 drv->filter_ssids[i].ssid_len,
3699 drv->filter_ssids[i].ssid);
3701 nla_put_nested(match_sets, i + 1, match_set_ssid);
3703 nlmsg_free(match_set_ssid);
3704 }
3706 nla_put_nested(msg, NL80211_ATTR_SCHED_SCAN_MATCH,
3707 match_sets);
3708 nlmsg_free(match_sets);
3709 }
3711 for (i = 0; i < params->num_ssids; i++) {
3712 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Sched scan SSID",
3713 params->ssids[i].ssid,
3714 params->ssids[i].ssid_len);
3715 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
3716 params->ssids[i].ssid);
3717 }
3718 if (params->num_ssids)
3719 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
3721 if (params->extra_ies) {
3722 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Sched scan extra IEs",
3723 params->extra_ies, params->extra_ies_len);
3724 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
3725 params->extra_ies);
3726 }
3728 if (params->freqs) {
3729 for (i = 0; params->freqs[i]; i++) {
3730 wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
3731 "MHz", params->freqs[i]);
3732 NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
3733 }
3734 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
3735 }
3737 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3739 /* TODO: if we get an error here, we should fall back to normal scan */
3741 msg = NULL;
3742 if (ret) {
3743 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
3744 "ret=%d (%s)", ret, strerror(-ret));
3745 goto nla_put_failure;
3746 }
3748 wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) "
3749 "intervals: short=%d ms long=%d ms num_short_intervals=%d"
3750 , ret, short_interval, long_interval, num_short_intervals);
3752 nla_put_failure:
3753 nlmsg_free(ssids);
3754 nlmsg_free(msg);
3755 nlmsg_free(freqs);
3756 return ret;
3757 }
3760 /**
3761 * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
3762 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3763 * Returns: 0 on success, -1 on failure or if not supported
3764 */
3765 static int wpa_driver_nl80211_stop_sched_scan(void *priv)
3766 {
3767 struct i802_bss *bss = priv;
3768 struct wpa_driver_nl80211_data *drv = bss->drv;
3769 int ret = 0;
3770 struct nl_msg *msg;
3772 #ifdef ANDROID
3773 if (!drv->capa.sched_scan_supported)
3774 return android_pno_stop(bss);
3775 #endif /* ANDROID */
3777 msg = nlmsg_alloc();
3778 if (!msg)
3779 return -1;
3781 nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
3783 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3785 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3786 msg = NULL;
3787 if (ret) {
3788 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
3789 "ret=%d (%s)", ret, strerror(-ret));
3790 goto nla_put_failure;
3791 }
3793 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
3795 nla_put_failure:
3796 nlmsg_free(msg);
3797 return ret;
3798 }
3801 static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
3802 {
3803 const u8 *end, *pos;
3805 if (ies == NULL)
3806 return NULL;
3808 pos = ies;
3809 end = ies + ies_len;
3811 while (pos + 1 < end) {
3812 if (pos + 2 + pos[1] > end)
3813 break;
3814 if (pos[0] == ie)
3815 return pos;
3816 pos += 2 + pos[1];
3817 }
3819 return NULL;
3820 }
3823 static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
3824 const u8 *ie, size_t ie_len)
3825 {
3826 const u8 *ssid;
3827 size_t i;
3829 if (drv->filter_ssids == NULL)
3830 return 0;
3832 ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
3833 if (ssid == NULL)
3834 return 1;
3836 for (i = 0; i < drv->num_filter_ssids; i++) {
3837 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
3838 os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
3839 0)
3840 return 0;
3841 }
3843 return 1;
3844 }
3847 static int bss_info_handler(struct nl_msg *msg, void *arg)
3848 {
3849 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3850 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3851 struct nlattr *bss[NL80211_BSS_MAX + 1];
3852 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
3853 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
3854 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
3855 [NL80211_BSS_TSF] = { .type = NLA_U64 },
3856 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
3857 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
3858 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
3859 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
3860 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
3861 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
3862 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
3863 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
3864 };
3865 struct nl80211_bss_info_arg *_arg = arg;
3866 struct wpa_scan_results *res = _arg->res;
3867 struct wpa_scan_res **tmp;
3868 struct wpa_scan_res *r;
3869 const u8 *ie, *beacon_ie;
3870 size_t ie_len, beacon_ie_len;
3871 u8 *pos;
3872 size_t i;
3874 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3875 genlmsg_attrlen(gnlh, 0), NULL);
3876 if (!tb[NL80211_ATTR_BSS])
3877 return NL_SKIP;
3878 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
3879 bss_policy))
3880 return NL_SKIP;
3881 if (bss[NL80211_BSS_STATUS]) {
3882 enum nl80211_bss_status status;
3883 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
3884 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
3885 bss[NL80211_BSS_FREQUENCY]) {
3886 _arg->assoc_freq =
3887 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
3888 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
3889 _arg->assoc_freq);
3890 }
3891 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
3892 bss[NL80211_BSS_BSSID]) {
3893 os_memcpy(_arg->assoc_bssid,
3894 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
3895 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
3896 MACSTR, MAC2STR(_arg->assoc_bssid));
3897 }
3898 }
3899 if (!res)
3900 return NL_SKIP;
3901 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
3902 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
3903 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
3904 } else {
3905 ie = NULL;
3906 ie_len = 0;
3907 }
3908 if (bss[NL80211_BSS_BEACON_IES]) {
3909 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
3910 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
3911 } else {
3912 beacon_ie = NULL;
3913 beacon_ie_len = 0;
3914 }
3916 if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
3917 ie ? ie_len : beacon_ie_len))
3918 return NL_SKIP;
3920 r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
3921 if (r == NULL)
3922 return NL_SKIP;
3923 if (bss[NL80211_BSS_BSSID])
3924 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
3925 ETH_ALEN);
3926 if (bss[NL80211_BSS_FREQUENCY])
3927 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
3928 if (bss[NL80211_BSS_BEACON_INTERVAL])
3929 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
3930 if (bss[NL80211_BSS_CAPABILITY])
3931 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
3932 r->flags |= WPA_SCAN_NOISE_INVALID;
3933 if (bss[NL80211_BSS_SIGNAL_MBM]) {
3934 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
3935 r->level /= 100; /* mBm to dBm */
3936 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
3937 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
3938 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
3939 r->flags |= WPA_SCAN_QUAL_INVALID;
3940 } else
3941 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
3942 if (bss[NL80211_BSS_TSF])
3943 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
3944 if (bss[NL80211_BSS_SEEN_MS_AGO])
3945 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
3946 r->ie_len = ie_len;
3947 pos = (u8 *) (r + 1);
3948 if (ie) {
3949 os_memcpy(pos, ie, ie_len);
3950 pos += ie_len;
3951 }
3952 r->beacon_ie_len = beacon_ie_len;
3953 if (beacon_ie)
3954 os_memcpy(pos, beacon_ie, beacon_ie_len);
3956 if (bss[NL80211_BSS_STATUS]) {
3957 enum nl80211_bss_status status;
3958 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
3959 switch (status) {
3960 case NL80211_BSS_STATUS_AUTHENTICATED:
3961 r->flags |= WPA_SCAN_AUTHENTICATED;
3962 break;
3963 case NL80211_BSS_STATUS_ASSOCIATED:
3964 r->flags |= WPA_SCAN_ASSOCIATED;
3965 break;
3966 default:
3967 break;
3968 }
3969 }
3971 /*
3972 * cfg80211 maintains separate BSS table entries for APs if the same
3973 * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
3974 * not use frequency as a separate key in the BSS table, so filter out
3975 * duplicated entries. Prefer associated BSS entry in such a case in
3976 * order to get the correct frequency into the BSS table.
3977 */
3978 for (i = 0; i < res->num; i++) {
3979 const u8 *s1, *s2;
3980 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
3981 continue;
3983 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
3984 res->res[i]->ie_len, WLAN_EID_SSID);
3985 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
3986 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
3987 os_memcmp(s1, s2, 2 + s1[1]) != 0)
3988 continue;
3990 /* Same BSSID,SSID was already included in scan results */
3991 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
3992 "for " MACSTR, MAC2STR(r->bssid));
3994 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
3995 !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
3996 os_free(res->res[i]);
3997 res->res[i] = r;
3998 } else
3999 os_free(r);
4000 return NL_SKIP;
4001 }
4003 tmp = os_realloc(res->res,
4004 (res->num + 1) * sizeof(struct wpa_scan_res *));
4005 if (tmp == NULL) {
4006 os_free(r);
4007 return NL_SKIP;
4008 }
4009 tmp[res->num++] = r;
4010 res->res = tmp;
4012 return NL_SKIP;
4013 }
4016 static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
4017 const u8 *addr)
4018 {
4019 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
4020 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
4021 "mismatch (" MACSTR ")", MAC2STR(addr));
4022 wpa_driver_nl80211_mlme(drv, addr,
4023 NL80211_CMD_DEAUTHENTICATE,
4024 WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
4025 }
4026 }
4029 static void wpa_driver_nl80211_check_bss_status(
4030 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
4031 {
4032 size_t i;
4034 for (i = 0; i < res->num; i++) {
4035 struct wpa_scan_res *r = res->res[i];
4036 if (r->flags & WPA_SCAN_AUTHENTICATED) {
4037 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4038 "indicates BSS status with " MACSTR
4039 " as authenticated",
4040 MAC2STR(r->bssid));
4041 if (is_sta_interface(drv->nlmode) &&
4042 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
4043 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
4044 0) {
4045 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
4046 " in local state (auth=" MACSTR
4047 " assoc=" MACSTR ")",
4048 MAC2STR(drv->auth_bssid),
4049 MAC2STR(drv->bssid));
4050 clear_state_mismatch(drv, r->bssid);
4051 }
4052 }
4054 if (r->flags & WPA_SCAN_ASSOCIATED) {
4055 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
4056 "indicate BSS status with " MACSTR
4057 " as associated",
4058 MAC2STR(r->bssid));
4059 if (is_sta_interface(drv->nlmode) &&
4060 !drv->associated) {
4061 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4062 "(not associated) does not match "
4063 "with BSS state");
4064 clear_state_mismatch(drv, r->bssid);
4065 } else if (is_sta_interface(drv->nlmode) &&
4066 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
4067 0) {
4068 wpa_printf(MSG_DEBUG, "nl80211: Local state "
4069 "(associated with " MACSTR ") does "
4070 "not match with BSS state",
4071 MAC2STR(drv->bssid));
4072 clear_state_mismatch(drv, r->bssid);
4073 clear_state_mismatch(drv, drv->bssid);
4074 }
4075 }
4076 }
4077 }
4080 static struct wpa_scan_results *
4081 nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
4082 {
4083 struct nl_msg *msg;
4084 struct wpa_scan_results *res;
4085 int ret;
4086 struct nl80211_bss_info_arg arg;
4088 res = os_zalloc(sizeof(*res));
4089 if (res == NULL)
4090 return NULL;
4091 msg = nlmsg_alloc();
4092 if (!msg)
4093 goto nla_put_failure;
4095 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
4096 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4098 arg.drv = drv;
4099 arg.res = res;
4100 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
4101 msg = NULL;
4102 if (ret == 0) {
4103 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
4104 "BSSes)", (unsigned long) res->num);
4105 nl80211_get_noise_for_scan_results(drv, res);
4106 return res;
4107 }
4108 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
4109 "(%s)", ret, strerror(-ret));
4110 nla_put_failure:
4111 nlmsg_free(msg);
4112 wpa_scan_results_free(res);
4113 return NULL;
4114 }
4117 /**
4118 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
4119 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
4120 * Returns: Scan results on success, -1 on failure
4121 */
4122 static struct wpa_scan_results *
4123 wpa_driver_nl80211_get_scan_results(void *priv)
4124 {
4125 struct i802_bss *bss = priv;
4126 struct wpa_driver_nl80211_data *drv = bss->drv;
4127 struct wpa_scan_results *res;
4129 res = nl80211_get_scan_results(drv);
4130 if (res)
4131 wpa_driver_nl80211_check_bss_status(drv, res);
4132 return res;
4133 }
4136 static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
4137 {
4138 struct wpa_scan_results *res;
4139 size_t i;
4141 res = nl80211_get_scan_results(drv);
4142 if (res == NULL) {
4143 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
4144 return;
4145 }
4147 wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
4148 for (i = 0; i < res->num; i++) {
4149 struct wpa_scan_res *r = res->res[i];
4150 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
4151 (int) i, (int) res->num, MAC2STR(r->bssid),
4152 r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
4153 r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
4154 }
4156 wpa_scan_results_free(res);
4157 }
4160 static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
4161 enum wpa_alg alg, const u8 *addr,
4162 int key_idx, int set_tx,
4163 const u8 *seq, size_t seq_len,
4164 const u8 *key, size_t key_len)
4165 {
4166 struct i802_bss *bss = priv;
4167 struct wpa_driver_nl80211_data *drv = bss->drv;
4168 int ifindex = if_nametoindex(ifname);
4169 struct nl_msg *msg;
4170 int ret;
4172 wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
4173 "set_tx=%d seq_len=%lu key_len=%lu",
4174 __func__, ifindex, alg, addr, key_idx, set_tx,
4175 (unsigned long) seq_len, (unsigned long) key_len);
4176 #ifdef CONFIG_TDLS
4177 if (key_idx == -1)
4178 key_idx = 0;
4179 #endif /* CONFIG_TDLS */
4181 msg = nlmsg_alloc();
4182 if (!msg)
4183 return -ENOMEM;
4185 if (alg == WPA_ALG_NONE) {
4186 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
4187 } else {
4188 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
4189 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
4190 switch (alg) {
4191 case WPA_ALG_WEP:
4192 if (key_len == 5)
4193 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4194 WLAN_CIPHER_SUITE_WEP40);
4195 else
4196 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4197 WLAN_CIPHER_SUITE_WEP104);
4198 break;
4199 case WPA_ALG_TKIP:
4200 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4201 WLAN_CIPHER_SUITE_TKIP);
4202 break;
4203 case WPA_ALG_CCMP:
4204 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4205 WLAN_CIPHER_SUITE_CCMP);
4206 break;
4207 case WPA_ALG_IGTK:
4208 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4209 WLAN_CIPHER_SUITE_AES_CMAC);
4210 break;
4211 default:
4212 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4213 "algorithm %d", __func__, alg);
4214 nlmsg_free(msg);
4215 return -1;
4216 }
4217 }
4219 if (seq && seq_len)
4220 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
4222 if (addr && !is_broadcast_ether_addr(addr)) {
4223 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
4224 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4226 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
4227 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
4228 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
4229 NL80211_KEYTYPE_GROUP);
4230 }
4231 } else if (addr && is_broadcast_ether_addr(addr)) {
4232 struct nl_msg *types;
4233 int err;
4234 wpa_printf(MSG_DEBUG, " broadcast key");
4235 types = nlmsg_alloc();
4236 if (!types)
4237 goto nla_put_failure;
4238 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4239 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4240 types);
4241 nlmsg_free(types);
4242 if (err)
4243 goto nla_put_failure;
4244 }
4245 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4246 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4248 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4249 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
4250 ret = 0;
4251 if (ret)
4252 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
4253 ret, strerror(-ret));
4255 /*
4256 * If we failed or don't need to set the default TX key (below),
4257 * we're done here.
4258 */
4259 if (ret || !set_tx || alg == WPA_ALG_NONE)
4260 return ret;
4261 if (is_ap_interface(drv->nlmode) && addr &&
4262 !is_broadcast_ether_addr(addr))
4263 return ret;
4265 msg = nlmsg_alloc();
4266 if (!msg)
4267 return -ENOMEM;
4269 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
4270 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4271 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4272 if (alg == WPA_ALG_IGTK)
4273 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
4274 else
4275 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
4276 if (addr && is_broadcast_ether_addr(addr)) {
4277 struct nl_msg *types;
4278 int err;
4279 types = nlmsg_alloc();
4280 if (!types)
4281 goto nla_put_failure;
4282 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4283 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4284 types);
4285 nlmsg_free(types);
4286 if (err)
4287 goto nla_put_failure;
4288 } else if (addr) {
4289 struct nl_msg *types;
4290 int err;
4291 types = nlmsg_alloc();
4292 if (!types)
4293 goto nla_put_failure;
4294 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST);
4295 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4296 types);
4297 nlmsg_free(types);
4298 if (err)
4299 goto nla_put_failure;
4300 }
4302 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4303 if (ret == -ENOENT)
4304 ret = 0;
4305 if (ret)
4306 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
4307 "err=%d %s)", ret, strerror(-ret));
4308 return ret;
4310 nla_put_failure:
4311 nlmsg_free(msg);
4312 return -ENOBUFS;
4313 }
4316 static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
4317 int key_idx, int defkey,
4318 const u8 *seq, size_t seq_len,
4319 const u8 *key, size_t key_len)
4320 {
4321 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
4322 if (!key_attr)
4323 return -1;
4325 if (defkey && alg == WPA_ALG_IGTK)
4326 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
4327 else if (defkey)
4328 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4330 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
4332 switch (alg) {
4333 case WPA_ALG_WEP:
4334 if (key_len == 5)
4335 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4336 WLAN_CIPHER_SUITE_WEP40);
4337 else
4338 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4339 WLAN_CIPHER_SUITE_WEP104);
4340 break;
4341 case WPA_ALG_TKIP:
4342 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
4343 break;
4344 case WPA_ALG_CCMP:
4345 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
4346 break;
4347 case WPA_ALG_IGTK:
4348 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4349 WLAN_CIPHER_SUITE_AES_CMAC);
4350 break;
4351 default:
4352 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4353 "algorithm %d", __func__, alg);
4354 return -1;
4355 }
4357 if (seq && seq_len)
4358 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
4360 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
4362 nla_nest_end(msg, key_attr);
4364 return 0;
4365 nla_put_failure:
4366 return -1;
4367 }
4370 static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
4371 struct nl_msg *msg)
4372 {
4373 int i, privacy = 0;
4374 struct nlattr *nl_keys, *nl_key;
4376 for (i = 0; i < 4; i++) {
4377 if (!params->wep_key[i])
4378 continue;
4379 privacy = 1;
4380 break;
4381 }
4382 if (params->wps == WPS_MODE_PRIVACY)
4383 privacy = 1;
4384 if (params->pairwise_suite &&
4385 params->pairwise_suite != WPA_CIPHER_NONE)
4386 privacy = 1;
4388 if (!privacy)
4389 return 0;
4391 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
4393 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
4394 if (!nl_keys)
4395 goto nla_put_failure;
4397 for (i = 0; i < 4; i++) {
4398 if (!params->wep_key[i])
4399 continue;
4401 nl_key = nla_nest_start(msg, i);
4402 if (!nl_key)
4403 goto nla_put_failure;
4405 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
4406 params->wep_key[i]);
4407 if (params->wep_key_len[i] == 5)
4408 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4409 WLAN_CIPHER_SUITE_WEP40);
4410 else
4411 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4412 WLAN_CIPHER_SUITE_WEP104);
4414 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
4416 if (i == params->wep_tx_keyidx)
4417 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4419 nla_nest_end(msg, nl_key);
4420 }
4421 nla_nest_end(msg, nl_keys);
4423 return 0;
4425 nla_put_failure:
4426 return -ENOBUFS;
4427 }
4430 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
4431 const u8 *addr, int cmd, u16 reason_code,
4432 int local_state_change)
4433 {
4434 int ret = -1;
4435 struct nl_msg *msg;
4437 msg = nlmsg_alloc();
4438 if (!msg)
4439 return -1;
4441 nl80211_cmd(drv, msg, 0, cmd);
4443 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4444 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
4445 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4446 if (local_state_change)
4447 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
4449 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4450 msg = NULL;
4451 if (ret) {
4452 wpa_dbg(drv->ctx, MSG_DEBUG,
4453 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
4454 reason_code, ret, strerror(-ret));
4455 goto nla_put_failure;
4456 }
4457 ret = 0;
4459 nla_put_failure:
4460 nlmsg_free(msg);
4461 return ret;
4462 }
4465 static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
4466 const u8 *addr, int reason_code)
4467 {
4468 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
4469 __func__, MAC2STR(addr), reason_code);
4470 drv->associated = 0;
4471 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT,
4472 reason_code, 0);
4473 }
4476 static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
4477 int reason_code)
4478 {
4479 struct i802_bss *bss = priv;
4480 struct wpa_driver_nl80211_data *drv = bss->drv;
4481 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
4482 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
4483 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
4484 __func__, MAC2STR(addr), reason_code);
4485 drv->associated = 0;
4486 if (drv->nlmode == NL80211_IFTYPE_ADHOC)
4487 return nl80211_leave_ibss(drv);
4488 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
4489 reason_code, 0);
4490 }
4493 static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
4494 int reason_code)
4495 {
4496 struct i802_bss *bss = priv;
4497 struct wpa_driver_nl80211_data *drv = bss->drv;
4498 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
4499 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
4500 wpa_printf(MSG_DEBUG, "%s", __func__);
4501 drv->associated = 0;
4502 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
4503 reason_code, 0);
4504 }
4507 static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
4508 struct wpa_driver_auth_params *params)
4509 {
4510 int i;
4512 drv->auth_freq = params->freq;
4513 drv->auth_alg = params->auth_alg;
4514 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
4515 drv->auth_local_state_change = params->local_state_change;
4516 drv->auth_p2p = params->p2p;
4518 if (params->bssid)
4519 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
4520 else
4521 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
4523 if (params->ssid) {
4524 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
4525 drv->auth_ssid_len = params->ssid_len;
4526 } else
4527 drv->auth_ssid_len = 0;
4530 os_free(drv->auth_ie);
4531 drv->auth_ie = NULL;
4532 drv->auth_ie_len = 0;
4533 if (params->ie) {
4534 drv->auth_ie = os_malloc(params->ie_len);
4535 if (drv->auth_ie) {
4536 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
4537 drv->auth_ie_len = params->ie_len;
4538 }
4539 }
4541 for (i = 0; i < 4; i++) {
4542 if (params->wep_key[i] && params->wep_key_len[i] &&
4543 params->wep_key_len[i] <= 16) {
4544 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
4545 params->wep_key_len[i]);
4546 drv->auth_wep_key_len[i] = params->wep_key_len[i];
4547 } else
4548 drv->auth_wep_key_len[i] = 0;
4549 }
4550 }
4553 static int wpa_driver_nl80211_authenticate(
4554 void *priv, struct wpa_driver_auth_params *params)
4555 {
4556 struct i802_bss *bss = priv;
4557 struct wpa_driver_nl80211_data *drv = bss->drv;
4558 int ret = -1, i;
4559 struct nl_msg *msg;
4560 enum nl80211_auth_type type;
4561 enum nl80211_iftype nlmode;
4562 int count = 0;
4563 int is_retry;
4565 is_retry = drv->retry_auth;
4566 drv->retry_auth = 0;
4568 drv->associated = 0;
4569 os_memset(drv->auth_bssid, 0, ETH_ALEN);
4570 /* FIX: IBSS mode */
4571 nlmode = params->p2p ?
4572 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4573 if (drv->nlmode != nlmode &&
4574 wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
4575 return -1;
4577 retry:
4578 msg = nlmsg_alloc();
4579 if (!msg)
4580 return -1;
4582 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
4583 drv->ifindex);
4585 nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
4587 for (i = 0; i < 4; i++) {
4588 if (!params->wep_key[i])
4589 continue;
4590 wpa_driver_nl80211_set_key(bss->ifname, priv, WPA_ALG_WEP,
4591 NULL, i,
4592 i == params->wep_tx_keyidx, NULL, 0,
4593 params->wep_key[i],
4594 params->wep_key_len[i]);
4595 if (params->wep_tx_keyidx != i)
4596 continue;
4597 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
4598 params->wep_key[i], params->wep_key_len[i])) {
4599 nlmsg_free(msg);
4600 return -1;
4601 }
4602 }
4604 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4605 if (params->bssid) {
4606 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4607 MAC2STR(params->bssid));
4608 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
4609 }
4610 if (params->freq) {
4611 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
4612 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4613 }
4614 if (params->ssid) {
4615 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4616 params->ssid, params->ssid_len);
4617 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
4618 params->ssid);
4619 }
4620 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
4621 if (params->ie)
4622 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
4623 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4624 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4625 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4626 type = NL80211_AUTHTYPE_SHARED_KEY;
4627 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4628 type = NL80211_AUTHTYPE_NETWORK_EAP;
4629 else if (params->auth_alg & WPA_AUTH_ALG_FT)
4630 type = NL80211_AUTHTYPE_FT;
4631 else
4632 goto nla_put_failure;
4633 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
4634 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
4635 if (params->local_state_change) {
4636 wpa_printf(MSG_DEBUG, " * Local state change only");
4637 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
4638 }
4640 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4641 msg = NULL;
4642 if (ret) {
4643 wpa_dbg(drv->ctx, MSG_DEBUG,
4644 "nl80211: MLME command failed (auth): ret=%d (%s)",
4645 ret, strerror(-ret));
4646 count++;
4647 if (ret == -EALREADY && count == 1 && params->bssid &&
4648 !params->local_state_change) {
4649 /*
4650 * mac80211 does not currently accept new
4651 * authentication if we are already authenticated. As a
4652 * workaround, force deauthentication and try again.
4653 */
4654 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
4655 "after forced deauthentication");
4656 wpa_driver_nl80211_deauthenticate(
4657 bss, params->bssid,
4658 WLAN_REASON_PREV_AUTH_NOT_VALID);
4659 nlmsg_free(msg);
4660 goto retry;
4661 }
4663 if (ret == -ENOENT && params->freq && !is_retry) {
4664 /*
4665 * cfg80211 has likely expired the BSS entry even
4666 * though it was previously available in our internal
4667 * BSS table. To recover quickly, start a single
4668 * channel scan on the specified channel.
4669 */
4670 struct wpa_driver_scan_params scan;
4671 int freqs[2];
4673 os_memset(&scan, 0, sizeof(scan));
4674 scan.num_ssids = 1;
4675 if (params->ssid) {
4676 scan.ssids[0].ssid = params->ssid;
4677 scan.ssids[0].ssid_len = params->ssid_len;
4678 }
4679 freqs[0] = params->freq;
4680 freqs[1] = 0;
4681 scan.freqs = freqs;
4682 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
4683 "channel scan to refresh cfg80211 BSS "
4684 "entry");
4685 ret = wpa_driver_nl80211_scan(bss, &scan);
4686 if (ret == 0) {
4687 nl80211_copy_auth_params(drv, params);
4688 drv->scan_for_auth = 1;
4689 }
4690 } else if (is_retry) {
4691 /*
4692 * Need to indicate this with an event since the return
4693 * value from the retry is not delivered to core code.
4694 */
4695 union wpa_event_data event;
4696 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
4697 "failed");
4698 os_memset(&event, 0, sizeof(event));
4699 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
4700 ETH_ALEN);
4701 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
4702 &event);
4703 }
4705 goto nla_put_failure;
4706 }
4707 ret = 0;
4708 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
4709 "successfully");
4711 nla_put_failure:
4712 nlmsg_free(msg);
4713 return ret;
4714 }
4717 static int wpa_driver_nl80211_authenticate_retry(
4718 struct wpa_driver_nl80211_data *drv)
4719 {
4720 struct wpa_driver_auth_params params;
4721 struct i802_bss *bss = &drv->first_bss;
4722 int i;
4724 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
4726 os_memset(¶ms, 0, sizeof(params));
4727 params.freq = drv->auth_freq;
4728 params.auth_alg = drv->auth_alg;
4729 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
4730 params.local_state_change = drv->auth_local_state_change;
4731 params.p2p = drv->auth_p2p;
4733 if (!is_zero_ether_addr(drv->auth_bssid_))
4734 params.bssid = drv->auth_bssid_;
4736 if (drv->auth_ssid_len) {
4737 params.ssid = drv->auth_ssid;
4738 params.ssid_len = drv->auth_ssid_len;
4739 }
4741 params.ie = drv->auth_ie;
4742 params.ie_len = drv->auth_ie_len;
4744 for (i = 0; i < 4; i++) {
4745 if (drv->auth_wep_key_len[i]) {
4746 params.wep_key[i] = drv->auth_wep_key[i];
4747 params.wep_key_len[i] = drv->auth_wep_key_len[i];
4748 }
4749 }
4751 drv->retry_auth = 1;
4752 return wpa_driver_nl80211_authenticate(bss, ¶ms);
4753 }
4756 struct phy_info_arg {
4757 u16 *num_modes;
4758 struct hostapd_hw_modes *modes;
4759 };
4761 static int phy_info_handler(struct nl_msg *msg, void *arg)
4762 {
4763 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
4764 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4765 struct phy_info_arg *phy_info = arg;
4767 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
4769 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
4770 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
4771 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
4772 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
4773 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
4774 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
4775 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
4776 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
4777 };
4779 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
4780 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
4781 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
4782 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
4783 };
4785 struct nlattr *nl_band;
4786 struct nlattr *nl_freq;
4787 struct nlattr *nl_rate;
4788 int rem_band, rem_freq, rem_rate;
4789 struct hostapd_hw_modes *mode;
4790 int idx, mode_is_set;
4792 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4793 genlmsg_attrlen(gnlh, 0), NULL);
4795 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
4796 return NL_SKIP;
4798 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
4799 mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
4800 if (!mode)
4801 return NL_SKIP;
4802 phy_info->modes = mode;
4804 mode_is_set = 0;
4806 mode = &phy_info->modes[*(phy_info->num_modes)];
4807 memset(mode, 0, sizeof(*mode));
4808 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN;
4809 *(phy_info->num_modes) += 1;
4811 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
4812 nla_len(nl_band), NULL);
4814 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
4815 mode->ht_capab = nla_get_u16(
4816 tb_band[NL80211_BAND_ATTR_HT_CAPA]);
4817 }
4819 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
4820 mode->a_mpdu_params |= nla_get_u8(
4821 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
4822 0x03;
4823 }
4825 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
4826 mode->a_mpdu_params |= nla_get_u8(
4827 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
4828 2;
4829 }
4831 if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
4832 nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
4833 u8 *mcs;
4834 mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
4835 os_memcpy(mode->mcs_set, mcs, 16);
4836 }
4838 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
4839 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
4840 nla_len(nl_freq), freq_policy);
4841 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
4842 continue;
4843 mode->num_channels++;
4844 }
4846 mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data));
4847 if (!mode->channels)
4848 return NL_SKIP;
4850 idx = 0;
4852 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
4853 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
4854 nla_len(nl_freq), freq_policy);
4855 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
4856 continue;
4858 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
4859 mode->channels[idx].flag = 0;
4861 if (!mode_is_set) {
4862 /* crude heuristic */
4863 if (mode->channels[idx].freq < 4000)
4864 mode->mode = HOSTAPD_MODE_IEEE80211B;
4865 else
4866 mode->mode = HOSTAPD_MODE_IEEE80211A;
4867 mode_is_set = 1;
4868 }
4870 /* crude heuristic */
4871 if (mode->channels[idx].freq < 4000)
4872 if (mode->channels[idx].freq == 2484)
4873 mode->channels[idx].chan = 14;
4874 else
4875 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
4876 else
4877 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
4879 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
4880 mode->channels[idx].flag |=
4881 HOSTAPD_CHAN_DISABLED;
4882 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
4883 mode->channels[idx].flag |=
4884 HOSTAPD_CHAN_PASSIVE_SCAN;
4885 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
4886 mode->channels[idx].flag |=
4887 HOSTAPD_CHAN_NO_IBSS;
4888 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
4889 mode->channels[idx].flag |=
4890 HOSTAPD_CHAN_RADAR;
4892 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
4893 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
4894 mode->channels[idx].max_tx_power =
4895 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
4897 idx++;
4898 }
4900 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
4901 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
4902 nla_len(nl_rate), rate_policy);
4903 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
4904 continue;
4905 mode->num_rates++;
4906 }
4908 mode->rates = os_zalloc(mode->num_rates * sizeof(int));
4909 if (!mode->rates)
4910 return NL_SKIP;
4912 idx = 0;
4914 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
4915 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
4916 nla_len(nl_rate), rate_policy);
4917 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
4918 continue;
4919 mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
4921 /* crude heuristic */
4922 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
4923 mode->rates[idx] > 200)
4924 mode->mode = HOSTAPD_MODE_IEEE80211G;
4926 idx++;
4927 }
4928 }
4930 return NL_SKIP;
4931 }
4933 static struct hostapd_hw_modes *
4934 wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
4935 {
4936 u16 m;
4937 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
4938 int i, mode11g_idx = -1;
4940 /* If only 802.11g mode is included, use it to construct matching
4941 * 802.11b mode data. */
4943 for (m = 0; m < *num_modes; m++) {
4944 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
4945 return modes; /* 802.11b already included */
4946 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
4947 mode11g_idx = m;
4948 }
4950 if (mode11g_idx < 0)
4951 return modes; /* 2.4 GHz band not supported at all */
4953 nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
4954 if (nmodes == NULL)
4955 return modes; /* Could not add 802.11b mode */
4957 mode = &nmodes[*num_modes];
4958 os_memset(mode, 0, sizeof(*mode));
4959 (*num_modes)++;
4960 modes = nmodes;
4962 mode->mode = HOSTAPD_MODE_IEEE80211B;
4964 mode11g = &modes[mode11g_idx];
4965 mode->num_channels = mode11g->num_channels;
4966 mode->channels = os_malloc(mode11g->num_channels *
4967 sizeof(struct hostapd_channel_data));
4968 if (mode->channels == NULL) {
4969 (*num_modes)--;
4970 return modes; /* Could not add 802.11b mode */
4971 }
4972 os_memcpy(mode->channels, mode11g->channels,
4973 mode11g->num_channels * sizeof(struct hostapd_channel_data));
4975 mode->num_rates = 0;
4976 mode->rates = os_malloc(4 * sizeof(int));
4977 if (mode->rates == NULL) {
4978 os_free(mode->channels);
4979 (*num_modes)--;
4980 return modes; /* Could not add 802.11b mode */
4981 }
4983 for (i = 0; i < mode11g->num_rates; i++) {
4984 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
4985 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
4986 continue;
4987 mode->rates[mode->num_rates] = mode11g->rates[i];
4988 mode->num_rates++;
4989 if (mode->num_rates == 4)
4990 break;
4991 }
4993 if (mode->num_rates == 0) {
4994 os_free(mode->channels);
4995 os_free(mode->rates);
4996 (*num_modes)--;
4997 return modes; /* No 802.11b rates */
4998 }
5000 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
5001 "information");
5003 return modes;
5004 }
5007 static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
5008 int end)
5009 {
5010 int c;
5012 for (c = 0; c < mode->num_channels; c++) {
5013 struct hostapd_channel_data *chan = &mode->channels[c];
5014 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
5015 chan->flag |= HOSTAPD_CHAN_HT40;
5016 }
5017 }
5020 static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
5021 int end)
5022 {
5023 int c;
5025 for (c = 0; c < mode->num_channels; c++) {
5026 struct hostapd_channel_data *chan = &mode->channels[c];
5027 if (!(chan->flag & HOSTAPD_CHAN_HT40))
5028 continue;
5029 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
5030 chan->flag |= HOSTAPD_CHAN_HT40MINUS;
5031 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
5032 chan->flag |= HOSTAPD_CHAN_HT40PLUS;
5033 }
5034 }
5037 static void nl80211_reg_rule_ht40(struct nlattr *tb[],
5038 struct phy_info_arg *results)
5039 {
5040 u32 start, end, max_bw;
5041 u16 m;
5043 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5044 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5045 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5046 return;
5048 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5049 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5050 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5052 wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
5053 start, end, max_bw);
5054 if (max_bw < 40)
5055 return;
5057 for (m = 0; m < *results->num_modes; m++) {
5058 if (!(results->modes[m].ht_capab &
5059 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5060 continue;
5061 nl80211_set_ht40_mode(&results->modes[m], start, end);
5062 }
5063 }
5066 static void nl80211_reg_rule_sec(struct nlattr *tb[],
5067 struct phy_info_arg *results)
5068 {
5069 u32 start, end, max_bw;
5070 u16 m;
5072 if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
5073 tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
5074 tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
5075 return;
5077 start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
5078 end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
5079 max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
5081 if (max_bw < 20)
5082 return;
5084 for (m = 0; m < *results->num_modes; m++) {
5085 if (!(results->modes[m].ht_capab &
5086 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
5087 continue;
5088 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
5089 }
5090 }
5093 static int nl80211_get_reg(struct nl_msg *msg, void *arg)
5094 {
5095 struct phy_info_arg *results = arg;
5096 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5097 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5098 struct nlattr *nl_rule;
5099 struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
5100 int rem_rule;
5101 static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5102 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
5103 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
5104 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
5105 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
5106 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
5107 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
5108 };
5110 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5111 genlmsg_attrlen(gnlh, 0), NULL);
5112 if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
5113 !tb_msg[NL80211_ATTR_REG_RULES]) {
5114 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
5115 "available");
5116 return NL_SKIP;
5117 }
5119 wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
5120 (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
5122 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5123 {
5124 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5125 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5126 nl80211_reg_rule_ht40(tb_rule, results);
5127 }
5129 nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5130 {
5131 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5132 nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5133 nl80211_reg_rule_sec(tb_rule, results);
5134 }
5136 return NL_SKIP;
5137 }
5140 static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
5141 struct phy_info_arg *results)
5142 {
5143 struct nl_msg *msg;
5145 msg = nlmsg_alloc();
5146 if (!msg)
5147 return -ENOMEM;
5149 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
5150 return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
5151 }
5154 static struct hostapd_hw_modes *
5155 wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
5156 {
5157 struct i802_bss *bss = priv;
5158 struct wpa_driver_nl80211_data *drv = bss->drv;
5159 struct nl_msg *msg;
5160 struct phy_info_arg result = {
5161 .num_modes = num_modes,
5162 .modes = NULL,
5163 };
5165 *num_modes = 0;
5166 *flags = 0;
5168 msg = nlmsg_alloc();
5169 if (!msg)
5170 return NULL;
5172 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
5174 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5176 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
5177 nl80211_set_ht40_flags(drv, &result);
5178 return wpa_driver_nl80211_add_11b(result.modes, num_modes);
5179 }
5180 msg = NULL;
5181 nla_put_failure:
5182 nlmsg_free(msg);
5183 return NULL;
5184 }
5187 static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
5188 const void *data, size_t len,
5189 int encrypt, int noack)
5190 {
5191 __u8 rtap_hdr[] = {
5192 0x00, 0x00, /* radiotap version */
5193 0x0e, 0x00, /* radiotap length */
5194 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
5195 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
5196 0x00, /* padding */
5197 0x00, 0x00, /* RX and TX flags to indicate that */
5198 0x00, 0x00, /* this is the injected frame directly */
5199 };
5200 struct iovec iov[2] = {
5201 {
5202 .iov_base = &rtap_hdr,
5203 .iov_len = sizeof(rtap_hdr),
5204 },
5205 {
5206 .iov_base = (void *) data,
5207 .iov_len = len,
5208 }
5209 };
5210 struct msghdr msg = {
5211 .msg_name = NULL,
5212 .msg_namelen = 0,
5213 .msg_iov = iov,
5214 .msg_iovlen = 2,
5215 .msg_control = NULL,
5216 .msg_controllen = 0,
5217 .msg_flags = 0,
5218 };
5219 int res;
5220 u16 txflags = 0;
5222 if (encrypt)
5223 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
5225 if (drv->monitor_sock < 0) {
5226 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
5227 "for %s", __func__);
5228 return -1;
5229 }
5231 if (noack)
5232 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
5233 *(le16 *) &rtap_hdr[12] = host_to_le16(txflags);
5235 res = sendmsg(drv->monitor_sock, &msg, 0);
5236 if (res < 0) {
5237 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
5238 return -1;
5239 }
5240 return 0;
5241 }
5244 static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
5245 const void *data, size_t len,
5246 int encrypt, int noack,
5247 unsigned int freq, int no_cck,
5248 int offchanok, unsigned int wait_time)
5249 {
5250 struct wpa_driver_nl80211_data *drv = bss->drv;
5251 u64 cookie;
5253 if (freq == 0)
5254 freq = bss->freq;
5256 if (drv->use_monitor)
5257 return wpa_driver_nl80211_send_mntr(drv, data, len,
5258 encrypt, noack);
5260 return nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
5261 &cookie, no_cck, noack, offchanok);
5262 }
5265 static int wpa_driver_nl80211_send_mlme_freq(struct i802_bss *bss,
5266 const u8 *data,
5267 size_t data_len, int noack,
5268 unsigned int freq, int no_cck,
5269 int offchanok,
5270 unsigned int wait_time)
5271 {
5272 struct wpa_driver_nl80211_data *drv = bss->drv;
5273 struct ieee80211_mgmt *mgmt;
5274 int encrypt = 1;
5275 u16 fc;
5277 mgmt = (struct ieee80211_mgmt *) data;
5278 fc = le_to_host16(mgmt->frame_control);
5280 if (is_sta_interface(drv->nlmode) &&
5281 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5282 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
5283 /*
5284 * The use of last_mgmt_freq is a bit of a hack,
5285 * but it works due to the single-threaded nature
5286 * of wpa_supplicant.
5287 */
5288 if (freq == 0)
5289 freq = drv->last_mgmt_freq;
5290 return nl80211_send_frame_cmd(bss, freq, 0,
5291 data, data_len, NULL, 1, noack,
5292 1);
5293 }
5295 #ifdef ANDROID_BRCM_P2P_PATCH
5296 if (is_ap_interface(drv->nlmode)) {
5297 wpa_printf(MSG_DEBUG, "%s: Sending frame on bss freq %d "
5298 "using nl80211_send_frame_cmd", __func__,
5299 bss->freq);
5300 return nl80211_send_frame_cmd(bss, bss->freq, 0,
5301 data, data_len,
5302 &drv->send_action_cookie, 0,
5303 noack, 0);
5304 }
5305 #else /* ANDROID_BRCM_P2P_PATCH */
5306 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
5307 if (freq == 0)
5308 freq = bss->freq;
5309 return nl80211_send_frame_cmd(bss, freq, 0,
5310 data, data_len,
5311 &drv->send_action_cookie,
5312 no_cck, noack, offchanok);
5313 }
5314 #endif /* ANDROID_BRCM_P2P_PATCH */
5316 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5317 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
5318 /*
5319 * Only one of the authentication frame types is encrypted.
5320 * In order for static WEP encryption to work properly (i.e.,
5321 * to not encrypt the frame), we need to tell mac80211 about
5322 * the frames that must not be encrypted.
5323 */
5324 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
5325 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
5326 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
5327 encrypt = 0;
5328 }
5330 wpa_printf(MSG_DEBUG, "%s: Sending frame using monitor interface/"
5331 "l2 socket", __func__);
5332 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
5333 noack, freq, no_cck, offchanok,
5334 wait_time);
5335 }
5338 static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
5339 size_t data_len, int noack)
5340 {
5341 struct i802_bss *bss = priv;
5342 return wpa_driver_nl80211_send_mlme_freq(bss, data, data_len, noack,
5343 0, 0, 0, 0);
5344 }
5347 static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
5348 int slot, int ht_opmode, int ap_isolate,
5349 int *basic_rates)
5350 {
5351 struct wpa_driver_nl80211_data *drv = bss->drv;
5352 struct nl_msg *msg;
5354 msg = nlmsg_alloc();
5355 if (!msg)
5356 return -ENOMEM;
5358 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
5360 if (cts >= 0)
5361 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
5362 if (preamble >= 0)
5363 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
5364 if (slot >= 0)
5365 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
5366 if (ht_opmode >= 0)
5367 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
5368 if (ap_isolate >= 0)
5369 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
5371 if (basic_rates) {
5372 u8 rates[NL80211_MAX_SUPP_RATES];
5373 u8 rates_len = 0;
5374 int i;
5376 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
5377 i++)
5378 rates[rates_len++] = basic_rates[i] / 5;
5380 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
5381 }
5383 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5385 return send_and_recv_msgs(drv, msg, NULL, NULL);
5386 nla_put_failure:
5387 nlmsg_free(msg);
5388 return -ENOBUFS;
5389 }
5392 static int wpa_driver_nl80211_set_ap(void *priv,
5393 struct wpa_driver_ap_params *params)
5394 {
5395 struct i802_bss *bss = priv;
5396 struct wpa_driver_nl80211_data *drv = bss->drv;
5397 struct nl_msg *msg;
5398 u8 cmd = NL80211_CMD_NEW_BEACON;
5399 int ret;
5400 int beacon_set;
5401 int ifindex = if_nametoindex(bss->ifname);
5402 int num_suites;
5403 u32 suites[10];
5404 u32 ver;
5406 beacon_set = bss->beacon_set;
5408 msg = nlmsg_alloc();
5409 if (!msg)
5410 return -ENOMEM;
5412 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
5413 beacon_set);
5414 if (beacon_set)
5415 cmd = NL80211_CMD_SET_BEACON;
5417 nl80211_cmd(drv, msg, 0, cmd);
5418 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
5419 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
5420 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5421 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
5422 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
5423 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5424 params->ssid);
5425 if (params->proberesp && params->proberesp_len)
5426 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
5427 params->proberesp);
5428 switch (params->hide_ssid) {
5429 case NO_SSID_HIDING:
5430 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5431 NL80211_HIDDEN_SSID_NOT_IN_USE);
5432 break;
5433 case HIDDEN_SSID_ZERO_LEN:
5434 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5435 NL80211_HIDDEN_SSID_ZERO_LEN);
5436 break;
5437 case HIDDEN_SSID_ZERO_CONTENTS:
5438 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5439 NL80211_HIDDEN_SSID_ZERO_CONTENTS);
5440 break;
5441 }
5442 if (params->privacy)
5443 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5444 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
5445 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
5446 /* Leave out the attribute */
5447 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
5448 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5449 NL80211_AUTHTYPE_SHARED_KEY);
5450 else
5451 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5452 NL80211_AUTHTYPE_OPEN_SYSTEM);
5454 ver = 0;
5455 if (params->wpa_version & WPA_PROTO_WPA)
5456 ver |= NL80211_WPA_VERSION_1;
5457 if (params->wpa_version & WPA_PROTO_RSN)
5458 ver |= NL80211_WPA_VERSION_2;
5459 if (ver)
5460 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
5462 num_suites = 0;
5463 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
5464 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
5465 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
5466 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
5467 if (num_suites) {
5468 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
5469 num_suites * sizeof(u32), suites);
5470 }
5472 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
5473 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
5474 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
5476 num_suites = 0;
5477 if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
5478 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5479 if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
5480 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5481 if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
5482 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5483 if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
5484 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5485 if (num_suites) {
5486 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5487 num_suites * sizeof(u32), suites);
5488 }
5490 switch (params->group_cipher) {
5491 case WPA_CIPHER_CCMP:
5492 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5493 WLAN_CIPHER_SUITE_CCMP);
5494 break;
5495 case WPA_CIPHER_TKIP:
5496 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5497 WLAN_CIPHER_SUITE_TKIP);
5498 break;
5499 case WPA_CIPHER_WEP104:
5500 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5501 WLAN_CIPHER_SUITE_WEP104);
5502 break;
5503 case WPA_CIPHER_WEP40:
5504 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5505 WLAN_CIPHER_SUITE_WEP40);
5506 break;
5507 }
5509 if (params->beacon_ies) {
5510 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
5511 wpabuf_head(params->beacon_ies));
5512 }
5513 if (params->proberesp_ies) {
5514 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
5515 wpabuf_len(params->proberesp_ies),
5516 wpabuf_head(params->proberesp_ies));
5517 }
5518 if (params->assocresp_ies) {
5519 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
5520 wpabuf_len(params->assocresp_ies),
5521 wpabuf_head(params->assocresp_ies));
5522 }
5524 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5525 if (ret) {
5526 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
5527 ret, strerror(-ret));
5528 } else {
5529 bss->beacon_set = 1;
5530 nl80211_set_bss(bss, params->cts_protect, params->preamble,
5531 params->short_slot_time, params->ht_opmode,
5532 params->isolate, params->basic_rates);
5533 }
5534 #if defined(ANDROID_BRCM_P2P_PATCH) && defined(HOSTAPD)
5535 wpa_driver_nl80211_probe_req_report(priv, 1);
5536 #endif
5537 return ret;
5538 nla_put_failure:
5539 nlmsg_free(msg);
5540 return -ENOBUFS;
5541 }
5544 static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
5545 int freq, int ht_enabled,
5546 int sec_channel_offset)
5547 {
5548 struct wpa_driver_nl80211_data *drv = bss->drv;
5549 struct nl_msg *msg;
5550 int ret;
5552 wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d "
5553 "sec_channel_offset=%d)",
5554 freq, ht_enabled, sec_channel_offset);
5555 msg = nlmsg_alloc();
5556 if (!msg)
5557 return -1;
5559 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
5561 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5562 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5563 if (ht_enabled) {
5564 switch (sec_channel_offset) {
5565 case -1:
5566 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5567 NL80211_CHAN_HT40MINUS);
5568 break;
5569 case 1:
5570 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5571 NL80211_CHAN_HT40PLUS);
5572 break;
5573 default:
5574 #ifdef ANDROID_BRCM_P2P_PATCH
5575 /* Should be changed to HT20 as a default value because
5576 * P2P firmware does not support 11n for BCM4329 */
5577 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5578 NL80211_CHAN_NO_HT);
5579 #else /* ANDROID_BRCM_P2P_PATCH */
5580 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5581 NL80211_CHAN_HT20);
5582 #endif /* ANDROID_BRCM_P2P_PATCH */
5583 break;
5584 }
5585 }
5587 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5588 msg = NULL;
5589 if (ret == 0) {
5590 bss->freq = freq;
5591 return 0;
5592 }
5593 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
5594 "%d (%s)", freq, ret, strerror(-ret));
5595 nla_put_failure:
5596 nlmsg_free(msg);
5597 return -1;
5598 }
5601 static u32 sta_flags_nl80211(int flags)
5602 {
5603 u32 f = 0;
5605 if (flags & WPA_STA_AUTHORIZED)
5606 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
5607 if (flags & WPA_STA_WMM)
5608 f |= BIT(NL80211_STA_FLAG_WME);
5609 if (flags & WPA_STA_SHORT_PREAMBLE)
5610 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
5611 if (flags & WPA_STA_MFP)
5612 f |= BIT(NL80211_STA_FLAG_MFP);
5613 if (flags & WPA_STA_TDLS_PEER)
5614 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
5616 return f;
5617 }
5620 static int wpa_driver_nl80211_sta_add(void *priv,
5621 struct hostapd_sta_add_params *params)
5622 {
5623 struct i802_bss *bss = priv;
5624 struct wpa_driver_nl80211_data *drv = bss->drv;
5625 struct nl_msg *msg, *wme = NULL;
5626 struct nl80211_sta_flag_update upd;
5627 int ret = -ENOBUFS;
5629 if ((params->flags & WPA_STA_TDLS_PEER) &&
5630 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
5631 return -EOPNOTSUPP;
5633 msg = nlmsg_alloc();
5634 if (!msg)
5635 return -ENOMEM;
5637 nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
5638 NL80211_CMD_NEW_STATION);
5640 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5641 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
5642 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
5643 params->supp_rates);
5644 if (!params->set) {
5645 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
5646 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
5647 params->listen_interval);
5648 }
5649 if (params->ht_capabilities) {
5650 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
5651 sizeof(*params->ht_capabilities),
5652 params->ht_capabilities);
5653 }
5655 os_memset(&upd, 0, sizeof(upd));
5656 upd.mask = sta_flags_nl80211(params->flags);
5657 upd.set = upd.mask;
5658 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
5660 if (params->flags & WPA_STA_WMM) {
5661 wme = nlmsg_alloc();
5662 if (!wme)
5663 goto nla_put_failure;
5665 NLA_PUT_U8(wme, NL80211_STA_WME_UAPSD_QUEUES,
5666 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
5667 NLA_PUT_U8(wme, NL80211_STA_WME_MAX_SP,
5668 (params->qosinfo > WMM_QOSINFO_STA_SP_SHIFT) &
5669 WMM_QOSINFO_STA_SP_MASK);
5670 nla_put_nested(msg, NL80211_ATTR_STA_WME, wme);
5671 }
5673 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5674 msg = NULL;
5675 if (ret)
5676 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
5677 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
5678 strerror(-ret));
5679 if (ret == -EEXIST)
5680 ret = 0;
5681 nla_put_failure:
5682 nlmsg_free(wme);
5683 nlmsg_free(msg);
5684 return ret;
5685 }
5688 static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
5689 {
5690 struct i802_bss *bss = priv;
5691 struct wpa_driver_nl80211_data *drv = bss->drv;
5692 struct nl_msg *msg;
5693 int ret;
5695 msg = nlmsg_alloc();
5696 if (!msg)
5697 return -ENOMEM;
5699 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
5701 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
5702 if_nametoindex(bss->ifname));
5703 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5705 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5706 if (ret == -ENOENT)
5707 return 0;
5708 return ret;
5709 nla_put_failure:
5710 nlmsg_free(msg);
5711 return -ENOBUFS;
5712 }
5715 static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
5716 int ifidx)
5717 {
5718 struct nl_msg *msg;
5720 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
5722 /* stop listening for EAPOL on this interface */
5723 del_ifidx(drv, ifidx);
5725 msg = nlmsg_alloc();
5726 if (!msg)
5727 goto nla_put_failure;
5729 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
5730 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
5732 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5733 return;
5734 msg = NULL;
5735 nla_put_failure:
5736 nlmsg_free(msg);
5737 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
5738 }
5741 static const char * nl80211_iftype_str(enum nl80211_iftype mode)
5742 {
5743 switch (mode) {
5744 case NL80211_IFTYPE_ADHOC:
5745 return "ADHOC";
5746 case NL80211_IFTYPE_STATION:
5747 return "STATION";
5748 case NL80211_IFTYPE_AP:
5749 return "AP";
5750 case NL80211_IFTYPE_MONITOR:
5751 return "MONITOR";
5752 case NL80211_IFTYPE_P2P_CLIENT:
5753 return "P2P_CLIENT";
5754 case NL80211_IFTYPE_P2P_GO:
5755 return "P2P_GO";
5756 default:
5757 return "unknown";
5758 }
5759 }
5762 static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
5763 const char *ifname,
5764 enum nl80211_iftype iftype,
5765 const u8 *addr, int wds)
5766 {
5767 struct nl_msg *msg, *flags = NULL;
5768 int ifidx;
5769 int ret = -ENOBUFS;
5771 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
5772 iftype, nl80211_iftype_str(iftype));
5774 msg = nlmsg_alloc();
5775 if (!msg)
5776 return -1;
5778 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
5779 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5780 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
5781 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
5783 if (iftype == NL80211_IFTYPE_MONITOR) {
5784 int err;
5786 flags = nlmsg_alloc();
5787 if (!flags)
5788 goto nla_put_failure;
5790 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
5792 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
5794 nlmsg_free(flags);
5796 if (err)
5797 goto nla_put_failure;
5798 } else if (wds) {
5799 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
5800 }
5802 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5803 msg = NULL;
5804 if (ret) {
5805 nla_put_failure:
5806 nlmsg_free(msg);
5807 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
5808 ifname, ret, strerror(-ret));
5809 return ret;
5810 }
5812 ifidx = if_nametoindex(ifname);
5813 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
5814 ifname, ifidx);
5816 if (ifidx <= 0)
5817 return -1;
5819 /* start listening for EAPOL on this interface */
5820 add_ifidx(drv, ifidx);
5822 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
5823 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
5824 nl80211_remove_iface(drv, ifidx);
5825 return -1;
5826 }
5828 return ifidx;
5829 }
5832 static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
5833 const char *ifname, enum nl80211_iftype iftype,
5834 const u8 *addr, int wds)
5835 {
5836 int ret;
5838 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
5840 /* if error occurred and interface exists already */
5841 if (ret == -ENFILE && if_nametoindex(ifname)) {
5842 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
5844 /* Try to remove the interface that was already there. */
5845 nl80211_remove_iface(drv, if_nametoindex(ifname));
5847 /* Try to create the interface again */
5848 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
5849 wds);
5850 }
5852 if (ret >= 0 && is_p2p_interface(iftype))
5853 nl80211_disable_11b_rates(drv, ret, 1);
5855 return ret;
5856 }
5859 static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
5860 {
5861 struct ieee80211_hdr *hdr;
5862 u16 fc;
5863 union wpa_event_data event;
5865 hdr = (struct ieee80211_hdr *) buf;
5866 fc = le_to_host16(hdr->frame_control);
5868 os_memset(&event, 0, sizeof(event));
5869 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
5870 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
5871 event.tx_status.dst = hdr->addr1;
5872 event.tx_status.data = buf;
5873 event.tx_status.data_len = len;
5874 event.tx_status.ack = ok;
5875 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
5876 }
5879 static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
5880 u8 *buf, size_t len)
5881 {
5882 struct ieee80211_hdr *hdr = (void *)buf;
5883 u16 fc;
5884 union wpa_event_data event;
5886 if (len < sizeof(*hdr))
5887 return;
5889 fc = le_to_host16(hdr->frame_control);
5891 os_memset(&event, 0, sizeof(event));
5892 event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
5893 event.rx_from_unknown.addr = hdr->addr2;
5894 event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
5895 (WLAN_FC_FROMDS | WLAN_FC_TODS);
5896 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
5897 }
5900 static void handle_frame(struct wpa_driver_nl80211_data *drv,
5901 u8 *buf, size_t len, int datarate, int ssi_signal)
5902 {
5903 struct ieee80211_hdr *hdr;
5904 u16 fc;
5905 union wpa_event_data event;
5907 hdr = (struct ieee80211_hdr *) buf;
5908 fc = le_to_host16(hdr->frame_control);
5910 switch (WLAN_FC_GET_TYPE(fc)) {
5911 case WLAN_FC_TYPE_MGMT:
5912 os_memset(&event, 0, sizeof(event));
5913 event.rx_mgmt.frame = buf;
5914 event.rx_mgmt.frame_len = len;
5915 event.rx_mgmt.datarate = datarate;
5916 event.rx_mgmt.ssi_signal = ssi_signal;
5917 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
5918 break;
5919 case WLAN_FC_TYPE_CTRL:
5920 /* can only get here with PS-Poll frames */
5921 wpa_printf(MSG_DEBUG, "CTRL");
5922 from_unknown_sta(drv, buf, len);
5923 break;
5924 case WLAN_FC_TYPE_DATA:
5925 from_unknown_sta(drv, buf, len);
5926 break;
5927 }
5928 }
5931 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
5932 {
5933 struct wpa_driver_nl80211_data *drv = eloop_ctx;
5934 int len;
5935 unsigned char buf[3000];
5936 struct ieee80211_radiotap_iterator iter;
5937 int ret;
5938 int datarate = 0, ssi_signal = 0;
5939 int injected = 0, failed = 0, rxflags = 0;
5941 len = recv(sock, buf, sizeof(buf), 0);
5942 if (len < 0) {
5943 perror("recv");
5944 return;
5945 }
5947 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
5948 printf("received invalid radiotap frame\n");
5949 return;
5950 }
5952 while (1) {
5953 ret = ieee80211_radiotap_iterator_next(&iter);
5954 if (ret == -ENOENT)
5955 break;
5956 if (ret) {
5957 printf("received invalid radiotap frame (%d)\n", ret);
5958 return;
5959 }
5960 switch (iter.this_arg_index) {
5961 case IEEE80211_RADIOTAP_FLAGS:
5962 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
5963 len -= 4;
5964 break;
5965 case IEEE80211_RADIOTAP_RX_FLAGS:
5966 rxflags = 1;
5967 break;
5968 case IEEE80211_RADIOTAP_TX_FLAGS:
5969 injected = 1;
5970 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
5971 IEEE80211_RADIOTAP_F_TX_FAIL;
5972 break;
5973 case IEEE80211_RADIOTAP_DATA_RETRIES:
5974 break;
5975 case IEEE80211_RADIOTAP_CHANNEL:
5976 /* TODO: convert from freq/flags to channel number */
5977 break;
5978 case IEEE80211_RADIOTAP_RATE:
5979 datarate = *iter.this_arg * 5;
5980 break;
5981 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
5982 ssi_signal = *iter.this_arg;
5983 break;
5984 }
5985 }
5987 if (rxflags && injected)
5988 return;
5990 if (!injected)
5991 handle_frame(drv, buf + iter.max_length,
5992 len - iter.max_length, datarate, ssi_signal);
5993 else
5994 handle_tx_callback(drv->ctx, buf + iter.max_length,
5995 len - iter.max_length, !failed);
5996 }
5999 /*
6000 * we post-process the filter code later and rewrite
6001 * this to the offset to the last instruction
6002 */
6003 #define PASS 0xFF
6004 #define FAIL 0xFE
6006 static struct sock_filter msock_filter_insns[] = {
6007 /*
6008 * do a little-endian load of the radiotap length field
6009 */
6010 /* load lower byte into A */
6011 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
6012 /* put it into X (== index register) */
6013 BPF_STMT(BPF_MISC| BPF_TAX, 0),
6014 /* load upper byte into A */
6015 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
6016 /* left-shift it by 8 */
6017 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
6018 /* or with X */
6019 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
6020 /* put result into X */
6021 BPF_STMT(BPF_MISC| BPF_TAX, 0),
6023 /*
6024 * Allow management frames through, this also gives us those
6025 * management frames that we sent ourselves with status
6026 */
6027 /* load the lower byte of the IEEE 802.11 frame control field */
6028 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6029 /* mask off frame type and version */
6030 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
6031 /* accept frame if it's both 0, fall through otherwise */
6032 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
6034 /*
6035 * TODO: add a bit to radiotap RX flags that indicates
6036 * that the sending station is not associated, then
6037 * add a filter here that filters on our DA and that flag
6038 * to allow us to deauth frames to that bad station.
6039 *
6040 * For now allow all To DS data frames through.
6041 */
6042 /* load the IEEE 802.11 frame control field */
6043 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 0),
6044 /* mask off frame type, version and DS status */
6045 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
6046 /* accept frame if version 0, type 2 and To DS, fall through otherwise
6047 */
6048 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
6050 #if 0
6051 /*
6052 * drop non-data frames
6053 */
6054 /* load the lower byte of the frame control field */
6055 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6056 /* mask off QoS bit */
6057 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
6058 /* drop non-data frames */
6059 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
6060 #endif
6061 /* load the upper byte of the frame control field */
6062 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
6063 /* mask off toDS/fromDS */
6064 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
6065 /* accept WDS frames */
6066 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
6068 /*
6069 * add header length to index
6070 */
6071 /* load the lower byte of the frame control field */
6072 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
6073 /* mask off QoS bit */
6074 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
6075 /* right shift it by 6 to give 0 or 2 */
6076 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
6077 /* add data frame header length */
6078 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
6079 /* add index, was start of 802.11 header */
6080 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
6081 /* move to index, now start of LL header */
6082 BPF_STMT(BPF_MISC | BPF_TAX, 0),
6084 /*
6085 * Accept empty data frames, we use those for
6086 * polling activity.
6087 */
6088 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
6089 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
6091 /*
6092 * Accept EAPOL frames
6093 */
6094 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
6095 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
6096 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
6097 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
6099 /* keep these last two statements or change the code below */
6100 /* return 0 == "DROP" */
6101 BPF_STMT(BPF_RET | BPF_K, 0),
6102 /* return ~0 == "keep all" */
6103 BPF_STMT(BPF_RET | BPF_K, ~0),
6104 };
6106 static struct sock_fprog msock_filter = {
6107 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
6108 .filter = msock_filter_insns,
6109 };
6112 static int add_monitor_filter(int s)
6113 {
6114 int idx;
6116 /* rewrite all PASS/FAIL jump offsets */
6117 for (idx = 0; idx < msock_filter.len; idx++) {
6118 struct sock_filter *insn = &msock_filter_insns[idx];
6120 if (BPF_CLASS(insn->code) == BPF_JMP) {
6121 if (insn->code == (BPF_JMP|BPF_JA)) {
6122 if (insn->k == PASS)
6123 insn->k = msock_filter.len - idx - 2;
6124 else if (insn->k == FAIL)
6125 insn->k = msock_filter.len - idx - 3;
6126 }
6128 if (insn->jt == PASS)
6129 insn->jt = msock_filter.len - idx - 2;
6130 else if (insn->jt == FAIL)
6131 insn->jt = msock_filter.len - idx - 3;
6133 if (insn->jf == PASS)
6134 insn->jf = msock_filter.len - idx - 2;
6135 else if (insn->jf == FAIL)
6136 insn->jf = msock_filter.len - idx - 3;
6137 }
6138 }
6140 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
6141 &msock_filter, sizeof(msock_filter))) {
6142 perror("SO_ATTACH_FILTER");
6143 return -1;
6144 }
6146 return 0;
6147 }
6150 static void nl80211_remove_monitor_interface(
6151 struct wpa_driver_nl80211_data *drv)
6152 {
6153 drv->monitor_refcount--;
6154 if (drv->monitor_refcount > 0)
6155 return;
6157 if (drv->monitor_ifidx >= 0) {
6158 nl80211_remove_iface(drv, drv->monitor_ifidx);
6159 drv->monitor_ifidx = -1;
6160 }
6161 if (drv->monitor_sock >= 0) {
6162 eloop_unregister_read_sock(drv->monitor_sock);
6163 close(drv->monitor_sock);
6164 drv->monitor_sock = -1;
6165 }
6166 }
6169 static int
6170 nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
6171 {
6172 char buf[IFNAMSIZ];
6173 struct sockaddr_ll ll;
6174 int optval;
6175 socklen_t optlen;
6177 if (drv->monitor_ifidx >= 0) {
6178 drv->monitor_refcount++;
6179 return 0;
6180 }
6182 if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
6183 /*
6184 * P2P interface name is of the format p2p-%s-%d. For monitor
6185 * interface name corresponding to P2P GO, replace "p2p-" with
6186 * "mon-" to retain the same interface name length and to
6187 * indicate that it is a monitor interface.
6188 */
6189 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
6190 } else {
6191 /* Non-P2P interface with AP functionality. */
6192 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
6193 }
6195 buf[IFNAMSIZ - 1] = '\0';
6197 drv->monitor_ifidx =
6198 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
6199 0);
6201 if (drv->monitor_ifidx == -EOPNOTSUPP) {
6202 /*
6203 * This is backward compatibility for a few versions of
6204 * the kernel only that didn't advertise the right
6205 * attributes for the only driver that then supported
6206 * AP mode w/o monitor -- ath6kl.
6207 */
6208 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
6209 "monitor interface type - try to run without it");
6210 drv->device_ap_sme = 1;
6211 }
6213 if (drv->monitor_ifidx < 0)
6214 return -1;
6216 if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
6217 goto error;
6219 memset(&ll, 0, sizeof(ll));
6220 ll.sll_family = AF_PACKET;
6221 ll.sll_ifindex = drv->monitor_ifidx;
6222 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
6223 if (drv->monitor_sock < 0) {
6224 perror("socket[PF_PACKET,SOCK_RAW]");
6225 goto error;
6226 }
6228 if (add_monitor_filter(drv->monitor_sock)) {
6229 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
6230 "interface; do filtering in user space");
6231 /* This works, but will cost in performance. */
6232 }
6234 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
6235 perror("monitor socket bind");
6236 goto error;
6237 }
6239 optlen = sizeof(optval);
6240 optval = 20;
6241 if (setsockopt
6242 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
6243 perror("Failed to set socket priority");
6244 goto error;
6245 }
6247 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
6248 drv, NULL)) {
6249 printf("Could not register monitor read socket\n");
6250 goto error;
6251 }
6253 return 0;
6254 error:
6255 nl80211_remove_monitor_interface(drv);
6256 return -1;
6257 }
6260 static int nl80211_setup_ap(struct i802_bss *bss)
6261 {
6262 struct wpa_driver_nl80211_data *drv = bss->drv;
6264 wpa_printf(MSG_DEBUG, "nl80211: Setup AP - device_ap_sme=%d "
6265 "use_monitor=%d", drv->device_ap_sme, drv->use_monitor);
6267 /*
6268 * Disable Probe Request reporting unless we need it in this way for
6269 * devices that include the AP SME, in the other case (unless using
6270 * monitor iface) we'll get it through the nl_mgmt socket instead.
6271 */
6272 if (!drv->device_ap_sme)
6273 wpa_driver_nl80211_probe_req_report(bss, 0);
6275 if (!drv->device_ap_sme && !drv->use_monitor)
6276 if (nl80211_mgmt_subscribe_ap(bss))
6277 return -1;
6279 if (drv->device_ap_sme && !drv->use_monitor)
6280 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
6281 return -1;
6283 if (!drv->device_ap_sme && drv->use_monitor &&
6284 nl80211_create_monitor_interface(drv) &&
6285 !drv->device_ap_sme)
6286 return -1;
6288 if (drv->device_ap_sme &&
6289 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
6290 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
6291 "Probe Request frame reporting in AP mode");
6292 /* Try to survive without this */
6293 }
6295 return 0;
6296 }
6299 static void nl80211_teardown_ap(struct i802_bss *bss)
6300 {
6301 struct wpa_driver_nl80211_data *drv = bss->drv;
6303 if (drv->device_ap_sme) {
6304 wpa_driver_nl80211_probe_req_report(bss, 0);
6305 if (!drv->use_monitor)
6306 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
6307 } else if (drv->use_monitor)
6308 nl80211_remove_monitor_interface(drv);
6309 else
6310 nl80211_mgmt_unsubscribe(bss, "AP teardown");
6312 bss->beacon_set = 0;
6313 }
6316 static int nl80211_send_eapol_data(struct i802_bss *bss,
6317 const u8 *addr, const u8 *data,
6318 size_t data_len)
6319 {
6320 struct sockaddr_ll ll;
6321 int ret;
6323 if (bss->drv->eapol_tx_sock < 0) {
6324 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
6325 return -1;
6326 }
6328 os_memset(&ll, 0, sizeof(ll));
6329 ll.sll_family = AF_PACKET;
6330 ll.sll_ifindex = bss->ifindex;
6331 ll.sll_protocol = htons(ETH_P_PAE);
6332 ll.sll_halen = ETH_ALEN;
6333 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
6334 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
6335 (struct sockaddr *) &ll, sizeof(ll));
6336 if (ret < 0)
6337 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
6338 strerror(errno));
6340 return ret;
6341 }
6344 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
6346 static int wpa_driver_nl80211_hapd_send_eapol(
6347 void *priv, const u8 *addr, const u8 *data,
6348 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
6349 {
6350 struct i802_bss *bss = priv;
6351 struct wpa_driver_nl80211_data *drv = bss->drv;
6352 struct ieee80211_hdr *hdr;
6353 size_t len;
6354 u8 *pos;
6355 int res;
6356 int qos = flags & WPA_STA_WMM;
6358 if (drv->device_ap_sme || !drv->use_monitor)
6359 return nl80211_send_eapol_data(bss, addr, data, data_len);
6361 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
6362 data_len;
6363 hdr = os_zalloc(len);
6364 if (hdr == NULL) {
6365 printf("malloc() failed for i802_send_data(len=%lu)\n",
6366 (unsigned long) len);
6367 return -1;
6368 }
6370 hdr->frame_control =
6371 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
6372 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
6373 if (encrypt)
6374 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
6375 if (qos) {
6376 hdr->frame_control |=
6377 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
6378 }
6380 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6381 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6382 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6383 pos = (u8 *) (hdr + 1);
6385 if (qos) {
6386 /* add an empty QoS header if needed */
6387 pos[0] = 0;
6388 pos[1] = 0;
6389 pos += 2;
6390 }
6392 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
6393 pos += sizeof(rfc1042_header);
6394 WPA_PUT_BE16(pos, ETH_P_PAE);
6395 pos += 2;
6396 memcpy(pos, data, data_len);
6398 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
6399 0, 0, 0, 0);
6400 if (res < 0) {
6401 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
6402 "failed: %d (%s)",
6403 (unsigned long) len, errno, strerror(errno));
6404 }
6405 os_free(hdr);
6407 return res;
6408 }
6411 static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
6412 int total_flags,
6413 int flags_or, int flags_and)
6414 {
6415 struct i802_bss *bss = priv;
6416 struct wpa_driver_nl80211_data *drv = bss->drv;
6417 struct nl_msg *msg, *flags = NULL;
6418 struct nl80211_sta_flag_update upd;
6420 msg = nlmsg_alloc();
6421 if (!msg)
6422 return -ENOMEM;
6424 flags = nlmsg_alloc();
6425 if (!flags) {
6426 nlmsg_free(msg);
6427 return -ENOMEM;
6428 }
6430 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
6432 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6433 if_nametoindex(bss->ifname));
6434 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6436 /*
6437 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
6438 * can be removed eventually.
6439 */
6440 if (total_flags & WPA_STA_AUTHORIZED)
6441 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
6443 if (total_flags & WPA_STA_WMM)
6444 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
6446 if (total_flags & WPA_STA_SHORT_PREAMBLE)
6447 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
6449 if (total_flags & WPA_STA_MFP)
6450 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
6452 if (total_flags & WPA_STA_TDLS_PEER)
6453 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_TDLS_PEER);
6455 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
6456 goto nla_put_failure;
6458 os_memset(&upd, 0, sizeof(upd));
6459 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
6460 upd.set = sta_flags_nl80211(flags_or);
6461 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6463 nlmsg_free(flags);
6465 return send_and_recv_msgs(drv, msg, NULL, NULL);
6466 nla_put_failure:
6467 nlmsg_free(msg);
6468 nlmsg_free(flags);
6469 return -ENOBUFS;
6470 }
6473 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
6474 struct wpa_driver_associate_params *params)
6475 {
6476 enum nl80211_iftype nlmode;
6478 if (params->p2p) {
6479 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
6480 "group (GO)");
6481 nlmode = NL80211_IFTYPE_P2P_GO;
6482 } else
6483 nlmode = NL80211_IFTYPE_AP;
6485 if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode) ||
6486 wpa_driver_nl80211_set_freq(&drv->first_bss, params->freq, 0, 0)) {
6487 nl80211_remove_monitor_interface(drv);
6488 return -1;
6489 }
6491 return 0;
6492 }
6495 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
6496 {
6497 struct nl_msg *msg;
6498 int ret = -1;
6500 msg = nlmsg_alloc();
6501 if (!msg)
6502 return -1;
6504 nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
6505 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6506 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6507 msg = NULL;
6508 if (ret) {
6509 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
6510 "(%s)", ret, strerror(-ret));
6511 goto nla_put_failure;
6512 }
6514 ret = 0;
6515 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
6517 nla_put_failure:
6518 nlmsg_free(msg);
6519 return ret;
6520 }
6523 static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
6524 struct wpa_driver_associate_params *params)
6525 {
6526 struct nl_msg *msg;
6527 int ret = -1;
6528 int count = 0;
6530 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
6532 if (wpa_driver_nl80211_set_mode(&drv->first_bss,
6533 NL80211_IFTYPE_ADHOC)) {
6534 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
6535 "IBSS mode");
6536 return -1;
6537 }
6539 retry:
6540 msg = nlmsg_alloc();
6541 if (!msg)
6542 return -1;
6544 nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
6545 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6547 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
6548 goto nla_put_failure;
6550 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6551 params->ssid, params->ssid_len);
6552 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6553 params->ssid);
6554 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6555 drv->ssid_len = params->ssid_len;
6557 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6558 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6560 ret = nl80211_set_conn_keys(params, msg);
6561 if (ret)
6562 goto nla_put_failure;
6564 if (params->bssid && params->fixed_bssid) {
6565 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
6566 MAC2STR(params->bssid));
6567 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6568 }
6570 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
6571 params->key_mgmt_suite == KEY_MGMT_PSK ||
6572 params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
6573 params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
6574 wpa_printf(MSG_DEBUG, " * control port");
6575 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
6576 }
6578 if (params->wpa_ie) {
6579 wpa_hexdump(MSG_DEBUG,
6580 " * Extra IEs for Beacon/Probe Response frames",
6581 params->wpa_ie, params->wpa_ie_len);
6582 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6583 params->wpa_ie);
6584 }
6586 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6587 msg = NULL;
6588 if (ret) {
6589 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
6590 ret, strerror(-ret));
6591 count++;
6592 if (ret == -EALREADY && count == 1) {
6593 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
6594 "forced leave");
6595 nl80211_leave_ibss(drv);
6596 nlmsg_free(msg);
6597 goto retry;
6598 }
6600 goto nla_put_failure;
6601 }
6602 ret = 0;
6603 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
6605 nla_put_failure:
6606 nlmsg_free(msg);
6607 return ret;
6608 }
6611 static unsigned int nl80211_get_assoc_bssid(struct wpa_driver_nl80211_data *drv,
6612 u8 *bssid)
6613 {
6614 struct nl_msg *msg;
6615 int ret;
6616 struct nl80211_bss_info_arg arg;
6618 os_memset(&arg, 0, sizeof(arg));
6619 msg = nlmsg_alloc();
6620 if (!msg)
6621 goto nla_put_failure;
6623 nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
6624 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6626 arg.drv = drv;
6627 ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
6628 msg = NULL;
6629 if (ret == 0) {
6630 if (is_zero_ether_addr(arg.assoc_bssid))
6631 return -ENOTCONN;
6632 os_memcpy(bssid, arg.assoc_bssid, ETH_ALEN);
6633 return 0;
6634 }
6635 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
6636 "(%s)", ret, strerror(-ret));
6637 nla_put_failure:
6638 nlmsg_free(msg);
6639 return drv->assoc_freq;
6640 }
6643 static int nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
6644 const u8 *bssid)
6645 {
6646 u8 addr[ETH_ALEN];
6648 if (bssid == NULL) {
6649 int res = nl80211_get_assoc_bssid(drv, addr);
6650 if (res)
6651 return res;
6652 bssid = addr;
6653 }
6655 return wpa_driver_nl80211_disconnect(drv, bssid,
6656 WLAN_REASON_PREV_AUTH_NOT_VALID);
6657 }
6660 static int wpa_driver_nl80211_connect(
6661 struct wpa_driver_nl80211_data *drv,
6662 struct wpa_driver_associate_params *params)
6663 {
6664 struct nl_msg *msg;
6665 enum nl80211_auth_type type;
6666 int ret = 0;
6667 int algs;
6669 msg = nlmsg_alloc();
6670 if (!msg)
6671 return -1;
6673 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
6674 nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
6676 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6677 if (params->bssid) {
6678 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6679 MAC2STR(params->bssid));
6680 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6681 }
6682 if (params->freq) {
6683 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6684 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6685 }
6686 if (params->ssid) {
6687 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6688 params->ssid, params->ssid_len);
6689 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6690 params->ssid);
6691 if (params->ssid_len > sizeof(drv->ssid))
6692 goto nla_put_failure;
6693 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6694 drv->ssid_len = params->ssid_len;
6695 }
6696 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
6697 if (params->wpa_ie)
6698 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6699 params->wpa_ie);
6701 algs = 0;
6702 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6703 algs++;
6704 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6705 algs++;
6706 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6707 algs++;
6708 if (algs > 1) {
6709 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
6710 "selection");
6711 goto skip_auth_type;
6712 }
6714 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6715 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
6716 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6717 type = NL80211_AUTHTYPE_SHARED_KEY;
6718 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6719 type = NL80211_AUTHTYPE_NETWORK_EAP;
6720 else if (params->auth_alg & WPA_AUTH_ALG_FT)
6721 type = NL80211_AUTHTYPE_FT;
6722 else
6723 goto nla_put_failure;
6725 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
6726 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
6728 skip_auth_type:
6729 if (params->wpa_proto) {
6730 enum nl80211_wpa_versions ver = 0;
6732 if (params->wpa_proto & WPA_PROTO_WPA)
6733 ver |= NL80211_WPA_VERSION_1;
6734 if (params->wpa_proto & WPA_PROTO_RSN)
6735 ver |= NL80211_WPA_VERSION_2;
6737 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
6738 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
6739 }
6741 if (params->pairwise_suite != CIPHER_NONE) {
6742 int cipher;
6744 switch (params->pairwise_suite) {
6745 case CIPHER_WEP40:
6746 cipher = WLAN_CIPHER_SUITE_WEP40;
6747 break;
6748 case CIPHER_WEP104:
6749 cipher = WLAN_CIPHER_SUITE_WEP104;
6750 break;
6751 case CIPHER_CCMP:
6752 cipher = WLAN_CIPHER_SUITE_CCMP;
6753 break;
6754 case CIPHER_TKIP:
6755 default:
6756 cipher = WLAN_CIPHER_SUITE_TKIP;
6757 break;
6758 }
6759 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
6760 }
6762 if (params->group_suite != CIPHER_NONE) {
6763 int cipher;
6765 switch (params->group_suite) {
6766 case CIPHER_WEP40:
6767 cipher = WLAN_CIPHER_SUITE_WEP40;
6768 break;
6769 case CIPHER_WEP104:
6770 cipher = WLAN_CIPHER_SUITE_WEP104;
6771 break;
6772 case CIPHER_CCMP:
6773 cipher = WLAN_CIPHER_SUITE_CCMP;
6774 break;
6775 case CIPHER_TKIP:
6776 default:
6777 cipher = WLAN_CIPHER_SUITE_TKIP;
6778 break;
6779 }
6780 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
6781 }
6783 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
6784 params->key_mgmt_suite == KEY_MGMT_PSK) {
6785 int mgmt = WLAN_AKM_SUITE_PSK;
6787 switch (params->key_mgmt_suite) {
6788 case KEY_MGMT_802_1X:
6789 mgmt = WLAN_AKM_SUITE_8021X;
6790 break;
6791 case KEY_MGMT_PSK:
6792 default:
6793 mgmt = WLAN_AKM_SUITE_PSK;
6794 break;
6795 }
6796 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
6797 }
6799 if (params->disable_ht)
6800 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
6802 if (params->htcaps && params->htcaps_mask) {
6803 int sz = sizeof(struct ieee80211_ht_capabilities);
6804 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
6805 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
6806 params->htcaps_mask);
6807 }
6809 ret = nl80211_set_conn_keys(params, msg);
6810 if (ret)
6811 goto nla_put_failure;
6813 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6814 msg = NULL;
6815 if (ret) {
6816 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
6817 "(%s)", ret, strerror(-ret));
6818 /*
6819 * cfg80211 does not currently accept new connection if we are
6820 * already connected. As a workaround, force disconnection and
6821 * try again once the driver indicates it completed
6822 * disconnection.
6823 */
6824 if (ret == -EALREADY)
6825 nl80211_disconnect(drv, params->bssid);
6826 goto nla_put_failure;
6827 }
6828 ret = 0;
6829 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
6831 nla_put_failure:
6832 nlmsg_free(msg);
6833 return ret;
6835 }
6838 static int wpa_driver_nl80211_associate(
6839 void *priv, struct wpa_driver_associate_params *params)
6840 {
6841 struct i802_bss *bss = priv;
6842 struct wpa_driver_nl80211_data *drv = bss->drv;
6843 int ret = -1;
6844 struct nl_msg *msg;
6846 if (params->mode == IEEE80211_MODE_AP)
6847 return wpa_driver_nl80211_ap(drv, params);
6849 if (params->mode == IEEE80211_MODE_IBSS)
6850 return wpa_driver_nl80211_ibss(drv, params);
6852 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
6853 enum nl80211_iftype nlmode = params->p2p ?
6854 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
6856 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
6857 return -1;
6858 return wpa_driver_nl80211_connect(drv, params);
6859 }
6861 drv->associated = 0;
6863 msg = nlmsg_alloc();
6864 if (!msg)
6865 return -1;
6867 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
6868 drv->ifindex);
6869 nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
6871 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6872 if (params->bssid) {
6873 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
6874 MAC2STR(params->bssid));
6875 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6876 }
6877 if (params->freq) {
6878 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
6879 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6880 drv->assoc_freq = params->freq;
6881 } else
6882 drv->assoc_freq = 0;
6883 if (params->ssid) {
6884 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
6885 params->ssid, params->ssid_len);
6886 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6887 params->ssid);
6888 if (params->ssid_len > sizeof(drv->ssid))
6889 goto nla_put_failure;
6890 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6891 drv->ssid_len = params->ssid_len;
6892 }
6893 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
6894 if (params->wpa_ie)
6895 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6896 params->wpa_ie);
6898 if (params->pairwise_suite != CIPHER_NONE) {
6899 int cipher;
6901 switch (params->pairwise_suite) {
6902 case CIPHER_WEP40:
6903 cipher = WLAN_CIPHER_SUITE_WEP40;
6904 break;
6905 case CIPHER_WEP104:
6906 cipher = WLAN_CIPHER_SUITE_WEP104;
6907 break;
6908 case CIPHER_CCMP:
6909 cipher = WLAN_CIPHER_SUITE_CCMP;
6910 break;
6911 case CIPHER_TKIP:
6912 default:
6913 cipher = WLAN_CIPHER_SUITE_TKIP;
6914 break;
6915 }
6916 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
6917 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
6918 }
6920 if (params->group_suite != CIPHER_NONE) {
6921 int cipher;
6923 switch (params->group_suite) {
6924 case CIPHER_WEP40:
6925 cipher = WLAN_CIPHER_SUITE_WEP40;
6926 break;
6927 case CIPHER_WEP104:
6928 cipher = WLAN_CIPHER_SUITE_WEP104;
6929 break;
6930 case CIPHER_CCMP:
6931 cipher = WLAN_CIPHER_SUITE_CCMP;
6932 break;
6933 case CIPHER_TKIP:
6934 default:
6935 cipher = WLAN_CIPHER_SUITE_TKIP;
6936 break;
6937 }
6938 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
6939 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
6940 }
6942 #ifdef CONFIG_IEEE80211W
6943 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
6944 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
6945 #endif /* CONFIG_IEEE80211W */
6947 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
6949 if (params->prev_bssid) {
6950 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
6951 MAC2STR(params->prev_bssid));
6952 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
6953 params->prev_bssid);
6954 }
6956 if (params->disable_ht)
6957 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
6959 if (params->htcaps && params->htcaps_mask) {
6960 int sz = sizeof(struct ieee80211_ht_capabilities);
6961 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
6962 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
6963 params->htcaps_mask);
6964 }
6966 if (params->p2p)
6967 wpa_printf(MSG_DEBUG, " * P2P group");
6969 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6970 msg = NULL;
6971 if (ret) {
6972 wpa_dbg(drv->ctx, MSG_DEBUG,
6973 "nl80211: MLME command failed (assoc): ret=%d (%s)",
6974 ret, strerror(-ret));
6975 nl80211_dump_scan(drv);
6976 goto nla_put_failure;
6977 }
6978 ret = 0;
6979 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
6980 "successfully");
6982 nla_put_failure:
6983 nlmsg_free(msg);
6984 return ret;
6985 }
6988 static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
6989 int ifindex, enum nl80211_iftype mode)
6990 {
6991 struct nl_msg *msg;
6992 int ret = -ENOBUFS;
6994 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
6995 ifindex, mode, nl80211_iftype_str(mode));
6997 msg = nlmsg_alloc();
6998 if (!msg)
6999 return -ENOMEM;
7001 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
7002 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
7003 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
7005 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7006 msg = NULL;
7007 if (!ret)
7008 return 0;
7009 nla_put_failure:
7010 nlmsg_free(msg);
7011 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
7012 " %d (%s)", ifindex, mode, ret, strerror(-ret));
7013 return ret;
7014 }
7017 static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
7018 enum nl80211_iftype nlmode)
7019 {
7020 struct wpa_driver_nl80211_data *drv = bss->drv;
7021 int ret = -1;
7022 int i;
7023 int was_ap = is_ap_interface(drv->nlmode);
7024 int res;
7026 res = nl80211_set_mode(drv, drv->ifindex, nlmode);
7027 if (res == 0) {
7028 drv->nlmode = nlmode;
7029 ret = 0;
7030 goto done;
7031 }
7033 if (res == -ENODEV)
7034 return -1;
7036 if (nlmode == drv->nlmode) {
7037 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
7038 "requested mode - ignore error");
7039 ret = 0;
7040 goto done; /* Already in the requested mode */
7041 }
7043 /* mac80211 doesn't allow mode changes while the device is up, so
7044 * take the device down, try to set the mode again, and bring the
7045 * device back up.
7046 */
7047 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
7048 "interface down");
7049 for (i = 0; i < 10; i++) {
7050 res = linux_set_iface_flags(drv->global->ioctl_sock,
7051 bss->ifname, 0);
7052 if (res == -EACCES || res == -ENODEV)
7053 break;
7054 if (res == 0) {
7055 /* Try to set the mode again while the interface is
7056 * down */
7057 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
7058 if (ret == -EACCES)
7059 break;
7060 res = linux_set_iface_flags(drv->global->ioctl_sock,
7061 bss->ifname, 1);
7062 if (res && !ret)
7063 ret = -1;
7064 else if (ret != -EBUSY)
7065 break;
7066 } else
7067 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
7068 "interface down");
7069 os_sleep(0, 100000);
7070 }
7072 if (!ret) {
7073 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
7074 "interface is down");
7075 drv->nlmode = nlmode;
7076 drv->ignore_if_down_event = 1;
7077 }
7079 done:
7080 if (ret) {
7081 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
7082 "from %d failed", nlmode, drv->nlmode);
7083 return ret;
7084 }
7086 if (is_ap_interface(nlmode)) {
7087 nl80211_mgmt_unsubscribe(bss, "start AP");
7088 /* Setup additional AP mode functionality if needed */
7089 if (nl80211_setup_ap(bss))
7090 return -1;
7091 } else if (was_ap) {
7092 /* Remove additional AP mode functionality */
7093 nl80211_teardown_ap(bss);
7094 } else {
7095 nl80211_mgmt_unsubscribe(bss, "mode change");
7096 }
7098 if (!is_ap_interface(nlmode) &&
7099 nl80211_mgmt_subscribe_non_ap(bss) < 0)
7100 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
7101 "frame processing - ignore for now");
7103 return 0;
7104 }
7107 static int wpa_driver_nl80211_get_capa(void *priv,
7108 struct wpa_driver_capa *capa)
7109 {
7110 struct i802_bss *bss = priv;
7111 struct wpa_driver_nl80211_data *drv = bss->drv;
7112 if (!drv->has_capability)
7113 return -1;
7114 os_memcpy(capa, &drv->capa, sizeof(*capa));
7115 return 0;
7116 }
7119 static int wpa_driver_nl80211_set_operstate(void *priv, int state)
7120 {
7121 struct i802_bss *bss = priv;
7122 struct wpa_driver_nl80211_data *drv = bss->drv;
7124 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
7125 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
7126 drv->operstate = state;
7127 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
7128 state ? IF_OPER_UP : IF_OPER_DORMANT);
7129 }
7132 static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
7133 {
7134 struct i802_bss *bss = priv;
7135 struct wpa_driver_nl80211_data *drv = bss->drv;
7136 struct nl_msg *msg;
7137 struct nl80211_sta_flag_update upd;
7139 msg = nlmsg_alloc();
7140 if (!msg)
7141 return -ENOMEM;
7143 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
7145 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7146 if_nametoindex(bss->ifname));
7147 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
7149 os_memset(&upd, 0, sizeof(upd));
7150 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
7151 if (authorized)
7152 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
7153 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7155 return send_and_recv_msgs(drv, msg, NULL, NULL);
7156 nla_put_failure:
7157 nlmsg_free(msg);
7158 return -ENOBUFS;
7159 }
7162 /* Set kernel driver on given frequency (MHz) */
7163 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
7164 {
7165 struct i802_bss *bss = priv;
7166 return wpa_driver_nl80211_set_freq(bss, freq->freq, freq->ht_enabled,
7167 freq->sec_channel_offset);
7168 }
7171 #if defined(HOSTAPD) || defined(CONFIG_AP)
7173 static inline int min_int(int a, int b)
7174 {
7175 if (a < b)
7176 return a;
7177 return b;
7178 }
7181 static int get_key_handler(struct nl_msg *msg, void *arg)
7182 {
7183 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7184 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7186 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7187 genlmsg_attrlen(gnlh, 0), NULL);
7189 /*
7190 * TODO: validate the key index and mac address!
7191 * Otherwise, there's a race condition as soon as
7192 * the kernel starts sending key notifications.
7193 */
7195 if (tb[NL80211_ATTR_KEY_SEQ])
7196 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
7197 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
7198 return NL_SKIP;
7199 }
7202 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
7203 int idx, u8 *seq)
7204 {
7205 struct i802_bss *bss = priv;
7206 struct wpa_driver_nl80211_data *drv = bss->drv;
7207 struct nl_msg *msg;
7209 msg = nlmsg_alloc();
7210 if (!msg)
7211 return -ENOMEM;
7213 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
7215 if (addr)
7216 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7217 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
7218 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
7220 memset(seq, 0, 6);
7222 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
7223 nla_put_failure:
7224 nlmsg_free(msg);
7225 return -ENOBUFS;
7226 }
7229 static int i802_set_rts(void *priv, int rts)
7230 {
7231 struct i802_bss *bss = priv;
7232 struct wpa_driver_nl80211_data *drv = bss->drv;
7233 struct nl_msg *msg;
7234 int ret = -ENOBUFS;
7235 u32 val;
7237 msg = nlmsg_alloc();
7238 if (!msg)
7239 return -ENOMEM;
7241 if (rts >= 2347)
7242 val = (u32) -1;
7243 else
7244 val = rts;
7246 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
7247 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7248 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
7250 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7251 msg = NULL;
7252 if (!ret)
7253 return 0;
7254 nla_put_failure:
7255 nlmsg_free(msg);
7256 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
7257 "%d (%s)", rts, ret, strerror(-ret));
7258 return ret;
7259 }
7262 static int i802_set_frag(void *priv, int frag)
7263 {
7264 struct i802_bss *bss = priv;
7265 struct wpa_driver_nl80211_data *drv = bss->drv;
7266 struct nl_msg *msg;
7267 int ret = -ENOBUFS;
7268 u32 val;
7270 msg = nlmsg_alloc();
7271 if (!msg)
7272 return -ENOMEM;
7274 if (frag >= 2346)
7275 val = (u32) -1;
7276 else
7277 val = frag;
7279 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
7280 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7281 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
7283 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7284 msg = NULL;
7285 if (!ret)
7286 return 0;
7287 nla_put_failure:
7288 nlmsg_free(msg);
7289 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
7290 "%d: %d (%s)", frag, ret, strerror(-ret));
7291 return ret;
7292 }
7295 static int i802_flush(void *priv)
7296 {
7297 struct i802_bss *bss = priv;
7298 struct wpa_driver_nl80211_data *drv = bss->drv;
7299 struct nl_msg *msg;
7300 int res;
7302 msg = nlmsg_alloc();
7303 if (!msg)
7304 return -1;
7306 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
7308 /*
7309 * XXX: FIX! this needs to flush all VLANs too
7310 */
7311 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7312 if_nametoindex(bss->ifname));
7314 res = send_and_recv_msgs(drv, msg, NULL, NULL);
7315 if (res) {
7316 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
7317 "(%s)", res, strerror(-res));
7318 }
7319 return res;
7320 nla_put_failure:
7321 nlmsg_free(msg);
7322 return -ENOBUFS;
7323 }
7326 static int get_sta_handler(struct nl_msg *msg, void *arg)
7327 {
7328 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7329 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7330 struct hostap_sta_driver_data *data = arg;
7331 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
7332 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
7333 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
7334 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
7335 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
7336 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
7337 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
7338 };
7340 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7341 genlmsg_attrlen(gnlh, 0), NULL);
7343 /*
7344 * TODO: validate the interface and mac address!
7345 * Otherwise, there's a race condition as soon as
7346 * the kernel starts sending station notifications.
7347 */
7349 if (!tb[NL80211_ATTR_STA_INFO]) {
7350 wpa_printf(MSG_DEBUG, "sta stats missing!");
7351 return NL_SKIP;
7352 }
7353 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
7354 tb[NL80211_ATTR_STA_INFO],
7355 stats_policy)) {
7356 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
7357 return NL_SKIP;
7358 }
7360 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
7361 data->inactive_msec =
7362 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
7363 if (stats[NL80211_STA_INFO_RX_BYTES])
7364 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
7365 if (stats[NL80211_STA_INFO_TX_BYTES])
7366 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
7367 if (stats[NL80211_STA_INFO_RX_PACKETS])
7368 data->rx_packets =
7369 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
7370 if (stats[NL80211_STA_INFO_TX_PACKETS])
7371 data->tx_packets =
7372 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
7374 return NL_SKIP;
7375 }
7377 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
7378 const u8 *addr)
7379 {
7380 struct i802_bss *bss = priv;
7381 struct wpa_driver_nl80211_data *drv = bss->drv;
7382 struct nl_msg *msg;
7384 os_memset(data, 0, sizeof(*data));
7385 msg = nlmsg_alloc();
7386 if (!msg)
7387 return -ENOMEM;
7389 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
7391 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7392 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7394 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
7395 nla_put_failure:
7396 nlmsg_free(msg);
7397 return -ENOBUFS;
7398 }
7401 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
7402 int cw_min, int cw_max, int burst_time)
7403 {
7404 struct i802_bss *bss = priv;
7405 struct wpa_driver_nl80211_data *drv = bss->drv;
7406 struct nl_msg *msg;
7407 struct nlattr *txq, *params;
7409 msg = nlmsg_alloc();
7410 if (!msg)
7411 return -1;
7413 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
7415 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7417 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
7418 if (!txq)
7419 goto nla_put_failure;
7421 /* We are only sending parameters for a single TXQ at a time */
7422 params = nla_nest_start(msg, 1);
7423 if (!params)
7424 goto nla_put_failure;
7426 switch (queue) {
7427 case 0:
7428 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
7429 break;
7430 case 1:
7431 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
7432 break;
7433 case 2:
7434 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
7435 break;
7436 case 3:
7437 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
7438 break;
7439 }
7440 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
7441 * 32 usec, so need to convert the value here. */
7442 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
7443 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
7444 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
7445 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
7447 nla_nest_end(msg, params);
7449 nla_nest_end(msg, txq);
7451 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7452 return 0;
7453 msg = NULL;
7454 nla_put_failure:
7455 nlmsg_free(msg);
7456 return -1;
7457 }
7460 static int i802_set_sta_vlan(void *priv, const u8 *addr,
7461 const char *ifname, int vlan_id)
7462 {
7463 struct i802_bss *bss = priv;
7464 struct wpa_driver_nl80211_data *drv = bss->drv;
7465 struct nl_msg *msg;
7466 int ret = -ENOBUFS;
7468 msg = nlmsg_alloc();
7469 if (!msg)
7470 return -ENOMEM;
7472 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
7474 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7475 if_nametoindex(bss->ifname));
7476 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7477 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
7478 if_nametoindex(ifname));
7480 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7481 msg = NULL;
7482 if (ret < 0) {
7483 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
7484 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
7485 MAC2STR(addr), ifname, vlan_id, ret,
7486 strerror(-ret));
7487 }
7488 nla_put_failure:
7489 nlmsg_free(msg);
7490 return ret;
7491 }
7494 static int i802_get_inact_sec(void *priv, const u8 *addr)
7495 {
7496 struct hostap_sta_driver_data data;
7497 int ret;
7499 data.inactive_msec = (unsigned long) -1;
7500 ret = i802_read_sta_data(priv, &data, addr);
7501 if (ret || data.inactive_msec == (unsigned long) -1)
7502 return -1;
7503 return data.inactive_msec / 1000;
7504 }
7507 static int i802_sta_clear_stats(void *priv, const u8 *addr)
7508 {
7509 #if 0
7510 /* TODO */
7511 #endif
7512 return 0;
7513 }
7516 static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
7517 int reason)
7518 {
7519 struct i802_bss *bss = priv;
7520 struct ieee80211_mgmt mgmt;
7522 memset(&mgmt, 0, sizeof(mgmt));
7523 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
7524 WLAN_FC_STYPE_DEAUTH);
7525 memcpy(mgmt.da, addr, ETH_ALEN);
7526 memcpy(mgmt.sa, own_addr, ETH_ALEN);
7527 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
7528 mgmt.u.deauth.reason_code = host_to_le16(reason);
7529 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
7530 IEEE80211_HDRLEN +
7531 sizeof(mgmt.u.deauth), 0);
7532 }
7535 static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
7536 int reason)
7537 {
7538 struct i802_bss *bss = priv;
7539 struct ieee80211_mgmt mgmt;
7541 memset(&mgmt, 0, sizeof(mgmt));
7542 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
7543 WLAN_FC_STYPE_DISASSOC);
7544 memcpy(mgmt.da, addr, ETH_ALEN);
7545 memcpy(mgmt.sa, own_addr, ETH_ALEN);
7546 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
7547 mgmt.u.disassoc.reason_code = host_to_le16(reason);
7548 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
7549 IEEE80211_HDRLEN +
7550 sizeof(mgmt.u.disassoc), 0);
7551 }
7553 #endif /* HOSTAPD || CONFIG_AP */
7555 #ifdef HOSTAPD
7557 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
7558 {
7559 int i;
7560 int *old;
7562 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
7563 ifidx);
7564 for (i = 0; i < drv->num_if_indices; i++) {
7565 if (drv->if_indices[i] == 0) {
7566 drv->if_indices[i] = ifidx;
7567 return;
7568 }
7569 }
7571 if (drv->if_indices != drv->default_if_indices)
7572 old = drv->if_indices;
7573 else
7574 old = NULL;
7576 drv->if_indices = os_realloc(old,
7577 sizeof(int) * (drv->num_if_indices + 1));
7578 if (!drv->if_indices) {
7579 if (!old)
7580 drv->if_indices = drv->default_if_indices;
7581 else
7582 drv->if_indices = old;
7583 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
7584 "interfaces");
7585 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
7586 return;
7587 } else if (!old)
7588 os_memcpy(drv->if_indices, drv->default_if_indices,
7589 sizeof(drv->default_if_indices));
7590 drv->if_indices[drv->num_if_indices] = ifidx;
7591 drv->num_if_indices++;
7592 }
7595 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
7596 {
7597 int i;
7599 for (i = 0; i < drv->num_if_indices; i++) {
7600 if (drv->if_indices[i] == ifidx) {
7601 drv->if_indices[i] = 0;
7602 break;
7603 }
7604 }
7605 }
7608 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
7609 {
7610 int i;
7612 for (i = 0; i < drv->num_if_indices; i++)
7613 if (drv->if_indices[i] == ifidx)
7614 return 1;
7616 return 0;
7617 }
7620 static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
7621 const char *bridge_ifname)
7622 {
7623 struct i802_bss *bss = priv;
7624 struct wpa_driver_nl80211_data *drv = bss->drv;
7625 char name[IFNAMSIZ + 1];
7627 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
7628 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
7629 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
7630 if (val) {
7631 if (!if_nametoindex(name)) {
7632 if (nl80211_create_iface(drv, name,
7633 NL80211_IFTYPE_AP_VLAN,
7634 NULL, 1) < 0)
7635 return -1;
7636 if (bridge_ifname &&
7637 linux_br_add_if(drv->global->ioctl_sock,
7638 bridge_ifname, name) < 0)
7639 return -1;
7640 }
7641 linux_set_iface_flags(drv->global->ioctl_sock, name, 1);
7642 return i802_set_sta_vlan(priv, addr, name, 0);
7643 } else {
7644 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
7645 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
7646 name);
7647 }
7648 }
7651 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
7652 {
7653 struct wpa_driver_nl80211_data *drv = eloop_ctx;
7654 struct sockaddr_ll lladdr;
7655 unsigned char buf[3000];
7656 int len;
7657 socklen_t fromlen = sizeof(lladdr);
7659 len = recvfrom(sock, buf, sizeof(buf), 0,
7660 (struct sockaddr *)&lladdr, &fromlen);
7661 if (len < 0) {
7662 perror("recv");
7663 return;
7664 }
7666 if (have_ifidx(drv, lladdr.sll_ifindex))
7667 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
7668 }
7671 static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
7672 struct i802_bss *bss,
7673 const char *brname, const char *ifname)
7674 {
7675 int ifindex;
7676 char in_br[IFNAMSIZ];
7678 os_strlcpy(bss->brname, brname, IFNAMSIZ);
7679 ifindex = if_nametoindex(brname);
7680 if (ifindex == 0) {
7681 /*
7682 * Bridge was configured, but the bridge device does
7683 * not exist. Try to add it now.
7684 */
7685 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
7686 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
7687 "bridge interface %s: %s",
7688 brname, strerror(errno));
7689 return -1;
7690 }
7691 bss->added_bridge = 1;
7692 add_ifidx(drv, if_nametoindex(brname));
7693 }
7695 if (linux_br_get(in_br, ifname) == 0) {
7696 if (os_strcmp(in_br, brname) == 0)
7697 return 0; /* already in the bridge */
7699 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
7700 "bridge %s", ifname, in_br);
7701 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
7702 0) {
7703 wpa_printf(MSG_ERROR, "nl80211: Failed to "
7704 "remove interface %s from bridge "
7705 "%s: %s",
7706 ifname, brname, strerror(errno));
7707 return -1;
7708 }
7709 }
7711 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
7712 ifname, brname);
7713 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
7714 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
7715 "into bridge %s: %s",
7716 ifname, brname, strerror(errno));
7717 return -1;
7718 }
7719 bss->added_if_into_bridge = 1;
7721 return 0;
7722 }
7725 static void *i802_init(struct hostapd_data *hapd,
7726 struct wpa_init_params *params)
7727 {
7728 struct wpa_driver_nl80211_data *drv;
7729 struct i802_bss *bss;
7730 size_t i;
7731 char brname[IFNAMSIZ];
7732 int ifindex, br_ifindex;
7733 int br_added = 0;
7735 bss = wpa_driver_nl80211_init(hapd, params->ifname,
7736 params->global_priv);
7737 if (bss == NULL)
7738 return NULL;
7740 drv = bss->drv;
7741 drv->nlmode = NL80211_IFTYPE_AP;
7742 drv->eapol_sock = -1;
7744 if (linux_br_get(brname, params->ifname) == 0) {
7745 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
7746 params->ifname, brname);
7747 br_ifindex = if_nametoindex(brname);
7748 } else {
7749 brname[0] = '\0';
7750 br_ifindex = 0;
7751 }
7753 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
7754 drv->if_indices = drv->default_if_indices;
7755 for (i = 0; i < params->num_bridge; i++) {
7756 if (params->bridge[i]) {
7757 ifindex = if_nametoindex(params->bridge[i]);
7758 if (ifindex)
7759 add_ifidx(drv, ifindex);
7760 if (ifindex == br_ifindex)
7761 br_added = 1;
7762 }
7763 }
7764 if (!br_added && br_ifindex &&
7765 (params->num_bridge == 0 || !params->bridge[0]))
7766 add_ifidx(drv, br_ifindex);
7768 /* start listening for EAPOL on the default AP interface */
7769 add_ifidx(drv, drv->ifindex);
7771 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
7772 goto failed;
7774 if (params->bssid) {
7775 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7776 params->bssid))
7777 goto failed;
7778 }
7780 if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
7781 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
7782 "into AP mode", bss->ifname);
7783 goto failed;
7784 }
7786 if (params->num_bridge && params->bridge[0] &&
7787 i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
7788 goto failed;
7790 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
7791 goto failed;
7793 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
7794 if (drv->eapol_sock < 0) {
7795 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
7796 goto failed;
7797 }
7799 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
7800 {
7801 printf("Could not register read socket for eapol\n");
7802 goto failed;
7803 }
7805 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7806 params->own_addr))
7807 goto failed;
7809 memcpy(bss->addr, params->own_addr, ETH_ALEN);
7811 return bss;
7813 failed:
7814 wpa_driver_nl80211_deinit(bss);
7815 return NULL;
7816 }
7819 static void i802_deinit(void *priv)
7820 {
7821 wpa_driver_nl80211_deinit(priv);
7822 }
7824 #endif /* HOSTAPD */
7827 static enum nl80211_iftype wpa_driver_nl80211_if_type(
7828 enum wpa_driver_if_type type)
7829 {
7830 switch (type) {
7831 case WPA_IF_STATION:
7832 return NL80211_IFTYPE_STATION;
7833 case WPA_IF_P2P_CLIENT:
7834 case WPA_IF_P2P_GROUP:
7835 return NL80211_IFTYPE_P2P_CLIENT;
7836 case WPA_IF_AP_VLAN:
7837 return NL80211_IFTYPE_AP_VLAN;
7838 case WPA_IF_AP_BSS:
7839 return NL80211_IFTYPE_AP;
7840 case WPA_IF_P2P_GO:
7841 return NL80211_IFTYPE_P2P_GO;
7842 }
7843 return -1;
7844 }
7847 #ifdef CONFIG_P2P
7849 static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
7850 {
7851 struct wpa_driver_nl80211_data *drv;
7852 dl_list_for_each(drv, &global->interfaces,
7853 struct wpa_driver_nl80211_data, list) {
7854 if (os_memcmp(addr, drv->first_bss.addr, ETH_ALEN) == 0)
7855 return 1;
7856 }
7857 return 0;
7858 }
7861 static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
7862 u8 *new_addr)
7863 {
7864 unsigned int idx;
7866 if (!drv->global)
7867 return -1;
7869 os_memcpy(new_addr, drv->first_bss.addr, ETH_ALEN);
7870 for (idx = 0; idx < 64; idx++) {
7871 new_addr[0] = drv->first_bss.addr[0] | 0x02;
7872 new_addr[0] ^= idx << 2;
7873 if (!nl80211_addr_in_use(drv->global, new_addr))
7874 break;
7875 }
7876 if (idx == 64)
7877 return -1;
7879 wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
7880 MACSTR, MAC2STR(new_addr));
7882 return 0;
7883 }
7885 #endif /* CONFIG_P2P */
7888 static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
7889 const char *ifname, const u8 *addr,
7890 void *bss_ctx, void **drv_priv,
7891 char *force_ifname, u8 *if_addr,
7892 const char *bridge)
7893 {
7894 struct i802_bss *bss = priv;
7895 struct wpa_driver_nl80211_data *drv = bss->drv;
7896 int ifidx;
7897 #ifdef HOSTAPD
7898 struct i802_bss *new_bss = NULL;
7900 if (type == WPA_IF_AP_BSS) {
7901 new_bss = os_zalloc(sizeof(*new_bss));
7902 if (new_bss == NULL)
7903 return -1;
7904 }
7905 #endif /* HOSTAPD */
7907 if (addr)
7908 os_memcpy(if_addr, addr, ETH_ALEN);
7909 ifidx = nl80211_create_iface(drv, ifname,
7910 wpa_driver_nl80211_if_type(type), addr,
7911 0);
7912 if (ifidx < 0) {
7913 #ifdef HOSTAPD
7914 os_free(new_bss);
7915 #endif /* HOSTAPD */
7916 return -1;
7917 }
7919 if (!addr &&
7920 linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7921 if_addr) < 0) {
7922 nl80211_remove_iface(drv, ifidx);
7923 return -1;
7924 }
7926 #ifdef CONFIG_P2P
7927 if (!addr &&
7928 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
7929 type == WPA_IF_P2P_GO)) {
7930 /* Enforce unique P2P Interface Address */
7931 u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
7933 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7934 own_addr) < 0 ||
7935 linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
7936 new_addr) < 0) {
7937 nl80211_remove_iface(drv, ifidx);
7938 return -1;
7939 }
7940 if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
7941 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
7942 "for P2P group interface");
7943 if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
7944 nl80211_remove_iface(drv, ifidx);
7945 return -1;
7946 }
7947 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
7948 new_addr) < 0) {
7949 nl80211_remove_iface(drv, ifidx);
7950 return -1;
7951 }
7952 }
7953 os_memcpy(if_addr, new_addr, ETH_ALEN);
7954 }
7955 #endif /* CONFIG_P2P */
7957 #ifdef HOSTAPD
7958 if (bridge &&
7959 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
7960 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
7961 "interface %s to a bridge %s", ifname, bridge);
7962 nl80211_remove_iface(drv, ifidx);
7963 os_free(new_bss);
7964 return -1;
7965 }
7967 if (type == WPA_IF_AP_BSS) {
7968 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
7969 {
7970 nl80211_remove_iface(drv, ifidx);
7971 os_free(new_bss);
7972 return -1;
7973 }
7974 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
7975 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
7976 new_bss->ifindex = ifidx;
7977 new_bss->drv = drv;
7978 new_bss->next = drv->first_bss.next;
7979 new_bss->freq = drv->first_bss.freq;
7980 drv->first_bss.next = new_bss;
7981 if (drv_priv)
7982 *drv_priv = new_bss;
7983 nl80211_init_bss(new_bss);
7985 /* Subscribe management frames for this WPA_IF_AP_BSS */
7986 if (nl80211_setup_ap(new_bss))
7987 return -1;
7988 }
7989 #endif /* HOSTAPD */
7991 if (drv->global)
7992 drv->global->if_add_ifindex = ifidx;
7994 return 0;
7995 }
7998 static int wpa_driver_nl80211_if_remove(void *priv,
7999 enum wpa_driver_if_type type,
8000 const char *ifname)
8001 {
8002 struct i802_bss *bss = priv;
8003 struct wpa_driver_nl80211_data *drv = bss->drv;
8004 int ifindex = if_nametoindex(ifname);
8006 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
8007 __func__, type, ifname, ifindex);
8008 if (ifindex <= 0)
8009 return -1;
8011 #ifdef HOSTAPD
8012 if (bss->added_if_into_bridge) {
8013 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
8014 bss->ifname) < 0)
8015 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8016 "interface %s from bridge %s: %s",
8017 bss->ifname, bss->brname, strerror(errno));
8018 }
8019 if (bss->added_bridge) {
8020 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
8021 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
8022 "bridge %s: %s",
8023 bss->brname, strerror(errno));
8024 }
8025 #endif /* HOSTAPD */
8027 nl80211_remove_iface(drv, ifindex);
8029 #ifdef HOSTAPD
8030 if (type != WPA_IF_AP_BSS)
8031 return 0;
8033 if (bss != &drv->first_bss) {
8034 struct i802_bss *tbss;
8036 for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
8037 if (tbss->next == bss) {
8038 tbss->next = bss->next;
8039 /* Unsubscribe management frames */
8040 nl80211_teardown_ap(bss);
8041 nl80211_destroy_bss(bss);
8042 os_free(bss);
8043 bss = NULL;
8044 break;
8045 }
8046 }
8047 if (bss)
8048 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
8049 "BSS %p in the list", __func__, bss);
8050 }
8051 #endif /* HOSTAPD */
8053 return 0;
8054 }
8057 static int cookie_handler(struct nl_msg *msg, void *arg)
8058 {
8059 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8060 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8061 u64 *cookie = arg;
8062 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8063 genlmsg_attrlen(gnlh, 0), NULL);
8064 if (tb[NL80211_ATTR_COOKIE])
8065 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
8066 return NL_SKIP;
8067 }
8070 static int nl80211_send_frame_cmd(struct i802_bss *bss,
8071 unsigned int freq, unsigned int wait,
8072 const u8 *buf, size_t buf_len,
8073 u64 *cookie_out, int no_cck, int no_ack,
8074 int offchanok)
8075 {
8076 struct wpa_driver_nl80211_data *drv = bss->drv;
8077 struct nl_msg *msg;
8078 u64 cookie;
8079 int ret = -1;
8081 msg = nlmsg_alloc();
8082 if (!msg)
8083 return -1;
8085 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
8087 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8088 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
8089 #ifndef ANDROID_BRCM_P2P_PATCH
8090 if (wait)
8091 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
8092 #endif /* ANDROID_BRCM_P2P_PATCH */
8093 if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
8094 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
8095 if (no_cck)
8096 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
8097 if (no_ack)
8098 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
8100 NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
8102 cookie = 0;
8103 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
8104 msg = NULL;
8105 if (ret) {
8106 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
8107 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
8108 freq, wait);
8109 goto nla_put_failure;
8110 }
8111 wpa_printf(MSG_DEBUG, "nl80211: Frame TX command accepted%s; "
8112 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
8113 (long long unsigned int) cookie);
8115 if (cookie_out)
8116 *cookie_out = no_ack ? (u64) -1 : cookie;
8118 nla_put_failure:
8119 nlmsg_free(msg);
8120 return ret;
8121 }
8124 static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq,
8125 unsigned int wait_time,
8126 const u8 *dst, const u8 *src,
8127 const u8 *bssid,
8128 const u8 *data, size_t data_len,
8129 int no_cck)
8130 {
8131 struct i802_bss *bss = priv;
8132 struct wpa_driver_nl80211_data *drv = bss->drv;
8133 int ret = -1;
8134 u8 *buf;
8135 struct ieee80211_hdr *hdr;
8137 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
8138 "freq=%u MHz wait=%d ms no_cck=%d)",
8139 drv->ifindex, freq, wait_time, no_cck);
8141 buf = os_zalloc(24 + data_len);
8142 if (buf == NULL)
8143 return ret;
8144 os_memcpy(buf + 24, data, data_len);
8145 hdr = (struct ieee80211_hdr *) buf;
8146 hdr->frame_control =
8147 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
8148 os_memcpy(hdr->addr1, dst, ETH_ALEN);
8149 os_memcpy(hdr->addr2, src, ETH_ALEN);
8150 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
8152 if (is_ap_interface(drv->nlmode))
8153 ret = wpa_driver_nl80211_send_mlme_freq(priv, buf,
8154 24 + data_len,
8155 0, freq, no_cck, 1,
8156 wait_time);
8157 else
8158 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
8159 24 + data_len,
8160 &drv->send_action_cookie,
8161 no_cck, 0, 1);
8163 os_free(buf);
8164 return ret;
8165 }
8168 static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
8169 {
8170 struct i802_bss *bss = priv;
8171 struct wpa_driver_nl80211_data *drv = bss->drv;
8172 struct nl_msg *msg;
8173 int ret;
8175 msg = nlmsg_alloc();
8176 if (!msg)
8177 return;
8179 nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
8181 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8182 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
8184 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8185 msg = NULL;
8186 if (ret)
8187 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
8188 "(%s)", ret, strerror(-ret));
8190 nla_put_failure:
8191 nlmsg_free(msg);
8192 }
8195 static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
8196 unsigned int duration)
8197 {
8198 struct i802_bss *bss = priv;
8199 struct wpa_driver_nl80211_data *drv = bss->drv;
8200 struct nl_msg *msg;
8201 int ret;
8202 u64 cookie;
8204 msg = nlmsg_alloc();
8205 if (!msg)
8206 return -1;
8208 nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
8210 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8211 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
8212 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
8214 cookie = 0;
8215 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
8216 msg = NULL;
8217 if (ret == 0) {
8218 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
8219 "0x%llx for freq=%u MHz duration=%u",
8220 (long long unsigned int) cookie, freq, duration);
8221 drv->remain_on_chan_cookie = cookie;
8222 drv->pending_remain_on_chan = 1;
8223 return 0;
8224 }
8225 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
8226 "(freq=%d duration=%u): %d (%s)",
8227 freq, duration, ret, strerror(-ret));
8228 nla_put_failure:
8229 nlmsg_free(msg);
8230 return -1;
8231 }
8234 static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
8235 {
8236 struct i802_bss *bss = priv;
8237 struct wpa_driver_nl80211_data *drv = bss->drv;
8238 struct nl_msg *msg;
8239 int ret;
8241 if (!drv->pending_remain_on_chan) {
8242 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
8243 "to cancel");
8244 return -1;
8245 }
8247 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
8248 "0x%llx",
8249 (long long unsigned int) drv->remain_on_chan_cookie);
8251 msg = nlmsg_alloc();
8252 if (!msg)
8253 return -1;
8255 nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
8257 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8258 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
8260 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8261 msg = NULL;
8262 if (ret == 0)
8263 return 0;
8264 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
8265 "%d (%s)", ret, strerror(-ret));
8266 nla_put_failure:
8267 nlmsg_free(msg);
8268 return -1;
8269 }
8272 static int wpa_driver_nl80211_probe_req_report(void *priv, int report)
8273 {
8274 struct i802_bss *bss = priv;
8275 struct wpa_driver_nl80211_data *drv = bss->drv;
8277 if (drv->nlmode != NL80211_IFTYPE_STATION) {
8278 wpa_printf(MSG_DEBUG, "nl80211: probe_req_report control only "
8279 "allowed in station mode (iftype=%d)",
8280 drv->nlmode);
8281 return -1;
8282 }
8284 if (!report) {
8285 if (bss->nl_preq && drv->device_ap_sme &&
8286 is_ap_interface(drv->nlmode)) {
8287 /*
8288 * Do not disable Probe Request reporting that was
8289 * enabled in nl80211_setup_ap().
8290 */
8291 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
8292 "Probe Request reporting nl_preq=%p while "
8293 "in AP mode", bss->nl_preq);
8294 } else if (bss->nl_preq) {
8295 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
8296 "reporting nl_preq=%p", bss->nl_preq);
8297 eloop_unregister_read_sock(
8298 nl_socket_get_fd(bss->nl_preq));
8299 nl_destroy_handles(&bss->nl_preq);
8300 }
8301 return 0;
8302 }
8304 if (bss->nl_preq) {
8305 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
8306 "already on! nl_preq=%p", bss->nl_preq);
8307 return 0;
8308 }
8310 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
8311 if (bss->nl_preq == NULL)
8312 return -1;
8313 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
8314 "reporting nl_preq=%p", bss->nl_preq);
8316 if (nl80211_register_frame(bss, bss->nl_preq,
8317 (WLAN_FC_TYPE_MGMT << 2) |
8318 (WLAN_FC_STYPE_PROBE_REQ << 4),
8319 NULL, 0) < 0)
8320 goto out_err;
8322 #ifdef ANDROID_BRCM_P2P_PATCH
8323 if (nl80211_register_frame(bss, bss->nl_preq,
8324 (WLAN_FC_TYPE_MGMT << 2) |
8325 (WLAN_FC_STYPE_ASSOC_REQ << 4),
8326 NULL, 0) < 0) {
8327 goto out_err;
8328 }
8330 if (nl80211_register_frame(bss, bss->nl_preq,
8331 (WLAN_FC_TYPE_MGMT << 2) |
8332 (WLAN_FC_STYPE_REASSOC_REQ << 4),
8333 NULL, 0) < 0) {
8334 goto out_err;
8335 }
8337 if (nl80211_register_frame(bss, bss->nl_preq,
8338 (WLAN_FC_TYPE_MGMT << 2) |
8339 (WLAN_FC_STYPE_DISASSOC << 4),
8340 NULL, 0) < 0) {
8341 goto out_err;
8342 }
8344 if (nl80211_register_frame(bss, bss->nl_preq,
8345 (WLAN_FC_TYPE_MGMT << 2) |
8346 (WLAN_FC_STYPE_DEAUTH << 4),
8347 NULL, 0) < 0) {
8348 goto out_err;
8349 }
8350 #endif /* ANDROID_BRCM_P2P_PATCH */
8352 eloop_register_read_sock(nl_socket_get_fd(bss->nl_preq),
8353 wpa_driver_nl80211_event_receive, bss->nl_cb,
8354 bss->nl_preq);
8356 return 0;
8358 out_err:
8359 nl_destroy_handles(&bss->nl_preq);
8360 return -1;
8361 }
8364 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
8365 int ifindex, int disabled)
8366 {
8367 struct nl_msg *msg;
8368 struct nlattr *bands, *band;
8369 int ret;
8371 msg = nlmsg_alloc();
8372 if (!msg)
8373 return -1;
8375 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
8376 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
8378 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
8379 if (!bands)
8380 goto nla_put_failure;
8382 /*
8383 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
8384 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
8385 * rates. All 5 GHz rates are left enabled.
8386 */
8387 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
8388 if (!band)
8389 goto nla_put_failure;
8390 if (disabled) {
8391 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
8392 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
8393 }
8394 nla_nest_end(msg, band);
8396 nla_nest_end(msg, bands);
8398 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8399 msg = NULL;
8400 if (ret) {
8401 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
8402 "(%s)", ret, strerror(-ret));
8403 }
8405 return ret;
8407 nla_put_failure:
8408 nlmsg_free(msg);
8409 return -1;
8410 }
8413 static int wpa_driver_nl80211_deinit_ap(void *priv)
8414 {
8415 struct i802_bss *bss = priv;
8416 struct wpa_driver_nl80211_data *drv = bss->drv;
8417 if (!is_ap_interface(drv->nlmode))
8418 return -1;
8419 wpa_driver_nl80211_del_beacon(drv);
8420 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
8421 }
8423 static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
8424 {
8425 struct i802_bss *bss = priv;
8426 struct wpa_driver_nl80211_data *drv = bss->drv;
8427 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
8428 return -1;
8429 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
8430 }
8432 static void wpa_driver_nl80211_resume(void *priv)
8433 {
8434 struct i802_bss *bss = priv;
8435 struct wpa_driver_nl80211_data *drv = bss->drv;
8436 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
8437 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
8438 "resume event");
8439 }
8440 }
8443 static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
8444 const u8 *ies, size_t ies_len)
8445 {
8446 struct i802_bss *bss = priv;
8447 struct wpa_driver_nl80211_data *drv = bss->drv;
8448 int ret;
8449 u8 *data, *pos;
8450 size_t data_len;
8451 const u8 *own_addr = bss->addr;
8453 if (action != 1) {
8454 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
8455 "action %d", action);
8456 return -1;
8457 }
8459 /*
8460 * Action frame payload:
8461 * Category[1] = 6 (Fast BSS Transition)
8462 * Action[1] = 1 (Fast BSS Transition Request)
8463 * STA Address
8464 * Target AP Address
8465 * FT IEs
8466 */
8468 data_len = 2 + 2 * ETH_ALEN + ies_len;
8469 data = os_malloc(data_len);
8470 if (data == NULL)
8471 return -1;
8472 pos = data;
8473 *pos++ = 0x06; /* FT Action category */
8474 *pos++ = action;
8475 os_memcpy(pos, own_addr, ETH_ALEN);
8476 pos += ETH_ALEN;
8477 os_memcpy(pos, target_ap, ETH_ALEN);
8478 pos += ETH_ALEN;
8479 os_memcpy(pos, ies, ies_len);
8481 ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
8482 drv->bssid, own_addr, drv->bssid,
8483 data, data_len, 0);
8484 os_free(data);
8486 return ret;
8487 }
8490 static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
8491 {
8492 struct i802_bss *bss = priv;
8493 struct wpa_driver_nl80211_data *drv = bss->drv;
8494 struct nl_msg *msg, *cqm = NULL;
8496 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
8497 "hysteresis=%d", threshold, hysteresis);
8499 msg = nlmsg_alloc();
8500 if (!msg)
8501 return -1;
8503 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
8505 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8507 cqm = nlmsg_alloc();
8508 if (cqm == NULL)
8509 return -1;
8511 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
8512 NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
8513 nla_put_nested(msg, NL80211_ATTR_CQM, cqm);
8515 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
8516 return 0;
8517 msg = NULL;
8519 nla_put_failure:
8520 nlmsg_free(cqm);
8521 nlmsg_free(msg);
8522 return -1;
8523 }
8526 static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
8527 {
8528 struct i802_bss *bss = priv;
8529 struct wpa_driver_nl80211_data *drv = bss->drv;
8530 int res;
8532 os_memset(si, 0, sizeof(*si));
8533 res = nl80211_get_link_signal(drv, si);
8534 if (res != 0)
8535 return res;
8537 return nl80211_get_link_noise(drv, si);
8538 }
8541 static int wpa_driver_nl80211_shared_freq(void *priv)
8542 {
8543 struct i802_bss *bss = priv;
8544 struct wpa_driver_nl80211_data *drv = bss->drv;
8545 struct wpa_driver_nl80211_data *driver;
8546 int freq = 0;
8548 /*
8549 * If the same PHY is in connected state with some other interface,
8550 * then retrieve the assoc freq.
8551 */
8552 wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
8553 drv->phyname);
8555 dl_list_for_each(driver, &drv->global->interfaces,
8556 struct wpa_driver_nl80211_data, list) {
8557 if (drv == driver ||
8558 os_strcmp(drv->phyname, driver->phyname) != 0 ||
8559 !driver->associated)
8560 continue;
8562 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
8563 MACSTR,
8564 driver->phyname, driver->first_bss.ifname,
8565 MAC2STR(driver->first_bss.addr));
8566 freq = nl80211_get_assoc_freq(driver);
8567 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
8568 drv->phyname, freq);
8569 }
8571 if (!freq)
8572 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
8573 "PHY (%s) in associated state", drv->phyname);
8575 return freq;
8576 }
8579 static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
8580 int encrypt)
8581 {
8582 struct i802_bss *bss = priv;
8583 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
8584 0, 0, 0, 0);
8585 }
8588 static int nl80211_set_param(void *priv, const char *param)
8589 {
8590 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
8591 if (param == NULL)
8592 return 0;
8594 #ifdef CONFIG_P2P
8595 if (os_strstr(param, "use_p2p_group_interface=1")) {
8596 struct i802_bss *bss = priv;
8597 struct wpa_driver_nl80211_data *drv = bss->drv;
8599 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
8600 "interface");
8601 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
8602 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
8603 }
8604 #endif /* CONFIG_P2P */
8606 return 0;
8607 }
8610 static void * nl80211_global_init(void)
8611 {
8612 struct nl80211_global *global;
8613 struct netlink_config *cfg;
8615 global = os_zalloc(sizeof(*global));
8616 if (global == NULL)
8617 return NULL;
8618 global->ioctl_sock = -1;
8619 dl_list_init(&global->interfaces);
8620 global->if_add_ifindex = -1;
8622 cfg = os_zalloc(sizeof(*cfg));
8623 if (cfg == NULL)
8624 goto err;
8626 cfg->ctx = global;
8627 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
8628 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
8629 global->netlink = netlink_init(cfg);
8630 if (global->netlink == NULL) {
8631 os_free(cfg);
8632 goto err;
8633 }
8635 if (wpa_driver_nl80211_init_nl_global(global) < 0)
8636 goto err;
8638 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
8639 if (global->ioctl_sock < 0) {
8640 perror("socket(PF_INET,SOCK_DGRAM)");
8641 goto err;
8642 }
8644 return global;
8646 err:
8647 nl80211_global_deinit(global);
8648 return NULL;
8649 }
8652 static void nl80211_global_deinit(void *priv)
8653 {
8654 struct nl80211_global *global = priv;
8655 if (global == NULL)
8656 return;
8657 if (!dl_list_empty(&global->interfaces)) {
8658 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
8659 "nl80211_global_deinit",
8660 dl_list_len(&global->interfaces));
8661 }
8663 if (global->netlink)
8664 netlink_deinit(global->netlink);
8666 nl_destroy_handles(&global->nl);
8668 if (global->nl_event) {
8669 eloop_unregister_read_sock(
8670 nl_socket_get_fd(global->nl_event));
8671 nl_destroy_handles(&global->nl_event);
8672 }
8674 nl_cb_put(global->nl_cb);
8676 if (global->ioctl_sock >= 0)
8677 close(global->ioctl_sock);
8679 os_free(global);
8680 }
8683 static const char * nl80211_get_radio_name(void *priv)
8684 {
8685 struct i802_bss *bss = priv;
8686 struct wpa_driver_nl80211_data *drv = bss->drv;
8687 return drv->phyname;
8688 }
8691 static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
8692 const u8 *pmkid)
8693 {
8694 struct nl_msg *msg;
8696 msg = nlmsg_alloc();
8697 if (!msg)
8698 return -ENOMEM;
8700 nl80211_cmd(bss->drv, msg, 0, cmd);
8702 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
8703 if (pmkid)
8704 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
8705 if (bssid)
8706 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
8708 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8709 nla_put_failure:
8710 nlmsg_free(msg);
8711 return -ENOBUFS;
8712 }
8714 #ifdef ANDROID
8716 #define MAX_PATTERN_SIZE 256
8717 #define MAX_MASK_SIZE (MAX_PATTERN_SIZE/8)
8719 /* Describes a single RX filter configuration */
8720 struct rx_filter {
8721 /* name - A human recongmizable name for the filter */
8722 char *name;
8724 /* get_pattern_handler - A handler which enables the user to configure
8725 * the pattern dynamically (For example filter according to the HW addr).
8726 * If NULL the static pattern configured will be used.
8727 * buf - the pattern will be copied to buf
8728 * buflen - the size of buf
8729 * arg - A generic input argumet which can be passed to the handler
8730 */
8731 int (* get_pattern_handler) (u8 *buf, int buflen, void* arg);
8733 /* pattern - A static pattern to filter
8734 * This array contains the bytes of the pattern. The mask field
8735 * indicates which bytes should be used in the filter and which
8736 * can be discarded
8737 */
8738 u8 pattern[MAX_PATTERN_SIZE];
8740 /* pattern_len - The number of bytes used in pattern */
8741 u8 pattern_len;
8743 /* mask - A bit mask indicating which bytes in pattern should be
8744 * used for filtering. Each bit here corresponds to a byte in pattern
8745 */
8746 u8 mask[MAX_MASK_SIZE];
8748 /* mask_len - The number of bytes used in mask */
8749 u8 mask_len;
8750 };
8752 static u8 *nl80211_rx_filter_get_pattern(struct rx_filter *filter, void *arg)
8753 {
8754 if (filter->get_pattern_handler) {
8755 if (filter->get_pattern_handler(filter->pattern,
8756 filter->pattern_len, arg)) {
8757 return NULL;
8758 }
8759 }
8761 return filter->pattern;
8762 }
8764 static int
8765 nl80211_self_filter_get_pattern_handler(u8 *buf, int buflen, void *arg)
8766 {
8767 int ret;
8768 struct i802_bss *bss = (struct i802_bss *)arg;
8770 ret = linux_get_ifhwaddr(bss->drv->global->ioctl_sock,
8771 bss->ifname, buf);
8772 if (ret) {
8773 wpa_printf(MSG_ERROR, "Failed to get own HW addr (%d)", ret);
8774 return -1;
8775 }
8776 return 0;
8777 }
8779 static struct rx_filter rx_filters[] = {
8780 /* ID 0 */
8781 {.name = "self",
8782 .pattern = {},
8783 .pattern_len = 6,
8784 .mask = { BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5) },
8785 .mask_len = 1,
8786 .get_pattern_handler = nl80211_self_filter_get_pattern_handler,
8787 },
8789 /* ID 1 */
8790 {.name = "bcast",
8791 .pattern = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
8792 .pattern_len = 6,
8793 .mask = { BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5) },
8794 .mask_len = 1,
8795 },
8797 /* ID 2 */
8798 {.name = "ipv4mc",
8799 .pattern = {0x01,0x00,0x5E},
8800 .pattern_len = 3,
8801 .mask = { BIT(0) | BIT(1) | BIT(2) },
8802 .mask_len = 1,
8803 },
8805 /* ID 3 */
8806 {.name = "ipv6mc",
8807 .pattern = {0x33,0x33},
8808 .pattern_len = 2,
8809 .mask = { BIT(0) | BIT(1) },
8810 .mask_len = 1,
8811 },
8813 /* ID 4 */
8814 {.name = "dhcp",
8815 .pattern = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0 , 0 ,
8816 0 , 0 , 0 , 0 , 0 , 0 , 0x45, 0 ,
8817 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0x11,
8818 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
8819 0 , 0 , 0 , 0 , 0x00, 0x44},
8820 .pattern_len = 38,
8821 .mask = { BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5),
8822 BIT(6), /* OCTET 2 */
8823 BIT(7), /* OCTET 3 */
8824 0, /* OCTET 4 */
8825 BIT(4) | BIT(5) }, /* OCTET 5 */
8826 .mask_len = 5,
8827 },
8829 /* ID 5 */
8830 {.name = "arp",
8831 .pattern = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,
8832 0 , 0 , 0 , 0 , 0x08, 0x06},
8833 .pattern_len = 14,
8834 .mask = { 0, /* OCTET 1 */
8835 BIT(4) | BIT(5) }, /* OCTET 2 */
8836 .mask_len = 2,
8837 },
8839 /* ID 6 */
8840 {.name = "ssdp",
8841 .pattern = {0x01, 0x00, 0x5E, 0 , 0 , 0 , 0 , 0 ,
8842 0 , 0 , 0 , 0 , 0 , 0 , 0x45, 0 ,
8843 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0x11,
8844 0 , 0 , 0 , 0 , 0 , 0 , 0xEF, 0xFF,
8845 0xFF, 0xFA, 0 , 0 , 0x07, 0x6C},
8846 .pattern_len = 38,
8847 .mask = { BIT(0) | BIT(1) | BIT(2), /* OCTET 1 */
8848 BIT(6), /* OCTET 2 */
8849 BIT(7), /* OCTET 3 */
8850 BIT(6) | BIT(7), /* OCTET 4 */
8851 BIT(0) | BIT(1) | BIT(4) | BIT(5) }, /* OCTET 5 */
8852 .mask_len = 5,
8853 },
8854 };
8856 #define NR_RX_FILTERS (int)(sizeof(rx_filters) / sizeof(struct rx_filter))
8858 static int nl80211_set_wowlan_triggers(struct i802_bss *bss, int enable)
8859 {
8860 struct nl_msg *msg, *pats = NULL;
8861 struct nlattr *wowtrig, *pat;
8862 int i, ret = -1;
8863 int filters;
8865 bss->drv->wowlan_enabled = !!enable;
8867 msg = nlmsg_alloc();
8868 if (!msg)
8869 return -ENOMEM;
8871 genlmsg_put(msg, 0, 0, bss->drv->global->nl80211_id, 0,
8872 0, NL80211_CMD_SET_WOWLAN, 0);
8874 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->drv->first_bss.ifindex);
8875 wowtrig = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
8877 if (!wowtrig) {
8878 ret = -ENOBUFS;
8879 goto nla_put_failure;
8880 }
8882 if (!enable) {
8883 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
8884 } else {
8885 pats = nlmsg_alloc();
8886 if (!pats) {
8887 ret = -ENOMEM;
8888 goto nla_put_failure;
8889 }
8891 /* In ginger filter 0 and 1 are always set but in ICS they
8892 were completely removed. Make sure to always set them
8893 otherwise unicast and bcast are dropped */
8894 filters = bss->drv->wowlan_triggers |= 3;
8896 for (i = 0; i < NR_RX_FILTERS; i++) {
8897 struct rx_filter *rx_filter = &rx_filters[i];
8898 int patnr = 1;
8899 u8 *pattern;
8901 if (!(filters & (1 << i)))
8902 continue;
8904 pattern = nl80211_rx_filter_get_pattern(rx_filter, bss);
8905 if (!pattern)
8906 continue;
8908 pat = nla_nest_start(pats, patnr++);
8909 NLA_PUT(pats, NL80211_WOWLAN_PKTPAT_MASK,
8910 rx_filter->mask_len,
8911 rx_filter->mask);
8913 NLA_PUT(pats, NL80211_WOWLAN_PKTPAT_PATTERN,
8914 rx_filter->pattern_len,
8915 pattern);
8917 nla_nest_end(pats, pat);
8918 }
8919 }
8921 if (pats)
8922 nla_put_nested(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN, pats);
8924 nla_nest_end(msg, wowtrig);
8926 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8928 if (ret < 0)
8929 wpa_printf(MSG_ERROR, "Failed to set WoWLAN trigger:%d\n", ret);
8931 if (pats)
8932 nlmsg_free(pats);
8934 return 0;
8936 nla_put_failure:
8937 nlmsg_free(msg);
8938 return ret;
8939 }
8941 static int nl80211_toggle_wowlan_trigger(struct i802_bss *bss, int nr,
8942 int enabled)
8943 {
8944 if (nr >= NR_RX_FILTERS) {
8945 wpa_printf(MSG_ERROR, "Unknown filter: %d\n", nr);
8946 return -1;
8947 }
8949 if (enabled)
8950 bss->drv->wowlan_triggers |= 1 << nr;
8951 else
8952 bss->drv->wowlan_triggers &= ~(1 << nr);
8954 if (bss->drv->wowlan_enabled)
8955 nl80211_set_wowlan_triggers(bss, 1);
8957 return 0;
8958 }
8960 static int nl80211_parse_wowlan_trigger_nr(char *s)
8961 {
8962 long i;
8963 char *endp;
8965 i = strtol(s, &endp, 10);
8967 if(endp == s)
8968 return -1;
8969 return i;
8970 }
8972 static int nl80211_toggle_dropbcast(int enable)
8973 {
8974 char filename[90];
8975 int rv;
8976 FILE *f;
8978 snprintf(filename, sizeof(filename) - 1,
8979 "/sys/bus/platform/devices/wl12xx/drop_bcast");
8980 f = fopen(filename, "w");
8981 if (f < 0) {
8982 wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
8983 filename, strerror(errno));
8984 return -1;
8985 }
8987 rv = fprintf(f, "%d", enable);
8988 fclose(f);
8989 if (rv < 1) {
8990 wpa_printf(MSG_DEBUG, "Could not write to file %s: %s",
8991 filename, strerror(errno));
8992 return -1;
8993 }
8995 return 0;
8996 }
8998 static int nl80211_dropbcast_get(char *buf, size_t buf_len)
8999 {
9000 char filename[90], value[10], *pos;
9001 int f, rv;
9003 snprintf(filename, sizeof(filename) - 1,
9004 "/sys/bus/platform/devices/wl12xx/drop_bcast");
9005 f = open(filename, O_RDONLY);
9006 if (f < 0) {
9007 wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
9008 filename, strerror(errno));
9009 return -1;
9010 }
9012 rv = read(f, value, sizeof(value) - 1);
9013 close(f);
9014 if (rv < 0) {
9015 wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
9016 filename, strerror(errno));
9017 return -1;
9018 }
9020 value[rv] = '\0';
9021 pos = os_strchr(value, '\n');
9022 if (pos)
9023 *pos = '\0';
9025 return snprintf(buf, buf_len, "Drop bcast = %s\n", value);
9026 }
9028 #endif /* ANDROID */
9030 static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9031 {
9032 struct i802_bss *bss = priv;
9033 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
9034 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
9035 }
9038 static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
9039 {
9040 struct i802_bss *bss = priv;
9041 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
9042 MAC2STR(bssid));
9043 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
9044 }
9047 static int nl80211_flush_pmkid(void *priv)
9048 {
9049 struct i802_bss *bss = priv;
9050 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
9051 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
9052 }
9055 static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
9056 const u8 *replay_ctr)
9057 {
9058 struct i802_bss *bss = priv;
9059 struct wpa_driver_nl80211_data *drv = bss->drv;
9060 struct nlattr *replay_nested;
9061 struct nl_msg *msg;
9063 msg = nlmsg_alloc();
9064 if (!msg)
9065 return;
9067 nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
9069 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9071 replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
9072 if (!replay_nested)
9073 goto nla_put_failure;
9075 NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
9076 NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
9077 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
9078 replay_ctr);
9080 nla_nest_end(msg, replay_nested);
9082 send_and_recv_msgs(drv, msg, NULL, NULL);
9083 return;
9084 nla_put_failure:
9085 nlmsg_free(msg);
9086 }
9089 static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
9090 const u8 *addr, int qos)
9091 {
9092 /* send data frame to poll STA and check whether
9093 * this frame is ACKed */
9094 struct {
9095 struct ieee80211_hdr hdr;
9096 u16 qos_ctl;
9097 } STRUCT_PACKED nulldata;
9098 size_t size;
9100 /* Send data frame to poll STA and check whether this frame is ACKed */
9102 os_memset(&nulldata, 0, sizeof(nulldata));
9104 if (qos) {
9105 nulldata.hdr.frame_control =
9106 IEEE80211_FC(WLAN_FC_TYPE_DATA,
9107 WLAN_FC_STYPE_QOS_NULL);
9108 size = sizeof(nulldata);
9109 } else {
9110 nulldata.hdr.frame_control =
9111 IEEE80211_FC(WLAN_FC_TYPE_DATA,
9112 WLAN_FC_STYPE_NULLFUNC);
9113 size = sizeof(struct ieee80211_hdr);
9114 }
9116 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
9117 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
9118 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
9119 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
9121 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0) < 0)
9122 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
9123 "send poll frame");
9124 }
9126 static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
9127 int qos)
9128 {
9129 struct i802_bss *bss = priv;
9130 struct wpa_driver_nl80211_data *drv = bss->drv;
9131 struct nl_msg *msg;
9133 if (!drv->poll_command_supported) {
9134 nl80211_send_null_frame(bss, own_addr, addr, qos);
9135 return;
9136 }
9138 msg = nlmsg_alloc();
9139 if (!msg)
9140 return;
9142 nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
9144 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9145 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
9147 send_and_recv_msgs(drv, msg, NULL, NULL);
9148 return;
9149 nla_put_failure:
9150 nlmsg_free(msg);
9151 }
9154 static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
9155 {
9156 struct nl_msg *msg;
9158 msg = nlmsg_alloc();
9159 if (!msg)
9160 return -ENOMEM;
9162 nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
9163 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9164 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
9165 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
9166 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
9167 nla_put_failure:
9168 nlmsg_free(msg);
9169 return -ENOBUFS;
9170 }
9173 static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
9174 int ctwindow)
9175 {
9176 #ifdef ANDROID_BRCM_P2P_PATCH
9177 char buf[MAX_DRV_CMD_SIZE];
9179 memset(buf, 0, sizeof(buf));
9180 wpa_printf(MSG_DEBUG, "%s: Entry", __func__);
9181 os_snprintf(buf, sizeof(buf), "P2P_SET_PS %d %d %d", legacy_ps, opp_ps,
9182 ctwindow);
9183 return wpa_driver_nl80211_driver_cmd(priv, buf, buf, strlen(buf) + 1);
9184 #else /* ANDROID_BRCM_P2P_PATCH */
9185 struct i802_bss *bss = priv;
9187 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
9188 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
9190 if (opp_ps != -1 || ctwindow != -1)
9191 return -1; /* Not yet supported */
9193 if (legacy_ps == -1)
9194 return 0;
9195 if (legacy_ps != 0 && legacy_ps != 1)
9196 return -1; /* Not yet supported */
9198 return nl80211_set_power_save(bss, legacy_ps);
9199 #endif /* ANDROID_BRCM_P2P_PATCH */
9200 }
9203 #ifdef CONFIG_TDLS
9205 static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
9206 u8 dialog_token, u16 status_code,
9207 const u8 *buf, size_t len)
9208 {
9209 struct i802_bss *bss = priv;
9210 struct wpa_driver_nl80211_data *drv = bss->drv;
9211 struct nl_msg *msg;
9213 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
9214 return -EOPNOTSUPP;
9216 if (!dst)
9217 return -EINVAL;
9219 msg = nlmsg_alloc();
9220 if (!msg)
9221 return -ENOMEM;
9223 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
9224 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9225 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
9226 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
9227 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
9228 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
9229 NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
9231 return send_and_recv_msgs(drv, msg, NULL, NULL);
9233 nla_put_failure:
9234 nlmsg_free(msg);
9235 return -ENOBUFS;
9236 }
9239 static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
9240 {
9241 struct i802_bss *bss = priv;
9242 struct wpa_driver_nl80211_data *drv = bss->drv;
9243 struct nl_msg *msg;
9244 enum nl80211_tdls_operation nl80211_oper;
9246 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
9247 return -EOPNOTSUPP;
9249 switch (oper) {
9250 case TDLS_DISCOVERY_REQ:
9251 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
9252 break;
9253 case TDLS_SETUP:
9254 nl80211_oper = NL80211_TDLS_SETUP;
9255 break;
9256 case TDLS_TEARDOWN:
9257 nl80211_oper = NL80211_TDLS_TEARDOWN;
9258 break;
9259 case TDLS_ENABLE_LINK:
9260 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
9261 break;
9262 case TDLS_DISABLE_LINK:
9263 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
9264 break;
9265 case TDLS_ENABLE:
9266 return 0;
9267 case TDLS_DISABLE:
9268 return 0;
9269 default:
9270 return -EINVAL;
9271 }
9273 msg = nlmsg_alloc();
9274 if (!msg)
9275 return -ENOMEM;
9277 nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
9278 NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
9279 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
9280 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
9282 return send_and_recv_msgs(drv, msg, NULL, NULL);
9284 nla_put_failure:
9285 nlmsg_free(msg);
9286 return -ENOBUFS;
9287 }
9289 #endif /* CONFIG TDLS */
9292 #ifdef ANDROID
9294 typedef struct android_wifi_priv_cmd {
9295 char *buf;
9296 int used_len;
9297 int total_len;
9298 } android_wifi_priv_cmd;
9300 static int drv_errors = 0;
9302 static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
9303 {
9304 drv_errors++;
9305 if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
9306 drv_errors = 0;
9307 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
9308 }
9309 }
9312 #define WPA_PS_ENABLED 0
9313 #define WPA_PS_DISABLED 1
9315 static int wpa_driver_set_power_save(void *priv, int state)
9316 {
9317 return nl80211_set_power_save(priv, state == WPA_PS_ENABLED);
9318 }
9321 static int get_power_mode_handler(struct nl_msg *msg, void *arg)
9322 {
9323 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9324 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9325 int *state = arg;
9327 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9328 genlmsg_attrlen(gnlh, 0), NULL);
9330 if (!tb[NL80211_ATTR_PS_STATE])
9331 return NL_SKIP;
9333 if (state) {
9334 int s = (int) nla_get_u32(tb[NL80211_ATTR_PS_STATE]);
9335 wpa_printf(MSG_DEBUG, "nl80211: Get power mode = %d", s);
9336 *state = (s == NL80211_PS_ENABLED) ?
9337 WPA_PS_ENABLED : WPA_PS_DISABLED;
9338 }
9340 return NL_SKIP;
9341 }
9344 static int wpa_driver_get_power_save(void *priv, int *state)
9345 {
9346 struct i802_bss *bss = priv;
9347 struct wpa_driver_nl80211_data *drv = bss->drv;
9348 struct nl_msg *msg;
9349 int ret = -1;
9350 enum nl80211_ps_state ps_state;
9352 msg = nlmsg_alloc();
9353 if (!msg)
9354 return -1;
9356 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_POWER_SAVE);
9358 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
9360 ret = send_and_recv_msgs(drv, msg, get_power_mode_handler, state);
9361 msg = NULL;
9362 if (ret < 0)
9363 wpa_printf(MSG_ERROR, "nl80211: Get power mode fail: %d", ret);
9364 nla_put_failure:
9365 nlmsg_free(msg);
9366 return ret;
9367 }
9370 static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
9371 {
9372 struct wpa_driver_nl80211_data *drv = bss->drv;
9373 struct ifreq ifr;
9374 android_wifi_priv_cmd priv_cmd;
9375 char buf[MAX_DRV_CMD_SIZE];
9376 int ret;
9378 os_memset(&ifr, 0, sizeof(ifr));
9379 os_memset(&priv_cmd, 0, sizeof(priv_cmd));
9380 os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
9382 os_memset(buf, 0, sizeof(buf));
9383 os_strlcpy(buf, cmd, sizeof(buf));
9385 priv_cmd.buf = buf;
9386 priv_cmd.used_len = sizeof(buf);
9387 priv_cmd.total_len = sizeof(buf);
9388 ifr.ifr_data = &priv_cmd;
9390 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9391 if (ret < 0) {
9392 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
9393 __func__);
9394 wpa_driver_send_hang_msg(drv);
9395 return ret;
9396 }
9398 drv_errors = 0;
9399 return 0;
9400 }
9403 static int android_pno_start(struct i802_bss *bss,
9404 struct wpa_driver_scan_params *params)
9405 {
9406 struct wpa_driver_nl80211_data *drv = bss->drv;
9407 struct ifreq ifr;
9408 android_wifi_priv_cmd priv_cmd;
9409 int ret = 0, i = 0, bp;
9410 char buf[WEXT_PNO_MAX_COMMAND_SIZE];
9412 bp = WEXT_PNOSETUP_HEADER_SIZE;
9413 os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
9414 buf[bp++] = WEXT_PNO_TLV_PREFIX;
9415 buf[bp++] = WEXT_PNO_TLV_VERSION;
9416 buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
9417 buf[bp++] = WEXT_PNO_TLV_RESERVED;
9419 while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
9420 /* Check that there is enough space needed for 1 more SSID, the
9421 * other sections and null termination */
9422 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
9423 WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
9424 break;
9425 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
9426 params->ssids[i].ssid,
9427 params->ssids[i].ssid_len);
9428 buf[bp++] = WEXT_PNO_SSID_SECTION;
9429 buf[bp++] = params->ssids[i].ssid_len;
9430 os_memcpy(&buf[bp], params->ssids[i].ssid,
9431 params->ssids[i].ssid_len);
9432 bp += params->ssids[i].ssid_len;
9433 i++;
9434 }
9436 buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
9437 os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
9438 WEXT_PNO_SCAN_INTERVAL);
9439 bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
9441 buf[bp++] = WEXT_PNO_REPEAT_SECTION;
9442 os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
9443 WEXT_PNO_REPEAT);
9444 bp += WEXT_PNO_REPEAT_LENGTH;
9446 buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
9447 os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
9448 WEXT_PNO_MAX_REPEAT);
9449 bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
9451 memset(&ifr, 0, sizeof(ifr));
9452 memset(&priv_cmd, 0, sizeof(priv_cmd));
9453 os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
9455 priv_cmd.buf = buf;
9456 priv_cmd.used_len = bp;
9457 priv_cmd.total_len = bp;
9458 ifr.ifr_data = &priv_cmd;
9460 ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
9462 if (ret < 0) {
9463 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
9464 ret);
9465 wpa_driver_send_hang_msg(drv);
9466 return ret;
9467 }
9469 drv_errors = 0;
9471 return android_priv_cmd(bss, "PNOFORCE 1");
9472 }
9475 static int android_pno_stop(struct i802_bss *bss)
9476 {
9477 return android_priv_cmd(bss, "PNOFORCE 0");
9478 }
9481 static int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
9482 size_t buf_len)
9483 {
9484 struct i802_bss *bss = priv;
9485 struct wpa_driver_nl80211_data *drv = bss->drv;
9486 struct ifreq ifr;
9487 android_wifi_priv_cmd priv_cmd;
9488 int ret = 0;
9490 if (os_strcasecmp(cmd, "STOP") == 0) {
9491 linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
9492 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "STOPPED");
9493 } else if (os_strcasecmp(cmd, "START") == 0) {
9494 linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
9495 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "STARTED");
9496 } else if (os_strcasecmp(cmd, "MACADDR") == 0) {
9497 u8 macaddr[ETH_ALEN] = {};
9499 ret = linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
9500 macaddr);
9501 if (!ret)
9502 ret = os_snprintf(buf, buf_len,
9503 "Macaddr = " MACSTR "\n",
9504 MAC2STR(macaddr));
9505 } else if (os_strcasecmp(cmd, "RELOAD") == 0) {
9506 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
9507 } else if (os_strncasecmp(cmd, "POWERMODE ", 10) == 0) {
9508 int state = atoi(cmd + 10);
9509 ret = wpa_driver_set_power_save(priv, state);
9510 if (ret < 0)
9511 wpa_driver_send_hang_msg(drv);
9512 else
9513 drv_errors = 0;
9514 } else if (os_strncasecmp(cmd, "GETPOWER", 8) == 0) {
9515 int state = -1;
9516 ret = wpa_driver_get_power_save(priv, &state);
9517 if (!ret && (state != -1))
9518 ret = os_snprintf(buf, buf_len, "POWERMODE = %d\n",
9519 state);
9520 } else if( os_strncasecmp(cmd, "RXFILTER-ADD ", 13) == 0 ) {
9521 int i = nl80211_parse_wowlan_trigger_nr(cmd + 13);
9522 if(i < 0)
9523 return i;
9524 return nl80211_toggle_wowlan_trigger(bss, i, 1);
9525 } else if( os_strncasecmp(cmd, "RXFILTER-REMOVE ", 16) == 0 ) {
9526 int i = nl80211_parse_wowlan_trigger_nr(cmd + 16);
9527 if(i < 0)
9528 return i;
9529 return nl80211_toggle_wowlan_trigger(bss, i, 0);
9530 } else if (os_strcasecmp(cmd, "RXFILTER-START") == 0) {
9531 return nl80211_set_wowlan_triggers(bss, 1);
9532 } else if (os_strcasecmp(cmd, "RXFILTER-STOP") == 0) {
9533 return nl80211_set_wowlan_triggers(bss, 0);
9534 } else if (os_strncasecmp(cmd, "DROPBCAST", 9) == 0) {
9535 char *value = cmd + 10;
9537 if (!os_strcasecmp(value, "ENABLE") ||
9538 !os_strcasecmp(value, "1")) {
9539 ret = nl80211_toggle_dropbcast(1);
9540 } else if (!os_strcasecmp(value, "DISABLE") ||
9541 !os_strcasecmp(value, "0")) {
9542 ret = nl80211_toggle_dropbcast(0);
9543 } else if (!os_strcasecmp(value, "GET") ||
9544 !os_strlen(value)) {
9545 ret = nl80211_dropbcast_get(buf, buf_len);
9546 } else {
9547 wpa_printf(MSG_ERROR,
9548 "Invalid parameter for DROPBCAST: %s",
9549 value);
9550 ret = -1;
9551 }
9552 } else if( os_strcasecmp(cmd, "LINKSPEED") == 0 ) {
9553 struct wpa_signal_info sig;
9554 int linkspeed;
9556 if (!drv->associated)
9557 return -1;
9559 ret = nl80211_get_link_signal(drv, &sig);
9560 if (ret == 0) {
9561 linkspeed = sig.current_txrate;
9562 ret = os_snprintf(buf, buf_len, "LinkSpeed %d\n",
9563 linkspeed);
9564 }
9565 } else if (os_strcasecmp(cmd, "RSSI") == 0 ||
9566 os_strcasecmp(cmd, "RSSI-APPROX") == 0) {
9567 struct wpa_signal_info sig;
9568 int rssi;
9570 if (!drv->associated)
9571 return -1;
9573 ret = nl80211_get_link_signal(drv, &sig);
9574 if (ret == 0) {
9575 rssi = sig.current_signal;
9576 ret = os_snprintf(buf, buf_len, "%s rssi %d\n",
9577 drv->ssid, rssi);
9578 }
9579 } else {
9580 wpa_printf(MSG_ERROR, "Unsupported command: %s", cmd);
9581 ret = -1;
9582 }
9584 return ret;
9585 }
9587 #ifdef ANDROID_BRCM_P2P_PATCH
9589 static int wpa_driver_set_p2p_noa(void *priv, u8 count, int start,
9590 int duration)
9591 {
9592 char buf[MAX_DRV_CMD_SIZE];
9594 memset(buf, 0, sizeof(buf));
9595 wpa_printf(MSG_DEBUG, "%s: Entry", __func__);
9596 os_snprintf(buf, sizeof(buf), "P2P_SET_NOA %d %d %d", count, start,
9597 duration);
9598 return wpa_driver_nl80211_driver_cmd(priv, buf, buf, strlen(buf) + 1);
9599 }
9602 static int wpa_driver_get_p2p_noa(void *priv, u8 *buf, size_t len)
9603 {
9604 char rbuf[MAX_DRV_CMD_SIZE];
9605 char *cmd = "P2P_GET_NOA";
9606 int ret;
9608 wpa_printf(MSG_DEBUG, "%s: Entry", __func__);
9609 os_memset(buf, 0, len);
9610 ret = wpa_driver_nl80211_driver_cmd(priv, cmd, rbuf, sizeof(rbuf));
9611 if (ret <= 0)
9612 return 0;
9613 ret >>= 1;
9614 if (ret > (int)len)
9615 ret = (int)len;
9616 hexstr2bin(rbuf, buf, ret);
9617 return ret;
9618 }
9621 static int wpa_driver_set_ap_wps_p2p_ie(void *priv,
9622 const struct wpabuf *beacon,
9623 const struct wpabuf *proberesp,
9624 const struct wpabuf *assocresp)
9625 {
9626 char buf[MAX_WPSP2PIE_CMD_SIZE];
9627 struct wpabuf *ap_wps_p2p_ie = NULL;
9628 char *_cmd = "SET_AP_WPS_P2P_IE";
9629 char *pbuf;
9630 int ret = 0;
9631 int i;
9632 struct cmd_desc {
9633 int cmd;
9634 const struct wpabuf *src;
9635 } cmd_arr[] = {
9636 {0x1, beacon},
9637 {0x2, proberesp},
9638 {0x4, assocresp},
9639 {-1, NULL}
9640 };
9642 wpa_printf(MSG_DEBUG, "%s: Entry", __func__);
9643 for (i = 0; cmd_arr[i].cmd != -1; i++) {
9644 os_memset(buf, 0, sizeof(buf));
9645 pbuf = buf;
9646 pbuf += sprintf(pbuf, "%s %d", _cmd, cmd_arr[i].cmd);
9647 *pbuf++ = '\0';
9648 ap_wps_p2p_ie = cmd_arr[i].src ?
9649 wpabuf_dup(cmd_arr[i].src) : NULL;
9650 if (ap_wps_p2p_ie) {
9651 os_memcpy(pbuf, wpabuf_head(ap_wps_p2p_ie),
9652 wpabuf_len(ap_wps_p2p_ie));
9653 ret = wpa_driver_nl80211_driver_cmd(
9654 priv, buf, buf,
9655 os_strlen(_cmd) + 3 +
9656 wpabuf_len(ap_wps_p2p_ie));
9657 wpabuf_free(ap_wps_p2p_ie);
9658 if (ret < 0)
9659 break;
9660 }
9661 }
9663 return ret;
9664 }
9666 #endif /* ANDROID_BRCM_P2P_PATCH */
9668 #endif /* ANDROID */
9671 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
9672 .name = "nl80211",
9673 .desc = "Linux nl80211/cfg80211",
9674 .get_bssid = wpa_driver_nl80211_get_bssid,
9675 .get_ssid = wpa_driver_nl80211_get_ssid,
9676 .set_key = wpa_driver_nl80211_set_key,
9677 .scan2 = wpa_driver_nl80211_scan,
9678 .sched_scan = wpa_driver_nl80211_sched_scan,
9679 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
9680 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
9681 .deauthenticate = wpa_driver_nl80211_deauthenticate,
9682 .disassociate = wpa_driver_nl80211_disassociate,
9683 .authenticate = wpa_driver_nl80211_authenticate,
9684 .associate = wpa_driver_nl80211_associate,
9685 .global_init = nl80211_global_init,
9686 .global_deinit = nl80211_global_deinit,
9687 .init2 = wpa_driver_nl80211_init,
9688 .deinit = wpa_driver_nl80211_deinit,
9689 .get_capa = wpa_driver_nl80211_get_capa,
9690 .set_operstate = wpa_driver_nl80211_set_operstate,
9691 .set_supp_port = wpa_driver_nl80211_set_supp_port,
9692 .set_country = wpa_driver_nl80211_set_country,
9693 .set_ap = wpa_driver_nl80211_set_ap,
9694 .if_add = wpa_driver_nl80211_if_add,
9695 .if_remove = wpa_driver_nl80211_if_remove,
9696 .send_mlme = wpa_driver_nl80211_send_mlme,
9697 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
9698 .sta_add = wpa_driver_nl80211_sta_add,
9699 .sta_remove = wpa_driver_nl80211_sta_remove,
9700 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
9701 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
9702 #ifdef HOSTAPD
9703 .hapd_init = i802_init,
9704 .hapd_deinit = i802_deinit,
9705 .set_wds_sta = i802_set_wds_sta,
9706 #endif /* HOSTAPD */
9707 #if defined(HOSTAPD) || defined(CONFIG_AP)
9708 .get_seqnum = i802_get_seqnum,
9709 .flush = i802_flush,
9710 .read_sta_data = i802_read_sta_data,
9711 .get_inact_sec = i802_get_inact_sec,
9712 .sta_clear_stats = i802_sta_clear_stats,
9713 .set_rts = i802_set_rts,
9714 .set_frag = i802_set_frag,
9715 .set_tx_queue_params = i802_set_tx_queue_params,
9716 .set_sta_vlan = i802_set_sta_vlan,
9717 .sta_deauth = i802_sta_deauth,
9718 .sta_disassoc = i802_sta_disassoc,
9719 #endif /* HOSTAPD || CONFIG_AP */
9720 .set_freq = i802_set_freq,
9721 .send_action = wpa_driver_nl80211_send_action,
9722 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
9723 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
9724 .cancel_remain_on_channel =
9725 wpa_driver_nl80211_cancel_remain_on_channel,
9726 .probe_req_report = wpa_driver_nl80211_probe_req_report,
9727 .deinit_ap = wpa_driver_nl80211_deinit_ap,
9728 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
9729 .resume = wpa_driver_nl80211_resume,
9730 .send_ft_action = nl80211_send_ft_action,
9731 .signal_monitor = nl80211_signal_monitor,
9732 .signal_poll = nl80211_signal_poll,
9733 .send_frame = nl80211_send_frame,
9734 .shared_freq = wpa_driver_nl80211_shared_freq,
9735 .set_param = nl80211_set_param,
9736 .get_radio_name = nl80211_get_radio_name,
9737 .add_pmkid = nl80211_add_pmkid,
9738 .remove_pmkid = nl80211_remove_pmkid,
9739 .flush_pmkid = nl80211_flush_pmkid,
9740 .set_rekey_info = nl80211_set_rekey_info,
9741 .poll_client = nl80211_poll_client,
9742 .set_p2p_powersave = nl80211_set_p2p_powersave,
9743 #ifdef CONFIG_TDLS
9744 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
9745 .tdls_oper = nl80211_tdls_oper,
9746 #endif /* CONFIG_TDLS */
9747 #ifdef ANDROID_BRCM_P2P_PATCH
9748 .get_noa = wpa_driver_get_p2p_noa,
9749 .set_noa = wpa_driver_set_p2p_noa,
9750 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
9751 #endif /* ANDROID_BRCM_P2P_PATCH */
9752 #ifdef ANDROID
9753 .driver_cmd = wpa_driver_nl80211_driver_cmd,
9754 #endif /* ANDROID */
9755 };