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