]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - build-utilities/hostap.git/blob - src/drivers/driver_nl80211.c
nl80211: Add private function support
[build-utilities/hostap.git] / src / drivers / driver_nl80211.c
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"
63 #endif /* ANDROID */
64 #ifdef CONFIG_LIBNL20
65 /* libnl 2.0 compatibility code */
66 #define nl_handle nl_sock
67 #define nl80211_handle_alloc nl_socket_alloc_cb
68 #define nl80211_handle_destroy nl_socket_free
69 #else
70 /*
71  * libnl 1.1 has a bug, it tries to allocate socket numbers densely
72  * but when you free a socket again it will mess up its bitmap and
73  * and use the wrong number the next time it needs a socket ID.
74  * Therefore, we wrap the handle alloc/destroy and add our own pid
75  * accounting.
76  */
77 static uint32_t port_bitmap[32] = { 0 };
79 static struct nl_handle *nl80211_handle_alloc(void *cb)
80 {
81         struct nl_handle *handle;
82         uint32_t pid = getpid() & 0x3FFFFF;
83         int i;
85         handle = nl_handle_alloc_cb(cb);
87         for (i = 0; i < 1024; i++) {
88                 if (port_bitmap[i / 32] & (1 << (i % 32)))
89                         continue;
90                 port_bitmap[i / 32] |= 1 << (i % 32);
91                 pid += i << 22;
92                 break;
93         }
95         nl_socket_set_local_port(handle, pid);
97         return handle;
98 }
100 static void nl80211_handle_destroy(struct nl_handle *handle)
102         uint32_t port = nl_socket_get_local_port(handle);
104         port >>= 22;
105         port_bitmap[port / 32] &= ~(1 << (port % 32));
107         nl_handle_destroy(handle);
109 #endif /* CONFIG_LIBNL20 */
112 static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
114         struct nl_handle *handle;
116         handle = nl80211_handle_alloc(cb);
117         if (handle == NULL) {
118                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
119                            "callbacks (%s)", dbg);
120                 return NULL;
121         }
123         if (genl_connect(handle)) {
124                 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
125                            "netlink (%s)", dbg);
126                 nl80211_handle_destroy(handle);
127                 return NULL;
128         }
130         return handle;
134 static void nl_destroy_handles(struct nl_handle **handle)
136         if (*handle == NULL)
137                 return;
138         nl80211_handle_destroy(*handle);
139         *handle = NULL;
143 #ifndef IFF_LOWER_UP
144 #define IFF_LOWER_UP   0x10000         /* driver signals L1 up         */
145 #endif
146 #ifndef IFF_DORMANT
147 #define IFF_DORMANT    0x20000         /* driver signals dormant       */
148 #endif
150 #ifndef IF_OPER_DORMANT
151 #define IF_OPER_DORMANT 5
152 #endif
153 #ifndef IF_OPER_UP
154 #define IF_OPER_UP 6
155 #endif
157 struct nl80211_global {
158         struct dl_list interfaces;
159         int if_add_ifindex;
160         struct netlink_data *netlink;
161         struct nl_cb *nl_cb;
162         struct nl_handle *nl;
163         int nl80211_id;
164         int ioctl_sock; /* socket for ioctl() use */
166         struct nl_handle *nl_event;
167 };
169 struct nl80211_wiphy_data {
170         struct dl_list list;
171         struct dl_list bsss;
172         struct dl_list drvs;
174         struct nl_handle *nl_beacons;
175         struct nl_cb *nl_cb;
177         int wiphy_idx;
178 };
180 static void nl80211_global_deinit(void *priv);
181 static void wpa_driver_nl80211_deinit(void *priv);
183 struct i802_bss {
184         struct wpa_driver_nl80211_data *drv;
185         struct i802_bss *next;
186         int ifindex;
187         char ifname[IFNAMSIZ + 1];
188         char brname[IFNAMSIZ];
189         unsigned int beacon_set:1;
190         unsigned int added_if_into_bridge:1;
191         unsigned int added_bridge:1;
193         u8 addr[ETH_ALEN];
195         int freq;
197         struct nl_handle *nl_preq, *nl_mgmt;
198         struct nl_cb *nl_cb;
200         struct nl80211_wiphy_data *wiphy_data;
201         struct dl_list wiphy_list;
202 };
204 struct wpa_driver_nl80211_data {
205         struct nl80211_global *global;
206         struct dl_list list;
207         struct dl_list wiphy_list;
208         char phyname[32];
209         void *ctx;
210         int ifindex;
211         int if_removed;
212         int if_disabled;
213         int ignore_if_down_event;
214         struct rfkill_data *rfkill;
215         struct wpa_driver_capa capa;
216         int has_capability;
218         int operstate;
220         int scan_complete_events;
222         struct nl_cb *nl_cb;
224         u8 auth_bssid[ETH_ALEN];
225         u8 bssid[ETH_ALEN];
226         int associated;
227         u8 ssid[32];
228         size_t ssid_len;
229         enum nl80211_iftype nlmode;
230         enum nl80211_iftype ap_scan_as_station;
231         unsigned int assoc_freq;
233         int monitor_sock;
234         int monitor_ifidx;
235         int monitor_refcount;
237         unsigned int disabled_11b_rates:1;
238         unsigned int pending_remain_on_chan:1;
239         unsigned int in_interface_list:1;
240         unsigned int device_ap_sme:1;
241         unsigned int poll_command_supported:1;
242         unsigned int data_tx_status:1;
243         unsigned int scan_for_auth:1;
244         unsigned int retry_auth:1;
245         unsigned int use_monitor:1;
247         u64 remain_on_chan_cookie;
248         u64 send_action_cookie;
250         unsigned int last_mgmt_freq;
252         struct wpa_driver_scan_filter *filter_ssids;
253         size_t num_filter_ssids;
255         struct i802_bss first_bss;
257         int eapol_tx_sock;
259 #ifdef HOSTAPD
260         int eapol_sock; /* socket for EAPOL frames */
262         int default_if_indices[16];
263         int *if_indices;
264         int num_if_indices;
266         int last_freq;
267         int last_freq_ht;
268 #endif /* HOSTAPD */
270         /* From failed authentication command */
271         int auth_freq;
272         u8 auth_bssid_[ETH_ALEN];
273         u8 auth_ssid[32];
274         size_t auth_ssid_len;
275         int auth_alg;
276         u8 *auth_ie;
277         size_t auth_ie_len;
278         u8 auth_wep_key[4][16];
279         size_t auth_wep_key_len[4];
280         int auth_wep_tx_keyidx;
281         int auth_local_state_change;
282         int auth_p2p;
283 };
286 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
287                                             void *timeout_ctx);
288 static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
289                                        enum nl80211_iftype nlmode);
290 static int
291 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
292 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
293                                    const u8 *addr, int cmd, u16 reason_code,
294                                    int local_state_change);
295 static void nl80211_remove_monitor_interface(
296         struct wpa_driver_nl80211_data *drv);
297 static int nl80211_send_frame_cmd(struct i802_bss *bss,
298                                   unsigned int freq, unsigned int wait,
299                                   const u8 *buf, size_t buf_len, u64 *cookie,
300                                   int no_cck, int no_ack, int offchanok);
301 static int wpa_driver_nl80211_probe_req_report(void *priv, int report);
302 #ifdef ANDROID
303 static int android_pno_start(struct i802_bss *bss,
304                              struct wpa_driver_scan_params *params);
305 static int android_pno_stop(struct i802_bss *bss);
306 #endif /* ANDROID */
308 #ifdef HOSTAPD
309 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
310 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
311 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
312 static int wpa_driver_nl80211_if_remove(void *priv,
313                                         enum wpa_driver_if_type type,
314                                         const char *ifname);
315 #else /* HOSTAPD */
316 static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
320 static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
324 static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
326         return 0;
328 #endif /* HOSTAPD */
330 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
331 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
332                                      int ifindex, int disabled);
334 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
335 static int wpa_driver_nl80211_authenticate_retry(
336         struct wpa_driver_nl80211_data *drv);
339 static int is_ap_interface(enum nl80211_iftype nlmode)
341         return (nlmode == NL80211_IFTYPE_AP ||
342                 nlmode == NL80211_IFTYPE_P2P_GO);
346 static int is_sta_interface(enum nl80211_iftype nlmode)
348         return (nlmode == NL80211_IFTYPE_STATION ||
349                 nlmode == NL80211_IFTYPE_P2P_CLIENT);
353 static int is_p2p_interface(enum nl80211_iftype nlmode)
355         return (nlmode == NL80211_IFTYPE_P2P_CLIENT ||
356                 nlmode == NL80211_IFTYPE_P2P_GO);
360 struct nl80211_bss_info_arg {
361         struct wpa_driver_nl80211_data *drv;
362         struct wpa_scan_results *res;
363         unsigned int assoc_freq;
364         u8 assoc_bssid[ETH_ALEN];
365 };
367 static int bss_info_handler(struct nl_msg *msg, void *arg);
370 /* nl80211 code */
371 static int ack_handler(struct nl_msg *msg, void *arg)
373         int *err = arg;
374         *err = 0;
375         return NL_STOP;
378 static int finish_handler(struct nl_msg *msg, void *arg)
380         int *ret = arg;
381         *ret = 0;
382         return NL_SKIP;
385 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
386                          void *arg)
388         int *ret = arg;
389         *ret = err->error;
390         return NL_SKIP;
394 static int no_seq_check(struct nl_msg *msg, void *arg)
396         return NL_OK;
400 static int send_and_recv(struct nl80211_global *global,
401                          struct nl_handle *nl_handle, struct nl_msg *msg,
402                          int (*valid_handler)(struct nl_msg *, void *),
403                          void *valid_data)
405         struct nl_cb *cb;
406         int err = -ENOMEM;
408         cb = nl_cb_clone(global->nl_cb);
409         if (!cb)
410                 goto out;
412         err = nl_send_auto_complete(nl_handle, msg);
413         if (err < 0)
414                 goto out;
416         err = 1;
418         nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
419         nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
420         nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
422         if (valid_handler)
423                 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
424                           valid_handler, valid_data);
426         while (err > 0)
427                 nl_recvmsgs(nl_handle, cb);
428  out:
429         nl_cb_put(cb);
430         nlmsg_free(msg);
431         return err;
435 static int send_and_recv_msgs_global(struct nl80211_global *global,
436                                      struct nl_msg *msg,
437                                      int (*valid_handler)(struct nl_msg *, void *),
438                                      void *valid_data)
440         return send_and_recv(global, global->nl, msg, valid_handler,
441                              valid_data);
445 static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
446                               struct nl_msg *msg,
447                               int (*valid_handler)(struct nl_msg *, void *),
448                               void *valid_data)
450         return send_and_recv(drv->global, drv->global->nl, msg,
451                              valid_handler, valid_data);
455 struct family_data {
456         const char *group;
457         int id;
458 };
461 static int family_handler(struct nl_msg *msg, void *arg)
463         struct family_data *res = arg;
464         struct nlattr *tb[CTRL_ATTR_MAX + 1];
465         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
466         struct nlattr *mcgrp;
467         int i;
469         nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
470                   genlmsg_attrlen(gnlh, 0), NULL);
471         if (!tb[CTRL_ATTR_MCAST_GROUPS])
472                 return NL_SKIP;
474         nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
475                 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
476                 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
477                           nla_len(mcgrp), NULL);
478                 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
479                     !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
480                     os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
481                                res->group,
482                                nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
483                         continue;
484                 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
485                 break;
486         };
488         return NL_SKIP;
492 static int nl_get_multicast_id(struct nl80211_global *global,
493                                const char *family, const char *group)
495         struct nl_msg *msg;
496         int ret = -1;
497         struct family_data res = { group, -ENOENT };
499         msg = nlmsg_alloc();
500         if (!msg)
501                 return -ENOMEM;
502         genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
503                     0, 0, CTRL_CMD_GETFAMILY, 0);
504         NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
506         ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
507         msg = NULL;
508         if (ret == 0)
509                 ret = res.id;
511 nla_put_failure:
512         nlmsg_free(msg);
513         return ret;
517 static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
518                           struct nl_msg *msg, int flags, uint8_t cmd)
520         return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
521                            0, flags, cmd, 0);
525 struct wiphy_idx_data {
526         int wiphy_idx;
527 };
530 static int netdev_info_handler(struct nl_msg *msg, void *arg)
532         struct nlattr *tb[NL80211_ATTR_MAX + 1];
533         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
534         struct wiphy_idx_data *info = arg;
536         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
537                   genlmsg_attrlen(gnlh, 0), NULL);
539         if (tb[NL80211_ATTR_WIPHY])
540                 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
542         return NL_SKIP;
546 static int nl80211_get_wiphy_index(struct i802_bss *bss)
548         struct nl_msg *msg;
549         struct wiphy_idx_data data = {
550                 .wiphy_idx = -1,
551         };
553         msg = nlmsg_alloc();
554         if (!msg)
555                 return -1;
557         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_GET_INTERFACE);
559         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
561         if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
562                 return data.wiphy_idx;
563         msg = NULL;
564 nla_put_failure:
565         nlmsg_free(msg);
566         return -1;
570 static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
571                                     struct nl80211_wiphy_data *w)
573         struct nl_msg *msg;
574         int ret = -1;
576         msg = nlmsg_alloc();
577         if (!msg)
578                 return -1;
580         nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS);
582         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx);
584         ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
585         msg = NULL;
586         if (ret) {
587                 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
588                            "failed: ret=%d (%s)",
589                            ret, strerror(-ret));
590                 goto nla_put_failure;
591         }
592         ret = 0;
593 nla_put_failure:
594         nlmsg_free(msg);
595         return ret;
599 static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
601         struct nl80211_wiphy_data *w = eloop_ctx;
603         wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
605         nl_recvmsgs(handle, w->nl_cb);
609 static int process_beacon_event(struct nl_msg *msg, void *arg)
611         struct nl80211_wiphy_data *w = arg;
612         struct wpa_driver_nl80211_data *drv;
613         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
614         struct nlattr *tb[NL80211_ATTR_MAX + 1];
615         union wpa_event_data event;
617         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
618                   genlmsg_attrlen(gnlh, 0), NULL);
620         if (gnlh->cmd != NL80211_CMD_FRAME) {
621                 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
622                            gnlh->cmd);
623                 return NL_SKIP;
624         }
626         if (!tb[NL80211_ATTR_FRAME])
627                 return NL_SKIP;
629         dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
630                          wiphy_list) {
631                 os_memset(&event, 0, sizeof(event));
632                 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
633                 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
634                 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
635         }
637         return NL_SKIP;
641 static struct nl80211_wiphy_data *
642 nl80211_get_wiphy_data_ap(struct i802_bss *bss)
644         static DEFINE_DL_LIST(nl80211_wiphys);
645         struct nl80211_wiphy_data *w;
646         int wiphy_idx, found = 0;
647         struct i802_bss *tmp_bss;
649         if (bss->wiphy_data != NULL)
650                 return bss->wiphy_data;
652         wiphy_idx = nl80211_get_wiphy_index(bss);
654         dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
655                 if (w->wiphy_idx == wiphy_idx)
656                         goto add;
657         }
659         /* alloc new one */
660         w = os_zalloc(sizeof(*w));
661         if (w == NULL)
662                 return NULL;
663         w->wiphy_idx = wiphy_idx;
664         dl_list_init(&w->bsss);
665         dl_list_init(&w->drvs);
667         w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
668         if (!w->nl_cb) {
669                 os_free(w);
670                 return NULL;
671         }
672         nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
673         nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
674                   w);
676         w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
677                                          "wiphy beacons");
678         if (w->nl_beacons == NULL) {
679                 os_free(w);
680                 return NULL;
681         }
683         if (nl80211_register_beacons(bss->drv, w)) {
684                 nl_destroy_handles(&w->nl_beacons);
685                 os_free(w);
686                 return NULL;
687         }
689         eloop_register_read_sock(nl_socket_get_fd(w->nl_beacons),
690                                  nl80211_recv_beacons, w, w->nl_beacons);
692         dl_list_add(&nl80211_wiphys, &w->list);
694 add:
695         /* drv entry for this bss already there? */
696         dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
697                 if (tmp_bss->drv == bss->drv) {
698                         found = 1;
699                         break;
700                 }
701         }
702         /* if not add it */
703         if (!found)
704                 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
706         dl_list_add(&w->bsss, &bss->wiphy_list);
707         bss->wiphy_data = w;
708         return w;
712 static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
714         struct nl80211_wiphy_data *w = bss->wiphy_data;
715         struct i802_bss *tmp_bss;
716         int found = 0;
718         if (w == NULL)
719                 return;
720         bss->wiphy_data = NULL;
721         dl_list_del(&bss->wiphy_list);
723         /* still any for this drv present? */
724         dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
725                 if (tmp_bss->drv == bss->drv) {
726                         found = 1;
727                         break;
728                 }
729         }
730         /* if not remove it */
731         if (!found)
732                 dl_list_del(&bss->drv->wiphy_list);
734         if (!dl_list_empty(&w->bsss))
735                 return;
737         eloop_unregister_read_sock(nl_socket_get_fd(w->nl_beacons));
739         nl_cb_put(w->nl_cb);
740         nl_destroy_handles(&w->nl_beacons);
741         dl_list_del(&w->list);
742         os_free(w);
746 static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
748         struct i802_bss *bss = priv;
749         struct wpa_driver_nl80211_data *drv = bss->drv;
750         if (!drv->associated)
751                 return -1;
752         os_memcpy(bssid, drv->bssid, ETH_ALEN);
753         return 0;
757 static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
759         struct i802_bss *bss = priv;
760         struct wpa_driver_nl80211_data *drv = bss->drv;
761         if (!drv->associated)
762                 return -1;
763         os_memcpy(ssid, drv->ssid, drv->ssid_len);
764         return drv->ssid_len;
768 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
769                                           char *buf, size_t len, int del)
771         union wpa_event_data event;
773         os_memset(&event, 0, sizeof(event));
774         if (len > sizeof(event.interface_status.ifname))
775                 len = sizeof(event.interface_status.ifname) - 1;
776         os_memcpy(event.interface_status.ifname, buf, len);
777         event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
778                 EVENT_INTERFACE_ADDED;
780         wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
781                    del ? "DEL" : "NEW",
782                    event.interface_status.ifname,
783                    del ? "removed" : "added");
785         if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
786                 if (del) {
787                         if (drv->if_removed) {
788                                 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
789                                            "already set - ignore event");
790                                 return;
791                         }
792                         drv->if_removed = 1;
793                 } else {
794                         if (if_nametoindex(drv->first_bss.ifname) == 0) {
795                                 wpa_printf(MSG_DEBUG, "nl80211: Interface %s "
796                                            "does not exist - ignore "
797                                            "RTM_NEWLINK",
798                                            drv->first_bss.ifname);
799                                 return;
800                         }
801                         if (!drv->if_removed) {
802                                 wpa_printf(MSG_DEBUG, "nl80211: if_removed "
803                                            "already cleared - ignore event");
804                                 return;
805                         }
806                         drv->if_removed = 0;
807                 }
808         }
810         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
814 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
815                                          u8 *buf, size_t len)
817         int attrlen, rta_len;
818         struct rtattr *attr;
820         attrlen = len;
821         attr = (struct rtattr *) buf;
823         rta_len = RTA_ALIGN(sizeof(struct rtattr));
824         while (RTA_OK(attr, attrlen)) {
825                 if (attr->rta_type == IFLA_IFNAME) {
826                         if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
827                             == 0)
828                                 return 1;
829                         else
830                                 break;
831                 }
832                 attr = RTA_NEXT(attr, attrlen);
833         }
835         return 0;
839 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
840                                           int ifindex, u8 *buf, size_t len)
842         if (drv->ifindex == ifindex)
843                 return 1;
845         if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
846                 drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
847                 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
848                            "interface");
849                 wpa_driver_nl80211_finish_drv_init(drv);
850                 return 1;
851         }
853         return 0;
857 static struct wpa_driver_nl80211_data *
858 nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
860         struct wpa_driver_nl80211_data *drv;
861         dl_list_for_each(drv, &global->interfaces,
862                          struct wpa_driver_nl80211_data, list) {
863                 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
864                     have_ifidx(drv, idx))
865                         return drv;
866         }
867         return NULL;
871 static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
872                                                  struct ifinfomsg *ifi,
873                                                  u8 *buf, size_t len)
875         struct nl80211_global *global = ctx;
876         struct wpa_driver_nl80211_data *drv;
877         int attrlen, rta_len;
878         struct rtattr *attr;
879         u32 brid = 0;
880         char namebuf[IFNAMSIZ];
882         drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
883         if (!drv) {
884                 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
885                            "ifindex %d", ifi->ifi_index);
886                 return;
887         }
889         wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
890                    "(%s%s%s%s)",
891                    drv->operstate, ifi->ifi_flags,
892                    (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
893                    (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
894                    (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
895                    (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
897         if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
898                 if (if_indextoname(ifi->ifi_index, namebuf) &&
899                     linux_iface_up(drv->global->ioctl_sock,
900                                    drv->first_bss.ifname) > 0) {
901                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
902                                    "event since interface %s is up", namebuf);
903                         return;
904                 }
905                 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
906                 if (drv->ignore_if_down_event) {
907                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
908                                    "event generated by mode change");
909                         drv->ignore_if_down_event = 0;
910                 } else {
911                         drv->if_disabled = 1;
912                         wpa_supplicant_event(drv->ctx,
913                                              EVENT_INTERFACE_DISABLED, NULL);
914                 }
915         }
917         if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
918                 if (if_indextoname(ifi->ifi_index, namebuf) &&
919                     linux_iface_up(drv->global->ioctl_sock,
920                                    drv->first_bss.ifname) == 0) {
921                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
922                                    "event since interface %s is down",
923                                    namebuf);
924                 } else if (if_nametoindex(drv->first_bss.ifname) == 0) {
925                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
926                                    "event since interface %s does not exist",
927                                    drv->first_bss.ifname);
928                 } else if (drv->if_removed) {
929                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
930                                    "event since interface %s is marked "
931                                    "removed", drv->first_bss.ifname);
932                 } else {
933                         wpa_printf(MSG_DEBUG, "nl80211: Interface up");
934                         drv->if_disabled = 0;
935                         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
936                                              NULL);
937                 }
938         }
940         /*
941          * Some drivers send the association event before the operup event--in
942          * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
943          * fails. This will hit us when wpa_supplicant does not need to do
944          * IEEE 802.1X authentication
945          */
946         if (drv->operstate == 1 &&
947             (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
948             !(ifi->ifi_flags & IFF_RUNNING))
949                 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
950                                        -1, IF_OPER_UP);
952         attrlen = len;
953         attr = (struct rtattr *) buf;
954         rta_len = RTA_ALIGN(sizeof(struct rtattr));
955         while (RTA_OK(attr, attrlen)) {
956                 if (attr->rta_type == IFLA_IFNAME) {
957                         wpa_driver_nl80211_event_link(
958                                 drv,
959                                 ((char *) attr) + rta_len,
960                                 attr->rta_len - rta_len, 0);
961                 } else if (attr->rta_type == IFLA_MASTER)
962                         brid = nla_get_u32((struct nlattr *) attr);
963                 attr = RTA_NEXT(attr, attrlen);
964         }
966         if (ifi->ifi_family == AF_BRIDGE && brid) {
967                 /* device has been added to bridge */
968                 if_indextoname(brid, namebuf);
969                 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
970                            brid, namebuf);
971                 add_ifidx(drv, brid);
972         }
976 static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
977                                                  struct ifinfomsg *ifi,
978                                                  u8 *buf, size_t len)
980         struct nl80211_global *global = ctx;
981         struct wpa_driver_nl80211_data *drv;
982         int attrlen, rta_len;
983         struct rtattr *attr;
984         u32 brid = 0;
986         drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
987         if (!drv) {
988                 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
989                            "foreign ifindex %d", ifi->ifi_index);
990                 return;
991         }
993         attrlen = len;
994         attr = (struct rtattr *) buf;
996         rta_len = RTA_ALIGN(sizeof(struct rtattr));
997         while (RTA_OK(attr, attrlen)) {
998                 if (attr->rta_type == IFLA_IFNAME) {
999                         wpa_driver_nl80211_event_link(
1000                                 drv,
1001                                 ((char *) attr) + rta_len,
1002                                 attr->rta_len - rta_len, 1);
1003                 } else if (attr->rta_type == IFLA_MASTER)
1004                         brid = nla_get_u32((struct nlattr *) attr);
1005                 attr = RTA_NEXT(attr, attrlen);
1006         }
1008         if (ifi->ifi_family == AF_BRIDGE && brid) {
1009                 /* device has been removed from bridge */
1010                 char namebuf[IFNAMSIZ];
1011                 if_indextoname(brid, namebuf);
1012                 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1013                            "%s", brid, namebuf);
1014                 del_ifidx(drv, brid);
1015         }
1019 static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
1020                             const u8 *frame, size_t len)
1022         const struct ieee80211_mgmt *mgmt;
1023         union wpa_event_data event;
1025         mgmt = (const struct ieee80211_mgmt *) frame;
1026         if (len < 24 + sizeof(mgmt->u.auth)) {
1027                 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1028                            "frame");
1029                 return;
1030         }
1032         os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
1033         os_memset(&event, 0, sizeof(event));
1034         os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
1035         event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
1036         event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
1037         if (len > 24 + sizeof(mgmt->u.auth)) {
1038                 event.auth.ies = mgmt->u.auth.variable;
1039                 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
1040         }
1042         wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
1046 static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1048         struct nl_msg *msg;
1049         int ret;
1050         struct nl80211_bss_info_arg arg;
1052         os_memset(&arg, 0, sizeof(arg));
1053         msg = nlmsg_alloc();
1054         if (!msg)
1055                 goto nla_put_failure;
1057         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1058         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1060         arg.drv = drv;
1061         ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1062         msg = NULL;
1063         if (ret == 0) {
1064                 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1065                            "associated BSS from scan results: %u MHz",
1066                            arg.assoc_freq);
1067                 return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq;
1068         }
1069         wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1070                    "(%s)", ret, strerror(-ret));
1071 nla_put_failure:
1072         nlmsg_free(msg);
1073         return drv->assoc_freq;
1077 static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
1078                             const u8 *frame, size_t len)
1080         const struct ieee80211_mgmt *mgmt;
1081         union wpa_event_data event;
1082         u16 status;
1084         mgmt = (const struct ieee80211_mgmt *) frame;
1085         if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
1086                 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
1087                            "frame");
1088                 return;
1089         }
1091         status = le_to_host16(mgmt->u.assoc_resp.status_code);
1092         if (status != WLAN_STATUS_SUCCESS) {
1093                 os_memset(&event, 0, sizeof(event));
1094                 event.assoc_reject.bssid = mgmt->bssid;
1095                 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1096                         event.assoc_reject.resp_ies =
1097                                 (u8 *) mgmt->u.assoc_resp.variable;
1098                         event.assoc_reject.resp_ies_len =
1099                                 len - 24 - sizeof(mgmt->u.assoc_resp);
1100                 }
1101                 event.assoc_reject.status_code = status;
1103                 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1104                 return;
1105         }
1107         drv->associated = 1;
1108         os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
1110         os_memset(&event, 0, sizeof(event));
1111         if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
1112                 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
1113                 event.assoc_info.resp_ies_len =
1114                         len - 24 - sizeof(mgmt->u.assoc_resp);
1115         }
1117         event.assoc_info.freq = drv->assoc_freq;
1119         wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1123 static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
1124                                enum nl80211_commands cmd, struct nlattr *status,
1125                                struct nlattr *addr, struct nlattr *req_ie,
1126                                struct nlattr *resp_ie)
1128         union wpa_event_data event;
1130         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1131                 /*
1132                  * Avoid reporting two association events that would confuse
1133                  * the core code.
1134                  */
1135                 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
1136                            "when using userspace SME", cmd);
1137                 return;
1138         }
1140         os_memset(&event, 0, sizeof(event));
1141         if (cmd == NL80211_CMD_CONNECT &&
1142             nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
1143                 if (addr)
1144                         event.assoc_reject.bssid = nla_data(addr);
1145                 if (resp_ie) {
1146                         event.assoc_reject.resp_ies = nla_data(resp_ie);
1147                         event.assoc_reject.resp_ies_len = nla_len(resp_ie);
1148                 }
1149                 event.assoc_reject.status_code = nla_get_u16(status);
1150                 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
1151                 return;
1152         }
1154         drv->associated = 1;
1155         if (addr)
1156                 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
1158         if (req_ie) {
1159                 event.assoc_info.req_ies = nla_data(req_ie);
1160                 event.assoc_info.req_ies_len = nla_len(req_ie);
1161         }
1162         if (resp_ie) {
1163                 event.assoc_info.resp_ies = nla_data(resp_ie);
1164                 event.assoc_info.resp_ies_len = nla_len(resp_ie);
1165         }
1167         event.assoc_info.freq = nl80211_get_assoc_freq(drv);
1169         wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
1173 static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
1174                                   struct nlattr *reason, struct nlattr *addr,
1175                                   struct nlattr *by_ap)
1177         union wpa_event_data data;
1179         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1180                 /*
1181                  * Avoid reporting two disassociation events that could
1182                  * confuse the core code.
1183                  */
1184                 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
1185                            "event when using userspace SME");
1186                 return;
1187         }
1189         drv->associated = 0;
1190         os_memset(&data, 0, sizeof(data));
1191         if (reason)
1192                 data.disassoc_info.reason_code = nla_get_u16(reason);
1193         data.disassoc_info.locally_generated = by_ap == NULL;
1194         wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, &data);
1198 static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
1199                                enum nl80211_commands cmd, struct nlattr *addr)
1201         union wpa_event_data event;
1202         enum wpa_event_type ev;
1204         if (nla_len(addr) != ETH_ALEN)
1205                 return;
1207         wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
1208                    cmd, MAC2STR((u8 *) nla_data(addr)));
1210         if (cmd == NL80211_CMD_AUTHENTICATE)
1211                 ev = EVENT_AUTH_TIMED_OUT;
1212         else if (cmd == NL80211_CMD_ASSOCIATE)
1213                 ev = EVENT_ASSOC_TIMED_OUT;
1214         else
1215                 return;
1217         os_memset(&event, 0, sizeof(event));
1218         os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
1219         wpa_supplicant_event(drv->ctx, ev, &event);
1223 static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
1224                             struct nlattr *freq, const u8 *frame, size_t len)
1226         const struct ieee80211_mgmt *mgmt;
1227         union wpa_event_data event;
1228         u16 fc, stype;
1230         mgmt = (const struct ieee80211_mgmt *) frame;
1231         if (len < 24) {
1232                 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
1233                 return;
1234         }
1236         fc = le_to_host16(mgmt->frame_control);
1237         stype = WLAN_FC_GET_STYPE(fc);
1239         os_memset(&event, 0, sizeof(event));
1240         if (freq) {
1241                 event.rx_action.freq = nla_get_u32(freq);
1242                 drv->last_mgmt_freq = event.rx_action.freq;
1243         }
1244         if (stype == WLAN_FC_STYPE_ACTION) {
1245                 event.rx_action.da = mgmt->da;
1246                 event.rx_action.sa = mgmt->sa;
1247                 event.rx_action.bssid = mgmt->bssid;
1248                 event.rx_action.category = mgmt->u.action.category;
1249                 event.rx_action.data = &mgmt->u.action.category + 1;
1250                 event.rx_action.len = frame + len - event.rx_action.data;
1251                 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
1252         } else {
1253                 event.rx_mgmt.frame = frame;
1254                 event.rx_mgmt.frame_len = len;
1255                 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
1256         }
1260 static void mlme_event_mgmt_tx_status(struct wpa_driver_nl80211_data *drv,
1261                                       struct nlattr *cookie, const u8 *frame,
1262                                       size_t len, struct nlattr *ack)
1264         union wpa_event_data event;
1265         const struct ieee80211_hdr *hdr;
1266         u16 fc;
1268         if (!is_ap_interface(drv->nlmode)) {
1269                 u64 cookie_val;
1271                 if (!cookie)
1272                         return;
1274                 cookie_val = nla_get_u64(cookie);
1275                 wpa_printf(MSG_DEBUG, "nl80211: Action TX status:"
1276                            " cookie=0%llx%s (ack=%d)",
1277                            (long long unsigned int) cookie_val,
1278                            cookie_val == drv->send_action_cookie ?
1279                            " (match)" : " (unknown)", ack != NULL);
1280                 if (cookie_val != drv->send_action_cookie)
1281                         return;
1282         }
1284         hdr = (const struct ieee80211_hdr *) frame;
1285         fc = le_to_host16(hdr->frame_control);
1287         os_memset(&event, 0, sizeof(event));
1288         event.tx_status.type = WLAN_FC_GET_TYPE(fc);
1289         event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
1290         event.tx_status.dst = hdr->addr1;
1291         event.tx_status.data = frame;
1292         event.tx_status.data_len = len;
1293         event.tx_status.ack = ack != NULL;
1294         wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
1298 static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
1299                                        enum wpa_event_type type,
1300                                        const u8 *frame, size_t len)
1302         const struct ieee80211_mgmt *mgmt;
1303         union wpa_event_data event;
1304         const u8 *bssid = NULL;
1305         u16 reason_code = 0;
1307         mgmt = (const struct ieee80211_mgmt *) frame;
1308         if (len >= 24) {
1309                 bssid = mgmt->bssid;
1311                 if (drv->associated != 0 &&
1312                     os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
1313                     os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
1314                         /*
1315                          * We have presumably received this deauth as a
1316                          * response to a clear_state_mismatch() outgoing
1317                          * deauth.  Don't let it take us offline!
1318                          */
1319                         wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
1320                                    "from Unknown BSSID " MACSTR " -- ignoring",
1321                                    MAC2STR(bssid));
1322                         return;
1323                 }
1324         }
1326         drv->associated = 0;
1327         os_memset(&event, 0, sizeof(event));
1329         /* Note: Same offset for Reason Code in both frame subtypes */
1330         if (len >= 24 + sizeof(mgmt->u.deauth))
1331                 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1333         if (type == EVENT_DISASSOC) {
1334                 event.disassoc_info.locally_generated =
1335                         !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
1336                 event.disassoc_info.addr = bssid;
1337                 event.disassoc_info.reason_code = reason_code;
1338                 if (frame + len > mgmt->u.disassoc.variable) {
1339                         event.disassoc_info.ie = mgmt->u.disassoc.variable;
1340                         event.disassoc_info.ie_len = frame + len -
1341                                 mgmt->u.disassoc.variable;
1342                 }
1343         } else {
1344                 event.deauth_info.locally_generated =
1345                         !os_memcmp(mgmt->sa, drv->first_bss.addr, ETH_ALEN);
1346                 event.deauth_info.addr = bssid;
1347                 event.deauth_info.reason_code = reason_code;
1348                 if (frame + len > mgmt->u.deauth.variable) {
1349                         event.deauth_info.ie = mgmt->u.deauth.variable;
1350                         event.deauth_info.ie_len = frame + len -
1351                                 mgmt->u.deauth.variable;
1352                 }
1353         }
1355         wpa_supplicant_event(drv->ctx, type, &event);
1359 static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1360                                          enum wpa_event_type type,
1361                                          const u8 *frame, size_t len)
1363         const struct ieee80211_mgmt *mgmt;
1364         union wpa_event_data event;
1365         u16 reason_code = 0;
1367         if (len < 24)
1368                 return;
1370         mgmt = (const struct ieee80211_mgmt *) frame;
1372         os_memset(&event, 0, sizeof(event));
1373         /* Note: Same offset for Reason Code in both frame subtypes */
1374         if (len >= 24 + sizeof(mgmt->u.deauth))
1375                 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1377         if (type == EVENT_UNPROT_DISASSOC) {
1378                 event.unprot_disassoc.sa = mgmt->sa;
1379                 event.unprot_disassoc.da = mgmt->da;
1380                 event.unprot_disassoc.reason_code = reason_code;
1381         } else {
1382                 event.unprot_deauth.sa = mgmt->sa;
1383                 event.unprot_deauth.da = mgmt->da;
1384                 event.unprot_deauth.reason_code = reason_code;
1385         }
1387         wpa_supplicant_event(drv->ctx, type, &event);
1391 static void mlme_event(struct wpa_driver_nl80211_data *drv,
1392                        enum nl80211_commands cmd, struct nlattr *frame,
1393                        struct nlattr *addr, struct nlattr *timed_out,
1394                        struct nlattr *freq, struct nlattr *ack,
1395                        struct nlattr *cookie)
1397         if (timed_out && addr) {
1398                 mlme_timeout_event(drv, cmd, addr);
1399                 return;
1400         }
1402         if (frame == NULL) {
1403                 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
1404                            "data", cmd);
1405                 return;
1406         }
1408         wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
1409         wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1410                     nla_data(frame), nla_len(frame));
1412         switch (cmd) {
1413         case NL80211_CMD_AUTHENTICATE:
1414                 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1415                 break;
1416         case NL80211_CMD_ASSOCIATE:
1417                 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1418                 break;
1419         case NL80211_CMD_DEAUTHENTICATE:
1420                 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1421                                            nla_data(frame), nla_len(frame));
1422                 break;
1423         case NL80211_CMD_DISASSOCIATE:
1424                 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1425                                            nla_data(frame), nla_len(frame));
1426                 break;
1427         case NL80211_CMD_FRAME:
1428                 mlme_event_mgmt(drv, freq, nla_data(frame), nla_len(frame));
1429                 break;
1430         case NL80211_CMD_FRAME_TX_STATUS:
1431                 mlme_event_mgmt_tx_status(drv, cookie, nla_data(frame),
1432                                           nla_len(frame), ack);
1433                 break;
1434         case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1435                 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1436                                              nla_data(frame), nla_len(frame));
1437                 break;
1438         case NL80211_CMD_UNPROT_DISASSOCIATE:
1439                 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1440                                              nla_data(frame), nla_len(frame));
1441                 break;
1442         default:
1443                 break;
1444         }
1448 static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
1449                                            struct nlattr *tb[])
1451         union wpa_event_data data;
1453         wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1454         os_memset(&data, 0, sizeof(data));
1455         if (tb[NL80211_ATTR_MAC]) {
1456                 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1457                             nla_data(tb[NL80211_ATTR_MAC]),
1458                             nla_len(tb[NL80211_ATTR_MAC]));
1459                 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1460         }
1461         if (tb[NL80211_ATTR_KEY_SEQ]) {
1462                 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1463                             nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1464                             nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1465         }
1466         if (tb[NL80211_ATTR_KEY_TYPE]) {
1467                 enum nl80211_key_type key_type =
1468                         nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1469                 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1470                 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1471                         data.michael_mic_failure.unicast = 1;
1472         } else
1473                 data.michael_mic_failure.unicast = 1;
1475         if (tb[NL80211_ATTR_KEY_IDX]) {
1476                 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1477                 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1478         }
1480         wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
1484 static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1485                                  struct nlattr *tb[])
1487         if (tb[NL80211_ATTR_MAC] == NULL) {
1488                 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1489                            "event");
1490                 return;
1491         }
1492         os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1493         drv->associated = 1;
1494         wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1495                    MAC2STR(drv->bssid));
1497         wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1501 static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1502                                          int cancel_event, struct nlattr *tb[])
1504         unsigned int freq, chan_type, duration;
1505         union wpa_event_data data;
1506         u64 cookie;
1508         if (tb[NL80211_ATTR_WIPHY_FREQ])
1509                 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1510         else
1511                 freq = 0;
1513         if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1514                 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1515         else
1516                 chan_type = 0;
1518         if (tb[NL80211_ATTR_DURATION])
1519                 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1520         else
1521                 duration = 0;
1523         if (tb[NL80211_ATTR_COOKIE])
1524                 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1525         else
1526                 cookie = 0;
1528         wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1529                    "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1530                    cancel_event, freq, chan_type, duration,
1531                    (long long unsigned int) cookie,
1532                    cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
1534         if (cookie != drv->remain_on_chan_cookie)
1535                 return; /* not for us */
1537         if (cancel_event)
1538                 drv->pending_remain_on_chan = 0;
1540         os_memset(&data, 0, sizeof(data));
1541         data.remain_on_channel.freq = freq;
1542         data.remain_on_channel.duration = duration;
1543         wpa_supplicant_event(drv->ctx, cancel_event ?
1544                              EVENT_CANCEL_REMAIN_ON_CHANNEL :
1545                              EVENT_REMAIN_ON_CHANNEL, &data);
1549 static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
1550                             struct nlattr *tb[])
1552         union wpa_event_data event;
1553         struct nlattr *nl;
1554         int rem;
1555         struct scan_info *info;
1556 #define MAX_REPORT_FREQS 50
1557         int freqs[MAX_REPORT_FREQS];
1558         int num_freqs = 0;
1560         if (drv->scan_for_auth) {
1561                 drv->scan_for_auth = 0;
1562                 wpa_printf(MSG_DEBUG, "nl80211: Scan results for missing "
1563                            "cfg80211 BSS entry");
1564                 wpa_driver_nl80211_authenticate_retry(drv);
1565                 return;
1566         }
1568         os_memset(&event, 0, sizeof(event));
1569         info = &event.scan_info;
1570         info->aborted = aborted;
1572         if (tb[NL80211_ATTR_SCAN_SSIDS]) {
1573                 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
1574                         struct wpa_driver_scan_ssid *s =
1575                                 &info->ssids[info->num_ssids];
1576                         s->ssid = nla_data(nl);
1577                         s->ssid_len = nla_len(nl);
1578                         info->num_ssids++;
1579                         if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
1580                                 break;
1581                 }
1582         }
1583         if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
1584                 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
1585                 {
1586                         freqs[num_freqs] = nla_get_u32(nl);
1587                         num_freqs++;
1588                         if (num_freqs == MAX_REPORT_FREQS - 1)
1589                                 break;
1590                 }
1591                 info->freqs = freqs;
1592                 info->num_freqs = num_freqs;
1593         }
1594         wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
1598 static int get_link_signal(struct nl_msg *msg, void *arg)
1600         struct nlattr *tb[NL80211_ATTR_MAX + 1];
1601         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1602         struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1603         static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1604                 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1605         };
1606         struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1607         static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1608                 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1609                 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1610                 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1611                 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1612         };
1613         struct wpa_signal_info *sig_change = arg;
1615         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1616                   genlmsg_attrlen(gnlh, 0), NULL);
1617         if (!tb[NL80211_ATTR_STA_INFO] ||
1618             nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1619                              tb[NL80211_ATTR_STA_INFO], policy))
1620                 return NL_SKIP;
1621         if (!sinfo[NL80211_STA_INFO_SIGNAL])
1622                 return NL_SKIP;
1624         sig_change->current_signal =
1625                 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1627         if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1628                 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1629                                      sinfo[NL80211_STA_INFO_TX_BITRATE],
1630                                      rate_policy)) {
1631                         sig_change->current_txrate = 0;
1632                 } else {
1633                         if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1634                                 sig_change->current_txrate =
1635                                         nla_get_u16(rinfo[
1636                                              NL80211_RATE_INFO_BITRATE]) * 100;
1637                         }
1638                 }
1639         }
1641         return NL_SKIP;
1645 static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1646                                    struct wpa_signal_info *sig)
1648         struct nl_msg *msg;
1650         sig->current_signal = -9999;
1651         sig->current_txrate = 0;
1653         msg = nlmsg_alloc();
1654         if (!msg)
1655                 return -ENOMEM;
1657         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
1659         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1660         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
1662         return send_and_recv_msgs(drv, msg, get_link_signal, sig);
1663  nla_put_failure:
1664         nlmsg_free(msg);
1665         return -ENOBUFS;
1669 static int get_link_noise(struct nl_msg *msg, void *arg)
1671         struct nlattr *tb[NL80211_ATTR_MAX + 1];
1672         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1673         struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1674         static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1675                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1676                 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1677         };
1678         struct wpa_signal_info *sig_change = arg;
1680         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1681                   genlmsg_attrlen(gnlh, 0), NULL);
1683         if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1684                 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1685                 return NL_SKIP;
1686         }
1688         if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1689                              tb[NL80211_ATTR_SURVEY_INFO],
1690                              survey_policy)) {
1691                 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1692                            "attributes!");
1693                 return NL_SKIP;
1694         }
1696         if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1697                 return NL_SKIP;
1699         if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1700             sig_change->frequency)
1701                 return NL_SKIP;
1703         if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1704                 return NL_SKIP;
1706         sig_change->current_noise =
1707                 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1709         return NL_SKIP;
1713 static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1714                                   struct wpa_signal_info *sig_change)
1716         struct nl_msg *msg;
1718         sig_change->current_noise = 9999;
1719         sig_change->frequency = drv->assoc_freq;
1721         msg = nlmsg_alloc();
1722         if (!msg)
1723                 return -ENOMEM;
1725         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1727         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1729         return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
1730  nla_put_failure:
1731         nlmsg_free(msg);
1732         return -ENOBUFS;
1736 static int get_noise_for_scan_results(struct nl_msg *msg, void *arg)
1738         struct nlattr *tb[NL80211_ATTR_MAX + 1];
1739         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1740         struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1741         static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1742                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1743                 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1744         };
1745         struct wpa_scan_results *scan_results = arg;
1746         struct wpa_scan_res *scan_res;
1747         size_t i;
1749         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1750                   genlmsg_attrlen(gnlh, 0), NULL);
1752         if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1753                 wpa_printf(MSG_DEBUG, "nl80211: Survey data missing");
1754                 return NL_SKIP;
1755         }
1757         if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1758                              tb[NL80211_ATTR_SURVEY_INFO],
1759                              survey_policy)) {
1760                 wpa_printf(MSG_DEBUG, "nl80211: Failed to parse nested "
1761                            "attributes");
1762                 return NL_SKIP;
1763         }
1765         if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1766                 return NL_SKIP;
1768         if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1769                 return NL_SKIP;
1771         for (i = 0; i < scan_results->num; ++i) {
1772                 scan_res = scan_results->res[i];
1773                 if (!scan_res)
1774                         continue;
1775                 if ((int) nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1776                     scan_res->freq)
1777                         continue;
1778                 if (!(scan_res->flags & WPA_SCAN_NOISE_INVALID))
1779                         continue;
1780                 scan_res->noise = (s8)
1781                         nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1782                 scan_res->flags &= ~WPA_SCAN_NOISE_INVALID;
1783         }
1785         return NL_SKIP;
1789 static int nl80211_get_noise_for_scan_results(
1790         struct wpa_driver_nl80211_data *drv,
1791         struct wpa_scan_results *scan_res)
1793         struct nl_msg *msg;
1795         msg = nlmsg_alloc();
1796         if (!msg)
1797                 return -ENOMEM;
1799         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1801         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1803         return send_and_recv_msgs(drv, msg, get_noise_for_scan_results,
1804                                   scan_res);
1805  nla_put_failure:
1806         nlmsg_free(msg);
1807         return -ENOBUFS;
1811 static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
1812                               struct nlattr *tb[])
1814         static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
1815                 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
1816                 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
1817                 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
1818                 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
1819         };
1820         struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
1821         enum nl80211_cqm_rssi_threshold_event event;
1822         union wpa_event_data ed;
1823         struct wpa_signal_info sig;
1824         int res;
1826         if (tb[NL80211_ATTR_CQM] == NULL ||
1827             nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
1828                              cqm_policy)) {
1829                 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
1830                 return;
1831         }
1833         os_memset(&ed, 0, sizeof(ed));
1835         if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
1836                 if (!tb[NL80211_ATTR_MAC])
1837                         return;
1838                 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
1839                           ETH_ALEN);
1840                 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
1841                 return;
1842         }
1844         if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
1845                 return;
1846         event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
1848         if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
1849                 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1850                            "event: RSSI high");
1851                 ed.signal_change.above_threshold = 1;
1852         } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
1853                 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1854                            "event: RSSI low");
1855                 ed.signal_change.above_threshold = 0;
1856         } else
1857                 return;
1859         res = nl80211_get_link_signal(drv, &sig);
1860         if (res == 0) {
1861                 ed.signal_change.current_signal = sig.current_signal;
1862                 ed.signal_change.current_txrate = sig.current_txrate;
1863                 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm  txrate: %d",
1864                            sig.current_signal, sig.current_txrate);
1865         }
1867         res = nl80211_get_link_noise(drv, &sig);
1868         if (res == 0) {
1869                 ed.signal_change.current_noise = sig.current_noise;
1870                 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
1871                            sig.current_noise);
1872         }
1874         wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
1878 static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
1879                                       struct nlattr **tb)
1881         u8 *addr;
1882         union wpa_event_data data;
1884         if (tb[NL80211_ATTR_MAC] == NULL)
1885                 return;
1886         addr = nla_data(tb[NL80211_ATTR_MAC]);
1887         wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
1889         if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
1890                 u8 *ies = NULL;
1891                 size_t ies_len = 0;
1892                 if (tb[NL80211_ATTR_IE]) {
1893                         ies = nla_data(tb[NL80211_ATTR_IE]);
1894                         ies_len = nla_len(tb[NL80211_ATTR_IE]);
1895                 }
1896                 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
1897                 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
1898                 return;
1899         }
1901         if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1902                 return;
1904         os_memset(&data, 0, sizeof(data));
1905         os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
1906         wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
1910 static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
1911                                       struct nlattr **tb)
1913         u8 *addr;
1914         union wpa_event_data data;
1916         if (tb[NL80211_ATTR_MAC] == NULL)
1917                 return;
1918         addr = nla_data(tb[NL80211_ATTR_MAC]);
1919         wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
1920                    MAC2STR(addr));
1922         if (is_ap_interface(drv->nlmode) && drv->device_ap_sme) {
1923                 drv_event_disassoc(drv->ctx, addr);
1924                 return;
1925         }
1927         if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1928                 return;
1930         os_memset(&data, 0, sizeof(data));
1931         os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
1932         wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
1936 static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
1937                                         struct nlattr **tb)
1939         struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
1940         static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
1941                 [NL80211_REKEY_DATA_KEK] = {
1942                         .minlen = NL80211_KEK_LEN,
1943                         .maxlen = NL80211_KEK_LEN,
1944                 },
1945                 [NL80211_REKEY_DATA_KCK] = {
1946                         .minlen = NL80211_KCK_LEN,
1947                         .maxlen = NL80211_KCK_LEN,
1948                 },
1949                 [NL80211_REKEY_DATA_REPLAY_CTR] = {
1950                         .minlen = NL80211_REPLAY_CTR_LEN,
1951                         .maxlen = NL80211_REPLAY_CTR_LEN,
1952                 },
1953         };
1954         union wpa_event_data data;
1956         if (!tb[NL80211_ATTR_MAC])
1957                 return;
1958         if (!tb[NL80211_ATTR_REKEY_DATA])
1959                 return;
1960         if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
1961                              tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
1962                 return;
1963         if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
1964                 return;
1966         os_memset(&data, 0, sizeof(data));
1967         data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
1968         wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
1969                    MAC2STR(data.driver_gtk_rekey.bssid));
1970         data.driver_gtk_rekey.replay_ctr =
1971                 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
1972         wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
1973                     data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
1974         wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
1978 static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
1979                                           struct nlattr **tb)
1981         struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
1982         static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
1983                 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
1984                 [NL80211_PMKSA_CANDIDATE_BSSID] = {
1985                         .minlen = ETH_ALEN,
1986                         .maxlen = ETH_ALEN,
1987                 },
1988                 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
1989         };
1990         union wpa_event_data data;
1992         if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
1993                 return;
1994         if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
1995                              tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
1996                 return;
1997         if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
1998             !cand[NL80211_PMKSA_CANDIDATE_BSSID])
1999                 return;
2001         os_memset(&data, 0, sizeof(data));
2002         os_memcpy(data.pmkid_candidate.bssid,
2003                   nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
2004         data.pmkid_candidate.index =
2005                 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
2006         data.pmkid_candidate.preauth =
2007                 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
2008         wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
2012 static void nl80211_client_probe_event(struct wpa_driver_nl80211_data *drv,
2013                                        struct nlattr **tb)
2015         union wpa_event_data data;
2017         if (!tb[NL80211_ATTR_MAC] || !tb[NL80211_ATTR_ACK])
2018                 return;
2020         os_memset(&data, 0, sizeof(data));
2021         os_memcpy(data.client_poll.addr,
2022                   nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
2024         wpa_supplicant_event(drv->ctx, EVENT_DRIVER_CLIENT_POLL_OK, &data);
2028 static void nl80211_spurious_frame(struct i802_bss *bss, struct nlattr **tb,
2029                                    int wds)
2031         struct wpa_driver_nl80211_data *drv = bss->drv;
2032         union wpa_event_data event;
2034         if (!tb[NL80211_ATTR_MAC])
2035                 return;
2037         os_memset(&event, 0, sizeof(event));
2038         event.rx_from_unknown.bssid = bss->addr;
2039         event.rx_from_unknown.addr = nla_data(tb[NL80211_ATTR_MAC]);
2040         event.rx_from_unknown.wds = wds;
2042         wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2046 static void do_process_drv_event(struct wpa_driver_nl80211_data *drv,
2047                                  int cmd, struct nlattr **tb)
2049         if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
2050             (cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
2051              cmd == NL80211_CMD_SCAN_ABORTED)) {
2052                 wpa_driver_nl80211_set_mode(&drv->first_bss,
2053                                             drv->ap_scan_as_station);
2054                 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
2055         }
2057         switch (cmd) {
2058         case NL80211_CMD_TRIGGER_SCAN:
2059                 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
2060                 break;
2061         case NL80211_CMD_START_SCHED_SCAN:
2062                 wpa_printf(MSG_DEBUG, "nl80211: Sched scan started");
2063                 break;
2064         case NL80211_CMD_SCHED_SCAN_STOPPED:
2065                 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stopped");
2066                 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
2067                 break;
2068         case NL80211_CMD_NEW_SCAN_RESULTS:
2069                 wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
2070                 drv->scan_complete_events = 1;
2071                 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2072                                      drv->ctx);
2073                 send_scan_event(drv, 0, tb);
2074                 break;
2075         case NL80211_CMD_SCHED_SCAN_RESULTS:
2076                 wpa_printf(MSG_DEBUG,
2077                            "nl80211: New sched scan results available");
2078                 send_scan_event(drv, 0, tb);
2079                 break;
2080         case NL80211_CMD_SCAN_ABORTED:
2081                 wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
2082                 /*
2083                  * Need to indicate that scan results are available in order
2084                  * not to make wpa_supplicant stop its scanning.
2085                  */
2086                 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
2087                                      drv->ctx);
2088                 send_scan_event(drv, 1, tb);
2089                 break;
2090         case NL80211_CMD_AUTHENTICATE:
2091         case NL80211_CMD_ASSOCIATE:
2092         case NL80211_CMD_DEAUTHENTICATE:
2093         case NL80211_CMD_DISASSOCIATE:
2094         case NL80211_CMD_FRAME_TX_STATUS:
2095         case NL80211_CMD_UNPROT_DEAUTHENTICATE:
2096         case NL80211_CMD_UNPROT_DISASSOCIATE:
2097                 mlme_event(drv, cmd, tb[NL80211_ATTR_FRAME],
2098                            tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2099                            tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
2100                            tb[NL80211_ATTR_COOKIE]);
2101                 break;
2102         case NL80211_CMD_CONNECT:
2103         case NL80211_CMD_ROAM:
2104                 mlme_event_connect(drv, cmd,
2105                                    tb[NL80211_ATTR_STATUS_CODE],
2106                                    tb[NL80211_ATTR_MAC],
2107                                    tb[NL80211_ATTR_REQ_IE],
2108                                    tb[NL80211_ATTR_RESP_IE]);
2109                 break;
2110         case NL80211_CMD_DISCONNECT:
2111                 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
2112                                       tb[NL80211_ATTR_MAC],
2113                                       tb[NL80211_ATTR_DISCONNECTED_BY_AP]);
2114                 break;
2115         case NL80211_CMD_MICHAEL_MIC_FAILURE:
2116                 mlme_event_michael_mic_failure(drv, tb);
2117                 break;
2118         case NL80211_CMD_JOIN_IBSS:
2119                 mlme_event_join_ibss(drv, tb);
2120                 break;
2121         case NL80211_CMD_REMAIN_ON_CHANNEL:
2122                 mlme_event_remain_on_channel(drv, 0, tb);
2123                 break;
2124         case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
2125                 mlme_event_remain_on_channel(drv, 1, tb);
2126                 break;
2127         case NL80211_CMD_NOTIFY_CQM:
2128                 nl80211_cqm_event(drv, tb);
2129                 break;
2130         case NL80211_CMD_REG_CHANGE:
2131                 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
2132                 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2133                                      NULL);
2134                 break;
2135         case NL80211_CMD_REG_BEACON_HINT:
2136                 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
2137                 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
2138                                      NULL);
2139                 break;
2140         case NL80211_CMD_NEW_STATION:
2141                 nl80211_new_station_event(drv, tb);
2142                 break;
2143         case NL80211_CMD_DEL_STATION:
2144                 nl80211_del_station_event(drv, tb);
2145                 break;
2146         case NL80211_CMD_SET_REKEY_OFFLOAD:
2147                 nl80211_rekey_offload_event(drv, tb);
2148                 break;
2149         case NL80211_CMD_PMKSA_CANDIDATE:
2150                 nl80211_pmksa_candidate_event(drv, tb);
2151                 break;
2152         case NL80211_CMD_PROBE_CLIENT:
2153                 nl80211_client_probe_event(drv, tb);
2154                 break;
2155         default:
2156                 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
2157                            "(cmd=%d)", cmd);
2158                 break;
2159         }
2163 static int process_drv_event(struct nl_msg *msg, void *arg)
2165         struct wpa_driver_nl80211_data *drv = arg;
2166         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2167         struct nlattr *tb[NL80211_ATTR_MAX + 1];
2169         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2170                   genlmsg_attrlen(gnlh, 0), NULL);
2172         if (tb[NL80211_ATTR_IFINDEX]) {
2173                 int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2174                 if (ifindex != drv->ifindex && !have_ifidx(drv, ifindex)) {
2175                         wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)"
2176                                    " for foreign interface (ifindex %d)",
2177                                    gnlh->cmd, ifindex);
2178                         return NL_SKIP;
2179                 }
2180         }
2182         do_process_drv_event(drv, gnlh->cmd, tb);
2183         return NL_SKIP;
2187 static int process_global_event(struct nl_msg *msg, void *arg)
2189         struct nl80211_global *global = arg;
2190         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2191         struct nlattr *tb[NL80211_ATTR_MAX + 1];
2192         struct wpa_driver_nl80211_data *drv;
2193         int ifidx = -1;
2195         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2196                   genlmsg_attrlen(gnlh, 0), NULL);
2198         if (tb[NL80211_ATTR_IFINDEX])
2199                 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
2201         dl_list_for_each(drv, &global->interfaces,
2202                          struct wpa_driver_nl80211_data, list) {
2203                 if (ifidx == -1 || ifidx == drv->ifindex ||
2204                     have_ifidx(drv, ifidx))
2205                         do_process_drv_event(drv, gnlh->cmd, tb);
2206         }
2208         return NL_SKIP;
2212 static int process_bss_event(struct nl_msg *msg, void *arg)
2214         struct i802_bss *bss = arg;
2215         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2216         struct nlattr *tb[NL80211_ATTR_MAX + 1];
2218         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2219                   genlmsg_attrlen(gnlh, 0), NULL);
2221         switch (gnlh->cmd) {
2222         case NL80211_CMD_FRAME:
2223         case NL80211_CMD_FRAME_TX_STATUS:
2224                 mlme_event(bss->drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
2225                            tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
2226                            tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
2227                            tb[NL80211_ATTR_COOKIE]);
2228                 break;
2229         case NL80211_CMD_UNEXPECTED_FRAME:
2230                 nl80211_spurious_frame(bss, tb, 0);
2231                 break;
2232         case NL80211_CMD_UNEXPECTED_4ADDR_FRAME:
2233                 nl80211_spurious_frame(bss, tb, 1);
2234                 break;
2235         default:
2236                 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
2237                            "(cmd=%d)", gnlh->cmd);
2238                 break;
2239         }
2241         return NL_SKIP;
2245 static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
2246                                              void *handle)
2248         struct nl_cb *cb = eloop_ctx;
2250         wpa_printf(MSG_DEBUG, "nl80211: Event message available");
2252         nl_recvmsgs(handle, cb);
2256 /**
2257  * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
2258  * @priv: driver_nl80211 private data
2259  * @alpha2_arg: country to which to switch to
2260  * Returns: 0 on success, -1 on failure
2261  *
2262  * This asks nl80211 to set the regulatory domain for given
2263  * country ISO / IEC alpha2.
2264  */
2265 static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
2267         struct i802_bss *bss = priv;
2268         struct wpa_driver_nl80211_data *drv = bss->drv;
2269         char alpha2[3];
2270         struct nl_msg *msg;
2272         msg = nlmsg_alloc();
2273         if (!msg)
2274                 return -ENOMEM;
2276         alpha2[0] = alpha2_arg[0];
2277         alpha2[1] = alpha2_arg[1];
2278         alpha2[2] = '\0';
2280         nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
2282         NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
2283         if (send_and_recv_msgs(drv, msg, NULL, NULL))
2284                 return -EINVAL;
2285         return 0;
2286 nla_put_failure:
2287         nlmsg_free(msg);
2288         return -EINVAL;
2292 struct wiphy_info_data {
2293         struct wpa_driver_capa *capa;
2295         unsigned int error:1;
2296         unsigned int device_ap_sme:1;
2297         unsigned int poll_command_supported:1;
2298         unsigned int data_tx_status:1;
2299         unsigned int monitor_supported:1;
2300 };
2303 static unsigned int probe_resp_offload_support(int supp_protocols)
2305         unsigned int prot = 0;
2307         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS)
2308                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS;
2309         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2)
2310                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_WPS2;
2311         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P)
2312                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_P2P;
2313         if (supp_protocols & NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U)
2314                 prot |= WPA_DRIVER_PROBE_RESP_OFFLOAD_INTERWORKING;
2316         return prot;
2320 static int wiphy_info_handler(struct nl_msg *msg, void *arg)
2322         struct nlattr *tb[NL80211_ATTR_MAX + 1];
2323         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2324         struct wiphy_info_data *info = arg;
2325         int p2p_go_supported = 0, p2p_client_supported = 0;
2326         int p2p_concurrent = 0;
2327         int auth_supported = 0, connect_supported = 0;
2328         struct wpa_driver_capa *capa = info->capa;
2329         static struct nla_policy
2330         iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
2331                 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
2332                 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
2333                 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
2334                 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
2335         },
2336         iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
2337                 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
2338                 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
2339         };
2341         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2342                   genlmsg_attrlen(gnlh, 0), NULL);
2344         if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
2345                 capa->max_scan_ssids =
2346                         nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
2348         if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
2349                 capa->max_sched_scan_ssids =
2350                         nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
2352         if (tb[NL80211_ATTR_MAX_MATCH_SETS])
2353                 capa->max_match_sets =
2354                         nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
2356         if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
2357                 struct nlattr *nl_mode;
2358                 int i;
2359                 nla_for_each_nested(nl_mode,
2360                                     tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
2361                         switch (nla_type(nl_mode)) {
2362                         case NL80211_IFTYPE_AP:
2363                                 capa->flags |= WPA_DRIVER_FLAGS_AP;
2364                                 break;
2365                         case NL80211_IFTYPE_P2P_GO:
2366                                 p2p_go_supported = 1;
2367                                 break;
2368                         case NL80211_IFTYPE_P2P_CLIENT:
2369                                 p2p_client_supported = 1;
2370                                 break;
2371                         case NL80211_IFTYPE_MONITOR:
2372                                 info->monitor_supported = 1;
2373                                 break;
2374                         }
2375                 }
2376         }
2378         if (tb[NL80211_ATTR_INTERFACE_COMBINATIONS]) {
2379                 struct nlattr *nl_combi;
2380                 int rem_combi;
2382                 nla_for_each_nested(nl_combi,
2383                                     tb[NL80211_ATTR_INTERFACE_COMBINATIONS],
2384                                     rem_combi) {
2385                         struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
2386                         struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
2387                         struct nlattr *nl_limit, *nl_mode;
2388                         int err, rem_limit, rem_mode;
2389                         int combination_has_p2p = 0, combination_has_mgd = 0;
2391                         err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
2392                                                nl_combi,
2393                                                iface_combination_policy);
2394                         if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
2395                             !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
2396                             !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
2397                                 goto broken_combination;
2399                         nla_for_each_nested(nl_limit,
2400                                             tb_comb[NL80211_IFACE_COMB_LIMITS],
2401                                             rem_limit) {
2402                                 err = nla_parse_nested(tb_limit,
2403                                                        MAX_NL80211_IFACE_LIMIT,
2404                                                        nl_limit,
2405                                                        iface_limit_policy);
2406                                 if (err ||
2407                                     !tb_limit[NL80211_IFACE_LIMIT_TYPES])
2408                                         goto broken_combination;
2410                                 nla_for_each_nested(
2411                                         nl_mode,
2412                                         tb_limit[NL80211_IFACE_LIMIT_TYPES],
2413                                         rem_mode) {
2414                                         int ift = nla_type(nl_mode);
2415                                         if (ift == NL80211_IFTYPE_P2P_GO ||
2416                                             ift == NL80211_IFTYPE_P2P_CLIENT)
2417                                                 combination_has_p2p = 1;
2418                                         if (ift == NL80211_IFTYPE_STATION)
2419                                                 combination_has_mgd = 1;
2420                                 }
2421                                 if (combination_has_p2p && combination_has_mgd)
2422                                         break;
2423                         }
2425                         if (combination_has_p2p && combination_has_mgd) {
2426                                 p2p_concurrent = 1;
2427                                 break;
2428                         }
2430 broken_combination:
2431                         ;
2432                 }
2433         }
2435         if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
2436                 struct nlattr *nl_cmd;
2437                 int i;
2439                 nla_for_each_nested(nl_cmd,
2440                                     tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
2441                         switch (nla_get_u32(nl_cmd)) {
2442                         case NL80211_CMD_AUTHENTICATE:
2443                                 auth_supported = 1;
2444                                 break;
2445                         case NL80211_CMD_CONNECT:
2446                                 connect_supported = 1;
2447                                 break;
2448                         case NL80211_CMD_START_SCHED_SCAN:
2449                                 capa->sched_scan_supported = 1;
2450                                 break;
2451                         case NL80211_CMD_PROBE_CLIENT:
2452                                 info->poll_command_supported = 1;
2453                                 break;
2454                         }
2455                 }
2456         }
2458         if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
2459                 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
2460                            "off-channel TX");
2461                 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
2462         }
2464         if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
2465                 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
2466                 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
2467         }
2469         /* default to 5000 since early versions of mac80211 don't set it */
2470         capa->max_remain_on_chan = 5000;
2472         if (tb[NL80211_ATTR_SUPPORT_AP_UAPSD])
2473                 capa->flags |= WPA_DRIVER_FLAGS_AP_UAPSD;
2475         if (tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION])
2476                 capa->max_remain_on_chan =
2477                         nla_get_u32(tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
2479         if (auth_supported)
2480                 capa->flags |= WPA_DRIVER_FLAGS_SME;
2481         else if (!connect_supported) {
2482                 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
2483                            "authentication/association or connect commands");
2484                 info->error = 1;
2485         }
2487         if (p2p_go_supported && p2p_client_supported)
2488                 capa->flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
2489         if (p2p_concurrent) {
2490                 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
2491                            "interface (driver advertised support)");
2492                 capa->flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
2493                 capa->flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
2494         }
2496         if (tb[NL80211_ATTR_TDLS_SUPPORT]) {
2497                 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
2498                 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
2500                 if (tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]) {
2501                         wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
2502                         capa->flags |=
2503                                 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
2504                 }
2505         }
2507         if (tb[NL80211_ATTR_DEVICE_AP_SME])
2508                 info->device_ap_sme = 1;
2510         if (tb[NL80211_ATTR_FEATURE_FLAGS]) {
2511                 u32 flags = nla_get_u32(tb[NL80211_ATTR_FEATURE_FLAGS]);
2513                 if (flags & NL80211_FEATURE_SK_TX_STATUS)
2514                         info->data_tx_status = 1;
2515         }
2517         if (tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]) {
2518                 int protocols =
2519                         nla_get_u32(tb[NL80211_ATTR_PROBE_RESP_OFFLOAD]);
2520                 wpa_printf(MSG_DEBUG, "nl80211: Supports Probe Response "
2521                            "offload in AP mode");
2522                 capa->flags |= WPA_DRIVER_FLAGS_PROBE_RESP_OFFLOAD;
2523                 capa->probe_resp_offloads =
2524                         probe_resp_offload_support(protocols);
2525         }
2527         return NL_SKIP;
2531 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
2532                                        struct wiphy_info_data *info)
2534         struct nl_msg *msg;
2536         os_memset(info, 0, sizeof(*info));
2537         info->capa = &drv->capa;
2539         msg = nlmsg_alloc();
2540         if (!msg)
2541                 return -1;
2543         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
2545         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
2547         if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0)
2548                 return 0;
2549         msg = NULL;
2550 nla_put_failure:
2551         nlmsg_free(msg);
2552         return -1;
2556 static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
2558         struct wiphy_info_data info;
2559         if (wpa_driver_nl80211_get_info(drv, &info))
2560                 return -1;
2562         if (info.error)
2563                 return -1;
2565         drv->has_capability = 1;
2566         /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
2567         drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2568                 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2569                 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2570                 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
2571         drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
2572                 WPA_DRIVER_CAPA_ENC_WEP104 |
2573                 WPA_DRIVER_CAPA_ENC_TKIP |
2574                 WPA_DRIVER_CAPA_ENC_CCMP;
2575         drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
2576                 WPA_DRIVER_AUTH_SHARED |
2577                 WPA_DRIVER_AUTH_LEAP;
2579         drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
2580         drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
2581         drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
2582         drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
2584         drv->device_ap_sme = info.device_ap_sme;
2585         drv->poll_command_supported = info.poll_command_supported;
2586         drv->data_tx_status = info.data_tx_status;
2588         /*
2589          * If poll command is supported mac80211 is new enough to
2590          * have everything we need to not need monitor interfaces.
2591          */
2592         drv->use_monitor = !info.poll_command_supported;
2594         if (drv->device_ap_sme && drv->use_monitor) {
2595                 /*
2596                  * Non-mac80211 drivers may not support monitor interface.
2597                  * Make sure we do not get stuck with incorrect capability here
2598                  * by explicitly testing this.
2599                  */
2600                 if (!info.monitor_supported) {
2601                         wpa_printf(MSG_DEBUG, "nl80211: Disable use_monitor "
2602                                    "with device_ap_sme since no monitor mode "
2603                                    "support detected");
2604                         drv->use_monitor = 0;
2605                 }
2606         }
2608         /*
2609          * If we aren't going to use monitor interfaces, but the
2610          * driver doesn't support data TX status, we won't get TX
2611          * status for EAPOL frames.
2612          */
2613         if (!drv->use_monitor && !info.data_tx_status)
2614                 drv->capa.flags &= ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
2616         return 0;
2620 #ifdef ANDROID
2621 static int android_genl_ctrl_resolve(struct nl_handle *handle,
2622                                      const char *name)
2624         /*
2625          * Android ICS has very minimal genl_ctrl_resolve() implementation, so
2626          * need to work around that.
2627          */
2628         struct nl_cache *cache = NULL;
2629         struct genl_family *nl80211 = NULL;
2630         int id = -1;
2632         if (genl_ctrl_alloc_cache(handle, &cache) < 0) {
2633                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
2634                            "netlink cache");
2635                 goto fail;
2636         }
2638         nl80211 = genl_ctrl_search_by_name(cache, name);
2639         if (nl80211 == NULL)
2640                 goto fail;
2642         id = genl_family_get_id(nl80211);
2644 fail:
2645         if (nl80211)
2646                 genl_family_put(nl80211);
2647         if (cache)
2648                 nl_cache_free(cache);
2650         return id;
2652 #define genl_ctrl_resolve android_genl_ctrl_resolve
2653 #endif /* ANDROID */
2656 static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
2658         int ret;
2660         global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2661         if (global->nl_cb == NULL) {
2662                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
2663                            "callbacks");
2664                 return -1;
2665         }
2667         global->nl = nl_create_handle(global->nl_cb, "nl");
2668         if (global->nl == NULL)
2669                 goto err;
2671         global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
2672         if (global->nl80211_id < 0) {
2673                 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
2674                            "found");
2675                 goto err;
2676         }
2678         global->nl_event = nl_create_handle(global->nl_cb, "event");
2679         if (global->nl_event == NULL)
2680                 goto err;
2682         ret = nl_get_multicast_id(global, "nl80211", "scan");
2683         if (ret >= 0)
2684                 ret = nl_socket_add_membership(global->nl_event, ret);
2685         if (ret < 0) {
2686                 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
2687                            "membership for scan events: %d (%s)",
2688                            ret, strerror(-ret));
2689                 goto err;
2690         }
2692         ret = nl_get_multicast_id(global, "nl80211", "mlme");
2693         if (ret >= 0)
2694                 ret = nl_socket_add_membership(global->nl_event, ret);
2695         if (ret < 0) {
2696                 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
2697                            "membership for mlme events: %d (%s)",
2698                            ret, strerror(-ret));
2699                 goto err;
2700         }
2702         ret = nl_get_multicast_id(global, "nl80211", "regulatory");
2703         if (ret >= 0)
2704                 ret = nl_socket_add_membership(global->nl_event, ret);
2705         if (ret < 0) {
2706                 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
2707                            "membership for regulatory events: %d (%s)",
2708                            ret, strerror(-ret));
2709                 /* Continue without regulatory events */
2710         }
2712         nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2713                   no_seq_check, NULL);
2714         nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2715                   process_global_event, global);
2717         eloop_register_read_sock(nl_socket_get_fd(global->nl_event),
2718                                  wpa_driver_nl80211_event_receive,
2719                                  global->nl_cb, global->nl_event);
2721         return 0;
2723 err:
2724         nl_destroy_handles(&global->nl_event);
2725         nl_destroy_handles(&global->nl);
2726         nl_cb_put(global->nl_cb);
2727         global->nl_cb = NULL;
2728         return -1;
2732 static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
2734         drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2735         if (!drv->nl_cb) {
2736                 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
2737                 return -1;
2738         }
2740         nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2741                   no_seq_check, NULL);
2742         nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2743                   process_drv_event, drv);
2745         return 0;
2749 static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
2751         wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
2752         /*
2753          * This may be for any interface; use ifdown event to disable
2754          * interface.
2755          */
2759 static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
2761         struct wpa_driver_nl80211_data *drv = ctx;
2762         wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
2763         if (linux_set_iface_flags(drv->global->ioctl_sock,
2764                                   drv->first_bss.ifname, 1)) {
2765                 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
2766                            "after rfkill unblock");
2767                 return;
2768         }
2769         /* rtnetlink ifup handler will report interface as enabled */
2773 static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
2775         /* Find phy (radio) to which this interface belongs */
2776         char buf[90], *pos;
2777         int f, rv;
2779         drv->phyname[0] = '\0';
2780         snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
2781                  drv->first_bss.ifname);
2782         f = open(buf, O_RDONLY);
2783         if (f < 0) {
2784                 wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
2785                            buf, strerror(errno));
2786                 return;
2787         }
2789         rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
2790         close(f);
2791         if (rv < 0) {
2792                 wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
2793                            buf, strerror(errno));
2794                 return;
2795         }
2797         drv->phyname[rv] = '\0';
2798         pos = os_strchr(drv->phyname, '\n');
2799         if (pos)
2800                 *pos = '\0';
2801         wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2802                    drv->first_bss.ifname, drv->phyname);
2806 static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
2807                                                       void *eloop_ctx,
2808                                                       void *handle)
2810         struct wpa_driver_nl80211_data *drv = eloop_ctx;
2811         u8 data[2048];
2812         struct msghdr msg;
2813         struct iovec entry;
2814         struct {
2815                 struct cmsghdr cm;
2816                 char control[512];
2817         } control;
2818         struct cmsghdr *cmsg;
2819         int res, found_ee = 0, found_wifi = 0, acked = 0;
2820         union wpa_event_data event;
2822         memset(&msg, 0, sizeof(msg));
2823         msg.msg_iov = &entry;
2824         msg.msg_iovlen = 1;
2825         entry.iov_base = data;
2826         entry.iov_len = sizeof(data);
2827         msg.msg_control = &control;
2828         msg.msg_controllen = sizeof(control);
2830         res = recvmsg(sock, &msg, MSG_ERRQUEUE);
2831         /* if error or not fitting 802.3 header, return */
2832         if (res < 14)
2833                 return;
2835         for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
2836         {
2837                 if (cmsg->cmsg_level == SOL_SOCKET &&
2838                     cmsg->cmsg_type == SCM_WIFI_STATUS) {
2839                         int *ack;
2841                         found_wifi = 1;
2842                         ack = (void *)CMSG_DATA(cmsg);
2843                         acked = *ack;
2844                 }
2846                 if (cmsg->cmsg_level == SOL_PACKET &&
2847                     cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
2848                         struct sock_extended_err *err =
2849                                 (struct sock_extended_err *)CMSG_DATA(cmsg);
2851                         if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
2852                                 found_ee = 1;
2853                 }
2854         }
2856         if (!found_ee || !found_wifi)
2857                 return;
2859         memset(&event, 0, sizeof(event));
2860         event.eapol_tx_status.dst = data;
2861         event.eapol_tx_status.data = data + 14;
2862         event.eapol_tx_status.data_len = res - 14;
2863         event.eapol_tx_status.ack = acked;
2864         wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
2868 static int nl80211_init_bss(struct i802_bss *bss)
2870         bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2871         if (!bss->nl_cb)
2872                 return -1;
2874         nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
2875                   no_seq_check, NULL);
2876         nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
2877                   process_bss_event, bss);
2879         return 0;
2883 static void nl80211_destroy_bss(struct i802_bss *bss)
2885         nl_cb_put(bss->nl_cb);
2886         bss->nl_cb = NULL;
2890 /**
2891  * wpa_driver_nl80211_init - Initialize nl80211 driver interface
2892  * @ctx: context to be used when calling wpa_supplicant functions,
2893  * e.g., wpa_supplicant_event()
2894  * @ifname: interface name, e.g., wlan0
2895  * @global_priv: private driver global data from global_init()
2896  * Returns: Pointer to private data, %NULL on failure
2897  */
2898 static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
2899                                       void *global_priv)
2901         struct wpa_driver_nl80211_data *drv;
2902         struct rfkill_config *rcfg;
2903         struct i802_bss *bss;
2905         if (global_priv == NULL)
2906                 return NULL;
2907         drv = os_zalloc(sizeof(*drv));
2908         if (drv == NULL)
2909                 return NULL;
2910         drv->global = global_priv;
2911         drv->ctx = ctx;
2912         bss = &drv->first_bss;
2913         bss->drv = drv;
2914         os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
2915         drv->monitor_ifidx = -1;
2916         drv->monitor_sock = -1;
2917         drv->eapol_tx_sock = -1;
2918         drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
2920         if (wpa_driver_nl80211_init_nl(drv)) {
2921                 os_free(drv);
2922                 return NULL;
2923         }
2925         if (nl80211_init_bss(bss))
2926                 goto failed;
2928         nl80211_get_phy_name(drv);
2930         rcfg = os_zalloc(sizeof(*rcfg));
2931         if (rcfg == NULL)
2932                 goto failed;
2933         rcfg->ctx = drv;
2934         os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
2935         rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
2936         rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
2937         drv->rfkill = rfkill_init(rcfg);
2938         if (drv->rfkill == NULL) {
2939                 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
2940                 os_free(rcfg);
2941         }
2943         if (wpa_driver_nl80211_finish_drv_init(drv))
2944                 goto failed;
2946         drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
2947         if (drv->eapol_tx_sock < 0)
2948                 goto failed;
2950         if (drv->data_tx_status) {
2951                 int enabled = 1;
2953                 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
2954                                &enabled, sizeof(enabled)) < 0) {
2955                         wpa_printf(MSG_DEBUG,
2956                                 "nl80211: wifi status sockopt failed\n");
2957                         drv->data_tx_status = 0;
2958                         if (!drv->use_monitor)
2959                                 drv->capa.flags &=
2960                                         ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
2961                 } else {
2962                         eloop_register_read_sock(drv->eapol_tx_sock,
2963                                 wpa_driver_nl80211_handle_eapol_tx_status,
2964                                 drv, NULL);
2965                 }
2966         }
2968         if (drv->global) {
2969                 dl_list_add(&drv->global->interfaces, &drv->list);
2970                 drv->in_interface_list = 1;
2971         }
2973         return bss;
2975 failed:
2976         wpa_driver_nl80211_deinit(bss);
2977         return NULL;
2981 static int nl80211_register_frame(struct i802_bss *bss,
2982                                   struct nl_handle *nl_handle,
2983                                   u16 type, const u8 *match, size_t match_len)
2985         struct wpa_driver_nl80211_data *drv = bss->drv;
2986         struct nl_msg *msg;
2987         int ret = -1;
2989         msg = nlmsg_alloc();
2990         if (!msg)
2991                 return -1;
2993         wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x nl_handle=%p",
2994                    type, nl_handle);
2995         wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
2996                     match, match_len);
2998         nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
3000         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
3001         NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
3002         NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
3004         ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
3005         msg = NULL;
3006         if (ret) {
3007                 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
3008                            "failed (type=%u): ret=%d (%s)",
3009                            type, ret, strerror(-ret));
3010                 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
3011                             match, match_len);
3012                 goto nla_put_failure;
3013         }
3014         ret = 0;
3015 nla_put_failure:
3016         nlmsg_free(msg);
3017         return ret;
3021 static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
3023         struct wpa_driver_nl80211_data *drv = bss->drv;
3025         if (bss->nl_mgmt) {
3026                 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
3027                            "already on! (nl_mgmt=%p)", bss->nl_mgmt);
3028                 return -1;
3029         }
3031         bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
3032         if (bss->nl_mgmt == NULL)
3033                 return -1;
3035         eloop_register_read_sock(nl_socket_get_fd(bss->nl_mgmt),
3036                                  wpa_driver_nl80211_event_receive, bss->nl_cb,
3037                                  bss->nl_mgmt);
3039         return 0;
3043 static int nl80211_register_action_frame(struct i802_bss *bss,
3044                                          const u8 *match, size_t match_len)
3046         u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
3047         return nl80211_register_frame(bss, bss->nl_mgmt,
3048                                       type, match, match_len);
3052 static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
3054         struct wpa_driver_nl80211_data *drv = bss->drv;
3056         if (nl80211_alloc_mgmt_handle(bss))
3057                 return -1;
3058         wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
3059                    "handle %p", bss->nl_mgmt);
3061 #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
3062         /* GAS Initial Request */
3063         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
3064                 return -1;
3065         /* GAS Initial Response */
3066         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
3067                 return -1;
3068         /* GAS Comeback Request */
3069         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
3070                 return -1;
3071         /* GAS Comeback Response */
3072         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
3073                 return -1;
3074 #endif /* CONFIG_P2P || CONFIG_INTERWORKING */
3075 #ifdef CONFIG_P2P
3076         /* P2P Public Action */
3077         if (nl80211_register_action_frame(bss,
3078                                           (u8 *) "\x04\x09\x50\x6f\x9a\x09",
3079                                           6) < 0)
3080                 return -1;
3081         /* P2P Action */
3082         if (nl80211_register_action_frame(bss,
3083                                           (u8 *) "\x7f\x50\x6f\x9a\x09",
3084                                           5) < 0)
3085                 return -1;
3086 #endif /* CONFIG_P2P */
3087 #ifdef CONFIG_IEEE80211W
3088         /* SA Query Response */
3089         if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
3090                 return -1;
3091 #endif /* CONFIG_IEEE80211W */
3092 #ifdef CONFIG_TDLS
3093         if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
3094                 /* TDLS Discovery Response */
3095                 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
3096                     0)
3097                         return -1;
3098         }
3099 #endif /* CONFIG_TDLS */
3101         /* FT Action frames */
3102         if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
3103                 return -1;
3104         else
3105                 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
3106                         WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
3108         /* WNM - BSS Transition Management Request */
3109         if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
3110                 return -1;
3112         return 0;
3116 static int nl80211_register_spurious_class3(struct i802_bss *bss)
3118         struct wpa_driver_nl80211_data *drv = bss->drv;
3119         struct nl_msg *msg;
3120         int ret = -1;
3122         msg = nlmsg_alloc();
3123         if (!msg)
3124                 return -1;
3126         nl80211_cmd(drv, msg, 0, NL80211_CMD_UNEXPECTED_FRAME);
3128         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
3130         ret = send_and_recv(drv->global, bss->nl_mgmt, msg, NULL, NULL);
3131         msg = NULL;
3132         if (ret) {
3133                 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
3134                            "failed: ret=%d (%s)",
3135                            ret, strerror(-ret));
3136                 goto nla_put_failure;
3137         }
3138         ret = 0;
3139 nla_put_failure:
3140         nlmsg_free(msg);
3141         return ret;
3145 static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
3147         static const int stypes[] = {
3148                 WLAN_FC_STYPE_AUTH,
3149                 WLAN_FC_STYPE_ASSOC_REQ,
3150                 WLAN_FC_STYPE_REASSOC_REQ,
3151                 WLAN_FC_STYPE_DISASSOC,
3152                 WLAN_FC_STYPE_DEAUTH,
3153                 WLAN_FC_STYPE_ACTION,
3154                 WLAN_FC_STYPE_PROBE_REQ,
3155 /* Beacon doesn't work as mac80211 doesn't currently allow
3156  * it, but it wouldn't really be the right thing anyway as
3157  * it isn't per interface ... maybe just dump the scan
3158  * results periodically for OLBC?
3159  */
3160 //              WLAN_FC_STYPE_BEACON,
3161         };
3162         unsigned int i;
3164         if (nl80211_alloc_mgmt_handle(bss))
3165                 return -1;
3166         wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3167                    "handle %p", bss->nl_mgmt);
3169         for (i = 0; i < sizeof(stypes) / sizeof(stypes[0]); i++) {
3170                 if (nl80211_register_frame(bss, bss->nl_mgmt,
3171                                            (WLAN_FC_TYPE_MGMT << 2) |
3172                                            (stypes[i] << 4),
3173                                            NULL, 0) < 0) {
3174                         goto out_err;
3175                 }
3176         }
3178         if (nl80211_register_spurious_class3(bss))
3179                 goto out_err;
3181         if (nl80211_get_wiphy_data_ap(bss) == NULL)
3182                 goto out_err;
3184         return 0;
3186 out_err:
3187         eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3188         nl_destroy_handles(&bss->nl_mgmt);
3189         return -1;
3193 static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
3195         if (nl80211_alloc_mgmt_handle(bss))
3196                 return -1;
3197         wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
3198                    "handle %p (device SME)", bss->nl_mgmt);
3200         if (nl80211_register_frame(bss, bss->nl_mgmt,
3201                                    (WLAN_FC_TYPE_MGMT << 2) |
3202                                    (WLAN_FC_STYPE_ACTION << 4),
3203                                    NULL, 0) < 0)
3204                 goto out_err;
3206         return 0;
3208 out_err:
3209         eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3210         nl_destroy_handles(&bss->nl_mgmt);
3211         return -1;
3215 static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
3217         if (bss->nl_mgmt == NULL)
3218                 return;
3219         wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
3220                    "(%s)", bss->nl_mgmt, reason);
3221         eloop_unregister_read_sock(nl_socket_get_fd(bss->nl_mgmt));
3222         nl_destroy_handles(&bss->nl_mgmt);
3224         nl80211_put_wiphy_data_ap(bss);
3228 static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
3230         wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
3234 static int
3235 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
3237         struct i802_bss *bss = &drv->first_bss;
3238         int send_rfkill_event = 0;
3240         drv->ifindex = if_nametoindex(bss->ifname);
3241         drv->first_bss.ifindex = drv->ifindex;
3243 #ifndef HOSTAPD
3244         /*
3245          * Make sure the interface starts up in station mode unless this is a
3246          * dynamically added interface (e.g., P2P) that was already configured
3247          * with proper iftype.
3248          */
3249         if (drv->ifindex != drv->global->if_add_ifindex &&
3250             wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION) < 0) {
3251                 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to "
3252                            "use managed mode");
3253                 return -1;
3254         }
3256         if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
3257                 if (rfkill_is_blocked(drv->rfkill)) {
3258                         wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
3259                                    "interface '%s' due to rfkill",
3260                                    bss->ifname);
3261                         drv->if_disabled = 1;
3262                         send_rfkill_event = 1;
3263                 } else {
3264                         wpa_printf(MSG_ERROR, "nl80211: Could not set "
3265                                    "interface '%s' UP", bss->ifname);
3266                         return -1;
3267                 }
3268         }
3270         netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
3271                                1, IF_OPER_DORMANT);
3272 #endif /* HOSTAPD */
3274         if (wpa_driver_nl80211_capa(drv))
3275                 return -1;
3277         if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
3278                                bss->addr))
3279                 return -1;
3281         if (send_rfkill_event) {
3282                 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
3283                                        drv, drv->ctx);
3284         }
3286         return 0;
3290 static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
3292         struct nl_msg *msg;
3294         msg = nlmsg_alloc();
3295         if (!msg)
3296                 return -ENOMEM;
3298         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
3299         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3301         return send_and_recv_msgs(drv, msg, NULL, NULL);
3302  nla_put_failure:
3303         nlmsg_free(msg);
3304         return -ENOBUFS;
3308 /**
3309  * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
3310  * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
3311  *
3312  * Shut down driver interface and processing of driver events. Free
3313  * private data buffer if one was allocated in wpa_driver_nl80211_init().
3314  */
3315 static void wpa_driver_nl80211_deinit(void *priv)
3317         struct i802_bss *bss = priv;
3318         struct wpa_driver_nl80211_data *drv = bss->drv;
3320         if (drv->data_tx_status)
3321                 eloop_unregister_read_sock(drv->eapol_tx_sock);
3322         if (drv->eapol_tx_sock >= 0)
3323                 close(drv->eapol_tx_sock);
3325         if (bss->nl_preq)
3326                 wpa_driver_nl80211_probe_req_report(bss, 0);
3327         if (bss->added_if_into_bridge) {
3328                 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
3329                                     bss->ifname) < 0)
3330                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3331                                    "interface %s from bridge %s: %s",
3332                                    bss->ifname, bss->brname, strerror(errno));
3333         }
3334         if (bss->added_bridge) {
3335                 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
3336                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
3337                                    "bridge %s: %s",
3338                                    bss->brname, strerror(errno));
3339         }
3341         nl80211_remove_monitor_interface(drv);
3343         if (is_ap_interface(drv->nlmode))
3344                 wpa_driver_nl80211_del_beacon(drv);
3346 #ifdef HOSTAPD
3347         if (drv->last_freq_ht) {
3348                 /* Clear HT flags from the driver */
3349                 struct hostapd_freq_params freq;
3350                 os_memset(&freq, 0, sizeof(freq));
3351                 freq.freq = drv->last_freq;
3352                 i802_set_freq(priv, &freq);
3353         }
3355         if (drv->eapol_sock >= 0) {
3356                 eloop_unregister_read_sock(drv->eapol_sock);
3357                 close(drv->eapol_sock);
3358         }
3360         if (drv->if_indices != drv->default_if_indices)
3361                 os_free(drv->if_indices);
3362 #endif /* HOSTAPD */
3364         if (drv->disabled_11b_rates)
3365                 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3367         netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
3368                                IF_OPER_UP);
3369         rfkill_deinit(drv->rfkill);
3371         eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3373         (void) linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
3374         wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
3375         nl80211_mgmt_unsubscribe(bss, "deinit");
3377         nl_cb_put(drv->nl_cb);
3379         nl80211_destroy_bss(&drv->first_bss);
3381         os_free(drv->filter_ssids);
3383         os_free(drv->auth_ie);
3385         if (drv->in_interface_list)
3386                 dl_list_del(&drv->list);
3388         os_free(drv);
3392 /**
3393  * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
3394  * @eloop_ctx: Driver private data
3395  * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
3396  *
3397  * This function can be used as registered timeout when starting a scan to
3398  * generate a scan completed event if the driver does not report this.
3399  */
3400 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
3402         struct wpa_driver_nl80211_data *drv = eloop_ctx;
3403         if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
3404                 wpa_driver_nl80211_set_mode(&drv->first_bss,
3405                                             drv->ap_scan_as_station);
3406                 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
3407         }
3408         wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
3409         wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
3413 /**
3414  * wpa_driver_nl80211_scan - Request the driver to initiate scan
3415  * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3416  * @params: Scan parameters
3417  * Returns: 0 on success, -1 on failure
3418  */
3419 static int wpa_driver_nl80211_scan(void *priv,
3420                                    struct wpa_driver_scan_params *params)
3422         struct i802_bss *bss = priv;
3423         struct wpa_driver_nl80211_data *drv = bss->drv;
3424         int ret = 0, timeout;
3425         struct nl_msg *msg, *ssids, *freqs, *rates;
3426         size_t i;
3428         drv->scan_for_auth = 0;
3430         msg = nlmsg_alloc();
3431         ssids = nlmsg_alloc();
3432         freqs = nlmsg_alloc();
3433         rates = nlmsg_alloc();
3434         if (!msg || !ssids || !freqs || !rates) {
3435                 nlmsg_free(msg);
3436                 nlmsg_free(ssids);
3437                 nlmsg_free(freqs);
3438                 nlmsg_free(rates);
3439                 return -1;
3440         }
3442         os_free(drv->filter_ssids);
3443         drv->filter_ssids = params->filter_ssids;
3444         params->filter_ssids = NULL;
3445         drv->num_filter_ssids = params->num_filter_ssids;
3447         nl80211_cmd(drv, msg, 0, NL80211_CMD_TRIGGER_SCAN);
3449         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3451         for (i = 0; i < params->num_ssids; i++) {
3452                 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
3453                                   params->ssids[i].ssid,
3454                                   params->ssids[i].ssid_len);
3455                 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
3456                         params->ssids[i].ssid);
3457         }
3458         if (params->num_ssids)
3459                 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
3461         if (params->extra_ies) {
3462                 wpa_hexdump(MSG_MSGDUMP, "nl80211: Scan extra IEs",
3463                             params->extra_ies, params->extra_ies_len);
3464                 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
3465                         params->extra_ies);
3466         }
3468         if (params->freqs) {
3469                 for (i = 0; params->freqs[i]; i++) {
3470                         wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
3471                                    "MHz", params->freqs[i]);
3472                         NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
3473                 }
3474                 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
3475         }
3477         if (params->p2p_probe) {
3478                 wpa_printf(MSG_DEBUG, "nl80211: P2P probe - mask SuppRates");
3480                 /*
3481                  * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
3482                  * by masking out everything else apart from the OFDM rates 6,
3483                  * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
3484                  * rates are left enabled.
3485                  */
3486                 NLA_PUT(rates, NL80211_BAND_2GHZ, 8,
3487                         "\x0c\x12\x18\x24\x30\x48\x60\x6c");
3488                 nla_put_nested(msg, NL80211_ATTR_SCAN_SUPP_RATES, rates);
3490                 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
3491         }
3493         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3494         msg = NULL;
3495         if (ret) {
3496                 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
3497                            "(%s)", ret, strerror(-ret));
3498 #ifdef HOSTAPD
3499                 if (is_ap_interface(drv->nlmode)) {
3500                         /*
3501                          * mac80211 does not allow scan requests in AP mode, so
3502                          * try to do this in station mode.
3503                          */
3504                         if (wpa_driver_nl80211_set_mode(
3505                                     bss, NL80211_IFTYPE_STATION))
3506                                 goto nla_put_failure;
3508                         if (wpa_driver_nl80211_scan(drv, params)) {
3509                                 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
3510                                 goto nla_put_failure;
3511                         }
3513                         /* Restore AP mode when processing scan results */
3514                         drv->ap_scan_as_station = drv->nlmode;
3515                         ret = 0;
3516                 } else
3517                         goto nla_put_failure;
3518 #else /* HOSTAPD */
3519                 goto nla_put_failure;
3520 #endif /* HOSTAPD */
3521         }
3523         /* Not all drivers generate "scan completed" wireless event, so try to
3524          * read results after a timeout. */
3525         timeout = 10;
3526         if (drv->scan_complete_events) {
3527                 /*
3528                  * The driver seems to deliver events to notify when scan is
3529                  * complete, so use longer timeout to avoid race conditions
3530                  * with scanning and following association request.
3531                  */
3532                 timeout = 30;
3533         }
3534         wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
3535                    "seconds", ret, timeout);
3536         eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
3537         eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
3538                                drv, drv->ctx);
3540 nla_put_failure:
3541         nlmsg_free(ssids);
3542         nlmsg_free(msg);
3543         nlmsg_free(freqs);
3544         nlmsg_free(rates);
3545         return ret;
3549 /**
3550  * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
3551  * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3552  * @params: Scan parameters
3553  * @interval: Interval between scan cycles in milliseconds
3554  * Returns: 0 on success, -1 on failure or if not supported
3555  */
3556 static int wpa_driver_nl80211_sched_scan(void *priv,
3557                                          struct wpa_driver_scan_params *params,
3558                                          u32 interval)
3560         struct i802_bss *bss = priv;
3561         struct wpa_driver_nl80211_data *drv = bss->drv;
3562         int ret = 0;
3563         struct nl_msg *msg, *ssids, *freqs, *match_set_ssid, *match_sets;
3564         size_t i;
3566 #ifdef ANDROID
3567         if (!drv->capa.sched_scan_supported)
3568                 return android_pno_start(bss, params);
3569 #endif /* ANDROID */
3571         msg = nlmsg_alloc();
3572         ssids = nlmsg_alloc();
3573         freqs = nlmsg_alloc();
3574         if (!msg || !ssids || !freqs) {
3575                 nlmsg_free(msg);
3576                 nlmsg_free(ssids);
3577                 nlmsg_free(freqs);
3578                 return -1;
3579         }
3581         os_free(drv->filter_ssids);
3582         drv->filter_ssids = params->filter_ssids;
3583         params->filter_ssids = NULL;
3584         drv->num_filter_ssids = params->num_filter_ssids;
3586         nl80211_cmd(drv, msg, 0, NL80211_CMD_START_SCHED_SCAN);
3588         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3590         NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
3592         if (drv->num_filter_ssids &&
3593             (int) drv->num_filter_ssids <= drv->capa.max_match_sets) {
3594                 match_sets = nlmsg_alloc();
3596                 for (i = 0; i < drv->num_filter_ssids; i++) {
3597                         wpa_hexdump_ascii(MSG_MSGDUMP,
3598                                           "nl80211: Sched scan filter SSID",
3599                                           drv->filter_ssids[i].ssid,
3600                                           drv->filter_ssids[i].ssid_len);
3602                         match_set_ssid = nlmsg_alloc();
3603                         nla_put(match_set_ssid,
3604                                 NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
3605                                 drv->filter_ssids[i].ssid_len,
3606                                 drv->filter_ssids[i].ssid);
3608                         nla_put_nested(match_sets, i + 1, match_set_ssid);
3610                         nlmsg_free(match_set_ssid);
3611                 }
3613                 nla_put_nested(msg, NL80211_ATTR_SCHED_SCAN_MATCH,
3614                                match_sets);
3615                 nlmsg_free(match_sets);
3616         }
3618         for (i = 0; i < params->num_ssids; i++) {
3619                 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Sched scan SSID",
3620                                   params->ssids[i].ssid,
3621                                   params->ssids[i].ssid_len);
3622                 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
3623                         params->ssids[i].ssid);
3624         }
3625         if (params->num_ssids)
3626                 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
3628         if (params->extra_ies) {
3629                 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Sched scan extra IEs",
3630                                   params->extra_ies, params->extra_ies_len);
3631                 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
3632                         params->extra_ies);
3633         }
3635         if (params->freqs) {
3636                 for (i = 0; params->freqs[i]; i++) {
3637                         wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
3638                                    "MHz", params->freqs[i]);
3639                         NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
3640                 }
3641                 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
3642         }
3644         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3646         /* TODO: if we get an error here, we should fall back to normal scan */
3648         msg = NULL;
3649         if (ret) {
3650                 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
3651                            "ret=%d (%s)", ret, strerror(-ret));
3652                 goto nla_put_failure;
3653         }
3655         wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
3656                    "scan interval %d msec", ret, interval);
3658 nla_put_failure:
3659         nlmsg_free(ssids);
3660         nlmsg_free(msg);
3661         nlmsg_free(freqs);
3662         return ret;
3666 /**
3667  * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
3668  * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
3669  * Returns: 0 on success, -1 on failure or if not supported
3670  */
3671 static int wpa_driver_nl80211_stop_sched_scan(void *priv)
3673         struct i802_bss *bss = priv;
3674         struct wpa_driver_nl80211_data *drv = bss->drv;
3675         int ret = 0;
3676         struct nl_msg *msg;
3678 #ifdef ANDROID
3679         if (!drv->capa.sched_scan_supported)
3680                 return android_pno_stop(bss);
3681 #endif /* ANDROID */
3683         msg = nlmsg_alloc();
3684         if (!msg)
3685                 return -1;
3687         nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
3689         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3691         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3692         msg = NULL;
3693         if (ret) {
3694                 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
3695                            "ret=%d (%s)", ret, strerror(-ret));
3696                 goto nla_put_failure;
3697         }
3699         wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
3701 nla_put_failure:
3702         nlmsg_free(msg);
3703         return ret;
3707 static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
3709         const u8 *end, *pos;
3711         if (ies == NULL)
3712                 return NULL;
3714         pos = ies;
3715         end = ies + ies_len;
3717         while (pos + 1 < end) {
3718                 if (pos + 2 + pos[1] > end)
3719                         break;
3720                 if (pos[0] == ie)
3721                         return pos;
3722                 pos += 2 + pos[1];
3723         }
3725         return NULL;
3729 static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
3730                                  const u8 *ie, size_t ie_len)
3732         const u8 *ssid;
3733         size_t i;
3735         if (drv->filter_ssids == NULL)
3736                 return 0;
3738         ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
3739         if (ssid == NULL)
3740                 return 1;
3742         for (i = 0; i < drv->num_filter_ssids; i++) {
3743                 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
3744                     os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
3745                     0)
3746                         return 0;
3747         }
3749         return 1;
3753 static int bss_info_handler(struct nl_msg *msg, void *arg)
3755         struct nlattr *tb[NL80211_ATTR_MAX + 1];
3756         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3757         struct nlattr *bss[NL80211_BSS_MAX + 1];
3758         static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
3759                 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
3760                 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
3761                 [NL80211_BSS_TSF] = { .type = NLA_U64 },
3762                 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
3763                 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
3764                 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
3765                 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
3766                 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
3767                 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
3768                 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
3769                 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
3770         };
3771         struct nl80211_bss_info_arg *_arg = arg;
3772         struct wpa_scan_results *res = _arg->res;
3773         struct wpa_scan_res **tmp;
3774         struct wpa_scan_res *r;
3775         const u8 *ie, *beacon_ie;
3776         size_t ie_len, beacon_ie_len;
3777         u8 *pos;
3778         size_t i;
3780         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3781                   genlmsg_attrlen(gnlh, 0), NULL);
3782         if (!tb[NL80211_ATTR_BSS])
3783                 return NL_SKIP;
3784         if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
3785                              bss_policy))
3786                 return NL_SKIP;
3787         if (bss[NL80211_BSS_STATUS]) {
3788                 enum nl80211_bss_status status;
3789                 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
3790                 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
3791                     bss[NL80211_BSS_FREQUENCY]) {
3792                         _arg->assoc_freq =
3793                                 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
3794                         wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
3795                                    _arg->assoc_freq);
3796                 }
3797                 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
3798                     bss[NL80211_BSS_BSSID]) {
3799                         os_memcpy(_arg->assoc_bssid,
3800                                   nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
3801                         wpa_printf(MSG_DEBUG, "nl80211: Associated with "
3802                                    MACSTR, MAC2STR(_arg->assoc_bssid));
3803                 }
3804         }
3805         if (!res)
3806                 return NL_SKIP;
3807         if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
3808                 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
3809                 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
3810         } else {
3811                 ie = NULL;
3812                 ie_len = 0;
3813         }
3814         if (bss[NL80211_BSS_BEACON_IES]) {
3815                 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
3816                 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
3817         } else {
3818                 beacon_ie = NULL;
3819                 beacon_ie_len = 0;
3820         }
3822         if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
3823                                   ie ? ie_len : beacon_ie_len))
3824                 return NL_SKIP;
3826         r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
3827         if (r == NULL)
3828                 return NL_SKIP;
3829         if (bss[NL80211_BSS_BSSID])
3830                 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
3831                           ETH_ALEN);
3832         if (bss[NL80211_BSS_FREQUENCY])
3833                 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
3834         if (bss[NL80211_BSS_BEACON_INTERVAL])
3835                 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
3836         if (bss[NL80211_BSS_CAPABILITY])
3837                 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
3838         r->flags |= WPA_SCAN_NOISE_INVALID;
3839         if (bss[NL80211_BSS_SIGNAL_MBM]) {
3840                 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
3841                 r->level /= 100; /* mBm to dBm */
3842                 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
3843         } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
3844                 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
3845                 r->flags |= WPA_SCAN_QUAL_INVALID;
3846         } else
3847                 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
3848         if (bss[NL80211_BSS_TSF])
3849                 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
3850         if (bss[NL80211_BSS_SEEN_MS_AGO])
3851                 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
3852         r->ie_len = ie_len;
3853         pos = (u8 *) (r + 1);
3854         if (ie) {
3855                 os_memcpy(pos, ie, ie_len);
3856                 pos += ie_len;
3857         }
3858         r->beacon_ie_len = beacon_ie_len;
3859         if (beacon_ie)
3860                 os_memcpy(pos, beacon_ie, beacon_ie_len);
3862         if (bss[NL80211_BSS_STATUS]) {
3863                 enum nl80211_bss_status status;
3864                 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
3865                 switch (status) {
3866                 case NL80211_BSS_STATUS_AUTHENTICATED:
3867                         r->flags |= WPA_SCAN_AUTHENTICATED;
3868                         break;
3869                 case NL80211_BSS_STATUS_ASSOCIATED:
3870                         r->flags |= WPA_SCAN_ASSOCIATED;
3871                         break;
3872                 default:
3873                         break;
3874                 }
3875         }
3877         /*
3878          * cfg80211 maintains separate BSS table entries for APs if the same
3879          * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
3880          * not use frequency as a separate key in the BSS table, so filter out
3881          * duplicated entries. Prefer associated BSS entry in such a case in
3882          * order to get the correct frequency into the BSS table.
3883          */
3884         for (i = 0; i < res->num; i++) {
3885                 const u8 *s1, *s2;
3886                 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
3887                         continue;
3889                 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
3890                                     res->res[i]->ie_len, WLAN_EID_SSID);
3891                 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
3892                 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
3893                     os_memcmp(s1, s2, 2 + s1[1]) != 0)
3894                         continue;
3896                 /* Same BSSID,SSID was already included in scan results */
3897                 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
3898                            "for " MACSTR, MAC2STR(r->bssid));
3900                 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
3901                     !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
3902                         os_free(res->res[i]);
3903                         res->res[i] = r;
3904                 } else
3905                         os_free(r);
3906                 return NL_SKIP;
3907         }
3909         tmp = os_realloc(res->res,
3910                          (res->num + 1) * sizeof(struct wpa_scan_res *));
3911         if (tmp == NULL) {
3912                 os_free(r);
3913                 return NL_SKIP;
3914         }
3915         tmp[res->num++] = r;
3916         res->res = tmp;
3918         return NL_SKIP;
3922 static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
3923                                  const u8 *addr)
3925         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
3926                 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
3927                            "mismatch (" MACSTR ")", MAC2STR(addr));
3928                 wpa_driver_nl80211_mlme(drv, addr,
3929                                         NL80211_CMD_DEAUTHENTICATE,
3930                                         WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
3931         }
3935 static void wpa_driver_nl80211_check_bss_status(
3936         struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
3938         size_t i;
3940         for (i = 0; i < res->num; i++) {
3941                 struct wpa_scan_res *r = res->res[i];
3942                 if (r->flags & WPA_SCAN_AUTHENTICATED) {
3943                         wpa_printf(MSG_DEBUG, "nl80211: Scan results "
3944                                    "indicates BSS status with " MACSTR
3945                                    " as authenticated",
3946                                    MAC2STR(r->bssid));
3947                         if (is_sta_interface(drv->nlmode) &&
3948                             os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
3949                             os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
3950                             0) {
3951                                 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
3952                                            " in local state (auth=" MACSTR
3953                                            " assoc=" MACSTR ")",
3954                                            MAC2STR(drv->auth_bssid),
3955                                            MAC2STR(drv->bssid));
3956                                 clear_state_mismatch(drv, r->bssid);
3957                         }
3958                 }
3960                 if (r->flags & WPA_SCAN_ASSOCIATED) {
3961                         wpa_printf(MSG_DEBUG, "nl80211: Scan results "
3962                                    "indicate BSS status with " MACSTR
3963                                    " as associated",
3964                                    MAC2STR(r->bssid));
3965                         if (is_sta_interface(drv->nlmode) &&
3966                             !drv->associated) {
3967                                 wpa_printf(MSG_DEBUG, "nl80211: Local state "
3968                                            "(not associated) does not match "
3969                                            "with BSS state");
3970                                 clear_state_mismatch(drv, r->bssid);
3971                         } else if (is_sta_interface(drv->nlmode) &&
3972                                    os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
3973                                    0) {
3974                                 wpa_printf(MSG_DEBUG, "nl80211: Local state "
3975                                            "(associated with " MACSTR ") does "
3976                                            "not match with BSS state",
3977                                            MAC2STR(drv->bssid));
3978                                 clear_state_mismatch(drv, r->bssid);
3979                                 clear_state_mismatch(drv, drv->bssid);
3980                         }
3981                 }
3982         }
3986 static struct wpa_scan_results *
3987 nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
3989         struct nl_msg *msg;
3990         struct wpa_scan_results *res;
3991         int ret;
3992         struct nl80211_bss_info_arg arg;
3994         res = os_zalloc(sizeof(*res));
3995         if (res == NULL)
3996                 return NULL;
3997         msg = nlmsg_alloc();
3998         if (!msg)
3999                 goto nla_put_failure;
4001         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
4002         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4004         arg.drv = drv;
4005         arg.res = res;
4006         ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
4007         msg = NULL;
4008         if (ret == 0) {
4009                 wpa_printf(MSG_DEBUG, "nl80211: Received scan results (%lu "
4010                            "BSSes)", (unsigned long) res->num);
4011                 nl80211_get_noise_for_scan_results(drv, res);
4012                 return res;
4013         }
4014         wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
4015                    "(%s)", ret, strerror(-ret));
4016 nla_put_failure:
4017         nlmsg_free(msg);
4018         wpa_scan_results_free(res);
4019         return NULL;
4023 /**
4024  * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
4025  * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
4026  * Returns: Scan results on success, -1 on failure
4027  */
4028 static struct wpa_scan_results *
4029 wpa_driver_nl80211_get_scan_results(void *priv)
4031         struct i802_bss *bss = priv;
4032         struct wpa_driver_nl80211_data *drv = bss->drv;
4033         struct wpa_scan_results *res;
4035         res = nl80211_get_scan_results(drv);
4036         if (res)
4037                 wpa_driver_nl80211_check_bss_status(drv, res);
4038         return res;
4042 static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
4044         struct wpa_scan_results *res;
4045         size_t i;
4047         res = nl80211_get_scan_results(drv);
4048         if (res == NULL) {
4049                 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
4050                 return;
4051         }
4053         wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
4054         for (i = 0; i < res->num; i++) {
4055                 struct wpa_scan_res *r = res->res[i];
4056                 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
4057                            (int) i, (int) res->num, MAC2STR(r->bssid),
4058                            r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
4059                            r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
4060         }
4062         wpa_scan_results_free(res);
4066 static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
4067                                       enum wpa_alg alg, const u8 *addr,
4068                                       int key_idx, int set_tx,
4069                                       const u8 *seq, size_t seq_len,
4070                                       const u8 *key, size_t key_len)
4072         struct i802_bss *bss = priv;
4073         struct wpa_driver_nl80211_data *drv = bss->drv;
4074         int ifindex = if_nametoindex(ifname);
4075         struct nl_msg *msg;
4076         int ret;
4078         wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
4079                    "set_tx=%d seq_len=%lu key_len=%lu",
4080                    __func__, ifindex, alg, addr, key_idx, set_tx,
4081                    (unsigned long) seq_len, (unsigned long) key_len);
4082 #ifdef CONFIG_TDLS
4083         if (key_idx == -1)
4084                 key_idx = 0;
4085 #endif /* CONFIG_TDLS */
4087         msg = nlmsg_alloc();
4088         if (!msg)
4089                 return -ENOMEM;
4091         if (alg == WPA_ALG_NONE) {
4092                 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
4093         } else {
4094                 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
4095                 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
4096                 switch (alg) {
4097                 case WPA_ALG_WEP:
4098                         if (key_len == 5)
4099                                 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4100                                             WLAN_CIPHER_SUITE_WEP40);
4101                         else
4102                                 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4103                                             WLAN_CIPHER_SUITE_WEP104);
4104                         break;
4105                 case WPA_ALG_TKIP:
4106                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4107                                     WLAN_CIPHER_SUITE_TKIP);
4108                         break;
4109                 case WPA_ALG_CCMP:
4110                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4111                                     WLAN_CIPHER_SUITE_CCMP);
4112                         break;
4113                 case WPA_ALG_IGTK:
4114                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
4115                                     WLAN_CIPHER_SUITE_AES_CMAC);
4116                         break;
4117                 default:
4118                         wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4119                                    "algorithm %d", __func__, alg);
4120                         nlmsg_free(msg);
4121                         return -1;
4122                 }
4123         }
4125         if (seq && seq_len)
4126                 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
4128         if (addr && !is_broadcast_ether_addr(addr)) {
4129                 wpa_printf(MSG_DEBUG, "   addr=" MACSTR, MAC2STR(addr));
4130                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4132                 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
4133                         wpa_printf(MSG_DEBUG, "   RSN IBSS RX GTK");
4134                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
4135                                     NL80211_KEYTYPE_GROUP);
4136                 }
4137         } else if (addr && is_broadcast_ether_addr(addr)) {
4138                 struct nl_msg *types;
4139                 int err;
4140                 wpa_printf(MSG_DEBUG, "   broadcast key");
4141                 types = nlmsg_alloc();
4142                 if (!types)
4143                         goto nla_put_failure;
4144                 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4145                 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4146                                      types);
4147                 nlmsg_free(types);
4148                 if (err)
4149                         goto nla_put_failure;
4150         }
4151         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4152         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4154         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4155         if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
4156                 ret = 0;
4157         if (ret)
4158                 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
4159                            ret, strerror(-ret));
4161         /*
4162          * If we failed or don't need to set the default TX key (below),
4163          * we're done here.
4164          */
4165         if (ret || !set_tx || alg == WPA_ALG_NONE)
4166                 return ret;
4167         if (is_ap_interface(drv->nlmode) && addr &&
4168             !is_broadcast_ether_addr(addr))
4169                 return ret;
4171         msg = nlmsg_alloc();
4172         if (!msg)
4173                 return -ENOMEM;
4175         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
4176         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
4177         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4178         if (alg == WPA_ALG_IGTK)
4179                 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
4180         else
4181                 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
4182         if (addr && is_broadcast_ether_addr(addr)) {
4183                 struct nl_msg *types;
4184                 int err;
4185                 types = nlmsg_alloc();
4186                 if (!types)
4187                         goto nla_put_failure;
4188                 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
4189                 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4190                                      types);
4191                 nlmsg_free(types);
4192                 if (err)
4193                         goto nla_put_failure;
4194         } else if (addr) {
4195                 struct nl_msg *types;
4196                 int err;
4197                 types = nlmsg_alloc();
4198                 if (!types)
4199                         goto nla_put_failure;
4200                 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST);
4201                 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
4202                                      types);
4203                 nlmsg_free(types);
4204                 if (err)
4205                         goto nla_put_failure;
4206         }
4208         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4209         if (ret == -ENOENT)
4210                 ret = 0;
4211         if (ret)
4212                 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
4213                            "err=%d %s)", ret, strerror(-ret));
4214         return ret;
4216 nla_put_failure:
4217         nlmsg_free(msg);
4218         return -ENOBUFS;
4222 static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
4223                       int key_idx, int defkey,
4224                       const u8 *seq, size_t seq_len,
4225                       const u8 *key, size_t key_len)
4227         struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
4228         if (!key_attr)
4229                 return -1;
4231         if (defkey && alg == WPA_ALG_IGTK)
4232                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
4233         else if (defkey)
4234                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4236         NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
4238         switch (alg) {
4239         case WPA_ALG_WEP:
4240                 if (key_len == 5)
4241                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4242                                     WLAN_CIPHER_SUITE_WEP40);
4243                 else
4244                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4245                                     WLAN_CIPHER_SUITE_WEP104);
4246                 break;
4247         case WPA_ALG_TKIP:
4248                 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
4249                 break;
4250         case WPA_ALG_CCMP:
4251                 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
4252                 break;
4253         case WPA_ALG_IGTK:
4254                 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4255                             WLAN_CIPHER_SUITE_AES_CMAC);
4256                 break;
4257         default:
4258                 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
4259                            "algorithm %d", __func__, alg);
4260                 return -1;
4261         }
4263         if (seq && seq_len)
4264                 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
4266         NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
4268         nla_nest_end(msg, key_attr);
4270         return 0;
4271  nla_put_failure:
4272         return -1;
4276 static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
4277                                  struct nl_msg *msg)
4279         int i, privacy = 0;
4280         struct nlattr *nl_keys, *nl_key;
4282         for (i = 0; i < 4; i++) {
4283                 if (!params->wep_key[i])
4284                         continue;
4285                 privacy = 1;
4286                 break;
4287         }
4288         if (params->wps == WPS_MODE_PRIVACY)
4289                 privacy = 1;
4290         if (params->pairwise_suite &&
4291             params->pairwise_suite != WPA_CIPHER_NONE)
4292                 privacy = 1;
4294         if (!privacy)
4295                 return 0;
4297         NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
4299         nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
4300         if (!nl_keys)
4301                 goto nla_put_failure;
4303         for (i = 0; i < 4; i++) {
4304                 if (!params->wep_key[i])
4305                         continue;
4307                 nl_key = nla_nest_start(msg, i);
4308                 if (!nl_key)
4309                         goto nla_put_failure;
4311                 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
4312                         params->wep_key[i]);
4313                 if (params->wep_key_len[i] == 5)
4314                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4315                                     WLAN_CIPHER_SUITE_WEP40);
4316                 else
4317                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
4318                                     WLAN_CIPHER_SUITE_WEP104);
4320                 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
4322                 if (i == params->wep_tx_keyidx)
4323                         NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
4325                 nla_nest_end(msg, nl_key);
4326         }
4327         nla_nest_end(msg, nl_keys);
4329         return 0;
4331 nla_put_failure:
4332         return -ENOBUFS;
4336 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
4337                                    const u8 *addr, int cmd, u16 reason_code,
4338                                    int local_state_change)
4340         int ret = -1;
4341         struct nl_msg *msg;
4343         msg = nlmsg_alloc();
4344         if (!msg)
4345                 return -1;
4347         nl80211_cmd(drv, msg, 0, cmd);
4349         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4350         NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
4351         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4352         if (local_state_change)
4353                 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
4355         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4356         msg = NULL;
4357         if (ret) {
4358                 wpa_dbg(drv->ctx, MSG_DEBUG,
4359                         "nl80211: MLME command failed: reason=%u ret=%d (%s)",
4360                         reason_code, ret, strerror(-ret));
4361                 goto nla_put_failure;
4362         }
4363         ret = 0;
4365 nla_put_failure:
4366         nlmsg_free(msg);
4367         return ret;
4371 static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
4372                                          const u8 *addr, int reason_code)
4374         wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
4375                    __func__, MAC2STR(addr), reason_code);
4376         drv->associated = 0;
4377         return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT,
4378                                        reason_code, 0);
4382 static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
4383                                              int reason_code)
4385         struct i802_bss *bss = priv;
4386         struct wpa_driver_nl80211_data *drv = bss->drv;
4387         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
4388                 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
4389         wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
4390                    __func__, MAC2STR(addr), reason_code);
4391         drv->associated = 0;
4392         if (drv->nlmode == NL80211_IFTYPE_ADHOC)
4393                 return nl80211_leave_ibss(drv);
4394         return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
4395                                        reason_code, 0);
4399 static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
4400                                            int reason_code)
4402         struct i802_bss *bss = priv;
4403         struct wpa_driver_nl80211_data *drv = bss->drv;
4404         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
4405                 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
4406         wpa_printf(MSG_DEBUG, "%s", __func__);
4407         drv->associated = 0;
4408         return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
4409                                        reason_code, 0);
4413 static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
4414                                      struct wpa_driver_auth_params *params)
4416         int i;
4418         drv->auth_freq = params->freq;
4419         drv->auth_alg = params->auth_alg;
4420         drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
4421         drv->auth_local_state_change = params->local_state_change;
4422         drv->auth_p2p = params->p2p;
4424         if (params->bssid)
4425                 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
4426         else
4427                 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
4429         if (params->ssid) {
4430                 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
4431                 drv->auth_ssid_len = params->ssid_len;
4432         } else
4433                 drv->auth_ssid_len = 0;
4436         os_free(drv->auth_ie);
4437         drv->auth_ie = NULL;
4438         drv->auth_ie_len = 0;
4439         if (params->ie) {
4440                 drv->auth_ie = os_malloc(params->ie_len);
4441                 if (drv->auth_ie) {
4442                         os_memcpy(drv->auth_ie, params->ie, params->ie_len);
4443                         drv->auth_ie_len = params->ie_len;
4444                 }
4445         }
4447         for (i = 0; i < 4; i++) {
4448                 if (params->wep_key[i] && params->wep_key_len[i] &&
4449                     params->wep_key_len[i] <= 16) {
4450                         os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
4451                                   params->wep_key_len[i]);
4452                         drv->auth_wep_key_len[i] = params->wep_key_len[i];
4453                 } else
4454                         drv->auth_wep_key_len[i] = 0;
4455         }
4459 static int wpa_driver_nl80211_authenticate(
4460         void *priv, struct wpa_driver_auth_params *params)
4462         struct i802_bss *bss = priv;
4463         struct wpa_driver_nl80211_data *drv = bss->drv;
4464         int ret = -1, i;
4465         struct nl_msg *msg;
4466         enum nl80211_auth_type type;
4467         enum nl80211_iftype nlmode;
4468         int count = 0;
4469         int is_retry;
4471         is_retry = drv->retry_auth;
4472         drv->retry_auth = 0;
4474         drv->associated = 0;
4475         os_memset(drv->auth_bssid, 0, ETH_ALEN);
4476         /* FIX: IBSS mode */
4477         nlmode = params->p2p ?
4478                 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4479         if (drv->nlmode != nlmode &&
4480             wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
4481                 return -1;
4483 retry:
4484         msg = nlmsg_alloc();
4485         if (!msg)
4486                 return -1;
4488         wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
4489                    drv->ifindex);
4491         nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
4493         for (i = 0; i < 4; i++) {
4494                 if (!params->wep_key[i])
4495                         continue;
4496                 wpa_driver_nl80211_set_key(bss->ifname, priv, WPA_ALG_WEP,
4497                                            NULL, i,
4498                                            i == params->wep_tx_keyidx, NULL, 0,
4499                                            params->wep_key[i],
4500                                            params->wep_key_len[i]);
4501                 if (params->wep_tx_keyidx != i)
4502                         continue;
4503                 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
4504                                params->wep_key[i], params->wep_key_len[i])) {
4505                         nlmsg_free(msg);
4506                         return -1;
4507                 }
4508         }
4510         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4511         if (params->bssid) {
4512                 wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
4513                            MAC2STR(params->bssid));
4514                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
4515         }
4516         if (params->freq) {
4517                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
4518                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
4519         }
4520         if (params->ssid) {
4521                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
4522                                   params->ssid, params->ssid_len);
4523                 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
4524                         params->ssid);
4525         }
4526         wpa_hexdump(MSG_DEBUG, "  * IEs", params->ie, params->ie_len);
4527         if (params->ie)
4528                 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
4529         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4530                 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4531         else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4532                 type = NL80211_AUTHTYPE_SHARED_KEY;
4533         else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4534                 type = NL80211_AUTHTYPE_NETWORK_EAP;
4535         else if (params->auth_alg & WPA_AUTH_ALG_FT)
4536                 type = NL80211_AUTHTYPE_FT;
4537         else
4538                 goto nla_put_failure;
4539         wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
4540         NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
4541         if (params->local_state_change) {
4542                 wpa_printf(MSG_DEBUG, "  * Local state change only");
4543                 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
4544         }
4546         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4547         msg = NULL;
4548         if (ret) {
4549                 wpa_dbg(drv->ctx, MSG_DEBUG,
4550                         "nl80211: MLME command failed (auth): ret=%d (%s)",
4551                         ret, strerror(-ret));
4552                 count++;
4553                 if (ret == -EALREADY && count == 1 && params->bssid &&
4554                     !params->local_state_change) {
4555                         /*
4556                          * mac80211 does not currently accept new
4557                          * authentication if we are already authenticated. As a
4558                          * workaround, force deauthentication and try again.
4559                          */
4560                         wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
4561                                    "after forced deauthentication");
4562                         wpa_driver_nl80211_deauthenticate(
4563                                 bss, params->bssid,
4564                                 WLAN_REASON_PREV_AUTH_NOT_VALID);
4565                         nlmsg_free(msg);
4566                         goto retry;
4567                 }
4569                 if (ret == -ENOENT && params->freq && !is_retry) {
4570                         /*
4571                          * cfg80211 has likely expired the BSS entry even
4572                          * though it was previously available in our internal
4573                          * BSS table. To recover quickly, start a single
4574                          * channel scan on the specified channel.
4575                          */
4576                         struct wpa_driver_scan_params scan;
4577                         int freqs[2];
4579                         os_memset(&scan, 0, sizeof(scan));
4580                         scan.num_ssids = 1;
4581                         if (params->ssid) {
4582                                 scan.ssids[0].ssid = params->ssid;
4583                                 scan.ssids[0].ssid_len = params->ssid_len;
4584                         }
4585                         freqs[0] = params->freq;
4586                         freqs[1] = 0;
4587                         scan.freqs = freqs;
4588                         wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
4589                                    "channel scan to refresh cfg80211 BSS "
4590                                    "entry");
4591                         ret = wpa_driver_nl80211_scan(bss, &scan);
4592                         if (ret == 0) {
4593                                 nl80211_copy_auth_params(drv, params);
4594                                 drv->scan_for_auth = 1;
4595                         }
4596                 } else if (is_retry) {
4597                         /*
4598                          * Need to indicate this with an event since the return
4599                          * value from the retry is not delivered to core code.
4600                          */
4601                         union wpa_event_data event;
4602                         wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
4603                                    "failed");
4604                         os_memset(&event, 0, sizeof(event));
4605                         os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
4606                                   ETH_ALEN);
4607                         wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
4608                                              &event);
4609                 }
4611                 goto nla_put_failure;
4612         }
4613         ret = 0;
4614         wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
4615                    "successfully");
4617 nla_put_failure:
4618         nlmsg_free(msg);
4619         return ret;
4623 static int wpa_driver_nl80211_authenticate_retry(
4624         struct wpa_driver_nl80211_data *drv)
4626         struct wpa_driver_auth_params params;
4627         struct i802_bss *bss = &drv->first_bss;
4628         int i;
4630         wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
4632         os_memset(&params, 0, sizeof(params));
4633         params.freq = drv->auth_freq;
4634         params.auth_alg = drv->auth_alg;
4635         params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
4636         params.local_state_change = drv->auth_local_state_change;
4637         params.p2p = drv->auth_p2p;
4639         if (!is_zero_ether_addr(drv->auth_bssid_))
4640                 params.bssid = drv->auth_bssid_;
4642         if (drv->auth_ssid_len) {
4643                 params.ssid = drv->auth_ssid;
4644                 params.ssid_len = drv->auth_ssid_len;
4645         }
4647         params.ie = drv->auth_ie;
4648         params.ie_len = drv->auth_ie_len;
4650         for (i = 0; i < 4; i++) {
4651                 if (drv->auth_wep_key_len[i]) {
4652                         params.wep_key[i] = drv->auth_wep_key[i];
4653                         params.wep_key_len[i] = drv->auth_wep_key_len[i];
4654                 }
4655         }
4657         drv->retry_auth = 1;
4658         return wpa_driver_nl80211_authenticate(bss, &params);
4662 struct phy_info_arg {
4663         u16 *num_modes;
4664         struct hostapd_hw_modes *modes;
4665 };
4667 static int phy_info_handler(struct nl_msg *msg, void *arg)
4669         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
4670         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4671         struct phy_info_arg *phy_info = arg;
4673         struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
4675         struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
4676         static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
4677                 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
4678                 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
4679                 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
4680                 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
4681                 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
4682                 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
4683         };
4685         struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
4686         static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
4687                 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
4688                 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
4689         };
4691         struct nlattr *nl_band;
4692         struct nlattr *nl_freq;
4693         struct nlattr *nl_rate;
4694         int rem_band, rem_freq, rem_rate;
4695         struct hostapd_hw_modes *mode;
4696         int idx, mode_is_set;
4698         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4699                   genlmsg_attrlen(gnlh, 0), NULL);
4701         if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
4702                 return NL_SKIP;
4704         nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
4705                 mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
4706                 if (!mode)
4707                         return NL_SKIP;
4708                 phy_info->modes = mode;
4710                 mode_is_set = 0;
4712                 mode = &phy_info->modes[*(phy_info->num_modes)];
4713                 memset(mode, 0, sizeof(*mode));
4714                 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN;
4715                 *(phy_info->num_modes) += 1;
4717                 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
4718                           nla_len(nl_band), NULL);
4720                 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
4721                         mode->ht_capab = nla_get_u16(
4722                                 tb_band[NL80211_BAND_ATTR_HT_CAPA]);
4723                 }
4725                 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
4726                         mode->a_mpdu_params |= nla_get_u8(
4727                                 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
4728                                 0x03;
4729                 }
4731                 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
4732                         mode->a_mpdu_params |= nla_get_u8(
4733                                 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
4734                                 2;
4735                 }
4737                 if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
4738                     nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
4739                         u8 *mcs;
4740                         mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
4741                         os_memcpy(mode->mcs_set, mcs, 16);
4742                 }
4744                 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
4745                         nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
4746                                   nla_len(nl_freq), freq_policy);
4747                         if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
4748                                 continue;
4749                         mode->num_channels++;
4750                 }
4752                 mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data));
4753                 if (!mode->channels)
4754                         return NL_SKIP;
4756                 idx = 0;
4758                 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
4759                         nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
4760                                   nla_len(nl_freq), freq_policy);
4761                         if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
4762                                 continue;
4764                         mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
4765                         mode->channels[idx].flag = 0;
4767                         if (!mode_is_set) {
4768                                 /* crude heuristic */
4769                                 if (mode->channels[idx].freq < 4000)
4770                                         mode->mode = HOSTAPD_MODE_IEEE80211B;
4771                                 else
4772                                         mode->mode = HOSTAPD_MODE_IEEE80211A;
4773                                 mode_is_set = 1;
4774                         }
4776                         /* crude heuristic */
4777                         if (mode->channels[idx].freq < 4000)
4778                                 if (mode->channels[idx].freq == 2484)
4779                                         mode->channels[idx].chan = 14;
4780                                 else
4781                                         mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
4782                         else
4783                                 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
4785                         if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
4786                                 mode->channels[idx].flag |=
4787                                         HOSTAPD_CHAN_DISABLED;
4788                         if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
4789                                 mode->channels[idx].flag |=
4790                                         HOSTAPD_CHAN_PASSIVE_SCAN;
4791                         if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
4792                                 mode->channels[idx].flag |=
4793                                         HOSTAPD_CHAN_NO_IBSS;
4794                         if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
4795                                 mode->channels[idx].flag |=
4796                                         HOSTAPD_CHAN_RADAR;
4798                         if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
4799                             !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
4800                                 mode->channels[idx].max_tx_power =
4801                                         nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
4803                         idx++;
4804                 }
4806                 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
4807                         nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
4808                                   nla_len(nl_rate), rate_policy);
4809                         if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
4810                                 continue;
4811                         mode->num_rates++;
4812                 }
4814                 mode->rates = os_zalloc(mode->num_rates * sizeof(int));
4815                 if (!mode->rates)
4816                         return NL_SKIP;
4818                 idx = 0;
4820                 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
4821                         nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
4822                                   nla_len(nl_rate), rate_policy);
4823                         if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
4824                                 continue;
4825                         mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
4827                         /* crude heuristic */
4828                         if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
4829                             mode->rates[idx] > 200)
4830                                 mode->mode = HOSTAPD_MODE_IEEE80211G;
4832                         idx++;
4833                 }
4834         }
4836         return NL_SKIP;
4839 static struct hostapd_hw_modes *
4840 wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
4842         u16 m;
4843         struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
4844         int i, mode11g_idx = -1;
4846         /* If only 802.11g mode is included, use it to construct matching
4847          * 802.11b mode data. */
4849         for (m = 0; m < *num_modes; m++) {
4850                 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
4851                         return modes; /* 802.11b already included */
4852                 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
4853                         mode11g_idx = m;
4854         }
4856         if (mode11g_idx < 0)
4857                 return modes; /* 2.4 GHz band not supported at all */
4859         nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
4860         if (nmodes == NULL)
4861                 return modes; /* Could not add 802.11b mode */
4863         mode = &nmodes[*num_modes];
4864         os_memset(mode, 0, sizeof(*mode));
4865         (*num_modes)++;
4866         modes = nmodes;
4868         mode->mode = HOSTAPD_MODE_IEEE80211B;
4870         mode11g = &modes[mode11g_idx];
4871         mode->num_channels = mode11g->num_channels;
4872         mode->channels = os_malloc(mode11g->num_channels *
4873                                    sizeof(struct hostapd_channel_data));
4874         if (mode->channels == NULL) {
4875                 (*num_modes)--;
4876                 return modes; /* Could not add 802.11b mode */
4877         }
4878         os_memcpy(mode->channels, mode11g->channels,
4879                   mode11g->num_channels * sizeof(struct hostapd_channel_data));
4881         mode->num_rates = 0;
4882         mode->rates = os_malloc(4 * sizeof(int));
4883         if (mode->rates == NULL) {
4884                 os_free(mode->channels);
4885                 (*num_modes)--;
4886                 return modes; /* Could not add 802.11b mode */
4887         }
4889         for (i = 0; i < mode11g->num_rates; i++) {
4890                 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
4891                     mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
4892                         continue;
4893                 mode->rates[mode->num_rates] = mode11g->rates[i];
4894                 mode->num_rates++;
4895                 if (mode->num_rates == 4)
4896                         break;
4897         }
4899         if (mode->num_rates == 0) {
4900                 os_free(mode->channels);
4901                 os_free(mode->rates);
4902                 (*num_modes)--;
4903                 return modes; /* No 802.11b rates */
4904         }
4906         wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
4907                    "information");
4909         return modes;
4913 static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
4914                                   int end)
4916         int c;
4918         for (c = 0; c < mode->num_channels; c++) {
4919                 struct hostapd_channel_data *chan = &mode->channels[c];
4920                 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
4921                         chan->flag |= HOSTAPD_CHAN_HT40;
4922         }
4926 static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
4927                                       int end)
4929         int c;
4931         for (c = 0; c < mode->num_channels; c++) {
4932                 struct hostapd_channel_data *chan = &mode->channels[c];
4933                 if (!(chan->flag & HOSTAPD_CHAN_HT40))
4934                         continue;
4935                 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
4936                         chan->flag |= HOSTAPD_CHAN_HT40MINUS;
4937                 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
4938                         chan->flag |= HOSTAPD_CHAN_HT40PLUS;
4939         }
4943 static void nl80211_reg_rule_ht40(struct nlattr *tb[],
4944                                   struct phy_info_arg *results)
4946         u32 start, end, max_bw;
4947         u16 m;
4949         if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
4950             tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
4951             tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
4952                 return;
4954         start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
4955         end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
4956         max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
4958         wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
4959                    start, end, max_bw);
4960         if (max_bw < 40)
4961                 return;
4963         for (m = 0; m < *results->num_modes; m++) {
4964                 if (!(results->modes[m].ht_capab &
4965                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
4966                         continue;
4967                 nl80211_set_ht40_mode(&results->modes[m], start, end);
4968         }
4972 static void nl80211_reg_rule_sec(struct nlattr *tb[],
4973                                  struct phy_info_arg *results)
4975         u32 start, end, max_bw;
4976         u16 m;
4978         if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
4979             tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
4980             tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
4981                 return;
4983         start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
4984         end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
4985         max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
4987         if (max_bw < 20)
4988                 return;
4990         for (m = 0; m < *results->num_modes; m++) {
4991                 if (!(results->modes[m].ht_capab &
4992                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
4993                         continue;
4994                 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
4995         }
4999 static int nl80211_get_reg(struct nl_msg *msg, void *arg)
5001         struct phy_info_arg *results = arg;
5002         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
5003         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5004         struct nlattr *nl_rule;
5005         struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
5006         int rem_rule;
5007         static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
5008                 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
5009                 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
5010                 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
5011                 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
5012                 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
5013                 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
5014         };
5016         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5017                   genlmsg_attrlen(gnlh, 0), NULL);
5018         if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
5019             !tb_msg[NL80211_ATTR_REG_RULES]) {
5020                 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
5021                            "available");
5022                 return NL_SKIP;
5023         }
5025         wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
5026                    (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
5028         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5029         {
5030                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5031                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5032                 nl80211_reg_rule_ht40(tb_rule, results);
5033         }
5035         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
5036         {
5037                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
5038                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
5039                 nl80211_reg_rule_sec(tb_rule, results);
5040         }
5042         return NL_SKIP;
5046 static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
5047                                   struct phy_info_arg *results)
5049         struct nl_msg *msg;
5051         msg = nlmsg_alloc();
5052         if (!msg)
5053                 return -ENOMEM;
5055         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
5056         return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
5060 static struct hostapd_hw_modes *
5061 wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
5063         struct i802_bss *bss = priv;
5064         struct wpa_driver_nl80211_data *drv = bss->drv;
5065         struct nl_msg *msg;
5066         struct phy_info_arg result = {
5067                 .num_modes = num_modes,
5068                 .modes = NULL,
5069         };
5071         *num_modes = 0;
5072         *flags = 0;
5074         msg = nlmsg_alloc();
5075         if (!msg)
5076                 return NULL;
5078         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
5080         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5082         if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
5083                 nl80211_set_ht40_flags(drv, &result);
5084                 return wpa_driver_nl80211_add_11b(result.modes, num_modes);
5085         }
5086         msg = NULL;
5087  nla_put_failure:
5088         nlmsg_free(msg);
5089         return NULL;
5093 static int wpa_driver_nl80211_send_mntr(struct wpa_driver_nl80211_data *drv,
5094                                         const void *data, size_t len,
5095                                         int encrypt, int noack)
5097         __u8 rtap_hdr[] = {
5098                 0x00, 0x00, /* radiotap version */
5099                 0x0e, 0x00, /* radiotap length */
5100                 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
5101                 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
5102                 0x00,       /* padding */
5103                 0x00, 0x00, /* RX and TX flags to indicate that */
5104                 0x00, 0x00, /* this is the injected frame directly */
5105         };
5106         struct iovec iov[2] = {
5107                 {
5108                         .iov_base = &rtap_hdr,
5109                         .iov_len = sizeof(rtap_hdr),
5110                 },
5111                 {
5112                         .iov_base = (void *) data,
5113                         .iov_len = len,
5114                 }
5115         };
5116         struct msghdr msg = {
5117                 .msg_name = NULL,
5118                 .msg_namelen = 0,
5119                 .msg_iov = iov,
5120                 .msg_iovlen = 2,
5121                 .msg_control = NULL,
5122                 .msg_controllen = 0,
5123                 .msg_flags = 0,
5124         };
5125         int res;
5126         u16 txflags = 0;
5128         if (encrypt)
5129                 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
5131         if (drv->monitor_sock < 0) {
5132                 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
5133                            "for %s", __func__);
5134                 return -1;
5135         }
5137         if (noack)
5138                 txflags |= IEEE80211_RADIOTAP_F_TX_NOACK;
5139         *(le16 *) &rtap_hdr[12] = host_to_le16(txflags);
5141         res = sendmsg(drv->monitor_sock, &msg, 0);
5142         if (res < 0) {
5143                 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
5144                 return -1;
5145         }
5146         return 0;
5150 static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
5151                                          const void *data, size_t len,
5152                                          int encrypt, int noack,
5153                                          unsigned int freq, int no_cck,
5154                                          int offchanok, unsigned int wait_time)
5156         struct wpa_driver_nl80211_data *drv = bss->drv;
5157         u64 cookie;
5159         if (freq == 0)
5160                 freq = bss->freq;
5162         if (drv->use_monitor)
5163                 return wpa_driver_nl80211_send_mntr(drv, data, len,
5164                                                     encrypt, noack);
5166         return nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
5167                                       &cookie, no_cck, noack, offchanok);
5171 static int wpa_driver_nl80211_send_mlme_freq(struct i802_bss *bss,
5172                                              const u8 *data,
5173                                              size_t data_len, int noack,
5174                                              unsigned int freq, int no_cck,
5175                                              int offchanok,
5176                                              unsigned int wait_time)
5178         struct wpa_driver_nl80211_data *drv = bss->drv;
5179         struct ieee80211_mgmt *mgmt;
5180         int encrypt = 1;
5181         u16 fc;
5183         mgmt = (struct ieee80211_mgmt *) data;
5184         fc = le_to_host16(mgmt->frame_control);
5186         if (is_sta_interface(drv->nlmode) &&
5187             WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5188             WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
5189                 /*
5190                  * The use of last_mgmt_freq is a bit of a hack,
5191                  * but it works due to the single-threaded nature
5192                  * of wpa_supplicant.
5193                  */
5194                 if (freq == 0)
5195                         freq = drv->last_mgmt_freq;
5196                 return nl80211_send_frame_cmd(bss, freq, 0,
5197                                               data, data_len, NULL, 1, noack,
5198                                               1);
5199         }
5201         if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
5202                 if (freq == 0)
5203                         freq = bss->freq;
5204                 return nl80211_send_frame_cmd(bss, freq, 0,
5205                                               data, data_len,
5206                                               &drv->send_action_cookie,
5207                                               no_cck, noack, offchanok);
5208         }
5210         if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
5211             WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
5212                 /*
5213                  * Only one of the authentication frame types is encrypted.
5214                  * In order for static WEP encryption to work properly (i.e.,
5215                  * to not encrypt the frame), we need to tell mac80211 about
5216                  * the frames that must not be encrypted.
5217                  */
5218                 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
5219                 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
5220                 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
5221                         encrypt = 0;
5222         }
5224         return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
5225                                              noack, freq, no_cck, offchanok,
5226                                              wait_time);
5230 static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
5231                                         size_t data_len, int noack)
5233         struct i802_bss *bss = priv;
5234         return wpa_driver_nl80211_send_mlme_freq(bss, data, data_len, noack,
5235                                                  0, 0, 0, 0);
5239 static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
5240                            int slot, int ht_opmode, int ap_isolate,
5241                            int *basic_rates)
5243         struct wpa_driver_nl80211_data *drv = bss->drv;
5244         struct nl_msg *msg;
5246         msg = nlmsg_alloc();
5247         if (!msg)
5248                 return -ENOMEM;
5250         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
5252         if (cts >= 0)
5253                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
5254         if (preamble >= 0)
5255                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
5256         if (slot >= 0)
5257                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
5258         if (ht_opmode >= 0)
5259                 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
5260         if (ap_isolate >= 0)
5261                 NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate);
5263         if (basic_rates) {
5264                 u8 rates[NL80211_MAX_SUPP_RATES];
5265                 u8 rates_len = 0;
5266                 int i;
5268                 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
5269                      i++)
5270                         rates[rates_len++] = basic_rates[i] / 5;
5272                 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
5273         }
5275         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5277         return send_and_recv_msgs(drv, msg, NULL, NULL);
5278  nla_put_failure:
5279         nlmsg_free(msg);
5280         return -ENOBUFS;
5284 static int wpa_driver_nl80211_set_ap(void *priv,
5285                                      struct wpa_driver_ap_params *params)
5287         struct i802_bss *bss = priv;
5288         struct wpa_driver_nl80211_data *drv = bss->drv;
5289         struct nl_msg *msg;
5290         u8 cmd = NL80211_CMD_NEW_BEACON;
5291         int ret;
5292         int beacon_set;
5293         int ifindex = if_nametoindex(bss->ifname);
5294         int num_suites;
5295         u32 suites[10];
5296         u32 ver;
5298         beacon_set = bss->beacon_set;
5300         msg = nlmsg_alloc();
5301         if (!msg)
5302                 return -ENOMEM;
5304         wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
5305                    beacon_set);
5306         if (beacon_set)
5307                 cmd = NL80211_CMD_SET_BEACON;
5309         nl80211_cmd(drv, msg, 0, cmd);
5310         NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
5311         NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
5312         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5313         NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
5314         NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
5315         NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5316                 params->ssid);
5317         if (params->proberesp && params->proberesp_len)
5318                 NLA_PUT(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
5319                         params->proberesp);
5320         switch (params->hide_ssid) {
5321         case NO_SSID_HIDING:
5322                 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5323                             NL80211_HIDDEN_SSID_NOT_IN_USE);
5324                 break;
5325         case HIDDEN_SSID_ZERO_LEN:
5326                 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5327                             NL80211_HIDDEN_SSID_ZERO_LEN);
5328                 break;
5329         case HIDDEN_SSID_ZERO_CONTENTS:
5330                 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
5331                             NL80211_HIDDEN_SSID_ZERO_CONTENTS);
5332                 break;
5333         }
5334         if (params->privacy)
5335                 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
5336         if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
5337             (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
5338                 /* Leave out the attribute */
5339         } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
5340                 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5341                             NL80211_AUTHTYPE_SHARED_KEY);
5342         else
5343                 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
5344                             NL80211_AUTHTYPE_OPEN_SYSTEM);
5346         ver = 0;
5347         if (params->wpa_version & WPA_PROTO_WPA)
5348                 ver |= NL80211_WPA_VERSION_1;
5349         if (params->wpa_version & WPA_PROTO_RSN)
5350                 ver |= NL80211_WPA_VERSION_2;
5351         if (ver)
5352                 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
5354         num_suites = 0;
5355         if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
5356                 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
5357         if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
5358                 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
5359         if (num_suites) {
5360                 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
5361                         num_suites * sizeof(u32), suites);
5362         }
5364         if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
5365             params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40))
5366                 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT);
5368         num_suites = 0;
5369         if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
5370                 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
5371         if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
5372                 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
5373         if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
5374                 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
5375         if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
5376                 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
5377         if (num_suites) {
5378                 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5379                         num_suites * sizeof(u32), suites);
5380         }
5382         switch (params->group_cipher) {
5383         case WPA_CIPHER_CCMP:
5384                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5385                             WLAN_CIPHER_SUITE_CCMP);
5386                 break;
5387         case WPA_CIPHER_TKIP:
5388                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5389                             WLAN_CIPHER_SUITE_TKIP);
5390                 break;
5391         case WPA_CIPHER_WEP104:
5392                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5393                             WLAN_CIPHER_SUITE_WEP104);
5394                 break;
5395         case WPA_CIPHER_WEP40:
5396                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
5397                             WLAN_CIPHER_SUITE_WEP40);
5398                 break;
5399         }
5401         if (params->beacon_ies) {
5402                 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
5403                         wpabuf_head(params->beacon_ies));
5404         }
5405         if (params->proberesp_ies) {
5406                 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
5407                         wpabuf_len(params->proberesp_ies),
5408                         wpabuf_head(params->proberesp_ies));
5409         }
5410         if (params->assocresp_ies) {
5411                 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
5412                         wpabuf_len(params->assocresp_ies),
5413                         wpabuf_head(params->assocresp_ies));
5414         }
5416         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5417         if (ret) {
5418                 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
5419                            ret, strerror(-ret));
5420         } else {
5421                 bss->beacon_set = 1;
5422                 nl80211_set_bss(bss, params->cts_protect, params->preamble,
5423                                 params->short_slot_time, params->ht_opmode,
5424                                 params->isolate, params->basic_rates);
5425         }
5426         return ret;
5427  nla_put_failure:
5428         nlmsg_free(msg);
5429         return -ENOBUFS;
5433 static int wpa_driver_nl80211_set_freq(struct i802_bss *bss,
5434                                        int freq, int ht_enabled,
5435                                        int sec_channel_offset)
5437         struct wpa_driver_nl80211_data *drv = bss->drv;
5438         struct nl_msg *msg;
5439         int ret;
5441         wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d "
5442                    "sec_channel_offset=%d)",
5443                    freq, ht_enabled, sec_channel_offset);
5444         msg = nlmsg_alloc();
5445         if (!msg)
5446                 return -1;
5448         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
5450         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5451         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5452         if (ht_enabled) {
5453                 switch (sec_channel_offset) {
5454                 case -1:
5455                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5456                                     NL80211_CHAN_HT40MINUS);
5457                         break;
5458                 case 1:
5459                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5460                                     NL80211_CHAN_HT40PLUS);
5461                         break;
5462                 default:
5463                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
5464                                     NL80211_CHAN_HT20);
5465                         break;
5466                 }
5467         }
5469         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5470         msg = NULL;
5471         if (ret == 0) {
5472                 bss->freq = freq;
5473                 return 0;
5474         }
5475         wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
5476                    "%d (%s)", freq, ret, strerror(-ret));
5477 nla_put_failure:
5478         nlmsg_free(msg);
5479         return -1;
5483 static u32 sta_flags_nl80211(int flags)
5485         u32 f = 0;
5487         if (flags & WPA_STA_AUTHORIZED)
5488                 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
5489         if (flags & WPA_STA_WMM)
5490                 f |= BIT(NL80211_STA_FLAG_WME);
5491         if (flags & WPA_STA_SHORT_PREAMBLE)
5492                 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
5493         if (flags & WPA_STA_MFP)
5494                 f |= BIT(NL80211_STA_FLAG_MFP);
5495         if (flags & WPA_STA_TDLS_PEER)
5496                 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
5498         return f;
5502 static int wpa_driver_nl80211_sta_add(void *priv,
5503                                       struct hostapd_sta_add_params *params)
5505         struct i802_bss *bss = priv;
5506         struct wpa_driver_nl80211_data *drv = bss->drv;
5507         struct nl_msg *msg, *wme = NULL;
5508         struct nl80211_sta_flag_update upd;
5509         int ret = -ENOBUFS;
5511         if ((params->flags & WPA_STA_TDLS_PEER) &&
5512             !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
5513                 return -EOPNOTSUPP;
5515         msg = nlmsg_alloc();
5516         if (!msg)
5517                 return -ENOMEM;
5519         nl80211_cmd(drv, msg, 0, params->set ? NL80211_CMD_SET_STATION :
5520                     NL80211_CMD_NEW_STATION);
5522         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5523         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
5524         NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
5525                 params->supp_rates);
5526         if (!params->set) {
5527                 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
5528                 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
5529                             params->listen_interval);
5530         }
5531         if (params->ht_capabilities) {
5532                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
5533                         sizeof(*params->ht_capabilities),
5534                         params->ht_capabilities);
5535         }
5537         os_memset(&upd, 0, sizeof(upd));
5538         upd.mask = sta_flags_nl80211(params->flags);
5539         upd.set = upd.mask;
5540         NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
5542         if (params->flags & WPA_STA_WMM) {
5543                 wme = nlmsg_alloc();
5544                 if (!wme)
5545                         goto nla_put_failure;
5547                 NLA_PUT_U8(wme, NL80211_STA_WME_UAPSD_QUEUES,
5548                                 params->qosinfo & WMM_QOSINFO_STA_AC_MASK);
5549                 NLA_PUT_U8(wme, NL80211_STA_WME_MAX_SP,
5550                                 (params->qosinfo > WMM_QOSINFO_STA_SP_SHIFT) &
5551                                 WMM_QOSINFO_STA_SP_MASK);
5552                 nla_put_nested(msg, NL80211_ATTR_STA_WME, wme);
5553         }
5555         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5556         msg = NULL;
5557         if (ret)
5558                 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
5559                            "result: %d (%s)", params->set ? "SET" : "NEW", ret,
5560                            strerror(-ret));
5561         if (ret == -EEXIST)
5562                 ret = 0;
5563  nla_put_failure:
5564         nlmsg_free(wme);
5565         nlmsg_free(msg);
5566         return ret;
5570 static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
5572         struct i802_bss *bss = priv;
5573         struct wpa_driver_nl80211_data *drv = bss->drv;
5574         struct nl_msg *msg;
5575         int ret;
5577         msg = nlmsg_alloc();
5578         if (!msg)
5579                 return -ENOMEM;
5581         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
5583         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
5584                     if_nametoindex(bss->ifname));
5585         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5587         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5588         if (ret == -ENOENT)
5589                 return 0;
5590         return ret;
5591  nla_put_failure:
5592         nlmsg_free(msg);
5593         return -ENOBUFS;
5597 static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
5598                                  int ifidx)
5600         struct nl_msg *msg;
5602         wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
5604         /* stop listening for EAPOL on this interface */
5605         del_ifidx(drv, ifidx);
5607         msg = nlmsg_alloc();
5608         if (!msg)
5609                 goto nla_put_failure;
5611         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
5612         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
5614         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5615                 return;
5616         msg = NULL;
5617  nla_put_failure:
5618         nlmsg_free(msg);
5619         wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
5623 static const char * nl80211_iftype_str(enum nl80211_iftype mode)
5625         switch (mode) {
5626         case NL80211_IFTYPE_ADHOC:
5627                 return "ADHOC";
5628         case NL80211_IFTYPE_STATION:
5629                 return "STATION";
5630         case NL80211_IFTYPE_AP:
5631                 return "AP";
5632         case NL80211_IFTYPE_MONITOR:
5633                 return "MONITOR";
5634         case NL80211_IFTYPE_P2P_CLIENT:
5635                 return "P2P_CLIENT";
5636         case NL80211_IFTYPE_P2P_GO:
5637                 return "P2P_GO";
5638         default:
5639                 return "unknown";
5640         }
5644 static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
5645                                      const char *ifname,
5646                                      enum nl80211_iftype iftype,
5647                                      const u8 *addr, int wds)
5649         struct nl_msg *msg, *flags = NULL;
5650         int ifidx;
5651         int ret = -ENOBUFS;
5653         wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
5654                    iftype, nl80211_iftype_str(iftype));
5656         msg = nlmsg_alloc();
5657         if (!msg)
5658                 return -1;
5660         nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
5661         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5662         NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
5663         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
5665         if (iftype == NL80211_IFTYPE_MONITOR) {
5666                 int err;
5668                 flags = nlmsg_alloc();
5669                 if (!flags)
5670                         goto nla_put_failure;
5672                 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
5674                 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
5676                 nlmsg_free(flags);
5678                 if (err)
5679                         goto nla_put_failure;
5680         } else if (wds) {
5681                 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
5682         }
5684         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5685         msg = NULL;
5686         if (ret) {
5687  nla_put_failure:
5688                 nlmsg_free(msg);
5689                 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
5690                            ifname, ret, strerror(-ret));
5691                 return ret;
5692         }
5694         ifidx = if_nametoindex(ifname);
5695         wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
5696                    ifname, ifidx);
5698         if (ifidx <= 0)
5699                 return -1;
5701         /* start listening for EAPOL on this interface */
5702         add_ifidx(drv, ifidx);
5704         if (addr && iftype != NL80211_IFTYPE_MONITOR &&
5705             linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
5706                 nl80211_remove_iface(drv, ifidx);
5707                 return -1;
5708         }
5710         return ifidx;
5714 static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
5715                                 const char *ifname, enum nl80211_iftype iftype,
5716                                 const u8 *addr, int wds)
5718         int ret;
5720         ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
5722         /* if error occurred and interface exists already */
5723         if (ret == -ENFILE && if_nametoindex(ifname)) {
5724                 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
5726                 /* Try to remove the interface that was already there. */
5727                 nl80211_remove_iface(drv, if_nametoindex(ifname));
5729                 /* Try to create the interface again */
5730                 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
5731                                                 wds);
5732         }
5734         if (ret >= 0 && is_p2p_interface(iftype))
5735                 nl80211_disable_11b_rates(drv, ret, 1);
5737         return ret;
5741 static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
5743         struct ieee80211_hdr *hdr;
5744         u16 fc;
5745         union wpa_event_data event;
5747         hdr = (struct ieee80211_hdr *) buf;
5748         fc = le_to_host16(hdr->frame_control);
5750         os_memset(&event, 0, sizeof(event));
5751         event.tx_status.type = WLAN_FC_GET_TYPE(fc);
5752         event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
5753         event.tx_status.dst = hdr->addr1;
5754         event.tx_status.data = buf;
5755         event.tx_status.data_len = len;
5756         event.tx_status.ack = ok;
5757         wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
5761 static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
5762                              u8 *buf, size_t len)
5764         struct ieee80211_hdr *hdr = (void *)buf;
5765         u16 fc;
5766         union wpa_event_data event;
5768         if (len < sizeof(*hdr))
5769                 return;
5771         fc = le_to_host16(hdr->frame_control);
5773         os_memset(&event, 0, sizeof(event));
5774         event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
5775         event.rx_from_unknown.addr = hdr->addr2;
5776         event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
5777                 (WLAN_FC_FROMDS | WLAN_FC_TODS);
5778         wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
5782 static void handle_frame(struct wpa_driver_nl80211_data *drv,
5783                          u8 *buf, size_t len, int datarate, int ssi_signal)
5785         struct ieee80211_hdr *hdr;
5786         u16 fc;
5787         union wpa_event_data event;
5789         hdr = (struct ieee80211_hdr *) buf;
5790         fc = le_to_host16(hdr->frame_control);
5792         switch (WLAN_FC_GET_TYPE(fc)) {
5793         case WLAN_FC_TYPE_MGMT:
5794                 os_memset(&event, 0, sizeof(event));
5795                 event.rx_mgmt.frame = buf;
5796                 event.rx_mgmt.frame_len = len;
5797                 event.rx_mgmt.datarate = datarate;
5798                 event.rx_mgmt.ssi_signal = ssi_signal;
5799                 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
5800                 break;
5801         case WLAN_FC_TYPE_CTRL:
5802                 /* can only get here with PS-Poll frames */
5803                 wpa_printf(MSG_DEBUG, "CTRL");
5804                 from_unknown_sta(drv, buf, len);
5805                 break;
5806         case WLAN_FC_TYPE_DATA:
5807                 from_unknown_sta(drv, buf, len);
5808                 break;
5809         }
5813 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
5815         struct wpa_driver_nl80211_data *drv = eloop_ctx;
5816         int len;
5817         unsigned char buf[3000];
5818         struct ieee80211_radiotap_iterator iter;
5819         int ret;
5820         int datarate = 0, ssi_signal = 0;
5821         int injected = 0, failed = 0, rxflags = 0;
5823         len = recv(sock, buf, sizeof(buf), 0);
5824         if (len < 0) {
5825                 perror("recv");
5826                 return;
5827         }
5829         if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
5830                 printf("received invalid radiotap frame\n");
5831                 return;
5832         }
5834         while (1) {
5835                 ret = ieee80211_radiotap_iterator_next(&iter);
5836                 if (ret == -ENOENT)
5837                         break;
5838                 if (ret) {
5839                         printf("received invalid radiotap frame (%d)\n", ret);
5840                         return;
5841                 }
5842                 switch (iter.this_arg_index) {
5843                 case IEEE80211_RADIOTAP_FLAGS:
5844                         if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
5845                                 len -= 4;
5846                         break;
5847                 case IEEE80211_RADIOTAP_RX_FLAGS:
5848                         rxflags = 1;
5849                         break;
5850                 case IEEE80211_RADIOTAP_TX_FLAGS:
5851                         injected = 1;
5852                         failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
5853                                         IEEE80211_RADIOTAP_F_TX_FAIL;
5854                         break;
5855                 case IEEE80211_RADIOTAP_DATA_RETRIES:
5856                         break;
5857                 case IEEE80211_RADIOTAP_CHANNEL:
5858                         /* TODO: convert from freq/flags to channel number */
5859                         break;
5860                 case IEEE80211_RADIOTAP_RATE:
5861                         datarate = *iter.this_arg * 5;
5862                         break;
5863                 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
5864                         ssi_signal = *iter.this_arg;
5865                         break;
5866                 }
5867         }
5869         if (rxflags && injected)
5870                 return;
5872         if (!injected)
5873                 handle_frame(drv, buf + iter.max_length,
5874                              len - iter.max_length, datarate, ssi_signal);
5875         else
5876                 handle_tx_callback(drv->ctx, buf + iter.max_length,
5877                                    len - iter.max_length, !failed);
5881 /*
5882  * we post-process the filter code later and rewrite
5883  * this to the offset to the last instruction
5884  */
5885 #define PASS    0xFF
5886 #define FAIL    0xFE
5888 static struct sock_filter msock_filter_insns[] = {
5889         /*
5890          * do a little-endian load of the radiotap length field
5891          */
5892         /* load lower byte into A */
5893         BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 2),
5894         /* put it into X (== index register) */
5895         BPF_STMT(BPF_MISC| BPF_TAX, 0),
5896         /* load upper byte into A */
5897         BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 3),
5898         /* left-shift it by 8 */
5899         BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
5900         /* or with X */
5901         BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
5902         /* put result into X */
5903         BPF_STMT(BPF_MISC| BPF_TAX, 0),
5905         /*
5906          * Allow management frames through, this also gives us those
5907          * management frames that we sent ourselves with status
5908          */
5909         /* load the lower byte of the IEEE 802.11 frame control field */
5910         BPF_STMT(BPF_LD  | BPF_B | BPF_IND, 0),
5911         /* mask off frame type and version */
5912         BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
5913         /* accept frame if it's both 0, fall through otherwise */
5914         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
5916         /*
5917          * TODO: add a bit to radiotap RX flags that indicates
5918          * that the sending station is not associated, then
5919          * add a filter here that filters on our DA and that flag
5920          * to allow us to deauth frames to that bad station.
5921          *
5922          * For now allow all To DS data frames through.
5923          */
5924         /* load the IEEE 802.11 frame control field */
5925         BPF_STMT(BPF_LD  | BPF_H | BPF_IND, 0),
5926         /* mask off frame type, version and DS status */
5927         BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
5928         /* accept frame if version 0, type 2 and To DS, fall through otherwise
5929          */
5930         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
5932 #if 0
5933         /*
5934          * drop non-data frames
5935          */
5936         /* load the lower byte of the frame control field */
5937         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
5938         /* mask off QoS bit */
5939         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x0c),
5940         /* drop non-data frames */
5941         BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 8, 0, FAIL),
5942 #endif
5943         /* load the upper byte of the frame control field */
5944         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 1),
5945         /* mask off toDS/fromDS */
5946         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x03),
5947         /* accept WDS frames */
5948         BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 3, PASS, 0),
5950         /*
5951          * add header length to index
5952          */
5953         /* load the lower byte of the frame control field */
5954         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
5955         /* mask off QoS bit */
5956         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x80),
5957         /* right shift it by 6 to give 0 or 2 */
5958         BPF_STMT(BPF_ALU  | BPF_RSH | BPF_K, 6),
5959         /* add data frame header length */
5960         BPF_STMT(BPF_ALU  | BPF_ADD | BPF_K, 24),
5961         /* add index, was start of 802.11 header */
5962         BPF_STMT(BPF_ALU  | BPF_ADD | BPF_X, 0),
5963         /* move to index, now start of LL header */
5964         BPF_STMT(BPF_MISC | BPF_TAX, 0),
5966         /*
5967          * Accept empty data frames, we use those for
5968          * polling activity.
5969          */
5970         BPF_STMT(BPF_LD  | BPF_W | BPF_LEN, 0),
5971         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
5973         /*
5974          * Accept EAPOL frames
5975          */
5976         BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 0),
5977         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
5978         BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 4),
5979         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
5981         /* keep these last two statements or change the code below */
5982         /* return 0 == "DROP" */
5983         BPF_STMT(BPF_RET | BPF_K, 0),
5984         /* return ~0 == "keep all" */
5985         BPF_STMT(BPF_RET | BPF_K, ~0),
5986 };
5988 static struct sock_fprog msock_filter = {
5989         .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
5990         .filter = msock_filter_insns,
5991 };
5994 static int add_monitor_filter(int s)
5996         int idx;
5998         /* rewrite all PASS/FAIL jump offsets */
5999         for (idx = 0; idx < msock_filter.len; idx++) {
6000                 struct sock_filter *insn = &msock_filter_insns[idx];
6002                 if (BPF_CLASS(insn->code) == BPF_JMP) {
6003                         if (insn->code == (BPF_JMP|BPF_JA)) {
6004                                 if (insn->k == PASS)
6005                                         insn->k = msock_filter.len - idx - 2;
6006                                 else if (insn->k == FAIL)
6007                                         insn->k = msock_filter.len - idx - 3;
6008                         }
6010                         if (insn->jt == PASS)
6011                                 insn->jt = msock_filter.len - idx - 2;
6012                         else if (insn->jt == FAIL)
6013                                 insn->jt = msock_filter.len - idx - 3;
6015                         if (insn->jf == PASS)
6016                                 insn->jf = msock_filter.len - idx - 2;
6017                         else if (insn->jf == FAIL)
6018                                 insn->jf = msock_filter.len - idx - 3;
6019                 }
6020         }
6022         if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
6023                        &msock_filter, sizeof(msock_filter))) {
6024                 perror("SO_ATTACH_FILTER");
6025                 return -1;
6026         }
6028         return 0;
6032 static void nl80211_remove_monitor_interface(
6033         struct wpa_driver_nl80211_data *drv)
6035         drv->monitor_refcount--;
6036         if (drv->monitor_refcount > 0)
6037                 return;
6039         if (drv->monitor_ifidx >= 0) {
6040                 nl80211_remove_iface(drv, drv->monitor_ifidx);
6041                 drv->monitor_ifidx = -1;
6042         }
6043         if (drv->monitor_sock >= 0) {
6044                 eloop_unregister_read_sock(drv->monitor_sock);
6045                 close(drv->monitor_sock);
6046                 drv->monitor_sock = -1;
6047         }
6051 static int
6052 nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
6054         char buf[IFNAMSIZ];
6055         struct sockaddr_ll ll;
6056         int optval;
6057         socklen_t optlen;
6059         if (drv->monitor_ifidx >= 0) {
6060                 drv->monitor_refcount++;
6061                 return 0;
6062         }
6064         if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
6065                 /*
6066                  * P2P interface name is of the format p2p-%s-%d. For monitor
6067                  * interface name corresponding to P2P GO, replace "p2p-" with
6068                  * "mon-" to retain the same interface name length and to
6069                  * indicate that it is a monitor interface.
6070                  */
6071                 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
6072         } else {
6073                 /* Non-P2P interface with AP functionality. */
6074                 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
6075         }
6077         buf[IFNAMSIZ - 1] = '\0';
6079         drv->monitor_ifidx =
6080                 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
6081                                      0);
6083         if (drv->monitor_ifidx == -EOPNOTSUPP) {
6084                 /*
6085                  * This is backward compatibility for a few versions of
6086                  * the kernel only that didn't advertise the right
6087                  * attributes for the only driver that then supported
6088                  * AP mode w/o monitor -- ath6kl.
6089                  */
6090                 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
6091                            "monitor interface type - try to run without it");
6092                 drv->device_ap_sme = 1;
6093         }
6095         if (drv->monitor_ifidx < 0)
6096                 return -1;
6098         if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
6099                 goto error;
6101         memset(&ll, 0, sizeof(ll));
6102         ll.sll_family = AF_PACKET;
6103         ll.sll_ifindex = drv->monitor_ifidx;
6104         drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
6105         if (drv->monitor_sock < 0) {
6106                 perror("socket[PF_PACKET,SOCK_RAW]");
6107                 goto error;
6108         }
6110         if (add_monitor_filter(drv->monitor_sock)) {
6111                 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
6112                            "interface; do filtering in user space");
6113                 /* This works, but will cost in performance. */
6114         }
6116         if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
6117                 perror("monitor socket bind");
6118                 goto error;
6119         }
6121         optlen = sizeof(optval);
6122         optval = 20;
6123         if (setsockopt
6124             (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
6125                 perror("Failed to set socket priority");
6126                 goto error;
6127         }
6129         if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
6130                                      drv, NULL)) {
6131                 printf("Could not register monitor read socket\n");
6132                 goto error;
6133         }
6135         return 0;
6136  error:
6137         nl80211_remove_monitor_interface(drv);
6138         return -1;
6142 static int nl80211_setup_ap(struct i802_bss *bss)
6144         struct wpa_driver_nl80211_data *drv = bss->drv;
6146         wpa_printf(MSG_DEBUG, "nl80211: Setup AP - device_ap_sme=%d "
6147                    "use_monitor=%d", drv->device_ap_sme, drv->use_monitor);
6149         /*
6150          * Disable Probe Request reporting unless we need it in this way for
6151          * devices that include the AP SME, in the other case (unless using
6152          * monitor iface) we'll get it through the nl_mgmt socket instead.
6153          */
6154         if (!drv->device_ap_sme)
6155                 wpa_driver_nl80211_probe_req_report(bss, 0);
6157         if (!drv->device_ap_sme && !drv->use_monitor)
6158                 if (nl80211_mgmt_subscribe_ap(bss))
6159                         return -1;
6161         if (drv->device_ap_sme && !drv->use_monitor)
6162                 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
6163                         return -1;
6165         if (!drv->device_ap_sme && drv->use_monitor &&
6166             nl80211_create_monitor_interface(drv) &&
6167             !drv->device_ap_sme)
6168                 return -1;
6170         if (drv->device_ap_sme &&
6171             wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
6172                 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
6173                            "Probe Request frame reporting in AP mode");
6174                 /* Try to survive without this */
6175         }
6177         return 0;
6181 static void nl80211_teardown_ap(struct i802_bss *bss)
6183         struct wpa_driver_nl80211_data *drv = bss->drv;
6185         if (drv->device_ap_sme) {
6186                 wpa_driver_nl80211_probe_req_report(bss, 0);
6187                 if (!drv->use_monitor)
6188                         nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
6189         } else if (drv->use_monitor)
6190                 nl80211_remove_monitor_interface(drv);
6191         else
6192                 nl80211_mgmt_unsubscribe(bss, "AP teardown");
6194         bss->beacon_set = 0;
6198 static int nl80211_send_eapol_data(struct i802_bss *bss,
6199                                    const u8 *addr, const u8 *data,
6200                                    size_t data_len)
6202         struct sockaddr_ll ll;
6203         int ret;
6205         if (bss->drv->eapol_tx_sock < 0) {
6206                 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
6207                 return -1;
6208         }
6210         os_memset(&ll, 0, sizeof(ll));
6211         ll.sll_family = AF_PACKET;
6212         ll.sll_ifindex = bss->ifindex;
6213         ll.sll_protocol = htons(ETH_P_PAE);
6214         ll.sll_halen = ETH_ALEN;
6215         os_memcpy(ll.sll_addr, addr, ETH_ALEN);
6216         ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
6217                      (struct sockaddr *) &ll, sizeof(ll));
6218         if (ret < 0)
6219                 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
6220                            strerror(errno));
6222         return ret;
6226 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
6228 static int wpa_driver_nl80211_hapd_send_eapol(
6229         void *priv, const u8 *addr, const u8 *data,
6230         size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
6232         struct i802_bss *bss = priv;
6233         struct wpa_driver_nl80211_data *drv = bss->drv;
6234         struct ieee80211_hdr *hdr;
6235         size_t len;
6236         u8 *pos;
6237         int res;
6238         int qos = flags & WPA_STA_WMM;
6240         if (drv->device_ap_sme || !drv->use_monitor)
6241                 return nl80211_send_eapol_data(bss, addr, data, data_len);
6243         len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
6244                 data_len;
6245         hdr = os_zalloc(len);
6246         if (hdr == NULL) {
6247                 printf("malloc() failed for i802_send_data(len=%lu)\n",
6248                        (unsigned long) len);
6249                 return -1;
6250         }
6252         hdr->frame_control =
6253                 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
6254         hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
6255         if (encrypt)
6256                 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
6257         if (qos) {
6258                 hdr->frame_control |=
6259                         host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
6260         }
6262         memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6263         memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6264         memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6265         pos = (u8 *) (hdr + 1);
6267         if (qos) {
6268                 /* add an empty QoS header if needed */
6269                 pos[0] = 0;
6270                 pos[1] = 0;
6271                 pos += 2;
6272         }
6274         memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
6275         pos += sizeof(rfc1042_header);
6276         WPA_PUT_BE16(pos, ETH_P_PAE);
6277         pos += 2;
6278         memcpy(pos, data, data_len);
6280         res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
6281                                             0, 0, 0, 0);
6282         if (res < 0) {
6283                 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
6284                            "failed: %d (%s)",
6285                            (unsigned long) len, errno, strerror(errno));
6286         }
6287         os_free(hdr);
6289         return res;
6293 static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
6294                                             int total_flags,
6295                                             int flags_or, int flags_and)
6297         struct i802_bss *bss = priv;
6298         struct wpa_driver_nl80211_data *drv = bss->drv;
6299         struct nl_msg *msg, *flags = NULL;
6300         struct nl80211_sta_flag_update upd;
6302         msg = nlmsg_alloc();
6303         if (!msg)
6304                 return -ENOMEM;
6306         flags = nlmsg_alloc();
6307         if (!flags) {
6308                 nlmsg_free(msg);
6309                 return -ENOMEM;
6310         }
6312         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
6314         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6315                     if_nametoindex(bss->ifname));
6316         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6318         /*
6319          * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
6320          * can be removed eventually.
6321          */
6322         if (total_flags & WPA_STA_AUTHORIZED)
6323                 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
6325         if (total_flags & WPA_STA_WMM)
6326                 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
6328         if (total_flags & WPA_STA_SHORT_PREAMBLE)
6329                 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
6331         if (total_flags & WPA_STA_MFP)
6332                 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
6334         if (total_flags & WPA_STA_TDLS_PEER)
6335                 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_TDLS_PEER);
6337         if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
6338                 goto nla_put_failure;
6340         os_memset(&upd, 0, sizeof(upd));
6341         upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
6342         upd.set = sta_flags_nl80211(flags_or);
6343         NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
6345         nlmsg_free(flags);
6347         return send_and_recv_msgs(drv, msg, NULL, NULL);
6348  nla_put_failure:
6349         nlmsg_free(msg);
6350         nlmsg_free(flags);
6351         return -ENOBUFS;
6355 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
6356                                  struct wpa_driver_associate_params *params)
6358         enum nl80211_iftype nlmode;
6360         if (params->p2p) {
6361                 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
6362                            "group (GO)");
6363                 nlmode = NL80211_IFTYPE_P2P_GO;
6364         } else
6365                 nlmode = NL80211_IFTYPE_AP;
6367         if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode) ||
6368             wpa_driver_nl80211_set_freq(&drv->first_bss, params->freq, 0, 0)) {
6369                 nl80211_remove_monitor_interface(drv);
6370                 return -1;
6371         }
6373         return 0;
6377 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
6379         struct nl_msg *msg;
6380         int ret = -1;
6382         msg = nlmsg_alloc();
6383         if (!msg)
6384                 return -1;
6386         nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
6387         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6388         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6389         msg = NULL;
6390         if (ret) {
6391                 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
6392                            "(%s)", ret, strerror(-ret));
6393                 goto nla_put_failure;
6394         }
6396         ret = 0;
6397         wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
6399 nla_put_failure:
6400         nlmsg_free(msg);
6401         return ret;
6405 static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
6406                                    struct wpa_driver_associate_params *params)
6408         struct nl_msg *msg;
6409         int ret = -1;
6410         int count = 0;
6412         wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
6414         if (wpa_driver_nl80211_set_mode(&drv->first_bss,
6415                                         NL80211_IFTYPE_ADHOC)) {
6416                 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
6417                            "IBSS mode");
6418                 return -1;
6419         }
6421 retry:
6422         msg = nlmsg_alloc();
6423         if (!msg)
6424                 return -1;
6426         nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
6427         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6429         if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
6430                 goto nla_put_failure;
6432         wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
6433                           params->ssid, params->ssid_len);
6434         NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6435                 params->ssid);
6436         os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6437         drv->ssid_len = params->ssid_len;
6439         wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
6440         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6442         ret = nl80211_set_conn_keys(params, msg);
6443         if (ret)
6444                 goto nla_put_failure;
6446         if (params->bssid && params->fixed_bssid) {
6447                 wpa_printf(MSG_DEBUG, "  * BSSID=" MACSTR,
6448                            MAC2STR(params->bssid));
6449                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6450         }
6452         if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
6453             params->key_mgmt_suite == KEY_MGMT_PSK ||
6454             params->key_mgmt_suite == KEY_MGMT_802_1X_SHA256 ||
6455             params->key_mgmt_suite == KEY_MGMT_PSK_SHA256) {
6456                 wpa_printf(MSG_DEBUG, "  * control port");
6457                 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
6458         }
6460         if (params->wpa_ie) {
6461                 wpa_hexdump(MSG_DEBUG,
6462                             "  * Extra IEs for Beacon/Probe Response frames",
6463                             params->wpa_ie, params->wpa_ie_len);
6464                 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6465                         params->wpa_ie);
6466         }
6468         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6469         msg = NULL;
6470         if (ret) {
6471                 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
6472                            ret, strerror(-ret));
6473                 count++;
6474                 if (ret == -EALREADY && count == 1) {
6475                         wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
6476                                    "forced leave");
6477                         nl80211_leave_ibss(drv);
6478                         nlmsg_free(msg);
6479                         goto retry;
6480                 }
6482                 goto nla_put_failure;
6483         }
6484         ret = 0;
6485         wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
6487 nla_put_failure:
6488         nlmsg_free(msg);
6489         return ret;
6493 static unsigned int nl80211_get_assoc_bssid(struct wpa_driver_nl80211_data *drv,
6494                                             u8 *bssid)
6496         struct nl_msg *msg;
6497         int ret;
6498         struct nl80211_bss_info_arg arg;
6500         os_memset(&arg, 0, sizeof(arg));
6501         msg = nlmsg_alloc();
6502         if (!msg)
6503                 goto nla_put_failure;
6505         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
6506         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6508         arg.drv = drv;
6509         ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
6510         msg = NULL;
6511         if (ret == 0) {
6512                 if (is_zero_ether_addr(arg.assoc_bssid))
6513                         return -ENOTCONN;
6514                 os_memcpy(bssid, arg.assoc_bssid, ETH_ALEN);
6515                 return 0;
6516         }
6517         wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
6518                    "(%s)", ret, strerror(-ret));
6519 nla_put_failure:
6520         nlmsg_free(msg);
6521         return drv->assoc_freq;
6525 static int nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
6526                               const u8 *bssid)
6528         u8 addr[ETH_ALEN];
6530         if (bssid == NULL) {
6531                 int res = nl80211_get_assoc_bssid(drv, addr);
6532                 if (res)
6533                         return res;
6534                 bssid = addr;
6535         }
6537         return wpa_driver_nl80211_disconnect(drv, bssid,
6538                                              WLAN_REASON_PREV_AUTH_NOT_VALID);
6542 static int wpa_driver_nl80211_connect(
6543         struct wpa_driver_nl80211_data *drv,
6544         struct wpa_driver_associate_params *params)
6546         struct nl_msg *msg;
6547         enum nl80211_auth_type type;
6548         int ret = 0;
6549         int algs;
6551         msg = nlmsg_alloc();
6552         if (!msg)
6553                 return -1;
6555         wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
6556         nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
6558         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6559         if (params->bssid) {
6560                 wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
6561                            MAC2STR(params->bssid));
6562                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6563         }
6564         if (params->freq) {
6565                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
6566                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6567         }
6568         if (params->ssid) {
6569                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
6570                                   params->ssid, params->ssid_len);
6571                 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6572                         params->ssid);
6573                 if (params->ssid_len > sizeof(drv->ssid))
6574                         goto nla_put_failure;
6575                 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6576                 drv->ssid_len = params->ssid_len;
6577         }
6578         wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie, params->wpa_ie_len);
6579         if (params->wpa_ie)
6580                 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6581                         params->wpa_ie);
6583         algs = 0;
6584         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6585                 algs++;
6586         if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6587                 algs++;
6588         if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6589                 algs++;
6590         if (algs > 1) {
6591                 wpa_printf(MSG_DEBUG, "  * Leave out Auth Type for automatic "
6592                            "selection");
6593                 goto skip_auth_type;
6594         }
6596         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
6597                 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
6598         else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
6599                 type = NL80211_AUTHTYPE_SHARED_KEY;
6600         else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
6601                 type = NL80211_AUTHTYPE_NETWORK_EAP;
6602         else if (params->auth_alg & WPA_AUTH_ALG_FT)
6603                 type = NL80211_AUTHTYPE_FT;
6604         else
6605                 goto nla_put_failure;
6607         wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
6608         NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
6610 skip_auth_type:
6611         if (params->wpa_proto) {
6612                 enum nl80211_wpa_versions ver = 0;
6614                 if (params->wpa_proto & WPA_PROTO_WPA)
6615                         ver |= NL80211_WPA_VERSION_1;
6616                 if (params->wpa_proto & WPA_PROTO_RSN)
6617                         ver |= NL80211_WPA_VERSION_2;
6619                 wpa_printf(MSG_DEBUG, "  * WPA Versions 0x%x", ver);
6620                 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
6621         }
6623         if (params->pairwise_suite != CIPHER_NONE) {
6624                 int cipher;
6626                 switch (params->pairwise_suite) {
6627                 case CIPHER_WEP40:
6628                         cipher = WLAN_CIPHER_SUITE_WEP40;
6629                         break;
6630                 case CIPHER_WEP104:
6631                         cipher = WLAN_CIPHER_SUITE_WEP104;
6632                         break;
6633                 case CIPHER_CCMP:
6634                         cipher = WLAN_CIPHER_SUITE_CCMP;
6635                         break;
6636                 case CIPHER_TKIP:
6637                 default:
6638                         cipher = WLAN_CIPHER_SUITE_TKIP;
6639                         break;
6640                 }
6641                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
6642         }
6644         if (params->group_suite != CIPHER_NONE) {
6645                 int cipher;
6647                 switch (params->group_suite) {
6648                 case CIPHER_WEP40:
6649                         cipher = WLAN_CIPHER_SUITE_WEP40;
6650                         break;
6651                 case CIPHER_WEP104:
6652                         cipher = WLAN_CIPHER_SUITE_WEP104;
6653                         break;
6654                 case CIPHER_CCMP:
6655                         cipher = WLAN_CIPHER_SUITE_CCMP;
6656                         break;
6657                 case CIPHER_TKIP:
6658                 default:
6659                         cipher = WLAN_CIPHER_SUITE_TKIP;
6660                         break;
6661                 }
6662                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
6663         }
6665         if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
6666             params->key_mgmt_suite == KEY_MGMT_PSK) {
6667                 int mgmt = WLAN_AKM_SUITE_PSK;
6669                 switch (params->key_mgmt_suite) {
6670                 case KEY_MGMT_802_1X:
6671                         mgmt = WLAN_AKM_SUITE_8021X;
6672                         break;
6673                 case KEY_MGMT_PSK:
6674                 default:
6675                         mgmt = WLAN_AKM_SUITE_PSK;
6676                         break;
6677                 }
6678                 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
6679         }
6681         if (params->disable_ht)
6682                 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
6684         if (params->htcaps && params->htcaps_mask) {
6685                 int sz = sizeof(struct ieee80211_ht_capabilities);
6686                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
6687                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
6688                         params->htcaps_mask);
6689         }
6691         ret = nl80211_set_conn_keys(params, msg);
6692         if (ret)
6693                 goto nla_put_failure;
6695         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6696         msg = NULL;
6697         if (ret) {
6698                 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
6699                            "(%s)", ret, strerror(-ret));
6700                 /*
6701                  * cfg80211 does not currently accept new connection if we are
6702                  * already connected. As a workaround, force disconnection and
6703                  * try again once the driver indicates it completed
6704                  * disconnection.
6705                  */
6706                 if (ret == -EALREADY)
6707                         nl80211_disconnect(drv, params->bssid);
6708                 goto nla_put_failure;
6709         }
6710         ret = 0;
6711         wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
6713 nla_put_failure:
6714         nlmsg_free(msg);
6715         return ret;
6720 static int wpa_driver_nl80211_associate(
6721         void *priv, struct wpa_driver_associate_params *params)
6723         struct i802_bss *bss = priv;
6724         struct wpa_driver_nl80211_data *drv = bss->drv;
6725         int ret = -1;
6726         struct nl_msg *msg;
6728         if (params->mode == IEEE80211_MODE_AP)
6729                 return wpa_driver_nl80211_ap(drv, params);
6731         if (params->mode == IEEE80211_MODE_IBSS)
6732                 return wpa_driver_nl80211_ibss(drv, params);
6734         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
6735                 enum nl80211_iftype nlmode = params->p2p ?
6736                         NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
6738                 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
6739                         return -1;
6740                 return wpa_driver_nl80211_connect(drv, params);
6741         }
6743         drv->associated = 0;
6745         msg = nlmsg_alloc();
6746         if (!msg)
6747                 return -1;
6749         wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
6750                    drv->ifindex);
6751         nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
6753         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6754         if (params->bssid) {
6755                 wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
6756                            MAC2STR(params->bssid));
6757                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
6758         }
6759         if (params->freq) {
6760                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
6761                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
6762                 drv->assoc_freq = params->freq;
6763         } else
6764                 drv->assoc_freq = 0;
6765         if (params->ssid) {
6766                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
6767                                   params->ssid, params->ssid_len);
6768                 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
6769                         params->ssid);
6770                 if (params->ssid_len > sizeof(drv->ssid))
6771                         goto nla_put_failure;
6772                 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
6773                 drv->ssid_len = params->ssid_len;
6774         }
6775         wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie, params->wpa_ie_len);
6776         if (params->wpa_ie)
6777                 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
6778                         params->wpa_ie);
6780         if (params->pairwise_suite != CIPHER_NONE) {
6781                 int cipher;
6783                 switch (params->pairwise_suite) {
6784                 case CIPHER_WEP40:
6785                         cipher = WLAN_CIPHER_SUITE_WEP40;
6786                         break;
6787                 case CIPHER_WEP104:
6788                         cipher = WLAN_CIPHER_SUITE_WEP104;
6789                         break;
6790                 case CIPHER_CCMP:
6791                         cipher = WLAN_CIPHER_SUITE_CCMP;
6792                         break;
6793                 case CIPHER_TKIP:
6794                 default:
6795                         cipher = WLAN_CIPHER_SUITE_TKIP;
6796                         break;
6797                 }
6798                 wpa_printf(MSG_DEBUG, "  * pairwise=0x%x", cipher);
6799                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
6800         }
6802         if (params->group_suite != CIPHER_NONE) {
6803                 int cipher;
6805                 switch (params->group_suite) {
6806                 case CIPHER_WEP40:
6807                         cipher = WLAN_CIPHER_SUITE_WEP40;
6808                         break;
6809                 case CIPHER_WEP104:
6810                         cipher = WLAN_CIPHER_SUITE_WEP104;
6811                         break;
6812                 case CIPHER_CCMP:
6813                         cipher = WLAN_CIPHER_SUITE_CCMP;
6814                         break;
6815                 case CIPHER_TKIP:
6816                 default:
6817                         cipher = WLAN_CIPHER_SUITE_TKIP;
6818                         break;
6819                 }
6820                 wpa_printf(MSG_DEBUG, "  * group=0x%x", cipher);
6821                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
6822         }
6824 #ifdef CONFIG_IEEE80211W
6825         if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
6826                 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
6827 #endif /* CONFIG_IEEE80211W */
6829         NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
6831         if (params->prev_bssid) {
6832                 wpa_printf(MSG_DEBUG, "  * prev_bssid=" MACSTR,
6833                            MAC2STR(params->prev_bssid));
6834                 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
6835                         params->prev_bssid);
6836         }
6838         if (params->disable_ht)
6839                 NLA_PUT_FLAG(msg, NL80211_ATTR_DISABLE_HT);
6841         if (params->htcaps && params->htcaps_mask) {
6842                 int sz = sizeof(struct ieee80211_ht_capabilities);
6843                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, sz, params->htcaps);
6844                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
6845                         params->htcaps_mask);
6846         }
6848         if (params->p2p)
6849                 wpa_printf(MSG_DEBUG, "  * P2P group");
6851         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6852         msg = NULL;
6853         if (ret) {
6854                 wpa_dbg(drv->ctx, MSG_DEBUG,
6855                         "nl80211: MLME command failed (assoc): ret=%d (%s)",
6856                         ret, strerror(-ret));
6857                 nl80211_dump_scan(drv);
6858                 goto nla_put_failure;
6859         }
6860         ret = 0;
6861         wpa_printf(MSG_DEBUG, "nl80211: Association request send "
6862                    "successfully");
6864 nla_put_failure:
6865         nlmsg_free(msg);
6866         return ret;
6870 static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
6871                             int ifindex, enum nl80211_iftype mode)
6873         struct nl_msg *msg;
6874         int ret = -ENOBUFS;
6876         wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
6877                    ifindex, mode, nl80211_iftype_str(mode));
6879         msg = nlmsg_alloc();
6880         if (!msg)
6881                 return -ENOMEM;
6883         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
6884         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
6885         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
6887         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6888         msg = NULL;
6889         if (!ret)
6890                 return 0;
6891 nla_put_failure:
6892         nlmsg_free(msg);
6893         wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
6894                    " %d (%s)", ifindex, mode, ret, strerror(-ret));
6895         return ret;
6899 static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
6900                                        enum nl80211_iftype nlmode)
6902         struct wpa_driver_nl80211_data *drv = bss->drv;
6903         int ret = -1;
6904         int i;
6905         int was_ap = is_ap_interface(drv->nlmode);
6906         int res;
6908         res = nl80211_set_mode(drv, drv->ifindex, nlmode);
6909         if (res == 0) {
6910                 drv->nlmode = nlmode;
6911                 ret = 0;
6912                 goto done;
6913         }
6915         if (res == -ENODEV)
6916                 return -1;
6918         if (nlmode == drv->nlmode) {
6919                 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
6920                            "requested mode - ignore error");
6921                 ret = 0;
6922                 goto done; /* Already in the requested mode */
6923         }
6925         /* mac80211 doesn't allow mode changes while the device is up, so
6926          * take the device down, try to set the mode again, and bring the
6927          * device back up.
6928          */
6929         wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
6930                    "interface down");
6931         for (i = 0; i < 10; i++) {
6932                 res = linux_set_iface_flags(drv->global->ioctl_sock,
6933                                             bss->ifname, 0);
6934                 if (res == -EACCES || res == -ENODEV)
6935                         break;
6936                 if (res == 0) {
6937                         /* Try to set the mode again while the interface is
6938                          * down */
6939                         ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
6940                         if (ret == -EACCES)
6941                                 break;
6942                         res = linux_set_iface_flags(drv->global->ioctl_sock,
6943                                                     bss->ifname, 1);
6944                         if (res && !ret)
6945                                 ret = -1;
6946                         else if (ret != -EBUSY)
6947                                 break;
6948                 } else
6949                         wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
6950                                    "interface down");
6951                 os_sleep(0, 100000);
6952         }
6954         if (!ret) {
6955                 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
6956                            "interface is down");
6957                 drv->nlmode = nlmode;
6958                 drv->ignore_if_down_event = 1;
6959         }
6961 done:
6962         if (ret) {
6963                 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
6964                            "from %d failed", nlmode, drv->nlmode);
6965                 return ret;
6966         }
6968         if (is_ap_interface(nlmode)) {
6969                 nl80211_mgmt_unsubscribe(bss, "start AP");
6970                 /* Setup additional AP mode functionality if needed */
6971                 if (nl80211_setup_ap(bss))
6972                         return -1;
6973         } else if (was_ap) {
6974                 /* Remove additional AP mode functionality */
6975                 nl80211_teardown_ap(bss);
6976         } else {
6977                 nl80211_mgmt_unsubscribe(bss, "mode change");
6978         }
6980         if (!is_ap_interface(nlmode) &&
6981             nl80211_mgmt_subscribe_non_ap(bss) < 0)
6982                 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
6983                            "frame processing - ignore for now");
6985         return 0;
6989 static int wpa_driver_nl80211_get_capa(void *priv,
6990                                        struct wpa_driver_capa *capa)
6992         struct i802_bss *bss = priv;
6993         struct wpa_driver_nl80211_data *drv = bss->drv;
6994         if (!drv->has_capability)
6995                 return -1;
6996         os_memcpy(capa, &drv->capa, sizeof(*capa));
6997         return 0;
7001 static int wpa_driver_nl80211_set_operstate(void *priv, int state)
7003         struct i802_bss *bss = priv;
7004         struct wpa_driver_nl80211_data *drv = bss->drv;
7006         wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
7007                    __func__, drv->operstate, state, state ? "UP" : "DORMANT");
7008         drv->operstate = state;
7009         return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
7010                                       state ? IF_OPER_UP : IF_OPER_DORMANT);
7014 static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
7016         struct i802_bss *bss = priv;
7017         struct wpa_driver_nl80211_data *drv = bss->drv;
7018         struct nl_msg *msg;
7019         struct nl80211_sta_flag_update upd;
7021         msg = nlmsg_alloc();
7022         if (!msg)
7023                 return -ENOMEM;
7025         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
7027         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7028                     if_nametoindex(bss->ifname));
7029         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
7031         os_memset(&upd, 0, sizeof(upd));
7032         upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
7033         if (authorized)
7034                 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
7035         NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
7037         return send_and_recv_msgs(drv, msg, NULL, NULL);
7038  nla_put_failure:
7039         nlmsg_free(msg);
7040         return -ENOBUFS;
7044 /* Set kernel driver on given frequency (MHz) */
7045 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
7047         struct i802_bss *bss = priv;
7048         return wpa_driver_nl80211_set_freq(bss, freq->freq, freq->ht_enabled,
7049                                            freq->sec_channel_offset);
7053 #if defined(HOSTAPD) || defined(CONFIG_AP)
7055 static inline int min_int(int a, int b)
7057         if (a < b)
7058                 return a;
7059         return b;
7063 static int get_key_handler(struct nl_msg *msg, void *arg)
7065         struct nlattr *tb[NL80211_ATTR_MAX + 1];
7066         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7068         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7069                   genlmsg_attrlen(gnlh, 0), NULL);
7071         /*
7072          * TODO: validate the key index and mac address!
7073          * Otherwise, there's a race condition as soon as
7074          * the kernel starts sending key notifications.
7075          */
7077         if (tb[NL80211_ATTR_KEY_SEQ])
7078                 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
7079                        min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
7080         return NL_SKIP;
7084 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
7085                            int idx, u8 *seq)
7087         struct i802_bss *bss = priv;
7088         struct wpa_driver_nl80211_data *drv = bss->drv;
7089         struct nl_msg *msg;
7091         msg = nlmsg_alloc();
7092         if (!msg)
7093                 return -ENOMEM;
7095         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
7097         if (addr)
7098                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7099         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
7100         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
7102         memset(seq, 0, 6);
7104         return send_and_recv_msgs(drv, msg, get_key_handler, seq);
7105  nla_put_failure:
7106         nlmsg_free(msg);
7107         return -ENOBUFS;
7111 static int i802_set_rts(void *priv, int rts)
7113         struct i802_bss *bss = priv;
7114         struct wpa_driver_nl80211_data *drv = bss->drv;
7115         struct nl_msg *msg;
7116         int ret = -ENOBUFS;
7117         u32 val;
7119         msg = nlmsg_alloc();
7120         if (!msg)
7121                 return -ENOMEM;
7123         if (rts >= 2347)
7124                 val = (u32) -1;
7125         else
7126                 val = rts;
7128         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
7129         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7130         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
7132         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7133         msg = NULL;
7134         if (!ret)
7135                 return 0;
7136 nla_put_failure:
7137         nlmsg_free(msg);
7138         wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
7139                    "%d (%s)", rts, ret, strerror(-ret));
7140         return ret;
7144 static int i802_set_frag(void *priv, int frag)
7146         struct i802_bss *bss = priv;
7147         struct wpa_driver_nl80211_data *drv = bss->drv;
7148         struct nl_msg *msg;
7149         int ret = -ENOBUFS;
7150         u32 val;
7152         msg = nlmsg_alloc();
7153         if (!msg)
7154                 return -ENOMEM;
7156         if (frag >= 2346)
7157                 val = (u32) -1;
7158         else
7159                 val = frag;
7161         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
7162         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7163         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
7165         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7166         msg = NULL;
7167         if (!ret)
7168                 return 0;
7169 nla_put_failure:
7170         nlmsg_free(msg);
7171         wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
7172                    "%d: %d (%s)", frag, ret, strerror(-ret));
7173         return ret;
7177 static int i802_flush(void *priv)
7179         struct i802_bss *bss = priv;
7180         struct wpa_driver_nl80211_data *drv = bss->drv;
7181         struct nl_msg *msg;
7182         int res;
7184         msg = nlmsg_alloc();
7185         if (!msg)
7186                 return -1;
7188         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
7190         /*
7191          * XXX: FIX! this needs to flush all VLANs too
7192          */
7193         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7194                     if_nametoindex(bss->ifname));
7196         res = send_and_recv_msgs(drv, msg, NULL, NULL);
7197         if (res) {
7198                 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
7199                            "(%s)", res, strerror(-res));
7200         }
7201         return res;
7202  nla_put_failure:
7203         nlmsg_free(msg);
7204         return -ENOBUFS;
7208 static int get_sta_handler(struct nl_msg *msg, void *arg)
7210         struct nlattr *tb[NL80211_ATTR_MAX + 1];
7211         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7212         struct hostap_sta_driver_data *data = arg;
7213         struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
7214         static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
7215                 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
7216                 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
7217                 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
7218                 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
7219                 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
7220         };
7222         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7223                   genlmsg_attrlen(gnlh, 0), NULL);
7225         /*
7226          * TODO: validate the interface and mac address!
7227          * Otherwise, there's a race condition as soon as
7228          * the kernel starts sending station notifications.
7229          */
7231         if (!tb[NL80211_ATTR_STA_INFO]) {
7232                 wpa_printf(MSG_DEBUG, "sta stats missing!");
7233                 return NL_SKIP;
7234         }
7235         if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
7236                              tb[NL80211_ATTR_STA_INFO],
7237                              stats_policy)) {
7238                 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
7239                 return NL_SKIP;
7240         }
7242         if (stats[NL80211_STA_INFO_INACTIVE_TIME])
7243                 data->inactive_msec =
7244                         nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
7245         if (stats[NL80211_STA_INFO_RX_BYTES])
7246                 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
7247         if (stats[NL80211_STA_INFO_TX_BYTES])
7248                 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
7249         if (stats[NL80211_STA_INFO_RX_PACKETS])
7250                 data->rx_packets =
7251                         nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
7252         if (stats[NL80211_STA_INFO_TX_PACKETS])
7253                 data->tx_packets =
7254                         nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
7256         return NL_SKIP;
7259 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
7260                               const u8 *addr)
7262         struct i802_bss *bss = priv;
7263         struct wpa_driver_nl80211_data *drv = bss->drv;
7264         struct nl_msg *msg;
7266         os_memset(data, 0, sizeof(*data));
7267         msg = nlmsg_alloc();
7268         if (!msg)
7269                 return -ENOMEM;
7271         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
7273         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7274         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7276         return send_and_recv_msgs(drv, msg, get_sta_handler, data);
7277  nla_put_failure:
7278         nlmsg_free(msg);
7279         return -ENOBUFS;
7283 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
7284                                     int cw_min, int cw_max, int burst_time)
7286         struct i802_bss *bss = priv;
7287         struct wpa_driver_nl80211_data *drv = bss->drv;
7288         struct nl_msg *msg;
7289         struct nlattr *txq, *params;
7291         msg = nlmsg_alloc();
7292         if (!msg)
7293                 return -1;
7295         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
7297         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7299         txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
7300         if (!txq)
7301                 goto nla_put_failure;
7303         /* We are only sending parameters for a single TXQ at a time */
7304         params = nla_nest_start(msg, 1);
7305         if (!params)
7306                 goto nla_put_failure;
7308         switch (queue) {
7309         case 0:
7310                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
7311                 break;
7312         case 1:
7313                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
7314                 break;
7315         case 2:
7316                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
7317                 break;
7318         case 3:
7319                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
7320                 break;
7321         }
7322         /* Burst time is configured in units of 0.1 msec and TXOP parameter in
7323          * 32 usec, so need to convert the value here. */
7324         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
7325         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
7326         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
7327         NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
7329         nla_nest_end(msg, params);
7331         nla_nest_end(msg, txq);
7333         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7334                 return 0;
7335         msg = NULL;
7336  nla_put_failure:
7337         nlmsg_free(msg);
7338         return -1;
7342 static int i802_set_sta_vlan(void *priv, const u8 *addr,
7343                              const char *ifname, int vlan_id)
7345         struct i802_bss *bss = priv;
7346         struct wpa_driver_nl80211_data *drv = bss->drv;
7347         struct nl_msg *msg;
7348         int ret = -ENOBUFS;
7350         msg = nlmsg_alloc();
7351         if (!msg)
7352                 return -ENOMEM;
7354         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
7356         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
7357                     if_nametoindex(bss->ifname));
7358         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
7359         NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
7360                     if_nametoindex(ifname));
7362         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7363         msg = NULL;
7364         if (ret < 0) {
7365                 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
7366                            MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
7367                            MAC2STR(addr), ifname, vlan_id, ret,
7368                            strerror(-ret));
7369         }
7370  nla_put_failure:
7371         nlmsg_free(msg);
7372         return ret;
7376 static int i802_get_inact_sec(void *priv, const u8 *addr)
7378         struct hostap_sta_driver_data data;
7379         int ret;
7381         data.inactive_msec = (unsigned long) -1;
7382         ret = i802_read_sta_data(priv, &data, addr);
7383         if (ret || data.inactive_msec == (unsigned long) -1)
7384                 return -1;
7385         return data.inactive_msec / 1000;
7389 static int i802_sta_clear_stats(void *priv, const u8 *addr)
7391 #if 0
7392         /* TODO */
7393 #endif
7394         return 0;
7398 static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
7399                            int reason)
7401         struct i802_bss *bss = priv;
7402         struct ieee80211_mgmt mgmt;
7404         memset(&mgmt, 0, sizeof(mgmt));
7405         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
7406                                           WLAN_FC_STYPE_DEAUTH);
7407         memcpy(mgmt.da, addr, ETH_ALEN);
7408         memcpy(mgmt.sa, own_addr, ETH_ALEN);
7409         memcpy(mgmt.bssid, own_addr, ETH_ALEN);
7410         mgmt.u.deauth.reason_code = host_to_le16(reason);
7411         return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
7412                                             IEEE80211_HDRLEN +
7413                                             sizeof(mgmt.u.deauth), 0);
7417 static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
7418                              int reason)
7420         struct i802_bss *bss = priv;
7421         struct ieee80211_mgmt mgmt;
7423         memset(&mgmt, 0, sizeof(mgmt));
7424         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
7425                                           WLAN_FC_STYPE_DISASSOC);
7426         memcpy(mgmt.da, addr, ETH_ALEN);
7427         memcpy(mgmt.sa, own_addr, ETH_ALEN);
7428         memcpy(mgmt.bssid, own_addr, ETH_ALEN);
7429         mgmt.u.disassoc.reason_code = host_to_le16(reason);
7430         return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
7431                                             IEEE80211_HDRLEN +
7432                                             sizeof(mgmt.u.disassoc), 0);
7435 #endif /* HOSTAPD || CONFIG_AP */
7437 #ifdef HOSTAPD
7439 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
7441         int i;
7442         int *old;
7444         wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
7445                    ifidx);
7446         for (i = 0; i < drv->num_if_indices; i++) {
7447                 if (drv->if_indices[i] == 0) {
7448                         drv->if_indices[i] = ifidx;
7449                         return;
7450                 }
7451         }
7453         if (drv->if_indices != drv->default_if_indices)
7454                 old = drv->if_indices;
7455         else
7456                 old = NULL;
7458         drv->if_indices = os_realloc(old,
7459                                      sizeof(int) * (drv->num_if_indices + 1));
7460         if (!drv->if_indices) {
7461                 if (!old)
7462                         drv->if_indices = drv->default_if_indices;
7463                 else
7464                         drv->if_indices = old;
7465                 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
7466                            "interfaces");
7467                 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
7468                 return;
7469         } else if (!old)
7470                 os_memcpy(drv->if_indices, drv->default_if_indices,
7471                           sizeof(drv->default_if_indices));
7472         drv->if_indices[drv->num_if_indices] = ifidx;
7473         drv->num_if_indices++;
7477 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
7479         int i;
7481         for (i = 0; i < drv->num_if_indices; i++) {
7482                 if (drv->if_indices[i] == ifidx) {
7483                         drv->if_indices[i] = 0;
7484                         break;
7485                 }
7486         }
7490 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
7492         int i;
7494         for (i = 0; i < drv->num_if_indices; i++)
7495                 if (drv->if_indices[i] == ifidx)
7496                         return 1;
7498         return 0;
7502 static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
7503                             const char *bridge_ifname)
7505         struct i802_bss *bss = priv;
7506         struct wpa_driver_nl80211_data *drv = bss->drv;
7507         char name[IFNAMSIZ + 1];
7509         os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
7510         wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
7511                    " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
7512         if (val) {
7513                 if (!if_nametoindex(name)) {
7514                         if (nl80211_create_iface(drv, name,
7515                                                  NL80211_IFTYPE_AP_VLAN,
7516                                                  NULL, 1) < 0)
7517                                 return -1;
7518                         if (bridge_ifname &&
7519                             linux_br_add_if(drv->global->ioctl_sock,
7520                                             bridge_ifname, name) < 0)
7521                                 return -1;
7522                 }
7523                 linux_set_iface_flags(drv->global->ioctl_sock, name, 1);
7524                 return i802_set_sta_vlan(priv, addr, name, 0);
7525         } else {
7526                 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
7527                 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
7528                                                     name);
7529         }
7533 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
7535         struct wpa_driver_nl80211_data *drv = eloop_ctx;
7536         struct sockaddr_ll lladdr;
7537         unsigned char buf[3000];
7538         int len;
7539         socklen_t fromlen = sizeof(lladdr);
7541         len = recvfrom(sock, buf, sizeof(buf), 0,
7542                        (struct sockaddr *)&lladdr, &fromlen);
7543         if (len < 0) {
7544                 perror("recv");
7545                 return;
7546         }
7548         if (have_ifidx(drv, lladdr.sll_ifindex))
7549                 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
7553 static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
7554                              struct i802_bss *bss,
7555                              const char *brname, const char *ifname)
7557         int ifindex;
7558         char in_br[IFNAMSIZ];
7560         os_strlcpy(bss->brname, brname, IFNAMSIZ);
7561         ifindex = if_nametoindex(brname);
7562         if (ifindex == 0) {
7563                 /*
7564                  * Bridge was configured, but the bridge device does
7565                  * not exist. Try to add it now.
7566                  */
7567                 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
7568                         wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
7569                                    "bridge interface %s: %s",
7570                                    brname, strerror(errno));
7571                         return -1;
7572                 }
7573                 bss->added_bridge = 1;
7574                 add_ifidx(drv, if_nametoindex(brname));
7575         }
7577         if (linux_br_get(in_br, ifname) == 0) {
7578                 if (os_strcmp(in_br, brname) == 0)
7579                         return 0; /* already in the bridge */
7581                 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
7582                            "bridge %s", ifname, in_br);
7583                 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
7584                     0) {
7585                         wpa_printf(MSG_ERROR, "nl80211: Failed to "
7586                                    "remove interface %s from bridge "
7587                                    "%s: %s",
7588                                    ifname, brname, strerror(errno));
7589                         return -1;
7590                 }
7591         }
7593         wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
7594                    ifname, brname);
7595         if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
7596                 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
7597                            "into bridge %s: %s",
7598                            ifname, brname, strerror(errno));
7599                 return -1;
7600         }
7601         bss->added_if_into_bridge = 1;
7603         return 0;
7607 static void *i802_init(struct hostapd_data *hapd,
7608                        struct wpa_init_params *params)
7610         struct wpa_driver_nl80211_data *drv;
7611         struct i802_bss *bss;
7612         size_t i;
7613         char brname[IFNAMSIZ];
7614         int ifindex, br_ifindex;
7615         int br_added = 0;
7617         bss = wpa_driver_nl80211_init(hapd, params->ifname,
7618                                       params->global_priv);
7619         if (bss == NULL)
7620                 return NULL;
7622         drv = bss->drv;
7623         drv->nlmode = NL80211_IFTYPE_AP;
7624         drv->eapol_sock = -1;
7626         if (linux_br_get(brname, params->ifname) == 0) {
7627                 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
7628                            params->ifname, brname);
7629                 br_ifindex = if_nametoindex(brname);
7630         } else {
7631                 brname[0] = '\0';
7632                 br_ifindex = 0;
7633         }
7635         drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
7636         drv->if_indices = drv->default_if_indices;
7637         for (i = 0; i < params->num_bridge; i++) {
7638                 if (params->bridge[i]) {
7639                         ifindex = if_nametoindex(params->bridge[i]);
7640                         if (ifindex)
7641                                 add_ifidx(drv, ifindex);
7642                         if (ifindex == br_ifindex)
7643                                 br_added = 1;
7644                 }
7645         }
7646         if (!br_added && br_ifindex &&
7647             (params->num_bridge == 0 || !params->bridge[0]))
7648                 add_ifidx(drv, br_ifindex);
7650         /* start listening for EAPOL on the default AP interface */
7651         add_ifidx(drv, drv->ifindex);
7653         if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
7654                 goto failed;
7656         if (params->bssid) {
7657                 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7658                                        params->bssid))
7659                         goto failed;
7660         }
7662         if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
7663                 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
7664                            "into AP mode", bss->ifname);
7665                 goto failed;
7666         }
7668         if (params->num_bridge && params->bridge[0] &&
7669             i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
7670                 goto failed;
7672         if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
7673                 goto failed;
7675         drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
7676         if (drv->eapol_sock < 0) {
7677                 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
7678                 goto failed;
7679         }
7681         if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
7682         {
7683                 printf("Could not register read socket for eapol\n");
7684                 goto failed;
7685         }
7687         if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7688                                params->own_addr))
7689                 goto failed;
7691         memcpy(bss->addr, params->own_addr, ETH_ALEN);
7693         return bss;
7695 failed:
7696         wpa_driver_nl80211_deinit(bss);
7697         return NULL;
7701 static void i802_deinit(void *priv)
7703         wpa_driver_nl80211_deinit(priv);
7706 #endif /* HOSTAPD */
7709 static enum nl80211_iftype wpa_driver_nl80211_if_type(
7710         enum wpa_driver_if_type type)
7712         switch (type) {
7713         case WPA_IF_STATION:
7714                 return NL80211_IFTYPE_STATION;
7715         case WPA_IF_P2P_CLIENT:
7716         case WPA_IF_P2P_GROUP:
7717                 return NL80211_IFTYPE_P2P_CLIENT;
7718         case WPA_IF_AP_VLAN:
7719                 return NL80211_IFTYPE_AP_VLAN;
7720         case WPA_IF_AP_BSS:
7721                 return NL80211_IFTYPE_AP;
7722         case WPA_IF_P2P_GO:
7723                 return NL80211_IFTYPE_P2P_GO;
7724         }
7725         return -1;
7729 #ifdef CONFIG_P2P
7731 static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
7733         struct wpa_driver_nl80211_data *drv;
7734         dl_list_for_each(drv, &global->interfaces,
7735                          struct wpa_driver_nl80211_data, list) {
7736                 if (os_memcmp(addr, drv->first_bss.addr, ETH_ALEN) == 0)
7737                         return 1;
7738         }
7739         return 0;
7743 static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
7744                                       u8 *new_addr)
7746         unsigned int idx;
7748         if (!drv->global)
7749                 return -1;
7751         os_memcpy(new_addr, drv->first_bss.addr, ETH_ALEN);
7752         for (idx = 0; idx < 64; idx++) {
7753                 new_addr[0] = drv->first_bss.addr[0] | 0x02;
7754                 new_addr[0] ^= idx << 2;
7755                 if (!nl80211_addr_in_use(drv->global, new_addr))
7756                         break;
7757         }
7758         if (idx == 64)
7759                 return -1;
7761         wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
7762                    MACSTR, MAC2STR(new_addr));
7764         return 0;
7767 #endif /* CONFIG_P2P */
7770 static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
7771                                      const char *ifname, const u8 *addr,
7772                                      void *bss_ctx, void **drv_priv,
7773                                      char *force_ifname, u8 *if_addr,
7774                                      const char *bridge)
7776         struct i802_bss *bss = priv;
7777         struct wpa_driver_nl80211_data *drv = bss->drv;
7778         int ifidx;
7779 #ifdef HOSTAPD
7780         struct i802_bss *new_bss = NULL;
7782         if (type == WPA_IF_AP_BSS) {
7783                 new_bss = os_zalloc(sizeof(*new_bss));
7784                 if (new_bss == NULL)
7785                         return -1;
7786         }
7787 #endif /* HOSTAPD */
7789         if (addr)
7790                 os_memcpy(if_addr, addr, ETH_ALEN);
7791         ifidx = nl80211_create_iface(drv, ifname,
7792                                      wpa_driver_nl80211_if_type(type), addr,
7793                                      0);
7794         if (ifidx < 0) {
7795 #ifdef HOSTAPD
7796                 os_free(new_bss);
7797 #endif /* HOSTAPD */
7798                 return -1;
7799         }
7801         if (!addr &&
7802             linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7803                                if_addr) < 0) {
7804                 nl80211_remove_iface(drv, ifidx);
7805                 return -1;
7806         }
7808 #ifdef CONFIG_P2P
7809         if (!addr &&
7810             (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
7811              type == WPA_IF_P2P_GO)) {
7812                 /* Enforce unique P2P Interface Address */
7813                 u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
7815                 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7816                                        own_addr) < 0 ||
7817                     linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
7818                                        new_addr) < 0) {
7819                         nl80211_remove_iface(drv, ifidx);
7820                         return -1;
7821                 }
7822                 if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
7823                         wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
7824                                    "for P2P group interface");
7825                         if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
7826                                 nl80211_remove_iface(drv, ifidx);
7827                                 return -1;
7828                         }
7829                         if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
7830                                                new_addr) < 0) {
7831                                 nl80211_remove_iface(drv, ifidx);
7832                                 return -1;
7833                         }
7834                 }
7835                 os_memcpy(if_addr, new_addr, ETH_ALEN);
7836         }
7837 #endif /* CONFIG_P2P */
7839 #ifdef HOSTAPD
7840         if (bridge &&
7841             i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
7842                 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
7843                            "interface %s to a bridge %s", ifname, bridge);
7844                 nl80211_remove_iface(drv, ifidx);
7845                 os_free(new_bss);
7846                 return -1;
7847         }
7849         if (type == WPA_IF_AP_BSS) {
7850                 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
7851                 {
7852                         nl80211_remove_iface(drv, ifidx);
7853                         os_free(new_bss);
7854                         return -1;
7855                 }
7856                 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
7857                 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
7858                 new_bss->ifindex = ifidx;
7859                 new_bss->drv = drv;
7860                 new_bss->next = drv->first_bss.next;
7861                 new_bss->freq = drv->first_bss.freq;
7862                 drv->first_bss.next = new_bss;
7863                 if (drv_priv)
7864                         *drv_priv = new_bss;
7865                 nl80211_init_bss(new_bss);
7867                 /* Subscribe management frames for this WPA_IF_AP_BSS */
7868                 if (nl80211_setup_ap(new_bss))
7869                         return -1;
7870         }
7871 #endif /* HOSTAPD */
7873         if (drv->global)
7874                 drv->global->if_add_ifindex = ifidx;
7876         return 0;
7880 static int wpa_driver_nl80211_if_remove(void *priv,
7881                                         enum wpa_driver_if_type type,
7882                                         const char *ifname)
7884         struct i802_bss *bss = priv;
7885         struct wpa_driver_nl80211_data *drv = bss->drv;
7886         int ifindex = if_nametoindex(ifname);
7888         wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
7889                    __func__, type, ifname, ifindex);
7890         if (ifindex <= 0)
7891                 return -1;
7893 #ifdef HOSTAPD
7894         if (bss->added_if_into_bridge) {
7895                 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
7896                                     bss->ifname) < 0)
7897                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7898                                    "interface %s from bridge %s: %s",
7899                                    bss->ifname, bss->brname, strerror(errno));
7900         }
7901         if (bss->added_bridge) {
7902                 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
7903                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
7904                                    "bridge %s: %s",
7905                                    bss->brname, strerror(errno));
7906         }
7907 #endif /* HOSTAPD */
7909         nl80211_remove_iface(drv, ifindex);
7911 #ifdef HOSTAPD
7912         if (type != WPA_IF_AP_BSS)
7913                 return 0;
7915         if (bss != &drv->first_bss) {
7916                 struct i802_bss *tbss;
7918                 for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
7919                         if (tbss->next == bss) {
7920                                 tbss->next = bss->next;
7921                                 /* Unsubscribe management frames */
7922                                 nl80211_teardown_ap(bss);
7923                                 nl80211_destroy_bss(bss);
7924                                 os_free(bss);
7925                                 bss = NULL;
7926                                 break;
7927                         }
7928                 }
7929                 if (bss)
7930                         wpa_printf(MSG_INFO, "nl80211: %s - could not find "
7931                                    "BSS %p in the list", __func__, bss);
7932         }
7933 #endif /* HOSTAPD */
7935         return 0;
7939 static int cookie_handler(struct nl_msg *msg, void *arg)
7941         struct nlattr *tb[NL80211_ATTR_MAX + 1];
7942         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7943         u64 *cookie = arg;
7944         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7945                   genlmsg_attrlen(gnlh, 0), NULL);
7946         if (tb[NL80211_ATTR_COOKIE])
7947                 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
7948         return NL_SKIP;
7952 static int nl80211_send_frame_cmd(struct i802_bss *bss,
7953                                   unsigned int freq, unsigned int wait,
7954                                   const u8 *buf, size_t buf_len,
7955                                   u64 *cookie_out, int no_cck, int no_ack,
7956                                   int offchanok)
7958         struct wpa_driver_nl80211_data *drv = bss->drv;
7959         struct nl_msg *msg;
7960         u64 cookie;
7961         int ret = -1;
7963         msg = nlmsg_alloc();
7964         if (!msg)
7965                 return -1;
7967         nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
7969         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
7970         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7971         if (wait)
7972                 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
7973         if (offchanok && (drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX))
7974                 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
7975         if (no_cck)
7976                 NLA_PUT_FLAG(msg, NL80211_ATTR_TX_NO_CCK_RATE);
7977         if (no_ack)
7978                 NLA_PUT_FLAG(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK);
7980         NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
7982         cookie = 0;
7983         ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
7984         msg = NULL;
7985         if (ret) {
7986                 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
7987                            "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
7988                            freq, wait);
7989                 goto nla_put_failure;
7990         }
7991         wpa_printf(MSG_DEBUG, "nl80211: Frame TX command accepted%s; "
7992                    "cookie 0x%llx", no_ack ? " (no ACK)" : "",
7993                    (long long unsigned int) cookie);
7995         if (cookie_out)
7996                 *cookie_out = no_ack ? (u64) -1 : cookie;
7998 nla_put_failure:
7999         nlmsg_free(msg);
8000         return ret;
8004 static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq,
8005                                           unsigned int wait_time,
8006                                           const u8 *dst, const u8 *src,
8007                                           const u8 *bssid,
8008                                           const u8 *data, size_t data_len,
8009                                           int no_cck)
8011         struct i802_bss *bss = priv;
8012         struct wpa_driver_nl80211_data *drv = bss->drv;
8013         int ret = -1;
8014         u8 *buf;
8015         struct ieee80211_hdr *hdr;
8017         wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
8018                    "freq=%u MHz wait=%d ms no_cck=%d)",
8019                    drv->ifindex, freq, wait_time, no_cck);
8021         buf = os_zalloc(24 + data_len);
8022         if (buf == NULL)
8023                 return ret;
8024         os_memcpy(buf + 24, data, data_len);
8025         hdr = (struct ieee80211_hdr *) buf;
8026         hdr->frame_control =
8027                 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
8028         os_memcpy(hdr->addr1, dst, ETH_ALEN);
8029         os_memcpy(hdr->addr2, src, ETH_ALEN);
8030         os_memcpy(hdr->addr3, bssid, ETH_ALEN);
8032         if (is_ap_interface(drv->nlmode))
8033                 ret = wpa_driver_nl80211_send_mlme_freq(priv, buf,
8034                                                         24 + data_len,
8035                                                         0, freq, no_cck, 1,
8036                                                         wait_time);
8037         else
8038                 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
8039                                              24 + data_len,
8040                                              &drv->send_action_cookie,
8041                                              no_cck, 0, 1);
8043         os_free(buf);
8044         return ret;
8048 static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
8050         struct i802_bss *bss = priv;
8051         struct wpa_driver_nl80211_data *drv = bss->drv;
8052         struct nl_msg *msg;
8053         int ret;
8055         msg = nlmsg_alloc();
8056         if (!msg)
8057                 return;
8059         nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
8061         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8062         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
8064         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8065         msg = NULL;
8066         if (ret)
8067                 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
8068                            "(%s)", ret, strerror(-ret));
8070  nla_put_failure:
8071         nlmsg_free(msg);
8075 static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
8076                                                 unsigned int duration)
8078         struct i802_bss *bss = priv;
8079         struct wpa_driver_nl80211_data *drv = bss->drv;
8080         struct nl_msg *msg;
8081         int ret;
8082         u64 cookie;
8084         msg = nlmsg_alloc();
8085         if (!msg)
8086                 return -1;
8088         nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
8090         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8091         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
8092         NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
8094         cookie = 0;
8095         ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
8096         msg = NULL;
8097         if (ret == 0) {
8098                 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
8099                            "0x%llx for freq=%u MHz duration=%u",
8100                            (long long unsigned int) cookie, freq, duration);
8101                 drv->remain_on_chan_cookie = cookie;
8102                 drv->pending_remain_on_chan = 1;
8103                 return 0;
8104         }
8105         wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
8106                    "(freq=%d duration=%u): %d (%s)",
8107                    freq, duration, ret, strerror(-ret));
8108 nla_put_failure:
8109         nlmsg_free(msg);
8110         return -1;
8114 static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
8116         struct i802_bss *bss = priv;
8117         struct wpa_driver_nl80211_data *drv = bss->drv;
8118         struct nl_msg *msg;
8119         int ret;
8121         if (!drv->pending_remain_on_chan) {
8122                 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
8123                            "to cancel");
8124                 return -1;
8125         }
8127         wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
8128                    "0x%llx",
8129                    (long long unsigned int) drv->remain_on_chan_cookie);
8131         msg = nlmsg_alloc();
8132         if (!msg)
8133                 return -1;
8135         nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
8137         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8138         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
8140         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8141         msg = NULL;
8142         if (ret == 0)
8143                 return 0;
8144         wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
8145                    "%d (%s)", ret, strerror(-ret));
8146 nla_put_failure:
8147         nlmsg_free(msg);
8148         return -1;
8152 static int wpa_driver_nl80211_probe_req_report(void *priv, int report)
8154         struct i802_bss *bss = priv;
8155         struct wpa_driver_nl80211_data *drv = bss->drv;
8157         if (!report) {
8158                 if (bss->nl_preq && drv->device_ap_sme &&
8159                     is_ap_interface(drv->nlmode)) {
8160                         /*
8161                          * Do not disable Probe Request reporting that was
8162                          * enabled in nl80211_setup_ap().
8163                          */
8164                         wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
8165                                    "Probe Request reporting nl_preq=%p while "
8166                                    "in AP mode", bss->nl_preq);
8167                 } else if (bss->nl_preq) {
8168                         wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
8169                                    "reporting nl_preq=%p", bss->nl_preq);
8170                         eloop_unregister_read_sock(
8171                                 nl_socket_get_fd(bss->nl_preq));
8172                         nl_destroy_handles(&bss->nl_preq);
8173                 }
8174                 return 0;
8175         }
8177         if (bss->nl_preq) {
8178                 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
8179                            "already on! nl_preq=%p", bss->nl_preq);
8180                 return 0;
8181         }
8183         bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
8184         if (bss->nl_preq == NULL)
8185                 return -1;
8186         wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
8187                    "reporting nl_preq=%p", bss->nl_preq);
8189         if (nl80211_register_frame(bss, bss->nl_preq,
8190                                    (WLAN_FC_TYPE_MGMT << 2) |
8191                                    (WLAN_FC_STYPE_PROBE_REQ << 4),
8192                                    NULL, 0) < 0)
8193                 goto out_err;
8195         eloop_register_read_sock(nl_socket_get_fd(bss->nl_preq),
8196                                  wpa_driver_nl80211_event_receive, bss->nl_cb,
8197                                  bss->nl_preq);
8199         return 0;
8201  out_err:
8202         nl_destroy_handles(&bss->nl_preq);
8203         return -1;
8207 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
8208                                      int ifindex, int disabled)
8210         struct nl_msg *msg;
8211         struct nlattr *bands, *band;
8212         int ret;
8214         msg = nlmsg_alloc();
8215         if (!msg)
8216                 return -1;
8218         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
8219         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
8221         bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
8222         if (!bands)
8223                 goto nla_put_failure;
8225         /*
8226          * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
8227          * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
8228          * rates. All 5 GHz rates are left enabled.
8229          */
8230         band = nla_nest_start(msg, NL80211_BAND_2GHZ);
8231         if (!band)
8232                 goto nla_put_failure;
8233         if (disabled) {
8234                 NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
8235                         "\x0c\x12\x18\x24\x30\x48\x60\x6c");
8236         }
8237         nla_nest_end(msg, band);
8239         nla_nest_end(msg, bands);
8241         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8242         msg = NULL;
8243         if (ret) {
8244                 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
8245                            "(%s)", ret, strerror(-ret));
8246         }
8248         return ret;
8250 nla_put_failure:
8251         nlmsg_free(msg);
8252         return -1;
8256 static int wpa_driver_nl80211_deinit_ap(void *priv)
8258         struct i802_bss *bss = priv;
8259         struct wpa_driver_nl80211_data *drv = bss->drv;
8260         if (!is_ap_interface(drv->nlmode))
8261                 return -1;
8262         wpa_driver_nl80211_del_beacon(drv);
8263         return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
8267 static void wpa_driver_nl80211_resume(void *priv)
8269         struct i802_bss *bss = priv;
8270         struct wpa_driver_nl80211_data *drv = bss->drv;
8271         if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
8272                 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
8273                            "resume event");
8274         }
8278 static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
8279                                   const u8 *ies, size_t ies_len)
8281         struct i802_bss *bss = priv;
8282         struct wpa_driver_nl80211_data *drv = bss->drv;
8283         int ret;
8284         u8 *data, *pos;
8285         size_t data_len;
8286         const u8 *own_addr = bss->addr;
8288         if (action != 1) {
8289                 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
8290                            "action %d", action);
8291                 return -1;
8292         }
8294         /*
8295          * Action frame payload:
8296          * Category[1] = 6 (Fast BSS Transition)
8297          * Action[1] = 1 (Fast BSS Transition Request)
8298          * STA Address
8299          * Target AP Address
8300          * FT IEs
8301          */
8303         data_len = 2 + 2 * ETH_ALEN + ies_len;
8304         data = os_malloc(data_len);
8305         if (data == NULL)
8306                 return -1;
8307         pos = data;
8308         *pos++ = 0x06; /* FT Action category */
8309         *pos++ = action;
8310         os_memcpy(pos, own_addr, ETH_ALEN);
8311         pos += ETH_ALEN;
8312         os_memcpy(pos, target_ap, ETH_ALEN);
8313         pos += ETH_ALEN;
8314         os_memcpy(pos, ies, ies_len);
8316         ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
8317                                              drv->bssid, own_addr, drv->bssid,
8318                                              data, data_len, 0);
8319         os_free(data);
8321         return ret;
8325 static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
8327         struct i802_bss *bss = priv;
8328         struct wpa_driver_nl80211_data *drv = bss->drv;
8329         struct nl_msg *msg, *cqm = NULL;
8331         wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
8332                    "hysteresis=%d", threshold, hysteresis);
8334         msg = nlmsg_alloc();
8335         if (!msg)
8336                 return -1;
8338         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
8340         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8342         cqm = nlmsg_alloc();
8343         if (cqm == NULL)
8344                 return -1;
8346         NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
8347         NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
8348         nla_put_nested(msg, NL80211_ATTR_CQM, cqm);
8350         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
8351                 return 0;
8352         msg = NULL;
8354 nla_put_failure:
8355         nlmsg_free(cqm);
8356         nlmsg_free(msg);
8357         return -1;
8361 static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
8363         struct i802_bss *bss = priv;
8364         struct wpa_driver_nl80211_data *drv = bss->drv;
8365         int res;
8367         os_memset(si, 0, sizeof(*si));
8368         res = nl80211_get_link_signal(drv, si);
8369         if (res != 0)
8370                 return res;
8372         return nl80211_get_link_noise(drv, si);
8376 static int wpa_driver_nl80211_shared_freq(void *priv)
8378         struct i802_bss *bss = priv;
8379         struct wpa_driver_nl80211_data *drv = bss->drv;
8380         struct wpa_driver_nl80211_data *driver;
8381         int freq = 0;
8383         /*
8384          * If the same PHY is in connected state with some other interface,
8385          * then retrieve the assoc freq.
8386          */
8387         wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
8388                    drv->phyname);
8390         dl_list_for_each(driver, &drv->global->interfaces,
8391                          struct wpa_driver_nl80211_data, list) {
8392                 if (drv == driver ||
8393                     os_strcmp(drv->phyname, driver->phyname) != 0 ||
8394                     !driver->associated)
8395                         continue;
8397                 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
8398                            MACSTR,
8399                            driver->phyname, driver->first_bss.ifname,
8400                            MAC2STR(driver->first_bss.addr));
8401                 freq = nl80211_get_assoc_freq(driver);
8402                 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
8403                            drv->phyname, freq);
8404         }
8406         if (!freq)
8407                 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
8408                            "PHY (%s) in associated state", drv->phyname);
8410         return freq;
8414 static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
8415                               int encrypt)
8417         struct i802_bss *bss = priv;
8418         return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
8419                                              0, 0, 0, 0);
8423 static int nl80211_set_param(void *priv, const char *param)
8425         wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
8426         if (param == NULL)
8427                 return 0;
8429 #ifdef CONFIG_P2P
8430         if (os_strstr(param, "use_p2p_group_interface=1")) {
8431                 struct i802_bss *bss = priv;
8432                 struct wpa_driver_nl80211_data *drv = bss->drv;
8434                 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
8435                            "interface");
8436                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
8437                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
8438         }
8439 #endif /* CONFIG_P2P */
8441         return 0;
8445 static void * nl80211_global_init(void)
8447         struct nl80211_global *global;
8448         struct netlink_config *cfg;
8450         global = os_zalloc(sizeof(*global));
8451         if (global == NULL)
8452                 return NULL;
8453         global->ioctl_sock = -1;
8454         dl_list_init(&global->interfaces);
8455         global->if_add_ifindex = -1;
8457         cfg = os_zalloc(sizeof(*cfg));
8458         if (cfg == NULL)
8459                 goto err;
8461         cfg->ctx = global;
8462         cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
8463         cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
8464         global->netlink = netlink_init(cfg);
8465         if (global->netlink == NULL) {
8466                 os_free(cfg);
8467                 goto err;
8468         }
8470         if (wpa_driver_nl80211_init_nl_global(global) < 0)
8471                 goto err;
8473         global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
8474         if (global->ioctl_sock < 0) {
8475                 perror("socket(PF_INET,SOCK_DGRAM)");
8476                 goto err;
8477         }
8479         return global;
8481 err:
8482         nl80211_global_deinit(global);
8483         return NULL;
8487 static void nl80211_global_deinit(void *priv)
8489         struct nl80211_global *global = priv;
8490         if (global == NULL)
8491                 return;
8492         if (!dl_list_empty(&global->interfaces)) {
8493                 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
8494                            "nl80211_global_deinit",
8495                            dl_list_len(&global->interfaces));
8496         }
8498         if (global->netlink)
8499                 netlink_deinit(global->netlink);
8501         nl_destroy_handles(&global->nl);
8503         if (global->nl_event) {
8504                 eloop_unregister_read_sock(
8505                         nl_socket_get_fd(global->nl_event));
8506                 nl_destroy_handles(&global->nl_event);
8507         }
8509         nl_cb_put(global->nl_cb);
8511         if (global->ioctl_sock >= 0)
8512                 close(global->ioctl_sock);
8514         os_free(global);
8518 static const char * nl80211_get_radio_name(void *priv)
8520         struct i802_bss *bss = priv;
8521         struct wpa_driver_nl80211_data *drv = bss->drv;
8522         return drv->phyname;
8526 static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
8527                          const u8 *pmkid)
8529         struct nl_msg *msg;
8531         msg = nlmsg_alloc();
8532         if (!msg)
8533                 return -ENOMEM;
8535         nl80211_cmd(bss->drv, msg, 0, cmd);
8537         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
8538         if (pmkid)
8539                 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
8540         if (bssid)
8541                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
8543         return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8544  nla_put_failure:
8545         nlmsg_free(msg);
8546         return -ENOBUFS;
8550 static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
8552         struct i802_bss *bss = priv;
8553         wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
8554         return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
8558 static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
8560         struct i802_bss *bss = priv;
8561         wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
8562                    MAC2STR(bssid));
8563         return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
8567 static int nl80211_flush_pmkid(void *priv)
8569         struct i802_bss *bss = priv;
8570         wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
8571         return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
8575 static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
8576                                    const u8 *replay_ctr)
8578         struct i802_bss *bss = priv;
8579         struct wpa_driver_nl80211_data *drv = bss->drv;
8580         struct nlattr *replay_nested;
8581         struct nl_msg *msg;
8583         msg = nlmsg_alloc();
8584         if (!msg)
8585                 return;
8587         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8589         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8591         replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8592         if (!replay_nested)
8593                 goto nla_put_failure;
8595         NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
8596         NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
8597         NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
8598                 replay_ctr);
8600         nla_nest_end(msg, replay_nested);
8602         send_and_recv_msgs(drv, msg, NULL, NULL);
8603         return;
8604  nla_put_failure:
8605         nlmsg_free(msg);
8609 static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
8610                                     const u8 *addr, int qos)
8612         /* send data frame to poll STA and check whether
8613          * this frame is ACKed */
8614         struct {
8615                 struct ieee80211_hdr hdr;
8616                 u16 qos_ctl;
8617         } STRUCT_PACKED nulldata;
8618         size_t size;
8620         /* Send data frame to poll STA and check whether this frame is ACKed */
8622         os_memset(&nulldata, 0, sizeof(nulldata));
8624         if (qos) {
8625                 nulldata.hdr.frame_control =
8626                         IEEE80211_FC(WLAN_FC_TYPE_DATA,
8627                                      WLAN_FC_STYPE_QOS_NULL);
8628                 size = sizeof(nulldata);
8629         } else {
8630                 nulldata.hdr.frame_control =
8631                         IEEE80211_FC(WLAN_FC_TYPE_DATA,
8632                                      WLAN_FC_STYPE_NULLFUNC);
8633                 size = sizeof(struct ieee80211_hdr);
8634         }
8636         nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
8637         os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
8638         os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
8639         os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
8641         if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0) < 0)
8642                 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
8643                            "send poll frame");
8646 static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
8647                                 int qos)
8649         struct i802_bss *bss = priv;
8650         struct wpa_driver_nl80211_data *drv = bss->drv;
8651         struct nl_msg *msg;
8653         if (!drv->poll_command_supported) {
8654                 nl80211_send_null_frame(bss, own_addr, addr, qos);
8655                 return;
8656         }
8658         msg = nlmsg_alloc();
8659         if (!msg)
8660                 return;
8662         nl80211_cmd(drv, msg, 0, NL80211_CMD_PROBE_CLIENT);
8664         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8665         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
8667         send_and_recv_msgs(drv, msg, NULL, NULL);
8668         return;
8669  nla_put_failure:
8670         nlmsg_free(msg);
8674 static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
8676         struct nl_msg *msg;
8678         msg = nlmsg_alloc();
8679         if (!msg)
8680                 return -ENOMEM;
8682         nl80211_cmd(bss->drv, msg, 0, NL80211_CMD_SET_POWER_SAVE);
8683         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
8684         NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE,
8685                     enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED);
8686         return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
8687 nla_put_failure:
8688         nlmsg_free(msg);
8689         return -ENOBUFS;
8693 static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
8694                                      int ctwindow)
8696         struct i802_bss *bss = priv;
8698         wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
8699                    "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
8701         if (opp_ps != -1 || ctwindow != -1)
8702                 return -1; /* Not yet supported */
8704         if (legacy_ps == -1)
8705                 return 0;
8706         if (legacy_ps != 0 && legacy_ps != 1)
8707                 return -1; /* Not yet supported */
8709         return nl80211_set_power_save(bss, legacy_ps);
8713 #ifdef CONFIG_TDLS
8715 static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
8716                                   u8 dialog_token, u16 status_code,
8717                                   const u8 *buf, size_t len)
8719         struct i802_bss *bss = priv;
8720         struct wpa_driver_nl80211_data *drv = bss->drv;
8721         struct nl_msg *msg;
8723         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8724                 return -EOPNOTSUPP;
8726         if (!dst)
8727                 return -EINVAL;
8729         msg = nlmsg_alloc();
8730         if (!msg)
8731                 return -ENOMEM;
8733         nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
8734         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8735         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
8736         NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
8737         NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
8738         NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
8739         NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
8741         return send_and_recv_msgs(drv, msg, NULL, NULL);
8743 nla_put_failure:
8744         nlmsg_free(msg);
8745         return -ENOBUFS;
8749 static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
8751         struct i802_bss *bss = priv;
8752         struct wpa_driver_nl80211_data *drv = bss->drv;
8753         struct nl_msg *msg;
8754         enum nl80211_tdls_operation nl80211_oper;
8756         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
8757                 return -EOPNOTSUPP;
8759         switch (oper) {
8760         case TDLS_DISCOVERY_REQ:
8761                 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
8762                 break;
8763         case TDLS_SETUP:
8764                 nl80211_oper = NL80211_TDLS_SETUP;
8765                 break;
8766         case TDLS_TEARDOWN:
8767                 nl80211_oper = NL80211_TDLS_TEARDOWN;
8768                 break;
8769         case TDLS_ENABLE_LINK:
8770                 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
8771                 break;
8772         case TDLS_DISABLE_LINK:
8773                 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
8774                 break;
8775         case TDLS_ENABLE:
8776                 return 0;
8777         case TDLS_DISABLE:
8778                 return 0;
8779         default:
8780                 return -EINVAL;
8781         }
8783         msg = nlmsg_alloc();
8784         if (!msg)
8785                 return -ENOMEM;
8787         nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
8788         NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
8789         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
8790         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
8792         return send_and_recv_msgs(drv, msg, NULL, NULL);
8794 nla_put_failure:
8795         nlmsg_free(msg);
8796         return -ENOBUFS;
8799 #endif /* CONFIG TDLS */
8802 #ifdef ANDROID
8804 typedef struct android_wifi_priv_cmd {
8805         char *buf;
8806         int used_len;
8807         int total_len;
8808 } android_wifi_priv_cmd;
8810 static int drv_errors = 0;
8812 static void wpa_driver_send_hang_msg(struct wpa_driver_nl80211_data *drv)
8814         drv_errors++;
8815         if (drv_errors > DRV_NUMBER_SEQUENTIAL_ERRORS) {
8816                 drv_errors = 0;
8817                 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
8818         }
8822 static int android_priv_cmd(struct i802_bss *bss, const char *cmd)
8824         struct wpa_driver_nl80211_data *drv = bss->drv;
8825         struct ifreq ifr;
8826         android_wifi_priv_cmd priv_cmd;
8827         char buf[MAX_DRV_CMD_SIZE];
8828         int ret;
8830         os_memset(&ifr, 0, sizeof(ifr));
8831         os_memset(&priv_cmd, 0, sizeof(priv_cmd));
8832         os_strlcpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
8834         os_memset(buf, 0, sizeof(buf));
8835         os_strlcpy(buf, cmd, sizeof(buf));
8837         priv_cmd.buf = buf;
8838         priv_cmd.used_len = sizeof(buf);
8839         priv_cmd.total_len = sizeof(buf);
8840         ifr.ifr_data = &priv_cmd;
8842         ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
8843         if (ret < 0) {
8844                 wpa_printf(MSG_ERROR, "%s: failed to issue private commands",
8845                            __func__);
8846                 wpa_driver_send_hang_msg(drv);
8847                 return ret;
8848         }
8850         drv_errors = 0;
8851         return 0;
8855 static int android_pno_start(struct i802_bss *bss,
8856                              struct wpa_driver_scan_params *params)
8858         struct wpa_driver_nl80211_data *drv = bss->drv;
8859         struct ifreq ifr;
8860         android_wifi_priv_cmd priv_cmd;
8861         int ret = 0, i = 0, bp;
8862         char buf[WEXT_PNO_MAX_COMMAND_SIZE];
8864         bp = WEXT_PNOSETUP_HEADER_SIZE;
8865         os_memcpy(buf, WEXT_PNOSETUP_HEADER, bp);
8866         buf[bp++] = WEXT_PNO_TLV_PREFIX;
8867         buf[bp++] = WEXT_PNO_TLV_VERSION;
8868         buf[bp++] = WEXT_PNO_TLV_SUBVERSION;
8869         buf[bp++] = WEXT_PNO_TLV_RESERVED;
8871         while (i < WEXT_PNO_AMOUNT && (size_t) i < params->num_ssids) {
8872                 /* Check that there is enough space needed for 1 more SSID, the
8873                  * other sections and null termination */
8874                 if ((bp + WEXT_PNO_SSID_HEADER_SIZE + MAX_SSID_LEN +
8875                      WEXT_PNO_NONSSID_SECTIONS_SIZE + 1) >= (int) sizeof(buf))
8876                         break;
8877                 wpa_hexdump_ascii(MSG_DEBUG, "For PNO Scan",
8878                                   params->ssids[i].ssid,
8879                                   params->ssids[i].ssid_len);
8880                 buf[bp++] = WEXT_PNO_SSID_SECTION;
8881                 buf[bp++] = params->ssids[i].ssid_len;
8882                 os_memcpy(&buf[bp], params->ssids[i].ssid,
8883                           params->ssids[i].ssid_len);
8884                 bp += params->ssids[i].ssid_len;
8885                 i++;
8886         }
8888         buf[bp++] = WEXT_PNO_SCAN_INTERVAL_SECTION;
8889         os_snprintf(&buf[bp], WEXT_PNO_SCAN_INTERVAL_LENGTH + 1, "%x",
8890                     WEXT_PNO_SCAN_INTERVAL);
8891         bp += WEXT_PNO_SCAN_INTERVAL_LENGTH;
8893         buf[bp++] = WEXT_PNO_REPEAT_SECTION;
8894         os_snprintf(&buf[bp], WEXT_PNO_REPEAT_LENGTH + 1, "%x",
8895                     WEXT_PNO_REPEAT);
8896         bp += WEXT_PNO_REPEAT_LENGTH;
8898         buf[bp++] = WEXT_PNO_MAX_REPEAT_SECTION;
8899         os_snprintf(&buf[bp], WEXT_PNO_MAX_REPEAT_LENGTH + 1, "%x",
8900                     WEXT_PNO_MAX_REPEAT);
8901         bp += WEXT_PNO_MAX_REPEAT_LENGTH + 1;
8903         memset(&ifr, 0, sizeof(ifr));
8904         memset(&priv_cmd, 0, sizeof(priv_cmd));
8905         os_strncpy(ifr.ifr_name, bss->ifname, IFNAMSIZ);
8907         priv_cmd.buf = buf;
8908         priv_cmd.used_len = bp;
8909         priv_cmd.total_len = bp;
8910         ifr.ifr_data = &priv_cmd;
8912         ret = ioctl(drv->global->ioctl_sock, SIOCDEVPRIVATE + 1, &ifr);
8914         if (ret < 0) {
8915                 wpa_printf(MSG_ERROR, "ioctl[SIOCSIWPRIV] (pnosetup): %d",
8916                            ret);
8917                 wpa_driver_send_hang_msg(drv);
8918                 return ret;
8919         }
8921         drv_errors = 0;
8923         return android_priv_cmd(bss, "PNOFORCE 1");
8927 static int android_pno_stop(struct i802_bss *bss)
8929         return android_priv_cmd(bss, "PNOFORCE 0");
8933 static int wpa_driver_nl80211_driver_cmd(void *priv, char *cmd, char *buf,
8934                                          size_t buf_len)
8936         struct i802_bss *bss = priv;
8937         struct wpa_driver_nl80211_data *drv = bss->drv;
8938         int ret = 0;
8940         if (os_strcasecmp(cmd, "STOP") == 0) {
8941                 linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
8942                 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "STOPPED");
8943         } else if (os_strcasecmp(cmd, "START") == 0) {
8944                 linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
8945                 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "STARTED");
8946         } else if (os_strcasecmp(cmd, "MACADDR") == 0) {
8947                 u8 macaddr[ETH_ALEN] = {};
8949                 ret = linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
8950                                          macaddr);
8951                 if (!ret)
8952                         ret = os_snprintf(buf, buf_len,
8953                                           "Macaddr = " MACSTR "\n",
8954                                           MAC2STR(macaddr));
8955         } else if (os_strcasecmp(cmd, "RELOAD") == 0) {
8956                 wpa_msg(drv->ctx, MSG_INFO, WPA_EVENT_DRIVER_STATE "HANGED");
8957         } else {
8958                 wpa_printf(MSG_ERROR, "Unsupported command: %s", cmd);
8959                 ret = -1;
8960         }
8962         return ret;
8965 #endif /* ANDROID */
8968 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
8969         .name = "nl80211",
8970         .desc = "Linux nl80211/cfg80211",
8971         .get_bssid = wpa_driver_nl80211_get_bssid,
8972         .get_ssid = wpa_driver_nl80211_get_ssid,
8973         .set_key = wpa_driver_nl80211_set_key,
8974         .scan2 = wpa_driver_nl80211_scan,
8975         .sched_scan = wpa_driver_nl80211_sched_scan,
8976         .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
8977         .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
8978         .deauthenticate = wpa_driver_nl80211_deauthenticate,
8979         .disassociate = wpa_driver_nl80211_disassociate,
8980         .authenticate = wpa_driver_nl80211_authenticate,
8981         .associate = wpa_driver_nl80211_associate,
8982         .global_init = nl80211_global_init,
8983         .global_deinit = nl80211_global_deinit,
8984         .init2 = wpa_driver_nl80211_init,
8985         .deinit = wpa_driver_nl80211_deinit,
8986         .get_capa = wpa_driver_nl80211_get_capa,
8987         .set_operstate = wpa_driver_nl80211_set_operstate,
8988         .set_supp_port = wpa_driver_nl80211_set_supp_port,
8989         .set_country = wpa_driver_nl80211_set_country,
8990         .set_ap = wpa_driver_nl80211_set_ap,
8991         .if_add = wpa_driver_nl80211_if_add,
8992         .if_remove = wpa_driver_nl80211_if_remove,
8993         .send_mlme = wpa_driver_nl80211_send_mlme,
8994         .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
8995         .sta_add = wpa_driver_nl80211_sta_add,
8996         .sta_remove = wpa_driver_nl80211_sta_remove,
8997         .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
8998         .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
8999 #ifdef HOSTAPD
9000         .hapd_init = i802_init,
9001         .hapd_deinit = i802_deinit,
9002         .set_wds_sta = i802_set_wds_sta,
9003 #endif /* HOSTAPD */
9004 #if defined(HOSTAPD) || defined(CONFIG_AP)
9005         .get_seqnum = i802_get_seqnum,
9006         .flush = i802_flush,
9007         .read_sta_data = i802_read_sta_data,
9008         .get_inact_sec = i802_get_inact_sec,
9009         .sta_clear_stats = i802_sta_clear_stats,
9010         .set_rts = i802_set_rts,
9011         .set_frag = i802_set_frag,
9012         .set_tx_queue_params = i802_set_tx_queue_params,
9013         .set_sta_vlan = i802_set_sta_vlan,
9014         .sta_deauth = i802_sta_deauth,
9015         .sta_disassoc = i802_sta_disassoc,
9016 #endif /* HOSTAPD || CONFIG_AP */
9017         .set_freq = i802_set_freq,
9018         .send_action = wpa_driver_nl80211_send_action,
9019         .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
9020         .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
9021         .cancel_remain_on_channel =
9022         wpa_driver_nl80211_cancel_remain_on_channel,
9023         .probe_req_report = wpa_driver_nl80211_probe_req_report,
9024         .deinit_ap = wpa_driver_nl80211_deinit_ap,
9025         .resume = wpa_driver_nl80211_resume,
9026         .send_ft_action = nl80211_send_ft_action,
9027         .signal_monitor = nl80211_signal_monitor,
9028         .signal_poll = nl80211_signal_poll,
9029         .send_frame = nl80211_send_frame,
9030         .shared_freq = wpa_driver_nl80211_shared_freq,
9031         .set_param = nl80211_set_param,
9032         .get_radio_name = nl80211_get_radio_name,
9033         .add_pmkid = nl80211_add_pmkid,
9034         .remove_pmkid = nl80211_remove_pmkid,
9035         .flush_pmkid = nl80211_flush_pmkid,
9036         .set_rekey_info = nl80211_set_rekey_info,
9037         .poll_client = nl80211_poll_client,
9038         .set_p2p_powersave = nl80211_set_p2p_powersave,
9039 #ifdef CONFIG_TDLS
9040         .send_tdls_mgmt = nl80211_send_tdls_mgmt,
9041         .tdls_oper = nl80211_tdls_oper,
9042 #endif /* CONFIG_TDLS */
9043 #ifdef ANDROID
9044         .driver_cmd = wpa_driver_nl80211_driver_cmd,
9045 #endif /* ANDROID */
9046 };