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