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