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