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