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