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