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