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