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