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