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