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