nl80211: Implement TDLS callback functions and propagate capabilities
[mech_eap.git] / src / drivers / driver_nl80211.c
1 /*
2  * Driver interaction with Linux nl80211/cfg80211
3  * Copyright (c) 2002-2010, 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 program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * Alternatively, this software may be distributed under the terms of BSD
14  * license.
15  *
16  * See README and COPYING for more details.
17  */
18
19 #include "includes.h"
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <net/if.h>
25 #include <netlink/genl/genl.h>
26 #include <netlink/genl/family.h>
27 #include <netlink/genl/ctrl.h>
28 #include <linux/rtnetlink.h>
29 #include <netpacket/packet.h>
30 #include <linux/filter.h>
31 #include "nl80211_copy.h"
32
33 #include "common.h"
34 #include "eloop.h"
35 #include "utils/list.h"
36 #include "common/ieee802_11_defs.h"
37 #include "common/ieee802_11_common.h"
38 #include "l2_packet/l2_packet.h"
39 #include "netlink.h"
40 #include "linux_ioctl.h"
41 #include "radiotap.h"
42 #include "radiotap_iter.h"
43 #include "rfkill.h"
44 #include "driver.h"
45
46 #ifdef CONFIG_LIBNL20
47 /* libnl 2.0 compatibility code */
48 #define nl_handle nl_sock
49 #define nl80211_handle_alloc nl_socket_alloc_cb
50 #define nl80211_handle_destroy nl_socket_free
51 #else
52 /*
53  * libnl 1.1 has a bug, it tries to allocate socket numbers densely
54  * but when you free a socket again it will mess up its bitmap and
55  * and use the wrong number the next time it needs a socket ID.
56  * Therefore, we wrap the handle alloc/destroy and add our own pid
57  * accounting.
58  */
59 static uint32_t port_bitmap[32] = { 0 };
60
61 static struct nl_handle *nl80211_handle_alloc(void *cb)
62 {
63         struct nl_handle *handle;
64         uint32_t pid = getpid() & 0x3FFFFF;
65         int i;
66
67         handle = nl_handle_alloc_cb(cb);
68
69         for (i = 0; i < 1024; i++) {
70                 if (port_bitmap[i / 32] & (1 << (i % 32)))
71                         continue;
72                 port_bitmap[i / 32] |= 1 << (i % 32);
73                 pid += i << 22;
74                 break;
75         }
76
77         nl_socket_set_local_port(handle, pid);
78
79         return handle;
80 }
81
82 static void nl80211_handle_destroy(struct nl_handle *handle)
83 {
84         uint32_t port = nl_socket_get_local_port(handle);
85
86         port >>= 22;
87         port_bitmap[port / 32] &= ~(1 << (port % 32));
88
89         nl_handle_destroy(handle);
90 }
91
92 static inline int __genl_ctrl_alloc_cache(struct nl_handle *h,
93                                           struct nl_cache **cache)
94 {
95         struct nl_cache *tmp = genl_ctrl_alloc_cache(h);
96         if (!tmp)
97                 return -ENOMEM;
98         *cache = tmp;
99         return 0;
100 }
101 #define genl_ctrl_alloc_cache __genl_ctrl_alloc_cache
102 #endif /* CONFIG_LIBNL20 */
103
104
105 struct nl80211_handles {
106         struct nl_handle *handle;
107         struct nl_cache *cache;
108 };
109
110
111 static int nl_create_handles(struct nl80211_handles *handles, struct nl_cb *cb,
112                              const char *dbg)
113 {
114         if (!handles)
115                 return -1;
116
117         handles->handle = nl80211_handle_alloc(cb);
118         if (handles->handle == NULL) {
119                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
120                            "callbacks (%s)", dbg);
121                 return -1;
122         }
123
124         if (genl_connect(handles->handle)) {
125                 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
126                            "netlink (%s)", dbg);
127                 goto err;
128         }
129
130         if (genl_ctrl_alloc_cache(handles->handle, &handles->cache) < 0) {
131                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
132                            "netlink cache (%s)", dbg);
133                 goto err;
134         }
135
136         return 0;
137 err:
138         nl80211_handle_destroy(handles->handle);
139         return -1;
140 }
141
142
143 static void nl_destroy_handles(struct nl80211_handles *handles)
144 {
145         if (handles->handle == NULL)
146                 return;
147         nl_cache_free(handles->cache);
148         nl80211_handle_destroy(handles->handle);
149         handles->handle = NULL;
150 }
151
152
153 #ifndef IFF_LOWER_UP
154 #define IFF_LOWER_UP   0x10000         /* driver signals L1 up         */
155 #endif
156 #ifndef IFF_DORMANT
157 #define IFF_DORMANT    0x20000         /* driver signals dormant       */
158 #endif
159
160 #ifndef IF_OPER_DORMANT
161 #define IF_OPER_DORMANT 5
162 #endif
163 #ifndef IF_OPER_UP
164 #define IF_OPER_UP 6
165 #endif
166
167 struct nl80211_global {
168         struct dl_list interfaces;
169         int if_add_ifindex;
170         struct netlink_data *netlink;
171         struct nl_cb *nl_cb;
172         struct nl80211_handles nl;
173         struct genl_family *nl80211;
174         int ioctl_sock; /* socket for ioctl() use */
175 };
176
177 static void nl80211_global_deinit(void *priv);
178 static void wpa_driver_nl80211_deinit(void *priv);
179
180 struct i802_bss {
181         struct wpa_driver_nl80211_data *drv;
182         struct i802_bss *next;
183         int ifindex;
184         char ifname[IFNAMSIZ + 1];
185         char brname[IFNAMSIZ];
186         unsigned int beacon_set:1;
187         unsigned int added_if_into_bridge:1;
188         unsigned int added_bridge:1;
189 };
190
191 struct wpa_driver_nl80211_data {
192         struct nl80211_global *global;
193         struct dl_list list;
194         u8 addr[ETH_ALEN];
195         char phyname[32];
196         void *ctx;
197         int ifindex;
198         int if_removed;
199         int if_disabled;
200         int ignore_if_down_event;
201         struct rfkill_data *rfkill;
202         struct wpa_driver_capa capa;
203         int has_capability;
204
205         int operstate;
206
207         int scan_complete_events;
208
209         struct nl80211_handles nl_event, nl_preq;
210
211         u8 auth_bssid[ETH_ALEN];
212         u8 bssid[ETH_ALEN];
213         int associated;
214         u8 ssid[32];
215         size_t ssid_len;
216         enum nl80211_iftype nlmode;
217         enum nl80211_iftype ap_scan_as_station;
218         unsigned int assoc_freq;
219
220         int monitor_sock;
221         int monitor_ifidx;
222         int no_monitor_iface_capab;
223         int disable_11b_rates;
224
225         unsigned int pending_remain_on_chan:1;
226         unsigned int in_interface_list:1;
227
228         u64 remain_on_chan_cookie;
229         u64 send_action_cookie;
230
231         unsigned int last_mgmt_freq;
232         unsigned int ap_oper_freq;
233
234         struct wpa_driver_scan_filter *filter_ssids;
235         size_t num_filter_ssids;
236
237         struct i802_bss first_bss;
238
239 #ifdef CONFIG_AP
240         struct l2_packet_data *l2;
241 #endif /* CONFIG_AP */
242
243 #ifdef HOSTAPD
244         int eapol_sock; /* socket for EAPOL frames */
245
246         int default_if_indices[16];
247         int *if_indices;
248         int num_if_indices;
249
250         int last_freq;
251         int last_freq_ht;
252 #endif /* HOSTAPD */
253 };
254
255
256 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
257                                             void *timeout_ctx);
258 static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
259                                        enum nl80211_iftype nlmode);
260 static int
261 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
262 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
263                                    const u8 *addr, int cmd, u16 reason_code,
264                                    int local_state_change);
265 static void nl80211_remove_monitor_interface(
266         struct wpa_driver_nl80211_data *drv);
267 static int nl80211_send_frame_cmd(struct wpa_driver_nl80211_data *drv,
268                                   unsigned int freq, unsigned int wait,
269                                   const u8 *buf, size_t buf_len, u64 *cookie);
270 static int wpa_driver_nl80211_probe_req_report(void *priv, int report);
271
272 #ifdef HOSTAPD
273 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
274 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
275 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
276 static int wpa_driver_nl80211_if_remove(void *priv,
277                                         enum wpa_driver_if_type type,
278                                         const char *ifname);
279 #else /* HOSTAPD */
280 static inline void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
281 {
282 }
283
284 static inline void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
285 {
286 }
287
288 static inline int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
289 {
290         return 0;
291 }
292 #endif /* HOSTAPD */
293
294 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
295 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
296                                      int ifindex, int disabled);
297
298 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
299
300
301 static int is_ap_interface(enum nl80211_iftype nlmode)
302 {
303         return (nlmode == NL80211_IFTYPE_AP ||
304                 nlmode == NL80211_IFTYPE_P2P_GO);
305 }
306
307
308 static int is_sta_interface(enum nl80211_iftype nlmode)
309 {
310         return (nlmode == NL80211_IFTYPE_STATION ||
311                 nlmode == NL80211_IFTYPE_P2P_CLIENT);
312 }
313
314
315 struct nl80211_bss_info_arg {
316         struct wpa_driver_nl80211_data *drv;
317         struct wpa_scan_results *res;
318         unsigned int assoc_freq;
319         u8 assoc_bssid[ETH_ALEN];
320 };
321
322 static int bss_info_handler(struct nl_msg *msg, void *arg);
323
324
325 /* nl80211 code */
326 static int ack_handler(struct nl_msg *msg, void *arg)
327 {
328         int *err = arg;
329         *err = 0;
330         return NL_STOP;
331 }
332
333 static int finish_handler(struct nl_msg *msg, void *arg)
334 {
335         int *ret = arg;
336         *ret = 0;
337         return NL_SKIP;
338 }
339
340 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
341                          void *arg)
342 {
343         int *ret = arg;
344         *ret = err->error;
345         return NL_SKIP;
346 }
347
348
349 static int no_seq_check(struct nl_msg *msg, void *arg)
350 {
351         return NL_OK;
352 }
353
354
355 static int send_and_recv(struct wpa_driver_nl80211_data *drv,
356                          struct nl_handle *nl_handle, struct nl_msg *msg,
357                          int (*valid_handler)(struct nl_msg *, void *),
358                          void *valid_data)
359 {
360         struct nl_cb *cb;
361         int err = -ENOMEM;
362
363         cb = nl_cb_clone(drv->global->nl_cb);
364         if (!cb)
365                 goto out;
366
367         err = nl_send_auto_complete(nl_handle, msg);
368         if (err < 0)
369                 goto out;
370
371         err = 1;
372
373         nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
374         nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
375         nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
376
377         if (valid_handler)
378                 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
379                           valid_handler, valid_data);
380
381         while (err > 0)
382                 nl_recvmsgs(nl_handle, cb);
383  out:
384         nl_cb_put(cb);
385         nlmsg_free(msg);
386         return err;
387 }
388
389
390 static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
391                               struct nl_msg *msg,
392                               int (*valid_handler)(struct nl_msg *, void *),
393                               void *valid_data)
394 {
395         return send_and_recv(drv, drv->global->nl.handle, msg, valid_handler,
396                              valid_data);
397 }
398
399
400 struct family_data {
401         const char *group;
402         int id;
403 };
404
405
406 static int family_handler(struct nl_msg *msg, void *arg)
407 {
408         struct family_data *res = arg;
409         struct nlattr *tb[CTRL_ATTR_MAX + 1];
410         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
411         struct nlattr *mcgrp;
412         int i;
413
414         nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
415                   genlmsg_attrlen(gnlh, 0), NULL);
416         if (!tb[CTRL_ATTR_MCAST_GROUPS])
417                 return NL_SKIP;
418
419         nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
420                 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
421                 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
422                           nla_len(mcgrp), NULL);
423                 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
424                     !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
425                     os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
426                                res->group,
427                                nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
428                         continue;
429                 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
430                 break;
431         };
432
433         return NL_SKIP;
434 }
435
436
437 static int nl_get_multicast_id(struct wpa_driver_nl80211_data *drv,
438                                const char *family, const char *group)
439 {
440         struct nl_msg *msg;
441         int ret = -1;
442         struct family_data res = { group, -ENOENT };
443
444         msg = nlmsg_alloc();
445         if (!msg)
446                 return -ENOMEM;
447         genlmsg_put(msg, 0, 0,
448                     genl_ctrl_resolve(drv->global->nl.handle, "nlctrl"),
449                     0, 0, CTRL_CMD_GETFAMILY, 0);
450         NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
451
452         ret = send_and_recv_msgs(drv, msg, family_handler, &res);
453         msg = NULL;
454         if (ret == 0)
455                 ret = res.id;
456
457 nla_put_failure:
458         nlmsg_free(msg);
459         return ret;
460 }
461
462
463 static void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
464                           struct nl_msg *msg, int flags, uint8_t cmd)
465 {
466         return genlmsg_put(msg, 0, 0, genl_family_get_id(drv->global->nl80211),
467                            0, flags, cmd, 0);
468 }
469
470
471 static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
472 {
473         struct i802_bss *bss = priv;
474         struct wpa_driver_nl80211_data *drv = bss->drv;
475         if (!drv->associated)
476                 return -1;
477         os_memcpy(bssid, drv->bssid, ETH_ALEN);
478         return 0;
479 }
480
481
482 static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
483 {
484         struct i802_bss *bss = priv;
485         struct wpa_driver_nl80211_data *drv = bss->drv;
486         if (!drv->associated)
487                 return -1;
488         os_memcpy(ssid, drv->ssid, drv->ssid_len);
489         return drv->ssid_len;
490 }
491
492
493 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
494                                           char *buf, size_t len, int del)
495 {
496         union wpa_event_data event;
497
498         os_memset(&event, 0, sizeof(event));
499         if (len > sizeof(event.interface_status.ifname))
500                 len = sizeof(event.interface_status.ifname) - 1;
501         os_memcpy(event.interface_status.ifname, buf, len);
502         event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
503                 EVENT_INTERFACE_ADDED;
504
505         wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
506                    del ? "DEL" : "NEW",
507                    event.interface_status.ifname,
508                    del ? "removed" : "added");
509
510         if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) {
511                 if (del)
512                         drv->if_removed = 1;
513                 else
514                         drv->if_removed = 0;
515         }
516
517         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
518 }
519
520
521 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
522                                          u8 *buf, size_t len)
523 {
524         int attrlen, rta_len;
525         struct rtattr *attr;
526
527         attrlen = len;
528         attr = (struct rtattr *) buf;
529
530         rta_len = RTA_ALIGN(sizeof(struct rtattr));
531         while (RTA_OK(attr, attrlen)) {
532                 if (attr->rta_type == IFLA_IFNAME) {
533                         if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname)
534                             == 0)
535                                 return 1;
536                         else
537                                 break;
538                 }
539                 attr = RTA_NEXT(attr, attrlen);
540         }
541
542         return 0;
543 }
544
545
546 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
547                                           int ifindex, u8 *buf, size_t len)
548 {
549         if (drv->ifindex == ifindex)
550                 return 1;
551
552         if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
553                 drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname);
554                 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
555                            "interface");
556                 wpa_driver_nl80211_finish_drv_init(drv);
557                 return 1;
558         }
559
560         return 0;
561 }
562
563
564 static struct wpa_driver_nl80211_data *
565 nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
566 {
567         struct wpa_driver_nl80211_data *drv;
568         dl_list_for_each(drv, &global->interfaces,
569                          struct wpa_driver_nl80211_data, list) {
570                 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
571                     have_ifidx(drv, idx))
572                         return drv;
573         }
574         return NULL;
575 }
576
577
578 static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
579                                                  struct ifinfomsg *ifi,
580                                                  u8 *buf, size_t len)
581 {
582         struct nl80211_global *global = ctx;
583         struct wpa_driver_nl80211_data *drv;
584         int attrlen, rta_len;
585         struct rtattr *attr;
586         u32 brid = 0;
587
588         drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
589         if (!drv) {
590                 wpa_printf(MSG_DEBUG, "nl80211: Ignore event for foreign "
591                            "ifindex %d", ifi->ifi_index);
592                 return;
593         }
594
595         wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
596                    "(%s%s%s%s)",
597                    drv->operstate, ifi->ifi_flags,
598                    (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
599                    (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
600                    (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
601                    (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
602
603         if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
604                 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
605                 if (drv->ignore_if_down_event) {
606                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
607                                    "event generated by mode change");
608                         drv->ignore_if_down_event = 0;
609                 } else {
610                         drv->if_disabled = 1;
611                         wpa_supplicant_event(drv->ctx,
612                                              EVENT_INTERFACE_DISABLED, NULL);
613                 }
614         }
615
616         if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
617                 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
618                 drv->if_disabled = 0;
619                 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
620         }
621
622         /*
623          * Some drivers send the association event before the operup event--in
624          * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
625          * fails. This will hit us when wpa_supplicant does not need to do
626          * IEEE 802.1X authentication
627          */
628         if (drv->operstate == 1 &&
629             (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
630             !(ifi->ifi_flags & IFF_RUNNING))
631                 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
632                                        -1, IF_OPER_UP);
633
634         attrlen = len;
635         attr = (struct rtattr *) buf;
636         rta_len = RTA_ALIGN(sizeof(struct rtattr));
637         while (RTA_OK(attr, attrlen)) {
638                 if (attr->rta_type == IFLA_IFNAME) {
639                         wpa_driver_nl80211_event_link(
640                                 drv,
641                                 ((char *) attr) + rta_len,
642                                 attr->rta_len - rta_len, 0);
643                 } else if (attr->rta_type == IFLA_MASTER)
644                         brid = nla_get_u32((struct nlattr *) attr);
645                 attr = RTA_NEXT(attr, attrlen);
646         }
647
648         if (ifi->ifi_family == AF_BRIDGE && brid) {
649                 /* device has been added to bridge */
650                 char namebuf[IFNAMSIZ];
651                 if_indextoname(brid, namebuf);
652                 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
653                            brid, namebuf);
654                 add_ifidx(drv, brid);
655         }
656 }
657
658
659 static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
660                                                  struct ifinfomsg *ifi,
661                                                  u8 *buf, size_t len)
662 {
663         struct nl80211_global *global = ctx;
664         struct wpa_driver_nl80211_data *drv;
665         int attrlen, rta_len;
666         struct rtattr *attr;
667         u32 brid = 0;
668
669         drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
670         if (!drv) {
671                 wpa_printf(MSG_DEBUG, "nl80211: Ignore dellink event for "
672                            "foreign ifindex %d", ifi->ifi_index);
673                 return;
674         }
675
676         attrlen = len;
677         attr = (struct rtattr *) buf;
678
679         rta_len = RTA_ALIGN(sizeof(struct rtattr));
680         while (RTA_OK(attr, attrlen)) {
681                 if (attr->rta_type == IFLA_IFNAME) {
682                         wpa_driver_nl80211_event_link(
683                                 drv,
684                                 ((char *) attr) + rta_len,
685                                 attr->rta_len - rta_len, 1);
686                 } else if (attr->rta_type == IFLA_MASTER)
687                         brid = nla_get_u32((struct nlattr *) attr);
688                 attr = RTA_NEXT(attr, attrlen);
689         }
690
691         if (ifi->ifi_family == AF_BRIDGE && brid) {
692                 /* device has been removed from bridge */
693                 char namebuf[IFNAMSIZ];
694                 if_indextoname(brid, namebuf);
695                 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
696                            "%s", brid, namebuf);
697                 del_ifidx(drv, brid);
698         }
699 }
700
701
702 static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
703                             const u8 *frame, size_t len)
704 {
705         const struct ieee80211_mgmt *mgmt;
706         union wpa_event_data event;
707
708         mgmt = (const struct ieee80211_mgmt *) frame;
709         if (len < 24 + sizeof(mgmt->u.auth)) {
710                 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
711                            "frame");
712                 return;
713         }
714
715         os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
716         os_memset(&event, 0, sizeof(event));
717         os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
718         event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
719         event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
720         if (len > 24 + sizeof(mgmt->u.auth)) {
721                 event.auth.ies = mgmt->u.auth.variable;
722                 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
723         }
724
725         wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
726 }
727
728
729 static unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
730 {
731         struct nl_msg *msg;
732         int ret;
733         struct nl80211_bss_info_arg arg;
734
735         os_memset(&arg, 0, sizeof(arg));
736         msg = nlmsg_alloc();
737         if (!msg)
738                 goto nla_put_failure;
739
740         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
741         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
742
743         arg.drv = drv;
744         ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
745         msg = NULL;
746         if (ret == 0) {
747                 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
748                            "associated BSS from scan results: %u MHz",
749                            arg.assoc_freq);
750                 return arg.assoc_freq ? arg.assoc_freq : drv->assoc_freq;
751         }
752         wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
753                    "(%s)", ret, strerror(-ret));
754 nla_put_failure:
755         nlmsg_free(msg);
756         return drv->assoc_freq;
757 }
758
759
760 static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
761                             const u8 *frame, size_t len)
762 {
763         const struct ieee80211_mgmt *mgmt;
764         union wpa_event_data event;
765         u16 status;
766
767         mgmt = (const struct ieee80211_mgmt *) frame;
768         if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
769                 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
770                            "frame");
771                 return;
772         }
773
774         status = le_to_host16(mgmt->u.assoc_resp.status_code);
775         if (status != WLAN_STATUS_SUCCESS) {
776                 os_memset(&event, 0, sizeof(event));
777                 event.assoc_reject.bssid = mgmt->bssid;
778                 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
779                         event.assoc_reject.resp_ies =
780                                 (u8 *) mgmt->u.assoc_resp.variable;
781                         event.assoc_reject.resp_ies_len =
782                                 len - 24 - sizeof(mgmt->u.assoc_resp);
783                 }
784                 event.assoc_reject.status_code = status;
785
786                 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
787                 return;
788         }
789
790         drv->associated = 1;
791         os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
792
793         os_memset(&event, 0, sizeof(event));
794         if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
795                 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
796                 event.assoc_info.resp_ies_len =
797                         len - 24 - sizeof(mgmt->u.assoc_resp);
798         }
799
800         event.assoc_info.freq = drv->assoc_freq;
801
802         wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
803 }
804
805
806 static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
807                                enum nl80211_commands cmd, struct nlattr *status,
808                                struct nlattr *addr, struct nlattr *req_ie,
809                                struct nlattr *resp_ie)
810 {
811         union wpa_event_data event;
812
813         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
814                 /*
815                  * Avoid reporting two association events that would confuse
816                  * the core code.
817                  */
818                 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
819                            "when using userspace SME", cmd);
820                 return;
821         }
822
823         os_memset(&event, 0, sizeof(event));
824         if (cmd == NL80211_CMD_CONNECT &&
825             nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
826                 if (addr)
827                         event.assoc_reject.bssid = nla_data(addr);
828                 if (resp_ie) {
829                         event.assoc_reject.resp_ies = nla_data(resp_ie);
830                         event.assoc_reject.resp_ies_len = nla_len(resp_ie);
831                 }
832                 event.assoc_reject.status_code = nla_get_u16(status);
833                 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
834                 return;
835         }
836
837         drv->associated = 1;
838         if (addr)
839                 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
840
841         if (req_ie) {
842                 event.assoc_info.req_ies = nla_data(req_ie);
843                 event.assoc_info.req_ies_len = nla_len(req_ie);
844         }
845         if (resp_ie) {
846                 event.assoc_info.resp_ies = nla_data(resp_ie);
847                 event.assoc_info.resp_ies_len = nla_len(resp_ie);
848         }
849
850         event.assoc_info.freq = nl80211_get_assoc_freq(drv);
851
852         wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
853 }
854
855
856 static void mlme_event_disconnect(struct wpa_driver_nl80211_data *drv,
857                                   struct nlattr *reason, struct nlattr *addr)
858 {
859         union wpa_event_data data;
860
861         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
862                 /*
863                  * Avoid reporting two disassociation events that could
864                  * confuse the core code.
865                  */
866                 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
867                            "event when using userspace SME");
868                 return;
869         }
870
871         drv->associated = 0;
872         os_memset(&data, 0, sizeof(data));
873         if (reason)
874                 data.disassoc_info.reason_code = nla_get_u16(reason);
875         wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, &data);
876 }
877
878
879 static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
880                                enum nl80211_commands cmd, struct nlattr *addr)
881 {
882         union wpa_event_data event;
883         enum wpa_event_type ev;
884
885         if (nla_len(addr) != ETH_ALEN)
886                 return;
887
888         wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
889                    cmd, MAC2STR((u8 *) nla_data(addr)));
890
891         if (cmd == NL80211_CMD_AUTHENTICATE)
892                 ev = EVENT_AUTH_TIMED_OUT;
893         else if (cmd == NL80211_CMD_ASSOCIATE)
894                 ev = EVENT_ASSOC_TIMED_OUT;
895         else
896                 return;
897
898         os_memset(&event, 0, sizeof(event));
899         os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
900         wpa_supplicant_event(drv->ctx, ev, &event);
901 }
902
903
904 static void mlme_event_mgmt(struct wpa_driver_nl80211_data *drv,
905                             struct nlattr *freq, const u8 *frame, size_t len)
906 {
907         const struct ieee80211_mgmt *mgmt;
908         union wpa_event_data event;
909         u16 fc, stype;
910
911         mgmt = (const struct ieee80211_mgmt *) frame;
912         if (len < 24) {
913                 wpa_printf(MSG_DEBUG, "nl80211: Too short action frame");
914                 return;
915         }
916
917         fc = le_to_host16(mgmt->frame_control);
918         stype = WLAN_FC_GET_STYPE(fc);
919
920         os_memset(&event, 0, sizeof(event));
921         if (freq) {
922                 event.rx_action.freq = nla_get_u32(freq);
923                 drv->last_mgmt_freq = event.rx_action.freq;
924         }
925         if (stype == WLAN_FC_STYPE_ACTION) {
926                 event.rx_action.da = mgmt->da;
927                 event.rx_action.sa = mgmt->sa;
928                 event.rx_action.bssid = mgmt->bssid;
929                 event.rx_action.category = mgmt->u.action.category;
930                 event.rx_action.data = &mgmt->u.action.category + 1;
931                 event.rx_action.len = frame + len - event.rx_action.data;
932                 wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event);
933         } else {
934                 event.rx_mgmt.frame = frame;
935                 event.rx_mgmt.frame_len = len;
936                 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
937         }
938 }
939
940
941 static void mlme_event_action_tx_status(struct wpa_driver_nl80211_data *drv,
942                                         struct nlattr *cookie, const u8 *frame,
943                                         size_t len, struct nlattr *ack)
944 {
945         union wpa_event_data event;
946         const struct ieee80211_hdr *hdr;
947         u16 fc;
948         u64 cookie_val;
949
950         if (!cookie)
951                 return;
952
953         cookie_val = nla_get_u64(cookie);
954         wpa_printf(MSG_DEBUG, "nl80211: Action TX status: cookie=0%llx%s "
955                    "(ack=%d)",
956                    (long long unsigned int) cookie_val,
957                    cookie_val == drv->send_action_cookie ?
958                    " (match)" : " (unknown)", ack != NULL);
959         if (cookie_val != drv->send_action_cookie)
960                 return;
961
962         hdr = (const struct ieee80211_hdr *) frame;
963         fc = le_to_host16(hdr->frame_control);
964
965         os_memset(&event, 0, sizeof(event));
966         event.tx_status.type = WLAN_FC_GET_TYPE(fc);
967         event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
968         event.tx_status.dst = hdr->addr1;
969         event.tx_status.data = frame;
970         event.tx_status.data_len = len;
971         event.tx_status.ack = ack != NULL;
972         wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event);
973 }
974
975
976 static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv,
977                                        enum wpa_event_type type,
978                                        const u8 *frame, size_t len)
979 {
980         const struct ieee80211_mgmt *mgmt;
981         union wpa_event_data event;
982         const u8 *bssid = NULL;
983         u16 reason_code = 0;
984
985         mgmt = (const struct ieee80211_mgmt *) frame;
986         if (len >= 24) {
987                 bssid = mgmt->bssid;
988
989                 if (drv->associated != 0 &&
990                     os_memcmp(bssid, drv->bssid, ETH_ALEN) != 0 &&
991                     os_memcmp(bssid, drv->auth_bssid, ETH_ALEN) != 0) {
992                         /*
993                          * We have presumably received this deauth as a
994                          * response to a clear_state_mismatch() outgoing
995                          * deauth.  Don't let it take us offline!
996                          */
997                         wpa_printf(MSG_DEBUG, "nl80211: Deauth received "
998                                    "from Unknown BSSID " MACSTR " -- ignoring",
999                                    MAC2STR(bssid));
1000                         return;
1001                 }
1002         }
1003
1004         drv->associated = 0;
1005         os_memset(&event, 0, sizeof(event));
1006
1007         /* Note: Same offset for Reason Code in both frame subtypes */
1008         if (len >= 24 + sizeof(mgmt->u.deauth))
1009                 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1010
1011         if (type == EVENT_DISASSOC) {
1012                 event.disassoc_info.addr = bssid;
1013                 event.disassoc_info.reason_code = reason_code;
1014                 if (frame + len > mgmt->u.disassoc.variable) {
1015                         event.disassoc_info.ie = mgmt->u.disassoc.variable;
1016                         event.disassoc_info.ie_len = frame + len -
1017                                 mgmt->u.disassoc.variable;
1018                 }
1019         } else {
1020                 event.deauth_info.addr = bssid;
1021                 event.deauth_info.reason_code = reason_code;
1022                 if (frame + len > mgmt->u.deauth.variable) {
1023                         event.deauth_info.ie = mgmt->u.deauth.variable;
1024                         event.deauth_info.ie_len = frame + len -
1025                                 mgmt->u.deauth.variable;
1026                 }
1027         }
1028
1029         wpa_supplicant_event(drv->ctx, type, &event);
1030 }
1031
1032
1033 static void mlme_event_unprot_disconnect(struct wpa_driver_nl80211_data *drv,
1034                                          enum wpa_event_type type,
1035                                          const u8 *frame, size_t len)
1036 {
1037         const struct ieee80211_mgmt *mgmt;
1038         union wpa_event_data event;
1039         u16 reason_code = 0;
1040
1041         if (len < 24)
1042                 return;
1043
1044         mgmt = (const struct ieee80211_mgmt *) frame;
1045
1046         os_memset(&event, 0, sizeof(event));
1047         /* Note: Same offset for Reason Code in both frame subtypes */
1048         if (len >= 24 + sizeof(mgmt->u.deauth))
1049                 reason_code = le_to_host16(mgmt->u.deauth.reason_code);
1050
1051         if (type == EVENT_UNPROT_DISASSOC) {
1052                 event.unprot_disassoc.sa = mgmt->sa;
1053                 event.unprot_disassoc.da = mgmt->da;
1054                 event.unprot_disassoc.reason_code = reason_code;
1055         } else {
1056                 event.unprot_deauth.sa = mgmt->sa;
1057                 event.unprot_deauth.da = mgmt->da;
1058                 event.unprot_deauth.reason_code = reason_code;
1059         }
1060
1061         wpa_supplicant_event(drv->ctx, type, &event);
1062 }
1063
1064
1065 static void mlme_event(struct wpa_driver_nl80211_data *drv,
1066                        enum nl80211_commands cmd, struct nlattr *frame,
1067                        struct nlattr *addr, struct nlattr *timed_out,
1068                        struct nlattr *freq, struct nlattr *ack,
1069                        struct nlattr *cookie)
1070 {
1071         if (timed_out && addr) {
1072                 mlme_timeout_event(drv, cmd, addr);
1073                 return;
1074         }
1075
1076         if (frame == NULL) {
1077                 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
1078                            "data", cmd);
1079                 return;
1080         }
1081
1082         wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
1083         wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
1084                     nla_data(frame), nla_len(frame));
1085
1086         switch (cmd) {
1087         case NL80211_CMD_AUTHENTICATE:
1088                 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
1089                 break;
1090         case NL80211_CMD_ASSOCIATE:
1091                 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
1092                 break;
1093         case NL80211_CMD_DEAUTHENTICATE:
1094                 mlme_event_deauth_disassoc(drv, EVENT_DEAUTH,
1095                                            nla_data(frame), nla_len(frame));
1096                 break;
1097         case NL80211_CMD_DISASSOCIATE:
1098                 mlme_event_deauth_disassoc(drv, EVENT_DISASSOC,
1099                                            nla_data(frame), nla_len(frame));
1100                 break;
1101         case NL80211_CMD_FRAME:
1102                 mlme_event_mgmt(drv, freq, nla_data(frame), nla_len(frame));
1103                 break;
1104         case NL80211_CMD_FRAME_TX_STATUS:
1105                 mlme_event_action_tx_status(drv, cookie, nla_data(frame),
1106                                             nla_len(frame), ack);
1107                 break;
1108         case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1109                 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DEAUTH,
1110                                              nla_data(frame), nla_len(frame));
1111                 break;
1112         case NL80211_CMD_UNPROT_DISASSOCIATE:
1113                 mlme_event_unprot_disconnect(drv, EVENT_UNPROT_DISASSOC,
1114                                              nla_data(frame), nla_len(frame));
1115                 break;
1116         default:
1117                 break;
1118         }
1119 }
1120
1121
1122 static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
1123                                            struct nlattr *tb[])
1124 {
1125         union wpa_event_data data;
1126
1127         wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
1128         os_memset(&data, 0, sizeof(data));
1129         if (tb[NL80211_ATTR_MAC]) {
1130                 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
1131                             nla_data(tb[NL80211_ATTR_MAC]),
1132                             nla_len(tb[NL80211_ATTR_MAC]));
1133                 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
1134         }
1135         if (tb[NL80211_ATTR_KEY_SEQ]) {
1136                 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
1137                             nla_data(tb[NL80211_ATTR_KEY_SEQ]),
1138                             nla_len(tb[NL80211_ATTR_KEY_SEQ]));
1139         }
1140         if (tb[NL80211_ATTR_KEY_TYPE]) {
1141                 enum nl80211_key_type key_type =
1142                         nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
1143                 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
1144                 if (key_type == NL80211_KEYTYPE_PAIRWISE)
1145                         data.michael_mic_failure.unicast = 1;
1146         } else
1147                 data.michael_mic_failure.unicast = 1;
1148
1149         if (tb[NL80211_ATTR_KEY_IDX]) {
1150                 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
1151                 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
1152         }
1153
1154         wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
1155 }
1156
1157
1158 static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
1159                                  struct nlattr *tb[])
1160 {
1161         if (tb[NL80211_ATTR_MAC] == NULL) {
1162                 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
1163                            "event");
1164                 return;
1165         }
1166         os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
1167         drv->associated = 1;
1168         wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
1169                    MAC2STR(drv->bssid));
1170
1171         wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
1172 }
1173
1174
1175 static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
1176                                          int cancel_event, struct nlattr *tb[])
1177 {
1178         unsigned int freq, chan_type, duration;
1179         union wpa_event_data data;
1180         u64 cookie;
1181
1182         if (tb[NL80211_ATTR_WIPHY_FREQ])
1183                 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1184         else
1185                 freq = 0;
1186
1187         if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1188                 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1189         else
1190                 chan_type = 0;
1191
1192         if (tb[NL80211_ATTR_DURATION])
1193                 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
1194         else
1195                 duration = 0;
1196
1197         if (tb[NL80211_ATTR_COOKIE])
1198                 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
1199         else
1200                 cookie = 0;
1201
1202         wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
1203                    "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
1204                    cancel_event, freq, chan_type, duration,
1205                    (long long unsigned int) cookie,
1206                    cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
1207
1208         if (cookie != drv->remain_on_chan_cookie)
1209                 return; /* not for us */
1210
1211         if (cancel_event)
1212                 drv->pending_remain_on_chan = 0;
1213
1214         os_memset(&data, 0, sizeof(data));
1215         data.remain_on_channel.freq = freq;
1216         data.remain_on_channel.duration = duration;
1217         wpa_supplicant_event(drv->ctx, cancel_event ?
1218                              EVENT_CANCEL_REMAIN_ON_CHANNEL :
1219                              EVENT_REMAIN_ON_CHANNEL, &data);
1220 }
1221
1222
1223 static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
1224                             struct nlattr *tb[])
1225 {
1226         union wpa_event_data event;
1227         struct nlattr *nl;
1228         int rem;
1229         struct scan_info *info;
1230 #define MAX_REPORT_FREQS 50
1231         int freqs[MAX_REPORT_FREQS];
1232         int num_freqs = 0;
1233
1234         os_memset(&event, 0, sizeof(event));
1235         info = &event.scan_info;
1236         info->aborted = aborted;
1237
1238         if (tb[NL80211_ATTR_SCAN_SSIDS]) {
1239                 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
1240                         struct wpa_driver_scan_ssid *s =
1241                                 &info->ssids[info->num_ssids];
1242                         s->ssid = nla_data(nl);
1243                         s->ssid_len = nla_len(nl);
1244                         info->num_ssids++;
1245                         if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
1246                                 break;
1247                 }
1248         }
1249         if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
1250                 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
1251                 {
1252                         freqs[num_freqs] = nla_get_u32(nl);
1253                         num_freqs++;
1254                         if (num_freqs == MAX_REPORT_FREQS - 1)
1255                                 break;
1256                 }
1257                 info->freqs = freqs;
1258                 info->num_freqs = num_freqs;
1259         }
1260         wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
1261 }
1262
1263
1264 static int get_link_signal(struct nl_msg *msg, void *arg)
1265 {
1266         struct nlattr *tb[NL80211_ATTR_MAX + 1];
1267         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1268         struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1269         static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1270                 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1271         };
1272         struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1273         static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1274                 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1275                 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1276                 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1277                 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1278         };
1279         struct wpa_signal_info *sig_change = arg;
1280
1281         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1282                   genlmsg_attrlen(gnlh, 0), NULL);
1283         if (!tb[NL80211_ATTR_STA_INFO] ||
1284             nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1285                              tb[NL80211_ATTR_STA_INFO], policy))
1286                 return NL_SKIP;
1287         if (!sinfo[NL80211_STA_INFO_SIGNAL])
1288                 return NL_SKIP;
1289
1290         sig_change->current_signal =
1291                 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1292
1293         if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1294                 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1295                                      sinfo[NL80211_STA_INFO_TX_BITRATE],
1296                                      rate_policy)) {
1297                         sig_change->current_txrate = 0;
1298                 } else {
1299                         if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1300                                 sig_change->current_txrate =
1301                                         nla_get_u16(rinfo[
1302                                              NL80211_RATE_INFO_BITRATE]) * 100;
1303                         }
1304                 }
1305         }
1306
1307         return NL_SKIP;
1308 }
1309
1310
1311 static int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1312                                    struct wpa_signal_info *sig)
1313 {
1314         struct nl_msg *msg;
1315
1316         sig->current_signal = -9999;
1317         sig->current_txrate = 0;
1318
1319         msg = nlmsg_alloc();
1320         if (!msg)
1321                 return -ENOMEM;
1322
1323         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
1324
1325         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1326         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
1327
1328         return send_and_recv_msgs(drv, msg, get_link_signal, sig);
1329  nla_put_failure:
1330         return -ENOBUFS;
1331 }
1332
1333
1334 static int get_link_noise(struct nl_msg *msg, void *arg)
1335 {
1336         struct nlattr *tb[NL80211_ATTR_MAX + 1];
1337         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1338         struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1339         static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1340                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1341                 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1342         };
1343         struct wpa_signal_info *sig_change = arg;
1344
1345         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1346                   genlmsg_attrlen(gnlh, 0), NULL);
1347
1348         if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1349                 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1350                 return NL_SKIP;
1351         }
1352
1353         if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1354                              tb[NL80211_ATTR_SURVEY_INFO],
1355                              survey_policy)) {
1356                 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1357                            "attributes!");
1358                 return NL_SKIP;
1359         }
1360
1361         if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1362                 return NL_SKIP;
1363
1364         if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1365             sig_change->frequency)
1366                 return NL_SKIP;
1367
1368         if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1369                 return NL_SKIP;
1370
1371         sig_change->current_noise =
1372                 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1373
1374         return NL_SKIP;
1375 }
1376
1377
1378 static int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1379                                   struct wpa_signal_info *sig_change)
1380 {
1381         struct nl_msg *msg;
1382
1383         sig_change->current_noise = 9999;
1384         sig_change->frequency = drv->assoc_freq;
1385
1386         msg = nlmsg_alloc();
1387         if (!msg)
1388                 return -ENOMEM;
1389
1390         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1391
1392         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1393
1394         return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
1395  nla_put_failure:
1396         return -ENOBUFS;
1397 }
1398
1399
1400 static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv,
1401                               struct nlattr *tb[])
1402 {
1403         static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
1404                 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
1405                 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 },
1406                 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
1407                 [NL80211_ATTR_CQM_PKT_LOSS_EVENT] = { .type = NLA_U32 },
1408         };
1409         struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
1410         enum nl80211_cqm_rssi_threshold_event event;
1411         union wpa_event_data ed;
1412         struct wpa_signal_info sig;
1413         int res;
1414
1415         if (tb[NL80211_ATTR_CQM] == NULL ||
1416             nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM],
1417                              cqm_policy)) {
1418                 wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event");
1419                 return;
1420         }
1421
1422         os_memset(&ed, 0, sizeof(ed));
1423
1424         if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
1425                 if (!tb[NL80211_ATTR_MAC])
1426                         return;
1427                 os_memcpy(ed.low_ack.addr, nla_data(tb[NL80211_ATTR_MAC]),
1428                           ETH_ALEN);
1429                 wpa_supplicant_event(drv->ctx, EVENT_STATION_LOW_ACK, &ed);
1430                 return;
1431         }
1432
1433         if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL)
1434                 return;
1435         event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
1436
1437         if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) {
1438                 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1439                            "event: RSSI high");
1440                 ed.signal_change.above_threshold = 1;
1441         } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) {
1442                 wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor "
1443                            "event: RSSI low");
1444                 ed.signal_change.above_threshold = 0;
1445         } else
1446                 return;
1447
1448         res = nl80211_get_link_signal(drv, &sig);
1449         if (res == 0) {
1450                 ed.signal_change.current_signal = sig.current_signal;
1451                 ed.signal_change.current_txrate = sig.current_txrate;
1452                 wpa_printf(MSG_DEBUG, "nl80211: Signal: %d dBm  txrate: %d",
1453                            sig.current_signal, sig.current_txrate);
1454         }
1455
1456         res = nl80211_get_link_noise(drv, &sig);
1457         if (res == 0) {
1458                 ed.signal_change.current_noise = sig.current_noise;
1459                 wpa_printf(MSG_DEBUG, "nl80211: Noise: %d dBm",
1460                            sig.current_noise);
1461         }
1462
1463         wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed);
1464 }
1465
1466
1467 static void nl80211_new_station_event(struct wpa_driver_nl80211_data *drv,
1468                                       struct nlattr **tb)
1469 {
1470         u8 *addr;
1471         union wpa_event_data data;
1472
1473         if (tb[NL80211_ATTR_MAC] == NULL)
1474                 return;
1475         addr = nla_data(tb[NL80211_ATTR_MAC]);
1476         wpa_printf(MSG_DEBUG, "nl80211: New station " MACSTR, MAC2STR(addr));
1477
1478         if (is_ap_interface(drv->nlmode) && drv->no_monitor_iface_capab) {
1479                 u8 *ies = NULL;
1480                 size_t ies_len = 0;
1481                 if (tb[NL80211_ATTR_IE]) {
1482                         ies = nla_data(tb[NL80211_ATTR_IE]);
1483                         ies_len = nla_len(tb[NL80211_ATTR_IE]);
1484                 }
1485                 wpa_hexdump(MSG_DEBUG, "nl80211: Assoc Req IEs", ies, ies_len);
1486                 drv_event_assoc(drv->ctx, addr, ies, ies_len, 0);
1487                 return;
1488         }
1489
1490         if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1491                 return;
1492
1493         os_memset(&data, 0, sizeof(data));
1494         os_memcpy(data.ibss_rsn_start.peer, addr, ETH_ALEN);
1495         wpa_supplicant_event(drv->ctx, EVENT_IBSS_RSN_START, &data);
1496 }
1497
1498
1499 static void nl80211_del_station_event(struct wpa_driver_nl80211_data *drv,
1500                                       struct nlattr **tb)
1501 {
1502         u8 *addr;
1503         union wpa_event_data data;
1504
1505         if (tb[NL80211_ATTR_MAC] == NULL)
1506                 return;
1507         addr = nla_data(tb[NL80211_ATTR_MAC]);
1508         wpa_printf(MSG_DEBUG, "nl80211: Delete station " MACSTR,
1509                    MAC2STR(addr));
1510
1511         if (is_ap_interface(drv->nlmode) && drv->no_monitor_iface_capab) {
1512                 drv_event_disassoc(drv->ctx, addr);
1513                 return;
1514         }
1515
1516         if (drv->nlmode != NL80211_IFTYPE_ADHOC)
1517                 return;
1518
1519         os_memset(&data, 0, sizeof(data));
1520         os_memcpy(data.ibss_peer_lost.peer, addr, ETH_ALEN);
1521         wpa_supplicant_event(drv->ctx, EVENT_IBSS_PEER_LOST, &data);
1522 }
1523
1524
1525 static void nl80211_rekey_offload_event(struct wpa_driver_nl80211_data *drv,
1526                                         struct nlattr **tb)
1527 {
1528         struct nlattr *rekey_info[NUM_NL80211_REKEY_DATA];
1529         static struct nla_policy rekey_policy[NUM_NL80211_REKEY_DATA] = {
1530                 [NL80211_REKEY_DATA_KEK] = {
1531                         .minlen = NL80211_KEK_LEN,
1532                         .maxlen = NL80211_KEK_LEN,
1533                 },
1534                 [NL80211_REKEY_DATA_KCK] = {
1535                         .minlen = NL80211_KCK_LEN,
1536                         .maxlen = NL80211_KCK_LEN,
1537                 },
1538                 [NL80211_REKEY_DATA_REPLAY_CTR] = {
1539                         .minlen = NL80211_REPLAY_CTR_LEN,
1540                         .maxlen = NL80211_REPLAY_CTR_LEN,
1541                 },
1542         };
1543         union wpa_event_data data;
1544
1545         if (!tb[NL80211_ATTR_MAC])
1546                 return;
1547         if (!tb[NL80211_ATTR_REKEY_DATA])
1548                 return;
1549         if (nla_parse_nested(rekey_info, MAX_NL80211_REKEY_DATA,
1550                              tb[NL80211_ATTR_REKEY_DATA], rekey_policy))
1551                 return;
1552         if (!rekey_info[NL80211_REKEY_DATA_REPLAY_CTR])
1553                 return;
1554
1555         os_memset(&data, 0, sizeof(data));
1556         data.driver_gtk_rekey.bssid = nla_data(tb[NL80211_ATTR_MAC]);
1557         wpa_printf(MSG_DEBUG, "nl80211: Rekey offload event for BSSID " MACSTR,
1558                    MAC2STR(data.driver_gtk_rekey.bssid));
1559         data.driver_gtk_rekey.replay_ctr =
1560                 nla_data(rekey_info[NL80211_REKEY_DATA_REPLAY_CTR]);
1561         wpa_hexdump(MSG_DEBUG, "nl80211: Rekey offload - Replay Counter",
1562                     data.driver_gtk_rekey.replay_ctr, NL80211_REPLAY_CTR_LEN);
1563         wpa_supplicant_event(drv->ctx, EVENT_DRIVER_GTK_REKEY, &data);
1564 }
1565
1566
1567 static void nl80211_pmksa_candidate_event(struct wpa_driver_nl80211_data *drv,
1568                                           struct nlattr **tb)
1569 {
1570         struct nlattr *cand[NUM_NL80211_PMKSA_CANDIDATE];
1571         static struct nla_policy cand_policy[NUM_NL80211_PMKSA_CANDIDATE] = {
1572                 [NL80211_PMKSA_CANDIDATE_INDEX] = { .type = NLA_U32 },
1573                 [NL80211_PMKSA_CANDIDATE_BSSID] = {
1574                         .minlen = ETH_ALEN,
1575                         .maxlen = ETH_ALEN,
1576                 },
1577                 [NL80211_PMKSA_CANDIDATE_PREAUTH] = { .type = NLA_FLAG },
1578         };
1579         union wpa_event_data data;
1580
1581         if (!tb[NL80211_ATTR_PMKSA_CANDIDATE])
1582                 return;
1583         if (nla_parse_nested(cand, MAX_NL80211_PMKSA_CANDIDATE,
1584                              tb[NL80211_ATTR_PMKSA_CANDIDATE], cand_policy))
1585                 return;
1586         if (!cand[NL80211_PMKSA_CANDIDATE_INDEX] ||
1587             !cand[NL80211_PMKSA_CANDIDATE_BSSID])
1588                 return;
1589
1590         os_memset(&data, 0, sizeof(data));
1591         os_memcpy(data.pmkid_candidate.bssid,
1592                   nla_data(cand[NL80211_PMKSA_CANDIDATE_BSSID]), ETH_ALEN);
1593         data.pmkid_candidate.index =
1594                 nla_get_u32(cand[NL80211_PMKSA_CANDIDATE_INDEX]);
1595         data.pmkid_candidate.preauth =
1596                 cand[NL80211_PMKSA_CANDIDATE_PREAUTH] != NULL;
1597         wpa_supplicant_event(drv->ctx, EVENT_PMKID_CANDIDATE, &data);
1598 }
1599
1600
1601 static int process_event(struct nl_msg *msg, void *arg)
1602 {
1603         struct wpa_driver_nl80211_data *drv = arg;
1604         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1605         struct nlattr *tb[NL80211_ATTR_MAX + 1];
1606
1607         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1608                   genlmsg_attrlen(gnlh, 0), NULL);
1609
1610         if (tb[NL80211_ATTR_IFINDEX]) {
1611                 int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1612                 if (ifindex != drv->ifindex && !have_ifidx(drv, ifindex)) {
1613                         wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)"
1614                                    " for foreign interface (ifindex %d)",
1615                                    gnlh->cmd, ifindex);
1616                         return NL_SKIP;
1617                 }
1618         }
1619
1620         if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED &&
1621             (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
1622              gnlh->cmd == NL80211_CMD_SCAN_ABORTED)) {
1623                 wpa_driver_nl80211_set_mode(&drv->first_bss,
1624                                             drv->ap_scan_as_station);
1625                 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
1626         }
1627
1628         switch (gnlh->cmd) {
1629         case NL80211_CMD_TRIGGER_SCAN:
1630                 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
1631                 break;
1632         case NL80211_CMD_START_SCHED_SCAN:
1633                 wpa_printf(MSG_DEBUG, "nl80211: Sched scan started");
1634                 break;
1635         case NL80211_CMD_SCHED_SCAN_STOPPED:
1636                 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stopped");
1637                 wpa_supplicant_event(drv->ctx, EVENT_SCHED_SCAN_STOPPED, NULL);
1638                 break;
1639         case NL80211_CMD_NEW_SCAN_RESULTS:
1640                 wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
1641                 drv->scan_complete_events = 1;
1642                 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
1643                                      drv->ctx);
1644                 send_scan_event(drv, 0, tb);
1645                 break;
1646         case NL80211_CMD_SCHED_SCAN_RESULTS:
1647                 wpa_printf(MSG_DEBUG,
1648                            "nl80211: New sched scan results available");
1649                 send_scan_event(drv, 0, tb);
1650                 break;
1651         case NL80211_CMD_SCAN_ABORTED:
1652                 wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
1653                 /*
1654                  * Need to indicate that scan results are available in order
1655                  * not to make wpa_supplicant stop its scanning.
1656                  */
1657                 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
1658                                      drv->ctx);
1659                 send_scan_event(drv, 1, tb);
1660                 break;
1661         case NL80211_CMD_AUTHENTICATE:
1662         case NL80211_CMD_ASSOCIATE:
1663         case NL80211_CMD_DEAUTHENTICATE:
1664         case NL80211_CMD_DISASSOCIATE:
1665         case NL80211_CMD_FRAME:
1666         case NL80211_CMD_FRAME_TX_STATUS:
1667         case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1668         case NL80211_CMD_UNPROT_DISASSOCIATE:
1669                 mlme_event(drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
1670                            tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT],
1671                            tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK],
1672                            tb[NL80211_ATTR_COOKIE]);
1673                 break;
1674         case NL80211_CMD_CONNECT:
1675         case NL80211_CMD_ROAM:
1676                 mlme_event_connect(drv, gnlh->cmd,
1677                                    tb[NL80211_ATTR_STATUS_CODE],
1678                                    tb[NL80211_ATTR_MAC],
1679                                    tb[NL80211_ATTR_REQ_IE],
1680                                    tb[NL80211_ATTR_RESP_IE]);
1681                 break;
1682         case NL80211_CMD_DISCONNECT:
1683                 mlme_event_disconnect(drv, tb[NL80211_ATTR_REASON_CODE],
1684                                       tb[NL80211_ATTR_MAC]);
1685                 break;
1686         case NL80211_CMD_MICHAEL_MIC_FAILURE:
1687                 mlme_event_michael_mic_failure(drv, tb);
1688                 break;
1689         case NL80211_CMD_JOIN_IBSS:
1690                 mlme_event_join_ibss(drv, tb);
1691                 break;
1692         case NL80211_CMD_REMAIN_ON_CHANNEL:
1693                 mlme_event_remain_on_channel(drv, 0, tb);
1694                 break;
1695         case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
1696                 mlme_event_remain_on_channel(drv, 1, tb);
1697                 break;
1698         case NL80211_CMD_NOTIFY_CQM:
1699                 nl80211_cqm_event(drv, tb);
1700                 break;
1701         case NL80211_CMD_REG_CHANGE:
1702                 wpa_printf(MSG_DEBUG, "nl80211: Regulatory domain change");
1703                 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
1704                                      NULL);
1705                 break;
1706         case NL80211_CMD_REG_BEACON_HINT:
1707                 wpa_printf(MSG_DEBUG, "nl80211: Regulatory beacon hint");
1708                 wpa_supplicant_event(drv->ctx, EVENT_CHANNEL_LIST_CHANGED,
1709                                      NULL);
1710                 break;
1711         case NL80211_CMD_NEW_STATION:
1712                 nl80211_new_station_event(drv, tb);
1713                 break;
1714         case NL80211_CMD_DEL_STATION:
1715                 nl80211_del_station_event(drv, tb);
1716                 break;
1717         case NL80211_CMD_SET_REKEY_OFFLOAD:
1718                 nl80211_rekey_offload_event(drv, tb);
1719                 break;
1720         case NL80211_CMD_PMKSA_CANDIDATE:
1721                 nl80211_pmksa_candidate_event(drv, tb);
1722                 break;
1723         default:
1724                 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
1725                            "(cmd=%d)", gnlh->cmd);
1726                 break;
1727         }
1728
1729         return NL_SKIP;
1730 }
1731
1732
1733 static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1734                                              void *handle)
1735 {
1736         struct nl_cb *cb;
1737         struct wpa_driver_nl80211_data *drv = eloop_ctx;
1738
1739         wpa_printf(MSG_DEBUG, "nl80211: Event message available");
1740
1741         cb = nl_cb_clone(drv->global->nl_cb);
1742         if (!cb)
1743                 return;
1744         nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
1745         nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, process_event, drv);
1746         nl_recvmsgs(handle, cb);
1747         nl_cb_put(cb);
1748 }
1749
1750
1751 /**
1752  * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1753  * @priv: driver_nl80211 private data
1754  * @alpha2_arg: country to which to switch to
1755  * Returns: 0 on success, -1 on failure
1756  *
1757  * This asks nl80211 to set the regulatory domain for given
1758  * country ISO / IEC alpha2.
1759  */
1760 static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1761 {
1762         struct i802_bss *bss = priv;
1763         struct wpa_driver_nl80211_data *drv = bss->drv;
1764         char alpha2[3];
1765         struct nl_msg *msg;
1766
1767         msg = nlmsg_alloc();
1768         if (!msg)
1769                 return -ENOMEM;
1770
1771         alpha2[0] = alpha2_arg[0];
1772         alpha2[1] = alpha2_arg[1];
1773         alpha2[2] = '\0';
1774
1775         nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG);
1776
1777         NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
1778         if (send_and_recv_msgs(drv, msg, NULL, NULL))
1779                 return -EINVAL;
1780         return 0;
1781 nla_put_failure:
1782         return -EINVAL;
1783 }
1784
1785
1786 struct wiphy_info_data {
1787         struct wpa_driver_capa *capa;
1788
1789         unsigned int error:1;
1790 };
1791
1792
1793 static int wiphy_info_handler(struct nl_msg *msg, void *arg)
1794 {
1795         struct nlattr *tb[NL80211_ATTR_MAX + 1];
1796         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1797         struct wiphy_info_data *info = arg;
1798         int p2p_go_supported = 0, p2p_client_supported = 0;
1799         int p2p_concurrent = 0;
1800         int auth_supported = 0, connect_supported = 0;
1801         struct wpa_driver_capa *capa = info->capa;
1802         static struct nla_policy
1803         iface_combination_policy[NUM_NL80211_IFACE_COMB] = {
1804                 [NL80211_IFACE_COMB_LIMITS] = { .type = NLA_NESTED },
1805                 [NL80211_IFACE_COMB_MAXNUM] = { .type = NLA_U32 },
1806                 [NL80211_IFACE_COMB_STA_AP_BI_MATCH] = { .type = NLA_FLAG },
1807                 [NL80211_IFACE_COMB_NUM_CHANNELS] = { .type = NLA_U32 },
1808         },
1809         iface_limit_policy[NUM_NL80211_IFACE_LIMIT] = {
1810                 [NL80211_IFACE_LIMIT_TYPES] = { .type = NLA_NESTED },
1811                 [NL80211_IFACE_LIMIT_MAX] = { .type = NLA_U32 },
1812         };
1813
1814         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1815                   genlmsg_attrlen(gnlh, 0), NULL);
1816
1817         if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
1818                 capa->max_scan_ssids =
1819                         nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
1820
1821         if (tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS])
1822                 capa->max_sched_scan_ssids =
1823                         nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS]);
1824
1825         if (tb[NL80211_ATTR_MAX_MATCH_SETS])
1826                 capa->max_match_sets =
1827                         nla_get_u8(tb[NL80211_ATTR_MAX_MATCH_SETS]);
1828
1829         if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
1830                 struct nlattr *nl_mode;
1831                 int i;
1832                 nla_for_each_nested(nl_mode,
1833                                     tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
1834                         switch (nla_type(nl_mode)) {
1835                         case NL80211_IFTYPE_AP:
1836                                 capa->flags |= WPA_DRIVER_FLAGS_AP;
1837                                 break;
1838                         case NL80211_IFTYPE_P2P_GO:
1839                                 p2p_go_supported = 1;
1840                                 break;
1841                         case NL80211_IFTYPE_P2P_CLIENT:
1842                                 p2p_client_supported = 1;
1843                                 break;
1844                         }
1845                 }
1846         }
1847
1848         if (tb[NL80211_ATTR_INTERFACE_COMBINATIONS]) {
1849                 struct nlattr *nl_combi;
1850                 int rem_combi;
1851
1852                 nla_for_each_nested(nl_combi,
1853                                     tb[NL80211_ATTR_INTERFACE_COMBINATIONS],
1854                                     rem_combi) {
1855                         struct nlattr *tb_comb[NUM_NL80211_IFACE_COMB];
1856                         struct nlattr *tb_limit[NUM_NL80211_IFACE_LIMIT];
1857                         struct nlattr *nl_limit, *nl_mode;
1858                         int err, rem_limit, rem_mode;
1859                         int combination_has_p2p = 0, combination_has_mgd = 0;
1860
1861                         err = nla_parse_nested(tb_comb, MAX_NL80211_IFACE_COMB,
1862                                                nl_combi,
1863                                                iface_combination_policy);
1864                         if (err || !tb_comb[NL80211_IFACE_COMB_LIMITS] ||
1865                             !tb_comb[NL80211_IFACE_COMB_MAXNUM] ||
1866                             !tb_comb[NL80211_IFACE_COMB_NUM_CHANNELS])
1867                                 goto broken_combination;
1868
1869                         nla_for_each_nested(nl_limit,
1870                                             tb_comb[NL80211_IFACE_COMB_LIMITS],
1871                                             rem_limit) {
1872                                 err = nla_parse_nested(tb_limit,
1873                                                        MAX_NL80211_IFACE_LIMIT,
1874                                                        nl_limit,
1875                                                        iface_limit_policy);
1876                                 if (err ||
1877                                     !tb_limit[NL80211_IFACE_LIMIT_TYPES])
1878                                         goto broken_combination;
1879
1880                                 nla_for_each_nested(
1881                                         nl_mode,
1882                                         tb_limit[NL80211_IFACE_LIMIT_TYPES],
1883                                         rem_mode) {
1884                                         int ift = nla_type(nl_mode);
1885                                         if (ift == NL80211_IFTYPE_P2P_GO ||
1886                                             ift == NL80211_IFTYPE_P2P_CLIENT)
1887                                                 combination_has_p2p = 1;
1888                                         if (ift == NL80211_IFTYPE_STATION)
1889                                                 combination_has_mgd = 1;
1890                                 }
1891                                 if (combination_has_p2p && combination_has_mgd)
1892                                         break;
1893                         }
1894
1895                         if (combination_has_p2p && combination_has_mgd) {
1896                                 p2p_concurrent = 1;
1897                                 break;
1898                         }
1899
1900 broken_combination:
1901                         ;
1902                 }
1903         }
1904
1905         if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
1906                 struct nlattr *nl_cmd;
1907                 int i;
1908
1909                 nla_for_each_nested(nl_cmd,
1910                                     tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
1911                         switch (nla_get_u32(nl_cmd)) {
1912                         case NL80211_CMD_AUTHENTICATE:
1913                                 auth_supported = 1;
1914                                 break;
1915                         case NL80211_CMD_CONNECT:
1916                                 connect_supported = 1;
1917                                 break;
1918                         case NL80211_CMD_START_SCHED_SCAN:
1919                                 capa->sched_scan_supported = 1;
1920                                 break;
1921                         }
1922                 }
1923         }
1924
1925         if (tb[NL80211_ATTR_OFFCHANNEL_TX_OK]) {
1926                 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based "
1927                            "off-channel TX");
1928                 capa->flags |= WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
1929         }
1930
1931         if (tb[NL80211_ATTR_ROAM_SUPPORT]) {
1932                 wpa_printf(MSG_DEBUG, "nl80211: Using driver-based roaming");
1933                 capa->flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
1934         }
1935
1936         /* default to 5000 since early versions of mac80211 don't set it */
1937         capa->max_remain_on_chan = 5000;
1938
1939         if (tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION])
1940                 capa->max_remain_on_chan =
1941                         nla_get_u32(tb[NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION]);
1942
1943         if (auth_supported)
1944                 capa->flags |= WPA_DRIVER_FLAGS_SME;
1945         else if (!connect_supported) {
1946                 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
1947                            "authentication/association or connect commands");
1948                 info->error = 1;
1949         }
1950
1951         if (p2p_go_supported && p2p_client_supported)
1952                 capa->flags |= WPA_DRIVER_FLAGS_P2P_CAPABLE;
1953         if (p2p_concurrent) {
1954                 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
1955                            "interface (driver advertised support)");
1956                 capa->flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
1957                 capa->flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
1958         }
1959
1960         if (tb[NL80211_ATTR_TDLS_SUPPORT]) {
1961                 wpa_printf(MSG_DEBUG, "nl80211: TDLS supported");
1962                 capa->flags |= WPA_DRIVER_FLAGS_TDLS_SUPPORT;
1963
1964                 if (tb[NL80211_ATTR_TDLS_EXTERNAL_SETUP]) {
1965                         wpa_printf(MSG_DEBUG, "nl80211: TDLS external setup");
1966                         capa->flags |=
1967                                 WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP;
1968                 }
1969         }
1970
1971         return NL_SKIP;
1972 }
1973
1974
1975 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
1976                                        struct wiphy_info_data *info)
1977 {
1978         struct nl_msg *msg;
1979
1980         os_memset(info, 0, sizeof(*info));
1981         info->capa = &drv->capa;
1982
1983         msg = nlmsg_alloc();
1984         if (!msg)
1985                 return -1;
1986
1987         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
1988
1989         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex);
1990
1991         if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0)
1992                 return 0;
1993         msg = NULL;
1994 nla_put_failure:
1995         nlmsg_free(msg);
1996         return -1;
1997 }
1998
1999
2000 static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
2001 {
2002         struct wiphy_info_data info;
2003         if (wpa_driver_nl80211_get_info(drv, &info))
2004                 return -1;
2005
2006         if (info.error)
2007                 return -1;
2008
2009         drv->has_capability = 1;
2010         /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
2011         drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2012                 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2013                 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2014                 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
2015         drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
2016                 WPA_DRIVER_CAPA_ENC_WEP104 |
2017                 WPA_DRIVER_CAPA_ENC_TKIP |
2018                 WPA_DRIVER_CAPA_ENC_CCMP;
2019         drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
2020                 WPA_DRIVER_AUTH_SHARED |
2021                 WPA_DRIVER_AUTH_LEAP;
2022
2023         drv->capa.flags |= WPA_DRIVER_FLAGS_SANE_ERROR_CODES;
2024         drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
2025         drv->capa.flags |= WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
2026         drv->capa.flags |= WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS;
2027
2028         return 0;
2029 }
2030
2031
2032 static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
2033 {
2034         global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
2035         if (global->nl_cb == NULL) {
2036                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
2037                            "callbacks");
2038                 return -1;
2039         }
2040
2041         if (nl_create_handles(&global->nl, global->nl_cb, "nl"))
2042                 return -1;
2043
2044         global->nl80211 = genl_ctrl_search_by_name(global->nl.cache,
2045                                                    "nl80211");
2046         if (global->nl80211 == NULL) {
2047                 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
2048                            "found");
2049                 return -1;
2050         }
2051
2052         return 0;
2053 }
2054
2055
2056 static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
2057 {
2058         struct nl80211_global *global = drv->global;
2059         int ret;
2060
2061         /* Initialize generic netlink and nl80211 */
2062
2063         if (nl_create_handles(&drv->nl_event, global->nl_cb, "event"))
2064                 goto err3;
2065
2066         ret = nl_get_multicast_id(drv, "nl80211", "scan");
2067         if (ret >= 0)
2068                 ret = nl_socket_add_membership(drv->nl_event.handle, ret);
2069         if (ret < 0) {
2070                 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
2071                            "membership for scan events: %d (%s)",
2072                            ret, strerror(-ret));
2073                 goto err4;
2074         }
2075
2076         ret = nl_get_multicast_id(drv, "nl80211", "mlme");
2077         if (ret >= 0)
2078                 ret = nl_socket_add_membership(drv->nl_event.handle, ret);
2079         if (ret < 0) {
2080                 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
2081                            "membership for mlme events: %d (%s)",
2082                            ret, strerror(-ret));
2083                 goto err4;
2084         }
2085
2086         ret = nl_get_multicast_id(drv, "nl80211", "regulatory");
2087         if (ret >= 0)
2088                 ret = nl_socket_add_membership(drv->nl_event.handle, ret);
2089         if (ret < 0) {
2090                 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
2091                            "membership for regulatory events: %d (%s)",
2092                            ret, strerror(-ret));
2093                 /* Continue without regulatory events */
2094         }
2095
2096         eloop_register_read_sock(nl_socket_get_fd(drv->nl_event.handle),
2097                                  wpa_driver_nl80211_event_receive, drv,
2098                                  drv->nl_event.handle);
2099
2100         return 0;
2101
2102 err4:
2103         nl_destroy_handles(&drv->nl_event);
2104 err3:
2105         return -1;
2106 }
2107
2108
2109 static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
2110 {
2111         wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
2112         /*
2113          * This may be for any interface; use ifdown event to disable
2114          * interface.
2115          */
2116 }
2117
2118
2119 static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
2120 {
2121         struct wpa_driver_nl80211_data *drv = ctx;
2122         wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
2123         if (linux_set_iface_flags(drv->global->ioctl_sock,
2124                                   drv->first_bss.ifname, 1)) {
2125                 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
2126                            "after rfkill unblock");
2127                 return;
2128         }
2129         /* rtnetlink ifup handler will report interface as enabled */
2130 }
2131
2132
2133 static void nl80211_get_phy_name(struct wpa_driver_nl80211_data *drv)
2134 {
2135         /* Find phy (radio) to which this interface belongs */
2136         char buf[90], *pos;
2137         int f, rv;
2138
2139         drv->phyname[0] = '\0';
2140         snprintf(buf, sizeof(buf) - 1, "/sys/class/net/%s/phy80211/name",
2141                  drv->first_bss.ifname);
2142         f = open(buf, O_RDONLY);
2143         if (f < 0) {
2144                 wpa_printf(MSG_DEBUG, "Could not open file %s: %s",
2145                            buf, strerror(errno));
2146                 return;
2147         }
2148
2149         rv = read(f, drv->phyname, sizeof(drv->phyname) - 1);
2150         close(f);
2151         if (rv < 0) {
2152                 wpa_printf(MSG_DEBUG, "Could not read file %s: %s",
2153                            buf, strerror(errno));
2154                 return;
2155         }
2156
2157         drv->phyname[rv] = '\0';
2158         pos = os_strchr(drv->phyname, '\n');
2159         if (pos)
2160                 *pos = '\0';
2161         wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2162                    drv->first_bss.ifname, drv->phyname);
2163 }
2164
2165
2166 #ifdef CONFIG_AP
2167 static void nl80211_l2_read(void *ctx, const u8 *src_addr, const u8 *buf,
2168                             size_t len)
2169 {
2170         wpa_printf(MSG_DEBUG, "nl80211: l2_packet read %u",
2171                    (unsigned int) len);
2172 }
2173 #endif /* CONFIG_AP */
2174
2175
2176 /**
2177  * wpa_driver_nl80211_init - Initialize nl80211 driver interface
2178  * @ctx: context to be used when calling wpa_supplicant functions,
2179  * e.g., wpa_supplicant_event()
2180  * @ifname: interface name, e.g., wlan0
2181  * @global_priv: private driver global data from global_init()
2182  * Returns: Pointer to private data, %NULL on failure
2183  */
2184 static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
2185                                       void *global_priv)
2186 {
2187         struct wpa_driver_nl80211_data *drv;
2188         struct rfkill_config *rcfg;
2189         struct i802_bss *bss;
2190
2191         drv = os_zalloc(sizeof(*drv));
2192         if (drv == NULL)
2193                 return NULL;
2194         drv->global = global_priv;
2195         drv->ctx = ctx;
2196         bss = &drv->first_bss;
2197         bss->drv = drv;
2198         os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
2199         drv->monitor_ifidx = -1;
2200         drv->monitor_sock = -1;
2201         drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
2202
2203         if (wpa_driver_nl80211_init_nl(drv)) {
2204                 os_free(drv);
2205                 return NULL;
2206         }
2207
2208         nl80211_get_phy_name(drv);
2209
2210         rcfg = os_zalloc(sizeof(*rcfg));
2211         if (rcfg == NULL)
2212                 goto failed;
2213         rcfg->ctx = drv;
2214         os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
2215         rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
2216         rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
2217         drv->rfkill = rfkill_init(rcfg);
2218         if (drv->rfkill == NULL) {
2219                 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
2220                 os_free(rcfg);
2221         }
2222
2223         if (wpa_driver_nl80211_finish_drv_init(drv))
2224                 goto failed;
2225
2226 #ifdef CONFIG_AP
2227         drv->l2 = l2_packet_init(ifname, NULL, ETH_P_EAPOL,
2228                                  nl80211_l2_read, drv, 0);
2229 #endif /* CONFIG_AP */
2230
2231         if (drv->global) {
2232                 dl_list_add(&drv->global->interfaces, &drv->list);
2233                 drv->in_interface_list = 1;
2234         }
2235
2236         return bss;
2237
2238 failed:
2239         wpa_driver_nl80211_deinit(bss);
2240         return NULL;
2241 }
2242
2243
2244 static int nl80211_register_frame(struct wpa_driver_nl80211_data *drv,
2245                                   struct nl_handle *nl_handle,
2246                                   u16 type, const u8 *match, size_t match_len)
2247 {
2248         struct nl_msg *msg;
2249         int ret = -1;
2250
2251         msg = nlmsg_alloc();
2252         if (!msg)
2253                 return -1;
2254
2255         nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_ACTION);
2256
2257         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2258         NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE, type);
2259         NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match);
2260
2261         ret = send_and_recv(drv, nl_handle, msg, NULL, NULL);
2262         msg = NULL;
2263         if (ret) {
2264                 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
2265                            "failed (type=%u): ret=%d (%s)",
2266                            type, ret, strerror(-ret));
2267                 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
2268                             match, match_len);
2269                 goto nla_put_failure;
2270         }
2271         ret = 0;
2272 nla_put_failure:
2273         nlmsg_free(msg);
2274         return ret;
2275 }
2276
2277
2278 static int nl80211_register_action_frame(struct wpa_driver_nl80211_data *drv,
2279                                          const u8 *match, size_t match_len)
2280 {
2281         u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
2282         return nl80211_register_frame(drv, drv->nl_event.handle,
2283                                       type, match, match_len);
2284 }
2285
2286
2287 static int nl80211_register_action_frames(struct wpa_driver_nl80211_data *drv)
2288 {
2289 #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
2290         /* GAS Initial Request */
2291         if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0a", 2) < 0)
2292                 return -1;
2293         /* GAS Initial Response */
2294         if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0b", 2) < 0)
2295                 return -1;
2296         /* GAS Comeback Request */
2297         if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0c", 2) < 0)
2298                 return -1;
2299         /* GAS Comeback Response */
2300         if (nl80211_register_action_frame(drv, (u8 *) "\x04\x0d", 2) < 0)
2301                 return -1;
2302 #endif /* CONFIG_P2P || CONFIG_INTERWORKING */
2303 #ifdef CONFIG_P2P
2304         /* P2P Public Action */
2305         if (nl80211_register_action_frame(drv,
2306                                           (u8 *) "\x04\x09\x50\x6f\x9a\x09",
2307                                           6) < 0)
2308                 return -1;
2309         /* P2P Action */
2310         if (nl80211_register_action_frame(drv,
2311                                           (u8 *) "\x7f\x50\x6f\x9a\x09",
2312                                           5) < 0)
2313                 return -1;
2314 #endif /* CONFIG_P2P */
2315 #ifdef CONFIG_IEEE80211W
2316         /* SA Query Response */
2317         if (nl80211_register_action_frame(drv, (u8 *) "\x08\x01", 2) < 0)
2318                 return -1;
2319 #endif /* CONFIG_IEEE80211W */
2320
2321         /* FT Action frames */
2322         if (nl80211_register_action_frame(drv, (u8 *) "\x06", 1) < 0)
2323                 return -1;
2324         else
2325                 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
2326                         WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2327
2328         /* WNM - BSS Transition Management Request */
2329         if (nl80211_register_action_frame(drv, (u8 *) "\x0a\x07", 2) < 0)
2330                 return -1;
2331
2332         return 0;
2333 }
2334
2335
2336 static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2337 {
2338         wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2339 }
2340
2341
2342 static int
2343 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
2344 {
2345         struct i802_bss *bss = &drv->first_bss;
2346         int send_rfkill_event = 0;
2347
2348         drv->ifindex = if_nametoindex(bss->ifname);
2349         drv->first_bss.ifindex = drv->ifindex;
2350
2351 #ifndef HOSTAPD
2352         /*
2353          * Make sure the interface starts up in station mode unless this is a
2354          * dynamically added interface (e.g., P2P) that was already configured
2355          * with proper iftype.
2356          */
2357         if ((drv->global == NULL ||
2358              drv->ifindex != drv->global->if_add_ifindex) &&
2359             wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION) < 0) {
2360                 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver to "
2361                            "use managed mode");
2362                 return -1;
2363         }
2364
2365         if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
2366                 if (rfkill_is_blocked(drv->rfkill)) {
2367                         wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2368                                    "interface '%s' due to rfkill",
2369                                    bss->ifname);
2370                         drv->if_disabled = 1;
2371                         send_rfkill_event = 1;
2372                 } else {
2373                         wpa_printf(MSG_ERROR, "nl80211: Could not set "
2374                                    "interface '%s' UP", bss->ifname);
2375                         return -1;
2376                 }
2377         }
2378
2379         netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2380                                1, IF_OPER_DORMANT);
2381 #endif /* HOSTAPD */
2382
2383         if (wpa_driver_nl80211_capa(drv))
2384                 return -1;
2385
2386         if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2387                                drv->addr))
2388                 return -1;
2389
2390         if (nl80211_register_action_frames(drv) < 0) {
2391                 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
2392                            "frame processing - ignore for now");
2393                 /*
2394                  * Older kernel versions did not support this, so ignore the
2395                  * error for now. Some functionality may not be available
2396                  * because of this.
2397                  */
2398         }
2399
2400         if (send_rfkill_event) {
2401                 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2402                                        drv, drv->ctx);
2403         }
2404
2405         return 0;
2406 }
2407
2408
2409 static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
2410 {
2411         struct nl_msg *msg;
2412
2413         msg = nlmsg_alloc();
2414         if (!msg)
2415                 return -ENOMEM;
2416
2417         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_BEACON);
2418         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2419
2420         return send_and_recv_msgs(drv, msg, NULL, NULL);
2421  nla_put_failure:
2422         return -ENOBUFS;
2423 }
2424
2425
2426 /**
2427  * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
2428  * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
2429  *
2430  * Shut down driver interface and processing of driver events. Free
2431  * private data buffer if one was allocated in wpa_driver_nl80211_init().
2432  */
2433 static void wpa_driver_nl80211_deinit(void *priv)
2434 {
2435         struct i802_bss *bss = priv;
2436         struct wpa_driver_nl80211_data *drv = bss->drv;
2437
2438 #ifdef CONFIG_AP
2439         if (drv->l2)
2440                 l2_packet_deinit(drv->l2);
2441 #endif /* CONFIG_AP */
2442
2443         if (drv->nl_preq.handle)
2444                 wpa_driver_nl80211_probe_req_report(bss, 0);
2445         if (bss->added_if_into_bridge) {
2446                 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2447                                     bss->ifname) < 0)
2448                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2449                                    "interface %s from bridge %s: %s",
2450                                    bss->ifname, bss->brname, strerror(errno));
2451         }
2452         if (bss->added_bridge) {
2453                 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
2454                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2455                                    "bridge %s: %s",
2456                                    bss->brname, strerror(errno));
2457         }
2458
2459         nl80211_remove_monitor_interface(drv);
2460
2461         if (is_ap_interface(drv->nlmode))
2462                 wpa_driver_nl80211_del_beacon(drv);
2463
2464 #ifdef HOSTAPD
2465         if (drv->last_freq_ht) {
2466                 /* Clear HT flags from the driver */
2467                 struct hostapd_freq_params freq;
2468                 os_memset(&freq, 0, sizeof(freq));
2469                 freq.freq = drv->last_freq;
2470                 i802_set_freq(priv, &freq);
2471         }
2472
2473         if (drv->eapol_sock >= 0) {
2474                 eloop_unregister_read_sock(drv->eapol_sock);
2475                 close(drv->eapol_sock);
2476         }
2477
2478         if (drv->if_indices != drv->default_if_indices)
2479                 os_free(drv->if_indices);
2480 #endif /* HOSTAPD */
2481
2482         if (drv->disable_11b_rates)
2483                 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2484
2485         netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2486                                IF_OPER_UP);
2487         rfkill_deinit(drv->rfkill);
2488
2489         eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2490
2491         (void) linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0);
2492         wpa_driver_nl80211_set_mode(bss, NL80211_IFTYPE_STATION);
2493
2494         eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_event.handle));
2495         nl_destroy_handles(&drv->nl_event);
2496
2497         os_free(drv->filter_ssids);
2498
2499         if (drv->in_interface_list)
2500                 dl_list_del(&drv->list);
2501
2502         os_free(drv);
2503 }
2504
2505
2506 /**
2507  * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
2508  * @eloop_ctx: Driver private data
2509  * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
2510  *
2511  * This function can be used as registered timeout when starting a scan to
2512  * generate a scan completed event if the driver does not report this.
2513  */
2514 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
2515 {
2516         struct wpa_driver_nl80211_data *drv = eloop_ctx;
2517         if (drv->ap_scan_as_station != NL80211_IFTYPE_UNSPECIFIED) {
2518                 wpa_driver_nl80211_set_mode(&drv->first_bss,
2519                                             drv->ap_scan_as_station);
2520                 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
2521         }
2522         wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
2523         wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
2524 }
2525
2526
2527 /**
2528  * wpa_driver_nl80211_scan - Request the driver to initiate scan
2529  * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
2530  * @params: Scan parameters
2531  * Returns: 0 on success, -1 on failure
2532  */
2533 static int wpa_driver_nl80211_scan(void *priv,
2534                                    struct wpa_driver_scan_params *params)
2535 {
2536         struct i802_bss *bss = priv;
2537         struct wpa_driver_nl80211_data *drv = bss->drv;
2538         int ret = 0, timeout;
2539         struct nl_msg *msg, *ssids, *freqs, *rates;
2540         size_t i;
2541
2542         msg = nlmsg_alloc();
2543         ssids = nlmsg_alloc();
2544         freqs = nlmsg_alloc();
2545         rates = nlmsg_alloc();
2546         if (!msg || !ssids || !freqs || !rates) {
2547                 nlmsg_free(msg);
2548                 nlmsg_free(ssids);
2549                 nlmsg_free(freqs);
2550                 nlmsg_free(rates);
2551                 return -1;
2552         }
2553
2554         os_free(drv->filter_ssids);
2555         drv->filter_ssids = params->filter_ssids;
2556         params->filter_ssids = NULL;
2557         drv->num_filter_ssids = params->num_filter_ssids;
2558
2559         nl80211_cmd(drv, msg, 0, NL80211_CMD_TRIGGER_SCAN);
2560
2561         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2562
2563         for (i = 0; i < params->num_ssids; i++) {
2564                 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID",
2565                                   params->ssids[i].ssid,
2566                                   params->ssids[i].ssid_len);
2567                 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
2568                         params->ssids[i].ssid);
2569         }
2570         if (params->num_ssids)
2571                 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
2572
2573         if (params->extra_ies) {
2574                 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan extra IEs",
2575                                   params->extra_ies, params->extra_ies_len);
2576                 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
2577                         params->extra_ies);
2578         }
2579
2580         if (params->freqs) {
2581                 for (i = 0; params->freqs[i]; i++) {
2582                         wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
2583                                    "MHz", params->freqs[i]);
2584                         NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
2585                 }
2586                 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
2587         }
2588
2589         if (params->p2p_probe) {
2590                 /*
2591                  * Remove 2.4 GHz rates 1, 2, 5.5, 11 Mbps from supported rates
2592                  * by masking out everything else apart from the OFDM rates 6,
2593                  * 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS rates. All 5 GHz
2594                  * rates are left enabled.
2595                  */
2596                 NLA_PUT(rates, NL80211_BAND_2GHZ, 8,
2597                         "\x0c\x12\x18\x24\x30\x48\x60\x6c");
2598                 nla_put_nested(msg, NL80211_ATTR_SCAN_SUPP_RATES, rates);
2599         }
2600
2601         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2602         msg = NULL;
2603         if (ret) {
2604                 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
2605                            "(%s)", ret, strerror(-ret));
2606 #ifdef HOSTAPD
2607                 if (is_ap_interface(drv->nlmode)) {
2608                         /*
2609                          * mac80211 does not allow scan requests in AP mode, so
2610                          * try to do this in station mode.
2611                          */
2612                         if (wpa_driver_nl80211_set_mode(
2613                                     bss, NL80211_IFTYPE_STATION))
2614                                 goto nla_put_failure;
2615
2616                         if (wpa_driver_nl80211_scan(drv, params)) {
2617                                 wpa_driver_nl80211_set_mode(bss, drv->nlmode);
2618                                 goto nla_put_failure;
2619                         }
2620
2621                         /* Restore AP mode when processing scan results */
2622                         drv->ap_scan_as_station = drv->nlmode;
2623                         ret = 0;
2624                 } else
2625                         goto nla_put_failure;
2626 #else /* HOSTAPD */
2627                 goto nla_put_failure;
2628 #endif /* HOSTAPD */
2629         }
2630
2631         /* Not all drivers generate "scan completed" wireless event, so try to
2632          * read results after a timeout. */
2633         timeout = 10;
2634         if (drv->scan_complete_events) {
2635                 /*
2636                  * The driver seems to deliver events to notify when scan is
2637                  * complete, so use longer timeout to avoid race conditions
2638                  * with scanning and following association request.
2639                  */
2640                 timeout = 30;
2641         }
2642         wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
2643                    "seconds", ret, timeout);
2644         eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2645         eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
2646                                drv, drv->ctx);
2647
2648 nla_put_failure:
2649         nlmsg_free(ssids);
2650         nlmsg_free(msg);
2651         nlmsg_free(freqs);
2652         nlmsg_free(rates);
2653         return ret;
2654 }
2655
2656
2657 /**
2658  * wpa_driver_nl80211_sched_scan - Initiate a scheduled scan
2659  * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
2660  * @params: Scan parameters
2661  * @interval: Interval between scan cycles in milliseconds
2662  * Returns: 0 on success, -1 on failure or if not supported
2663  */
2664 static int wpa_driver_nl80211_sched_scan(void *priv,
2665                                          struct wpa_driver_scan_params *params,
2666                                          u32 interval)
2667 {
2668         struct i802_bss *bss = priv;
2669         struct wpa_driver_nl80211_data *drv = bss->drv;
2670         int ret = 0;
2671         struct nl_msg *msg, *ssids, *freqs, *match_set_ssid, *match_sets;
2672         size_t i;
2673
2674         msg = nlmsg_alloc();
2675         ssids = nlmsg_alloc();
2676         freqs = nlmsg_alloc();
2677         if (!msg || !ssids || !freqs) {
2678                 nlmsg_free(msg);
2679                 nlmsg_free(ssids);
2680                 nlmsg_free(freqs);
2681                 return -1;
2682         }
2683
2684         os_free(drv->filter_ssids);
2685         drv->filter_ssids = params->filter_ssids;
2686         params->filter_ssids = NULL;
2687         drv->num_filter_ssids = params->num_filter_ssids;
2688
2689         nl80211_cmd(drv, msg, 0, NL80211_CMD_START_SCHED_SCAN);
2690
2691         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2692
2693         NLA_PUT_U32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, interval);
2694
2695         if (drv->num_filter_ssids) {
2696                 match_sets = nlmsg_alloc();
2697
2698                 for (i = 0; i < drv->num_filter_ssids; i++) {
2699                         wpa_hexdump_ascii(MSG_MSGDUMP,
2700                                           "nl80211: Sched scan filter SSID",
2701                                           drv->filter_ssids[i].ssid,
2702                                           drv->filter_ssids[i].ssid_len);
2703
2704                         match_set_ssid = nlmsg_alloc();
2705                         nla_put(match_set_ssid,
2706                                 NL80211_ATTR_SCHED_SCAN_MATCH_SSID,
2707                                 drv->filter_ssids[i].ssid_len,
2708                                 drv->filter_ssids[i].ssid);
2709
2710                         nla_put_nested(match_sets, i + 1, match_set_ssid);
2711
2712                         nlmsg_free(match_set_ssid);
2713                 }
2714
2715                 nla_put_nested(msg, NL80211_ATTR_SCHED_SCAN_MATCH,
2716                                match_sets);
2717                 nlmsg_free(match_sets);
2718         }
2719
2720         for (i = 0; i < params->num_ssids; i++) {
2721                 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Sched scan SSID",
2722                                   params->ssids[i].ssid,
2723                                   params->ssids[i].ssid_len);
2724                 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
2725                         params->ssids[i].ssid);
2726         }
2727         if (params->num_ssids)
2728                 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
2729
2730         if (params->extra_ies) {
2731                 wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Sched scan extra IEs",
2732                                   params->extra_ies, params->extra_ies_len);
2733                 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
2734                         params->extra_ies);
2735         }
2736
2737         if (params->freqs) {
2738                 for (i = 0; params->freqs[i]; i++) {
2739                         wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u "
2740                                    "MHz", params->freqs[i]);
2741                         NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
2742                 }
2743                 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
2744         }
2745
2746         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2747
2748         /* TODO: if we get an error here, we should fall back to normal scan */
2749
2750         msg = NULL;
2751         if (ret) {
2752                 wpa_printf(MSG_DEBUG, "nl80211: Sched scan start failed: "
2753                            "ret=%d (%s)", ret, strerror(-ret));
2754                 goto nla_put_failure;
2755         }
2756
2757         wpa_printf(MSG_DEBUG, "nl80211: Sched scan requested (ret=%d) - "
2758                    "scan interval %d msec", ret, interval);
2759
2760 nla_put_failure:
2761         nlmsg_free(ssids);
2762         nlmsg_free(msg);
2763         nlmsg_free(freqs);
2764         return ret;
2765 }
2766
2767
2768 /**
2769  * wpa_driver_nl80211_stop_sched_scan - Stop a scheduled scan
2770  * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
2771  * Returns: 0 on success, -1 on failure or if not supported
2772  */
2773 static int wpa_driver_nl80211_stop_sched_scan(void *priv)
2774 {
2775         struct i802_bss *bss = priv;
2776         struct wpa_driver_nl80211_data *drv = bss->drv;
2777         int ret = 0;
2778         struct nl_msg *msg;
2779
2780         msg = nlmsg_alloc();
2781         if (!msg)
2782                 return -1;
2783
2784         nl80211_cmd(drv, msg, 0, NL80211_CMD_STOP_SCHED_SCAN);
2785
2786         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2787
2788         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2789         msg = NULL;
2790         if (ret) {
2791                 wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop failed: "
2792                            "ret=%d (%s)", ret, strerror(-ret));
2793                 goto nla_put_failure;
2794         }
2795
2796         wpa_printf(MSG_DEBUG, "nl80211: Sched scan stop sent (ret=%d)", ret);
2797
2798 nla_put_failure:
2799         nlmsg_free(msg);
2800         return ret;
2801 }
2802
2803
2804 static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie)
2805 {
2806         const u8 *end, *pos;
2807
2808         if (ies == NULL)
2809                 return NULL;
2810
2811         pos = ies;
2812         end = ies + ies_len;
2813
2814         while (pos + 1 < end) {
2815                 if (pos + 2 + pos[1] > end)
2816                         break;
2817                 if (pos[0] == ie)
2818                         return pos;
2819                 pos += 2 + pos[1];
2820         }
2821
2822         return NULL;
2823 }
2824
2825
2826 static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv,
2827                                  const u8 *ie, size_t ie_len)
2828 {
2829         const u8 *ssid;
2830         size_t i;
2831
2832         if (drv->filter_ssids == NULL)
2833                 return 0;
2834
2835         ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID);
2836         if (ssid == NULL)
2837                 return 1;
2838
2839         for (i = 0; i < drv->num_filter_ssids; i++) {
2840                 if (ssid[1] == drv->filter_ssids[i].ssid_len &&
2841                     os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) ==
2842                     0)
2843                         return 0;
2844         }
2845
2846         return 1;
2847 }
2848
2849
2850 static int bss_info_handler(struct nl_msg *msg, void *arg)
2851 {
2852         struct nlattr *tb[NL80211_ATTR_MAX + 1];
2853         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2854         struct nlattr *bss[NL80211_BSS_MAX + 1];
2855         static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
2856                 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
2857                 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
2858                 [NL80211_BSS_TSF] = { .type = NLA_U64 },
2859                 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
2860                 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
2861                 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
2862                 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
2863                 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
2864                 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
2865                 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
2866                 [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC },
2867         };
2868         struct nl80211_bss_info_arg *_arg = arg;
2869         struct wpa_scan_results *res = _arg->res;
2870         struct wpa_scan_res **tmp;
2871         struct wpa_scan_res *r;
2872         const u8 *ie, *beacon_ie;
2873         size_t ie_len, beacon_ie_len;
2874         u8 *pos;
2875         size_t i;
2876
2877         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2878                   genlmsg_attrlen(gnlh, 0), NULL);
2879         if (!tb[NL80211_ATTR_BSS])
2880                 return NL_SKIP;
2881         if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
2882                              bss_policy))
2883                 return NL_SKIP;
2884         if (bss[NL80211_BSS_STATUS]) {
2885                 enum nl80211_bss_status status;
2886                 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
2887                 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
2888                     bss[NL80211_BSS_FREQUENCY]) {
2889                         _arg->assoc_freq =
2890                                 nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
2891                         wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
2892                                    _arg->assoc_freq);
2893                 }
2894                 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
2895                     bss[NL80211_BSS_BSSID]) {
2896                         os_memcpy(_arg->assoc_bssid,
2897                                   nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
2898                         wpa_printf(MSG_DEBUG, "nl80211: Associated with "
2899                                    MACSTR, MAC2STR(_arg->assoc_bssid));
2900                 }
2901         }
2902         if (!res)
2903                 return NL_SKIP;
2904         if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
2905                 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
2906                 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
2907         } else {
2908                 ie = NULL;
2909                 ie_len = 0;
2910         }
2911         if (bss[NL80211_BSS_BEACON_IES]) {
2912                 beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]);
2913                 beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]);
2914         } else {
2915                 beacon_ie = NULL;
2916                 beacon_ie_len = 0;
2917         }
2918
2919         if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie,
2920                                   ie ? ie_len : beacon_ie_len))
2921                 return NL_SKIP;
2922
2923         r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len);
2924         if (r == NULL)
2925                 return NL_SKIP;
2926         if (bss[NL80211_BSS_BSSID])
2927                 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
2928                           ETH_ALEN);
2929         if (bss[NL80211_BSS_FREQUENCY])
2930                 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
2931         if (bss[NL80211_BSS_BEACON_INTERVAL])
2932                 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
2933         if (bss[NL80211_BSS_CAPABILITY])
2934                 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
2935         r->flags |= WPA_SCAN_NOISE_INVALID;
2936         if (bss[NL80211_BSS_SIGNAL_MBM]) {
2937                 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
2938                 r->level /= 100; /* mBm to dBm */
2939                 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
2940         } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
2941                 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
2942                 r->flags |= WPA_SCAN_LEVEL_INVALID;
2943         } else
2944                 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
2945         if (bss[NL80211_BSS_TSF])
2946                 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
2947         if (bss[NL80211_BSS_SEEN_MS_AGO])
2948                 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
2949         r->ie_len = ie_len;
2950         pos = (u8 *) (r + 1);
2951         if (ie) {
2952                 os_memcpy(pos, ie, ie_len);
2953                 pos += ie_len;
2954         }
2955         r->beacon_ie_len = beacon_ie_len;
2956         if (beacon_ie)
2957                 os_memcpy(pos, beacon_ie, beacon_ie_len);
2958
2959         if (bss[NL80211_BSS_STATUS]) {
2960                 enum nl80211_bss_status status;
2961                 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
2962                 switch (status) {
2963                 case NL80211_BSS_STATUS_AUTHENTICATED:
2964                         r->flags |= WPA_SCAN_AUTHENTICATED;
2965                         break;
2966                 case NL80211_BSS_STATUS_ASSOCIATED:
2967                         r->flags |= WPA_SCAN_ASSOCIATED;
2968                         break;
2969                 default:
2970                         break;
2971                 }
2972         }
2973
2974         /*
2975          * cfg80211 maintains separate BSS table entries for APs if the same
2976          * BSSID,SSID pair is seen on multiple channels. wpa_supplicant does
2977          * not use frequency as a separate key in the BSS table, so filter out
2978          * duplicated entries. Prefer associated BSS entry in such a case in
2979          * order to get the correct frequency into the BSS table.
2980          */
2981         for (i = 0; i < res->num; i++) {
2982                 const u8 *s1, *s2;
2983                 if (os_memcmp(res->res[i]->bssid, r->bssid, ETH_ALEN) != 0)
2984                         continue;
2985
2986                 s1 = nl80211_get_ie((u8 *) (res->res[i] + 1),
2987                                     res->res[i]->ie_len, WLAN_EID_SSID);
2988                 s2 = nl80211_get_ie((u8 *) (r + 1), r->ie_len, WLAN_EID_SSID);
2989                 if (s1 == NULL || s2 == NULL || s1[1] != s2[1] ||
2990                     os_memcmp(s1, s2, 2 + s1[1]) != 0)
2991                         continue;
2992
2993                 /* Same BSSID,SSID was already included in scan results */
2994                 wpa_printf(MSG_DEBUG, "nl80211: Remove duplicated scan result "
2995                            "for " MACSTR, MAC2STR(r->bssid));
2996
2997                 if ((r->flags & WPA_SCAN_ASSOCIATED) &&
2998                     !(res->res[i]->flags & WPA_SCAN_ASSOCIATED)) {
2999                         os_free(res->res[i]);
3000                         res->res[i] = r;
3001                 } else
3002                         os_free(r);
3003                 return NL_SKIP;
3004         }
3005
3006         tmp = os_realloc(res->res,
3007                          (res->num + 1) * sizeof(struct wpa_scan_res *));
3008         if (tmp == NULL) {
3009                 os_free(r);
3010                 return NL_SKIP;
3011         }
3012         tmp[res->num++] = r;
3013         res->res = tmp;
3014
3015         return NL_SKIP;
3016 }
3017
3018
3019 static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
3020                                  const u8 *addr)
3021 {
3022         if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
3023                 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
3024                            "mismatch (" MACSTR ")", MAC2STR(addr));
3025                 wpa_driver_nl80211_mlme(drv, addr,
3026                                         NL80211_CMD_DEAUTHENTICATE,
3027                                         WLAN_REASON_PREV_AUTH_NOT_VALID, 1);
3028         }
3029 }
3030
3031
3032 static void wpa_driver_nl80211_check_bss_status(
3033         struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
3034 {
3035         size_t i;
3036
3037         for (i = 0; i < res->num; i++) {
3038                 struct wpa_scan_res *r = res->res[i];
3039                 if (r->flags & WPA_SCAN_AUTHENTICATED) {
3040                         wpa_printf(MSG_DEBUG, "nl80211: Scan results "
3041                                    "indicates BSS status with " MACSTR
3042                                    " as authenticated",
3043                                    MAC2STR(r->bssid));
3044                         if (is_sta_interface(drv->nlmode) &&
3045                             os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
3046                             os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
3047                             0) {
3048                                 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
3049                                            " in local state (auth=" MACSTR
3050                                            " assoc=" MACSTR ")",
3051                                            MAC2STR(drv->auth_bssid),
3052                                            MAC2STR(drv->bssid));
3053                                 clear_state_mismatch(drv, r->bssid);
3054                         }
3055                 }
3056
3057                 if (r->flags & WPA_SCAN_ASSOCIATED) {
3058                         wpa_printf(MSG_DEBUG, "nl80211: Scan results "
3059                                    "indicate BSS status with " MACSTR
3060                                    " as associated",
3061                                    MAC2STR(r->bssid));
3062                         if (is_sta_interface(drv->nlmode) &&
3063                             !drv->associated) {
3064                                 wpa_printf(MSG_DEBUG, "nl80211: Local state "
3065                                            "(not associated) does not match "
3066                                            "with BSS state");
3067                                 clear_state_mismatch(drv, r->bssid);
3068                         } else if (is_sta_interface(drv->nlmode) &&
3069                                    os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
3070                                    0) {
3071                                 wpa_printf(MSG_DEBUG, "nl80211: Local state "
3072                                            "(associated with " MACSTR ") does "
3073                                            "not match with BSS state",
3074                                            MAC2STR(drv->bssid));
3075                                 clear_state_mismatch(drv, r->bssid);
3076                                 clear_state_mismatch(drv, drv->bssid);
3077                         }
3078                 }
3079         }
3080 }
3081
3082
3083 static struct wpa_scan_results *
3084 nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv)
3085 {
3086         struct nl_msg *msg;
3087         struct wpa_scan_results *res;
3088         int ret;
3089         struct nl80211_bss_info_arg arg;
3090
3091         res = os_zalloc(sizeof(*res));
3092         if (res == NULL)
3093                 return NULL;
3094         msg = nlmsg_alloc();
3095         if (!msg)
3096                 goto nla_put_failure;
3097
3098         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
3099         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3100
3101         arg.drv = drv;
3102         arg.res = res;
3103         ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
3104         msg = NULL;
3105         if (ret == 0) {
3106                 wpa_printf(MSG_DEBUG, "Received scan results (%lu BSSes)",
3107                            (unsigned long) res->num);
3108                 return res;
3109         }
3110         wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
3111                    "(%s)", ret, strerror(-ret));
3112 nla_put_failure:
3113         nlmsg_free(msg);
3114         wpa_scan_results_free(res);
3115         return NULL;
3116 }
3117
3118
3119 /**
3120  * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
3121  * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
3122  * Returns: Scan results on success, -1 on failure
3123  */
3124 static struct wpa_scan_results *
3125 wpa_driver_nl80211_get_scan_results(void *priv)
3126 {
3127         struct i802_bss *bss = priv;
3128         struct wpa_driver_nl80211_data *drv = bss->drv;
3129         struct wpa_scan_results *res;
3130
3131         res = nl80211_get_scan_results(drv);
3132         if (res)
3133                 wpa_driver_nl80211_check_bss_status(drv, res);
3134         return res;
3135 }
3136
3137
3138 static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv)
3139 {
3140         struct wpa_scan_results *res;
3141         size_t i;
3142
3143         res = nl80211_get_scan_results(drv);
3144         if (res == NULL) {
3145                 wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results");
3146                 return;
3147         }
3148
3149         wpa_printf(MSG_DEBUG, "nl80211: Scan result dump");
3150         for (i = 0; i < res->num; i++) {
3151                 struct wpa_scan_res *r = res->res[i];
3152                 wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s",
3153                            (int) i, (int) res->num, MAC2STR(r->bssid),
3154                            r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "",
3155                            r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : "");
3156         }
3157
3158         wpa_scan_results_free(res);
3159 }
3160
3161
3162 static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
3163                                       enum wpa_alg alg, const u8 *addr,
3164                                       int key_idx, int set_tx,
3165                                       const u8 *seq, size_t seq_len,
3166                                       const u8 *key, size_t key_len)
3167 {
3168         struct i802_bss *bss = priv;
3169         struct wpa_driver_nl80211_data *drv = bss->drv;
3170         int ifindex = if_nametoindex(ifname);
3171         struct nl_msg *msg;
3172         int ret;
3173
3174         wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
3175                    "set_tx=%d seq_len=%lu key_len=%lu",
3176                    __func__, ifindex, alg, addr, key_idx, set_tx,
3177                    (unsigned long) seq_len, (unsigned long) key_len);
3178 #ifdef CONFIG_TDLS
3179         if (key_idx == -1)
3180                 key_idx = 0;
3181 #endif /* CONFIG_TDLS */
3182
3183         msg = nlmsg_alloc();
3184         if (!msg)
3185                 return -ENOMEM;
3186
3187         if (alg == WPA_ALG_NONE) {
3188                 nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_KEY);
3189         } else {
3190                 nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_KEY);
3191                 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
3192                 switch (alg) {
3193                 case WPA_ALG_WEP:
3194                         if (key_len == 5)
3195                                 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
3196                                             WLAN_CIPHER_SUITE_WEP40);
3197                         else
3198                                 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
3199                                             WLAN_CIPHER_SUITE_WEP104);
3200                         break;
3201                 case WPA_ALG_TKIP:
3202                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
3203                                     WLAN_CIPHER_SUITE_TKIP);
3204                         break;
3205                 case WPA_ALG_CCMP:
3206                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
3207                                     WLAN_CIPHER_SUITE_CCMP);
3208                         break;
3209                 case WPA_ALG_IGTK:
3210                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
3211                                     WLAN_CIPHER_SUITE_AES_CMAC);
3212                         break;
3213                 default:
3214                         wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
3215                                    "algorithm %d", __func__, alg);
3216                         nlmsg_free(msg);
3217                         return -1;
3218                 }
3219         }
3220
3221         if (seq && seq_len)
3222                 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
3223
3224         if (addr && !is_broadcast_ether_addr(addr)) {
3225                 wpa_printf(MSG_DEBUG, "   addr=" MACSTR, MAC2STR(addr));
3226                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3227
3228                 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
3229                         wpa_printf(MSG_DEBUG, "   RSN IBSS RX GTK");
3230                         NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE,
3231                                     NL80211_KEYTYPE_GROUP);
3232                 }
3233         } else if (addr && is_broadcast_ether_addr(addr)) {
3234                 struct nl_msg *types;
3235                 int err;
3236                 wpa_printf(MSG_DEBUG, "   broadcast key");
3237                 types = nlmsg_alloc();
3238                 if (!types)
3239                         goto nla_put_failure;
3240                 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
3241                 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
3242                                      types);
3243                 nlmsg_free(types);
3244                 if (err)
3245                         goto nla_put_failure;
3246         }
3247         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
3248         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
3249
3250         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3251         if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
3252                 ret = 0;
3253         if (ret)
3254                 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
3255                            ret, strerror(-ret));
3256
3257         /*
3258          * If we failed or don't need to set the default TX key (below),
3259          * we're done here.
3260          */
3261         if (ret || !set_tx || alg == WPA_ALG_NONE)
3262                 return ret;
3263         if (is_ap_interface(drv->nlmode) && addr &&
3264             !is_broadcast_ether_addr(addr))
3265                 return ret;
3266
3267         msg = nlmsg_alloc();
3268         if (!msg)
3269                 return -ENOMEM;
3270
3271         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_KEY);
3272         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
3273         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
3274         if (alg == WPA_ALG_IGTK)
3275                 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
3276         else
3277                 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
3278         if (addr && is_broadcast_ether_addr(addr)) {
3279                 struct nl_msg *types;
3280                 int err;
3281                 types = nlmsg_alloc();
3282                 if (!types)
3283                         goto nla_put_failure;
3284                 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_MULTICAST);
3285                 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
3286                                      types);
3287                 nlmsg_free(types);
3288                 if (err)
3289                         goto nla_put_failure;
3290         } else if (addr) {
3291                 struct nl_msg *types;
3292                 int err;
3293                 types = nlmsg_alloc();
3294                 if (!types)
3295                         goto nla_put_failure;
3296                 NLA_PUT_FLAG(types, NL80211_KEY_DEFAULT_TYPE_UNICAST);
3297                 err = nla_put_nested(msg, NL80211_ATTR_KEY_DEFAULT_TYPES,
3298                                      types);
3299                 nlmsg_free(types);
3300                 if (err)
3301                         goto nla_put_failure;
3302         }
3303
3304         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3305         if (ret == -ENOENT)
3306                 ret = 0;
3307         if (ret)
3308                 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
3309                            "err=%d %s)", ret, strerror(-ret));
3310         return ret;
3311
3312 nla_put_failure:
3313         return -ENOBUFS;
3314 }
3315
3316
3317 static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
3318                       int key_idx, int defkey,
3319                       const u8 *seq, size_t seq_len,
3320                       const u8 *key, size_t key_len)
3321 {
3322         struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
3323         if (!key_attr)
3324                 return -1;
3325
3326         if (defkey && alg == WPA_ALG_IGTK)
3327                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
3328         else if (defkey)
3329                 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
3330
3331         NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
3332
3333         switch (alg) {
3334         case WPA_ALG_WEP:
3335                 if (key_len == 5)
3336                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
3337                                     WLAN_CIPHER_SUITE_WEP40);
3338                 else
3339                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
3340                                     WLAN_CIPHER_SUITE_WEP104);
3341                 break;
3342         case WPA_ALG_TKIP:
3343                 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
3344                 break;
3345         case WPA_ALG_CCMP:
3346                 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
3347                 break;
3348         case WPA_ALG_IGTK:
3349                 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
3350                             WLAN_CIPHER_SUITE_AES_CMAC);
3351                 break;
3352         default:
3353                 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
3354                            "algorithm %d", __func__, alg);
3355                 return -1;
3356         }
3357
3358         if (seq && seq_len)
3359                 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
3360
3361         NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
3362
3363         nla_nest_end(msg, key_attr);
3364
3365         return 0;
3366  nla_put_failure:
3367         return -1;
3368 }
3369
3370
3371 static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
3372                                  struct nl_msg *msg)
3373 {
3374         int i, privacy = 0;
3375         struct nlattr *nl_keys, *nl_key;
3376
3377         for (i = 0; i < 4; i++) {
3378                 if (!params->wep_key[i])
3379                         continue;
3380                 privacy = 1;
3381                 break;
3382         }
3383         if (params->wps == WPS_MODE_PRIVACY)
3384                 privacy = 1;
3385         if (params->pairwise_suite &&
3386             params->pairwise_suite != WPA_CIPHER_NONE)
3387                 privacy = 1;
3388
3389         if (!privacy)
3390                 return 0;
3391
3392         NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
3393
3394         nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
3395         if (!nl_keys)
3396                 goto nla_put_failure;
3397
3398         for (i = 0; i < 4; i++) {
3399                 if (!params->wep_key[i])
3400                         continue;
3401
3402                 nl_key = nla_nest_start(msg, i);
3403                 if (!nl_key)
3404                         goto nla_put_failure;
3405
3406                 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
3407                         params->wep_key[i]);
3408                 if (params->wep_key_len[i] == 5)
3409                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
3410                                     WLAN_CIPHER_SUITE_WEP40);
3411                 else
3412                         NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
3413                                     WLAN_CIPHER_SUITE_WEP104);
3414
3415                 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
3416
3417                 if (i == params->wep_tx_keyidx)
3418                         NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
3419
3420                 nla_nest_end(msg, nl_key);
3421         }
3422         nla_nest_end(msg, nl_keys);
3423
3424         return 0;
3425
3426 nla_put_failure:
3427         return -ENOBUFS;
3428 }
3429
3430
3431 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
3432                                    const u8 *addr, int cmd, u16 reason_code,
3433                                    int local_state_change)
3434 {
3435         int ret = -1;
3436         struct nl_msg *msg;
3437
3438         msg = nlmsg_alloc();
3439         if (!msg)
3440                 return -1;
3441
3442         nl80211_cmd(drv, msg, 0, cmd);
3443
3444         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3445         NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
3446         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3447         if (local_state_change)
3448                 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
3449
3450         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3451         msg = NULL;
3452         if (ret) {
3453                 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
3454                            "(%s)", ret, strerror(-ret));
3455                 goto nla_put_failure;
3456         }
3457         ret = 0;
3458
3459 nla_put_failure:
3460         nlmsg_free(msg);
3461         return ret;
3462 }
3463
3464
3465 static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
3466                                          const u8 *addr, int reason_code)
3467 {
3468         wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3469                    __func__, MAC2STR(addr), reason_code);
3470         drv->associated = 0;
3471         return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT,
3472                                        reason_code, 0);
3473 }
3474
3475
3476 static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
3477                                              int reason_code)
3478 {
3479         struct i802_bss *bss = priv;
3480         struct wpa_driver_nl80211_data *drv = bss->drv;
3481         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
3482                 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
3483         wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3484                    __func__, MAC2STR(addr), reason_code);
3485         drv->associated = 0;
3486         if (drv->nlmode == NL80211_IFTYPE_ADHOC)
3487                 return nl80211_leave_ibss(drv);
3488         return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
3489                                        reason_code, 0);
3490 }
3491
3492
3493 static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
3494                                            int reason_code)
3495 {
3496         struct i802_bss *bss = priv;
3497         struct wpa_driver_nl80211_data *drv = bss->drv;
3498         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
3499                 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
3500         wpa_printf(MSG_DEBUG, "%s", __func__);
3501         drv->associated = 0;
3502         return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
3503                                        reason_code, 0);
3504 }
3505
3506
3507 static int wpa_driver_nl80211_authenticate(
3508         void *priv, struct wpa_driver_auth_params *params)
3509 {
3510         struct i802_bss *bss = priv;
3511         struct wpa_driver_nl80211_data *drv = bss->drv;
3512         int ret = -1, i;
3513         struct nl_msg *msg;
3514         enum nl80211_auth_type type;
3515         enum nl80211_iftype nlmode;
3516         int count = 0;
3517
3518         drv->associated = 0;
3519         os_memset(drv->auth_bssid, 0, ETH_ALEN);
3520         /* FIX: IBSS mode */
3521         nlmode = params->p2p ?
3522                 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
3523         if (drv->nlmode != nlmode &&
3524             wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
3525                 return -1;
3526
3527 retry:
3528         msg = nlmsg_alloc();
3529         if (!msg)
3530                 return -1;
3531
3532         wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
3533                    drv->ifindex);
3534
3535         nl80211_cmd(drv, msg, 0, NL80211_CMD_AUTHENTICATE);
3536
3537         for (i = 0; i < 4; i++) {
3538                 if (!params->wep_key[i])
3539                         continue;
3540                 wpa_driver_nl80211_set_key(bss->ifname, priv, WPA_ALG_WEP,
3541                                            NULL, i,
3542                                            i == params->wep_tx_keyidx, NULL, 0,
3543                                            params->wep_key[i],
3544                                            params->wep_key_len[i]);
3545                 if (params->wep_tx_keyidx != i)
3546                         continue;
3547                 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
3548                                params->wep_key[i], params->wep_key_len[i])) {
3549                         nlmsg_free(msg);
3550                         return -1;
3551                 }
3552         }
3553
3554         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3555         if (params->bssid) {
3556                 wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
3557                            MAC2STR(params->bssid));
3558                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
3559         }
3560         if (params->freq) {
3561                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
3562                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
3563         }
3564         if (params->ssid) {
3565                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
3566                                   params->ssid, params->ssid_len);
3567                 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
3568                         params->ssid);
3569         }
3570         wpa_hexdump(MSG_DEBUG, "  * IEs", params->ie, params->ie_len);
3571         if (params->ie)
3572                 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
3573         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
3574                 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
3575         else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
3576                 type = NL80211_AUTHTYPE_SHARED_KEY;
3577         else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
3578                 type = NL80211_AUTHTYPE_NETWORK_EAP;
3579         else if (params->auth_alg & WPA_AUTH_ALG_FT)
3580                 type = NL80211_AUTHTYPE_FT;
3581         else
3582                 goto nla_put_failure;
3583         wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
3584         NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
3585         if (params->local_state_change) {
3586                 wpa_printf(MSG_DEBUG, "  * Local state change only");
3587                 NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE);
3588         }
3589
3590         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3591         msg = NULL;
3592         if (ret) {
3593                 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
3594                            "(%s)", ret, strerror(-ret));
3595                 count++;
3596                 if (ret == -EALREADY && count == 1 && params->bssid &&
3597                     !params->local_state_change) {
3598                         /*
3599                          * mac80211 does not currently accept new
3600                          * authentication if we are already authenticated. As a
3601                          * workaround, force deauthentication and try again.
3602                          */
3603                         wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3604                                    "after forced deauthentication");
3605                         wpa_driver_nl80211_deauthenticate(
3606                                 bss, params->bssid,
3607                                 WLAN_REASON_PREV_AUTH_NOT_VALID);
3608                         nlmsg_free(msg);
3609                         goto retry;
3610                 }
3611                 goto nla_put_failure;
3612         }
3613         ret = 0;
3614         wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
3615                    "successfully");
3616
3617 nla_put_failure:
3618         nlmsg_free(msg);
3619         return ret;
3620 }
3621
3622
3623 struct phy_info_arg {
3624         u16 *num_modes;
3625         struct hostapd_hw_modes *modes;
3626 };
3627
3628 static int phy_info_handler(struct nl_msg *msg, void *arg)
3629 {
3630         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3631         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3632         struct phy_info_arg *phy_info = arg;
3633
3634         struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
3635
3636         struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
3637         static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
3638                 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
3639                 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
3640                 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
3641                 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
3642                 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
3643                 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
3644         };
3645
3646         struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
3647         static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
3648                 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
3649                 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
3650         };
3651
3652         struct nlattr *nl_band;
3653         struct nlattr *nl_freq;
3654         struct nlattr *nl_rate;
3655         int rem_band, rem_freq, rem_rate;
3656         struct hostapd_hw_modes *mode;
3657         int idx, mode_is_set;
3658
3659         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3660                   genlmsg_attrlen(gnlh, 0), NULL);
3661
3662         if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
3663                 return NL_SKIP;
3664
3665         nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
3666                 mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
3667                 if (!mode)
3668                         return NL_SKIP;
3669                 phy_info->modes = mode;
3670
3671                 mode_is_set = 0;
3672
3673                 mode = &phy_info->modes[*(phy_info->num_modes)];
3674                 memset(mode, 0, sizeof(*mode));
3675                 mode->flags = HOSTAPD_MODE_FLAG_HT_INFO_KNOWN;
3676                 *(phy_info->num_modes) += 1;
3677
3678                 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
3679                           nla_len(nl_band), NULL);
3680
3681                 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
3682                         mode->ht_capab = nla_get_u16(
3683                                 tb_band[NL80211_BAND_ATTR_HT_CAPA]);
3684                 }
3685
3686                 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
3687                         mode->a_mpdu_params |= nla_get_u8(
3688                                 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
3689                                 0x03;
3690                 }
3691
3692                 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
3693                         mode->a_mpdu_params |= nla_get_u8(
3694                                 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
3695                                 2;
3696                 }
3697
3698                 if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
3699                     nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
3700                         u8 *mcs;
3701                         mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
3702                         os_memcpy(mode->mcs_set, mcs, 16);
3703                 }
3704
3705                 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
3706                         nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
3707                                   nla_len(nl_freq), freq_policy);
3708                         if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
3709                                 continue;
3710                         mode->num_channels++;
3711                 }
3712
3713                 mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data));
3714                 if (!mode->channels)
3715                         return NL_SKIP;
3716
3717                 idx = 0;
3718
3719                 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
3720                         nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
3721                                   nla_len(nl_freq), freq_policy);
3722                         if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
3723                                 continue;
3724
3725                         mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
3726                         mode->channels[idx].flag = 0;
3727
3728                         if (!mode_is_set) {
3729                                 /* crude heuristic */
3730                                 if (mode->channels[idx].freq < 4000)
3731                                         mode->mode = HOSTAPD_MODE_IEEE80211B;
3732                                 else
3733                                         mode->mode = HOSTAPD_MODE_IEEE80211A;
3734                                 mode_is_set = 1;
3735                         }
3736
3737                         /* crude heuristic */
3738                         if (mode->channels[idx].freq < 4000)
3739                                 if (mode->channels[idx].freq == 2484)
3740                                         mode->channels[idx].chan = 14;
3741                                 else
3742                                         mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
3743                         else
3744                                 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
3745
3746                         if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
3747                                 mode->channels[idx].flag |=
3748                                         HOSTAPD_CHAN_DISABLED;
3749                         if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
3750                                 mode->channels[idx].flag |=
3751                                         HOSTAPD_CHAN_PASSIVE_SCAN;
3752                         if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
3753                                 mode->channels[idx].flag |=
3754                                         HOSTAPD_CHAN_NO_IBSS;
3755                         if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
3756                                 mode->channels[idx].flag |=
3757                                         HOSTAPD_CHAN_RADAR;
3758
3759                         if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
3760                             !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
3761                                 mode->channels[idx].max_tx_power =
3762                                         nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
3763
3764                         idx++;
3765                 }
3766
3767                 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
3768                         nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
3769                                   nla_len(nl_rate), rate_policy);
3770                         if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
3771                                 continue;
3772                         mode->num_rates++;
3773                 }
3774
3775                 mode->rates = os_zalloc(mode->num_rates * sizeof(int));
3776                 if (!mode->rates)
3777                         return NL_SKIP;
3778
3779                 idx = 0;
3780
3781                 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
3782                         nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
3783                                   nla_len(nl_rate), rate_policy);
3784                         if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
3785                                 continue;
3786                         mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
3787
3788                         /* crude heuristic */
3789                         if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
3790                             mode->rates[idx] > 200)
3791                                 mode->mode = HOSTAPD_MODE_IEEE80211G;
3792
3793                         idx++;
3794                 }
3795         }
3796
3797         return NL_SKIP;
3798 }
3799
3800 static struct hostapd_hw_modes *
3801 wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
3802 {
3803         u16 m;
3804         struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
3805         int i, mode11g_idx = -1;
3806
3807         /* If only 802.11g mode is included, use it to construct matching
3808          * 802.11b mode data. */
3809
3810         for (m = 0; m < *num_modes; m++) {
3811                 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
3812                         return modes; /* 802.11b already included */
3813                 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
3814                         mode11g_idx = m;
3815         }
3816
3817         if (mode11g_idx < 0)
3818                 return modes; /* 2.4 GHz band not supported at all */
3819
3820         nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
3821         if (nmodes == NULL)
3822                 return modes; /* Could not add 802.11b mode */
3823
3824         mode = &nmodes[*num_modes];
3825         os_memset(mode, 0, sizeof(*mode));
3826         (*num_modes)++;
3827         modes = nmodes;
3828
3829         mode->mode = HOSTAPD_MODE_IEEE80211B;
3830
3831         mode11g = &modes[mode11g_idx];
3832         mode->num_channels = mode11g->num_channels;
3833         mode->channels = os_malloc(mode11g->num_channels *
3834                                    sizeof(struct hostapd_channel_data));
3835         if (mode->channels == NULL) {
3836                 (*num_modes)--;
3837                 return modes; /* Could not add 802.11b mode */
3838         }
3839         os_memcpy(mode->channels, mode11g->channels,
3840                   mode11g->num_channels * sizeof(struct hostapd_channel_data));
3841
3842         mode->num_rates = 0;
3843         mode->rates = os_malloc(4 * sizeof(int));
3844         if (mode->rates == NULL) {
3845                 os_free(mode->channels);
3846                 (*num_modes)--;
3847                 return modes; /* Could not add 802.11b mode */
3848         }
3849
3850         for (i = 0; i < mode11g->num_rates; i++) {
3851                 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
3852                     mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
3853                         continue;
3854                 mode->rates[mode->num_rates] = mode11g->rates[i];
3855                 mode->num_rates++;
3856                 if (mode->num_rates == 4)
3857                         break;
3858         }
3859
3860         if (mode->num_rates == 0) {
3861                 os_free(mode->channels);
3862                 os_free(mode->rates);
3863                 (*num_modes)--;
3864                 return modes; /* No 802.11b rates */
3865         }
3866
3867         wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
3868                    "information");
3869
3870         return modes;
3871 }
3872
3873
3874 static void nl80211_set_ht40_mode(struct hostapd_hw_modes *mode, int start,
3875                                   int end)
3876 {
3877         int c;
3878
3879         for (c = 0; c < mode->num_channels; c++) {
3880                 struct hostapd_channel_data *chan = &mode->channels[c];
3881                 if (chan->freq - 10 >= start && chan->freq + 10 <= end)
3882                         chan->flag |= HOSTAPD_CHAN_HT40;
3883         }
3884 }
3885
3886
3887 static void nl80211_set_ht40_mode_sec(struct hostapd_hw_modes *mode, int start,
3888                                       int end)
3889 {
3890         int c;
3891
3892         for (c = 0; c < mode->num_channels; c++) {
3893                 struct hostapd_channel_data *chan = &mode->channels[c];
3894                 if (!(chan->flag & HOSTAPD_CHAN_HT40))
3895                         continue;
3896                 if (chan->freq - 30 >= start && chan->freq - 10 <= end)
3897                         chan->flag |= HOSTAPD_CHAN_HT40MINUS;
3898                 if (chan->freq + 10 >= start && chan->freq + 30 <= end)
3899                         chan->flag |= HOSTAPD_CHAN_HT40PLUS;
3900         }
3901 }
3902
3903
3904 static void nl80211_reg_rule_ht40(struct nlattr *tb[],
3905                                   struct phy_info_arg *results)
3906 {
3907         u32 start, end, max_bw;
3908         u16 m;
3909
3910         if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
3911             tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
3912             tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
3913                 return;
3914
3915         start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
3916         end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
3917         max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
3918
3919         wpa_printf(MSG_DEBUG, "nl80211: %u-%u @ %u MHz",
3920                    start, end, max_bw);
3921         if (max_bw < 40)
3922                 return;
3923
3924         for (m = 0; m < *results->num_modes; m++) {
3925                 if (!(results->modes[m].ht_capab &
3926                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
3927                         continue;
3928                 nl80211_set_ht40_mode(&results->modes[m], start, end);
3929         }
3930 }
3931
3932
3933 static void nl80211_reg_rule_sec(struct nlattr *tb[],
3934                                  struct phy_info_arg *results)
3935 {
3936         u32 start, end, max_bw;
3937         u16 m;
3938
3939         if (tb[NL80211_ATTR_FREQ_RANGE_START] == NULL ||
3940             tb[NL80211_ATTR_FREQ_RANGE_END] == NULL ||
3941             tb[NL80211_ATTR_FREQ_RANGE_MAX_BW] == NULL)
3942                 return;
3943
3944         start = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]) / 1000;
3945         end = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]) / 1000;
3946         max_bw = nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]) / 1000;
3947
3948         if (max_bw < 20)
3949                 return;
3950
3951         for (m = 0; m < *results->num_modes; m++) {
3952                 if (!(results->modes[m].ht_capab &
3953                       HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET))
3954                         continue;
3955                 nl80211_set_ht40_mode_sec(&results->modes[m], start, end);
3956         }
3957 }
3958
3959
3960 static int nl80211_get_reg(struct nl_msg *msg, void *arg)
3961 {
3962         struct phy_info_arg *results = arg;
3963         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
3964         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3965         struct nlattr *nl_rule;
3966         struct nlattr *tb_rule[NL80211_FREQUENCY_ATTR_MAX + 1];
3967         int rem_rule;
3968         static struct nla_policy reg_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
3969                 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3970                 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3971                 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3972                 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3973                 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3974                 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3975         };
3976
3977         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3978                   genlmsg_attrlen(gnlh, 0), NULL);
3979         if (!tb_msg[NL80211_ATTR_REG_ALPHA2] ||
3980             !tb_msg[NL80211_ATTR_REG_RULES]) {
3981                 wpa_printf(MSG_DEBUG, "nl80211: No regulatory information "
3982                            "available");
3983                 return NL_SKIP;
3984         }
3985
3986         wpa_printf(MSG_DEBUG, "nl80211: Regulatory information - country=%s",
3987                    (char *) nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]));
3988
3989         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
3990         {
3991                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
3992                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
3993                 nl80211_reg_rule_ht40(tb_rule, results);
3994         }
3995
3996         nla_for_each_nested(nl_rule, tb_msg[NL80211_ATTR_REG_RULES], rem_rule)
3997         {
3998                 nla_parse(tb_rule, NL80211_FREQUENCY_ATTR_MAX,
3999                           nla_data(nl_rule), nla_len(nl_rule), reg_policy);
4000                 nl80211_reg_rule_sec(tb_rule, results);
4001         }
4002
4003         return NL_SKIP;
4004 }
4005
4006
4007 static int nl80211_set_ht40_flags(struct wpa_driver_nl80211_data *drv,
4008                                   struct phy_info_arg *results)
4009 {
4010         struct nl_msg *msg;
4011
4012         msg = nlmsg_alloc();
4013         if (!msg)
4014                 return -ENOMEM;
4015
4016         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
4017         return send_and_recv_msgs(drv, msg, nl80211_get_reg, results);
4018 }
4019
4020
4021 static struct hostapd_hw_modes *
4022 wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
4023 {
4024         struct i802_bss *bss = priv;
4025         struct wpa_driver_nl80211_data *drv = bss->drv;
4026         struct nl_msg *msg;
4027         struct phy_info_arg result = {
4028                 .num_modes = num_modes,
4029                 .modes = NULL,
4030         };
4031
4032         *num_modes = 0;
4033         *flags = 0;
4034
4035         msg = nlmsg_alloc();
4036         if (!msg)
4037                 return NULL;
4038
4039         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_WIPHY);
4040
4041         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4042
4043         if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) {
4044                 nl80211_set_ht40_flags(drv, &result);
4045                 return wpa_driver_nl80211_add_11b(result.modes, num_modes);
4046         }
4047  nla_put_failure:
4048         return NULL;
4049 }
4050
4051
4052 static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data *drv,
4053                                          const void *data, size_t len,
4054                                          int encrypt)
4055 {
4056         __u8 rtap_hdr[] = {
4057                 0x00, 0x00, /* radiotap version */
4058                 0x0e, 0x00, /* radiotap length */
4059                 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
4060                 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
4061                 0x00,       /* padding */
4062                 0x00, 0x00, /* RX and TX flags to indicate that */
4063                 0x00, 0x00, /* this is the injected frame directly */
4064         };
4065         struct iovec iov[2] = {
4066                 {
4067                         .iov_base = &rtap_hdr,
4068                         .iov_len = sizeof(rtap_hdr),
4069                 },
4070                 {
4071                         .iov_base = (void *) data,
4072                         .iov_len = len,
4073                 }
4074         };
4075         struct msghdr msg = {
4076                 .msg_name = NULL,
4077                 .msg_namelen = 0,
4078                 .msg_iov = iov,
4079                 .msg_iovlen = 2,
4080                 .msg_control = NULL,
4081                 .msg_controllen = 0,
4082                 .msg_flags = 0,
4083         };
4084         int res;
4085
4086         if (encrypt)
4087                 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
4088
4089         if (drv->monitor_sock < 0) {
4090                 wpa_printf(MSG_DEBUG, "nl80211: No monitor socket available "
4091                            "for %s", __func__);
4092                 return -1;
4093         }
4094
4095         res = sendmsg(drv->monitor_sock, &msg, 0);
4096         if (res < 0) {
4097                 wpa_printf(MSG_INFO, "nl80211: sendmsg: %s", strerror(errno));
4098                 return -1;
4099         }
4100         return 0;
4101 }
4102
4103
4104 static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
4105                                         size_t data_len)
4106 {
4107         struct i802_bss *bss = priv;
4108         struct wpa_driver_nl80211_data *drv = bss->drv;
4109         struct ieee80211_mgmt *mgmt;
4110         int encrypt = 1;
4111         u16 fc;
4112
4113         mgmt = (struct ieee80211_mgmt *) data;
4114         fc = le_to_host16(mgmt->frame_control);
4115
4116         if (is_sta_interface(drv->nlmode) &&
4117             WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4118             WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
4119                 /*
4120                  * The use of last_mgmt_freq is a bit of a hack,
4121                  * but it works due to the single-threaded nature
4122                  * of wpa_supplicant.
4123                  */
4124                 return nl80211_send_frame_cmd(drv, drv->last_mgmt_freq, 0,
4125                                               data, data_len, NULL);
4126         }
4127
4128         if (drv->no_monitor_iface_capab && is_ap_interface(drv->nlmode)) {
4129                 return nl80211_send_frame_cmd(drv, drv->ap_oper_freq, 0,
4130                                               data, data_len, NULL);
4131         }
4132
4133         if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
4134             WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
4135                 /*
4136                  * Only one of the authentication frame types is encrypted.
4137                  * In order for static WEP encryption to work properly (i.e.,
4138                  * to not encrypt the frame), we need to tell mac80211 about
4139                  * the frames that must not be encrypted.
4140                  */
4141                 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
4142                 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
4143                 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
4144                         encrypt = 0;
4145         }
4146
4147         return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
4148 }
4149
4150
4151 static int nl80211_set_ap_isolate(struct i802_bss *bss, int enabled)
4152 {
4153         struct wpa_driver_nl80211_data *drv = bss->drv;
4154         struct nl_msg *msg;
4155
4156         msg = nlmsg_alloc();
4157         if (!msg)
4158                 return -ENOMEM;
4159
4160         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
4161
4162         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
4163         NLA_PUT_U8(msg, NL80211_ATTR_AP_ISOLATE, enabled);
4164
4165         return send_and_recv_msgs(drv, msg, NULL, NULL);
4166  nla_put_failure:
4167         return -ENOBUFS;
4168 }
4169
4170
4171 static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
4172                            int slot, int ht_opmode)
4173 {
4174         struct wpa_driver_nl80211_data *drv = bss->drv;
4175         struct nl_msg *msg;
4176
4177         msg = nlmsg_alloc();
4178         if (!msg)
4179                 return -ENOMEM;
4180
4181         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
4182
4183         if (cts >= 0)
4184                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
4185         if (preamble >= 0)
4186                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
4187         if (slot >= 0)
4188                 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
4189         if (ht_opmode >= 0)
4190                 NLA_PUT_U16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode);
4191         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
4192
4193         return send_and_recv_msgs(drv, msg, NULL, NULL);
4194  nla_put_failure:
4195         return -ENOBUFS;
4196 }
4197
4198
4199 static int wpa_driver_nl80211_set_ap(void *priv,
4200                                      struct wpa_driver_ap_params *params)
4201 {
4202         struct i802_bss *bss = priv;
4203         struct wpa_driver_nl80211_data *drv = bss->drv;
4204         struct nl_msg *msg;
4205         u8 cmd = NL80211_CMD_NEW_BEACON;
4206         int ret;
4207         int beacon_set;
4208         int ifindex = if_nametoindex(bss->ifname);
4209         int num_suites;
4210         u32 suites[10];
4211         u32 ver;
4212
4213         beacon_set = bss->beacon_set;
4214
4215         msg = nlmsg_alloc();
4216         if (!msg)
4217                 return -ENOMEM;
4218
4219         wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
4220                    beacon_set);
4221         if (beacon_set)
4222                 cmd = NL80211_CMD_SET_BEACON;
4223
4224         nl80211_cmd(drv, msg, 0, cmd);
4225         NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, params->head_len, params->head);
4226         NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len, params->tail);
4227         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
4228         NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, params->beacon_int);
4229         NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period);
4230         NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
4231                 params->ssid);
4232         switch (params->hide_ssid) {
4233         case NO_SSID_HIDING:
4234                 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
4235                             NL80211_HIDDEN_SSID_NOT_IN_USE);
4236                 break;
4237         case HIDDEN_SSID_ZERO_LEN:
4238                 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
4239                             NL80211_HIDDEN_SSID_ZERO_LEN);
4240                 break;
4241         case HIDDEN_SSID_ZERO_CONTENTS:
4242                 NLA_PUT_U32(msg, NL80211_ATTR_HIDDEN_SSID,
4243                             NL80211_HIDDEN_SSID_ZERO_CONTENTS);
4244                 break;
4245         }
4246         if (params->privacy)
4247                 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
4248         if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
4249             (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
4250                 /* Leave out the attribute */
4251         } else if (params->auth_algs & WPA_AUTH_ALG_SHARED)
4252                 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
4253                             NL80211_AUTHTYPE_SHARED_KEY);
4254         else
4255                 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE,
4256                             NL80211_AUTHTYPE_OPEN_SYSTEM);
4257
4258         ver = 0;
4259         if (params->wpa_version & WPA_PROTO_WPA)
4260                 ver |= NL80211_WPA_VERSION_1;
4261         if (params->wpa_version & WPA_PROTO_RSN)
4262                 ver |= NL80211_WPA_VERSION_2;
4263         if (ver)
4264                 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
4265
4266         num_suites = 0;
4267         if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
4268                 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
4269         if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
4270                 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
4271         if (num_suites) {
4272                 NLA_PUT(msg, NL80211_ATTR_AKM_SUITES,
4273                         num_suites * sizeof(u32), suites);
4274         }
4275
4276         num_suites = 0;
4277         if (params->pairwise_ciphers & WPA_CIPHER_CCMP)
4278                 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
4279         if (params->pairwise_ciphers & WPA_CIPHER_TKIP)
4280                 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
4281         if (params->pairwise_ciphers & WPA_CIPHER_WEP104)
4282                 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
4283         if (params->pairwise_ciphers & WPA_CIPHER_WEP40)
4284                 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
4285         if (num_suites) {
4286                 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4287                         num_suites * sizeof(u32), suites);
4288         }
4289
4290         switch (params->group_cipher) {
4291         case WPA_CIPHER_CCMP:
4292                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
4293                             WLAN_CIPHER_SUITE_CCMP);
4294                 break;
4295         case WPA_CIPHER_TKIP:
4296                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
4297                             WLAN_CIPHER_SUITE_TKIP);
4298                 break;
4299         case WPA_CIPHER_WEP104:
4300                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
4301                             WLAN_CIPHER_SUITE_WEP104);
4302                 break;
4303         case WPA_CIPHER_WEP40:
4304                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP,
4305                             WLAN_CIPHER_SUITE_WEP40);
4306                 break;
4307         }
4308
4309         if (params->beacon_ies) {
4310                 NLA_PUT(msg, NL80211_ATTR_IE, wpabuf_len(params->beacon_ies),
4311                         wpabuf_head(params->beacon_ies));
4312         }
4313         if (params->proberesp_ies) {
4314                 NLA_PUT(msg, NL80211_ATTR_IE_PROBE_RESP,
4315                         wpabuf_len(params->proberesp_ies),
4316                         wpabuf_head(params->proberesp_ies));
4317         }
4318         if (params->assocresp_ies) {
4319                 NLA_PUT(msg, NL80211_ATTR_IE_ASSOC_RESP,
4320                         wpabuf_len(params->assocresp_ies),
4321                         wpabuf_head(params->assocresp_ies));
4322         }
4323
4324         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4325         if (ret) {
4326                 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
4327                            ret, strerror(-ret));
4328         } else {
4329                 bss->beacon_set = 1;
4330                 ret = nl80211_set_ap_isolate(bss, params->isolate);
4331                 if (!params->isolate && ret) {
4332                         wpa_printf(MSG_DEBUG, "nl80211: Ignore AP isolation "
4333                                    "configuration error since isolation is "
4334                                    "not used");
4335                         ret = 0;
4336                 }
4337
4338                 nl80211_set_bss(bss, params->cts_protect, params->preamble,
4339                                 params->short_slot_time, params->ht_opmode);
4340         }
4341         return ret;
4342  nla_put_failure:
4343         return -ENOBUFS;
4344 }
4345
4346
4347 static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data *drv,
4348                                        int freq, int ht_enabled,
4349                                        int sec_channel_offset)
4350 {
4351         struct nl_msg *msg;
4352         int ret;
4353
4354         wpa_printf(MSG_DEBUG, "nl80211: Set freq %d (ht_enabled=%d "
4355                    "sec_channel_offset=%d)",
4356                    freq, ht_enabled, sec_channel_offset);
4357         msg = nlmsg_alloc();
4358         if (!msg)
4359                 return -1;
4360
4361         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
4362
4363         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4364         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
4365         if (ht_enabled) {
4366                 switch (sec_channel_offset) {
4367                 case -1:
4368                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4369                                     NL80211_CHAN_HT40MINUS);
4370                         break;
4371                 case 1:
4372                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4373                                     NL80211_CHAN_HT40PLUS);
4374                         break;
4375                 default:
4376                         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4377                                     NL80211_CHAN_HT20);
4378                         break;
4379                 }
4380         }
4381
4382         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4383         if (ret == 0)
4384                 return 0;
4385         wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
4386                    "%d (%s)", freq, ret, strerror(-ret));
4387 nla_put_failure:
4388         return -1;
4389 }
4390
4391
4392 static u32 sta_flags_nl80211(int flags)
4393 {
4394         u32 f = 0;
4395
4396         if (flags & WPA_STA_AUTHORIZED)
4397                 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
4398         if (flags & WPA_STA_WMM)
4399                 f |= BIT(NL80211_STA_FLAG_WME);
4400         if (flags & WPA_STA_SHORT_PREAMBLE)
4401                 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
4402         if (flags & WPA_STA_MFP)
4403                 f |= BIT(NL80211_STA_FLAG_MFP);
4404
4405         return f;
4406 }
4407
4408
4409 static int wpa_driver_nl80211_sta_add(void *priv,
4410                                       struct hostapd_sta_add_params *params)
4411 {
4412         struct i802_bss *bss = priv;
4413         struct wpa_driver_nl80211_data *drv = bss->drv;
4414         struct nl_msg *msg;
4415         struct nl80211_sta_flag_update upd;
4416         int ret = -ENOBUFS;
4417
4418         msg = nlmsg_alloc();
4419         if (!msg)
4420                 return -ENOMEM;
4421
4422         nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_STATION);
4423
4424         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
4425         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
4426         NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
4427         NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
4428                 params->supp_rates);
4429         NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4430                     params->listen_interval);
4431         if (params->ht_capabilities) {
4432                 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
4433                         sizeof(*params->ht_capabilities),
4434                         params->ht_capabilities);
4435         }
4436
4437         os_memset(&upd, 0, sizeof(upd));
4438         upd.mask = sta_flags_nl80211(params->flags);
4439         upd.set = upd.mask;
4440         NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
4441
4442         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4443         if (ret)
4444                 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION "
4445                            "result: %d (%s)", ret, strerror(-ret));
4446         if (ret == -EEXIST)
4447                 ret = 0;
4448  nla_put_failure:
4449         return ret;
4450 }
4451
4452
4453 static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
4454 {
4455         struct i802_bss *bss = priv;
4456         struct wpa_driver_nl80211_data *drv = bss->drv;
4457         struct nl_msg *msg;
4458         int ret;
4459
4460         msg = nlmsg_alloc();
4461         if (!msg)
4462                 return -ENOMEM;
4463
4464         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
4465
4466         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
4467                     if_nametoindex(bss->ifname));
4468         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4469
4470         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4471         if (ret == -ENOENT)
4472                 return 0;
4473         return ret;
4474  nla_put_failure:
4475         return -ENOBUFS;
4476 }
4477
4478
4479 static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
4480                                  int ifidx)
4481 {
4482         struct nl_msg *msg;
4483
4484         wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4485
4486         /* stop listening for EAPOL on this interface */
4487         del_ifidx(drv, ifidx);
4488
4489         msg = nlmsg_alloc();
4490         if (!msg)
4491                 goto nla_put_failure;
4492
4493         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_INTERFACE);
4494         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
4495
4496         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4497                 return;
4498  nla_put_failure:
4499         wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
4500 }
4501
4502
4503 static const char * nl80211_iftype_str(enum nl80211_iftype mode)
4504 {
4505         switch (mode) {
4506         case NL80211_IFTYPE_ADHOC:
4507                 return "ADHOC";
4508         case NL80211_IFTYPE_STATION:
4509                 return "STATION";
4510         case NL80211_IFTYPE_AP:
4511                 return "AP";
4512         case NL80211_IFTYPE_MONITOR:
4513                 return "MONITOR";
4514         case NL80211_IFTYPE_P2P_CLIENT:
4515                 return "P2P_CLIENT";
4516         case NL80211_IFTYPE_P2P_GO:
4517                 return "P2P_GO";
4518         default:
4519                 return "unknown";
4520         }
4521 }
4522
4523
4524 static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4525                                      const char *ifname,
4526                                      enum nl80211_iftype iftype,
4527                                      const u8 *addr, int wds)
4528 {
4529         struct nl_msg *msg, *flags = NULL;
4530         int ifidx;
4531         int ret = -ENOBUFS;
4532
4533         wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4534                    iftype, nl80211_iftype_str(iftype));
4535
4536         msg = nlmsg_alloc();
4537         if (!msg)
4538                 return -1;
4539
4540         nl80211_cmd(drv, msg, 0, NL80211_CMD_NEW_INTERFACE);
4541         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4542         NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
4543         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
4544
4545         if (iftype == NL80211_IFTYPE_MONITOR) {
4546                 int err;
4547
4548                 flags = nlmsg_alloc();
4549                 if (!flags)
4550                         goto nla_put_failure;
4551
4552                 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
4553
4554                 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
4555
4556                 nlmsg_free(flags);
4557
4558                 if (err)
4559                         goto nla_put_failure;
4560         } else if (wds) {
4561                 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
4562         }
4563
4564         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4565         if (ret) {
4566  nla_put_failure:
4567                 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4568                            ifname, ret, strerror(-ret));
4569                 return ret;
4570         }
4571
4572         ifidx = if_nametoindex(ifname);
4573         wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4574                    ifname, ifidx);
4575
4576         if (ifidx <= 0)
4577                 return -1;
4578
4579         /* start listening for EAPOL on this interface */
4580         add_ifidx(drv, ifidx);
4581
4582         if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4583             linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
4584                 nl80211_remove_iface(drv, ifidx);
4585                 return -1;
4586         }
4587
4588         return ifidx;
4589 }
4590
4591
4592 static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4593                                 const char *ifname, enum nl80211_iftype iftype,
4594                                 const u8 *addr, int wds)
4595 {
4596         int ret;
4597
4598         ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
4599
4600         /* if error occurred and interface exists already */
4601         if (ret == -ENFILE && if_nametoindex(ifname)) {
4602                 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4603
4604                 /* Try to remove the interface that was already there. */
4605                 nl80211_remove_iface(drv, if_nametoindex(ifname));
4606
4607                 /* Try to create the interface again */
4608                 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
4609                                                 wds);
4610         }
4611
4612         if (ret >= 0 && drv->disable_11b_rates)
4613                 nl80211_disable_11b_rates(drv, ret, 1);
4614
4615         return ret;
4616 }
4617
4618
4619 static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
4620 {
4621         struct ieee80211_hdr *hdr;
4622         u16 fc;
4623         union wpa_event_data event;
4624
4625         hdr = (struct ieee80211_hdr *) buf;
4626         fc = le_to_host16(hdr->frame_control);
4627
4628         os_memset(&event, 0, sizeof(event));
4629         event.tx_status.type = WLAN_FC_GET_TYPE(fc);
4630         event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
4631         event.tx_status.dst = hdr->addr1;
4632         event.tx_status.data = buf;
4633         event.tx_status.data_len = len;
4634         event.tx_status.ack = ok;
4635         wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
4636 }
4637
4638
4639 static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
4640                              u8 *buf, size_t len)
4641 {
4642         struct ieee80211_hdr *hdr = (void *)buf;
4643         u16 fc;
4644         union wpa_event_data event;
4645
4646         if (len < sizeof(*hdr))
4647                 return;
4648
4649         fc = le_to_host16(hdr->frame_control);
4650
4651         os_memset(&event, 0, sizeof(event));
4652         event.rx_from_unknown.bssid = get_hdr_bssid(hdr, len);
4653         event.rx_from_unknown.addr = hdr->addr2;
4654         event.rx_from_unknown.wds = (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) ==
4655                 (WLAN_FC_FROMDS | WLAN_FC_TODS);
4656         wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
4657 }
4658
4659
4660 static void handle_frame(struct wpa_driver_nl80211_data *drv,
4661                          u8 *buf, size_t len, int datarate, int ssi_signal)
4662 {
4663         struct ieee80211_hdr *hdr;
4664         u16 fc;
4665         union wpa_event_data event;
4666
4667         hdr = (struct ieee80211_hdr *) buf;
4668         fc = le_to_host16(hdr->frame_control);
4669
4670         switch (WLAN_FC_GET_TYPE(fc)) {
4671         case WLAN_FC_TYPE_MGMT:
4672                 os_memset(&event, 0, sizeof(event));
4673                 event.rx_mgmt.frame = buf;
4674                 event.rx_mgmt.frame_len = len;
4675                 event.rx_mgmt.datarate = datarate;
4676                 event.rx_mgmt.ssi_signal = ssi_signal;
4677                 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
4678                 break;
4679         case WLAN_FC_TYPE_CTRL:
4680                 /* can only get here with PS-Poll frames */
4681                 wpa_printf(MSG_DEBUG, "CTRL");
4682                 from_unknown_sta(drv, buf, len);
4683                 break;
4684         case WLAN_FC_TYPE_DATA:
4685                 from_unknown_sta(drv, buf, len);
4686                 break;
4687         }
4688 }
4689
4690
4691 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
4692 {
4693         struct wpa_driver_nl80211_data *drv = eloop_ctx;
4694         int len;
4695         unsigned char buf[3000];
4696         struct ieee80211_radiotap_iterator iter;
4697         int ret;
4698         int datarate = 0, ssi_signal = 0;
4699         int injected = 0, failed = 0, rxflags = 0;
4700
4701         len = recv(sock, buf, sizeof(buf), 0);
4702         if (len < 0) {
4703                 perror("recv");
4704                 return;
4705         }
4706
4707         if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
4708                 printf("received invalid radiotap frame\n");
4709                 return;
4710         }
4711
4712         while (1) {
4713                 ret = ieee80211_radiotap_iterator_next(&iter);
4714                 if (ret == -ENOENT)
4715                         break;
4716                 if (ret) {
4717                         printf("received invalid radiotap frame (%d)\n", ret);
4718                         return;
4719                 }
4720                 switch (iter.this_arg_index) {
4721                 case IEEE80211_RADIOTAP_FLAGS:
4722                         if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
4723                                 len -= 4;
4724                         break;
4725                 case IEEE80211_RADIOTAP_RX_FLAGS:
4726                         rxflags = 1;
4727                         break;
4728                 case IEEE80211_RADIOTAP_TX_FLAGS:
4729                         injected = 1;
4730                         failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
4731                                         IEEE80211_RADIOTAP_F_TX_FAIL;
4732                         break;
4733                 case IEEE80211_RADIOTAP_DATA_RETRIES:
4734                         break;
4735                 case IEEE80211_RADIOTAP_CHANNEL:
4736                         /* TODO: convert from freq/flags to channel number */
4737                         break;
4738                 case IEEE80211_RADIOTAP_RATE:
4739                         datarate = *iter.this_arg * 5;
4740                         break;
4741                 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
4742                         ssi_signal = *iter.this_arg;
4743                         break;
4744                 }
4745         }
4746
4747         if (rxflags && injected)
4748                 return;
4749
4750         if (!injected)
4751                 handle_frame(drv, buf + iter.max_length,
4752                              len - iter.max_length, datarate, ssi_signal);
4753         else
4754                 handle_tx_callback(drv->ctx, buf + iter.max_length,
4755                                    len - iter.max_length, !failed);
4756 }
4757
4758
4759 /*
4760  * we post-process the filter code later and rewrite
4761  * this to the offset to the last instruction
4762  */
4763 #define PASS    0xFF
4764 #define FAIL    0xFE
4765
4766 static struct sock_filter msock_filter_insns[] = {
4767         /*
4768          * do a little-endian load of the radiotap length field
4769          */
4770         /* load lower byte into A */
4771         BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 2),
4772         /* put it into X (== index register) */
4773         BPF_STMT(BPF_MISC| BPF_TAX, 0),
4774         /* load upper byte into A */
4775         BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 3),
4776         /* left-shift it by 8 */
4777         BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
4778         /* or with X */
4779         BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
4780         /* put result into X */
4781         BPF_STMT(BPF_MISC| BPF_TAX, 0),
4782
4783         /*
4784          * Allow management frames through, this also gives us those
4785          * management frames that we sent ourselves with status
4786          */
4787         /* load the lower byte of the IEEE 802.11 frame control field */
4788         BPF_STMT(BPF_LD  | BPF_B | BPF_IND, 0),
4789         /* mask off frame type and version */
4790         BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
4791         /* accept frame if it's both 0, fall through otherwise */
4792         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
4793
4794         /*
4795          * TODO: add a bit to radiotap RX flags that indicates
4796          * that the sending station is not associated, then
4797          * add a filter here that filters on our DA and that flag
4798          * to allow us to deauth frames to that bad station.
4799          *
4800          * For now allow all To DS data frames through.
4801          */
4802         /* load the IEEE 802.11 frame control field */
4803         BPF_STMT(BPF_LD  | BPF_H | BPF_IND, 0),
4804         /* mask off frame type, version and DS status */
4805         BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0F03),
4806         /* accept frame if version 0, type 2 and To DS, fall through otherwise
4807          */
4808         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0801, PASS, 0),
4809
4810 #if 0
4811         /*
4812          * drop non-data frames
4813          */
4814         /* load the lower byte of the frame control field */
4815         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
4816         /* mask off QoS bit */
4817         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x0c),
4818         /* drop non-data frames */
4819         BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 8, 0, FAIL),
4820 #endif
4821         /* load the upper byte of the frame control field */
4822         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 1),
4823         /* mask off toDS/fromDS */
4824         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x03),
4825         /* accept WDS frames */
4826         BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 3, PASS, 0),
4827
4828         /*
4829          * add header length to index
4830          */
4831         /* load the lower byte of the frame control field */
4832         BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
4833         /* mask off QoS bit */
4834         BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x80),
4835         /* right shift it by 6 to give 0 or 2 */
4836         BPF_STMT(BPF_ALU  | BPF_RSH | BPF_K, 6),
4837         /* add data frame header length */
4838         BPF_STMT(BPF_ALU  | BPF_ADD | BPF_K, 24),
4839         /* add index, was start of 802.11 header */
4840         BPF_STMT(BPF_ALU  | BPF_ADD | BPF_X, 0),
4841         /* move to index, now start of LL header */
4842         BPF_STMT(BPF_MISC | BPF_TAX, 0),
4843
4844         /*
4845          * Accept empty data frames, we use those for
4846          * polling activity.
4847          */
4848         BPF_STMT(BPF_LD  | BPF_W | BPF_LEN, 0),
4849         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
4850
4851         /*
4852          * Accept EAPOL frames
4853          */
4854         BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 0),
4855         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
4856         BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 4),
4857         BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
4858
4859         /* keep these last two statements or change the code below */
4860         /* return 0 == "DROP" */
4861         BPF_STMT(BPF_RET | BPF_K, 0),
4862         /* return ~0 == "keep all" */
4863         BPF_STMT(BPF_RET | BPF_K, ~0),
4864 };
4865
4866 static struct sock_fprog msock_filter = {
4867         .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
4868         .filter = msock_filter_insns,
4869 };
4870
4871
4872 static int add_monitor_filter(int s)
4873 {
4874         int idx;
4875
4876         /* rewrite all PASS/FAIL jump offsets */
4877         for (idx = 0; idx < msock_filter.len; idx++) {
4878                 struct sock_filter *insn = &msock_filter_insns[idx];
4879
4880                 if (BPF_CLASS(insn->code) == BPF_JMP) {
4881                         if (insn->code == (BPF_JMP|BPF_JA)) {
4882                                 if (insn->k == PASS)
4883                                         insn->k = msock_filter.len - idx - 2;
4884                                 else if (insn->k == FAIL)
4885                                         insn->k = msock_filter.len - idx - 3;
4886                         }
4887
4888                         if (insn->jt == PASS)
4889                                 insn->jt = msock_filter.len - idx - 2;
4890                         else if (insn->jt == FAIL)
4891                                 insn->jt = msock_filter.len - idx - 3;
4892
4893                         if (insn->jf == PASS)
4894                                 insn->jf = msock_filter.len - idx - 2;
4895                         else if (insn->jf == FAIL)
4896                                 insn->jf = msock_filter.len - idx - 3;
4897                 }
4898         }
4899
4900         if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
4901                        &msock_filter, sizeof(msock_filter))) {
4902                 perror("SO_ATTACH_FILTER");
4903                 return -1;
4904         }
4905
4906         return 0;
4907 }
4908
4909
4910 static void nl80211_remove_monitor_interface(
4911         struct wpa_driver_nl80211_data *drv)
4912 {
4913         if (drv->monitor_ifidx >= 0) {
4914                 nl80211_remove_iface(drv, drv->monitor_ifidx);
4915                 drv->monitor_ifidx = -1;
4916         }
4917         if (drv->monitor_sock >= 0) {
4918                 eloop_unregister_read_sock(drv->monitor_sock);
4919                 close(drv->monitor_sock);
4920                 drv->monitor_sock = -1;
4921         }
4922 }
4923
4924
4925 static int
4926 nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
4927 {
4928         char buf[IFNAMSIZ];
4929         struct sockaddr_ll ll;
4930         int optval;
4931         socklen_t optlen;
4932
4933         if (os_strncmp(drv->first_bss.ifname, "p2p-", 4) == 0) {
4934                 /*
4935                  * P2P interface name is of the format p2p-%s-%d. For monitor
4936                  * interface name corresponding to P2P GO, replace "p2p-" with
4937                  * "mon-" to retain the same interface name length and to
4938                  * indicate that it is a monitor interface.
4939                  */
4940                 snprintf(buf, IFNAMSIZ, "mon-%s", drv->first_bss.ifname + 4);
4941         } else {
4942                 /* Non-P2P interface with AP functionality. */
4943                 snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname);
4944         }
4945
4946         buf[IFNAMSIZ - 1] = '\0';
4947
4948         drv->monitor_ifidx =
4949                 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
4950                                      0);
4951
4952         if (drv->monitor_ifidx == -EOPNOTSUPP) {
4953                 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support "
4954                            "monitor interface type - try to run without it");
4955                 drv->no_monitor_iface_capab = 1;
4956         }
4957
4958         if (drv->monitor_ifidx < 0)
4959                 return -1;
4960
4961         if (linux_set_iface_flags(drv->global->ioctl_sock, buf, 1))
4962                 goto error;
4963
4964         memset(&ll, 0, sizeof(ll));
4965         ll.sll_family = AF_PACKET;
4966         ll.sll_ifindex = drv->monitor_ifidx;
4967         drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
4968         if (drv->monitor_sock < 0) {
4969                 perror("socket[PF_PACKET,SOCK_RAW]");
4970                 goto error;
4971         }
4972
4973         if (add_monitor_filter(drv->monitor_sock)) {
4974                 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
4975                            "interface; do filtering in user space");
4976                 /* This works, but will cost in performance. */
4977         }
4978
4979         if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
4980                 perror("monitor socket bind");
4981                 goto error;
4982         }
4983
4984         optlen = sizeof(optval);
4985         optval = 20;
4986         if (setsockopt
4987             (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
4988                 perror("Failed to set socket priority");
4989                 goto error;
4990         }
4991
4992         if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
4993                                      drv, NULL)) {
4994                 printf("Could not register monitor read socket\n");
4995                 goto error;
4996         }
4997
4998         return 0;
4999  error:
5000         nl80211_remove_monitor_interface(drv);
5001         return -1;
5002 }
5003
5004
5005 #ifdef CONFIG_AP
5006 static int nl80211_send_eapol_data(struct i802_bss *bss,
5007                                    const u8 *addr, const u8 *data,
5008                                    size_t data_len, const u8 *own_addr)
5009 {
5010         if (bss->drv->l2 == NULL) {
5011                 wpa_printf(MSG_DEBUG, "nl80211: No l2_packet to send EAPOL");
5012                 return -1;
5013         }
5014
5015         if (l2_packet_send(bss->drv->l2, addr, ETH_P_EAPOL, data, data_len) <
5016             0)
5017                 return -1;
5018         return 0;
5019 }
5020 #endif /* CONFIG_AP */
5021
5022
5023 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
5024
5025 static int wpa_driver_nl80211_hapd_send_eapol(
5026         void *priv, const u8 *addr, const u8 *data,
5027         size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
5028 {
5029         struct i802_bss *bss = priv;
5030         struct wpa_driver_nl80211_data *drv = bss->drv;
5031         struct ieee80211_hdr *hdr;
5032         size_t len;
5033         u8 *pos;
5034         int res;
5035         int qos = flags & WPA_STA_WMM;
5036
5037 #ifdef CONFIG_AP
5038         if (drv->no_monitor_iface_capab)
5039                 return nl80211_send_eapol_data(bss, addr, data, data_len,
5040                                                own_addr);
5041 #endif /* CONFIG_AP */
5042
5043         len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
5044                 data_len;
5045         hdr = os_zalloc(len);
5046         if (hdr == NULL) {
5047                 printf("malloc() failed for i802_send_data(len=%lu)\n",
5048                        (unsigned long) len);
5049                 return -1;
5050         }
5051
5052         hdr->frame_control =
5053                 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
5054         hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
5055         if (encrypt)
5056                 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
5057         if (qos) {
5058                 hdr->frame_control |=
5059                         host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
5060         }
5061
5062         memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
5063         memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
5064         memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
5065         pos = (u8 *) (hdr + 1);
5066
5067         if (qos) {
5068                 /* add an empty QoS header if needed */
5069                 pos[0] = 0;
5070                 pos[1] = 0;
5071                 pos += 2;
5072         }
5073
5074         memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
5075         pos += sizeof(rfc1042_header);
5076         WPA_PUT_BE16(pos, ETH_P_PAE);
5077         pos += 2;
5078         memcpy(pos, data, data_len);
5079
5080         res = wpa_driver_nl80211_send_frame(drv, (u8 *) hdr, len, encrypt);
5081         if (res < 0) {
5082                 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
5083                            "failed: %d (%s)",
5084                            (unsigned long) len, errno, strerror(errno));
5085         }
5086         os_free(hdr);
5087
5088         return res;
5089 }
5090
5091
5092 static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
5093                                             int total_flags,
5094                                             int flags_or, int flags_and)
5095 {
5096         struct i802_bss *bss = priv;
5097         struct wpa_driver_nl80211_data *drv = bss->drv;
5098         struct nl_msg *msg, *flags = NULL;
5099         struct nl80211_sta_flag_update upd;
5100
5101         msg = nlmsg_alloc();
5102         if (!msg)
5103                 return -ENOMEM;
5104
5105         flags = nlmsg_alloc();
5106         if (!flags) {
5107                 nlmsg_free(msg);
5108                 return -ENOMEM;
5109         }
5110
5111         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
5112
5113         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
5114                     if_nametoindex(bss->ifname));
5115         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5116
5117         /*
5118          * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
5119          * can be removed eventually.
5120          */
5121         if (total_flags & WPA_STA_AUTHORIZED)
5122                 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
5123
5124         if (total_flags & WPA_STA_WMM)
5125                 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
5126
5127         if (total_flags & WPA_STA_SHORT_PREAMBLE)
5128                 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
5129
5130         if (total_flags & WPA_STA_MFP)
5131                 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
5132
5133         if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
5134                 goto nla_put_failure;
5135
5136         os_memset(&upd, 0, sizeof(upd));
5137         upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
5138         upd.set = sta_flags_nl80211(flags_or);
5139         NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
5140
5141         nlmsg_free(flags);
5142
5143         return send_and_recv_msgs(drv, msg, NULL, NULL);
5144  nla_put_failure:
5145         nlmsg_free(flags);
5146         return -ENOBUFS;
5147 }
5148
5149
5150 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
5151                                  struct wpa_driver_associate_params *params)
5152 {
5153         enum nl80211_iftype nlmode;
5154
5155         if (params->p2p) {
5156                 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
5157                            "group (GO)");
5158                 nlmode = NL80211_IFTYPE_P2P_GO;
5159         } else
5160                 nlmode = NL80211_IFTYPE_AP;
5161
5162         if (wpa_driver_nl80211_set_mode(&drv->first_bss, nlmode) ||
5163             wpa_driver_nl80211_set_freq(drv, params->freq, 0, 0)) {
5164                 nl80211_remove_monitor_interface(drv);
5165                 return -1;
5166         }
5167
5168         if (drv->no_monitor_iface_capab) {
5169                 if (wpa_driver_nl80211_probe_req_report(&drv->first_bss, 1) < 0)
5170                 {
5171                         wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
5172                                    "Probe Request frame reporting in AP mode");
5173                         /* Try to survive without this */
5174                 }
5175         }
5176
5177         drv->ap_oper_freq = params->freq;
5178
5179         return 0;
5180 }
5181
5182
5183 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
5184 {
5185         struct nl_msg *msg;
5186         int ret = -1;
5187
5188         msg = nlmsg_alloc();
5189         if (!msg)
5190                 return -1;
5191
5192         nl80211_cmd(drv, msg, 0, NL80211_CMD_LEAVE_IBSS);
5193         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5194         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5195         msg = NULL;
5196         if (ret) {
5197                 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
5198                            "(%s)", ret, strerror(-ret));
5199                 goto nla_put_failure;
5200         }
5201
5202         ret = 0;
5203         wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
5204
5205 nla_put_failure:
5206         nlmsg_free(msg);
5207         return ret;
5208 }
5209
5210
5211 static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
5212                                    struct wpa_driver_associate_params *params)
5213 {
5214         struct nl_msg *msg;
5215         int ret = -1;
5216         int count = 0;
5217
5218         wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
5219
5220         if (wpa_driver_nl80211_set_mode(&drv->first_bss,
5221                                         NL80211_IFTYPE_ADHOC)) {
5222                 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
5223                            "IBSS mode");
5224                 return -1;
5225         }
5226
5227 retry:
5228         msg = nlmsg_alloc();
5229         if (!msg)
5230                 return -1;
5231
5232         nl80211_cmd(drv, msg, 0, NL80211_CMD_JOIN_IBSS);
5233         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5234
5235         if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
5236                 goto nla_put_failure;
5237
5238         wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
5239                           params->ssid, params->ssid_len);
5240         NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5241                 params->ssid);
5242         os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5243         drv->ssid_len = params->ssid_len;
5244
5245         wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
5246         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
5247
5248         ret = nl80211_set_conn_keys(params, msg);
5249         if (ret)
5250                 goto nla_put_failure;
5251
5252         if (params->wpa_ie) {
5253                 wpa_hexdump(MSG_DEBUG,
5254                             "  * Extra IEs for Beacon/Probe Response frames",
5255                             params->wpa_ie, params->wpa_ie_len);
5256                 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5257                         params->wpa_ie);
5258         }
5259
5260         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5261         msg = NULL;
5262         if (ret) {
5263                 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
5264                            ret, strerror(-ret));
5265                 count++;
5266                 if (ret == -EALREADY && count == 1) {
5267                         wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
5268                                    "forced leave");
5269                         nl80211_leave_ibss(drv);
5270                         nlmsg_free(msg);
5271                         goto retry;
5272                 }
5273
5274                 goto nla_put_failure;
5275         }
5276         ret = 0;
5277         wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
5278
5279 nla_put_failure:
5280         nlmsg_free(msg);
5281         return ret;
5282 }
5283
5284
5285 static unsigned int nl80211_get_assoc_bssid(struct wpa_driver_nl80211_data *drv,
5286                                             u8 *bssid)
5287 {
5288         struct nl_msg *msg;
5289         int ret;
5290         struct nl80211_bss_info_arg arg;
5291
5292         os_memset(&arg, 0, sizeof(arg));
5293         msg = nlmsg_alloc();
5294         if (!msg)
5295                 goto nla_put_failure;
5296
5297         nl80211_cmd(drv, msg, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
5298         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5299
5300         arg.drv = drv;
5301         ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
5302         msg = NULL;
5303         if (ret == 0) {
5304                 if (is_zero_ether_addr(arg.assoc_bssid))
5305                         return -ENOTCONN;
5306                 os_memcpy(bssid, arg.assoc_bssid, ETH_ALEN);
5307                 return 0;
5308         }
5309         wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
5310                    "(%s)", ret, strerror(-ret));
5311 nla_put_failure:
5312         nlmsg_free(msg);
5313         return drv->assoc_freq;
5314 }
5315
5316
5317 static int nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
5318                               const u8 *bssid)
5319 {
5320         u8 addr[ETH_ALEN];
5321
5322         if (bssid == NULL) {
5323                 int res = nl80211_get_assoc_bssid(drv, addr);
5324                 if (res)
5325                         return res;
5326                 bssid = addr;
5327         }
5328
5329         return wpa_driver_nl80211_disconnect(drv, bssid,
5330                                              WLAN_REASON_PREV_AUTH_NOT_VALID);
5331 }
5332
5333
5334 static int wpa_driver_nl80211_connect(
5335         struct wpa_driver_nl80211_data *drv,
5336         struct wpa_driver_associate_params *params)
5337 {
5338         struct nl_msg *msg;
5339         enum nl80211_auth_type type;
5340         int ret = 0;
5341         int algs;
5342
5343         msg = nlmsg_alloc();
5344         if (!msg)
5345                 return -1;
5346
5347         wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
5348         nl80211_cmd(drv, msg, 0, NL80211_CMD_CONNECT);
5349
5350         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5351         if (params->bssid) {
5352                 wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
5353                            MAC2STR(params->bssid));
5354                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
5355         }
5356         if (params->freq) {
5357                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
5358                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
5359         }
5360         if (params->ssid) {
5361                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
5362                                   params->ssid, params->ssid_len);
5363                 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5364                         params->ssid);
5365                 if (params->ssid_len > sizeof(drv->ssid))
5366                         goto nla_put_failure;
5367                 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5368                 drv->ssid_len = params->ssid_len;
5369         }
5370         wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie, params->wpa_ie_len);
5371         if (params->wpa_ie)
5372                 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5373                         params->wpa_ie);
5374
5375         algs = 0;
5376         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5377                 algs++;
5378         if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5379                 algs++;
5380         if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5381                 algs++;
5382         if (algs > 1) {
5383                 wpa_printf(MSG_DEBUG, "  * Leave out Auth Type for automatic "
5384                            "selection");
5385                 goto skip_auth_type;
5386         }
5387
5388         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5389                 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
5390         else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5391                 type = NL80211_AUTHTYPE_SHARED_KEY;
5392         else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5393                 type = NL80211_AUTHTYPE_NETWORK_EAP;
5394         else if (params->auth_alg & WPA_AUTH_ALG_FT)
5395                 type = NL80211_AUTHTYPE_FT;
5396         else
5397                 goto nla_put_failure;
5398
5399         wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
5400         NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
5401
5402 skip_auth_type:
5403         if (params->wpa_proto) {
5404                 enum nl80211_wpa_versions ver = 0;
5405
5406                 if (params->wpa_proto & WPA_PROTO_WPA)
5407                         ver |= NL80211_WPA_VERSION_1;
5408                 if (params->wpa_proto & WPA_PROTO_RSN)
5409                         ver |= NL80211_WPA_VERSION_2;
5410
5411                 wpa_printf(MSG_DEBUG, "  * WPA Versions 0x%x", ver);
5412                 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
5413         }
5414
5415         if (params->pairwise_suite != CIPHER_NONE) {
5416                 int cipher;
5417
5418                 switch (params->pairwise_suite) {
5419                 case CIPHER_WEP40:
5420                         cipher = WLAN_CIPHER_SUITE_WEP40;
5421                         break;
5422                 case CIPHER_WEP104:
5423                         cipher = WLAN_CIPHER_SUITE_WEP104;
5424                         break;
5425                 case CIPHER_CCMP:
5426                         cipher = WLAN_CIPHER_SUITE_CCMP;
5427                         break;
5428                 case CIPHER_TKIP:
5429                 default:
5430                         cipher = WLAN_CIPHER_SUITE_TKIP;
5431                         break;
5432                 }
5433                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
5434         }
5435
5436         if (params->group_suite != CIPHER_NONE) {
5437                 int cipher;
5438
5439                 switch (params->group_suite) {
5440                 case CIPHER_WEP40:
5441                         cipher = WLAN_CIPHER_SUITE_WEP40;
5442                         break;
5443                 case CIPHER_WEP104:
5444                         cipher = WLAN_CIPHER_SUITE_WEP104;
5445                         break;
5446                 case CIPHER_CCMP:
5447                         cipher = WLAN_CIPHER_SUITE_CCMP;
5448                         break;
5449                 case CIPHER_TKIP:
5450                 default:
5451                         cipher = WLAN_CIPHER_SUITE_TKIP;
5452                         break;
5453                 }
5454                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
5455         }
5456
5457         if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
5458             params->key_mgmt_suite == KEY_MGMT_PSK) {
5459                 int mgmt = WLAN_AKM_SUITE_PSK;
5460
5461                 switch (params->key_mgmt_suite) {
5462                 case KEY_MGMT_802_1X:
5463                         mgmt = WLAN_AKM_SUITE_8021X;
5464                         break;
5465                 case KEY_MGMT_PSK:
5466                 default:
5467                         mgmt = WLAN_AKM_SUITE_PSK;
5468                         break;
5469                 }
5470                 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
5471         }
5472
5473         ret = nl80211_set_conn_keys(params, msg);
5474         if (ret)
5475                 goto nla_put_failure;
5476
5477         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5478         msg = NULL;
5479         if (ret) {
5480                 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
5481                            "(%s)", ret, strerror(-ret));
5482                 /*
5483                  * cfg80211 does not currently accept new connection if we are
5484                  * already connected. As a workaround, force disconnection and
5485                  * try again once the driver indicates it completed
5486                  * disconnection.
5487                  */
5488                 if (ret == -EALREADY)
5489                         nl80211_disconnect(drv, params->bssid);
5490                 goto nla_put_failure;
5491         }
5492         ret = 0;
5493         wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
5494
5495 nla_put_failure:
5496         nlmsg_free(msg);
5497         return ret;
5498
5499 }
5500
5501
5502 static int wpa_driver_nl80211_associate(
5503         void *priv, struct wpa_driver_associate_params *params)
5504 {
5505         struct i802_bss *bss = priv;
5506         struct wpa_driver_nl80211_data *drv = bss->drv;
5507         int ret = -1;
5508         struct nl_msg *msg;
5509
5510         if (params->mode == IEEE80211_MODE_AP)
5511                 return wpa_driver_nl80211_ap(drv, params);
5512
5513         if (params->mode == IEEE80211_MODE_IBSS)
5514                 return wpa_driver_nl80211_ibss(drv, params);
5515
5516         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
5517                 enum nl80211_iftype nlmode = params->p2p ?
5518                         NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5519
5520                 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
5521                         return -1;
5522                 return wpa_driver_nl80211_connect(drv, params);
5523         }
5524
5525         drv->associated = 0;
5526
5527         msg = nlmsg_alloc();
5528         if (!msg)
5529                 return -1;
5530
5531         wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
5532                    drv->ifindex);
5533         nl80211_cmd(drv, msg, 0, NL80211_CMD_ASSOCIATE);
5534
5535         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5536         if (params->bssid) {
5537                 wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
5538                            MAC2STR(params->bssid));
5539                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
5540         }
5541         if (params->freq) {
5542                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
5543                 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
5544                 drv->assoc_freq = params->freq;
5545         } else
5546                 drv->assoc_freq = 0;
5547         if (params->ssid) {
5548                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
5549                                   params->ssid, params->ssid_len);
5550                 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
5551                         params->ssid);
5552                 if (params->ssid_len > sizeof(drv->ssid))
5553                         goto nla_put_failure;
5554                 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5555                 drv->ssid_len = params->ssid_len;
5556         }
5557         wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie, params->wpa_ie_len);
5558         if (params->wpa_ie)
5559                 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5560                         params->wpa_ie);
5561
5562         if (params->pairwise_suite != CIPHER_NONE) {
5563                 int cipher;
5564
5565                 switch (params->pairwise_suite) {
5566                 case CIPHER_WEP40:
5567                         cipher = WLAN_CIPHER_SUITE_WEP40;
5568                         break;
5569                 case CIPHER_WEP104:
5570                         cipher = WLAN_CIPHER_SUITE_WEP104;
5571                         break;
5572                 case CIPHER_CCMP:
5573                         cipher = WLAN_CIPHER_SUITE_CCMP;
5574                         break;
5575                 case CIPHER_TKIP:
5576                 default:
5577                         cipher = WLAN_CIPHER_SUITE_TKIP;
5578                         break;
5579                 }
5580                 wpa_printf(MSG_DEBUG, "  * pairwise=0x%x", cipher);
5581                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
5582         }
5583
5584         if (params->group_suite != CIPHER_NONE) {
5585                 int cipher;
5586
5587                 switch (params->group_suite) {
5588                 case CIPHER_WEP40:
5589                         cipher = WLAN_CIPHER_SUITE_WEP40;
5590                         break;
5591                 case CIPHER_WEP104:
5592                         cipher = WLAN_CIPHER_SUITE_WEP104;
5593                         break;
5594                 case CIPHER_CCMP:
5595                         cipher = WLAN_CIPHER_SUITE_CCMP;
5596                         break;
5597                 case CIPHER_TKIP:
5598                 default:
5599                         cipher = WLAN_CIPHER_SUITE_TKIP;
5600                         break;
5601                 }
5602                 wpa_printf(MSG_DEBUG, "  * group=0x%x", cipher);
5603                 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
5604         }
5605
5606 #ifdef CONFIG_IEEE80211W
5607         if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
5608                 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
5609 #endif /* CONFIG_IEEE80211W */
5610
5611         NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
5612
5613         if (params->prev_bssid) {
5614                 wpa_printf(MSG_DEBUG, "  * prev_bssid=" MACSTR,
5615                            MAC2STR(params->prev_bssid));
5616                 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
5617                         params->prev_bssid);
5618         }
5619
5620         if (params->p2p)
5621                 wpa_printf(MSG_DEBUG, "  * P2P group");
5622
5623         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5624         msg = NULL;
5625         if (ret) {
5626                 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
5627                            "(%s)", ret, strerror(-ret));
5628                 nl80211_dump_scan(drv);
5629                 goto nla_put_failure;
5630         }
5631         ret = 0;
5632         wpa_printf(MSG_DEBUG, "nl80211: Association request send "
5633                    "successfully");
5634
5635 nla_put_failure:
5636         nlmsg_free(msg);
5637         return ret;
5638 }
5639
5640
5641 static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
5642                             int ifindex, enum nl80211_iftype mode)
5643 {
5644         struct nl_msg *msg;
5645         int ret = -ENOBUFS;
5646
5647         wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
5648                    ifindex, mode, nl80211_iftype_str(mode));
5649
5650         msg = nlmsg_alloc();
5651         if (!msg)
5652                 return -ENOMEM;
5653
5654         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_INTERFACE);
5655         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
5656         NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
5657
5658         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5659         if (!ret)
5660                 return 0;
5661 nla_put_failure:
5662         wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
5663                    " %d (%s)", ifindex, mode, ret, strerror(-ret));
5664         return ret;
5665 }
5666
5667
5668 static int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
5669                                        enum nl80211_iftype nlmode)
5670 {
5671         struct wpa_driver_nl80211_data *drv = bss->drv;
5672         int ret = -1;
5673         int i;
5674         int was_ap = is_ap_interface(drv->nlmode);
5675
5676         if (nl80211_set_mode(drv, drv->ifindex, nlmode) == 0) {
5677                 drv->nlmode = nlmode;
5678                 ret = 0;
5679                 goto done;
5680         }
5681
5682         if (nlmode == drv->nlmode) {
5683                 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
5684                            "requested mode - ignore error");
5685                 ret = 0;
5686                 goto done; /* Already in the requested mode */
5687         }
5688
5689         /* mac80211 doesn't allow mode changes while the device is up, so
5690          * take the device down, try to set the mode again, and bring the
5691          * device back up.
5692          */
5693         wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
5694                    "interface down");
5695         for (i = 0; i < 10; i++) {
5696                 int res;
5697                 res = linux_set_iface_flags(drv->global->ioctl_sock,
5698                                             bss->ifname, 0);
5699                 if (res == -EACCES || res == -ENODEV)
5700                         break;
5701                 if (res == 0) {
5702                         /* Try to set the mode again while the interface is
5703                          * down */
5704                         ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
5705                         if (ret == -EACCES)
5706                                 break;
5707                         res = linux_set_iface_flags(drv->global->ioctl_sock,
5708                                                     bss->ifname, 1);
5709                         if (res && !ret)
5710                                 ret = -1;
5711                         else if (ret != -EBUSY)
5712                                 break;
5713                 } else
5714                         wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
5715                                    "interface down");
5716                 os_sleep(0, 100000);
5717         }
5718
5719         if (!ret) {
5720                 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5721                            "interface is down");
5722                 drv->nlmode = nlmode;
5723                 drv->ignore_if_down_event = 1;
5724         }
5725
5726 done:
5727         if (!ret && is_ap_interface(nlmode)) {
5728                 /* Setup additional AP mode functionality if needed */
5729                 if (!drv->no_monitor_iface_capab && drv->monitor_ifidx < 0 &&
5730                     nl80211_create_monitor_interface(drv) &&
5731                     !drv->no_monitor_iface_capab)
5732                         return -1;
5733         } else if (!ret && !is_ap_interface(nlmode)) {
5734                 /* Remove additional AP mode functionality */
5735                 if (was_ap && drv->no_monitor_iface_capab)
5736                         wpa_driver_nl80211_probe_req_report(bss, 0);
5737                 nl80211_remove_monitor_interface(drv);
5738                 bss->beacon_set = 0;
5739         }
5740
5741         if (ret)
5742                 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5743                            "from %d failed", nlmode, drv->nlmode);
5744
5745         return ret;
5746 }
5747
5748
5749 static int wpa_driver_nl80211_get_capa(void *priv,
5750                                        struct wpa_driver_capa *capa)
5751 {
5752         struct i802_bss *bss = priv;
5753         struct wpa_driver_nl80211_data *drv = bss->drv;
5754         if (!drv->has_capability)
5755                 return -1;
5756         os_memcpy(capa, &drv->capa, sizeof(*capa));
5757         return 0;
5758 }
5759
5760
5761 static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5762 {
5763         struct i802_bss *bss = priv;
5764         struct wpa_driver_nl80211_data *drv = bss->drv;
5765
5766         wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
5767                    __func__, drv->operstate, state, state ? "UP" : "DORMANT");
5768         drv->operstate = state;
5769         return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
5770                                       state ? IF_OPER_UP : IF_OPER_DORMANT);
5771 }
5772
5773
5774 static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5775 {
5776         struct i802_bss *bss = priv;
5777         struct wpa_driver_nl80211_data *drv = bss->drv;
5778         struct nl_msg *msg;
5779         struct nl80211_sta_flag_update upd;
5780
5781         msg = nlmsg_alloc();
5782         if (!msg)
5783                 return -ENOMEM;
5784
5785         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
5786
5787         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
5788                     if_nametoindex(bss->ifname));
5789         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
5790
5791         os_memset(&upd, 0, sizeof(upd));
5792         upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5793         if (authorized)
5794                 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
5795         NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
5796
5797         return send_and_recv_msgs(drv, msg, NULL, NULL);
5798  nla_put_failure:
5799         return -ENOBUFS;
5800 }
5801
5802
5803 /* Set kernel driver on given frequency (MHz) */
5804 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
5805 {
5806         struct i802_bss *bss = priv;
5807         struct wpa_driver_nl80211_data *drv = bss->drv;
5808         return wpa_driver_nl80211_set_freq(drv, freq->freq, freq->ht_enabled,
5809                                            freq->sec_channel_offset);
5810 }
5811
5812
5813 #if defined(HOSTAPD) || defined(CONFIG_AP)
5814
5815 static inline int min_int(int a, int b)
5816 {
5817         if (a < b)
5818                 return a;
5819         return b;
5820 }
5821
5822
5823 static int get_key_handler(struct nl_msg *msg, void *arg)
5824 {
5825         struct nlattr *tb[NL80211_ATTR_MAX + 1];
5826         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5827
5828         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5829                   genlmsg_attrlen(gnlh, 0), NULL);
5830
5831         /*
5832          * TODO: validate the key index and mac address!
5833          * Otherwise, there's a race condition as soon as
5834          * the kernel starts sending key notifications.
5835          */
5836
5837         if (tb[NL80211_ATTR_KEY_SEQ])
5838                 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5839                        min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5840         return NL_SKIP;
5841 }
5842
5843
5844 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5845                            int idx, u8 *seq)
5846 {
5847         struct i802_bss *bss = priv;
5848         struct wpa_driver_nl80211_data *drv = bss->drv;
5849         struct nl_msg *msg;
5850
5851         msg = nlmsg_alloc();
5852         if (!msg)
5853                 return -ENOMEM;
5854
5855         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_KEY);
5856
5857         if (addr)
5858                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5859         NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
5860         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
5861
5862         memset(seq, 0, 6);
5863
5864         return send_and_recv_msgs(drv, msg, get_key_handler, seq);
5865  nla_put_failure:
5866         return -ENOBUFS;
5867 }
5868
5869
5870 static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
5871                               int mode)
5872 {
5873         struct i802_bss *bss = priv;
5874         struct wpa_driver_nl80211_data *drv = bss->drv;
5875         struct nl_msg *msg;
5876         u8 rates[NL80211_MAX_SUPP_RATES];
5877         u8 rates_len = 0;
5878         int i;
5879
5880         msg = nlmsg_alloc();
5881         if (!msg)
5882                 return -ENOMEM;
5883
5884         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_BSS);
5885
5886         for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
5887                 rates[rates_len++] = basic_rates[i] / 5;
5888
5889         NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
5890
5891         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
5892
5893         return send_and_recv_msgs(drv, msg, NULL, NULL);
5894  nla_put_failure:
5895         return -ENOBUFS;
5896 }
5897
5898
5899 static int i802_set_rts(void *priv, int rts)
5900 {
5901         struct i802_bss *bss = priv;
5902         struct wpa_driver_nl80211_data *drv = bss->drv;
5903         struct nl_msg *msg;
5904         int ret = -ENOBUFS;
5905         u32 val;
5906
5907         msg = nlmsg_alloc();
5908         if (!msg)
5909                 return -ENOMEM;
5910
5911         if (rts >= 2347)
5912                 val = (u32) -1;
5913         else
5914                 val = rts;
5915
5916         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
5917         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5918         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
5919
5920         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5921         if (!ret)
5922                 return 0;
5923 nla_put_failure:
5924         wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5925                    "%d (%s)", rts, ret, strerror(-ret));
5926         return ret;
5927 }
5928
5929
5930 static int i802_set_frag(void *priv, int frag)
5931 {
5932         struct i802_bss *bss = priv;
5933         struct wpa_driver_nl80211_data *drv = bss->drv;
5934         struct nl_msg *msg;
5935         int ret = -ENOBUFS;
5936         u32 val;
5937
5938         msg = nlmsg_alloc();
5939         if (!msg)
5940                 return -ENOMEM;
5941
5942         if (frag >= 2346)
5943                 val = (u32) -1;
5944         else
5945                 val = frag;
5946
5947         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
5948         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
5949         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
5950
5951         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5952         if (!ret)
5953                 return 0;
5954 nla_put_failure:
5955         wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
5956                    "%d: %d (%s)", frag, ret, strerror(-ret));
5957         return ret;
5958 }
5959
5960
5961 static int i802_flush(void *priv)
5962 {
5963         struct i802_bss *bss = priv;
5964         struct wpa_driver_nl80211_data *drv = bss->drv;
5965         struct nl_msg *msg;
5966
5967         msg = nlmsg_alloc();
5968         if (!msg)
5969                 return -1;
5970
5971         nl80211_cmd(drv, msg, 0, NL80211_CMD_DEL_STATION);
5972
5973         /*
5974          * XXX: FIX! this needs to flush all VLANs too
5975          */
5976         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
5977                     if_nametoindex(bss->ifname));
5978
5979         return send_and_recv_msgs(drv, msg, NULL, NULL);
5980  nla_put_failure:
5981         return -ENOBUFS;
5982 }
5983
5984
5985 static int get_sta_handler(struct nl_msg *msg, void *arg)
5986 {
5987         struct nlattr *tb[NL80211_ATTR_MAX + 1];
5988         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5989         struct hostap_sta_driver_data *data = arg;
5990         struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
5991         static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
5992                 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
5993                 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
5994                 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
5995                 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
5996                 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
5997         };
5998
5999         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6000                   genlmsg_attrlen(gnlh, 0), NULL);
6001
6002         /*
6003          * TODO: validate the interface and mac address!
6004          * Otherwise, there's a race condition as soon as
6005          * the kernel starts sending station notifications.
6006          */
6007
6008         if (!tb[NL80211_ATTR_STA_INFO]) {
6009                 wpa_printf(MSG_DEBUG, "sta stats missing!");
6010                 return NL_SKIP;
6011         }
6012         if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
6013                              tb[NL80211_ATTR_STA_INFO],
6014                              stats_policy)) {
6015                 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
6016                 return NL_SKIP;
6017         }
6018
6019         if (stats[NL80211_STA_INFO_INACTIVE_TIME])
6020                 data->inactive_msec =
6021                         nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
6022         if (stats[NL80211_STA_INFO_RX_BYTES])
6023                 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
6024         if (stats[NL80211_STA_INFO_TX_BYTES])
6025                 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
6026         if (stats[NL80211_STA_INFO_RX_PACKETS])
6027                 data->rx_packets =
6028                         nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
6029         if (stats[NL80211_STA_INFO_TX_PACKETS])
6030                 data->tx_packets =
6031                         nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
6032
6033         return NL_SKIP;
6034 }
6035
6036 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
6037                               const u8 *addr)
6038 {
6039         struct i802_bss *bss = priv;
6040         struct wpa_driver_nl80211_data *drv = bss->drv;
6041         struct nl_msg *msg;
6042
6043         os_memset(data, 0, sizeof(*data));
6044         msg = nlmsg_alloc();
6045         if (!msg)
6046                 return -ENOMEM;
6047
6048         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_STATION);
6049
6050         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6051         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6052
6053         return send_and_recv_msgs(drv, msg, get_sta_handler, data);
6054  nla_put_failure:
6055         return -ENOBUFS;
6056 }
6057
6058
6059 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
6060                                     int cw_min, int cw_max, int burst_time)
6061 {
6062         struct i802_bss *bss = priv;
6063         struct wpa_driver_nl80211_data *drv = bss->drv;
6064         struct nl_msg *msg;
6065         struct nlattr *txq, *params;
6066
6067         msg = nlmsg_alloc();
6068         if (!msg)
6069                 return -1;
6070
6071         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_WIPHY);
6072
6073         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
6074
6075         txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
6076         if (!txq)
6077                 goto nla_put_failure;
6078
6079         /* We are only sending parameters for a single TXQ at a time */
6080         params = nla_nest_start(msg, 1);
6081         if (!params)
6082                 goto nla_put_failure;
6083
6084         switch (queue) {
6085         case 0:
6086                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO);
6087                 break;
6088         case 1:
6089                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI);
6090                 break;
6091         case 2:
6092                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE);
6093                 break;
6094         case 3:
6095                 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK);
6096                 break;
6097         }
6098         /* Burst time is configured in units of 0.1 msec and TXOP parameter in
6099          * 32 usec, so need to convert the value here. */
6100         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
6101         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
6102         NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
6103         NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
6104
6105         nla_nest_end(msg, params);
6106
6107         nla_nest_end(msg, txq);
6108
6109         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
6110                 return 0;
6111  nla_put_failure:
6112         return -1;
6113 }
6114
6115
6116 static int i802_set_sta_vlan(void *priv, const u8 *addr,
6117                              const char *ifname, int vlan_id)
6118 {
6119         struct i802_bss *bss = priv;
6120         struct wpa_driver_nl80211_data *drv = bss->drv;
6121         struct nl_msg *msg;
6122         int ret = -ENOBUFS;
6123
6124         msg = nlmsg_alloc();
6125         if (!msg)
6126                 return -ENOMEM;
6127
6128         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_STATION);
6129
6130         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
6131                     if_nametoindex(bss->ifname));
6132         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6133         NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
6134                     if_nametoindex(ifname));
6135
6136         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6137         if (ret < 0) {
6138                 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
6139                            MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
6140                            MAC2STR(addr), ifname, vlan_id, ret,
6141                            strerror(-ret));
6142         }
6143  nla_put_failure:
6144         return ret;
6145 }
6146
6147
6148 static int i802_get_inact_sec(void *priv, const u8 *addr)
6149 {
6150         struct hostap_sta_driver_data data;
6151         int ret;
6152
6153         data.inactive_msec = (unsigned long) -1;
6154         ret = i802_read_sta_data(priv, &data, addr);
6155         if (ret || data.inactive_msec == (unsigned long) -1)
6156                 return -1;
6157         return data.inactive_msec / 1000;
6158 }
6159
6160
6161 static int i802_sta_clear_stats(void *priv, const u8 *addr)
6162 {
6163 #if 0
6164         /* TODO */
6165 #endif
6166         return 0;
6167 }
6168
6169
6170 static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
6171                            int reason)
6172 {
6173         struct i802_bss *bss = priv;
6174         struct ieee80211_mgmt mgmt;
6175
6176         memset(&mgmt, 0, sizeof(mgmt));
6177         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6178                                           WLAN_FC_STYPE_DEAUTH);
6179         memcpy(mgmt.da, addr, ETH_ALEN);
6180         memcpy(mgmt.sa, own_addr, ETH_ALEN);
6181         memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6182         mgmt.u.deauth.reason_code = host_to_le16(reason);
6183         return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6184                                             IEEE80211_HDRLEN +
6185                                             sizeof(mgmt.u.deauth));
6186 }
6187
6188
6189 static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
6190                              int reason)
6191 {
6192         struct i802_bss *bss = priv;
6193         struct ieee80211_mgmt mgmt;
6194
6195         memset(&mgmt, 0, sizeof(mgmt));
6196         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6197                                           WLAN_FC_STYPE_DISASSOC);
6198         memcpy(mgmt.da, addr, ETH_ALEN);
6199         memcpy(mgmt.sa, own_addr, ETH_ALEN);
6200         memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6201         mgmt.u.disassoc.reason_code = host_to_le16(reason);
6202         return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6203                                             IEEE80211_HDRLEN +
6204                                             sizeof(mgmt.u.disassoc));
6205 }
6206
6207 #endif /* HOSTAPD || CONFIG_AP */
6208
6209 #ifdef HOSTAPD
6210
6211 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
6212 {
6213         int i;
6214         int *old;
6215
6216         wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
6217                    ifidx);
6218         for (i = 0; i < drv->num_if_indices; i++) {
6219                 if (drv->if_indices[i] == 0) {
6220                         drv->if_indices[i] = ifidx;
6221                         return;
6222                 }
6223         }
6224
6225         if (drv->if_indices != drv->default_if_indices)
6226                 old = drv->if_indices;
6227         else
6228                 old = NULL;
6229
6230         drv->if_indices = os_realloc(old,
6231                                      sizeof(int) * (drv->num_if_indices + 1));
6232         if (!drv->if_indices) {
6233                 if (!old)
6234                         drv->if_indices = drv->default_if_indices;
6235                 else
6236                         drv->if_indices = old;
6237                 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
6238                            "interfaces");
6239                 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
6240                 return;
6241         } else if (!old)
6242                 os_memcpy(drv->if_indices, drv->default_if_indices,
6243                           sizeof(drv->default_if_indices));
6244         drv->if_indices[drv->num_if_indices] = ifidx;
6245         drv->num_if_indices++;
6246 }
6247
6248
6249 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
6250 {
6251         int i;
6252
6253         for (i = 0; i < drv->num_if_indices; i++) {
6254                 if (drv->if_indices[i] == ifidx) {
6255                         drv->if_indices[i] = 0;
6256                         break;
6257                 }
6258         }
6259 }
6260
6261
6262 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
6263 {
6264         int i;
6265
6266         for (i = 0; i < drv->num_if_indices; i++)
6267                 if (drv->if_indices[i] == ifidx)
6268                         return 1;
6269
6270         return 0;
6271 }
6272
6273
6274 static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
6275                             const char *bridge_ifname)
6276 {
6277         struct i802_bss *bss = priv;
6278         struct wpa_driver_nl80211_data *drv = bss->drv;
6279         char name[IFNAMSIZ + 1];
6280
6281         os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
6282         wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
6283                    " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
6284         if (val) {
6285                 if (!if_nametoindex(name)) {
6286                         if (nl80211_create_iface(drv, name,
6287                                                  NL80211_IFTYPE_AP_VLAN,
6288                                                  NULL, 1) < 0)
6289                                 return -1;
6290                         if (bridge_ifname &&
6291                             linux_br_add_if(drv->global->ioctl_sock,
6292                                             bridge_ifname, name) < 0)
6293                                 return -1;
6294                 }
6295                 linux_set_iface_flags(drv->global->ioctl_sock, name, 1);
6296                 return i802_set_sta_vlan(priv, addr, name, 0);
6297         } else {
6298                 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
6299                 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
6300                                                     name);
6301         }
6302 }
6303
6304
6305 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
6306 {
6307         struct wpa_driver_nl80211_data *drv = eloop_ctx;
6308         struct sockaddr_ll lladdr;
6309         unsigned char buf[3000];
6310         int len;
6311         socklen_t fromlen = sizeof(lladdr);
6312
6313         len = recvfrom(sock, buf, sizeof(buf), 0,
6314                        (struct sockaddr *)&lladdr, &fromlen);
6315         if (len < 0) {
6316                 perror("recv");
6317                 return;
6318         }
6319
6320         if (have_ifidx(drv, lladdr.sll_ifindex))
6321                 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
6322 }
6323
6324
6325 static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
6326                              struct i802_bss *bss,
6327                              const char *brname, const char *ifname)
6328 {
6329         int ifindex;
6330         char in_br[IFNAMSIZ];
6331
6332         os_strlcpy(bss->brname, brname, IFNAMSIZ);
6333         ifindex = if_nametoindex(brname);
6334         if (ifindex == 0) {
6335                 /*
6336                  * Bridge was configured, but the bridge device does
6337                  * not exist. Try to add it now.
6338                  */
6339                 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
6340                         wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
6341                                    "bridge interface %s: %s",
6342                                    brname, strerror(errno));
6343                         return -1;
6344                 }
6345                 bss->added_bridge = 1;
6346                 add_ifidx(drv, if_nametoindex(brname));
6347         }
6348
6349         if (linux_br_get(in_br, ifname) == 0) {
6350                 if (os_strcmp(in_br, brname) == 0)
6351                         return 0; /* already in the bridge */
6352
6353                 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
6354                            "bridge %s", ifname, in_br);
6355                 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
6356                     0) {
6357                         wpa_printf(MSG_ERROR, "nl80211: Failed to "
6358                                    "remove interface %s from bridge "
6359                                    "%s: %s",
6360                                    ifname, brname, strerror(errno));
6361                         return -1;
6362                 }
6363         }
6364
6365         wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
6366                    ifname, brname);
6367         if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
6368                 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
6369                            "into bridge %s: %s",
6370                            ifname, brname, strerror(errno));
6371                 return -1;
6372         }
6373         bss->added_if_into_bridge = 1;
6374
6375         return 0;
6376 }
6377
6378
6379 static void *i802_init(struct hostapd_data *hapd,
6380                        struct wpa_init_params *params)
6381 {
6382         struct wpa_driver_nl80211_data *drv;
6383         struct i802_bss *bss;
6384         size_t i;
6385         char brname[IFNAMSIZ];
6386         int ifindex, br_ifindex;
6387         int br_added = 0;
6388
6389         bss = wpa_driver_nl80211_init(hapd, params->ifname,
6390                                       params->global_priv);
6391         if (bss == NULL)
6392                 return NULL;
6393
6394         drv = bss->drv;
6395         drv->nlmode = NL80211_IFTYPE_AP;
6396         drv->eapol_sock = -1;
6397
6398         if (linux_br_get(brname, params->ifname) == 0) {
6399                 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
6400                            params->ifname, brname);
6401                 br_ifindex = if_nametoindex(brname);
6402         } else {
6403                 brname[0] = '\0';
6404                 br_ifindex = 0;
6405         }
6406
6407         drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
6408         drv->if_indices = drv->default_if_indices;
6409         for (i = 0; i < params->num_bridge; i++) {
6410                 if (params->bridge[i]) {
6411                         ifindex = if_nametoindex(params->bridge[i]);
6412                         if (ifindex)
6413                                 add_ifidx(drv, ifindex);
6414                         if (ifindex == br_ifindex)
6415                                 br_added = 1;
6416                 }
6417         }
6418         if (!br_added && br_ifindex &&
6419             (params->num_bridge == 0 || !params->bridge[0]))
6420                 add_ifidx(drv, br_ifindex);
6421
6422         /* start listening for EAPOL on the default AP interface */
6423         add_ifidx(drv, drv->ifindex);
6424
6425         if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0))
6426                 goto failed;
6427
6428         if (params->bssid) {
6429                 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6430                                        params->bssid))
6431                         goto failed;
6432         }
6433
6434         if (wpa_driver_nl80211_set_mode(bss, drv->nlmode)) {
6435                 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
6436                            "into AP mode", bss->ifname);
6437                 goto failed;
6438         }
6439
6440         if (params->num_bridge && params->bridge[0] &&
6441             i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
6442                 goto failed;
6443
6444         if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1))
6445                 goto failed;
6446
6447         drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
6448         if (drv->eapol_sock < 0) {
6449                 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
6450                 goto failed;
6451         }
6452
6453         if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
6454         {
6455                 printf("Could not register read socket for eapol\n");
6456                 goto failed;
6457         }
6458
6459         if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6460                                params->own_addr))
6461                 goto failed;
6462
6463         return bss;
6464
6465 failed:
6466         wpa_driver_nl80211_deinit(bss);
6467         return NULL;
6468 }
6469
6470
6471 static void i802_deinit(void *priv)
6472 {
6473         wpa_driver_nl80211_deinit(priv);
6474 }
6475
6476 #endif /* HOSTAPD */
6477
6478
6479 static enum nl80211_iftype wpa_driver_nl80211_if_type(
6480         enum wpa_driver_if_type type)
6481 {
6482         switch (type) {
6483         case WPA_IF_STATION:
6484                 return NL80211_IFTYPE_STATION;
6485         case WPA_IF_P2P_CLIENT:
6486         case WPA_IF_P2P_GROUP:
6487                 return NL80211_IFTYPE_P2P_CLIENT;
6488         case WPA_IF_AP_VLAN:
6489                 return NL80211_IFTYPE_AP_VLAN;
6490         case WPA_IF_AP_BSS:
6491                 return NL80211_IFTYPE_AP;
6492         case WPA_IF_P2P_GO:
6493                 return NL80211_IFTYPE_P2P_GO;
6494         }
6495         return -1;
6496 }
6497
6498
6499 #ifdef CONFIG_P2P
6500
6501 static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
6502 {
6503         struct wpa_driver_nl80211_data *drv;
6504         dl_list_for_each(drv, &global->interfaces,
6505                          struct wpa_driver_nl80211_data, list) {
6506                 if (os_memcmp(addr, drv->addr, ETH_ALEN) == 0)
6507                         return 1;
6508         }
6509         return 0;
6510 }
6511
6512
6513 static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
6514                                       u8 *new_addr)
6515 {
6516         unsigned int idx;
6517
6518         if (!drv->global)
6519                 return -1;
6520
6521         os_memcpy(new_addr, drv->addr, ETH_ALEN);
6522         for (idx = 0; idx < 64; idx++) {
6523                 new_addr[0] = drv->addr[0] | 0x02;
6524                 new_addr[0] ^= idx << 2;
6525                 if (!nl80211_addr_in_use(drv->global, new_addr))
6526                         break;
6527         }
6528         if (idx == 64)
6529                 return -1;
6530
6531         wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
6532                    MACSTR, MAC2STR(new_addr));
6533
6534         return 0;
6535 }
6536
6537 #endif /* CONFIG_P2P */
6538
6539
6540 static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
6541                                      const char *ifname, const u8 *addr,
6542                                      void *bss_ctx, void **drv_priv,
6543                                      char *force_ifname, u8 *if_addr,
6544                                      const char *bridge)
6545 {
6546         struct i802_bss *bss = priv;
6547         struct wpa_driver_nl80211_data *drv = bss->drv;
6548         int ifidx;
6549 #ifdef HOSTAPD
6550         struct i802_bss *new_bss = NULL;
6551
6552         if (type == WPA_IF_AP_BSS) {
6553                 new_bss = os_zalloc(sizeof(*new_bss));
6554                 if (new_bss == NULL)
6555                         return -1;
6556         }
6557 #endif /* HOSTAPD */
6558
6559         if (addr)
6560                 os_memcpy(if_addr, addr, ETH_ALEN);
6561         ifidx = nl80211_create_iface(drv, ifname,
6562                                      wpa_driver_nl80211_if_type(type), addr,
6563                                      0);
6564         if (ifidx < 0) {
6565 #ifdef HOSTAPD
6566                 os_free(new_bss);
6567 #endif /* HOSTAPD */
6568                 return -1;
6569         }
6570
6571         if (!addr &&
6572             linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6573                                if_addr) < 0) {
6574                 nl80211_remove_iface(drv, ifidx);
6575                 return -1;
6576         }
6577
6578 #ifdef CONFIG_P2P
6579         if (!addr &&
6580             (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
6581              type == WPA_IF_P2P_GO)) {
6582                 /* Enforce unique P2P Interface Address */
6583                 u8 new_addr[ETH_ALEN], own_addr[ETH_ALEN];
6584
6585                 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6586                                        own_addr) < 0 ||
6587                     linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
6588                                        new_addr) < 0) {
6589                         nl80211_remove_iface(drv, ifidx);
6590                         return -1;
6591                 }
6592                 if (os_memcmp(own_addr, new_addr, ETH_ALEN) == 0) {
6593                         wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
6594                                    "for P2P group interface");
6595                         if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
6596                                 nl80211_remove_iface(drv, ifidx);
6597                                 return -1;
6598                         }
6599                         if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
6600                                                new_addr) < 0) {
6601                                 nl80211_remove_iface(drv, ifidx);
6602                                 return -1;
6603                         }
6604                 }
6605                 os_memcpy(if_addr, new_addr, ETH_ALEN);
6606         }
6607 #endif /* CONFIG_P2P */
6608
6609 #ifdef HOSTAPD
6610         if (bridge &&
6611             i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
6612                 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
6613                            "interface %s to a bridge %s", ifname, bridge);
6614                 nl80211_remove_iface(drv, ifidx);
6615                 os_free(new_bss);
6616                 return -1;
6617         }
6618
6619         if (type == WPA_IF_AP_BSS) {
6620                 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
6621                 {
6622                         nl80211_remove_iface(drv, ifidx);
6623                         os_free(new_bss);
6624                         return -1;
6625                 }
6626                 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
6627                 new_bss->ifindex = ifidx;
6628                 new_bss->drv = drv;
6629                 new_bss->next = drv->first_bss.next;
6630                 drv->first_bss.next = new_bss;
6631                 if (drv_priv)
6632                         *drv_priv = new_bss;
6633         }
6634 #endif /* HOSTAPD */
6635
6636         if (drv->global)
6637                 drv->global->if_add_ifindex = ifidx;
6638
6639         return 0;
6640 }
6641
6642
6643 static int wpa_driver_nl80211_if_remove(void *priv,
6644                                         enum wpa_driver_if_type type,
6645                                         const char *ifname)
6646 {
6647         struct i802_bss *bss = priv;
6648         struct wpa_driver_nl80211_data *drv = bss->drv;
6649         int ifindex = if_nametoindex(ifname);
6650
6651         wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d",
6652                    __func__, type, ifname, ifindex);
6653         if (ifindex <= 0)
6654                 return -1;
6655
6656 #ifdef HOSTAPD
6657         if (bss->added_if_into_bridge) {
6658                 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
6659                                     bss->ifname) < 0)
6660                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6661                                    "interface %s from bridge %s: %s",
6662                                    bss->ifname, bss->brname, strerror(errno));
6663         }
6664         if (bss->added_bridge) {
6665                 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
6666                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6667                                    "bridge %s: %s",
6668                                    bss->brname, strerror(errno));
6669         }
6670 #endif /* HOSTAPD */
6671
6672         nl80211_remove_iface(drv, ifindex);
6673
6674 #ifdef HOSTAPD
6675         if (type != WPA_IF_AP_BSS)
6676                 return 0;
6677
6678         if (bss != &drv->first_bss) {
6679                 struct i802_bss *tbss;
6680
6681                 for (tbss = &drv->first_bss; tbss; tbss = tbss->next) {
6682                         if (tbss->next == bss) {
6683                                 tbss->next = bss->next;
6684                                 os_free(bss);
6685                                 bss = NULL;
6686                                 break;
6687                         }
6688                 }
6689                 if (bss)
6690                         wpa_printf(MSG_INFO, "nl80211: %s - could not find "
6691                                    "BSS %p in the list", __func__, bss);
6692         }
6693 #endif /* HOSTAPD */
6694
6695         return 0;
6696 }
6697
6698
6699 static int cookie_handler(struct nl_msg *msg, void *arg)
6700 {
6701         struct nlattr *tb[NL80211_ATTR_MAX + 1];
6702         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6703         u64 *cookie = arg;
6704         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6705                   genlmsg_attrlen(gnlh, 0), NULL);
6706         if (tb[NL80211_ATTR_COOKIE])
6707                 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
6708         return NL_SKIP;
6709 }
6710
6711
6712 static int nl80211_send_frame_cmd(struct wpa_driver_nl80211_data *drv,
6713                                   unsigned int freq, unsigned int wait,
6714                                   const u8 *buf, size_t buf_len,
6715                                   u64 *cookie_out)
6716 {
6717         struct nl_msg *msg;
6718         u64 cookie;
6719         int ret = -1;
6720
6721         msg = nlmsg_alloc();
6722         if (!msg)
6723                 return -1;
6724
6725         nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME);
6726
6727         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6728         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
6729         if (wait)
6730                 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, wait);
6731         NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
6732         NLA_PUT(msg, NL80211_ATTR_FRAME, buf_len, buf);
6733
6734         cookie = 0;
6735         ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6736         msg = NULL;
6737         if (ret) {
6738                 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
6739                            "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
6740                            freq, wait);
6741                 goto nla_put_failure;
6742         }
6743         wpa_printf(MSG_DEBUG, "nl80211: Frame TX command accepted; "
6744                    "cookie 0x%llx", (long long unsigned int) cookie);
6745
6746         if (cookie_out)
6747                 *cookie_out = cookie;
6748
6749 nla_put_failure:
6750         nlmsg_free(msg);
6751         return ret;
6752 }
6753
6754
6755 static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq,
6756                                           unsigned int wait_time,
6757                                           const u8 *dst, const u8 *src,
6758                                           const u8 *bssid,
6759                                           const u8 *data, size_t data_len)
6760 {
6761         struct i802_bss *bss = priv;
6762         struct wpa_driver_nl80211_data *drv = bss->drv;
6763         int ret = -1;
6764         u8 *buf;
6765         struct ieee80211_hdr *hdr;
6766
6767         wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
6768                    "wait=%d ms)", drv->ifindex, wait_time);
6769
6770         buf = os_zalloc(24 + data_len);
6771         if (buf == NULL)
6772                 return ret;
6773         os_memcpy(buf + 24, data, data_len);
6774         hdr = (struct ieee80211_hdr *) buf;
6775         hdr->frame_control =
6776                 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
6777         os_memcpy(hdr->addr1, dst, ETH_ALEN);
6778         os_memcpy(hdr->addr2, src, ETH_ALEN);
6779         os_memcpy(hdr->addr3, bssid, ETH_ALEN);
6780
6781         if (is_ap_interface(drv->nlmode))
6782                 ret = wpa_driver_nl80211_send_mlme(priv, buf, 24 + data_len);
6783         else
6784                 ret = nl80211_send_frame_cmd(drv, freq, wait_time, buf,
6785                                              24 + data_len,
6786                                              &drv->send_action_cookie);
6787
6788         os_free(buf);
6789         return ret;
6790 }
6791
6792
6793 static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
6794 {
6795         struct i802_bss *bss = priv;
6796         struct wpa_driver_nl80211_data *drv = bss->drv;
6797         struct nl_msg *msg;
6798         int ret;
6799
6800         msg = nlmsg_alloc();
6801         if (!msg)
6802                 return;
6803
6804         nl80211_cmd(drv, msg, 0, NL80211_CMD_FRAME_WAIT_CANCEL);
6805
6806         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6807         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie);
6808
6809         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6810         msg = NULL;
6811         if (ret)
6812                 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
6813                            "(%s)", ret, strerror(-ret));
6814
6815  nla_put_failure:
6816         nlmsg_free(msg);
6817 }
6818
6819
6820 static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
6821                                                 unsigned int duration)
6822 {
6823         struct i802_bss *bss = priv;
6824         struct wpa_driver_nl80211_data *drv = bss->drv;
6825         struct nl_msg *msg;
6826         int ret;
6827         u64 cookie;
6828
6829         msg = nlmsg_alloc();
6830         if (!msg)
6831                 return -1;
6832
6833         nl80211_cmd(drv, msg, 0, NL80211_CMD_REMAIN_ON_CHANNEL);
6834
6835         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6836         NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
6837         NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
6838
6839         cookie = 0;
6840         ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6841         if (ret == 0) {
6842                 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
6843                            "0x%llx for freq=%u MHz duration=%u",
6844                            (long long unsigned int) cookie, freq, duration);
6845                 drv->remain_on_chan_cookie = cookie;
6846                 drv->pending_remain_on_chan = 1;
6847                 return 0;
6848         }
6849         wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
6850                    "(freq=%d duration=%u): %d (%s)",
6851                    freq, duration, ret, strerror(-ret));
6852 nla_put_failure:
6853         return -1;
6854 }
6855
6856
6857 static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
6858 {
6859         struct i802_bss *bss = priv;
6860         struct wpa_driver_nl80211_data *drv = bss->drv;
6861         struct nl_msg *msg;
6862         int ret;
6863
6864         if (!drv->pending_remain_on_chan) {
6865                 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
6866                            "to cancel");
6867                 return -1;
6868         }
6869
6870         wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
6871                    "0x%llx",
6872                    (long long unsigned int) drv->remain_on_chan_cookie);
6873
6874         msg = nlmsg_alloc();
6875         if (!msg)
6876                 return -1;
6877
6878         nl80211_cmd(drv, msg, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
6879
6880         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
6881         NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
6882
6883         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6884         if (ret == 0)
6885                 return 0;
6886         wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
6887                    "%d (%s)", ret, strerror(-ret));
6888 nla_put_failure:
6889         return -1;
6890 }
6891
6892
6893 static int wpa_driver_nl80211_probe_req_report(void *priv, int report)
6894 {
6895         struct i802_bss *bss = priv;
6896         struct wpa_driver_nl80211_data *drv = bss->drv;
6897
6898         if (!report) {
6899                 if (drv->nl_preq.handle) {
6900                         eloop_unregister_read_sock(
6901                                 nl_socket_get_fd(drv->nl_preq.handle));
6902                         nl_destroy_handles(&drv->nl_preq);
6903                 }
6904                 return 0;
6905         }
6906
6907         if (drv->nl_preq.handle) {
6908                 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
6909                            "already on!");
6910                 return 0;
6911         }
6912
6913         if (nl_create_handles(&drv->nl_preq, drv->global->nl_cb, "preq"))
6914                 return -1;
6915
6916         if (nl80211_register_frame(drv, drv->nl_preq.handle,
6917                                    (WLAN_FC_TYPE_MGMT << 2) |
6918                                    (WLAN_FC_STYPE_PROBE_REQ << 4),
6919                                    NULL, 0) < 0)
6920                 goto out_err;
6921
6922         eloop_register_read_sock(nl_socket_get_fd(drv->nl_preq.handle),
6923                                  wpa_driver_nl80211_event_receive, drv,
6924                                  drv->nl_preq.handle);
6925
6926         return 0;
6927
6928  out_err:
6929         nl_destroy_handles(&drv->nl_preq);
6930         return -1;
6931 }
6932
6933
6934 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
6935                                      int ifindex, int disabled)
6936 {
6937         struct nl_msg *msg;
6938         struct nlattr *bands, *band;
6939         int ret;
6940
6941         msg = nlmsg_alloc();
6942         if (!msg)
6943                 return -1;
6944
6945         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_TX_BITRATE_MASK);
6946         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
6947
6948         bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
6949         if (!bands)
6950                 goto nla_put_failure;
6951
6952         /*
6953          * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
6954          * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
6955          * rates. All 5 GHz rates are left enabled.
6956          */
6957         band = nla_nest_start(msg, NL80211_BAND_2GHZ);
6958         if (!band)
6959                 goto nla_put_failure;
6960         NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8,
6961                 "\x0c\x12\x18\x24\x30\x48\x60\x6c");
6962         nla_nest_end(msg, band);
6963
6964         nla_nest_end(msg, bands);
6965
6966         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6967         msg = NULL;
6968         if (ret) {
6969                 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
6970                            "(%s)", ret, strerror(-ret));
6971         }
6972
6973         return ret;
6974
6975 nla_put_failure:
6976         nlmsg_free(msg);
6977         return -1;
6978 }
6979
6980
6981 static int wpa_driver_nl80211_disable_11b_rates(void *priv, int disabled)
6982 {
6983         struct i802_bss *bss = priv;
6984         struct wpa_driver_nl80211_data *drv = bss->drv;
6985         drv->disable_11b_rates = disabled;
6986         return nl80211_disable_11b_rates(drv, drv->ifindex, disabled);
6987 }
6988
6989
6990 static int wpa_driver_nl80211_deinit_ap(void *priv)
6991 {
6992         struct i802_bss *bss = priv;
6993         struct wpa_driver_nl80211_data *drv = bss->drv;
6994         if (!is_ap_interface(drv->nlmode))
6995                 return -1;
6996         wpa_driver_nl80211_del_beacon(drv);
6997         return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
6998 }
6999
7000
7001 static void wpa_driver_nl80211_resume(void *priv)
7002 {
7003         struct i802_bss *bss = priv;
7004         struct wpa_driver_nl80211_data *drv = bss->drv;
7005         if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1)) {
7006                 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on "
7007                            "resume event");
7008         }
7009 }
7010
7011
7012 static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap,
7013                                   const u8 *ies, size_t ies_len)
7014 {
7015         struct i802_bss *bss = priv;
7016         struct wpa_driver_nl80211_data *drv = bss->drv;
7017         int ret;
7018         u8 *data, *pos;
7019         size_t data_len;
7020         u8 own_addr[ETH_ALEN];
7021
7022         if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
7023                                own_addr) < 0)
7024                 return -1;
7025
7026         if (action != 1) {
7027                 wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action "
7028                            "action %d", action);
7029                 return -1;
7030         }
7031
7032         /*
7033          * Action frame payload:
7034          * Category[1] = 6 (Fast BSS Transition)
7035          * Action[1] = 1 (Fast BSS Transition Request)
7036          * STA Address
7037          * Target AP Address
7038          * FT IEs
7039          */
7040
7041         data_len = 2 + 2 * ETH_ALEN + ies_len;
7042         data = os_malloc(data_len);
7043         if (data == NULL)
7044                 return -1;
7045         pos = data;
7046         *pos++ = 0x06; /* FT Action category */
7047         *pos++ = action;
7048         os_memcpy(pos, own_addr, ETH_ALEN);
7049         pos += ETH_ALEN;
7050         os_memcpy(pos, target_ap, ETH_ALEN);
7051         pos += ETH_ALEN;
7052         os_memcpy(pos, ies, ies_len);
7053
7054         ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, 0,
7055                                              drv->bssid, own_addr, drv->bssid,
7056                                              data, data_len);
7057         os_free(data);
7058
7059         return ret;
7060 }
7061
7062
7063 static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
7064 {
7065         struct i802_bss *bss = priv;
7066         struct wpa_driver_nl80211_data *drv = bss->drv;
7067         struct nl_msg *msg, *cqm = NULL;
7068
7069         wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
7070                    "hysteresis=%d", threshold, hysteresis);
7071
7072         msg = nlmsg_alloc();
7073         if (!msg)
7074                 return -1;
7075
7076         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_CQM);
7077
7078         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
7079
7080         cqm = nlmsg_alloc();
7081         if (cqm == NULL)
7082                 return -1;
7083
7084         NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold);
7085         NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis);
7086         nla_put_nested(msg, NL80211_ATTR_CQM, cqm);
7087
7088         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
7089                 return 0;
7090         msg = NULL;
7091
7092 nla_put_failure:
7093         if (cqm)
7094                 nlmsg_free(cqm);
7095         nlmsg_free(msg);
7096         return -1;
7097 }
7098
7099
7100 static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
7101 {
7102         struct i802_bss *bss = priv;
7103         struct wpa_driver_nl80211_data *drv = bss->drv;
7104         int res;
7105
7106         os_memset(si, 0, sizeof(*si));
7107         res = nl80211_get_link_signal(drv, si);
7108         if (res != 0)
7109                 return res;
7110
7111         return nl80211_get_link_noise(drv, si);
7112 }
7113
7114
7115 static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
7116                               int encrypt)
7117 {
7118         struct i802_bss *bss = priv;
7119         struct wpa_driver_nl80211_data *drv = bss->drv;
7120         return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
7121 }
7122
7123
7124 static int nl80211_set_param(void *priv, const char *param)
7125 {
7126         wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
7127         if (param == NULL)
7128                 return 0;
7129
7130 #ifdef CONFIG_P2P
7131         if (os_strstr(param, "use_p2p_group_interface=1")) {
7132                 struct i802_bss *bss = priv;
7133                 struct wpa_driver_nl80211_data *drv = bss->drv;
7134
7135                 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
7136                            "interface");
7137                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
7138                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
7139         }
7140 #endif /* CONFIG_P2P */
7141
7142         return 0;
7143 }
7144
7145
7146 static void * nl80211_global_init(void)
7147 {
7148         struct nl80211_global *global;
7149         struct netlink_config *cfg;
7150
7151         global = os_zalloc(sizeof(*global));
7152         if (global == NULL)
7153                 return NULL;
7154         global->ioctl_sock = -1;
7155         dl_list_init(&global->interfaces);
7156         global->if_add_ifindex = -1;
7157
7158         cfg = os_zalloc(sizeof(*cfg));
7159         if (cfg == NULL)
7160                 goto err;
7161
7162         cfg->ctx = global;
7163         cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
7164         cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
7165         global->netlink = netlink_init(cfg);
7166         if (global->netlink == NULL) {
7167                 os_free(cfg);
7168                 goto err;
7169         }
7170
7171         if (wpa_driver_nl80211_init_nl_global(global) < 0)
7172                 goto err;
7173
7174         global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
7175         if (global->ioctl_sock < 0) {
7176                 perror("socket(PF_INET,SOCK_DGRAM)");
7177                 goto err;
7178         }
7179
7180         return global;
7181
7182 err:
7183         nl80211_global_deinit(global);
7184         return NULL;
7185 }
7186
7187
7188 static void nl80211_global_deinit(void *priv)
7189 {
7190         struct nl80211_global *global = priv;
7191         if (global == NULL)
7192                 return;
7193         if (!dl_list_empty(&global->interfaces)) {
7194                 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
7195                            "nl80211_global_deinit",
7196                            dl_list_len(&global->interfaces));
7197         }
7198
7199         if (global->netlink)
7200                 netlink_deinit(global->netlink);
7201
7202         if (global->nl80211)
7203                 genl_family_put(global->nl80211);
7204         nl_destroy_handles(&global->nl);
7205
7206         if (global->nl_cb)
7207                 nl_cb_put(global->nl_cb);
7208
7209         if (global->ioctl_sock >= 0)
7210                 close(global->ioctl_sock);
7211
7212         os_free(global);
7213 }
7214
7215
7216 static const char * nl80211_get_radio_name(void *priv)
7217 {
7218         struct i802_bss *bss = priv;
7219         struct wpa_driver_nl80211_data *drv = bss->drv;
7220         return drv->phyname;
7221 }
7222
7223
7224 static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
7225                          const u8 *pmkid)
7226 {
7227         struct nl_msg *msg;
7228
7229         msg = nlmsg_alloc();
7230         if (!msg)
7231                 return -ENOMEM;
7232
7233         nl80211_cmd(bss->drv, msg, 0, cmd);
7234
7235         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname));
7236         if (pmkid)
7237                 NLA_PUT(msg, NL80211_ATTR_PMKID, 16, pmkid);
7238         if (bssid)
7239                 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7240
7241         return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
7242  nla_put_failure:
7243         return -ENOBUFS;
7244 }
7245
7246
7247 static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
7248 {
7249         struct i802_bss *bss = priv;
7250         wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
7251         return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
7252 }
7253
7254
7255 static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
7256 {
7257         struct i802_bss *bss = priv;
7258         wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
7259                    MAC2STR(bssid));
7260         return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
7261 }
7262
7263
7264 static int nl80211_flush_pmkid(void *priv)
7265 {
7266         struct i802_bss *bss = priv;
7267         wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
7268         return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
7269 }
7270
7271
7272 static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
7273                                    const u8 *replay_ctr)
7274 {
7275         struct i802_bss *bss = priv;
7276         struct wpa_driver_nl80211_data *drv = bss->drv;
7277         struct nlattr *replay_nested;
7278         struct nl_msg *msg;
7279
7280         msg = nlmsg_alloc();
7281         if (!msg)
7282                 return;
7283
7284         nl80211_cmd(drv, msg, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7285
7286         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
7287
7288         replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7289         if (!replay_nested)
7290                 goto nla_put_failure;
7291
7292         NLA_PUT(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek);
7293         NLA_PUT(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck);
7294         NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
7295                 replay_ctr);
7296
7297         nla_nest_end(msg, replay_nested);
7298
7299         send_and_recv_msgs(drv, msg, NULL, NULL);
7300         return;
7301  nla_put_failure:
7302         nlmsg_free(msg);
7303 }
7304
7305
7306 static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
7307                                 int qos)
7308 {
7309         struct i802_bss *bss = priv;
7310         struct {
7311                 struct ieee80211_hdr hdr;
7312                 u16 qos_ctl;
7313         } STRUCT_PACKED nulldata;
7314         size_t size;
7315
7316         /* Send data frame to poll STA and check whether this frame is ACKed */
7317
7318         os_memset(&nulldata, 0, sizeof(nulldata));
7319
7320         if (qos) {
7321                 nulldata.hdr.frame_control =
7322                         IEEE80211_FC(WLAN_FC_TYPE_DATA,
7323                                      WLAN_FC_STYPE_QOS_NULL);
7324                 size = sizeof(nulldata);
7325         } else {
7326                 nulldata.hdr.frame_control =
7327                         IEEE80211_FC(WLAN_FC_TYPE_DATA,
7328                                      WLAN_FC_STYPE_NULLFUNC);
7329                 size = sizeof(struct ieee80211_hdr);
7330         }
7331
7332         nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
7333         os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
7334         os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
7335         os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
7336
7337         if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size) < 0)
7338                 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
7339                            "send poll frame");
7340 }
7341
7342
7343 #ifdef CONFIG_TDLS
7344
7345 static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
7346                                   u8 dialog_token, u16 status_code,
7347                                   const u8 *buf, size_t len)
7348 {
7349         struct i802_bss *bss = priv;
7350         struct wpa_driver_nl80211_data *drv = bss->drv;
7351         struct nl_msg *msg;
7352
7353         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7354                 return -EOPNOTSUPP;
7355
7356         if (!dst)
7357                 return -EINVAL;
7358
7359         msg = nlmsg_alloc();
7360         if (!msg)
7361                 return -ENOMEM;
7362
7363         nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_MGMT);
7364         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7365         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
7366         NLA_PUT_U8(msg, NL80211_ATTR_TDLS_ACTION, action_code);
7367         NLA_PUT_U8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token);
7368         NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status_code);
7369         NLA_PUT(msg, NL80211_ATTR_IE, len, buf);
7370
7371         return send_and_recv_msgs(drv, msg, NULL, NULL);
7372
7373 nla_put_failure:
7374         nlmsg_free(msg);
7375         return -ENOBUFS;
7376 }
7377
7378
7379 static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
7380 {
7381         struct i802_bss *bss = priv;
7382         struct wpa_driver_nl80211_data *drv = bss->drv;
7383         struct nl_msg *msg;
7384         enum nl80211_tdls_operation nl80211_oper;
7385
7386         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7387                 return -EOPNOTSUPP;
7388
7389         switch (oper) {
7390         case TDLS_DISCOVERY_REQ:
7391                 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
7392                 break;
7393         case TDLS_SETUP:
7394                 nl80211_oper = NL80211_TDLS_SETUP;
7395                 break;
7396         case TDLS_TEARDOWN:
7397                 nl80211_oper = NL80211_TDLS_TEARDOWN;
7398                 break;
7399         case TDLS_ENABLE_LINK:
7400                 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
7401                 break;
7402         case TDLS_DISABLE_LINK:
7403                 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
7404                 break;
7405         case TDLS_ENABLE:
7406                 return 0;
7407         case TDLS_DISABLE:
7408                 return 0;
7409         default:
7410                 return -EINVAL;
7411         }
7412
7413         msg = nlmsg_alloc();
7414         if (!msg)
7415                 return -ENOMEM;
7416
7417         nl80211_cmd(drv, msg, 0, NL80211_CMD_TDLS_OPER);
7418         NLA_PUT_U8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper);
7419         NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
7420         NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7421
7422         return send_and_recv_msgs(drv, msg, NULL, NULL);
7423
7424 nla_put_failure:
7425         nlmsg_free(msg);
7426         return -ENOBUFS;
7427 }
7428
7429 #endif /* CONFIG TDLS */
7430
7431
7432 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
7433         .name = "nl80211",
7434         .desc = "Linux nl80211/cfg80211",
7435         .get_bssid = wpa_driver_nl80211_get_bssid,
7436         .get_ssid = wpa_driver_nl80211_get_ssid,
7437         .set_key = wpa_driver_nl80211_set_key,
7438         .scan2 = wpa_driver_nl80211_scan,
7439         .sched_scan = wpa_driver_nl80211_sched_scan,
7440         .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
7441         .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
7442         .deauthenticate = wpa_driver_nl80211_deauthenticate,
7443         .disassociate = wpa_driver_nl80211_disassociate,
7444         .authenticate = wpa_driver_nl80211_authenticate,
7445         .associate = wpa_driver_nl80211_associate,
7446         .global_init = nl80211_global_init,
7447         .global_deinit = nl80211_global_deinit,
7448         .init2 = wpa_driver_nl80211_init,
7449         .deinit = wpa_driver_nl80211_deinit,
7450         .get_capa = wpa_driver_nl80211_get_capa,
7451         .set_operstate = wpa_driver_nl80211_set_operstate,
7452         .set_supp_port = wpa_driver_nl80211_set_supp_port,
7453         .set_country = wpa_driver_nl80211_set_country,
7454         .set_ap = wpa_driver_nl80211_set_ap,
7455         .if_add = wpa_driver_nl80211_if_add,
7456         .if_remove = wpa_driver_nl80211_if_remove,
7457         .send_mlme = wpa_driver_nl80211_send_mlme,
7458         .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
7459         .sta_add = wpa_driver_nl80211_sta_add,
7460         .sta_remove = wpa_driver_nl80211_sta_remove,
7461         .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
7462         .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
7463 #ifdef HOSTAPD
7464         .hapd_init = i802_init,
7465         .hapd_deinit = i802_deinit,
7466         .set_wds_sta = i802_set_wds_sta,
7467 #endif /* HOSTAPD */
7468 #if defined(HOSTAPD) || defined(CONFIG_AP)
7469         .get_seqnum = i802_get_seqnum,
7470         .flush = i802_flush,
7471         .read_sta_data = i802_read_sta_data,
7472         .get_inact_sec = i802_get_inact_sec,
7473         .sta_clear_stats = i802_sta_clear_stats,
7474         .set_rts = i802_set_rts,
7475         .set_frag = i802_set_frag,
7476         .set_tx_queue_params = i802_set_tx_queue_params,
7477         .set_sta_vlan = i802_set_sta_vlan,
7478         .set_rate_sets = i802_set_rate_sets,
7479         .sta_deauth = i802_sta_deauth,
7480         .sta_disassoc = i802_sta_disassoc,
7481 #endif /* HOSTAPD || CONFIG_AP */
7482         .set_freq = i802_set_freq,
7483         .send_action = wpa_driver_nl80211_send_action,
7484         .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
7485         .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
7486         .cancel_remain_on_channel =
7487         wpa_driver_nl80211_cancel_remain_on_channel,
7488         .probe_req_report = wpa_driver_nl80211_probe_req_report,
7489         .disable_11b_rates = wpa_driver_nl80211_disable_11b_rates,
7490         .deinit_ap = wpa_driver_nl80211_deinit_ap,
7491         .resume = wpa_driver_nl80211_resume,
7492         .send_ft_action = nl80211_send_ft_action,
7493         .signal_monitor = nl80211_signal_monitor,
7494         .signal_poll = nl80211_signal_poll,
7495         .send_frame = nl80211_send_frame,
7496         .set_param = nl80211_set_param,
7497         .get_radio_name = nl80211_get_radio_name,
7498         .add_pmkid = nl80211_add_pmkid,
7499         .remove_pmkid = nl80211_remove_pmkid,
7500         .flush_pmkid = nl80211_flush_pmkid,
7501         .set_rekey_info = nl80211_set_rekey_info,
7502         .poll_client = nl80211_poll_client,
7503 #ifdef CONFIG_TDLS
7504         .send_tdls_mgmt = nl80211_send_tdls_mgmt,
7505         .tdls_oper = nl80211_tdls_oper,
7506 #endif /* CONFIG_TDLS */
7507 };