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