nl80211: Add more debug prints for 11b rate disabling and re-enabling
[mech_eap.git] / src / drivers / driver_nl80211.c
1 /*
2  * Driver interaction with Linux nl80211/cfg80211
3  * Copyright (c) 2002-2014, 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 software may be distributed under the terms of the BSD license.
10  * See README for more details.
11  */
12
13 #include "includes.h"
14 #include <sys/types.h>
15 #include <fcntl.h>
16 #include <net/if.h>
17 #include <netlink/genl/genl.h>
18 #include <netlink/genl/ctrl.h>
19 #ifdef CONFIG_LIBNL3_ROUTE
20 #include <netlink/route/neighbour.h>
21 #endif /* CONFIG_LIBNL3_ROUTE */
22 #include <linux/rtnetlink.h>
23 #include <netpacket/packet.h>
24 #include <linux/errqueue.h>
25
26 #include "common.h"
27 #include "eloop.h"
28 #include "common/qca-vendor.h"
29 #include "common/qca-vendor-attr.h"
30 #include "common/ieee802_11_defs.h"
31 #include "common/ieee802_11_common.h"
32 #include "l2_packet/l2_packet.h"
33 #include "netlink.h"
34 #include "linux_defines.h"
35 #include "linux_ioctl.h"
36 #include "radiotap.h"
37 #include "radiotap_iter.h"
38 #include "rfkill.h"
39 #include "driver_nl80211.h"
40
41
42 #ifndef CONFIG_LIBNL20
43 /*
44  * libnl 1.1 has a bug, it tries to allocate socket numbers densely
45  * but when you free a socket again it will mess up its bitmap and
46  * and use the wrong number the next time it needs a socket ID.
47  * Therefore, we wrap the handle alloc/destroy and add our own pid
48  * accounting.
49  */
50 static uint32_t port_bitmap[32] = { 0 };
51
52 static struct nl_handle *nl80211_handle_alloc(void *cb)
53 {
54         struct nl_handle *handle;
55         uint32_t pid = getpid() & 0x3FFFFF;
56         int i;
57
58         handle = nl_handle_alloc_cb(cb);
59
60         for (i = 0; i < 1024; i++) {
61                 if (port_bitmap[i / 32] & (1 << (i % 32)))
62                         continue;
63                 port_bitmap[i / 32] |= 1 << (i % 32);
64                 pid += i << 22;
65                 break;
66         }
67
68         nl_socket_set_local_port(handle, pid);
69
70         return handle;
71 }
72
73 static void nl80211_handle_destroy(struct nl_handle *handle)
74 {
75         uint32_t port = nl_socket_get_local_port(handle);
76
77         port >>= 22;
78         port_bitmap[port / 32] &= ~(1 << (port % 32));
79
80         nl_handle_destroy(handle);
81 }
82 #endif /* CONFIG_LIBNL20 */
83
84
85 #ifdef ANDROID
86 /* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
87 #undef nl_socket_set_nonblocking
88 #define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
89
90 #define genl_ctrl_resolve android_genl_ctrl_resolve
91 #endif /* ANDROID */
92
93
94 static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
95 {
96         struct nl_handle *handle;
97
98         handle = nl80211_handle_alloc(cb);
99         if (handle == NULL) {
100                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
101                            "callbacks (%s)", dbg);
102                 return NULL;
103         }
104
105         if (genl_connect(handle)) {
106                 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
107                            "netlink (%s)", dbg);
108                 nl80211_handle_destroy(handle);
109                 return NULL;
110         }
111
112         return handle;
113 }
114
115
116 static void nl_destroy_handles(struct nl_handle **handle)
117 {
118         if (*handle == NULL)
119                 return;
120         nl80211_handle_destroy(*handle);
121         *handle = NULL;
122 }
123
124
125 #if __WORDSIZE == 64
126 #define ELOOP_SOCKET_INVALID    (intptr_t) 0x8888888888888889ULL
127 #else
128 #define ELOOP_SOCKET_INVALID    (intptr_t) 0x88888889ULL
129 #endif
130
131 static void nl80211_register_eloop_read(struct nl_handle **handle,
132                                         eloop_sock_handler handler,
133                                         void *eloop_data)
134 {
135         nl_socket_set_nonblocking(*handle);
136         eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
137                                  eloop_data, *handle);
138         *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
139 }
140
141
142 static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
143 {
144         *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
145         eloop_unregister_read_sock(nl_socket_get_fd(*handle));
146         nl_destroy_handles(handle);
147 }
148
149
150 static void nl80211_global_deinit(void *priv);
151
152 static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
153 static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
154                                             struct hostapd_freq_params *freq);
155
156 static int
157 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
158                                    const u8 *set_addr, int first,
159                                    const char *driver_params);
160 static int nl80211_send_frame_cmd(struct i802_bss *bss,
161                                   unsigned int freq, unsigned int wait,
162                                   const u8 *buf, size_t buf_len, u64 *cookie,
163                                   int no_cck, int no_ack, int offchanok);
164 static int nl80211_register_frame(struct i802_bss *bss,
165                                   struct nl_handle *hl_handle,
166                                   u16 type, const u8 *match, size_t match_len);
167 static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
168                                                int report);
169
170 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
171 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
172 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
173 static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
174                                         enum wpa_driver_if_type type,
175                                         const char *ifname);
176
177 static int nl80211_set_channel(struct i802_bss *bss,
178                                struct hostapd_freq_params *freq, int set_chan);
179 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
180                                      int ifindex, int disabled);
181
182 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv);
183
184 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
185 static int i802_set_iface_flags(struct i802_bss *bss, int up);
186 static int nl80211_set_param(void *priv, const char *param);
187
188
189 /* Converts nl80211_chan_width to a common format */
190 enum chan_width convert2width(int width)
191 {
192         switch (width) {
193         case NL80211_CHAN_WIDTH_20_NOHT:
194                 return CHAN_WIDTH_20_NOHT;
195         case NL80211_CHAN_WIDTH_20:
196                 return CHAN_WIDTH_20;
197         case NL80211_CHAN_WIDTH_40:
198                 return CHAN_WIDTH_40;
199         case NL80211_CHAN_WIDTH_80:
200                 return CHAN_WIDTH_80;
201         case NL80211_CHAN_WIDTH_80P80:
202                 return CHAN_WIDTH_80P80;
203         case NL80211_CHAN_WIDTH_160:
204                 return CHAN_WIDTH_160;
205         }
206         return CHAN_WIDTH_UNKNOWN;
207 }
208
209
210 int is_ap_interface(enum nl80211_iftype nlmode)
211 {
212         return nlmode == NL80211_IFTYPE_AP ||
213                 nlmode == NL80211_IFTYPE_P2P_GO;
214 }
215
216
217 int is_sta_interface(enum nl80211_iftype nlmode)
218 {
219         return nlmode == NL80211_IFTYPE_STATION ||
220                 nlmode == NL80211_IFTYPE_P2P_CLIENT;
221 }
222
223
224 static int is_p2p_net_interface(enum nl80211_iftype nlmode)
225 {
226         return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
227                 nlmode == NL80211_IFTYPE_P2P_GO;
228 }
229
230
231 struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
232                                   int ifindex)
233 {
234         struct i802_bss *bss;
235
236         for (bss = drv->first_bss; bss; bss = bss->next) {
237                 if (bss->ifindex == ifindex)
238                         return bss;
239         }
240
241         return NULL;
242 }
243
244
245 static int is_mesh_interface(enum nl80211_iftype nlmode)
246 {
247         return nlmode == NL80211_IFTYPE_MESH_POINT;
248 }
249
250
251 void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
252 {
253         if (drv->associated)
254                 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
255         drv->associated = 0;
256         os_memset(drv->bssid, 0, ETH_ALEN);
257 }
258
259
260 /* nl80211 code */
261 static int ack_handler(struct nl_msg *msg, void *arg)
262 {
263         int *err = arg;
264         *err = 0;
265         return NL_STOP;
266 }
267
268 static int finish_handler(struct nl_msg *msg, void *arg)
269 {
270         int *ret = arg;
271         *ret = 0;
272         return NL_SKIP;
273 }
274
275 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
276                          void *arg)
277 {
278         int *ret = arg;
279         *ret = err->error;
280         return NL_SKIP;
281 }
282
283
284 static int no_seq_check(struct nl_msg *msg, void *arg)
285 {
286         return NL_OK;
287 }
288
289
290 static int send_and_recv(struct nl80211_global *global,
291                          struct nl_handle *nl_handle, struct nl_msg *msg,
292                          int (*valid_handler)(struct nl_msg *, void *),
293                          void *valid_data)
294 {
295         struct nl_cb *cb;
296         int err = -ENOMEM;
297
298         if (!msg)
299                 return -ENOMEM;
300
301         cb = nl_cb_clone(global->nl_cb);
302         if (!cb)
303                 goto out;
304
305         err = nl_send_auto_complete(nl_handle, msg);
306         if (err < 0)
307                 goto out;
308
309         err = 1;
310
311         nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
312         nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
313         nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
314
315         if (valid_handler)
316                 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
317                           valid_handler, valid_data);
318
319         while (err > 0) {
320                 int res = nl_recvmsgs(nl_handle, cb);
321                 if (res < 0) {
322                         wpa_printf(MSG_INFO,
323                                    "nl80211: %s->nl_recvmsgs failed: %d",
324                                    __func__, res);
325                 }
326         }
327  out:
328         nl_cb_put(cb);
329         nlmsg_free(msg);
330         return err;
331 }
332
333
334 static int send_and_recv_msgs_global(struct nl80211_global *global,
335                                      struct nl_msg *msg,
336                                      int (*valid_handler)(struct nl_msg *, void *),
337                                      void *valid_data)
338 {
339         return send_and_recv(global, global->nl, msg, valid_handler,
340                              valid_data);
341 }
342
343
344 int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
345                        struct nl_msg *msg,
346                        int (*valid_handler)(struct nl_msg *, void *),
347                        void *valid_data)
348 {
349         return send_and_recv(drv->global, drv->global->nl, msg,
350                              valid_handler, valid_data);
351 }
352
353
354 struct family_data {
355         const char *group;
356         int id;
357 };
358
359
360 static int family_handler(struct nl_msg *msg, void *arg)
361 {
362         struct family_data *res = arg;
363         struct nlattr *tb[CTRL_ATTR_MAX + 1];
364         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
365         struct nlattr *mcgrp;
366         int i;
367
368         nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
369                   genlmsg_attrlen(gnlh, 0), NULL);
370         if (!tb[CTRL_ATTR_MCAST_GROUPS])
371                 return NL_SKIP;
372
373         nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
374                 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
375                 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
376                           nla_len(mcgrp), NULL);
377                 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
378                     !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
379                     os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
380                                res->group,
381                                nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
382                         continue;
383                 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
384                 break;
385         };
386
387         return NL_SKIP;
388 }
389
390
391 static int nl_get_multicast_id(struct nl80211_global *global,
392                                const char *family, const char *group)
393 {
394         struct nl_msg *msg;
395         int ret;
396         struct family_data res = { group, -ENOENT };
397
398         msg = nlmsg_alloc();
399         if (!msg)
400                 return -ENOMEM;
401         if (!genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
402                          0, 0, CTRL_CMD_GETFAMILY, 0) ||
403             nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
404                 nlmsg_free(msg);
405                 return -1;
406         }
407
408         ret = send_and_recv_msgs_global(global, msg, family_handler, &res);
409         if (ret == 0)
410                 ret = res.id;
411         return ret;
412 }
413
414
415 void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
416                    struct nl_msg *msg, int flags, uint8_t cmd)
417 {
418         return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
419                            0, flags, cmd, 0);
420 }
421
422
423 static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
424 {
425         if (bss->wdev_id_set)
426                 return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
427         return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
428 }
429
430
431 struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
432 {
433         struct nl_msg *msg;
434
435         msg = nlmsg_alloc();
436         if (!msg)
437                 return NULL;
438
439         if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
440             nl80211_set_iface_id(msg, bss) < 0) {
441                 nlmsg_free(msg);
442                 return NULL;
443         }
444
445         return msg;
446 }
447
448
449 static struct nl_msg *
450 nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
451                     int flags, uint8_t cmd)
452 {
453         struct nl_msg *msg;
454
455         msg = nlmsg_alloc();
456         if (!msg)
457                 return NULL;
458
459         if (!nl80211_cmd(drv, msg, flags, cmd) ||
460             nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
461                 nlmsg_free(msg);
462                 return NULL;
463         }
464
465         return msg;
466 }
467
468
469 struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
470                                 uint8_t cmd)
471 {
472         return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
473 }
474
475
476 struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
477 {
478         return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
479 }
480
481
482 struct wiphy_idx_data {
483         int wiphy_idx;
484         enum nl80211_iftype nlmode;
485         u8 *macaddr;
486 };
487
488
489 static int netdev_info_handler(struct nl_msg *msg, void *arg)
490 {
491         struct nlattr *tb[NL80211_ATTR_MAX + 1];
492         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
493         struct wiphy_idx_data *info = arg;
494
495         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
496                   genlmsg_attrlen(gnlh, 0), NULL);
497
498         if (tb[NL80211_ATTR_WIPHY])
499                 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
500
501         if (tb[NL80211_ATTR_IFTYPE])
502                 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
503
504         if (tb[NL80211_ATTR_MAC] && info->macaddr)
505                 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
506                           ETH_ALEN);
507
508         return NL_SKIP;
509 }
510
511
512 int nl80211_get_wiphy_index(struct i802_bss *bss)
513 {
514         struct nl_msg *msg;
515         struct wiphy_idx_data data = {
516                 .wiphy_idx = -1,
517                 .macaddr = NULL,
518         };
519
520         if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
521                 return -1;
522
523         if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
524                 return data.wiphy_idx;
525         return -1;
526 }
527
528
529 static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
530 {
531         struct nl_msg *msg;
532         struct wiphy_idx_data data = {
533                 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
534                 .macaddr = NULL,
535         };
536
537         if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
538                 return NL80211_IFTYPE_UNSPECIFIED;
539
540         if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
541                 return data.nlmode;
542         return NL80211_IFTYPE_UNSPECIFIED;
543 }
544
545
546 static int nl80211_get_macaddr(struct i802_bss *bss)
547 {
548         struct nl_msg *msg;
549         struct wiphy_idx_data data = {
550                 .macaddr = bss->addr,
551         };
552
553         if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
554                 return -1;
555
556         return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
557 }
558
559
560 static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
561                                     struct nl80211_wiphy_data *w)
562 {
563         struct nl_msg *msg;
564         int ret;
565
566         msg = nlmsg_alloc();
567         if (!msg)
568                 return -1;
569
570         if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
571             nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
572                 nlmsg_free(msg);
573                 return -1;
574         }
575
576         ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
577         if (ret) {
578                 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
579                            "failed: ret=%d (%s)",
580                            ret, strerror(-ret));
581         }
582         return ret;
583 }
584
585
586 static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
587 {
588         struct nl80211_wiphy_data *w = eloop_ctx;
589         int res;
590
591         wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
592
593         res = nl_recvmsgs(handle, w->nl_cb);
594         if (res < 0) {
595                 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
596                            __func__, res);
597         }
598 }
599
600
601 static int process_beacon_event(struct nl_msg *msg, void *arg)
602 {
603         struct nl80211_wiphy_data *w = arg;
604         struct wpa_driver_nl80211_data *drv;
605         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
606         struct nlattr *tb[NL80211_ATTR_MAX + 1];
607         union wpa_event_data event;
608
609         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
610                   genlmsg_attrlen(gnlh, 0), NULL);
611
612         if (gnlh->cmd != NL80211_CMD_FRAME) {
613                 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
614                            gnlh->cmd);
615                 return NL_SKIP;
616         }
617
618         if (!tb[NL80211_ATTR_FRAME])
619                 return NL_SKIP;
620
621         dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
622                          wiphy_list) {
623                 os_memset(&event, 0, sizeof(event));
624                 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
625                 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
626                 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
627         }
628
629         return NL_SKIP;
630 }
631
632
633 static struct nl80211_wiphy_data *
634 nl80211_get_wiphy_data_ap(struct i802_bss *bss)
635 {
636         static DEFINE_DL_LIST(nl80211_wiphys);
637         struct nl80211_wiphy_data *w;
638         int wiphy_idx, found = 0;
639         struct i802_bss *tmp_bss;
640
641         if (bss->wiphy_data != NULL)
642                 return bss->wiphy_data;
643
644         wiphy_idx = nl80211_get_wiphy_index(bss);
645
646         dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
647                 if (w->wiphy_idx == wiphy_idx)
648                         goto add;
649         }
650
651         /* alloc new one */
652         w = os_zalloc(sizeof(*w));
653         if (w == NULL)
654                 return NULL;
655         w->wiphy_idx = wiphy_idx;
656         dl_list_init(&w->bsss);
657         dl_list_init(&w->drvs);
658
659         w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
660         if (!w->nl_cb) {
661                 os_free(w);
662                 return NULL;
663         }
664         nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
665         nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
666                   w);
667
668         w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
669                                          "wiphy beacons");
670         if (w->nl_beacons == NULL) {
671                 os_free(w);
672                 return NULL;
673         }
674
675         if (nl80211_register_beacons(bss->drv, w)) {
676                 nl_destroy_handles(&w->nl_beacons);
677                 os_free(w);
678                 return NULL;
679         }
680
681         nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
682
683         dl_list_add(&nl80211_wiphys, &w->list);
684
685 add:
686         /* drv entry for this bss already there? */
687         dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
688                 if (tmp_bss->drv == bss->drv) {
689                         found = 1;
690                         break;
691                 }
692         }
693         /* if not add it */
694         if (!found)
695                 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
696
697         dl_list_add(&w->bsss, &bss->wiphy_list);
698         bss->wiphy_data = w;
699         return w;
700 }
701
702
703 static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
704 {
705         struct nl80211_wiphy_data *w = bss->wiphy_data;
706         struct i802_bss *tmp_bss;
707         int found = 0;
708
709         if (w == NULL)
710                 return;
711         bss->wiphy_data = NULL;
712         dl_list_del(&bss->wiphy_list);
713
714         /* still any for this drv present? */
715         dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
716                 if (tmp_bss->drv == bss->drv) {
717                         found = 1;
718                         break;
719                 }
720         }
721         /* if not remove it */
722         if (!found)
723                 dl_list_del(&bss->drv->wiphy_list);
724
725         if (!dl_list_empty(&w->bsss))
726                 return;
727
728         nl80211_destroy_eloop_handle(&w->nl_beacons);
729
730         nl_cb_put(w->nl_cb);
731         dl_list_del(&w->list);
732         os_free(w);
733 }
734
735
736 static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
737 {
738         struct i802_bss *bss = priv;
739         struct wpa_driver_nl80211_data *drv = bss->drv;
740         if (!drv->associated)
741                 return -1;
742         os_memcpy(bssid, drv->bssid, ETH_ALEN);
743         return 0;
744 }
745
746
747 static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
748 {
749         struct i802_bss *bss = priv;
750         struct wpa_driver_nl80211_data *drv = bss->drv;
751         if (!drv->associated)
752                 return -1;
753         os_memcpy(ssid, drv->ssid, drv->ssid_len);
754         return drv->ssid_len;
755 }
756
757
758 static void wpa_driver_nl80211_event_newlink(
759         struct wpa_driver_nl80211_data *drv, char *ifname)
760 {
761         union wpa_event_data event;
762
763         if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
764                 if (if_nametoindex(drv->first_bss->ifname) == 0) {
765                         wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
766                                    drv->first_bss->ifname);
767                         return;
768                 }
769                 if (!drv->if_removed)
770                         return;
771                 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
772                            drv->first_bss->ifname);
773                 drv->if_removed = 0;
774         }
775
776         os_memset(&event, 0, sizeof(event));
777         os_strlcpy(event.interface_status.ifname, ifname,
778                    sizeof(event.interface_status.ifname));
779         event.interface_status.ievent = EVENT_INTERFACE_ADDED;
780         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
781 }
782
783
784 static void wpa_driver_nl80211_event_dellink(
785         struct wpa_driver_nl80211_data *drv, char *ifname)
786 {
787         union wpa_event_data event;
788
789         if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
790                 if (drv->if_removed) {
791                         wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
792                                    ifname);
793                         return;
794                 }
795                 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
796                            ifname);
797                 drv->if_removed = 1;
798         } else {
799                 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
800                            ifname);
801         }
802
803         os_memset(&event, 0, sizeof(event));
804         os_strlcpy(event.interface_status.ifname, ifname,
805                    sizeof(event.interface_status.ifname));
806         event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
807         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
808 }
809
810
811 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
812                                          u8 *buf, size_t len)
813 {
814         int attrlen, rta_len;
815         struct rtattr *attr;
816
817         attrlen = len;
818         attr = (struct rtattr *) buf;
819
820         rta_len = RTA_ALIGN(sizeof(struct rtattr));
821         while (RTA_OK(attr, attrlen)) {
822                 if (attr->rta_type == IFLA_IFNAME) {
823                         if (os_strcmp(((char *) attr) + rta_len,
824                                       drv->first_bss->ifname) == 0)
825                                 return 1;
826                         else
827                                 break;
828                 }
829                 attr = RTA_NEXT(attr, attrlen);
830         }
831
832         return 0;
833 }
834
835
836 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
837                                           int ifindex, u8 *buf, size_t len)
838 {
839         if (drv->ifindex == ifindex)
840                 return 1;
841
842         if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
843                 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
844                            "interface");
845                 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL);
846                 return 1;
847         }
848
849         return 0;
850 }
851
852
853 static struct wpa_driver_nl80211_data *
854 nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
855 {
856         struct wpa_driver_nl80211_data *drv;
857         dl_list_for_each(drv, &global->interfaces,
858                          struct wpa_driver_nl80211_data, list) {
859                 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
860                     have_ifidx(drv, idx))
861                         return drv;
862         }
863         return NULL;
864 }
865
866
867 static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
868                                                  struct ifinfomsg *ifi,
869                                                  u8 *buf, size_t len)
870 {
871         struct nl80211_global *global = ctx;
872         struct wpa_driver_nl80211_data *drv;
873         int attrlen;
874         struct rtattr *attr;
875         u32 brid = 0;
876         char namebuf[IFNAMSIZ];
877         char ifname[IFNAMSIZ + 1];
878         char extra[100], *pos, *end;
879
880         drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
881         if (!drv) {
882                 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
883                            ifi->ifi_index);
884                 return;
885         }
886
887         extra[0] = '\0';
888         pos = extra;
889         end = pos + sizeof(extra);
890         ifname[0] = '\0';
891
892         attrlen = len;
893         attr = (struct rtattr *) buf;
894         while (RTA_OK(attr, attrlen)) {
895                 switch (attr->rta_type) {
896                 case IFLA_IFNAME:
897                         if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
898                                 break;
899                         os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
900                         ifname[RTA_PAYLOAD(attr)] = '\0';
901                         break;
902                 case IFLA_MASTER:
903                         brid = nla_get_u32((struct nlattr *) attr);
904                         pos += os_snprintf(pos, end - pos, " master=%u", brid);
905                         break;
906                 case IFLA_WIRELESS:
907                         pos += os_snprintf(pos, end - pos, " wext");
908                         break;
909                 case IFLA_OPERSTATE:
910                         pos += os_snprintf(pos, end - pos, " operstate=%u",
911                                            nla_get_u32((struct nlattr *) attr));
912                         break;
913                 case IFLA_LINKMODE:
914                         pos += os_snprintf(pos, end - pos, " linkmode=%u",
915                                            nla_get_u32((struct nlattr *) attr));
916                         break;
917                 }
918                 attr = RTA_NEXT(attr, attrlen);
919         }
920         extra[sizeof(extra) - 1] = '\0';
921
922         wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
923                    ifi->ifi_index, ifname, extra, ifi->ifi_family,
924                    ifi->ifi_flags,
925                    (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
926                    (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
927                    (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
928                    (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
929
930         if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
931                 if (if_indextoname(ifi->ifi_index, namebuf) &&
932                     linux_iface_up(drv->global->ioctl_sock,
933                                    drv->first_bss->ifname) > 0) {
934                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
935                                    "event since interface %s is up", namebuf);
936                         drv->ignore_if_down_event = 0;
937                         return;
938                 }
939                 wpa_printf(MSG_DEBUG, "nl80211: Interface down");
940                 if (drv->ignore_if_down_event) {
941                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
942                                    "event generated by mode change");
943                         drv->ignore_if_down_event = 0;
944                 } else {
945                         drv->if_disabled = 1;
946                         wpa_supplicant_event(drv->ctx,
947                                              EVENT_INTERFACE_DISABLED, NULL);
948
949                         /*
950                          * Try to get drv again, since it may be removed as
951                          * part of the EVENT_INTERFACE_DISABLED handling for
952                          * dynamic interfaces
953                          */
954                         drv = nl80211_find_drv(global, ifi->ifi_index,
955                                                buf, len);
956                         if (!drv)
957                                 return;
958                 }
959         }
960
961         if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
962                 if (if_indextoname(ifi->ifi_index, namebuf) &&
963                     linux_iface_up(drv->global->ioctl_sock,
964                                    drv->first_bss->ifname) == 0) {
965                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
966                                    "event since interface %s is down",
967                                    namebuf);
968                 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
969                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
970                                    "event since interface %s does not exist",
971                                    drv->first_bss->ifname);
972                 } else if (drv->if_removed) {
973                         wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
974                                    "event since interface %s is marked "
975                                    "removed", drv->first_bss->ifname);
976                 } else {
977                         struct i802_bss *bss;
978                         u8 addr[ETH_ALEN];
979
980                         /* Re-read MAC address as it may have changed */
981                         bss = get_bss_ifindex(drv, ifi->ifi_index);
982                         if (bss &&
983                             linux_get_ifhwaddr(drv->global->ioctl_sock,
984                                                bss->ifname, addr) < 0) {
985                                 wpa_printf(MSG_DEBUG,
986                                            "nl80211: %s: failed to re-read MAC address",
987                                            bss->ifname);
988                         } else if (bss &&
989                                    os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
990                                 wpa_printf(MSG_DEBUG,
991                                            "nl80211: Own MAC address on ifindex %d (%s) changed from "
992                                            MACSTR " to " MACSTR,
993                                            ifi->ifi_index, bss->ifname,
994                                            MAC2STR(bss->addr),
995                                            MAC2STR(addr));
996                                 os_memcpy(bss->addr, addr, ETH_ALEN);
997                         }
998
999                         wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1000                         drv->if_disabled = 0;
1001                         wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1002                                              NULL);
1003                 }
1004         }
1005
1006         /*
1007          * Some drivers send the association event before the operup event--in
1008          * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1009          * fails. This will hit us when wpa_supplicant does not need to do
1010          * IEEE 802.1X authentication
1011          */
1012         if (drv->operstate == 1 &&
1013             (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
1014             !(ifi->ifi_flags & IFF_RUNNING)) {
1015                 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
1016                 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
1017                                        -1, IF_OPER_UP);
1018         }
1019
1020         if (ifname[0])
1021                 wpa_driver_nl80211_event_newlink(drv, ifname);
1022
1023         if (ifi->ifi_family == AF_BRIDGE && brid) {
1024                 struct i802_bss *bss;
1025
1026                 /* device has been added to bridge */
1027                 if_indextoname(brid, namebuf);
1028                 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1029                            brid, namebuf);
1030                 add_ifidx(drv, brid);
1031
1032                 for (bss = drv->first_bss; bss; bss = bss->next) {
1033                         if (os_strcmp(ifname, bss->ifname) == 0) {
1034                                 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1035                                 break;
1036                         }
1037                 }
1038         }
1039 }
1040
1041
1042 static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1043                                                  struct ifinfomsg *ifi,
1044                                                  u8 *buf, size_t len)
1045 {
1046         struct nl80211_global *global = ctx;
1047         struct wpa_driver_nl80211_data *drv;
1048         int attrlen;
1049         struct rtattr *attr;
1050         u32 brid = 0;
1051         char ifname[IFNAMSIZ + 1];
1052         char extra[100], *pos, *end;
1053
1054         drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
1055         if (!drv) {
1056                 wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
1057                            ifi->ifi_index);
1058                 return;
1059         }
1060
1061         extra[0] = '\0';
1062         pos = extra;
1063         end = pos + sizeof(extra);
1064         ifname[0] = '\0';
1065
1066         attrlen = len;
1067         attr = (struct rtattr *) buf;
1068         while (RTA_OK(attr, attrlen)) {
1069                 switch (attr->rta_type) {
1070                 case IFLA_IFNAME:
1071                         if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1072                                 break;
1073                         os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1074                         ifname[RTA_PAYLOAD(attr)] = '\0';
1075                         break;
1076                 case IFLA_MASTER:
1077                         brid = nla_get_u32((struct nlattr *) attr);
1078                         pos += os_snprintf(pos, end - pos, " master=%u", brid);
1079                         break;
1080                 case IFLA_OPERSTATE:
1081                         pos += os_snprintf(pos, end - pos, " operstate=%u",
1082                                            nla_get_u32((struct nlattr *) attr));
1083                         break;
1084                 case IFLA_LINKMODE:
1085                         pos += os_snprintf(pos, end - pos, " linkmode=%u",
1086                                            nla_get_u32((struct nlattr *) attr));
1087                         break;
1088                 }
1089                 attr = RTA_NEXT(attr, attrlen);
1090         }
1091         extra[sizeof(extra) - 1] = '\0';
1092
1093         wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1094                    ifi->ifi_index, ifname, extra, ifi->ifi_family,
1095                    ifi->ifi_flags,
1096                    (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1097                    (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1098                    (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1099                    (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1100
1101         if (ifname[0] && (ifi->ifi_family != AF_BRIDGE || !brid))
1102                 wpa_driver_nl80211_event_dellink(drv, ifname);
1103
1104         if (ifi->ifi_family == AF_BRIDGE && brid) {
1105                 /* device has been removed from bridge */
1106                 char namebuf[IFNAMSIZ];
1107                 if_indextoname(brid, namebuf);
1108                 wpa_printf(MSG_DEBUG, "nl80211: Remove ifindex %u for bridge "
1109                            "%s", brid, namebuf);
1110                 del_ifidx(drv, brid);
1111         }
1112 }
1113
1114
1115 unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1116 {
1117         struct nl_msg *msg;
1118         int ret;
1119         struct nl80211_bss_info_arg arg;
1120
1121         msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1122         os_memset(&arg, 0, sizeof(arg));
1123         arg.drv = drv;
1124         ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
1125         if (ret == 0) {
1126                 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1127                         arg.ibss_freq : arg.assoc_freq;
1128                 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
1129                            "associated BSS from scan results: %u MHz", freq);
1130                 if (freq)
1131                         drv->assoc_freq = freq;
1132                 return drv->assoc_freq;
1133         }
1134         wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1135                    "(%s)", ret, strerror(-ret));
1136         return drv->assoc_freq;
1137 }
1138
1139
1140 static int get_link_signal(struct nl_msg *msg, void *arg)
1141 {
1142         struct nlattr *tb[NL80211_ATTR_MAX + 1];
1143         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1144         struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1145         static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1146                 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
1147                 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
1148         };
1149         struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1150         static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1151                 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1152                 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1153                 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1154                 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1155         };
1156         struct wpa_signal_info *sig_change = arg;
1157
1158         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1159                   genlmsg_attrlen(gnlh, 0), NULL);
1160         if (!tb[NL80211_ATTR_STA_INFO] ||
1161             nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1162                              tb[NL80211_ATTR_STA_INFO], policy))
1163                 return NL_SKIP;
1164         if (!sinfo[NL80211_STA_INFO_SIGNAL])
1165                 return NL_SKIP;
1166
1167         sig_change->current_signal =
1168                 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1169
1170         if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1171                 sig_change->avg_signal =
1172                         (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1173         else
1174                 sig_change->avg_signal = 0;
1175
1176         if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1177                 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1178                                      sinfo[NL80211_STA_INFO_TX_BITRATE],
1179                                      rate_policy)) {
1180                         sig_change->current_txrate = 0;
1181                 } else {
1182                         if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1183                                 sig_change->current_txrate =
1184                                         nla_get_u16(rinfo[
1185                                              NL80211_RATE_INFO_BITRATE]) * 100;
1186                         }
1187                 }
1188         }
1189
1190         return NL_SKIP;
1191 }
1192
1193
1194 int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1195                             struct wpa_signal_info *sig)
1196 {
1197         struct nl_msg *msg;
1198
1199         sig->current_signal = -9999;
1200         sig->current_txrate = 0;
1201
1202         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
1203             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1204                 nlmsg_free(msg);
1205                 return -ENOBUFS;
1206         }
1207
1208         return send_and_recv_msgs(drv, msg, get_link_signal, sig);
1209 }
1210
1211
1212 static int get_link_noise(struct nl_msg *msg, void *arg)
1213 {
1214         struct nlattr *tb[NL80211_ATTR_MAX + 1];
1215         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1216         struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1217         static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1218                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1219                 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1220         };
1221         struct wpa_signal_info *sig_change = arg;
1222
1223         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1224                   genlmsg_attrlen(gnlh, 0), NULL);
1225
1226         if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1227                 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1228                 return NL_SKIP;
1229         }
1230
1231         if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1232                              tb[NL80211_ATTR_SURVEY_INFO],
1233                              survey_policy)) {
1234                 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1235                            "attributes!");
1236                 return NL_SKIP;
1237         }
1238
1239         if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1240                 return NL_SKIP;
1241
1242         if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1243             sig_change->frequency)
1244                 return NL_SKIP;
1245
1246         if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1247                 return NL_SKIP;
1248
1249         sig_change->current_noise =
1250                 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1251
1252         return NL_SKIP;
1253 }
1254
1255
1256 int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1257                            struct wpa_signal_info *sig_change)
1258 {
1259         struct nl_msg *msg;
1260
1261         sig_change->current_noise = 9999;
1262         sig_change->frequency = drv->assoc_freq;
1263
1264         msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
1265         return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
1266 }
1267
1268
1269 static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1270                                              void *handle)
1271 {
1272         struct nl_cb *cb = eloop_ctx;
1273         int res;
1274
1275         wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
1276
1277         res = nl_recvmsgs(handle, cb);
1278         if (res < 0) {
1279                 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1280                            __func__, res);
1281         }
1282 }
1283
1284
1285 /**
1286  * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1287  * @priv: driver_nl80211 private data
1288  * @alpha2_arg: country to which to switch to
1289  * Returns: 0 on success, -1 on failure
1290  *
1291  * This asks nl80211 to set the regulatory domain for given
1292  * country ISO / IEC alpha2.
1293  */
1294 static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1295 {
1296         struct i802_bss *bss = priv;
1297         struct wpa_driver_nl80211_data *drv = bss->drv;
1298         char alpha2[3];
1299         struct nl_msg *msg;
1300
1301         msg = nlmsg_alloc();
1302         if (!msg)
1303                 return -ENOMEM;
1304
1305         alpha2[0] = alpha2_arg[0];
1306         alpha2[1] = alpha2_arg[1];
1307         alpha2[2] = '\0';
1308
1309         if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1310             nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1311                 nlmsg_free(msg);
1312                 return -EINVAL;
1313         }
1314         if (send_and_recv_msgs(drv, msg, NULL, NULL))
1315                 return -EINVAL;
1316         return 0;
1317 }
1318
1319
1320 static int nl80211_get_country(struct nl_msg *msg, void *arg)
1321 {
1322         char *alpha2 = arg;
1323         struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1324         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1325
1326         nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1327                   genlmsg_attrlen(gnlh, 0), NULL);
1328         if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1329                 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1330                 return NL_SKIP;
1331         }
1332         os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1333         return NL_SKIP;
1334 }
1335
1336
1337 static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1338 {
1339         struct i802_bss *bss = priv;
1340         struct wpa_driver_nl80211_data *drv = bss->drv;
1341         struct nl_msg *msg;
1342         int ret;
1343
1344         msg = nlmsg_alloc();
1345         if (!msg)
1346                 return -ENOMEM;
1347
1348         nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1349         alpha2[0] = '\0';
1350         ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1351         if (!alpha2[0])
1352                 ret = -1;
1353
1354         return ret;
1355 }
1356
1357
1358 static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
1359 {
1360         int ret;
1361
1362         global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1363         if (global->nl_cb == NULL) {
1364                 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1365                            "callbacks");
1366                 return -1;
1367         }
1368
1369         global->nl = nl_create_handle(global->nl_cb, "nl");
1370         if (global->nl == NULL)
1371                 goto err;
1372
1373         global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1374         if (global->nl80211_id < 0) {
1375                 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1376                            "found");
1377                 goto err;
1378         }
1379
1380         global->nl_event = nl_create_handle(global->nl_cb, "event");
1381         if (global->nl_event == NULL)
1382                 goto err;
1383
1384         ret = nl_get_multicast_id(global, "nl80211", "scan");
1385         if (ret >= 0)
1386                 ret = nl_socket_add_membership(global->nl_event, ret);
1387         if (ret < 0) {
1388                 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1389                            "membership for scan events: %d (%s)",
1390                            ret, strerror(-ret));
1391                 goto err;
1392         }
1393
1394         ret = nl_get_multicast_id(global, "nl80211", "mlme");
1395         if (ret >= 0)
1396                 ret = nl_socket_add_membership(global->nl_event, ret);
1397         if (ret < 0) {
1398                 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1399                            "membership for mlme events: %d (%s)",
1400                            ret, strerror(-ret));
1401                 goto err;
1402         }
1403
1404         ret = nl_get_multicast_id(global, "nl80211", "regulatory");
1405         if (ret >= 0)
1406                 ret = nl_socket_add_membership(global->nl_event, ret);
1407         if (ret < 0) {
1408                 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1409                            "membership for regulatory events: %d (%s)",
1410                            ret, strerror(-ret));
1411                 /* Continue without regulatory events */
1412         }
1413
1414         ret = nl_get_multicast_id(global, "nl80211", "vendor");
1415         if (ret >= 0)
1416                 ret = nl_socket_add_membership(global->nl_event, ret);
1417         if (ret < 0) {
1418                 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1419                            "membership for vendor events: %d (%s)",
1420                            ret, strerror(-ret));
1421                 /* Continue without vendor events */
1422         }
1423
1424         nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1425                   no_seq_check, NULL);
1426         nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1427                   process_global_event, global);
1428
1429         nl80211_register_eloop_read(&global->nl_event,
1430                                     wpa_driver_nl80211_event_receive,
1431                                     global->nl_cb);
1432
1433         return 0;
1434
1435 err:
1436         nl_destroy_handles(&global->nl_event);
1437         nl_destroy_handles(&global->nl);
1438         nl_cb_put(global->nl_cb);
1439         global->nl_cb = NULL;
1440         return -1;
1441 }
1442
1443
1444 static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv)
1445 {
1446         drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1447         if (!drv->nl_cb) {
1448                 wpa_printf(MSG_ERROR, "nl80211: Failed to alloc cb struct");
1449                 return -1;
1450         }
1451
1452         nl_cb_set(drv->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1453                   no_seq_check, NULL);
1454         nl_cb_set(drv->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1455                   process_drv_event, drv);
1456
1457         return 0;
1458 }
1459
1460
1461 static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1462 {
1463         wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
1464         /*
1465          * This may be for any interface; use ifdown event to disable
1466          * interface.
1467          */
1468 }
1469
1470
1471 static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1472 {
1473         struct wpa_driver_nl80211_data *drv = ctx;
1474         wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
1475         if (i802_set_iface_flags(drv->first_bss, 1)) {
1476                 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1477                            "after rfkill unblock");
1478                 return;
1479         }
1480         /* rtnetlink ifup handler will report interface as enabled */
1481 }
1482
1483
1484 static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1485                                                       void *eloop_ctx,
1486                                                       void *handle)
1487 {
1488         struct wpa_driver_nl80211_data *drv = eloop_ctx;
1489         u8 data[2048];
1490         struct msghdr msg;
1491         struct iovec entry;
1492         u8 control[512];
1493         struct cmsghdr *cmsg;
1494         int res, found_ee = 0, found_wifi = 0, acked = 0;
1495         union wpa_event_data event;
1496
1497         memset(&msg, 0, sizeof(msg));
1498         msg.msg_iov = &entry;
1499         msg.msg_iovlen = 1;
1500         entry.iov_base = data;
1501         entry.iov_len = sizeof(data);
1502         msg.msg_control = &control;
1503         msg.msg_controllen = sizeof(control);
1504
1505         res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1506         /* if error or not fitting 802.3 header, return */
1507         if (res < 14)
1508                 return;
1509
1510         for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1511         {
1512                 if (cmsg->cmsg_level == SOL_SOCKET &&
1513                     cmsg->cmsg_type == SCM_WIFI_STATUS) {
1514                         int *ack;
1515
1516                         found_wifi = 1;
1517                         ack = (void *)CMSG_DATA(cmsg);
1518                         acked = *ack;
1519                 }
1520
1521                 if (cmsg->cmsg_level == SOL_PACKET &&
1522                     cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1523                         struct sock_extended_err *err =
1524                                 (struct sock_extended_err *)CMSG_DATA(cmsg);
1525
1526                         if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1527                                 found_ee = 1;
1528                 }
1529         }
1530
1531         if (!found_ee || !found_wifi)
1532                 return;
1533
1534         memset(&event, 0, sizeof(event));
1535         event.eapol_tx_status.dst = data;
1536         event.eapol_tx_status.data = data + 14;
1537         event.eapol_tx_status.data_len = res - 14;
1538         event.eapol_tx_status.ack = acked;
1539         wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1540 }
1541
1542
1543 static int nl80211_init_bss(struct i802_bss *bss)
1544 {
1545         bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1546         if (!bss->nl_cb)
1547                 return -1;
1548
1549         nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1550                   no_seq_check, NULL);
1551         nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1552                   process_bss_event, bss);
1553
1554         return 0;
1555 }
1556
1557
1558 static void nl80211_destroy_bss(struct i802_bss *bss)
1559 {
1560         nl_cb_put(bss->nl_cb);
1561         bss->nl_cb = NULL;
1562 }
1563
1564
1565 static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
1566                                           void *global_priv, int hostapd,
1567                                           const u8 *set_addr,
1568                                           const char *driver_params)
1569 {
1570         struct wpa_driver_nl80211_data *drv;
1571         struct rfkill_config *rcfg;
1572         struct i802_bss *bss;
1573
1574         if (global_priv == NULL)
1575                 return NULL;
1576         drv = os_zalloc(sizeof(*drv));
1577         if (drv == NULL)
1578                 return NULL;
1579         drv->global = global_priv;
1580         drv->ctx = ctx;
1581         drv->hostapd = !!hostapd;
1582         drv->eapol_sock = -1;
1583         drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
1584         drv->if_indices = drv->default_if_indices;
1585
1586         drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
1587         if (!drv->first_bss) {
1588                 os_free(drv);
1589                 return NULL;
1590         }
1591         bss = drv->first_bss;
1592         bss->drv = drv;
1593         bss->ctx = ctx;
1594
1595         os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
1596         drv->monitor_ifidx = -1;
1597         drv->monitor_sock = -1;
1598         drv->eapol_tx_sock = -1;
1599         drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
1600
1601         if (wpa_driver_nl80211_init_nl(drv)) {
1602                 os_free(drv);
1603                 return NULL;
1604         }
1605
1606         if (nl80211_init_bss(bss))
1607                 goto failed;
1608
1609         rcfg = os_zalloc(sizeof(*rcfg));
1610         if (rcfg == NULL)
1611                 goto failed;
1612         rcfg->ctx = drv;
1613         os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
1614         rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
1615         rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
1616         drv->rfkill = rfkill_init(rcfg);
1617         if (drv->rfkill == NULL) {
1618                 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
1619                 os_free(rcfg);
1620         }
1621
1622         if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
1623                 drv->start_iface_up = 1;
1624
1625         if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
1626                 goto failed;
1627
1628         drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
1629         if (drv->eapol_tx_sock < 0)
1630                 goto failed;
1631
1632         if (drv->data_tx_status) {
1633                 int enabled = 1;
1634
1635                 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
1636                                &enabled, sizeof(enabled)) < 0) {
1637                         wpa_printf(MSG_DEBUG,
1638                                 "nl80211: wifi status sockopt failed\n");
1639                         drv->data_tx_status = 0;
1640                         if (!drv->use_monitor)
1641                                 drv->capa.flags &=
1642                                         ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1643                 } else {
1644                         eloop_register_read_sock(drv->eapol_tx_sock,
1645                                 wpa_driver_nl80211_handle_eapol_tx_status,
1646                                 drv, NULL);
1647                 }
1648         }
1649
1650         if (drv->global) {
1651                 dl_list_add(&drv->global->interfaces, &drv->list);
1652                 drv->in_interface_list = 1;
1653         }
1654
1655         return bss;
1656
1657 failed:
1658         wpa_driver_nl80211_deinit(bss);
1659         return NULL;
1660 }
1661
1662
1663 /**
1664  * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1665  * @ctx: context to be used when calling wpa_supplicant functions,
1666  * e.g., wpa_supplicant_event()
1667  * @ifname: interface name, e.g., wlan0
1668  * @global_priv: private driver global data from global_init()
1669  * Returns: Pointer to private data, %NULL on failure
1670  */
1671 static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
1672                                       void *global_priv)
1673 {
1674         return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
1675                                            NULL);
1676 }
1677
1678
1679 static int nl80211_register_frame(struct i802_bss *bss,
1680                                   struct nl_handle *nl_handle,
1681                                   u16 type, const u8 *match, size_t match_len)
1682 {
1683         struct wpa_driver_nl80211_data *drv = bss->drv;
1684         struct nl_msg *msg;
1685         int ret;
1686         char buf[30];
1687
1688         buf[0] = '\0';
1689         wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
1690         wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
1691                    type, fc2str(type), nl_handle, buf);
1692
1693         if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
1694             nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
1695             nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
1696                 nlmsg_free(msg);
1697                 return -1;
1698         }
1699
1700         ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
1701         if (ret) {
1702                 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
1703                            "failed (type=%u): ret=%d (%s)",
1704                            type, ret, strerror(-ret));
1705                 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
1706                             match, match_len);
1707         }
1708         return ret;
1709 }
1710
1711
1712 static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
1713 {
1714         struct wpa_driver_nl80211_data *drv = bss->drv;
1715
1716         if (bss->nl_mgmt) {
1717                 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
1718                            "already on! (nl_mgmt=%p)", bss->nl_mgmt);
1719                 return -1;
1720         }
1721
1722         bss->nl_mgmt = nl_create_handle(drv->nl_cb, "mgmt");
1723         if (bss->nl_mgmt == NULL)
1724                 return -1;
1725
1726         return 0;
1727 }
1728
1729
1730 static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
1731 {
1732         nl80211_register_eloop_read(&bss->nl_mgmt,
1733                                     wpa_driver_nl80211_event_receive,
1734                                     bss->nl_cb);
1735 }
1736
1737
1738 static int nl80211_register_action_frame(struct i802_bss *bss,
1739                                          const u8 *match, size_t match_len)
1740 {
1741         u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
1742         return nl80211_register_frame(bss, bss->nl_mgmt,
1743                                       type, match, match_len);
1744 }
1745
1746
1747 static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
1748 {
1749         struct wpa_driver_nl80211_data *drv = bss->drv;
1750         int ret = 0;
1751
1752         if (nl80211_alloc_mgmt_handle(bss))
1753                 return -1;
1754         wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
1755                    "handle %p", bss->nl_mgmt);
1756
1757         if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
1758                 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
1759
1760                 /* register for any AUTH message */
1761                 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
1762         }
1763
1764 #ifdef CONFIG_INTERWORKING
1765         /* QoS Map Configure */
1766         if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
1767                 ret = -1;
1768 #endif /* CONFIG_INTERWORKING */
1769 #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
1770         /* GAS Initial Request */
1771         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
1772                 ret = -1;
1773         /* GAS Initial Response */
1774         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
1775                 ret = -1;
1776         /* GAS Comeback Request */
1777         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
1778                 ret = -1;
1779         /* GAS Comeback Response */
1780         if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
1781                 ret = -1;
1782         /* Protected GAS Initial Request */
1783         if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
1784                 ret = -1;
1785         /* Protected GAS Initial Response */
1786         if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
1787                 ret = -1;
1788         /* Protected GAS Comeback Request */
1789         if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
1790                 ret = -1;
1791         /* Protected GAS Comeback Response */
1792         if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
1793                 ret = -1;
1794 #endif /* CONFIG_P2P || CONFIG_INTERWORKING */
1795 #ifdef CONFIG_P2P
1796         /* P2P Public Action */
1797         if (nl80211_register_action_frame(bss,
1798                                           (u8 *) "\x04\x09\x50\x6f\x9a\x09",
1799                                           6) < 0)
1800                 ret = -1;
1801         /* P2P Action */
1802         if (nl80211_register_action_frame(bss,
1803                                           (u8 *) "\x7f\x50\x6f\x9a\x09",
1804                                           5) < 0)
1805                 ret = -1;
1806 #endif /* CONFIG_P2P */
1807 #ifdef CONFIG_IEEE80211W
1808         /* SA Query Response */
1809         if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
1810                 ret = -1;
1811 #endif /* CONFIG_IEEE80211W */
1812 #ifdef CONFIG_TDLS
1813         if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
1814                 /* TDLS Discovery Response */
1815                 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
1816                     0)
1817                         ret = -1;
1818         }
1819 #endif /* CONFIG_TDLS */
1820
1821         /* FT Action frames */
1822         if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
1823                 ret = -1;
1824         else
1825                 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
1826                         WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
1827
1828         /* WNM - BSS Transition Management Request */
1829         if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
1830                 ret = -1;
1831         /* WNM-Sleep Mode Response */
1832         if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
1833                 ret = -1;
1834
1835 #ifdef CONFIG_HS20
1836         /* WNM-Notification */
1837         if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
1838                 ret = -1;
1839 #endif /* CONFIG_HS20 */
1840
1841         /* WMM-AC ADDTS Response */
1842         if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
1843                 return -1;
1844
1845         /* WMM-AC DELTS */
1846         if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
1847                 return -1;
1848
1849         /* Radio Measurement - Neighbor Report Response */
1850         if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
1851                 ret = -1;
1852
1853         /* Radio Measurement - Link Measurement Request */
1854         if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
1855             (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
1856                 ret = -1;
1857
1858         nl80211_mgmt_handle_register_eloop(bss);
1859
1860         return ret;
1861 }
1862
1863
1864 static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
1865 {
1866         int ret = 0;
1867
1868         if (nl80211_alloc_mgmt_handle(bss))
1869                 return -1;
1870
1871         wpa_printf(MSG_DEBUG,
1872                    "nl80211: Subscribe to mgmt frames with mesh handle %p",
1873                    bss->nl_mgmt);
1874
1875         /* Auth frames for mesh SAE */
1876         if (nl80211_register_frame(bss, bss->nl_mgmt,
1877                                    (WLAN_FC_TYPE_MGMT << 2) |
1878                                    (WLAN_FC_STYPE_AUTH << 4),
1879                                    NULL, 0) < 0)
1880                 ret = -1;
1881
1882         /* Mesh peering open */
1883         if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
1884                 ret = -1;
1885         /* Mesh peering confirm */
1886         if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
1887                 ret = -1;
1888         /* Mesh peering close */
1889         if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
1890                 ret = -1;
1891
1892         nl80211_mgmt_handle_register_eloop(bss);
1893
1894         return ret;
1895 }
1896
1897
1898 static int nl80211_register_spurious_class3(struct i802_bss *bss)
1899 {
1900         struct nl_msg *msg;
1901         int ret;
1902
1903         msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
1904         ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
1905         if (ret) {
1906                 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
1907                            "failed: ret=%d (%s)",
1908                            ret, strerror(-ret));
1909         }
1910         return ret;
1911 }
1912
1913
1914 static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
1915 {
1916         static const int stypes[] = {
1917                 WLAN_FC_STYPE_AUTH,
1918                 WLAN_FC_STYPE_ASSOC_REQ,
1919                 WLAN_FC_STYPE_REASSOC_REQ,
1920                 WLAN_FC_STYPE_DISASSOC,
1921                 WLAN_FC_STYPE_DEAUTH,
1922                 WLAN_FC_STYPE_ACTION,
1923                 WLAN_FC_STYPE_PROBE_REQ,
1924 /* Beacon doesn't work as mac80211 doesn't currently allow
1925  * it, but it wouldn't really be the right thing anyway as
1926  * it isn't per interface ... maybe just dump the scan
1927  * results periodically for OLBC?
1928  */
1929                 /* WLAN_FC_STYPE_BEACON, */
1930         };
1931         unsigned int i;
1932
1933         if (nl80211_alloc_mgmt_handle(bss))
1934                 return -1;
1935         wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
1936                    "handle %p", bss->nl_mgmt);
1937
1938         for (i = 0; i < ARRAY_SIZE(stypes); i++) {
1939                 if (nl80211_register_frame(bss, bss->nl_mgmt,
1940                                            (WLAN_FC_TYPE_MGMT << 2) |
1941                                            (stypes[i] << 4),
1942                                            NULL, 0) < 0) {
1943                         goto out_err;
1944                 }
1945         }
1946
1947         if (nl80211_register_spurious_class3(bss))
1948                 goto out_err;
1949
1950         if (nl80211_get_wiphy_data_ap(bss) == NULL)
1951                 goto out_err;
1952
1953         nl80211_mgmt_handle_register_eloop(bss);
1954         return 0;
1955
1956 out_err:
1957         nl_destroy_handles(&bss->nl_mgmt);
1958         return -1;
1959 }
1960
1961
1962 static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
1963 {
1964         if (nl80211_alloc_mgmt_handle(bss))
1965                 return -1;
1966         wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
1967                    "handle %p (device SME)", bss->nl_mgmt);
1968
1969         if (nl80211_register_frame(bss, bss->nl_mgmt,
1970                                    (WLAN_FC_TYPE_MGMT << 2) |
1971                                    (WLAN_FC_STYPE_ACTION << 4),
1972                                    NULL, 0) < 0)
1973                 goto out_err;
1974
1975         nl80211_mgmt_handle_register_eloop(bss);
1976         return 0;
1977
1978 out_err:
1979         nl_destroy_handles(&bss->nl_mgmt);
1980         return -1;
1981 }
1982
1983
1984 static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
1985 {
1986         if (bss->nl_mgmt == NULL)
1987                 return;
1988         wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
1989                    "(%s)", bss->nl_mgmt, reason);
1990         nl80211_destroy_eloop_handle(&bss->nl_mgmt);
1991
1992         nl80211_put_wiphy_data_ap(bss);
1993 }
1994
1995
1996 static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
1997 {
1998         wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
1999 }
2000
2001
2002 static void nl80211_del_p2pdev(struct i802_bss *bss)
2003 {
2004         struct nl_msg *msg;
2005         int ret;
2006
2007         msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2008         ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
2009
2010         wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2011                    bss->ifname, (long long unsigned int) bss->wdev_id,
2012                    strerror(-ret));
2013 }
2014
2015
2016 static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2017 {
2018         struct nl_msg *msg;
2019         int ret;
2020
2021         msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2022                               NL80211_CMD_STOP_P2P_DEVICE);
2023         ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
2024
2025         wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2026                    start ? "Start" : "Stop",
2027                    bss->ifname, (long long unsigned int) bss->wdev_id,
2028                    strerror(-ret));
2029         return ret;
2030 }
2031
2032
2033 static int i802_set_iface_flags(struct i802_bss *bss, int up)
2034 {
2035         enum nl80211_iftype nlmode;
2036
2037         nlmode = nl80211_get_ifmode(bss);
2038         if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2039                 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2040                                              bss->ifname, up);
2041         }
2042
2043         /* P2P Device has start/stop which is equivalent */
2044         return nl80211_set_p2pdev(bss, up);
2045 }
2046
2047
2048 static int
2049 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
2050                                    const u8 *set_addr, int first,
2051                                    const char *driver_params)
2052 {
2053         struct i802_bss *bss = drv->first_bss;
2054         int send_rfkill_event = 0;
2055         enum nl80211_iftype nlmode;
2056
2057         drv->ifindex = if_nametoindex(bss->ifname);
2058         bss->ifindex = drv->ifindex;
2059         bss->wdev_id = drv->global->if_add_wdevid;
2060         bss->wdev_id_set = drv->global->if_add_wdevid_set;
2061
2062         bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2063         bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
2064         drv->global->if_add_wdevid_set = 0;
2065
2066         if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2067                 bss->static_ap = 1;
2068
2069         if (wpa_driver_nl80211_capa(drv))
2070                 return -1;
2071
2072         if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2073                 return -1;
2074
2075         wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2076                    bss->ifname, drv->phyname);
2077
2078         if (set_addr &&
2079             (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2080              linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2081                                 set_addr)))
2082                 return -1;
2083
2084         if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2085                 drv->start_mode_ap = 1;
2086
2087         if (drv->hostapd || bss->static_ap)
2088                 nlmode = NL80211_IFTYPE_AP;
2089         else if (bss->if_dynamic)
2090                 nlmode = nl80211_get_ifmode(bss);
2091         else
2092                 nlmode = NL80211_IFTYPE_STATION;
2093
2094         if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
2095                 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
2096                 return -1;
2097         }
2098
2099         if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2100                 nl80211_get_macaddr(bss);
2101
2102         if (!rfkill_is_blocked(drv->rfkill)) {
2103                 int ret = i802_set_iface_flags(bss, 1);
2104                 if (ret) {
2105                         wpa_printf(MSG_ERROR, "nl80211: Could not set "
2106                                    "interface '%s' UP", bss->ifname);
2107                         return ret;
2108                 }
2109                 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2110                         return ret;
2111         } else {
2112                 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2113                            "interface '%s' due to rfkill", bss->ifname);
2114                 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2115                         return 0;
2116                 drv->if_disabled = 1;
2117                 send_rfkill_event = 1;
2118         }
2119
2120         if (!drv->hostapd)
2121                 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2122                                        1, IF_OPER_DORMANT);
2123
2124         if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2125                                bss->addr))
2126                 return -1;
2127         os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
2128
2129         if (send_rfkill_event) {
2130                 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2131                                        drv, drv->ctx);
2132         }
2133
2134         return 0;
2135 }
2136
2137
2138 static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
2139 {
2140         struct nl_msg *msg;
2141
2142         wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2143                    drv->ifindex);
2144         msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
2145         return send_and_recv_msgs(drv, msg, NULL, NULL);
2146 }
2147
2148
2149 /**
2150  * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
2151  * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
2152  *
2153  * Shut down driver interface and processing of driver events. Free
2154  * private data buffer if one was allocated in wpa_driver_nl80211_init().
2155  */
2156 static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
2157 {
2158         struct wpa_driver_nl80211_data *drv = bss->drv;
2159
2160         wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2161                    bss->ifname, drv->disabled_11b_rates);
2162
2163         bss->in_deinit = 1;
2164         if (drv->data_tx_status)
2165                 eloop_unregister_read_sock(drv->eapol_tx_sock);
2166         if (drv->eapol_tx_sock >= 0)
2167                 close(drv->eapol_tx_sock);
2168
2169         if (bss->nl_preq)
2170                 wpa_driver_nl80211_probe_req_report(bss, 0);
2171         if (bss->added_if_into_bridge) {
2172                 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2173                                     bss->ifname) < 0)
2174                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2175                                    "interface %s from bridge %s: %s",
2176                                    bss->ifname, bss->brname, strerror(errno));
2177                 if (drv->rtnl_sk)
2178                         nl80211_handle_destroy(drv->rtnl_sk);
2179         }
2180         if (bss->added_bridge) {
2181                 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
2182                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2183                                    "bridge %s: %s",
2184                                    bss->brname, strerror(errno));
2185         }
2186
2187         nl80211_remove_monitor_interface(drv);
2188
2189         if (is_ap_interface(drv->nlmode))
2190                 wpa_driver_nl80211_del_beacon(drv);
2191
2192         if (drv->eapol_sock >= 0) {
2193                 eloop_unregister_read_sock(drv->eapol_sock);
2194                 close(drv->eapol_sock);
2195         }
2196
2197         if (drv->if_indices != drv->default_if_indices)
2198                 os_free(drv->if_indices);
2199
2200         if (drv->disabled_11b_rates)
2201                 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2202
2203         netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2204                                IF_OPER_UP);
2205         eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
2206         rfkill_deinit(drv->rfkill);
2207
2208         eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2209
2210         if (!drv->start_iface_up)
2211                 (void) i802_set_iface_flags(bss, 0);
2212
2213         if (drv->addr_changed) {
2214                 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2215                                           0) < 0) {
2216                         wpa_printf(MSG_DEBUG,
2217                                    "nl80211: Could not set interface down to restore permanent MAC address");
2218                 }
2219                 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2220                                        drv->perm_addr) < 0) {
2221                         wpa_printf(MSG_DEBUG,
2222                                    "nl80211: Could not restore permanent MAC address");
2223                 }
2224         }
2225
2226         if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2227                 if (!drv->hostapd || !drv->start_mode_ap)
2228                         wpa_driver_nl80211_set_mode(bss,
2229                                                     NL80211_IFTYPE_STATION);
2230                 nl80211_mgmt_unsubscribe(bss, "deinit");
2231         } else {
2232                 nl80211_mgmt_unsubscribe(bss, "deinit");
2233                 nl80211_del_p2pdev(bss);
2234         }
2235         nl_cb_put(drv->nl_cb);
2236
2237         nl80211_destroy_bss(drv->first_bss);
2238
2239         os_free(drv->filter_ssids);
2240
2241         os_free(drv->auth_ie);
2242
2243         if (drv->in_interface_list)
2244                 dl_list_del(&drv->list);
2245
2246         os_free(drv->extended_capa);
2247         os_free(drv->extended_capa_mask);
2248         os_free(drv->first_bss);
2249         os_free(drv);
2250 }
2251
2252
2253 static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2254 {
2255         switch (alg) {
2256         case WPA_ALG_WEP:
2257                 if (key_len == 5)
2258                         return WLAN_CIPHER_SUITE_WEP40;
2259                 return WLAN_CIPHER_SUITE_WEP104;
2260         case WPA_ALG_TKIP:
2261                 return WLAN_CIPHER_SUITE_TKIP;
2262         case WPA_ALG_CCMP:
2263                 return WLAN_CIPHER_SUITE_CCMP;
2264         case WPA_ALG_GCMP:
2265                 return WLAN_CIPHER_SUITE_GCMP;
2266         case WPA_ALG_CCMP_256:
2267                 return WLAN_CIPHER_SUITE_CCMP_256;
2268         case WPA_ALG_GCMP_256:
2269                 return WLAN_CIPHER_SUITE_GCMP_256;
2270         case WPA_ALG_IGTK:
2271                 return WLAN_CIPHER_SUITE_AES_CMAC;
2272         case WPA_ALG_BIP_GMAC_128:
2273                 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
2274         case WPA_ALG_BIP_GMAC_256:
2275                 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
2276         case WPA_ALG_BIP_CMAC_256:
2277                 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
2278         case WPA_ALG_SMS4:
2279                 return WLAN_CIPHER_SUITE_SMS4;
2280         case WPA_ALG_KRK:
2281                 return WLAN_CIPHER_SUITE_KRK;
2282         case WPA_ALG_NONE:
2283         case WPA_ALG_PMK:
2284                 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2285                            alg);
2286                 return 0;
2287         }
2288
2289         wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2290                    alg);
2291         return 0;
2292 }
2293
2294
2295 static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2296 {
2297         switch (cipher) {
2298         case WPA_CIPHER_CCMP_256:
2299                 return WLAN_CIPHER_SUITE_CCMP_256;
2300         case WPA_CIPHER_GCMP_256:
2301                 return WLAN_CIPHER_SUITE_GCMP_256;
2302         case WPA_CIPHER_CCMP:
2303                 return WLAN_CIPHER_SUITE_CCMP;
2304         case WPA_CIPHER_GCMP:
2305                 return WLAN_CIPHER_SUITE_GCMP;
2306         case WPA_CIPHER_TKIP:
2307                 return WLAN_CIPHER_SUITE_TKIP;
2308         case WPA_CIPHER_WEP104:
2309                 return WLAN_CIPHER_SUITE_WEP104;
2310         case WPA_CIPHER_WEP40:
2311                 return WLAN_CIPHER_SUITE_WEP40;
2312         case WPA_CIPHER_GTK_NOT_USED:
2313                 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
2314         }
2315
2316         return 0;
2317 }
2318
2319
2320 static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2321                                        int max_suites)
2322 {
2323         int num_suites = 0;
2324
2325         if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
2326                 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
2327         if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
2328                 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
2329         if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
2330                 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
2331         if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
2332                 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
2333         if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
2334                 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
2335         if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
2336                 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
2337         if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
2338                 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
2339
2340         return num_suites;
2341 }
2342
2343
2344 static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2345                                   const u8 *key, size_t key_len)
2346 {
2347         struct nl_msg *msg;
2348         int ret;
2349
2350         if (!drv->key_mgmt_set_key_vendor_cmd_avail)
2351                 return 0;
2352
2353         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2354             nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2355             nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2356                         QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2357             nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
2358                 nlmsg_free(msg);
2359                 return -1;
2360         }
2361         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2362         if (ret) {
2363                 wpa_printf(MSG_DEBUG,
2364                            "nl80211: Key management set key failed: ret=%d (%s)",
2365                            ret, strerror(-ret));
2366         }
2367
2368         return ret;
2369 }
2370
2371
2372 static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
2373                                       enum wpa_alg alg, const u8 *addr,
2374                                       int key_idx, int set_tx,
2375                                       const u8 *seq, size_t seq_len,
2376                                       const u8 *key, size_t key_len)
2377 {
2378         struct wpa_driver_nl80211_data *drv = bss->drv;
2379         int ifindex;
2380         struct nl_msg *msg;
2381         int ret;
2382         int tdls = 0;
2383
2384         /* Ignore for P2P Device */
2385         if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2386                 return 0;
2387
2388         ifindex = if_nametoindex(ifname);
2389         wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
2390                    "set_tx=%d seq_len=%lu key_len=%lu",
2391                    __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
2392                    (unsigned long) seq_len, (unsigned long) key_len);
2393 #ifdef CONFIG_TDLS
2394         if (key_idx == -1) {
2395                 key_idx = 0;
2396                 tdls = 1;
2397         }
2398 #endif /* CONFIG_TDLS */
2399
2400         if (alg == WPA_ALG_PMK && drv->key_mgmt_set_key_vendor_cmd_avail) {
2401                 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2402                            __func__);
2403                 ret = issue_key_mgmt_set_key(drv, key, key_len);
2404                 return ret;
2405         }
2406
2407         if (alg == WPA_ALG_NONE) {
2408                 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2409                 if (!msg)
2410                         return -ENOBUFS;
2411         } else {
2412                 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2413                 if (!msg ||
2414                     nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
2415                     nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER,
2416                                 wpa_alg_to_cipher_suite(alg, key_len)))
2417                         goto fail;
2418                 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
2419         }
2420
2421         if (seq && seq_len) {
2422                 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2423                         goto fail;
2424                 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2425         }
2426
2427         if (addr && !is_broadcast_ether_addr(addr)) {
2428                 wpa_printf(MSG_DEBUG, "   addr=" MACSTR, MAC2STR(addr));
2429                 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2430                         goto fail;
2431
2432                 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2433                         wpa_printf(MSG_DEBUG, "   RSN IBSS RX GTK");
2434                         if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
2435                                         NL80211_KEYTYPE_GROUP))
2436                                 goto fail;
2437                 }
2438         } else if (addr && is_broadcast_ether_addr(addr)) {
2439                 struct nlattr *types;
2440
2441                 wpa_printf(MSG_DEBUG, "   broadcast key");
2442
2443                 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
2444                 if (!types ||
2445                     nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2446                         goto fail;
2447                 nla_nest_end(msg, types);
2448         }
2449         if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2450                 goto fail;
2451
2452         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2453         if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
2454                 ret = 0;
2455         if (ret)
2456                 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2457                            ret, strerror(-ret));
2458
2459         /*
2460          * If we failed or don't need to set the default TX key (below),
2461          * we're done here.
2462          */
2463         if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
2464                 return ret;
2465         if (is_ap_interface(drv->nlmode) && addr &&
2466             !is_broadcast_ether_addr(addr))
2467                 return ret;
2468
2469         msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
2470         if (!msg ||
2471             nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
2472             nla_put_flag(msg, alg == WPA_ALG_IGTK ?
2473                          NL80211_ATTR_KEY_DEFAULT_MGMT :
2474                          NL80211_ATTR_KEY_DEFAULT))
2475                 goto fail;
2476         if (addr && is_broadcast_ether_addr(addr)) {
2477                 struct nlattr *types;
2478
2479                 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
2480                 if (!types ||
2481                     nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2482                         goto fail;
2483                 nla_nest_end(msg, types);
2484         } else if (addr) {
2485                 struct nlattr *types;
2486
2487                 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
2488                 if (!types ||
2489                     nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
2490                         goto fail;
2491                 nla_nest_end(msg, types);
2492         }
2493
2494         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2495         if (ret == -ENOENT)
2496                 ret = 0;
2497         if (ret)
2498                 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
2499                            "err=%d %s)", ret, strerror(-ret));
2500         return ret;
2501
2502 fail:
2503         nlmsg_free(msg);
2504         return -ENOBUFS;
2505 }
2506
2507
2508 static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
2509                       int key_idx, int defkey,
2510                       const u8 *seq, size_t seq_len,
2511                       const u8 *key, size_t key_len)
2512 {
2513         struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
2514         if (!key_attr)
2515                 return -1;
2516
2517         if (defkey && alg == WPA_ALG_IGTK) {
2518                 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
2519                         return -1;
2520         } else if (defkey) {
2521                 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
2522                         return -1;
2523         }
2524
2525         if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
2526             nla_put_u32(msg, NL80211_KEY_CIPHER,
2527                         wpa_alg_to_cipher_suite(alg, key_len)) ||
2528             (seq && seq_len &&
2529              nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
2530             nla_put(msg, NL80211_KEY_DATA, key_len, key))
2531                 return -1;
2532
2533         nla_nest_end(msg, key_attr);
2534
2535         return 0;
2536 }
2537
2538
2539 static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
2540                                  struct nl_msg *msg)
2541 {
2542         int i, privacy = 0;
2543         struct nlattr *nl_keys, *nl_key;
2544
2545         for (i = 0; i < 4; i++) {
2546                 if (!params->wep_key[i])
2547                         continue;
2548                 privacy = 1;
2549                 break;
2550         }
2551         if (params->wps == WPS_MODE_PRIVACY)
2552                 privacy = 1;
2553         if (params->pairwise_suite &&
2554             params->pairwise_suite != WPA_CIPHER_NONE)
2555                 privacy = 1;
2556
2557         if (!privacy)
2558                 return 0;
2559
2560         if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
2561                 return -ENOBUFS;
2562
2563         nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
2564         if (!nl_keys)
2565                 return -ENOBUFS;
2566
2567         for (i = 0; i < 4; i++) {
2568                 if (!params->wep_key[i])
2569                         continue;
2570
2571                 nl_key = nla_nest_start(msg, i);
2572                 if (!nl_key ||
2573                     nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
2574                             params->wep_key[i]) ||
2575                     nla_put_u32(msg, NL80211_KEY_CIPHER,
2576                                 params->wep_key_len[i] == 5 ?
2577                                 WLAN_CIPHER_SUITE_WEP40 :
2578                                 WLAN_CIPHER_SUITE_WEP104) ||
2579                     nla_put_u8(msg, NL80211_KEY_IDX, i) ||
2580                     (i == params->wep_tx_keyidx &&
2581                      nla_put_flag(msg, NL80211_KEY_DEFAULT)))
2582                         return -ENOBUFS;
2583
2584                 nla_nest_end(msg, nl_key);
2585         }
2586         nla_nest_end(msg, nl_keys);
2587
2588         return 0;
2589 }
2590
2591
2592 int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
2593                             const u8 *addr, int cmd, u16 reason_code,
2594                             int local_state_change)
2595 {
2596         int ret;
2597         struct nl_msg *msg;
2598
2599         if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
2600             nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
2601             (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
2602             (local_state_change &&
2603              nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
2604                 nlmsg_free(msg);
2605                 return -1;
2606         }
2607
2608         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2609         if (ret) {
2610                 wpa_dbg(drv->ctx, MSG_DEBUG,
2611                         "nl80211: MLME command failed: reason=%u ret=%d (%s)",
2612                         reason_code, ret, strerror(-ret));
2613         }
2614         return ret;
2615 }
2616
2617
2618 static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
2619                                          int reason_code)
2620 {
2621         int ret;
2622
2623         wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
2624         nl80211_mark_disconnected(drv);
2625         /* Disconnect command doesn't need BSSID - it uses cached value */
2626         ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
2627                                       reason_code, 0);
2628         /*
2629          * For locally generated disconnect, supplicant already generates a
2630          * DEAUTH event, so ignore the event from NL80211.
2631          */
2632         drv->ignore_next_local_disconnect = ret == 0;
2633
2634         return ret;
2635 }
2636
2637
2638 static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
2639                                              const u8 *addr, int reason_code)
2640 {
2641         struct wpa_driver_nl80211_data *drv = bss->drv;
2642         int ret;
2643
2644         if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
2645                 nl80211_mark_disconnected(drv);
2646                 return nl80211_leave_ibss(drv);
2647         }
2648         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
2649                 return wpa_driver_nl80211_disconnect(drv, reason_code);
2650         wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
2651                    __func__, MAC2STR(addr), reason_code);
2652         nl80211_mark_disconnected(drv);
2653         ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
2654                                       reason_code, 0);
2655         /*
2656          * For locally generated deauthenticate, supplicant already generates a
2657          * DEAUTH event, so ignore the event from NL80211.
2658          */
2659         drv->ignore_next_local_deauth = ret == 0;
2660         return ret;
2661 }
2662
2663
2664 static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
2665                                      struct wpa_driver_auth_params *params)
2666 {
2667         int i;
2668
2669         drv->auth_freq = params->freq;
2670         drv->auth_alg = params->auth_alg;
2671         drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
2672         drv->auth_local_state_change = params->local_state_change;
2673         drv->auth_p2p = params->p2p;
2674
2675         if (params->bssid)
2676                 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
2677         else
2678                 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
2679
2680         if (params->ssid) {
2681                 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
2682                 drv->auth_ssid_len = params->ssid_len;
2683         } else
2684                 drv->auth_ssid_len = 0;
2685
2686
2687         os_free(drv->auth_ie);
2688         drv->auth_ie = NULL;
2689         drv->auth_ie_len = 0;
2690         if (params->ie) {
2691                 drv->auth_ie = os_malloc(params->ie_len);
2692                 if (drv->auth_ie) {
2693                         os_memcpy(drv->auth_ie, params->ie, params->ie_len);
2694                         drv->auth_ie_len = params->ie_len;
2695                 }
2696         }
2697
2698         for (i = 0; i < 4; i++) {
2699                 if (params->wep_key[i] && params->wep_key_len[i] &&
2700                     params->wep_key_len[i] <= 16) {
2701                         os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
2702                                   params->wep_key_len[i]);
2703                         drv->auth_wep_key_len[i] = params->wep_key_len[i];
2704                 } else
2705                         drv->auth_wep_key_len[i] = 0;
2706         }
2707 }
2708
2709
2710 static int wpa_driver_nl80211_authenticate(
2711         struct i802_bss *bss, struct wpa_driver_auth_params *params)
2712 {
2713         struct wpa_driver_nl80211_data *drv = bss->drv;
2714         int ret = -1, i;
2715         struct nl_msg *msg;
2716         enum nl80211_auth_type type;
2717         enum nl80211_iftype nlmode;
2718         int count = 0;
2719         int is_retry;
2720
2721         is_retry = drv->retry_auth;
2722         drv->retry_auth = 0;
2723         drv->ignore_deauth_event = 0;
2724
2725         nl80211_mark_disconnected(drv);
2726         os_memset(drv->auth_bssid, 0, ETH_ALEN);
2727         if (params->bssid)
2728                 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
2729         else
2730                 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
2731         /* FIX: IBSS mode */
2732         nlmode = params->p2p ?
2733                 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
2734         if (drv->nlmode != nlmode &&
2735             wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
2736                 return -1;
2737
2738 retry:
2739         wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
2740                    drv->ifindex);
2741
2742         msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
2743         if (!msg)
2744                 goto fail;
2745
2746         for (i = 0; i < 4; i++) {
2747                 if (!params->wep_key[i])
2748                         continue;
2749                 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
2750                                            NULL, i,
2751                                            i == params->wep_tx_keyidx, NULL, 0,
2752                                            params->wep_key[i],
2753                                            params->wep_key_len[i]);
2754                 if (params->wep_tx_keyidx != i)
2755                         continue;
2756                 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
2757                                params->wep_key[i], params->wep_key_len[i]))
2758                         goto fail;
2759         }
2760
2761         if (params->bssid) {
2762                 wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
2763                            MAC2STR(params->bssid));
2764                 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
2765                         goto fail;
2766         }
2767         if (params->freq) {
2768                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
2769                 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
2770                         goto fail;
2771         }
2772         if (params->ssid) {
2773                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
2774                                   params->ssid, params->ssid_len);
2775                 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
2776                             params->ssid))
2777                         goto fail;
2778         }
2779         wpa_hexdump(MSG_DEBUG, "  * IEs", params->ie, params->ie_len);
2780         if (params->ie &&
2781             nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
2782                 goto fail;
2783         if (params->sae_data) {
2784                 wpa_hexdump(MSG_DEBUG, "  * SAE data", params->sae_data,
2785                             params->sae_data_len);
2786                 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
2787                             params->sae_data))
2788                         goto fail;
2789         }
2790         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
2791                 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2792         else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
2793                 type = NL80211_AUTHTYPE_SHARED_KEY;
2794         else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
2795                 type = NL80211_AUTHTYPE_NETWORK_EAP;
2796         else if (params->auth_alg & WPA_AUTH_ALG_FT)
2797                 type = NL80211_AUTHTYPE_FT;
2798         else if (params->auth_alg & WPA_AUTH_ALG_SAE)
2799                 type = NL80211_AUTHTYPE_SAE;
2800         else
2801                 goto fail;
2802         wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
2803         if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
2804                 goto fail;
2805         if (params->local_state_change) {
2806                 wpa_printf(MSG_DEBUG, "  * Local state change only");
2807                 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
2808                         goto fail;
2809         }
2810
2811         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2812         msg = NULL;
2813         if (ret) {
2814                 wpa_dbg(drv->ctx, MSG_DEBUG,
2815                         "nl80211: MLME command failed (auth): ret=%d (%s)",
2816                         ret, strerror(-ret));
2817                 count++;
2818                 if (ret == -EALREADY && count == 1 && params->bssid &&
2819                     !params->local_state_change) {
2820                         /*
2821                          * mac80211 does not currently accept new
2822                          * authentication if we are already authenticated. As a
2823                          * workaround, force deauthentication and try again.
2824                          */
2825                         wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
2826                                    "after forced deauthentication");
2827                         drv->ignore_deauth_event = 1;
2828                         wpa_driver_nl80211_deauthenticate(
2829                                 bss, params->bssid,
2830                                 WLAN_REASON_PREV_AUTH_NOT_VALID);
2831                         nlmsg_free(msg);
2832                         goto retry;
2833                 }
2834
2835                 if (ret == -ENOENT && params->freq && !is_retry) {
2836                         /*
2837                          * cfg80211 has likely expired the BSS entry even
2838                          * though it was previously available in our internal
2839                          * BSS table. To recover quickly, start a single
2840                          * channel scan on the specified channel.
2841                          */
2842                         struct wpa_driver_scan_params scan;
2843                         int freqs[2];
2844
2845                         os_memset(&scan, 0, sizeof(scan));
2846                         scan.num_ssids = 1;
2847                         if (params->ssid) {
2848                                 scan.ssids[0].ssid = params->ssid;
2849                                 scan.ssids[0].ssid_len = params->ssid_len;
2850                         }
2851                         freqs[0] = params->freq;
2852                         freqs[1] = 0;
2853                         scan.freqs = freqs;
2854                         wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
2855                                    "channel scan to refresh cfg80211 BSS "
2856                                    "entry");
2857                         ret = wpa_driver_nl80211_scan(bss, &scan);
2858                         if (ret == 0) {
2859                                 nl80211_copy_auth_params(drv, params);
2860                                 drv->scan_for_auth = 1;
2861                         }
2862                 } else if (is_retry) {
2863                         /*
2864                          * Need to indicate this with an event since the return
2865                          * value from the retry is not delivered to core code.
2866                          */
2867                         union wpa_event_data event;
2868                         wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
2869                                    "failed");
2870                         os_memset(&event, 0, sizeof(event));
2871                         os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
2872                                   ETH_ALEN);
2873                         wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
2874                                              &event);
2875                 }
2876         } else {
2877                 wpa_printf(MSG_DEBUG,
2878                            "nl80211: Authentication request send successfully");
2879         }
2880
2881 fail:
2882         nlmsg_free(msg);
2883         return ret;
2884 }
2885
2886
2887 int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
2888 {
2889         struct wpa_driver_auth_params params;
2890         struct i802_bss *bss = drv->first_bss;
2891         int i;
2892
2893         wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
2894
2895         os_memset(&params, 0, sizeof(params));
2896         params.freq = drv->auth_freq;
2897         params.auth_alg = drv->auth_alg;
2898         params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
2899         params.local_state_change = drv->auth_local_state_change;
2900         params.p2p = drv->auth_p2p;
2901
2902         if (!is_zero_ether_addr(drv->auth_bssid_))
2903                 params.bssid = drv->auth_bssid_;
2904
2905         if (drv->auth_ssid_len) {
2906                 params.ssid = drv->auth_ssid;
2907                 params.ssid_len = drv->auth_ssid_len;
2908         }
2909
2910         params.ie = drv->auth_ie;
2911         params.ie_len = drv->auth_ie_len;
2912
2913         for (i = 0; i < 4; i++) {
2914                 if (drv->auth_wep_key_len[i]) {
2915                         params.wep_key[i] = drv->auth_wep_key[i];
2916                         params.wep_key_len[i] = drv->auth_wep_key_len[i];
2917                 }
2918         }
2919
2920         drv->retry_auth = 1;
2921         return wpa_driver_nl80211_authenticate(bss, &params);
2922 }
2923
2924
2925 static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
2926                                          const void *data, size_t len,
2927                                          int encrypt, int noack,
2928                                          unsigned int freq, int no_cck,
2929                                          int offchanok, unsigned int wait_time)
2930 {
2931         struct wpa_driver_nl80211_data *drv = bss->drv;
2932         u64 cookie;
2933         int res;
2934
2935         if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
2936                 freq = nl80211_get_assoc_freq(drv);
2937                 wpa_printf(MSG_DEBUG,
2938                            "nl80211: send_frame - Use assoc_freq=%u for IBSS",
2939                            freq);
2940         }
2941         if (freq == 0) {
2942                 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
2943                            bss->freq);
2944                 freq = bss->freq;
2945         }
2946
2947         if (drv->use_monitor) {
2948                 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
2949                            freq, bss->freq);
2950                 return nl80211_send_monitor(drv, data, len, encrypt, noack);
2951         }
2952
2953         wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
2954         res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
2955                                      &cookie, no_cck, noack, offchanok);
2956         if (res == 0 && !noack) {
2957                 const struct ieee80211_mgmt *mgmt;
2958                 u16 fc;
2959
2960                 mgmt = (const struct ieee80211_mgmt *) data;
2961                 fc = le_to_host16(mgmt->frame_control);
2962                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2963                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
2964                         wpa_printf(MSG_MSGDUMP,
2965                                    "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
2966                                    (long long unsigned int)
2967                                    drv->send_action_cookie,
2968                                    (long long unsigned int) cookie);
2969                         drv->send_action_cookie = cookie;
2970                 }
2971         }
2972
2973         return res;
2974 }
2975
2976
2977 static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
2978                                         size_t data_len, int noack,
2979                                         unsigned int freq, int no_cck,
2980                                         int offchanok,
2981                                         unsigned int wait_time)
2982 {
2983         struct wpa_driver_nl80211_data *drv = bss->drv;
2984         struct ieee80211_mgmt *mgmt;
2985         int encrypt = 1;
2986         u16 fc;
2987
2988         mgmt = (struct ieee80211_mgmt *) data;
2989         fc = le_to_host16(mgmt->frame_control);
2990         wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
2991                    " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
2992                    MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
2993                    fc, fc2str(fc), drv->nlmode);
2994
2995         if ((is_sta_interface(drv->nlmode) ||
2996              drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
2997             WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2998             WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
2999                 /*
3000                  * The use of last_mgmt_freq is a bit of a hack,
3001                  * but it works due to the single-threaded nature
3002                  * of wpa_supplicant.
3003                  */
3004                 if (freq == 0) {
3005                         wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3006                                    drv->last_mgmt_freq);
3007                         freq = drv->last_mgmt_freq;
3008                 }
3009                 return nl80211_send_frame_cmd(bss, freq, 0,
3010                                               data, data_len, NULL, 1, noack,
3011                                               1);
3012         }
3013
3014         if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
3015                 if (freq == 0) {
3016                         wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3017                                    bss->freq);
3018                         freq = bss->freq;
3019                 }
3020                 return nl80211_send_frame_cmd(bss, freq,
3021                                               (int) freq == bss->freq ? 0 :
3022                                               wait_time,
3023                                               data, data_len,
3024                                               &drv->send_action_cookie,
3025                                               no_cck, noack, offchanok);
3026         }
3027
3028         if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3029             WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3030                 /*
3031                  * Only one of the authentication frame types is encrypted.
3032                  * In order for static WEP encryption to work properly (i.e.,
3033                  * to not encrypt the frame), we need to tell mac80211 about
3034                  * the frames that must not be encrypted.
3035                  */
3036                 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3037                 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3038                 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3039                         encrypt = 0;
3040         }
3041
3042         wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
3043         return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
3044                                              noack, freq, no_cck, offchanok,
3045                                              wait_time);
3046 }
3047
3048
3049 static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3050                            int slot, int ht_opmode, int ap_isolate,
3051                            int *basic_rates)
3052 {
3053         struct wpa_driver_nl80211_data *drv = bss->drv;
3054         struct nl_msg *msg;
3055
3056         if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3057             (cts >= 0 &&
3058              nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3059             (preamble >= 0 &&
3060              nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3061             (slot >= 0 &&
3062              nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3063             (ht_opmode >= 0 &&
3064              nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3065             (ap_isolate >= 0 &&
3066              nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)))
3067                 goto fail;
3068
3069         if (basic_rates) {
3070                 u8 rates[NL80211_MAX_SUPP_RATES];
3071                 u8 rates_len = 0;
3072                 int i;
3073
3074                 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0;
3075                      i++)
3076                         rates[rates_len++] = basic_rates[i] / 5;
3077
3078                 if (nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len,
3079                             rates))
3080                         goto fail;
3081         }
3082
3083         return send_and_recv_msgs(drv, msg, NULL, NULL);
3084 fail:
3085         nlmsg_free(msg);
3086         return -ENOBUFS;
3087 }
3088
3089
3090 static int wpa_driver_nl80211_set_acl(void *priv,
3091                                       struct hostapd_acl_params *params)
3092 {
3093         struct i802_bss *bss = priv;
3094         struct wpa_driver_nl80211_data *drv = bss->drv;
3095         struct nl_msg *msg;
3096         struct nlattr *acl;
3097         unsigned int i;
3098         int ret;
3099
3100         if (!(drv->capa.max_acl_mac_addrs))
3101                 return -ENOTSUP;
3102
3103         if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3104                 return -ENOTSUP;
3105
3106         wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3107                    params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3108
3109         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
3110             nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3111                         NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3112                         NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3113             (acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS)) == NULL) {
3114                 nlmsg_free(msg);
3115                 return -ENOMEM;
3116         }
3117
3118         for (i = 0; i < params->num_mac_acl; i++) {
3119                 if (nla_put(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3120                         nlmsg_free(msg);
3121                         return -ENOMEM;
3122                 }
3123         }
3124
3125         nla_nest_end(msg, acl);
3126
3127         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3128         if (ret) {
3129                 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3130                            ret, strerror(-ret));
3131         }
3132
3133         return ret;
3134 }
3135
3136
3137 static int wpa_driver_nl80211_set_ap(void *priv,
3138                                      struct wpa_driver_ap_params *params)
3139 {
3140         struct i802_bss *bss = priv;
3141         struct wpa_driver_nl80211_data *drv = bss->drv;
3142         struct nl_msg *msg;
3143         u8 cmd = NL80211_CMD_NEW_BEACON;
3144         int ret;
3145         int beacon_set;
3146         int num_suites;
3147         int smps_mode;
3148         u32 suites[10], suite;
3149         u32 ver;
3150
3151         beacon_set = bss->beacon_set;
3152
3153         wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
3154                    beacon_set);
3155         if (beacon_set)
3156                 cmd = NL80211_CMD_SET_BEACON;
3157
3158         wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3159                     params->head, params->head_len);
3160         wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3161                     params->tail, params->tail_len);
3162         wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
3163         wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
3164         wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
3165         wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3166                           params->ssid, params->ssid_len);
3167         if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
3168             nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3169                     params->head) ||
3170             nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3171                     params->tail) ||
3172             nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3173                         params->beacon_int) ||
3174             nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period) ||
3175             nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3176                 goto fail;
3177         if (params->proberesp && params->proberesp_len) {
3178                 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3179                             params->proberesp, params->proberesp_len);
3180                 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3181                             params->proberesp))
3182                         goto fail;
3183         }
3184         switch (params->hide_ssid) {
3185         case NO_SSID_HIDING:
3186                 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
3187                 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3188                                 NL80211_HIDDEN_SSID_NOT_IN_USE))
3189                         goto fail;
3190                 break;
3191         case HIDDEN_SSID_ZERO_LEN:
3192                 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
3193                 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3194                                 NL80211_HIDDEN_SSID_ZERO_LEN))
3195                         goto fail;
3196                 break;
3197         case HIDDEN_SSID_ZERO_CONTENTS:
3198                 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
3199                 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3200                                 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3201                         goto fail;
3202                 break;
3203         }
3204         wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
3205         if (params->privacy &&
3206             nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3207                 goto fail;
3208         wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
3209         if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3210             (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3211                 /* Leave out the attribute */
3212         } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3213                 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3214                                 NL80211_AUTHTYPE_SHARED_KEY))
3215                         goto fail;
3216         } else {
3217                 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3218                                 NL80211_AUTHTYPE_OPEN_SYSTEM))
3219                         goto fail;
3220         }
3221
3222         wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
3223         ver = 0;
3224         if (params->wpa_version & WPA_PROTO_WPA)
3225                 ver |= NL80211_WPA_VERSION_1;
3226         if (params->wpa_version & WPA_PROTO_RSN)
3227                 ver |= NL80211_WPA_VERSION_2;
3228         if (ver &&
3229             nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3230                 goto fail;
3231
3232         wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3233                    params->key_mgmt_suites);
3234         num_suites = 0;
3235         if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
3236                 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
3237         if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
3238                 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
3239         if (num_suites &&
3240             nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
3241                     suites))
3242                 goto fail;
3243
3244         if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X &&
3245             params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40) &&
3246             nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
3247                 goto fail;
3248
3249         wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
3250                    params->pairwise_ciphers);
3251         num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
3252                                                  suites, ARRAY_SIZE(suites));
3253         if (num_suites &&
3254             nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
3255                     num_suites * sizeof(u32), suites))
3256                 goto fail;
3257
3258         wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
3259                    params->group_cipher);
3260         suite = wpa_cipher_to_cipher_suite(params->group_cipher);
3261         if (suite &&
3262             nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
3263                 goto fail;
3264
3265         switch (params->smps_mode) {
3266         case HT_CAP_INFO_SMPS_DYNAMIC:
3267                 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
3268                 smps_mode = NL80211_SMPS_DYNAMIC;
3269                 break;
3270         case HT_CAP_INFO_SMPS_STATIC:
3271                 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
3272                 smps_mode = NL80211_SMPS_STATIC;
3273                 break;
3274         default:
3275                 /* invalid - fallback to smps off */
3276         case HT_CAP_INFO_SMPS_DISABLED:
3277                 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
3278                 smps_mode = NL80211_SMPS_OFF;
3279                 break;
3280         }
3281         if (nla_put_u32(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
3282                 goto fail;
3283
3284         if (params->beacon_ies) {
3285                 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
3286                                 params->beacon_ies);
3287                 if (nla_put(msg, NL80211_ATTR_IE,
3288                             wpabuf_len(params->beacon_ies),
3289                             wpabuf_head(params->beacon_ies)))
3290                         goto fail;
3291         }
3292         if (params->proberesp_ies) {
3293                 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
3294                                 params->proberesp_ies);
3295                 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
3296                             wpabuf_len(params->proberesp_ies),
3297                             wpabuf_head(params->proberesp_ies)))
3298                         goto fail;
3299         }
3300         if (params->assocresp_ies) {
3301                 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
3302                                 params->assocresp_ies);
3303                 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
3304                             wpabuf_len(params->assocresp_ies),
3305                             wpabuf_head(params->assocresp_ies)))
3306                         goto fail;
3307         }
3308
3309         if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)  {
3310                 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
3311                            params->ap_max_inactivity);
3312                 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
3313                                 params->ap_max_inactivity))
3314                         goto fail;
3315         }
3316
3317         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3318         if (ret) {
3319                 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
3320                            ret, strerror(-ret));
3321         } else {
3322                 bss->beacon_set = 1;
3323                 nl80211_set_bss(bss, params->cts_protect, params->preamble,
3324                                 params->short_slot_time, params->ht_opmode,
3325                                 params->isolate, params->basic_rates);
3326                 if (beacon_set && params->freq &&
3327                     params->freq->bandwidth != bss->bandwidth) {
3328                         wpa_printf(MSG_DEBUG,
3329                                    "nl80211: Update BSS %s bandwidth: %d -> %d",
3330                                    bss->ifname, bss->bandwidth,
3331                                    params->freq->bandwidth);
3332                         ret = nl80211_set_channel(bss, params->freq, 1);
3333                         if (ret) {
3334                                 wpa_printf(MSG_DEBUG,
3335                                            "nl80211: Frequency set failed: %d (%s)",
3336                                            ret, strerror(-ret));
3337                         } else {
3338                                 wpa_printf(MSG_DEBUG,
3339                                            "nl80211: Frequency set succeeded for ht2040 coex");
3340                                 bss->bandwidth = params->freq->bandwidth;
3341                         }
3342                 } else if (!beacon_set) {
3343                         /*
3344                          * cfg80211 updates the driver on frequence change in AP
3345                          * mode only at the point when beaconing is started, so
3346                          * set the initial value here.
3347                          */
3348                         bss->bandwidth = params->freq->bandwidth;
3349                 }
3350         }
3351         return ret;
3352 fail:
3353         nlmsg_free(msg);
3354         return -ENOBUFS;
3355 }
3356
3357
3358 static int nl80211_put_freq_params(struct nl_msg *msg,
3359                                    struct hostapd_freq_params *freq)
3360 {
3361         if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
3362                 return -ENOBUFS;
3363
3364         if (freq->vht_enabled) {
3365                 enum nl80211_chan_width cw;
3366
3367                 switch (freq->bandwidth) {
3368                 case 20:
3369                         cw = NL80211_CHAN_WIDTH_20;
3370                         break;
3371                 case 40:
3372                         cw = NL80211_CHAN_WIDTH_40;
3373                         break;
3374                 case 80:
3375                         if (freq->center_freq2)
3376                                 cw = NL80211_CHAN_WIDTH_80P80;
3377                         else
3378                                 cw = NL80211_CHAN_WIDTH_80;
3379                         break;
3380                 case 160:
3381                         cw = NL80211_CHAN_WIDTH_160;
3382                         break;
3383                 default:
3384                         return -EINVAL;
3385                 }
3386
3387                 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
3388                     nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
3389                                 freq->center_freq1) ||
3390                     (freq->center_freq2 &&
3391                      nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
3392                                  freq->center_freq2)))
3393                         return -ENOBUFS;
3394         } else if (freq->ht_enabled) {
3395                 enum nl80211_channel_type ct;
3396
3397                 switch (freq->sec_channel_offset) {
3398                 case -1:
3399                         ct = NL80211_CHAN_HT40MINUS;
3400                         break;
3401                 case 1:
3402                         ct = NL80211_CHAN_HT40PLUS;
3403                         break;
3404                 default:
3405                         ct = NL80211_CHAN_HT20;
3406                         break;
3407                 }
3408
3409                 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
3410                         return -ENOBUFS;
3411         }
3412         return 0;
3413 }
3414
3415
3416 static int nl80211_set_channel(struct i802_bss *bss,
3417                                struct hostapd_freq_params *freq, int set_chan)
3418 {
3419         struct wpa_driver_nl80211_data *drv = bss->drv;
3420         struct nl_msg *msg;
3421         int ret;
3422
3423         wpa_printf(MSG_DEBUG,
3424                    "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
3425                    freq->freq, freq->ht_enabled, freq->vht_enabled,
3426                    freq->bandwidth, freq->center_freq1, freq->center_freq2);
3427
3428         msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
3429                               NL80211_CMD_SET_WIPHY);
3430         if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
3431                 nlmsg_free(msg);
3432                 return -1;
3433         }
3434
3435         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3436         if (ret == 0) {
3437                 bss->freq = freq->freq;
3438                 return 0;
3439         }
3440         wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
3441                    "%d (%s)", freq->freq, ret, strerror(-ret));
3442         return -1;
3443 }
3444
3445
3446 static u32 sta_flags_nl80211(int flags)
3447 {
3448         u32 f = 0;
3449
3450         if (flags & WPA_STA_AUTHORIZED)
3451                 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
3452         if (flags & WPA_STA_WMM)
3453                 f |= BIT(NL80211_STA_FLAG_WME);
3454         if (flags & WPA_STA_SHORT_PREAMBLE)
3455                 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
3456         if (flags & WPA_STA_MFP)
3457                 f |= BIT(NL80211_STA_FLAG_MFP);
3458         if (flags & WPA_STA_TDLS_PEER)
3459                 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
3460         if (flags & WPA_STA_AUTHENTICATED)
3461                 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
3462
3463         return f;
3464 }
3465
3466
3467 #ifdef CONFIG_MESH
3468 static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
3469 {
3470         switch (state) {
3471         case PLINK_LISTEN:
3472                 return NL80211_PLINK_LISTEN;
3473         case PLINK_OPEN_SENT:
3474                 return NL80211_PLINK_OPN_SNT;
3475         case PLINK_OPEN_RCVD:
3476                 return NL80211_PLINK_OPN_RCVD;
3477         case PLINK_CNF_RCVD:
3478                 return NL80211_PLINK_CNF_RCVD;
3479         case PLINK_ESTAB:
3480                 return NL80211_PLINK_ESTAB;
3481         case PLINK_HOLDING:
3482                 return NL80211_PLINK_HOLDING;
3483         case PLINK_BLOCKED:
3484                 return NL80211_PLINK_BLOCKED;
3485         default:
3486                 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
3487                            state);
3488         }
3489         return -1;
3490 }
3491 #endif /* CONFIG_MESH */
3492
3493
3494 static int wpa_driver_nl80211_sta_add(void *priv,
3495                                       struct hostapd_sta_add_params *params)
3496 {
3497         struct i802_bss *bss = priv;
3498         struct wpa_driver_nl80211_data *drv = bss->drv;
3499         struct nl_msg *msg;
3500         struct nl80211_sta_flag_update upd;
3501         int ret = -ENOBUFS;
3502
3503         if ((params->flags & WPA_STA_TDLS_PEER) &&
3504             !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
3505                 return -EOPNOTSUPP;
3506
3507         wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
3508                    params->set ? "Set" : "Add", MAC2STR(params->addr));
3509         msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
3510                               NL80211_CMD_NEW_STATION);
3511         if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
3512                 goto fail;
3513
3514         if (!params->set || (params->flags & WPA_STA_TDLS_PEER)) {
3515                 wpa_hexdump(MSG_DEBUG, "  * supported rates",
3516                             params->supp_rates, params->supp_rates_len);
3517                 wpa_printf(MSG_DEBUG, "  * capability=0x%x",
3518                            params->capability);
3519                 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
3520                             params->supp_rates_len, params->supp_rates) ||
3521                     nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
3522                                 params->capability))
3523                         goto fail;
3524
3525                 if (params->ht_capabilities) {
3526                         wpa_hexdump(MSG_DEBUG, "  * ht_capabilities",
3527                                     (u8 *) params->ht_capabilities,
3528                                     sizeof(*params->ht_capabilities));
3529                         if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
3530                                     sizeof(*params->ht_capabilities),
3531                                     params->ht_capabilities))
3532                                 goto fail;
3533                 }
3534
3535                 if (params->vht_capabilities) {
3536                         wpa_hexdump(MSG_DEBUG, "  * vht_capabilities",
3537                                     (u8 *) params->vht_capabilities,
3538                                     sizeof(*params->vht_capabilities));
3539                         if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
3540                                     sizeof(*params->vht_capabilities),
3541                                     params->vht_capabilities))
3542                                 goto fail;
3543                 }
3544
3545                 if (params->ext_capab) {
3546                         wpa_hexdump(MSG_DEBUG, "  * ext_capab",
3547                                     params->ext_capab, params->ext_capab_len);
3548                         if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
3549                                     params->ext_capab_len, params->ext_capab))
3550                                 goto fail;
3551                 }
3552         }
3553         if (!params->set) {
3554                 if (params->aid) {
3555                         wpa_printf(MSG_DEBUG, "  * aid=%u", params->aid);
3556                         if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
3557                                 goto fail;
3558                 } else {
3559                         /*
3560                          * cfg80211 validates that AID is non-zero, so we have
3561                          * to make this a non-zero value for the TDLS case where
3562                          * a dummy STA entry is used for now.
3563                          */
3564                         wpa_printf(MSG_DEBUG, "  * aid=1 (TDLS workaround)");
3565                         if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
3566                                 goto fail;
3567                 }
3568                 wpa_printf(MSG_DEBUG, "  * listen_interval=%u",
3569                            params->listen_interval);
3570                 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
3571                                 params->listen_interval))
3572                         goto fail;
3573         } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
3574                 wpa_printf(MSG_DEBUG, "  * peer_aid=%u", params->aid);
3575                 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
3576                         goto fail;
3577         }
3578
3579         if (params->vht_opmode_enabled) {
3580                 wpa_printf(MSG_DEBUG, "  * opmode=%u", params->vht_opmode);
3581                 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
3582                                params->vht_opmode))
3583                         goto fail;
3584         }
3585
3586         if (params->supp_channels) {
3587                 wpa_hexdump(MSG_DEBUG, "  * supported channels",
3588                             params->supp_channels, params->supp_channels_len);
3589                 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
3590                             params->supp_channels_len, params->supp_channels))
3591                         goto fail;
3592         }
3593
3594         if (params->supp_oper_classes) {
3595                 wpa_hexdump(MSG_DEBUG, "  * supported operating classes",
3596                             params->supp_oper_classes,
3597                             params->supp_oper_classes_len);
3598                 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
3599                             params->supp_oper_classes_len,
3600                             params->supp_oper_classes))
3601                         goto fail;
3602         }
3603
3604         os_memset(&upd, 0, sizeof(upd));
3605         upd.set = sta_flags_nl80211(params->flags);
3606         upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
3607         wpa_printf(MSG_DEBUG, "  * flags set=0x%x mask=0x%x",
3608                    upd.set, upd.mask);
3609         if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
3610                 goto fail;
3611
3612 #ifdef CONFIG_MESH
3613         if (params->plink_state &&
3614             nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
3615                        sta_plink_state_nl80211(params->plink_state)))
3616                 goto fail;
3617 #endif /* CONFIG_MESH */
3618
3619         if (params->flags & WPA_STA_WMM) {
3620                 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
3621
3622                 wpa_printf(MSG_DEBUG, "  * qosinfo=0x%x", params->qosinfo);
3623                 if (!wme ||
3624                     nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
3625                                params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
3626                     nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
3627                                (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
3628                                WMM_QOSINFO_STA_SP_MASK))
3629                         goto fail;
3630                 nla_nest_end(msg, wme);
3631         }
3632
3633         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3634         msg = NULL;
3635         if (ret)
3636                 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
3637                            "result: %d (%s)", params->set ? "SET" : "NEW", ret,
3638                            strerror(-ret));
3639         if (ret == -EEXIST)
3640                 ret = 0;
3641 fail:
3642         nlmsg_free(msg);
3643         return ret;
3644 }
3645
3646
3647 static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
3648 {
3649 #ifdef CONFIG_LIBNL3_ROUTE
3650         struct wpa_driver_nl80211_data *drv = bss->drv;
3651         struct rtnl_neigh *rn;
3652         struct nl_addr *nl_addr;
3653         int err;
3654
3655         rn = rtnl_neigh_alloc();
3656         if (!rn)
3657                 return;
3658
3659         rtnl_neigh_set_family(rn, AF_BRIDGE);
3660         rtnl_neigh_set_ifindex(rn, bss->ifindex);
3661         nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
3662         if (!nl_addr) {
3663                 rtnl_neigh_put(rn);
3664                 return;
3665         }
3666         rtnl_neigh_set_lladdr(rn, nl_addr);
3667
3668         err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
3669         if (err < 0) {
3670                 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
3671                            MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
3672                            bss->ifindex, nl_geterror(err));
3673         } else {
3674                 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
3675                            MACSTR, MAC2STR(addr));
3676         }
3677
3678         nl_addr_put(nl_addr);
3679         rtnl_neigh_put(rn);
3680 #endif /* CONFIG_LIBNL3_ROUTE */
3681 }
3682
3683
3684 static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
3685                                          int deauth, u16 reason_code)
3686 {
3687         struct wpa_driver_nl80211_data *drv = bss->drv;
3688         struct nl_msg *msg;
3689         int ret;
3690
3691         if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
3692             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
3693             (deauth == 0 &&
3694              nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
3695                         WLAN_FC_STYPE_DISASSOC)) ||
3696             (deauth == 1 &&
3697              nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
3698                         WLAN_FC_STYPE_DEAUTH)) ||
3699             (reason_code &&
3700              nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
3701                 nlmsg_free(msg);
3702                 return -ENOBUFS;
3703         }
3704
3705         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3706         wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
3707                    " --> %d (%s)",
3708                    bss->ifname, MAC2STR(addr), ret, strerror(-ret));
3709
3710         if (drv->rtnl_sk)
3711                 rtnl_neigh_delete_fdb_entry(bss, addr);
3712
3713         if (ret == -ENOENT)
3714                 return 0;
3715         return ret;
3716 }
3717
3718
3719 void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
3720 {
3721         struct nl_msg *msg;
3722         struct wpa_driver_nl80211_data *drv2;
3723
3724         wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
3725
3726         /* stop listening for EAPOL on this interface */
3727         dl_list_for_each(drv2, &drv->global->interfaces,
3728                          struct wpa_driver_nl80211_data, list)
3729                 del_ifidx(drv2, ifidx);
3730
3731         msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
3732         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
3733                 return;
3734         wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
3735 }
3736
3737
3738 static const char * nl80211_iftype_str(enum nl80211_iftype mode)
3739 {
3740         switch (mode) {
3741         case NL80211_IFTYPE_ADHOC:
3742                 return "ADHOC";
3743         case NL80211_IFTYPE_STATION:
3744                 return "STATION";
3745         case NL80211_IFTYPE_AP:
3746                 return "AP";
3747         case NL80211_IFTYPE_AP_VLAN:
3748                 return "AP_VLAN";
3749         case NL80211_IFTYPE_WDS:
3750                 return "WDS";
3751         case NL80211_IFTYPE_MONITOR:
3752                 return "MONITOR";
3753         case NL80211_IFTYPE_MESH_POINT:
3754                 return "MESH_POINT";
3755         case NL80211_IFTYPE_P2P_CLIENT:
3756                 return "P2P_CLIENT";
3757         case NL80211_IFTYPE_P2P_GO:
3758                 return "P2P_GO";
3759         case NL80211_IFTYPE_P2P_DEVICE:
3760                 return "P2P_DEVICE";
3761         default:
3762                 return "unknown";
3763         }
3764 }
3765
3766
3767 static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
3768                                      const char *ifname,
3769                                      enum nl80211_iftype iftype,
3770                                      const u8 *addr, int wds,
3771                                      int (*handler)(struct nl_msg *, void *),
3772                                      void *arg)
3773 {
3774         struct nl_msg *msg;
3775         int ifidx;
3776         int ret = -ENOBUFS;
3777
3778         wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
3779                    iftype, nl80211_iftype_str(iftype));
3780
3781         msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
3782         if (!msg ||
3783             nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
3784             nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
3785                 goto fail;
3786
3787         if (iftype == NL80211_IFTYPE_MONITOR) {
3788                 struct nlattr *flags;
3789
3790                 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
3791                 if (!flags ||
3792                     nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
3793                         goto fail;
3794
3795                 nla_nest_end(msg, flags);
3796         } else if (wds) {
3797                 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
3798                         goto fail;
3799         }
3800
3801         /*
3802          * Tell cfg80211 that the interface belongs to the socket that created
3803          * it, and the interface should be deleted when the socket is closed.
3804          */
3805         if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
3806                 goto fail;
3807
3808         ret = send_and_recv_msgs(drv, msg, handler, arg);
3809         msg = NULL;
3810         if (ret) {
3811         fail:
3812                 nlmsg_free(msg);
3813                 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
3814                            ifname, ret, strerror(-ret));
3815                 return ret;
3816         }
3817
3818         if (iftype == NL80211_IFTYPE_P2P_DEVICE)
3819                 return 0;
3820
3821         ifidx = if_nametoindex(ifname);
3822         wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
3823                    ifname, ifidx);
3824
3825         if (ifidx <= 0)
3826                 return -1;
3827
3828         /*
3829          * Some virtual interfaces need to process EAPOL packets and events on
3830          * the parent interface. This is used mainly with hostapd.
3831          */
3832         if (drv->hostapd ||
3833             iftype == NL80211_IFTYPE_AP_VLAN ||
3834             iftype == NL80211_IFTYPE_WDS ||
3835             iftype == NL80211_IFTYPE_MONITOR) {
3836                 /* start listening for EAPOL on this interface */
3837                 add_ifidx(drv, ifidx);
3838         }
3839
3840         if (addr && iftype != NL80211_IFTYPE_MONITOR &&
3841             linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
3842                 nl80211_remove_iface(drv, ifidx);
3843                 return -1;
3844         }
3845
3846         return ifidx;
3847 }
3848
3849
3850 int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
3851                          const char *ifname, enum nl80211_iftype iftype,
3852                          const u8 *addr, int wds,
3853                          int (*handler)(struct nl_msg *, void *),
3854                          void *arg, int use_existing)
3855 {
3856         int ret;
3857
3858         ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
3859                                         arg);
3860
3861         /* if error occurred and interface exists already */
3862         if (ret == -ENFILE && if_nametoindex(ifname)) {
3863                 if (use_existing) {
3864                         wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
3865                                    ifname);
3866                         if (addr && iftype != NL80211_IFTYPE_MONITOR &&
3867                             linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
3868                                                addr) < 0 &&
3869                             (linux_set_iface_flags(drv->global->ioctl_sock,
3870                                                    ifname, 0) < 0 ||
3871                              linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
3872                                                 addr) < 0 ||
3873                              linux_set_iface_flags(drv->global->ioctl_sock,
3874                                                    ifname, 1) < 0))
3875                                         return -1;
3876                         return -ENFILE;
3877                 }
3878                 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
3879
3880                 /* Try to remove the interface that was already there. */
3881                 nl80211_remove_iface(drv, if_nametoindex(ifname));
3882
3883                 /* Try to create the interface again */
3884                 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
3885                                                 wds, handler, arg);
3886         }
3887
3888         if (ret >= 0 && is_p2p_net_interface(iftype)) {
3889                 wpa_printf(MSG_DEBUG,
3890                            "nl80211: Interface %s created for P2P - disable 11b rates",
3891                            ifname);
3892                 nl80211_disable_11b_rates(drv, ret, 1);
3893         }
3894
3895         return ret;
3896 }
3897
3898
3899 static int nl80211_setup_ap(struct i802_bss *bss)
3900 {
3901         struct wpa_driver_nl80211_data *drv = bss->drv;
3902
3903         wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
3904                    bss->ifname, drv->device_ap_sme, drv->use_monitor);
3905
3906         /*
3907          * Disable Probe Request reporting unless we need it in this way for
3908          * devices that include the AP SME, in the other case (unless using
3909          * monitor iface) we'll get it through the nl_mgmt socket instead.
3910          */
3911         if (!drv->device_ap_sme)
3912                 wpa_driver_nl80211_probe_req_report(bss, 0);
3913
3914         if (!drv->device_ap_sme && !drv->use_monitor)
3915                 if (nl80211_mgmt_subscribe_ap(bss))
3916                         return -1;
3917
3918         if (drv->device_ap_sme && !drv->use_monitor)
3919                 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
3920                         return -1;
3921
3922         if (!drv->device_ap_sme && drv->use_monitor &&
3923             nl80211_create_monitor_interface(drv) &&
3924             !drv->device_ap_sme)
3925                 return -1;
3926
3927         if (drv->device_ap_sme &&
3928             wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
3929                 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
3930                            "Probe Request frame reporting in AP mode");
3931                 /* Try to survive without this */
3932         }
3933
3934         return 0;
3935 }
3936
3937
3938 static void nl80211_teardown_ap(struct i802_bss *bss)
3939 {
3940         struct wpa_driver_nl80211_data *drv = bss->drv;
3941
3942         wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
3943                    bss->ifname, drv->device_ap_sme, drv->use_monitor);
3944         if (drv->device_ap_sme) {
3945                 wpa_driver_nl80211_probe_req_report(bss, 0);
3946                 if (!drv->use_monitor)
3947                         nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
3948         } else if (drv->use_monitor)
3949                 nl80211_remove_monitor_interface(drv);
3950         else
3951                 nl80211_mgmt_unsubscribe(bss, "AP teardown");
3952
3953         bss->beacon_set = 0;
3954 }
3955
3956
3957 static int nl80211_send_eapol_data(struct i802_bss *bss,
3958                                    const u8 *addr, const u8 *data,
3959                                    size_t data_len)
3960 {
3961         struct sockaddr_ll ll;
3962         int ret;
3963
3964         if (bss->drv->eapol_tx_sock < 0) {
3965                 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
3966                 return -1;
3967         }
3968
3969         os_memset(&ll, 0, sizeof(ll));
3970         ll.sll_family = AF_PACKET;
3971         ll.sll_ifindex = bss->ifindex;
3972         ll.sll_protocol = htons(ETH_P_PAE);
3973         ll.sll_halen = ETH_ALEN;
3974         os_memcpy(ll.sll_addr, addr, ETH_ALEN);
3975         ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
3976                      (struct sockaddr *) &ll, sizeof(ll));
3977         if (ret < 0)
3978                 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
3979                            strerror(errno));
3980
3981         return ret;
3982 }
3983
3984
3985 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
3986
3987 static int wpa_driver_nl80211_hapd_send_eapol(
3988         void *priv, const u8 *addr, const u8 *data,
3989         size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
3990 {
3991         struct i802_bss *bss = priv;
3992         struct wpa_driver_nl80211_data *drv = bss->drv;
3993         struct ieee80211_hdr *hdr;
3994         size_t len;
3995         u8 *pos;
3996         int res;
3997         int qos = flags & WPA_STA_WMM;
3998
3999         if (drv->device_ap_sme || !drv->use_monitor)
4000                 return nl80211_send_eapol_data(bss, addr, data, data_len);
4001
4002         len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4003                 data_len;
4004         hdr = os_zalloc(len);
4005         if (hdr == NULL) {
4006                 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4007                            (unsigned long) len);
4008                 return -1;
4009         }
4010
4011         hdr->frame_control =
4012                 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4013         hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4014         if (encrypt)
4015                 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4016         if (qos) {
4017                 hdr->frame_control |=
4018                         host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4019         }
4020
4021         memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4022         memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4023         memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4024         pos = (u8 *) (hdr + 1);
4025
4026         if (qos) {
4027                 /* Set highest priority in QoS header */
4028                 pos[0] = 7;
4029                 pos[1] = 0;
4030                 pos += 2;
4031         }
4032
4033         memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4034         pos += sizeof(rfc1042_header);
4035         WPA_PUT_BE16(pos, ETH_P_PAE);
4036         pos += 2;
4037         memcpy(pos, data, data_len);
4038
4039         res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
4040                                             0, 0, 0, 0);
4041         if (res < 0) {
4042                 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4043                            "failed: %d (%s)",
4044                            (unsigned long) len, errno, strerror(errno));
4045         }
4046         os_free(hdr);
4047
4048         return res;
4049 }
4050
4051
4052 static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
4053                                             int total_flags,
4054                                             int flags_or, int flags_and)
4055 {
4056         struct i802_bss *bss = priv;
4057         struct nl_msg *msg;
4058         struct nlattr *flags;
4059         struct nl80211_sta_flag_update upd;
4060
4061         wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4062                    " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4063                    bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4064                    !!(total_flags & WPA_STA_AUTHORIZED));
4065
4066         if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4067             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4068                 goto fail;
4069
4070         /*
4071          * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4072          * can be removed eventually.
4073          */
4074         flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
4075         if (!flags ||
4076             ((total_flags & WPA_STA_AUTHORIZED) &&
4077              nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4078             ((total_flags & WPA_STA_WMM) &&
4079              nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4080             ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4081              nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4082             ((total_flags & WPA_STA_MFP) &&
4083              nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4084             ((total_flags & WPA_STA_TDLS_PEER) &&
4085              nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4086                 goto fail;
4087
4088         nla_nest_end(msg, flags);
4089
4090         os_memset(&upd, 0, sizeof(upd));
4091         upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4092         upd.set = sta_flags_nl80211(flags_or);
4093         if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4094                 goto fail;
4095
4096         return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
4097 fail:
4098         nlmsg_free(msg);
4099         return -ENOBUFS;
4100 }
4101
4102
4103 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4104                                  struct wpa_driver_associate_params *params)
4105 {
4106         enum nl80211_iftype nlmode, old_mode;
4107
4108         if (params->p2p) {
4109                 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4110                            "group (GO)");
4111                 nlmode = NL80211_IFTYPE_P2P_GO;
4112         } else
4113                 nlmode = NL80211_IFTYPE_AP;
4114
4115         old_mode = drv->nlmode;
4116         if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
4117                 nl80211_remove_monitor_interface(drv);
4118                 return -1;
4119         }
4120
4121         if (nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
4122                 if (old_mode != nlmode)
4123                         wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
4124                 nl80211_remove_monitor_interface(drv);
4125                 return -1;
4126         }
4127
4128         return 0;
4129 }
4130
4131
4132 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
4133 {
4134         struct nl_msg *msg;
4135         int ret;
4136
4137         msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
4138         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4139         if (ret) {
4140                 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
4141                            "(%s)", ret, strerror(-ret));
4142         } else {
4143                 wpa_printf(MSG_DEBUG,
4144                            "nl80211: Leave IBSS request sent successfully");
4145         }
4146
4147         if (wpa_driver_nl80211_set_mode(drv->first_bss,
4148                                         NL80211_IFTYPE_STATION)) {
4149                 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4150                            "station mode");
4151         }
4152
4153         return ret;
4154 }
4155
4156
4157 static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
4158                                    struct wpa_driver_associate_params *params)
4159 {
4160         struct nl_msg *msg;
4161         int ret = -1;
4162         int count = 0;
4163
4164         wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
4165
4166         if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
4167                 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4168                            "IBSS mode");
4169                 return -1;
4170         }
4171
4172 retry:
4173         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
4174             params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
4175                 goto fail;
4176
4177         wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
4178                           params->ssid, params->ssid_len);
4179         if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4180                 goto fail;
4181         os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4182         drv->ssid_len = params->ssid_len;
4183
4184         wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq.freq);
4185         wpa_printf(MSG_DEBUG, "  * ht_enabled=%d", params->freq.ht_enabled);
4186         wpa_printf(MSG_DEBUG, "  * sec_channel_offset=%d",
4187                    params->freq.sec_channel_offset);
4188         wpa_printf(MSG_DEBUG, "  * vht_enabled=%d", params->freq.vht_enabled);
4189         wpa_printf(MSG_DEBUG, "  * center_freq1=%d", params->freq.center_freq1);
4190         wpa_printf(MSG_DEBUG, "  * center_freq2=%d", params->freq.center_freq2);
4191         wpa_printf(MSG_DEBUG, "  * bandwidth=%d", params->freq.bandwidth);
4192         if (nl80211_put_freq_params(msg, &params->freq) < 0)
4193                 goto fail;
4194
4195         if (params->beacon_int > 0) {
4196                 wpa_printf(MSG_DEBUG, "  * beacon_int=%d", params->beacon_int);
4197                 if (nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
4198                                 params->beacon_int))
4199                         goto fail;
4200         }
4201
4202         ret = nl80211_set_conn_keys(params, msg);
4203         if (ret)
4204                 goto fail;
4205
4206         if (params->bssid && params->fixed_bssid) {
4207                 wpa_printf(MSG_DEBUG, "  * BSSID=" MACSTR,
4208                            MAC2STR(params->bssid));
4209                 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4210                         goto fail;
4211         }
4212
4213         if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4214             params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4215             params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
4216             params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
4217                 wpa_printf(MSG_DEBUG, "  * control port");
4218                 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4219                         goto fail;
4220         }
4221
4222         if (params->wpa_ie) {
4223                 wpa_hexdump(MSG_DEBUG,
4224                             "  * Extra IEs for Beacon/Probe Response frames",
4225                             params->wpa_ie, params->wpa_ie_len);
4226                 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
4227                             params->wpa_ie))
4228                         goto fail;
4229         }
4230
4231         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4232         msg = NULL;
4233         if (ret) {
4234                 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
4235                            ret, strerror(-ret));
4236                 count++;
4237                 if (ret == -EALREADY && count == 1) {
4238                         wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
4239                                    "forced leave");
4240                         nl80211_leave_ibss(drv);
4241                         nlmsg_free(msg);
4242                         goto retry;
4243                 }
4244         } else {
4245                 wpa_printf(MSG_DEBUG,
4246                            "nl80211: Join IBSS request sent successfully");
4247         }
4248
4249 fail:
4250         nlmsg_free(msg);
4251         return ret;
4252 }
4253
4254
4255 static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
4256                                   struct wpa_driver_associate_params *params,
4257                                   struct nl_msg *msg)
4258 {
4259         if (params->bssid) {
4260                 wpa_printf(MSG_DEBUG, "  * bssid=" MACSTR,
4261                            MAC2STR(params->bssid));
4262                 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4263                         return -1;
4264         }
4265
4266         if (params->bssid_hint) {
4267                 wpa_printf(MSG_DEBUG, "  * bssid_hint=" MACSTR,
4268                            MAC2STR(params->bssid_hint));
4269                 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
4270                             params->bssid_hint))
4271                         return -1;
4272         }
4273
4274         if (params->freq.freq) {
4275                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq.freq);
4276                 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
4277                                 params->freq.freq))
4278                         return -1;
4279                 drv->assoc_freq = params->freq.freq;
4280         } else
4281                 drv->assoc_freq = 0;
4282
4283         if (params->freq_hint) {
4284                 wpa_printf(MSG_DEBUG, "  * freq_hint=%d", params->freq_hint);
4285                 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
4286                                 params->freq_hint))
4287                         return -1;
4288         }
4289
4290         if (params->bg_scan_period >= 0) {
4291                 wpa_printf(MSG_DEBUG, "  * bg scan period=%d",
4292                            params->bg_scan_period);
4293                 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
4294                                 params->bg_scan_period))
4295                         return -1;
4296         }
4297
4298         if (params->ssid) {
4299                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
4300                                   params->ssid, params->ssid_len);
4301                 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
4302                             params->ssid))
4303                         return -1;
4304                 if (params->ssid_len > sizeof(drv->ssid))
4305                         return -1;
4306                 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4307                 drv->ssid_len = params->ssid_len;
4308         }
4309
4310         wpa_hexdump(MSG_DEBUG, "  * IEs", params->wpa_ie, params->wpa_ie_len);
4311         if (params->wpa_ie &&
4312             nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
4313                 return -1;
4314
4315         if (params->wpa_proto) {
4316                 enum nl80211_wpa_versions ver = 0;
4317
4318                 if (params->wpa_proto & WPA_PROTO_WPA)
4319                         ver |= NL80211_WPA_VERSION_1;
4320                 if (params->wpa_proto & WPA_PROTO_RSN)
4321                         ver |= NL80211_WPA_VERSION_2;
4322
4323                 wpa_printf(MSG_DEBUG, "  * WPA Versions 0x%x", ver);
4324                 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
4325                         return -1;
4326         }
4327
4328         if (params->pairwise_suite != WPA_CIPHER_NONE) {
4329                 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
4330                 wpa_printf(MSG_DEBUG, "  * pairwise=0x%x", cipher);
4331                 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4332                                 cipher))
4333                         return -1;
4334         }
4335
4336         if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
4337             !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
4338                 /*
4339                  * This is likely to work even though many drivers do not
4340                  * advertise support for operations without GTK.
4341                  */
4342                 wpa_printf(MSG_DEBUG, "  * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
4343         } else if (params->group_suite != WPA_CIPHER_NONE) {
4344                 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
4345                 wpa_printf(MSG_DEBUG, "  * group=0x%x", cipher);
4346                 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
4347                         return -1;
4348         }
4349
4350         if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4351             params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4352             params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
4353             params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
4354             params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
4355             params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
4356             params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
4357             params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
4358             params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
4359                 int mgmt = WLAN_AKM_SUITE_PSK;
4360
4361                 switch (params->key_mgmt_suite) {
4362                 case WPA_KEY_MGMT_CCKM:
4363                         mgmt = WLAN_AKM_SUITE_CCKM;
4364                         break;
4365                 case WPA_KEY_MGMT_IEEE8021X:
4366                         mgmt = WLAN_AKM_SUITE_8021X;
4367                         break;
4368                 case WPA_KEY_MGMT_FT_IEEE8021X:
4369                         mgmt = WLAN_AKM_SUITE_FT_8021X;
4370                         break;
4371                 case WPA_KEY_MGMT_FT_PSK:
4372                         mgmt = WLAN_AKM_SUITE_FT_PSK;
4373                         break;
4374                 case WPA_KEY_MGMT_IEEE8021X_SHA256:
4375                         mgmt = WLAN_AKM_SUITE_8021X_SHA256;
4376                         break;
4377                 case WPA_KEY_MGMT_PSK_SHA256:
4378                         mgmt = WLAN_AKM_SUITE_PSK_SHA256;
4379                         break;
4380                 case WPA_KEY_MGMT_OSEN:
4381                         mgmt = WLAN_AKM_SUITE_OSEN;
4382                         break;
4383                 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
4384                         mgmt = WLAN_AKM_SUITE_8021X_SUITE_B;
4385                         break;
4386                 case WPA_KEY_MGMT_PSK:
4387                 default:
4388                         mgmt = WLAN_AKM_SUITE_PSK;
4389                         break;
4390                 }
4391                 wpa_printf(MSG_DEBUG, "  * akm=0x%x", mgmt);
4392                 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
4393                         return -1;
4394         }
4395
4396         if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4397                 return -1;
4398
4399         if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
4400             nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
4401                 return -1;
4402
4403         if (params->rrm_used) {
4404                 u32 drv_rrm_flags = drv->capa.rrm_flags;
4405                 if (!(drv_rrm_flags &
4406                       WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) ||
4407                     !(drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET) ||
4408                     nla_put_flag(msg, NL80211_ATTR_USE_RRM))
4409                         return -1;
4410         }
4411
4412         if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
4413                 return -1;
4414
4415         if (params->htcaps && params->htcaps_mask) {
4416                 int sz = sizeof(struct ieee80211_ht_capabilities);
4417                 wpa_hexdump(MSG_DEBUG, "  * htcaps", params->htcaps, sz);
4418                 wpa_hexdump(MSG_DEBUG, "  * htcaps_mask",
4419                             params->htcaps_mask, sz);
4420                 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
4421                             params->htcaps) ||
4422                     nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
4423                             params->htcaps_mask))
4424                         return -1;
4425         }
4426
4427 #ifdef CONFIG_VHT_OVERRIDES
4428         if (params->disable_vht) {
4429                 wpa_printf(MSG_DEBUG, "  * VHT disabled");
4430                 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
4431                         return -1;
4432         }
4433
4434         if (params->vhtcaps && params->vhtcaps_mask) {
4435                 int sz = sizeof(struct ieee80211_vht_capabilities);
4436                 wpa_hexdump(MSG_DEBUG, "  * vhtcaps", params->vhtcaps, sz);
4437                 wpa_hexdump(MSG_DEBUG, "  * vhtcaps_mask",
4438                             params->vhtcaps_mask, sz);
4439                 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
4440                             params->vhtcaps) ||
4441                     nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
4442                             params->vhtcaps_mask))
4443                         return -1;
4444         }
4445 #endif /* CONFIG_VHT_OVERRIDES */
4446
4447         if (params->p2p)
4448                 wpa_printf(MSG_DEBUG, "  * P2P group");
4449
4450         return 0;
4451 }
4452
4453
4454 static int wpa_driver_nl80211_try_connect(
4455         struct wpa_driver_nl80211_data *drv,
4456         struct wpa_driver_associate_params *params)
4457 {
4458         struct nl_msg *msg;
4459         enum nl80211_auth_type type;
4460         int ret;
4461         int algs;
4462
4463         if (params->req_key_mgmt_offload && params->psk &&
4464             (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4465              params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
4466              params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
4467                 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
4468                 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
4469                 if (ret)
4470                         return ret;
4471         }
4472
4473         wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
4474         msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
4475         if (!msg)
4476                 return -1;
4477
4478         ret = nl80211_connect_common(drv, params, msg);
4479         if (ret)
4480                 goto fail;
4481
4482         algs = 0;
4483         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4484                 algs++;
4485         if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4486                 algs++;
4487         if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4488                 algs++;
4489         if (algs > 1) {
4490                 wpa_printf(MSG_DEBUG, "  * Leave out Auth Type for automatic "
4491                            "selection");
4492                 goto skip_auth_type;
4493         }
4494
4495         if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4496                 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4497         else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4498                 type = NL80211_AUTHTYPE_SHARED_KEY;
4499         else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4500                 type = NL80211_AUTHTYPE_NETWORK_EAP;
4501         else if (params->auth_alg & WPA_AUTH_ALG_FT)
4502                 type = NL80211_AUTHTYPE_FT;
4503         else
4504                 goto fail;
4505
4506         wpa_printf(MSG_DEBUG, "  * Auth Type %d", type);
4507         if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
4508                 goto fail;
4509
4510 skip_auth_type:
4511         ret = nl80211_set_conn_keys(params, msg);
4512         if (ret)
4513                 goto fail;
4514
4515         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4516         msg = NULL;
4517         if (ret) {
4518                 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
4519                            "(%s)", ret, strerror(-ret));
4520         } else {
4521                 wpa_printf(MSG_DEBUG,
4522                            "nl80211: Connect request send successfully");
4523         }
4524
4525 fail:
4526         nlmsg_free(msg);
4527         return ret;
4528
4529 }
4530
4531
4532 static int wpa_driver_nl80211_connect(
4533         struct wpa_driver_nl80211_data *drv,
4534         struct wpa_driver_associate_params *params)
4535 {
4536         int ret;
4537
4538         /* Store the connection attempted bssid for future use */
4539         if (params->bssid)
4540                 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
4541         else
4542                 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
4543
4544         ret = wpa_driver_nl80211_try_connect(drv, params);
4545         if (ret == -EALREADY) {
4546                 /*
4547                  * cfg80211 does not currently accept new connections if
4548                  * we are already connected. As a workaround, force
4549                  * disconnection and try again.
4550                  */
4551                 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
4552                            "disconnecting before reassociation "
4553                            "attempt");
4554                 if (wpa_driver_nl80211_disconnect(
4555                             drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
4556                         return -1;
4557                 ret = wpa_driver_nl80211_try_connect(drv, params);
4558         }
4559         return ret;
4560 }
4561
4562
4563 static int wpa_driver_nl80211_associate(
4564         void *priv, struct wpa_driver_associate_params *params)
4565 {
4566         struct i802_bss *bss = priv;
4567         struct wpa_driver_nl80211_data *drv = bss->drv;
4568         int ret = -1;
4569         struct nl_msg *msg;
4570
4571         if (params->mode == IEEE80211_MODE_AP)
4572                 return wpa_driver_nl80211_ap(drv, params);
4573
4574         if (params->mode == IEEE80211_MODE_IBSS)
4575                 return wpa_driver_nl80211_ibss(drv, params);
4576
4577         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
4578                 enum nl80211_iftype nlmode = params->p2p ?
4579                         NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4580
4581                 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
4582                         return -1;
4583                 return wpa_driver_nl80211_connect(drv, params);
4584         }
4585
4586         nl80211_mark_disconnected(drv);
4587
4588         wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
4589                    drv->ifindex);
4590         msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
4591         if (!msg)
4592                 return -1;
4593
4594         ret = nl80211_connect_common(drv, params, msg);
4595         if (ret)
4596                 goto fail;
4597
4598         if (params->prev_bssid) {
4599                 wpa_printf(MSG_DEBUG, "  * prev_bssid=" MACSTR,
4600                            MAC2STR(params->prev_bssid));
4601                 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
4602                             params->prev_bssid))
4603                         goto fail;
4604         }
4605
4606         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4607         msg = NULL;
4608         if (ret) {
4609                 wpa_dbg(drv->ctx, MSG_DEBUG,
4610                         "nl80211: MLME command failed (assoc): ret=%d (%s)",
4611                         ret, strerror(-ret));
4612                 nl80211_dump_scan(drv);
4613         } else {
4614                 wpa_printf(MSG_DEBUG,
4615                            "nl80211: Association request send successfully");
4616         }
4617
4618 fail:
4619         nlmsg_free(msg);
4620         return ret;
4621 }
4622
4623
4624 static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
4625                             int ifindex, enum nl80211_iftype mode)
4626 {
4627         struct nl_msg *msg;
4628         int ret = -ENOBUFS;
4629
4630         wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
4631                    ifindex, mode, nl80211_iftype_str(mode));
4632
4633         msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
4634         if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
4635                 goto fail;
4636
4637         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4638         msg = NULL;
4639         if (!ret)
4640                 return 0;
4641 fail:
4642         nlmsg_free(msg);
4643         wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
4644                    " %d (%s)", ifindex, mode, ret, strerror(-ret));
4645         return ret;
4646 }
4647
4648
4649 static int wpa_driver_nl80211_set_mode_impl(
4650                 struct i802_bss *bss,
4651                 enum nl80211_iftype nlmode,
4652                 struct hostapd_freq_params *desired_freq_params)
4653 {
4654         struct wpa_driver_nl80211_data *drv = bss->drv;
4655         int ret = -1;
4656         int i;
4657         int was_ap = is_ap_interface(drv->nlmode);
4658         int res;
4659         int mode_switch_res;
4660
4661         mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
4662         if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
4663                 mode_switch_res = 0;
4664
4665         if (mode_switch_res == 0) {
4666                 drv->nlmode = nlmode;
4667                 ret = 0;
4668                 goto done;
4669         }
4670
4671         if (mode_switch_res == -ENODEV)
4672                 return -1;
4673
4674         if (nlmode == drv->nlmode) {
4675                 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
4676                            "requested mode - ignore error");
4677                 ret = 0;
4678                 goto done; /* Already in the requested mode */
4679         }
4680
4681         /* mac80211 doesn't allow mode changes while the device is up, so
4682          * take the device down, try to set the mode again, and bring the
4683          * device back up.
4684          */
4685         wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
4686                    "interface down");
4687         for (i = 0; i < 10; i++) {
4688                 res = i802_set_iface_flags(bss, 0);
4689                 if (res == -EACCES || res == -ENODEV)
4690                         break;
4691                 if (res != 0) {
4692                         wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
4693                                    "interface down");
4694                         os_sleep(0, 100000);
4695                         continue;
4696                 }
4697
4698                 /*
4699                  * Setting the mode will fail for some drivers if the phy is
4700                  * on a frequency that the mode is disallowed in.
4701                  */
4702                 if (desired_freq_params) {
4703                         res = i802_set_freq(bss, desired_freq_params);
4704                         if (res) {
4705                                 wpa_printf(MSG_DEBUG,
4706                                            "nl80211: Failed to set frequency on interface");
4707                         }
4708                 }
4709
4710                 /* Try to set the mode again while the interface is down */
4711                 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
4712                 if (mode_switch_res == -EBUSY) {
4713                         wpa_printf(MSG_DEBUG,
4714                                    "nl80211: Delaying mode set while interface going down");
4715                         os_sleep(0, 100000);
4716                         continue;
4717                 }
4718                 ret = mode_switch_res;
4719                 break;
4720         }
4721
4722         if (!ret) {
4723                 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
4724                            "interface is down");
4725                 drv->nlmode = nlmode;
4726                 drv->ignore_if_down_event = 1;
4727         }
4728
4729         /* Bring the interface back up */
4730         res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
4731         if (res != 0) {
4732                 wpa_printf(MSG_DEBUG,
4733                            "nl80211: Failed to set interface up after switching mode");
4734                 ret = -1;
4735         }
4736
4737 done:
4738         if (ret) {
4739                 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
4740                            "from %d failed", nlmode, drv->nlmode);
4741                 return ret;
4742         }
4743
4744         if (is_p2p_net_interface(nlmode)) {
4745                 wpa_printf(MSG_DEBUG,
4746                            "nl80211: Interface %s mode change to P2P - disable 11b rates",
4747                            bss->ifname);
4748                 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
4749         } else if (drv->disabled_11b_rates) {
4750                 wpa_printf(MSG_DEBUG,
4751                            "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
4752                            bss->ifname);
4753                 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
4754         }
4755
4756         if (is_ap_interface(nlmode)) {
4757                 nl80211_mgmt_unsubscribe(bss, "start AP");
4758                 /* Setup additional AP mode functionality if needed */
4759                 if (nl80211_setup_ap(bss))
4760                         return -1;
4761         } else if (was_ap) {
4762                 /* Remove additional AP mode functionality */
4763                 nl80211_teardown_ap(bss);
4764         } else {
4765                 nl80211_mgmt_unsubscribe(bss, "mode change");
4766         }
4767
4768         if (is_mesh_interface(nlmode) &&
4769             nl80211_mgmt_subscribe_mesh(bss))
4770                 return -1;
4771
4772         if (!bss->in_deinit && !is_ap_interface(nlmode) &&
4773             !is_mesh_interface(nlmode) &&
4774             nl80211_mgmt_subscribe_non_ap(bss) < 0)
4775                 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
4776                            "frame processing - ignore for now");
4777
4778         return 0;
4779 }
4780
4781
4782 int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
4783                                 enum nl80211_iftype nlmode)
4784 {
4785         return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
4786 }
4787
4788
4789 static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
4790                                             struct hostapd_freq_params *freq)
4791 {
4792         return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
4793                                                 freq);
4794 }
4795
4796
4797 static int wpa_driver_nl80211_get_capa(void *priv,
4798                                        struct wpa_driver_capa *capa)
4799 {
4800         struct i802_bss *bss = priv;
4801         struct wpa_driver_nl80211_data *drv = bss->drv;
4802
4803         if (!drv->has_capability)
4804                 return -1;
4805         os_memcpy(capa, &drv->capa, sizeof(*capa));
4806         if (drv->extended_capa && drv->extended_capa_mask) {
4807                 capa->extended_capa = drv->extended_capa;
4808                 capa->extended_capa_mask = drv->extended_capa_mask;
4809                 capa->extended_capa_len = drv->extended_capa_len;
4810         }
4811
4812         return 0;
4813 }
4814
4815
4816 static int wpa_driver_nl80211_set_operstate(void *priv, int state)
4817 {
4818         struct i802_bss *bss = priv;
4819         struct wpa_driver_nl80211_data *drv = bss->drv;
4820
4821         wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
4822                    bss->ifname, drv->operstate, state,
4823                    state ? "UP" : "DORMANT");
4824         drv->operstate = state;
4825         return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
4826                                       state ? IF_OPER_UP : IF_OPER_DORMANT);
4827 }
4828
4829
4830 static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
4831 {
4832         struct i802_bss *bss = priv;
4833         struct wpa_driver_nl80211_data *drv = bss->drv;
4834         struct nl_msg *msg;
4835         struct nl80211_sta_flag_update upd;
4836         int ret;
4837
4838         if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
4839                 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
4840                 return 0;
4841         }
4842
4843         wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
4844                    MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
4845
4846         os_memset(&upd, 0, sizeof(upd));
4847         upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
4848         if (authorized)
4849                 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
4850
4851         if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4852             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
4853             nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
4854                 nlmsg_free(msg);
4855                 return -ENOBUFS;
4856         }
4857
4858         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4859         if (!ret)
4860                 return 0;
4861         wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
4862                    ret, strerror(-ret));
4863         return ret;
4864 }
4865
4866
4867 /* Set kernel driver on given frequency (MHz) */
4868 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
4869 {
4870         struct i802_bss *bss = priv;
4871         return nl80211_set_channel(bss, freq, 0);
4872 }
4873
4874
4875 static inline int min_int(int a, int b)
4876 {
4877         if (a < b)
4878                 return a;
4879         return b;
4880 }
4881
4882
4883 static int get_key_handler(struct nl_msg *msg, void *arg)
4884 {
4885         struct nlattr *tb[NL80211_ATTR_MAX + 1];
4886         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4887
4888         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4889                   genlmsg_attrlen(gnlh, 0), NULL);
4890
4891         /*
4892          * TODO: validate the key index and mac address!
4893          * Otherwise, there's a race condition as soon as
4894          * the kernel starts sending key notifications.
4895          */
4896
4897         if (tb[NL80211_ATTR_KEY_SEQ])
4898                 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
4899                        min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
4900         return NL_SKIP;
4901 }
4902
4903
4904 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
4905                            int idx, u8 *seq)
4906 {
4907         struct i802_bss *bss = priv;
4908         struct wpa_driver_nl80211_data *drv = bss->drv;
4909         struct nl_msg *msg;
4910
4911         msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
4912                                   NL80211_CMD_GET_KEY);
4913         if (!msg ||
4914             (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
4915             nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
4916                 nlmsg_free(msg);
4917                 return -ENOBUFS;
4918         }
4919
4920         memset(seq, 0, 6);
4921
4922         return send_and_recv_msgs(drv, msg, get_key_handler, seq);
4923 }
4924
4925
4926 static int i802_set_rts(void *priv, int rts)
4927 {
4928         struct i802_bss *bss = priv;
4929         struct wpa_driver_nl80211_data *drv = bss->drv;
4930         struct nl_msg *msg;
4931         int ret;
4932         u32 val;
4933
4934         if (rts >= 2347)
4935                 val = (u32) -1;
4936         else
4937                 val = rts;
4938
4939         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
4940             nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
4941                 nlmsg_free(msg);
4942                 return -ENOBUFS;
4943         }
4944
4945         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4946         if (!ret)
4947                 return 0;
4948         wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
4949                    "%d (%s)", rts, ret, strerror(-ret));
4950         return ret;
4951 }
4952
4953
4954 static int i802_set_frag(void *priv, int frag)
4955 {
4956         struct i802_bss *bss = priv;
4957         struct wpa_driver_nl80211_data *drv = bss->drv;
4958         struct nl_msg *msg;
4959         int ret;
4960         u32 val;
4961
4962         if (frag >= 2346)
4963                 val = (u32) -1;
4964         else
4965                 val = frag;
4966
4967         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
4968             nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
4969                 nlmsg_free(msg);
4970                 return -ENOBUFS;
4971         }
4972
4973         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4974         if (!ret)
4975                 return 0;
4976         wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
4977                    "%d: %d (%s)", frag, ret, strerror(-ret));
4978         return ret;
4979 }
4980
4981
4982 static int i802_flush(void *priv)
4983 {
4984         struct i802_bss *bss = priv;
4985         struct nl_msg *msg;
4986         int res;
4987
4988         wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
4989                    bss->ifname);
4990
4991         /*
4992          * XXX: FIX! this needs to flush all VLANs too
4993          */
4994         msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
4995         res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
4996         if (res) {
4997                 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
4998                            "(%s)", res, strerror(-res));
4999         }
5000         return res;
5001 }
5002
5003
5004 static int get_sta_handler(struct nl_msg *msg, void *arg)
5005 {
5006         struct nlattr *tb[NL80211_ATTR_MAX + 1];
5007         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5008         struct hostap_sta_driver_data *data = arg;
5009         struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
5010         static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
5011                 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
5012                 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
5013                 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
5014                 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
5015                 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
5016                 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
5017         };
5018
5019         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5020                   genlmsg_attrlen(gnlh, 0), NULL);
5021
5022         /*
5023          * TODO: validate the interface and mac address!
5024          * Otherwise, there's a race condition as soon as
5025          * the kernel starts sending station notifications.
5026          */
5027
5028         if (!tb[NL80211_ATTR_STA_INFO]) {
5029                 wpa_printf(MSG_DEBUG, "sta stats missing!");
5030                 return NL_SKIP;
5031         }
5032         if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
5033                              tb[NL80211_ATTR_STA_INFO],
5034                              stats_policy)) {
5035                 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
5036                 return NL_SKIP;
5037         }
5038
5039         if (stats[NL80211_STA_INFO_INACTIVE_TIME])
5040                 data->inactive_msec =
5041                         nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
5042         if (stats[NL80211_STA_INFO_RX_BYTES])
5043                 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
5044         if (stats[NL80211_STA_INFO_TX_BYTES])
5045                 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
5046         if (stats[NL80211_STA_INFO_RX_PACKETS])
5047                 data->rx_packets =
5048                         nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
5049         if (stats[NL80211_STA_INFO_TX_PACKETS])
5050                 data->tx_packets =
5051                         nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
5052         if (stats[NL80211_STA_INFO_TX_FAILED])
5053                 data->tx_retry_failed =
5054                         nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
5055
5056         return NL_SKIP;
5057 }
5058
5059 static int i802_read_sta_data(struct i802_bss *bss,
5060                               struct hostap_sta_driver_data *data,
5061                               const u8 *addr)
5062 {
5063         struct nl_msg *msg;
5064
5065         os_memset(data, 0, sizeof(*data));
5066
5067         if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
5068             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
5069                 nlmsg_free(msg);
5070                 return -ENOBUFS;
5071         }
5072
5073         return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
5074 }
5075
5076
5077 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
5078                                     int cw_min, int cw_max, int burst_time)
5079 {
5080         struct i802_bss *bss = priv;
5081         struct wpa_driver_nl80211_data *drv = bss->drv;
5082         struct nl_msg *msg;
5083         struct nlattr *txq, *params;
5084
5085         msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
5086         if (!msg)
5087                 return -1;
5088
5089         txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
5090         if (!txq)
5091                 goto fail;
5092
5093         /* We are only sending parameters for a single TXQ at a time */
5094         params = nla_nest_start(msg, 1);
5095         if (!params)
5096                 goto fail;
5097
5098         switch (queue) {
5099         case 0:
5100                 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
5101                         goto fail;
5102                 break;
5103         case 1:
5104                 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
5105                         goto fail;
5106                 break;
5107         case 2:
5108                 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
5109                         goto fail;
5110                 break;
5111         case 3:
5112                 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
5113                         goto fail;
5114                 break;
5115         }
5116         /* Burst time is configured in units of 0.1 msec and TXOP parameter in
5117          * 32 usec, so need to convert the value here. */
5118         if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
5119                         (burst_time * 100 + 16) / 32) ||
5120             nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
5121             nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
5122             nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
5123                 goto fail;
5124
5125         nla_nest_end(msg, params);
5126
5127         nla_nest_end(msg, txq);
5128
5129         if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5130                 return 0;
5131         msg = NULL;
5132 fail:
5133         nlmsg_free(msg);
5134         return -1;
5135 }
5136
5137
5138 static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
5139                              const char *ifname, int vlan_id)
5140 {
5141         struct wpa_driver_nl80211_data *drv = bss->drv;
5142         struct nl_msg *msg;
5143         int ret;
5144
5145         wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
5146                    ", ifname=%s[%d], vlan_id=%d)",
5147                    bss->ifname, if_nametoindex(bss->ifname),
5148                    MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
5149         if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5150             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5151             nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
5152                 nlmsg_free(msg);
5153                 return -ENOBUFS;
5154         }
5155
5156         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5157         if (ret < 0) {
5158                 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
5159                            MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
5160                            MAC2STR(addr), ifname, vlan_id, ret,
5161                            strerror(-ret));
5162         }
5163         return ret;
5164 }
5165
5166
5167 static int i802_get_inact_sec(void *priv, const u8 *addr)
5168 {
5169         struct hostap_sta_driver_data data;
5170         int ret;
5171
5172         data.inactive_msec = (unsigned long) -1;
5173         ret = i802_read_sta_data(priv, &data, addr);
5174         if (ret || data.inactive_msec == (unsigned long) -1)
5175                 return -1;
5176         return data.inactive_msec / 1000;
5177 }
5178
5179
5180 static int i802_sta_clear_stats(void *priv, const u8 *addr)
5181 {
5182 #if 0
5183         /* TODO */
5184 #endif
5185         return 0;
5186 }
5187
5188
5189 static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
5190                            int reason)
5191 {
5192         struct i802_bss *bss = priv;
5193         struct wpa_driver_nl80211_data *drv = bss->drv;
5194         struct ieee80211_mgmt mgmt;
5195
5196         if (is_mesh_interface(drv->nlmode))
5197                 return -1;
5198
5199         if (drv->device_ap_sme)
5200                 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
5201
5202         memset(&mgmt, 0, sizeof(mgmt));
5203         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5204                                           WLAN_FC_STYPE_DEAUTH);
5205         memcpy(mgmt.da, addr, ETH_ALEN);
5206         memcpy(mgmt.sa, own_addr, ETH_ALEN);
5207         memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5208         mgmt.u.deauth.reason_code = host_to_le16(reason);
5209         return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5210                                             IEEE80211_HDRLEN +
5211                                             sizeof(mgmt.u.deauth), 0, 0, 0, 0,
5212                                             0);
5213 }
5214
5215
5216 static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
5217                              int reason)
5218 {
5219         struct i802_bss *bss = priv;
5220         struct wpa_driver_nl80211_data *drv = bss->drv;
5221         struct ieee80211_mgmt mgmt;
5222
5223         if (is_mesh_interface(drv->nlmode))
5224                 return -1;
5225
5226         if (drv->device_ap_sme)
5227                 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
5228
5229         memset(&mgmt, 0, sizeof(mgmt));
5230         mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5231                                           WLAN_FC_STYPE_DISASSOC);
5232         memcpy(mgmt.da, addr, ETH_ALEN);
5233         memcpy(mgmt.sa, own_addr, ETH_ALEN);
5234         memcpy(mgmt.bssid, own_addr, ETH_ALEN);
5235         mgmt.u.disassoc.reason_code = host_to_le16(reason);
5236         return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
5237                                             IEEE80211_HDRLEN +
5238                                             sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
5239                                             0);
5240 }
5241
5242
5243 static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
5244 {
5245         char buf[200], *pos, *end;
5246         int i, res;
5247
5248         pos = buf;
5249         end = pos + sizeof(buf);
5250
5251         for (i = 0; i < drv->num_if_indices; i++) {
5252                 if (!drv->if_indices[i])
5253                         continue;
5254                 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
5255                 if (os_snprintf_error(end - pos, res))
5256                         break;
5257                 pos += res;
5258         }
5259         *pos = '\0';
5260
5261         wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
5262                    drv->num_if_indices, buf);
5263 }
5264
5265
5266 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5267 {
5268         int i;
5269         int *old;
5270
5271         wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
5272                    ifidx);
5273         if (have_ifidx(drv, ifidx)) {
5274                 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
5275                            ifidx);
5276                 return;
5277         }
5278         for (i = 0; i < drv->num_if_indices; i++) {
5279                 if (drv->if_indices[i] == 0) {
5280                         drv->if_indices[i] = ifidx;
5281                         dump_ifidx(drv);
5282                         return;
5283                 }
5284         }
5285
5286         if (drv->if_indices != drv->default_if_indices)
5287                 old = drv->if_indices;
5288         else
5289                 old = NULL;
5290
5291         drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
5292                                            sizeof(int));
5293         if (!drv->if_indices) {
5294                 if (!old)
5295                         drv->if_indices = drv->default_if_indices;
5296                 else
5297                         drv->if_indices = old;
5298                 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
5299                            "interfaces");
5300                 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
5301                 return;
5302         } else if (!old)
5303                 os_memcpy(drv->if_indices, drv->default_if_indices,
5304                           sizeof(drv->default_if_indices));
5305         drv->if_indices[drv->num_if_indices] = ifidx;
5306         drv->num_if_indices++;
5307         dump_ifidx(drv);
5308 }
5309
5310
5311 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5312 {
5313         int i;
5314
5315         for (i = 0; i < drv->num_if_indices; i++) {
5316                 if (drv->if_indices[i] == ifidx) {
5317                         drv->if_indices[i] = 0;
5318                         break;
5319                 }
5320         }
5321         dump_ifidx(drv);
5322 }
5323
5324
5325 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5326 {
5327         int i;
5328
5329         for (i = 0; i < drv->num_if_indices; i++)
5330                 if (drv->if_indices[i] == ifidx)
5331                         return 1;
5332
5333         return 0;
5334 }
5335
5336
5337 static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
5338                             const char *bridge_ifname, char *ifname_wds)
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         if (ifname_wds)
5346                 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
5347
5348         wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
5349                    " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
5350         if (val) {
5351                 if (!if_nametoindex(name)) {
5352                         if (nl80211_create_iface(drv, name,
5353                                                  NL80211_IFTYPE_AP_VLAN,
5354                                                  bss->addr, 1, NULL, NULL, 0) <
5355                             0)
5356                                 return -1;
5357                         if (bridge_ifname &&
5358                             linux_br_add_if(drv->global->ioctl_sock,
5359                                             bridge_ifname, name) < 0)
5360                                 return -1;
5361                 }
5362                 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
5363                         wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
5364                                    "interface %s up", name);
5365                 }
5366                 return i802_set_sta_vlan(priv, addr, name, 0);
5367         } else {
5368                 if (bridge_ifname)
5369                         linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
5370                                         name);
5371
5372                 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
5373                 nl80211_remove_iface(drv, if_nametoindex(name));
5374                 return 0;
5375         }
5376 }
5377
5378
5379 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
5380 {
5381         struct wpa_driver_nl80211_data *drv = eloop_ctx;
5382         struct sockaddr_ll lladdr;
5383         unsigned char buf[3000];
5384         int len;
5385         socklen_t fromlen = sizeof(lladdr);
5386
5387         len = recvfrom(sock, buf, sizeof(buf), 0,
5388                        (struct sockaddr *)&lladdr, &fromlen);
5389         if (len < 0) {
5390                 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
5391                            strerror(errno));
5392                 return;
5393         }
5394
5395         if (have_ifidx(drv, lladdr.sll_ifindex))
5396                 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
5397 }
5398
5399
5400 static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
5401                              struct i802_bss *bss,
5402                              const char *brname, const char *ifname)
5403 {
5404         int br_ifindex;
5405         char in_br[IFNAMSIZ];
5406
5407         os_strlcpy(bss->brname, brname, IFNAMSIZ);
5408         br_ifindex = if_nametoindex(brname);
5409         if (br_ifindex == 0) {
5410                 /*
5411                  * Bridge was configured, but the bridge device does
5412                  * not exist. Try to add it now.
5413                  */
5414                 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
5415                         wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
5416                                    "bridge interface %s: %s",
5417                                    brname, strerror(errno));
5418                         return -1;
5419                 }
5420                 bss->added_bridge = 1;
5421                 br_ifindex = if_nametoindex(brname);
5422                 add_ifidx(drv, br_ifindex);
5423         }
5424         bss->br_ifindex = br_ifindex;
5425
5426         if (linux_br_get(in_br, ifname) == 0) {
5427                 if (os_strcmp(in_br, brname) == 0)
5428                         return 0; /* already in the bridge */
5429
5430                 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
5431                            "bridge %s", ifname, in_br);
5432                 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
5433                     0) {
5434                         wpa_printf(MSG_ERROR, "nl80211: Failed to "
5435                                    "remove interface %s from bridge "
5436                                    "%s: %s",
5437                                    ifname, brname, strerror(errno));
5438                         return -1;
5439                 }
5440         }
5441
5442         wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
5443                    ifname, brname);
5444         if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
5445                 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
5446                            "into bridge %s: %s",
5447                            ifname, brname, strerror(errno));
5448                 return -1;
5449         }
5450         bss->added_if_into_bridge = 1;
5451
5452         return 0;
5453 }
5454
5455
5456 static void *i802_init(struct hostapd_data *hapd,
5457                        struct wpa_init_params *params)
5458 {
5459         struct wpa_driver_nl80211_data *drv;
5460         struct i802_bss *bss;
5461         size_t i;
5462         char brname[IFNAMSIZ];
5463         int ifindex, br_ifindex;
5464         int br_added = 0;
5465
5466         bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
5467                                           params->global_priv, 1,
5468                                           params->bssid, params->driver_params);
5469         if (bss == NULL)
5470                 return NULL;
5471
5472         drv = bss->drv;
5473
5474         if (linux_br_get(brname, params->ifname) == 0) {
5475                 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
5476                            params->ifname, brname);
5477                 br_ifindex = if_nametoindex(brname);
5478                 os_strlcpy(bss->brname, brname, IFNAMSIZ);
5479         } else {
5480                 brname[0] = '\0';
5481                 br_ifindex = 0;
5482         }
5483         bss->br_ifindex = br_ifindex;
5484
5485         for (i = 0; i < params->num_bridge; i++) {
5486                 if (params->bridge[i]) {
5487                         ifindex = if_nametoindex(params->bridge[i]);
5488                         if (ifindex)
5489                                 add_ifidx(drv, ifindex);
5490                         if (ifindex == br_ifindex)
5491                                 br_added = 1;
5492                 }
5493         }
5494         if (!br_added && br_ifindex &&
5495             (params->num_bridge == 0 || !params->bridge[0]))
5496                 add_ifidx(drv, br_ifindex);
5497
5498         /* start listening for EAPOL on the default AP interface */
5499         add_ifidx(drv, drv->ifindex);
5500
5501         if (params->num_bridge && params->bridge[0] &&
5502             i802_check_bridge(drv, bss, params->bridge[0], params->ifname) < 0)
5503                 goto failed;
5504
5505 #ifdef CONFIG_LIBNL3_ROUTE
5506         if (bss->added_if_into_bridge) {
5507                 drv->rtnl_sk = nl_socket_alloc();
5508                 if (drv->rtnl_sk == NULL) {
5509                         wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
5510                         goto failed;
5511                 }
5512
5513                 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
5514                         wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
5515                                    strerror(errno));
5516                         goto failed;
5517                 }
5518         }
5519 #endif /* CONFIG_LIBNL3_ROUTE */
5520
5521         drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
5522         if (drv->eapol_sock < 0) {
5523                 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
5524                            strerror(errno));
5525                 goto failed;
5526         }
5527
5528         if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
5529         {
5530                 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
5531                 goto failed;
5532         }
5533
5534         if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
5535                                params->own_addr))
5536                 goto failed;
5537         os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
5538
5539         memcpy(bss->addr, params->own_addr, ETH_ALEN);
5540
5541         return bss;
5542
5543 failed:
5544         wpa_driver_nl80211_deinit(bss);
5545         return NULL;
5546 }
5547
5548
5549 static void i802_deinit(void *priv)
5550 {
5551         struct i802_bss *bss = priv;
5552         wpa_driver_nl80211_deinit(bss);
5553 }
5554
5555
5556 static enum nl80211_iftype wpa_driver_nl80211_if_type(
5557         enum wpa_driver_if_type type)
5558 {
5559         switch (type) {
5560         case WPA_IF_STATION:
5561                 return NL80211_IFTYPE_STATION;
5562         case WPA_IF_P2P_CLIENT:
5563         case WPA_IF_P2P_GROUP:
5564                 return NL80211_IFTYPE_P2P_CLIENT;
5565         case WPA_IF_AP_VLAN:
5566                 return NL80211_IFTYPE_AP_VLAN;
5567         case WPA_IF_AP_BSS:
5568                 return NL80211_IFTYPE_AP;
5569         case WPA_IF_P2P_GO:
5570                 return NL80211_IFTYPE_P2P_GO;
5571         case WPA_IF_P2P_DEVICE:
5572                 return NL80211_IFTYPE_P2P_DEVICE;
5573         }
5574         return -1;
5575 }
5576
5577
5578 #ifdef CONFIG_P2P
5579
5580 static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
5581 {
5582         struct wpa_driver_nl80211_data *drv;
5583         dl_list_for_each(drv, &global->interfaces,
5584                          struct wpa_driver_nl80211_data, list) {
5585                 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
5586                         return 1;
5587         }
5588         return 0;
5589 }
5590
5591
5592 static int nl80211_p2p_interface_addr(struct wpa_driver_nl80211_data *drv,
5593                                       u8 *new_addr)
5594 {
5595         unsigned int idx;
5596
5597         if (!drv->global)
5598                 return -1;
5599
5600         os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
5601         for (idx = 0; idx < 64; idx++) {
5602                 new_addr[0] = drv->first_bss->addr[0] | 0x02;
5603                 new_addr[0] ^= idx << 2;
5604                 if (!nl80211_addr_in_use(drv->global, new_addr))
5605                         break;
5606         }
5607         if (idx == 64)
5608                 return -1;
5609
5610         wpa_printf(MSG_DEBUG, "nl80211: Assigned new P2P Interface Address "
5611                    MACSTR, MAC2STR(new_addr));
5612
5613         return 0;
5614 }
5615
5616 #endif /* CONFIG_P2P */
5617
5618
5619 struct wdev_info {
5620         u64 wdev_id;
5621         int wdev_id_set;
5622         u8 macaddr[ETH_ALEN];
5623 };
5624
5625 static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
5626 {
5627         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5628         struct nlattr *tb[NL80211_ATTR_MAX + 1];
5629         struct wdev_info *wi = arg;
5630
5631         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5632                   genlmsg_attrlen(gnlh, 0), NULL);
5633         if (tb[NL80211_ATTR_WDEV]) {
5634                 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
5635                 wi->wdev_id_set = 1;
5636         }
5637
5638         if (tb[NL80211_ATTR_MAC])
5639                 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
5640                           ETH_ALEN);
5641
5642         return NL_SKIP;
5643 }
5644
5645
5646 static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
5647                                      const char *ifname, const u8 *addr,
5648                                      void *bss_ctx, void **drv_priv,
5649                                      char *force_ifname, u8 *if_addr,
5650                                      const char *bridge, int use_existing)
5651 {
5652         enum nl80211_iftype nlmode;
5653         struct i802_bss *bss = priv;
5654         struct wpa_driver_nl80211_data *drv = bss->drv;
5655         int ifidx;
5656         int added = 1;
5657
5658         if (addr)
5659                 os_memcpy(if_addr, addr, ETH_ALEN);
5660         nlmode = wpa_driver_nl80211_if_type(type);
5661         if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
5662                 struct wdev_info p2pdev_info;
5663
5664                 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
5665                 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
5666                                              0, nl80211_wdev_handler,
5667                                              &p2pdev_info, use_existing);
5668                 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
5669                         wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
5670                                    ifname);
5671                         return -1;
5672                 }
5673
5674                 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
5675                 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
5676                 if (!is_zero_ether_addr(p2pdev_info.macaddr))
5677                         os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
5678                 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
5679                            ifname,
5680                            (long long unsigned int) p2pdev_info.wdev_id);
5681         } else {
5682                 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
5683                                              0, NULL, NULL, use_existing);
5684                 if (use_existing && ifidx == -ENFILE) {
5685                         added = 0;
5686                         ifidx = if_nametoindex(ifname);
5687                 } else if (ifidx < 0) {
5688                         return -1;
5689                 }
5690         }
5691
5692         if (!addr) {
5693                 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
5694                         os_memcpy(if_addr, bss->addr, ETH_ALEN);
5695                 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
5696                                             bss->ifname, if_addr) < 0) {
5697                         if (added)
5698                                 nl80211_remove_iface(drv, ifidx);
5699                         return -1;
5700                 }
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];
5709
5710                 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
5711                                        new_addr) < 0) {
5712                         if (added)
5713                                 nl80211_remove_iface(drv, ifidx);
5714                         return -1;
5715                 }
5716                 if (nl80211_addr_in_use(drv->global, new_addr)) {
5717                         wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
5718                                    "for P2P group interface");
5719                         if (nl80211_p2p_interface_addr(drv, new_addr) < 0) {
5720                                 if (added)
5721                                         nl80211_remove_iface(drv, ifidx);
5722                                 return -1;
5723                         }
5724                         if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
5725                                                new_addr) < 0) {
5726                                 if (added)
5727                                         nl80211_remove_iface(drv, ifidx);
5728                                 return -1;
5729                         }
5730                 }
5731                 os_memcpy(if_addr, new_addr, ETH_ALEN);
5732         }
5733 #endif /* CONFIG_P2P */
5734
5735         if (type == WPA_IF_AP_BSS) {
5736                 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
5737                 if (new_bss == NULL) {
5738                         if (added)
5739                                 nl80211_remove_iface(drv, ifidx);
5740                         return -1;
5741                 }
5742
5743                 if (bridge &&
5744                     i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
5745                         wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
5746                                    "interface %s to a bridge %s",
5747                                    ifname, bridge);
5748                         if (added)
5749                                 nl80211_remove_iface(drv, ifidx);
5750                         os_free(new_bss);
5751                         return -1;
5752                 }
5753
5754                 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
5755                 {
5756                         if (added)
5757                                 nl80211_remove_iface(drv, ifidx);
5758                         os_free(new_bss);
5759                         return -1;
5760                 }
5761                 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
5762                 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
5763                 new_bss->ifindex = ifidx;
5764                 new_bss->drv = drv;
5765                 new_bss->next = drv->first_bss->next;
5766                 new_bss->freq = drv->first_bss->freq;
5767                 new_bss->ctx = bss_ctx;
5768                 new_bss->added_if = added;
5769                 drv->first_bss->next = new_bss;
5770                 if (drv_priv)
5771                         *drv_priv = new_bss;
5772                 nl80211_init_bss(new_bss);
5773
5774                 /* Subscribe management frames for this WPA_IF_AP_BSS */
5775                 if (nl80211_setup_ap(new_bss))
5776                         return -1;
5777         }
5778
5779         if (drv->global)
5780                 drv->global->if_add_ifindex = ifidx;
5781
5782         /*
5783          * Some virtual interfaces need to process EAPOL packets and events on
5784          * the parent interface. This is used mainly with hostapd.
5785          */
5786         if (ifidx > 0 &&
5787             (drv->hostapd ||
5788              nlmode == NL80211_IFTYPE_AP_VLAN ||
5789              nlmode == NL80211_IFTYPE_WDS ||
5790              nlmode == NL80211_IFTYPE_MONITOR))
5791                 add_ifidx(drv, ifidx);
5792
5793         return 0;
5794 }
5795
5796
5797 static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
5798                                         enum wpa_driver_if_type type,
5799                                         const char *ifname)
5800 {
5801         struct wpa_driver_nl80211_data *drv = bss->drv;
5802         int ifindex = if_nametoindex(ifname);
5803
5804         wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
5805                    __func__, type, ifname, ifindex, bss->added_if);
5806         if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
5807                 nl80211_remove_iface(drv, ifindex);
5808         else if (ifindex > 0 && !bss->added_if) {
5809                 struct wpa_driver_nl80211_data *drv2;
5810                 dl_list_for_each(drv2, &drv->global->interfaces,
5811                                  struct wpa_driver_nl80211_data, list)
5812                         del_ifidx(drv2, ifindex);
5813         }
5814
5815         if (type != WPA_IF_AP_BSS)
5816                 return 0;
5817
5818         if (bss->added_if_into_bridge) {
5819                 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
5820                                     bss->ifname) < 0)
5821                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
5822                                    "interface %s from bridge %s: %s",
5823                                    bss->ifname, bss->brname, strerror(errno));
5824         }
5825         if (bss->added_bridge) {
5826                 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
5827                         wpa_printf(MSG_INFO, "nl80211: Failed to remove "
5828                                    "bridge %s: %s",
5829                                    bss->brname, strerror(errno));
5830         }
5831
5832         if (bss != drv->first_bss) {
5833                 struct i802_bss *tbss;
5834
5835                 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
5836                 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
5837                         if (tbss->next == bss) {
5838                                 tbss->next = bss->next;
5839                                 /* Unsubscribe management frames */
5840                                 nl80211_teardown_ap(bss);
5841                                 nl80211_destroy_bss(bss);
5842                                 if (!bss->added_if)
5843                                         i802_set_iface_flags(bss, 0);
5844                                 os_free(bss);
5845                                 bss = NULL;
5846                                 break;
5847                         }
5848                 }
5849                 if (bss)
5850                         wpa_printf(MSG_INFO, "nl80211: %s - could not find "
5851                                    "BSS %p in the list", __func__, bss);
5852         } else {
5853                 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
5854                 nl80211_teardown_ap(bss);
5855                 if (!bss->added_if && !drv->first_bss->next)
5856                         wpa_driver_nl80211_del_beacon(drv);
5857                 nl80211_destroy_bss(bss);
5858                 if (!bss->added_if)
5859                         i802_set_iface_flags(bss, 0);
5860                 if (drv->first_bss->next) {
5861                         drv->first_bss = drv->first_bss->next;
5862                         drv->ctx = drv->first_bss->ctx;
5863                         os_free(bss);
5864                 } else {
5865                         wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
5866                 }
5867         }
5868
5869         return 0;
5870 }
5871
5872
5873 static int cookie_handler(struct nl_msg *msg, void *arg)
5874 {
5875         struct nlattr *tb[NL80211_ATTR_MAX + 1];
5876         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5877         u64 *cookie = arg;
5878         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5879                   genlmsg_attrlen(gnlh, 0), NULL);
5880         if (tb[NL80211_ATTR_COOKIE])
5881                 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
5882         return NL_SKIP;
5883 }
5884
5885
5886 static int nl80211_send_frame_cmd(struct i802_bss *bss,
5887                                   unsigned int freq, unsigned int wait,
5888                                   const u8 *buf, size_t buf_len,
5889                                   u64 *cookie_out, int no_cck, int no_ack,
5890                                   int offchanok)
5891 {
5892         struct wpa_driver_nl80211_data *drv = bss->drv;
5893         struct nl_msg *msg;
5894         u64 cookie;
5895         int ret = -1;
5896
5897         wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
5898                    "no_ack=%d offchanok=%d",
5899                    freq, wait, no_cck, no_ack, offchanok);
5900         wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
5901
5902         if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
5903             (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
5904             (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
5905             (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
5906                            drv->test_use_roc_tx) &&
5907              nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
5908             (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
5909             (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
5910             nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
5911                 goto fail;
5912
5913         cookie = 0;
5914         ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
5915         msg = NULL;
5916         if (ret) {
5917                 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
5918                            "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
5919                            freq, wait);
5920         } else {
5921                 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
5922                            "cookie 0x%llx", no_ack ? " (no ACK)" : "",
5923                            (long long unsigned int) cookie);
5924
5925                 if (cookie_out)
5926                         *cookie_out = no_ack ? (u64) -1 : cookie;
5927         }
5928
5929 fail:
5930         nlmsg_free(msg);
5931         return ret;
5932 }
5933
5934
5935 static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
5936                                           unsigned int freq,
5937                                           unsigned int wait_time,
5938                                           const u8 *dst, const u8 *src,
5939                                           const u8 *bssid,
5940                                           const u8 *data, size_t data_len,
5941                                           int no_cck)
5942 {
5943         struct wpa_driver_nl80211_data *drv = bss->drv;
5944         int ret = -1;
5945         u8 *buf;
5946         struct ieee80211_hdr *hdr;
5947
5948         wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
5949                    "freq=%u MHz wait=%d ms no_cck=%d)",
5950                    drv->ifindex, freq, wait_time, no_cck);
5951
5952         buf = os_zalloc(24 + data_len);
5953         if (buf == NULL)
5954                 return ret;
5955         os_memcpy(buf + 24, data, data_len);
5956         hdr = (struct ieee80211_hdr *) buf;
5957         hdr->frame_control =
5958                 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
5959         os_memcpy(hdr->addr1, dst, ETH_ALEN);
5960         os_memcpy(hdr->addr2, src, ETH_ALEN);
5961         os_memcpy(hdr->addr3, bssid, ETH_ALEN);
5962
5963         if (is_ap_interface(drv->nlmode) &&
5964             (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
5965              (int) freq == bss->freq || drv->device_ap_sme ||
5966              !drv->use_monitor))
5967                 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
5968                                                    0, freq, no_cck, 1,
5969                                                    wait_time);
5970         else
5971                 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
5972                                              24 + data_len,
5973                                              &drv->send_action_cookie,
5974                                              no_cck, 0, 1);
5975
5976         os_free(buf);
5977         return ret;
5978 }
5979
5980
5981 static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
5982 {
5983         struct i802_bss *bss = priv;
5984         struct wpa_driver_nl80211_data *drv = bss->drv;
5985         struct nl_msg *msg;
5986         int ret;
5987
5988         wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
5989                    (long long unsigned int) drv->send_action_cookie);
5990         if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
5991             nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie)) {
5992                 nlmsg_free(msg);
5993                 return;
5994         }
5995
5996         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5997         if (ret)
5998                 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
5999                            "(%s)", ret, strerror(-ret));
6000 }
6001
6002
6003 static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
6004                                                 unsigned int duration)
6005 {
6006         struct i802_bss *bss = priv;
6007         struct wpa_driver_nl80211_data *drv = bss->drv;
6008         struct nl_msg *msg;
6009         int ret;
6010         u64 cookie;
6011
6012         if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
6013             nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
6014             nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
6015                 nlmsg_free(msg);
6016                 return -1;
6017         }
6018
6019         cookie = 0;
6020         ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6021         if (ret == 0) {
6022                 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
6023                            "0x%llx for freq=%u MHz duration=%u",
6024                            (long long unsigned int) cookie, freq, duration);
6025                 drv->remain_on_chan_cookie = cookie;
6026                 drv->pending_remain_on_chan = 1;
6027                 return 0;
6028         }
6029         wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
6030                    "(freq=%d duration=%u): %d (%s)",
6031                    freq, duration, ret, strerror(-ret));
6032         return -1;
6033 }
6034
6035
6036 static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
6037 {
6038         struct i802_bss *bss = priv;
6039         struct wpa_driver_nl80211_data *drv = bss->drv;
6040         struct nl_msg *msg;
6041         int ret;
6042
6043         if (!drv->pending_remain_on_chan) {
6044                 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
6045                            "to cancel");
6046                 return -1;
6047         }
6048
6049         wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
6050                    "0x%llx",
6051                    (long long unsigned int) drv->remain_on_chan_cookie);
6052
6053         msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
6054         if (!msg ||
6055             nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
6056                 nlmsg_free(msg);
6057                 return -1;
6058         }
6059
6060         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6061         if (ret == 0)
6062                 return 0;
6063         wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
6064                    "%d (%s)", ret, strerror(-ret));
6065         return -1;
6066 }
6067
6068
6069 static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
6070 {
6071         struct wpa_driver_nl80211_data *drv = bss->drv;
6072
6073         if (!report) {
6074                 if (bss->nl_preq && drv->device_ap_sme &&
6075                     is_ap_interface(drv->nlmode) && !bss->in_deinit &&
6076                     !bss->static_ap) {
6077                         /*
6078                          * Do not disable Probe Request reporting that was
6079                          * enabled in nl80211_setup_ap().
6080                          */
6081                         wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
6082                                    "Probe Request reporting nl_preq=%p while "
6083                                    "in AP mode", bss->nl_preq);
6084                 } else if (bss->nl_preq) {
6085                         wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
6086                                    "reporting nl_preq=%p", bss->nl_preq);
6087                         nl80211_destroy_eloop_handle(&bss->nl_preq);
6088                 }
6089                 return 0;
6090         }
6091
6092         if (bss->nl_preq) {
6093                 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
6094                            "already on! nl_preq=%p", bss->nl_preq);
6095                 return 0;
6096         }
6097
6098         bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
6099         if (bss->nl_preq == NULL)
6100                 return -1;
6101         wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
6102                    "reporting nl_preq=%p", bss->nl_preq);
6103
6104         if (nl80211_register_frame(bss, bss->nl_preq,
6105                                    (WLAN_FC_TYPE_MGMT << 2) |
6106                                    (WLAN_FC_STYPE_PROBE_REQ << 4),
6107                                    NULL, 0) < 0)
6108                 goto out_err;
6109
6110         nl80211_register_eloop_read(&bss->nl_preq,
6111                                     wpa_driver_nl80211_event_receive,
6112                                     bss->nl_cb);
6113
6114         return 0;
6115
6116  out_err:
6117         nl_destroy_handles(&bss->nl_preq);
6118         return -1;
6119 }
6120
6121
6122 static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
6123                                      int ifindex, int disabled)
6124 {
6125         struct nl_msg *msg;
6126         struct nlattr *bands, *band;
6127         int ret;
6128
6129         wpa_printf(MSG_DEBUG,
6130                    "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
6131                    ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
6132                    "no NL80211_TXRATE_LEGACY constraint");
6133
6134         msg = nl80211_ifindex_msg(drv, ifindex, 0,
6135                                   NL80211_CMD_SET_TX_BITRATE_MASK);
6136         if (!msg)
6137                 return -1;
6138
6139         bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
6140         if (!bands)
6141                 goto fail;
6142
6143         /*
6144          * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
6145          * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
6146          * rates. All 5 GHz rates are left enabled.
6147          */
6148         band = nla_nest_start(msg, NL80211_BAND_2GHZ);
6149         if (!band ||
6150             (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
6151                                  "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
6152                 goto fail;
6153         nla_nest_end(msg, band);
6154
6155         nla_nest_end(msg, bands);
6156
6157         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6158         if (ret) {
6159                 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
6160                            "(%s)", ret, strerror(-ret));
6161         } else
6162                 drv->disabled_11b_rates = disabled;
6163
6164         return ret;
6165
6166 fail:
6167         nlmsg_free(msg);
6168         return -1;
6169 }
6170
6171
6172 static int wpa_driver_nl80211_deinit_ap(void *priv)
6173 {
6174         struct i802_bss *bss = priv;
6175         struct wpa_driver_nl80211_data *drv = bss->drv;
6176         if (!is_ap_interface(drv->nlmode))
6177                 return -1;
6178         wpa_driver_nl80211_del_beacon(drv);
6179         bss->beacon_set = 0;
6180
6181         /*
6182          * If the P2P GO interface was dynamically added, then it is
6183          * possible that the interface change to station is not possible.
6184          */
6185         if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
6186                 return 0;
6187
6188         return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
6189 }
6190
6191
6192 static int wpa_driver_nl80211_stop_ap(void *priv)
6193 {
6194         struct i802_bss *bss = priv;
6195         struct wpa_driver_nl80211_data *drv = bss->drv;
6196         if (!is_ap_interface(drv->nlmode))
6197                 return -1;
6198         wpa_driver_nl80211_del_beacon(drv);
6199         bss->beacon_set = 0;
6200         return 0;
6201 }
6202
6203
6204 static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
6205 {
6206         struct i802_bss *bss = priv;
6207         struct wpa_driver_nl80211_data *drv = bss->drv;
6208         if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
6209                 return -1;
6210
6211         /*
6212          * If the P2P Client interface was dynamically added, then it is
6213          * possible that the interface change to station is not possible.
6214          */
6215         if (bss->if_dynamic)
6216                 return 0;
6217
6218         return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
6219 }
6220
6221
6222 static void wpa_driver_nl80211_resume(void *priv)
6223 {
6224         struct i802_bss *bss = priv;
6225
6226         if (i802_set_iface_flags(bss, 1))
6227                 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
6228 }
6229
6230
6231 static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
6232 {
6233         struct i802_bss *bss = priv;
6234         struct wpa_driver_nl80211_data *drv = bss->drv;
6235         struct nl_msg *msg;
6236         struct nlattr *cqm;
6237
6238         wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
6239                    "hysteresis=%d", threshold, hysteresis);
6240
6241         if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
6242             !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
6243             nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
6244             nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
6245                 nlmsg_free(msg);
6246                 return -1;
6247         }
6248         nla_nest_end(msg, cqm);
6249
6250         return send_and_recv_msgs(drv, msg, NULL, NULL);
6251 }
6252
6253
6254 static int get_channel_width(struct nl_msg *msg, void *arg)
6255 {
6256         struct nlattr *tb[NL80211_ATTR_MAX + 1];
6257         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6258         struct wpa_signal_info *sig_change = arg;
6259
6260         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6261                   genlmsg_attrlen(gnlh, 0), NULL);
6262
6263         sig_change->center_frq1 = -1;
6264         sig_change->center_frq2 = -1;
6265         sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
6266
6267         if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
6268                 sig_change->chanwidth = convert2width(
6269                         nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
6270                 if (tb[NL80211_ATTR_CENTER_FREQ1])
6271                         sig_change->center_frq1 =
6272                                 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
6273                 if (tb[NL80211_ATTR_CENTER_FREQ2])
6274                         sig_change->center_frq2 =
6275                                 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
6276         }
6277
6278         return NL_SKIP;
6279 }
6280
6281
6282 static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
6283                                      struct wpa_signal_info *sig)
6284 {
6285         struct nl_msg *msg;
6286
6287         msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
6288         return send_and_recv_msgs(drv, msg, get_channel_width, sig);
6289 }
6290
6291
6292 static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
6293 {
6294         struct i802_bss *bss = priv;
6295         struct wpa_driver_nl80211_data *drv = bss->drv;
6296         int res;
6297
6298         os_memset(si, 0, sizeof(*si));
6299         res = nl80211_get_link_signal(drv, si);
6300         if (res != 0)
6301                 return res;
6302
6303         res = nl80211_get_channel_width(drv, si);
6304         if (res != 0)
6305                 return res;
6306
6307         return nl80211_get_link_noise(drv, si);
6308 }
6309
6310
6311 static int wpa_driver_nl80211_shared_freq(void *priv)
6312 {
6313         struct i802_bss *bss = priv;
6314         struct wpa_driver_nl80211_data *drv = bss->drv;
6315         struct wpa_driver_nl80211_data *driver;
6316         int freq = 0;
6317
6318         /*
6319          * If the same PHY is in connected state with some other interface,
6320          * then retrieve the assoc freq.
6321          */
6322         wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
6323                    drv->phyname);
6324
6325         dl_list_for_each(driver, &drv->global->interfaces,
6326                          struct wpa_driver_nl80211_data, list) {
6327                 if (drv == driver ||
6328                     os_strcmp(drv->phyname, driver->phyname) != 0 ||
6329                     !driver->associated)
6330                         continue;
6331
6332                 wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
6333                            MACSTR,
6334                            driver->phyname, driver->first_bss->ifname,
6335                            MAC2STR(driver->first_bss->addr));
6336                 if (is_ap_interface(driver->nlmode))
6337                         freq = driver->first_bss->freq;
6338                 else
6339                         freq = nl80211_get_assoc_freq(driver);
6340                 wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
6341                            drv->phyname, freq);
6342         }
6343
6344         if (!freq)
6345                 wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
6346                            "PHY (%s) in associated state", drv->phyname);
6347
6348         return freq;
6349 }
6350
6351
6352 static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
6353                               int encrypt)
6354 {
6355         struct i802_bss *bss = priv;
6356         return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
6357                                              0, 0, 0, 0);
6358 }
6359
6360
6361 static int nl80211_set_param(void *priv, const char *param)
6362 {
6363         wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
6364         if (param == NULL)
6365                 return 0;
6366
6367 #ifdef CONFIG_P2P
6368         if (os_strstr(param, "use_p2p_group_interface=1")) {
6369                 struct i802_bss *bss = priv;
6370                 struct wpa_driver_nl80211_data *drv = bss->drv;
6371
6372                 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
6373                            "interface");
6374                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
6375                 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
6376         }
6377 #endif /* CONFIG_P2P */
6378
6379         if (os_strstr(param, "use_monitor=1")) {
6380                 struct i802_bss *bss = priv;
6381                 struct wpa_driver_nl80211_data *drv = bss->drv;
6382                 drv->use_monitor = 1;
6383         }
6384
6385         if (os_strstr(param, "force_connect_cmd=1")) {
6386                 struct i802_bss *bss = priv;
6387                 struct wpa_driver_nl80211_data *drv = bss->drv;
6388                 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
6389                 drv->force_connect_cmd = 1;
6390         }
6391
6392         if (os_strstr(param, "no_offchannel_tx=1")) {
6393                 struct i802_bss *bss = priv;
6394                 struct wpa_driver_nl80211_data *drv = bss->drv;
6395                 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
6396                 drv->test_use_roc_tx = 1;
6397         }
6398
6399         return 0;
6400 }
6401
6402
6403 static void * nl80211_global_init(void)
6404 {
6405         struct nl80211_global *global;
6406         struct netlink_config *cfg;
6407
6408         global = os_zalloc(sizeof(*global));
6409         if (global == NULL)
6410                 return NULL;
6411         global->ioctl_sock = -1;
6412         dl_list_init(&global->interfaces);
6413         global->if_add_ifindex = -1;
6414
6415         cfg = os_zalloc(sizeof(*cfg));
6416         if (cfg == NULL)
6417                 goto err;
6418
6419         cfg->ctx = global;
6420         cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
6421         cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
6422         global->netlink = netlink_init(cfg);
6423         if (global->netlink == NULL) {
6424                 os_free(cfg);
6425                 goto err;
6426         }
6427
6428         if (wpa_driver_nl80211_init_nl_global(global) < 0)
6429                 goto err;
6430
6431         global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
6432         if (global->ioctl_sock < 0) {
6433                 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
6434                            strerror(errno));
6435                 goto err;
6436         }
6437
6438         return global;
6439
6440 err:
6441         nl80211_global_deinit(global);
6442         return NULL;
6443 }
6444
6445
6446 static void nl80211_global_deinit(void *priv)
6447 {
6448         struct nl80211_global *global = priv;
6449         if (global == NULL)
6450                 return;
6451         if (!dl_list_empty(&global->interfaces)) {
6452                 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
6453                            "nl80211_global_deinit",
6454                            dl_list_len(&global->interfaces));
6455         }
6456
6457         if (global->netlink)
6458                 netlink_deinit(global->netlink);
6459
6460         nl_destroy_handles(&global->nl);
6461
6462         if (global->nl_event)
6463                 nl80211_destroy_eloop_handle(&global->nl_event);
6464
6465         nl_cb_put(global->nl_cb);
6466
6467         if (global->ioctl_sock >= 0)
6468                 close(global->ioctl_sock);
6469
6470         os_free(global);
6471 }
6472
6473
6474 static const char * nl80211_get_radio_name(void *priv)
6475 {
6476         struct i802_bss *bss = priv;
6477         struct wpa_driver_nl80211_data *drv = bss->drv;
6478         return drv->phyname;
6479 }
6480
6481
6482 static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
6483                          const u8 *pmkid)
6484 {
6485         struct nl_msg *msg;
6486
6487         if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
6488             (pmkid && nla_put(msg, NL80211_ATTR_PMKID, 16, pmkid)) ||
6489             (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))) {
6490                 nlmsg_free(msg);
6491                 return -ENOBUFS;
6492         }
6493
6494         return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
6495 }
6496
6497
6498 static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6499 {
6500         struct i802_bss *bss = priv;
6501         wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
6502         return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
6503 }
6504
6505
6506 static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6507 {
6508         struct i802_bss *bss = priv;
6509         wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
6510                    MAC2STR(bssid));
6511         return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
6512 }
6513
6514
6515 static int nl80211_flush_pmkid(void *priv)
6516 {
6517         struct i802_bss *bss = priv;
6518         wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
6519         return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
6520 }
6521
6522
6523 static void clean_survey_results(struct survey_results *survey_results)
6524 {
6525         struct freq_survey *survey, *tmp;
6526
6527         if (dl_list_empty(&survey_results->survey_list))
6528                 return;
6529
6530         dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
6531                               struct freq_survey, list) {
6532                 dl_list_del(&survey->list);
6533                 os_free(survey);
6534         }
6535 }
6536
6537
6538 static void add_survey(struct nlattr **sinfo, u32 ifidx,
6539                        struct dl_list *survey_list)
6540 {
6541         struct freq_survey *survey;
6542
6543         survey = os_zalloc(sizeof(struct freq_survey));
6544         if  (!survey)
6545                 return;
6546
6547         survey->ifidx = ifidx;
6548         survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
6549         survey->filled = 0;
6550
6551         if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
6552                 survey->nf = (int8_t)
6553                         nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
6554                 survey->filled |= SURVEY_HAS_NF;
6555         }
6556
6557         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
6558                 survey->channel_time =
6559                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
6560                 survey->filled |= SURVEY_HAS_CHAN_TIME;
6561         }
6562
6563         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
6564                 survey->channel_time_busy =
6565                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
6566                 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
6567         }
6568
6569         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
6570                 survey->channel_time_rx =
6571                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
6572                 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
6573         }
6574
6575         if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
6576                 survey->channel_time_tx =
6577                         nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
6578                 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
6579         }
6580
6581         wpa_printf(MSG_DEBUG, "nl80211: Freq survey dump event (freq=%d MHz noise=%d channel_time=%ld busy_time=%ld tx_time=%ld rx_time=%ld filled=%04x)",
6582                    survey->freq,
6583                    survey->nf,
6584                    (unsigned long int) survey->channel_time,
6585                    (unsigned long int) survey->channel_time_busy,
6586                    (unsigned long int) survey->channel_time_tx,
6587                    (unsigned long int) survey->channel_time_rx,
6588                    survey->filled);
6589
6590         dl_list_add_tail(survey_list, &survey->list);
6591 }
6592
6593
6594 static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
6595                            unsigned int freq_filter)
6596 {
6597         if (!freq_filter)
6598                 return 1;
6599
6600         return freq_filter == surveyed_freq;
6601 }
6602
6603
6604 static int survey_handler(struct nl_msg *msg, void *arg)
6605 {
6606         struct nlattr *tb[NL80211_ATTR_MAX + 1];
6607         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6608         struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
6609         struct survey_results *survey_results;
6610         u32 surveyed_freq = 0;
6611         u32 ifidx;
6612
6613         static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
6614                 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
6615                 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
6616         };
6617
6618         survey_results = (struct survey_results *) arg;
6619
6620         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6621                   genlmsg_attrlen(gnlh, 0), NULL);
6622
6623         if (!tb[NL80211_ATTR_IFINDEX])
6624                 return NL_SKIP;
6625
6626         ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
6627
6628         if (!tb[NL80211_ATTR_SURVEY_INFO])
6629                 return NL_SKIP;
6630
6631         if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
6632                              tb[NL80211_ATTR_SURVEY_INFO],
6633                              survey_policy))
6634                 return NL_SKIP;
6635
6636         if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
6637                 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
6638                 return NL_SKIP;
6639         }
6640
6641         surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
6642
6643         if (!check_survey_ok(sinfo, surveyed_freq,
6644                              survey_results->freq_filter))
6645                 return NL_SKIP;
6646
6647         if (survey_results->freq_filter &&
6648             survey_results->freq_filter != surveyed_freq) {
6649                 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
6650                            surveyed_freq);
6651                 return NL_SKIP;
6652         }
6653
6654         add_survey(sinfo, ifidx, &survey_results->survey_list);
6655
6656         return NL_SKIP;
6657 }
6658
6659
6660 static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
6661 {
6662         struct i802_bss *bss = priv;
6663         struct wpa_driver_nl80211_data *drv = bss->drv;
6664         struct nl_msg *msg;
6665         int err;
6666         union wpa_event_data data;
6667         struct survey_results *survey_results;
6668
6669         os_memset(&data, 0, sizeof(data));
6670         survey_results = &data.survey_results;
6671
6672         dl_list_init(&survey_results->survey_list);
6673
6674         msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
6675         if (!msg)
6676                 return -ENOBUFS;
6677
6678         if (freq)
6679                 data.survey_results.freq_filter = freq;
6680
6681         do {
6682                 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
6683                 err = send_and_recv_msgs(drv, msg, survey_handler,
6684                                          survey_results);
6685         } while (err > 0);
6686
6687         if (err)
6688                 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
6689         else
6690                 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
6691
6692         clean_survey_results(survey_results);
6693         return err;
6694 }
6695
6696
6697 static void nl80211_set_rekey_info(void *priv, const u8 *kek, const u8 *kck,
6698                                    const u8 *replay_ctr)
6699 {
6700         struct i802_bss *bss = priv;
6701         struct wpa_driver_nl80211_data *drv = bss->drv;
6702         struct nlattr *replay_nested;
6703         struct nl_msg *msg;
6704
6705         if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
6706             !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
6707             nla_put(msg, NL80211_REKEY_DATA_KEK, NL80211_KEK_LEN, kek) ||
6708             nla_put(msg, NL80211_REKEY_DATA_KCK, NL80211_KCK_LEN, kck) ||
6709             nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
6710                     replay_ctr)) {
6711                 nlmsg_free(msg);
6712                 return;
6713         }
6714
6715         nla_nest_end(msg, replay_nested);
6716
6717         send_and_recv_msgs(drv, msg, NULL, NULL);
6718 }
6719
6720
6721 static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
6722                                     const u8 *addr, int qos)
6723 {
6724         /* send data frame to poll STA and check whether
6725          * this frame is ACKed */
6726         struct {
6727                 struct ieee80211_hdr hdr;
6728                 u16 qos_ctl;
6729         } STRUCT_PACKED nulldata;
6730         size_t size;
6731
6732         /* Send data frame to poll STA and check whether this frame is ACKed */
6733
6734         os_memset(&nulldata, 0, sizeof(nulldata));
6735
6736         if (qos) {
6737                 nulldata.hdr.frame_control =
6738                         IEEE80211_FC(WLAN_FC_TYPE_DATA,
6739                                      WLAN_FC_STYPE_QOS_NULL);
6740                 size = sizeof(nulldata);
6741         } else {
6742                 nulldata.hdr.frame_control =
6743                         IEEE80211_FC(WLAN_FC_TYPE_DATA,
6744                                      WLAN_FC_STYPE_NULLFUNC);
6745                 size = sizeof(struct ieee80211_hdr);
6746         }
6747
6748         nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
6749         os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6750         os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6751         os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6752
6753         if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
6754                                          0, 0) < 0)
6755                 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
6756                            "send poll frame");
6757 }
6758
6759 static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
6760                                 int qos)
6761 {
6762         struct i802_bss *bss = priv;
6763         struct wpa_driver_nl80211_data *drv = bss->drv;
6764         struct nl_msg *msg;
6765
6766         if (!drv->poll_command_supported) {
6767                 nl80211_send_null_frame(bss, own_addr, addr, qos);
6768                 return;
6769         }
6770
6771         if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
6772             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
6773                 nlmsg_free(msg);
6774                 return;
6775         }
6776
6777         send_and_recv_msgs(drv, msg, NULL, NULL);
6778 }
6779
6780
6781 static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
6782 {
6783         struct nl_msg *msg;
6784
6785         if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
6786             nla_put_u32(msg, NL80211_ATTR_PS_STATE,
6787                         enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
6788                 nlmsg_free(msg);
6789                 return -ENOBUFS;
6790         }
6791         return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
6792 }
6793
6794
6795 static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
6796                                      int ctwindow)
6797 {
6798         struct i802_bss *bss = priv;
6799
6800         wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
6801                    "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
6802
6803         if (opp_ps != -1 || ctwindow != -1) {
6804 #ifdef ANDROID_P2P
6805                 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
6806 #else /* ANDROID_P2P */
6807                 return -1; /* Not yet supported */
6808 #endif /* ANDROID_P2P */
6809         }
6810
6811         if (legacy_ps == -1)
6812                 return 0;
6813         if (legacy_ps != 0 && legacy_ps != 1)
6814                 return -1; /* Not yet supported */
6815
6816         return nl80211_set_power_save(bss, legacy_ps);
6817 }
6818
6819
6820 static int nl80211_start_radar_detection(void *priv,
6821                                          struct hostapd_freq_params *freq)
6822 {
6823         struct i802_bss *bss = priv;
6824         struct wpa_driver_nl80211_data *drv = bss->drv;
6825         struct nl_msg *msg;
6826         int ret;
6827
6828         wpa_printf(MSG_DEBUG, "nl80211: Start radar detection (CAC) %d MHz (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
6829                    freq->freq, freq->ht_enabled, freq->vht_enabled,
6830                    freq->bandwidth, freq->center_freq1, freq->center_freq2);
6831
6832         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
6833                 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
6834                            "detection");
6835                 return -1;
6836         }
6837
6838         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
6839             nl80211_put_freq_params(msg, freq) < 0) {
6840                 nlmsg_free(msg);
6841                 return -1;
6842         }
6843
6844         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6845         if (ret == 0)
6846                 return 0;
6847         wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
6848                    "%d (%s)", ret, strerror(-ret));
6849         return -1;
6850 }
6851
6852 #ifdef CONFIG_TDLS
6853
6854 static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
6855                                   u8 dialog_token, u16 status_code,
6856                                   u32 peer_capab, int initiator, const u8 *buf,
6857                                   size_t len)
6858 {
6859         struct i802_bss *bss = priv;
6860         struct wpa_driver_nl80211_data *drv = bss->drv;
6861         struct nl_msg *msg;
6862
6863         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
6864                 return -EOPNOTSUPP;
6865
6866         if (!dst)
6867                 return -EINVAL;
6868
6869         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
6870             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
6871             nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
6872             nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
6873             nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
6874                 goto fail;
6875         if (peer_capab) {
6876                 /*
6877                  * The internal enum tdls_peer_capability definition is
6878                  * currently identical with the nl80211 enum
6879                  * nl80211_tdls_peer_capability, so no conversion is needed
6880                  * here.
6881                  */
6882                 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
6883                                 peer_capab))
6884                         goto fail;
6885         }
6886         if ((initiator &&
6887              nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
6888             nla_put(msg, NL80211_ATTR_IE, len, buf))
6889                 goto fail;
6890
6891         return send_and_recv_msgs(drv, msg, NULL, NULL);
6892
6893 fail:
6894         nlmsg_free(msg);
6895         return -ENOBUFS;
6896 }
6897
6898
6899 static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
6900 {
6901         struct i802_bss *bss = priv;
6902         struct wpa_driver_nl80211_data *drv = bss->drv;
6903         struct nl_msg *msg;
6904         enum nl80211_tdls_operation nl80211_oper;
6905
6906         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
6907                 return -EOPNOTSUPP;
6908
6909         switch (oper) {
6910         case TDLS_DISCOVERY_REQ:
6911                 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
6912                 break;
6913         case TDLS_SETUP:
6914                 nl80211_oper = NL80211_TDLS_SETUP;
6915                 break;
6916         case TDLS_TEARDOWN:
6917                 nl80211_oper = NL80211_TDLS_TEARDOWN;
6918                 break;
6919         case TDLS_ENABLE_LINK:
6920                 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
6921                 break;
6922         case TDLS_DISABLE_LINK:
6923                 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
6924                 break;
6925         case TDLS_ENABLE:
6926                 return 0;
6927         case TDLS_DISABLE:
6928                 return 0;
6929         default:
6930                 return -EINVAL;
6931         }
6932
6933         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
6934             nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
6935             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
6936                 nlmsg_free(msg);
6937                 return -ENOBUFS;
6938         }
6939
6940         return send_and_recv_msgs(drv, msg, NULL, NULL);
6941 }
6942
6943 #endif /* CONFIG TDLS */
6944
6945
6946 static int driver_nl80211_set_key(const char *ifname, void *priv,
6947                                   enum wpa_alg alg, const u8 *addr,
6948                                   int key_idx, int set_tx,
6949                                   const u8 *seq, size_t seq_len,
6950                                   const u8 *key, size_t key_len)
6951 {
6952         struct i802_bss *bss = priv;
6953         return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
6954                                           set_tx, seq, seq_len, key, key_len);
6955 }
6956
6957
6958 static int driver_nl80211_scan2(void *priv,
6959                                 struct wpa_driver_scan_params *params)
6960 {
6961         struct i802_bss *bss = priv;
6962         return wpa_driver_nl80211_scan(bss, params);
6963 }
6964
6965
6966 static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
6967                                          int reason_code)
6968 {
6969         struct i802_bss *bss = priv;
6970         return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
6971 }
6972
6973
6974 static int driver_nl80211_authenticate(void *priv,
6975                                        struct wpa_driver_auth_params *params)
6976 {
6977         struct i802_bss *bss = priv;
6978         return wpa_driver_nl80211_authenticate(bss, params);
6979 }
6980
6981
6982 static void driver_nl80211_deinit(void *priv)
6983 {
6984         struct i802_bss *bss = priv;
6985         wpa_driver_nl80211_deinit(bss);
6986 }
6987
6988
6989 static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
6990                                     const char *ifname)
6991 {
6992         struct i802_bss *bss = priv;
6993         return wpa_driver_nl80211_if_remove(bss, type, ifname);
6994 }
6995
6996
6997 static int driver_nl80211_send_mlme(void *priv, const u8 *data,
6998                                     size_t data_len, int noack)
6999 {
7000         struct i802_bss *bss = priv;
7001         return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
7002                                             0, 0, 0, 0);
7003 }
7004
7005
7006 static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
7007 {
7008         struct i802_bss *bss = priv;
7009         return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
7010 }
7011
7012
7013 static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
7014                                        const char *ifname, int vlan_id)
7015 {
7016         struct i802_bss *bss = priv;
7017         return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
7018 }
7019
7020
7021 static int driver_nl80211_read_sta_data(void *priv,
7022                                         struct hostap_sta_driver_data *data,
7023                                         const u8 *addr)
7024 {
7025         struct i802_bss *bss = priv;
7026         return i802_read_sta_data(bss, data, addr);
7027 }
7028
7029
7030 static int driver_nl80211_send_action(void *priv, unsigned int freq,
7031                                       unsigned int wait_time,
7032                                       const u8 *dst, const u8 *src,
7033                                       const u8 *bssid,
7034                                       const u8 *data, size_t data_len,
7035                                       int no_cck)
7036 {
7037         struct i802_bss *bss = priv;
7038         return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
7039                                               bssid, data, data_len, no_cck);
7040 }
7041
7042
7043 static int driver_nl80211_probe_req_report(void *priv, int report)
7044 {
7045         struct i802_bss *bss = priv;
7046         return wpa_driver_nl80211_probe_req_report(bss, report);
7047 }
7048
7049
7050 static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
7051                                             const u8 *ies, size_t ies_len)
7052 {
7053         int ret;
7054         struct nl_msg *msg;
7055         struct i802_bss *bss = priv;
7056         struct wpa_driver_nl80211_data *drv = bss->drv;
7057         u16 mdid = WPA_GET_LE16(md);
7058
7059         wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
7060         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
7061             nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
7062             nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
7063                 nlmsg_free(msg);
7064                 return -ENOBUFS;
7065         }
7066
7067         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7068         if (ret) {
7069                 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
7070                            "err=%d (%s)", ret, strerror(-ret));
7071         }
7072
7073         return ret;
7074 }
7075
7076
7077 const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
7078 {
7079         struct i802_bss *bss = priv;
7080         struct wpa_driver_nl80211_data *drv = bss->drv;
7081
7082         if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
7083                 return NULL;
7084
7085         return bss->addr;
7086 }
7087
7088
7089 static const char * scan_state_str(enum scan_states scan_state)
7090 {
7091         switch (scan_state) {
7092         case NO_SCAN:
7093                 return "NO_SCAN";
7094         case SCAN_REQUESTED:
7095                 return "SCAN_REQUESTED";
7096         case SCAN_STARTED:
7097                 return "SCAN_STARTED";
7098         case SCAN_COMPLETED:
7099                 return "SCAN_COMPLETED";
7100         case SCAN_ABORTED:
7101                 return "SCAN_ABORTED";
7102         case SCHED_SCAN_STARTED:
7103                 return "SCHED_SCAN_STARTED";
7104         case SCHED_SCAN_STOPPED:
7105                 return "SCHED_SCAN_STOPPED";
7106         case SCHED_SCAN_RESULTS:
7107                 return "SCHED_SCAN_RESULTS";
7108         }
7109
7110         return "??";
7111 }
7112
7113
7114 static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
7115 {
7116         struct i802_bss *bss = priv;
7117         struct wpa_driver_nl80211_data *drv = bss->drv;
7118         int res;
7119         char *pos, *end;
7120
7121         pos = buf;
7122         end = buf + buflen;
7123
7124         res = os_snprintf(pos, end - pos,
7125                           "ifindex=%d\n"
7126                           "ifname=%s\n"
7127                           "brname=%s\n"
7128                           "addr=" MACSTR "\n"
7129                           "freq=%d\n"
7130                           "%s%s%s%s%s",
7131                           bss->ifindex,
7132                           bss->ifname,
7133                           bss->brname,
7134                           MAC2STR(bss->addr),
7135                           bss->freq,
7136                           bss->beacon_set ? "beacon_set=1\n" : "",
7137                           bss->added_if_into_bridge ?
7138                           "added_if_into_bridge=1\n" : "",
7139                           bss->added_bridge ? "added_bridge=1\n" : "",
7140                           bss->in_deinit ? "in_deinit=1\n" : "",
7141                           bss->if_dynamic ? "if_dynamic=1\n" : "");
7142         if (os_snprintf_error(end - pos, res))
7143                 return pos - buf;
7144         pos += res;
7145
7146         if (bss->wdev_id_set) {
7147                 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
7148                                   (unsigned long long) bss->wdev_id);
7149                 if (os_snprintf_error(end - pos, res))
7150                         return pos - buf;
7151                 pos += res;
7152         }
7153
7154         res = os_snprintf(pos, end - pos,
7155                           "phyname=%s\n"
7156                           "perm_addr=" MACSTR "\n"
7157                           "drv_ifindex=%d\n"
7158                           "operstate=%d\n"
7159                           "scan_state=%s\n"
7160                           "auth_bssid=" MACSTR "\n"
7161                           "auth_attempt_bssid=" MACSTR "\n"
7162                           "bssid=" MACSTR "\n"
7163                           "prev_bssid=" MACSTR "\n"
7164                           "associated=%d\n"
7165                           "assoc_freq=%u\n"
7166                           "monitor_sock=%d\n"
7167                           "monitor_ifidx=%d\n"
7168                           "monitor_refcount=%d\n"
7169                           "last_mgmt_freq=%u\n"
7170                           "eapol_tx_sock=%d\n"
7171                           "%s%s%s%s%s%s%s%s%s%s%s%s%s",
7172                           drv->phyname,
7173                           MAC2STR(drv->perm_addr),
7174                           drv->ifindex,
7175                           drv->operstate,
7176                           scan_state_str(drv->scan_state),
7177                           MAC2STR(drv->auth_bssid),
7178                           MAC2STR(drv->auth_attempt_bssid),
7179                           MAC2STR(drv->bssid),
7180                           MAC2STR(drv->prev_bssid),
7181                           drv->associated,
7182                           drv->assoc_freq,
7183                           drv->monitor_sock,
7184                           drv->monitor_ifidx,
7185                           drv->monitor_refcount,
7186                           drv->last_mgmt_freq,
7187                           drv->eapol_tx_sock,
7188                           drv->ignore_if_down_event ?
7189                           "ignore_if_down_event=1\n" : "",
7190                           drv->scan_complete_events ?
7191                           "scan_complete_events=1\n" : "",
7192                           drv->disabled_11b_rates ?
7193                           "disabled_11b_rates=1\n" : "",
7194                           drv->pending_remain_on_chan ?
7195                           "pending_remain_on_chan=1\n" : "",
7196                           drv->in_interface_list ? "in_interface_list=1\n" : "",
7197                           drv->device_ap_sme ? "device_ap_sme=1\n" : "",
7198                           drv->poll_command_supported ?
7199                           "poll_command_supported=1\n" : "",
7200                           drv->data_tx_status ? "data_tx_status=1\n" : "",
7201                           drv->scan_for_auth ? "scan_for_auth=1\n" : "",
7202                           drv->retry_auth ? "retry_auth=1\n" : "",
7203                           drv->use_monitor ? "use_monitor=1\n" : "",
7204                           drv->ignore_next_local_disconnect ?
7205                           "ignore_next_local_disconnect=1\n" : "",
7206                           drv->ignore_next_local_deauth ?
7207                           "ignore_next_local_deauth=1\n" : "");
7208         if (os_snprintf_error(end - pos, res))
7209                 return pos - buf;
7210         pos += res;
7211
7212         if (drv->has_capability) {
7213                 res = os_snprintf(pos, end - pos,
7214                                   "capa.key_mgmt=0x%x\n"
7215                                   "capa.enc=0x%x\n"
7216                                   "capa.auth=0x%x\n"
7217                                   "capa.flags=0x%llx\n"
7218                                   "capa.max_scan_ssids=%d\n"
7219                                   "capa.max_sched_scan_ssids=%d\n"
7220                                   "capa.sched_scan_supported=%d\n"
7221                                   "capa.max_match_sets=%d\n"
7222                                   "capa.max_remain_on_chan=%u\n"
7223                                   "capa.max_stations=%u\n"
7224                                   "capa.probe_resp_offloads=0x%x\n"
7225                                   "capa.max_acl_mac_addrs=%u\n"
7226                                   "capa.num_multichan_concurrent=%u\n",
7227                                   drv->capa.key_mgmt,
7228                                   drv->capa.enc,
7229                                   drv->capa.auth,
7230                                   (unsigned long long) drv->capa.flags,
7231                                   drv->capa.max_scan_ssids,
7232                                   drv->capa.max_sched_scan_ssids,
7233                                   drv->capa.sched_scan_supported,
7234                                   drv->capa.max_match_sets,
7235                                   drv->capa.max_remain_on_chan,
7236                                   drv->capa.max_stations,
7237                                   drv->capa.probe_resp_offloads,
7238                                   drv->capa.max_acl_mac_addrs,
7239                                   drv->capa.num_multichan_concurrent);
7240                 if (os_snprintf_error(end - pos, res))
7241                         return pos - buf;
7242                 pos += res;
7243         }
7244
7245         return pos - buf;
7246 }
7247
7248
7249 static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
7250 {
7251         if ((settings->head &&
7252              nla_put(msg, NL80211_ATTR_BEACON_HEAD,
7253                      settings->head_len, settings->head)) ||
7254             (settings->tail &&
7255              nla_put(msg, NL80211_ATTR_BEACON_TAIL,
7256                      settings->tail_len, settings->tail)) ||
7257             (settings->beacon_ies &&
7258              nla_put(msg, NL80211_ATTR_IE,
7259                      settings->beacon_ies_len, settings->beacon_ies)) ||
7260             (settings->proberesp_ies &&
7261              nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
7262                      settings->proberesp_ies_len, settings->proberesp_ies)) ||
7263             (settings->assocresp_ies &&
7264              nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
7265                      settings->assocresp_ies_len, settings->assocresp_ies)) ||
7266             (settings->probe_resp &&
7267              nla_put(msg, NL80211_ATTR_PROBE_RESP,
7268                      settings->probe_resp_len, settings->probe_resp)))
7269                 return -ENOBUFS;
7270
7271         return 0;
7272 }
7273
7274
7275 static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
7276 {
7277         struct nl_msg *msg;
7278         struct i802_bss *bss = priv;
7279         struct wpa_driver_nl80211_data *drv = bss->drv;
7280         struct nlattr *beacon_csa;
7281         int ret = -ENOBUFS;
7282
7283         wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
7284                    settings->cs_count, settings->block_tx,
7285                    settings->freq_params.freq, settings->freq_params.bandwidth,
7286                    settings->freq_params.center_freq1,
7287                    settings->freq_params.center_freq2);
7288
7289         if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
7290                 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
7291                 return -EOPNOTSUPP;
7292         }
7293
7294         if ((drv->nlmode != NL80211_IFTYPE_AP) &&
7295             (drv->nlmode != NL80211_IFTYPE_P2P_GO))
7296                 return -EOPNOTSUPP;
7297
7298         /* check settings validity */
7299         if (!settings->beacon_csa.tail ||
7300             ((settings->beacon_csa.tail_len <=
7301               settings->counter_offset_beacon) ||
7302              (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
7303               settings->cs_count)))
7304                 return -EINVAL;
7305
7306         if (settings->beacon_csa.probe_resp &&
7307             ((settings->beacon_csa.probe_resp_len <=
7308               settings->counter_offset_presp) ||
7309              (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
7310               settings->cs_count)))
7311                 return -EINVAL;
7312
7313         if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
7314             nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
7315                         settings->cs_count) ||
7316             (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
7317             (settings->block_tx &&
7318              nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
7319                 goto error;
7320
7321         /* beacon_after params */
7322         ret = set_beacon_data(msg, &settings->beacon_after);
7323         if (ret)
7324                 goto error;
7325
7326         /* beacon_csa params */
7327         beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
7328         if (!beacon_csa)
7329                 goto fail;
7330
7331         ret = set_beacon_data(msg, &settings->beacon_csa);
7332         if (ret)
7333                 goto error;
7334
7335         if (nla_put_u16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
7336                         settings->counter_offset_beacon) ||
7337             (settings->beacon_csa.probe_resp &&
7338              nla_put_u16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
7339                          settings->counter_offset_presp)))
7340                 goto fail;
7341
7342         nla_nest_end(msg, beacon_csa);
7343         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7344         if (ret) {
7345                 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
7346                            ret, strerror(-ret));
7347         }
7348         return ret;
7349
7350 fail:
7351         ret = -ENOBUFS;
7352 error:
7353         nlmsg_free(msg);
7354         wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
7355         return ret;
7356 }
7357
7358
7359 static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
7360                           u8 user_priority, u16 admitted_time)
7361 {
7362         struct i802_bss *bss = priv;
7363         struct wpa_driver_nl80211_data *drv = bss->drv;
7364         struct nl_msg *msg;
7365         int ret;
7366
7367         wpa_printf(MSG_DEBUG,
7368                    "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
7369                    tsid, admitted_time, user_priority);
7370
7371         if (!is_sta_interface(drv->nlmode))
7372                 return -ENOTSUP;
7373
7374         msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
7375         if (!msg ||
7376             nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
7377             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7378             nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
7379             nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
7380                 nlmsg_free(msg);
7381                 return -ENOBUFS;
7382         }
7383
7384         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7385         if (ret)
7386                 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
7387                            ret, strerror(-ret));
7388         return ret;
7389 }
7390
7391
7392 static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
7393 {
7394         struct i802_bss *bss = priv;
7395         struct wpa_driver_nl80211_data *drv = bss->drv;
7396         struct nl_msg *msg;
7397         int ret;
7398
7399         wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
7400
7401         if (!is_sta_interface(drv->nlmode))
7402                 return -ENOTSUP;
7403
7404         if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
7405             nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
7406             nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7407                 nlmsg_free(msg);
7408                 return -ENOBUFS;
7409         }
7410
7411         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7412         if (ret)
7413                 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
7414                            ret, strerror(-ret));
7415         return ret;
7416 }
7417
7418
7419 #ifdef CONFIG_TESTING_OPTIONS
7420 static int cmd_reply_handler(struct nl_msg *msg, void *arg)
7421 {
7422         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7423         struct wpabuf *buf = arg;
7424
7425         if (!buf)
7426                 return NL_SKIP;
7427
7428         if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
7429                 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
7430                 return NL_SKIP;
7431         }
7432
7433         wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
7434                         genlmsg_attrlen(gnlh, 0));
7435
7436         return NL_SKIP;
7437 }
7438 #endif /* CONFIG_TESTING_OPTIONS */
7439
7440
7441 static int vendor_reply_handler(struct nl_msg *msg, void *arg)
7442 {
7443         struct nlattr *tb[NL80211_ATTR_MAX + 1];
7444         struct nlattr *nl_vendor_reply, *nl;
7445         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7446         struct wpabuf *buf = arg;
7447         int rem;
7448
7449         if (!buf)
7450                 return NL_SKIP;
7451
7452         nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7453                   genlmsg_attrlen(gnlh, 0), NULL);
7454         nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
7455
7456         if (!nl_vendor_reply)
7457                 return NL_SKIP;
7458
7459         if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
7460                 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
7461                 return NL_SKIP;
7462         }
7463
7464         nla_for_each_nested(nl, nl_vendor_reply, rem) {
7465                 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
7466         }
7467
7468         return NL_SKIP;
7469 }
7470
7471
7472 static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
7473                               unsigned int subcmd, const u8 *data,
7474                               size_t data_len, struct wpabuf *buf)
7475 {
7476         struct i802_bss *bss = priv;
7477         struct wpa_driver_nl80211_data *drv = bss->drv;
7478         struct nl_msg *msg;
7479         int ret;
7480
7481 #ifdef CONFIG_TESTING_OPTIONS
7482         if (vendor_id == 0xffffffff) {
7483                 msg = nlmsg_alloc();
7484                 if (!msg)
7485                         return -ENOMEM;
7486
7487                 nl80211_cmd(drv, msg, 0, subcmd);
7488                 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
7489                     0)
7490                         goto fail;
7491                 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
7492                 if (ret)
7493                         wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
7494                                    ret);
7495                 return ret;
7496         }
7497 #endif /* CONFIG_TESTING_OPTIONS */
7498
7499         if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
7500             nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
7501             nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
7502             (data &&
7503              nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
7504                 goto fail;
7505
7506         ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
7507         if (ret)
7508                 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
7509                            ret);
7510         return ret;
7511
7512 fail:
7513         nlmsg_free(msg);
7514         return -ENOBUFS;
7515 }
7516
7517
7518 static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
7519                                u8 qos_map_set_len)
7520 {
7521         struct i802_bss *bss = priv;
7522         struct wpa_driver_nl80211_data *drv = bss->drv;
7523         struct nl_msg *msg;
7524         int ret;
7525
7526         wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
7527                     qos_map_set, qos_map_set_len);
7528
7529         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
7530             nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
7531                 nlmsg_free(msg);
7532                 return -ENOBUFS;
7533         }
7534
7535         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7536         if (ret)
7537                 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
7538
7539         return ret;
7540 }
7541
7542
7543 static int nl80211_set_wowlan(void *priv,
7544                               const struct wowlan_triggers *triggers)
7545 {
7546         struct i802_bss *bss = priv;
7547         struct wpa_driver_nl80211_data *drv = bss->drv;
7548         struct nl_msg *msg;
7549         struct nlattr *wowlan_triggers;
7550         int ret;
7551
7552         wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
7553
7554         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WOWLAN)) ||
7555             !(wowlan_triggers = nla_nest_start(msg,
7556                                                NL80211_ATTR_WOWLAN_TRIGGERS)) ||
7557             (triggers->any &&
7558              nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7559             (triggers->disconnect &&
7560              nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7561             (triggers->magic_pkt &&
7562              nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7563             (triggers->gtk_rekey_failure &&
7564              nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7565             (triggers->eap_identity_req &&
7566              nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7567             (triggers->four_way_handshake &&
7568              nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7569             (triggers->rfkill_release &&
7570              nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
7571                 nlmsg_free(msg);
7572                 return -ENOBUFS;
7573         }
7574
7575         nla_nest_end(msg, wowlan_triggers);
7576
7577         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7578         if (ret)
7579                 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
7580
7581         return ret;
7582 }
7583
7584
7585 static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
7586 {
7587         struct i802_bss *bss = priv;
7588         struct wpa_driver_nl80211_data *drv = bss->drv;
7589         struct nl_msg *msg;
7590         struct nlattr *params;
7591
7592         wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
7593
7594         if (!drv->roaming_vendor_cmd_avail) {
7595                 wpa_printf(MSG_DEBUG,
7596                            "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
7597                 return -1;
7598         }
7599
7600         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
7601             nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
7602             nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
7603                         QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
7604             !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
7605             nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
7606                         allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
7607                         QCA_ROAMING_NOT_ALLOWED) ||
7608             (bssid &&
7609              nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
7610                 nlmsg_free(msg);
7611                 return -1;
7612         }
7613         nla_nest_end(msg, params);
7614
7615         return send_and_recv_msgs(drv, msg, NULL, NULL);
7616 }
7617
7618
7619 static int nl80211_set_mac_addr(void *priv, const u8 *addr)
7620 {
7621         struct i802_bss *bss = priv;
7622         struct wpa_driver_nl80211_data *drv = bss->drv;
7623         int new_addr = addr != NULL;
7624
7625         if (!addr)
7626                 addr = drv->perm_addr;
7627
7628         if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
7629                 return -1;
7630
7631         if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
7632         {
7633                 wpa_printf(MSG_DEBUG,
7634                            "nl80211: failed to set_mac_addr for %s to " MACSTR,
7635                            bss->ifname, MAC2STR(addr));
7636                 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
7637                                           1) < 0) {
7638                         wpa_printf(MSG_DEBUG,
7639                                    "nl80211: Could not restore interface UP after failed set_mac_addr");
7640                 }
7641                 return -1;
7642         }
7643
7644         wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
7645                    bss->ifname, MAC2STR(addr));
7646         drv->addr_changed = new_addr;
7647         os_memcpy(bss->addr, addr, ETH_ALEN);
7648
7649         if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
7650         {
7651                 wpa_printf(MSG_DEBUG,
7652                            "nl80211: Could not restore interface UP after set_mac_addr");
7653         }
7654
7655         return 0;
7656 }
7657
7658
7659 #ifdef CONFIG_MESH
7660
7661 static int wpa_driver_nl80211_init_mesh(void *priv)
7662 {
7663         if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
7664                 wpa_printf(MSG_INFO,
7665                            "nl80211: Failed to set interface into mesh mode");
7666                 return -1;
7667         }
7668         return 0;
7669 }
7670
7671
7672 static int
7673 wpa_driver_nl80211_join_mesh(void *priv,
7674                              struct wpa_driver_mesh_join_params *params)
7675 {
7676         struct i802_bss *bss = priv;
7677         struct wpa_driver_nl80211_data *drv = bss->drv;
7678         struct nl_msg *msg;
7679         struct nlattr *container;
7680         int ret = 0;
7681
7682         wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
7683         msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
7684         if (!msg)
7685                 goto fail;
7686         if (params->freq) {
7687                 wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
7688                 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
7689                         goto fail;
7690         }
7691
7692         if (params->ht_mode) {
7693                 unsigned int ht_value;
7694                 char *ht_mode = "";
7695
7696                 switch (params->ht_mode) {
7697                 default:
7698                 case CHAN_NO_HT:
7699                         ht_value = NL80211_CHAN_NO_HT;
7700                         ht_mode = "NOHT";
7701                         break;
7702                 case CHAN_HT20:
7703                         ht_value = NL80211_CHAN_HT20;
7704                         ht_mode = "HT20";
7705                         break;
7706                 case CHAN_HT40PLUS:
7707                         ht_value = NL80211_CHAN_HT40PLUS;
7708                         ht_mode = "HT40+";
7709                         break;
7710                 case CHAN_HT40MINUS:
7711                         ht_value = NL80211_CHAN_HT40MINUS;
7712                         ht_mode = "HT40-";
7713                         break;
7714                 }
7715                 wpa_printf(MSG_DEBUG, "  * ht_mode=%s", ht_mode);
7716                 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ht_value))
7717                         goto fail;
7718         }
7719
7720         if (params->basic_rates) {
7721                 u8 rates[NL80211_MAX_SUPP_RATES];
7722                 u8 rates_len = 0;
7723                 int i;
7724
7725                 for (i = 0; i < NL80211_MAX_SUPP_RATES; i++) {
7726                         if (params->basic_rates[i] < 0)
7727                                 break;
7728                         rates[rates_len++] = params->basic_rates[i] / 5;
7729                 }
7730
7731                 if (nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len,
7732                             rates))
7733                         goto fail;
7734         }
7735
7736         if (params->meshid) {
7737                 wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
7738                                   params->meshid, params->meshid_len);
7739                 if (nla_put(msg, NL80211_ATTR_MESH_ID, params->meshid_len,
7740                             params->meshid))
7741                         goto fail;
7742         }
7743
7744         wpa_printf(MSG_DEBUG, "  * flags=%08X", params->flags);
7745
7746         container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
7747         if (!container)
7748                 goto fail;
7749
7750         if (params->ies) {
7751                 wpa_hexdump(MSG_DEBUG, "  * IEs", params->ies, params->ie_len);
7752                 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
7753                             params->ies))
7754                         goto fail;
7755         }
7756         /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
7757         if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
7758                 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
7759                     nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
7760                         goto fail;
7761         }
7762         if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
7763             nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
7764                 goto fail;
7765         if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
7766             nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
7767                 goto fail;
7768         nla_nest_end(msg, container);
7769
7770         container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
7771         if (!container)
7772                 goto fail;
7773
7774         if (!(params->conf.flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
7775             nla_put_u32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS, 0))
7776                 goto fail;
7777         nla_nest_end(msg, container);
7778
7779         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7780         msg = NULL;
7781         if (ret) {
7782                 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
7783                            ret, strerror(-ret));
7784                 goto fail;
7785         }
7786         ret = 0;
7787         bss->freq = params->freq;
7788         wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
7789
7790 fail:
7791         nlmsg_free(msg);
7792         return ret;
7793 }
7794
7795
7796 static int wpa_driver_nl80211_leave_mesh(void *priv)
7797 {
7798         struct i802_bss *bss = priv;
7799         struct wpa_driver_nl80211_data *drv = bss->drv;
7800         struct nl_msg *msg;
7801         int ret;
7802
7803         wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
7804         msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
7805         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7806         if (ret) {
7807                 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
7808                            ret, strerror(-ret));
7809         } else {
7810                 wpa_printf(MSG_DEBUG,
7811                            "nl80211: mesh leave request send successfully");
7812         }
7813
7814         if (wpa_driver_nl80211_set_mode(drv->first_bss,
7815                                         NL80211_IFTYPE_STATION)) {
7816                 wpa_printf(MSG_INFO,
7817                            "nl80211: Failed to set interface into station mode");
7818         }
7819         return ret;
7820 }
7821
7822 #endif /* CONFIG_MESH */
7823
7824
7825 static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
7826                                       const u8 *ipaddr, int prefixlen,
7827                                       const u8 *addr)
7828 {
7829 #ifdef CONFIG_LIBNL3_ROUTE
7830         struct i802_bss *bss = priv;
7831         struct wpa_driver_nl80211_data *drv = bss->drv;
7832         struct rtnl_neigh *rn;
7833         struct nl_addr *nl_ipaddr = NULL;
7834         struct nl_addr *nl_lladdr = NULL;
7835         int family, addrsize;
7836         int res;
7837
7838         if (!ipaddr || prefixlen == 0 || !addr)
7839                 return -EINVAL;
7840
7841         if (bss->br_ifindex == 0) {
7842                 wpa_printf(MSG_DEBUG,
7843                            "nl80211: bridge must be set before adding an ip neigh to it");
7844                 return -1;
7845         }
7846
7847         if (!drv->rtnl_sk) {
7848                 wpa_printf(MSG_DEBUG,
7849                            "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
7850                 return -1;
7851         }
7852
7853         if (version == 4) {
7854                 family = AF_INET;
7855                 addrsize = 4;
7856         } else if (version == 6) {
7857                 family = AF_INET6;
7858                 addrsize = 16;
7859         } else {
7860                 return -EINVAL;
7861         }
7862
7863         rn = rtnl_neigh_alloc();
7864         if (rn == NULL)
7865                 return -ENOMEM;
7866
7867         /* set the destination ip address for neigh */
7868         nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
7869         if (nl_ipaddr == NULL) {
7870                 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
7871                 res = -ENOMEM;
7872                 goto errout;
7873         }
7874         nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
7875         res = rtnl_neigh_set_dst(rn, nl_ipaddr);
7876         if (res) {
7877                 wpa_printf(MSG_DEBUG,
7878                            "nl80211: neigh set destination addr failed");
7879                 goto errout;
7880         }
7881
7882         /* set the corresponding lladdr for neigh */
7883         nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
7884         if (nl_lladdr == NULL) {
7885                 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
7886                 res = -ENOMEM;
7887                 goto errout;
7888         }
7889         rtnl_neigh_set_lladdr(rn, nl_lladdr);
7890
7891         rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
7892         rtnl_neigh_set_state(rn, NUD_PERMANENT);
7893
7894         res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
7895         if (res) {
7896                 wpa_printf(MSG_DEBUG,
7897                            "nl80211: Adding bridge ip neigh failed: %s",
7898                            strerror(errno));
7899         }
7900 errout:
7901         if (nl_lladdr)
7902                 nl_addr_put(nl_lladdr);
7903         if (nl_ipaddr)
7904                 nl_addr_put(nl_ipaddr);
7905         if (rn)
7906                 rtnl_neigh_put(rn);
7907         return res;
7908 #else /* CONFIG_LIBNL3_ROUTE */
7909         return -1;
7910 #endif /* CONFIG_LIBNL3_ROUTE */
7911 }
7912
7913
7914 static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
7915                                          const u8 *ipaddr)
7916 {
7917 #ifdef CONFIG_LIBNL3_ROUTE
7918         struct i802_bss *bss = priv;
7919         struct wpa_driver_nl80211_data *drv = bss->drv;
7920         struct rtnl_neigh *rn;
7921         struct nl_addr *nl_ipaddr;
7922         int family, addrsize;
7923         int res;
7924
7925         if (!ipaddr)
7926                 return -EINVAL;
7927
7928         if (version == 4) {
7929                 family = AF_INET;
7930                 addrsize = 4;
7931         } else if (version == 6) {
7932                 family = AF_INET6;
7933                 addrsize = 16;
7934         } else {
7935                 return -EINVAL;
7936         }
7937
7938         if (bss->br_ifindex == 0) {
7939                 wpa_printf(MSG_DEBUG,
7940                            "nl80211: bridge must be set to delete an ip neigh");
7941                 return -1;
7942         }
7943
7944         if (!drv->rtnl_sk) {
7945                 wpa_printf(MSG_DEBUG,
7946                            "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
7947                 return -1;
7948         }
7949
7950         rn = rtnl_neigh_alloc();
7951         if (rn == NULL)
7952                 return -ENOMEM;
7953
7954         /* set the destination ip address for neigh */
7955         nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
7956         if (nl_ipaddr == NULL) {
7957                 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
7958                 res = -ENOMEM;
7959                 goto errout;
7960         }
7961         res = rtnl_neigh_set_dst(rn, nl_ipaddr);
7962         if (res) {
7963                 wpa_printf(MSG_DEBUG,
7964                            "nl80211: neigh set destination addr failed");
7965                 goto errout;
7966         }
7967
7968         rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
7969
7970         res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
7971         if (res) {
7972                 wpa_printf(MSG_DEBUG,
7973                            "nl80211: Deleting bridge ip neigh failed: %s",
7974                            strerror(errno));
7975         }
7976 errout:
7977         if (nl_ipaddr)
7978                 nl_addr_put(nl_ipaddr);
7979         if (rn)
7980                 rtnl_neigh_put(rn);
7981         return res;
7982 #else /* CONFIG_LIBNL3_ROUTE */
7983         return -1;
7984 #endif /* CONFIG_LIBNL3_ROUTE */
7985 }
7986
7987
7988 static int linux_write_system_file(const char *path, unsigned int val)
7989 {
7990         char buf[50];
7991         int fd, len;
7992
7993         len = os_snprintf(buf, sizeof(buf), "%u\n", val);
7994         if (os_snprintf_error(sizeof(buf), len))
7995                 return -1;
7996
7997         fd = open(path, O_WRONLY);
7998         if (fd < 0)
7999                 return -1;
8000
8001         if (write(fd, buf, len) < 0) {
8002                 wpa_printf(MSG_DEBUG,
8003                            "nl80211: Failed to write Linux system file: %s with the value of %d",
8004                            path, val);
8005                 close(fd);
8006                 return -1;
8007         }
8008         close(fd);
8009
8010         return 0;
8011 }
8012
8013
8014 static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
8015 {
8016         switch (attr) {
8017         case DRV_BR_PORT_ATTR_PROXYARP:
8018                 return "proxyarp";
8019         case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
8020                 return "hairpin_mode";
8021         }
8022
8023         return NULL;
8024 }
8025
8026
8027 static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
8028                                        unsigned int val)
8029 {
8030         struct i802_bss *bss = priv;
8031         char path[128];
8032         const char *attr_txt;
8033
8034         attr_txt = drv_br_port_attr_str(attr);
8035         if (attr_txt == NULL)
8036                 return -EINVAL;
8037
8038         os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
8039                     bss->ifname, attr_txt);
8040
8041         if (linux_write_system_file(path, val))
8042                 return -1;
8043
8044         return 0;
8045 }
8046
8047
8048 static const char * drv_br_net_param_str(enum drv_br_net_param param)
8049 {
8050         switch (param) {
8051         case DRV_BR_NET_PARAM_GARP_ACCEPT:
8052                 return "arp_accept";
8053         }
8054
8055         return NULL;
8056 }
8057
8058
8059 static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
8060                                        unsigned int val)
8061 {
8062         struct i802_bss *bss = priv;
8063         char path[128];
8064         const char *param_txt;
8065         int ip_version = 4;
8066
8067         param_txt = drv_br_net_param_str(param);
8068         if (param_txt == NULL)
8069                 return -EINVAL;
8070
8071         switch (param) {
8072                 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8073                         ip_version = 4;
8074                         break;
8075                 default:
8076                         return -EINVAL;
8077         }
8078
8079         os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
8080                     ip_version, bss->brname, param_txt);
8081
8082         if (linux_write_system_file(path, val))
8083                 return -1;
8084
8085         return 0;
8086 }
8087
8088
8089 static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
8090 {
8091         switch (hw_mode) {
8092         case HOSTAPD_MODE_IEEE80211B:
8093                 return QCA_ACS_MODE_IEEE80211B;
8094         case HOSTAPD_MODE_IEEE80211G:
8095                 return QCA_ACS_MODE_IEEE80211G;
8096         case HOSTAPD_MODE_IEEE80211A:
8097                 return QCA_ACS_MODE_IEEE80211A;
8098         case HOSTAPD_MODE_IEEE80211AD:
8099                 return QCA_ACS_MODE_IEEE80211AD;
8100         default:
8101                 return -1;
8102         }
8103 }
8104
8105
8106 static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
8107 {
8108         struct i802_bss *bss = priv;
8109         struct wpa_driver_nl80211_data *drv = bss->drv;
8110         struct nl_msg *msg;
8111         struct nlattr *data;
8112         int ret;
8113         int mode;
8114
8115         mode = hw_mode_to_qca_acs(params->hw_mode);
8116         if (mode < 0)
8117                 return -1;
8118
8119         if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8120             nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8121             nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8122                         QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
8123             !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8124             nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
8125             (params->ht_enabled &&
8126              nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
8127             (params->ht40_enabled &&
8128              nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED))) {
8129                 nlmsg_free(msg);
8130                 return -ENOBUFS;
8131         }
8132         nla_nest_end(msg, data);
8133
8134         ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8135         if (ret) {
8136                 wpa_printf(MSG_DEBUG,
8137                            "nl80211: Failed to invoke driver ACS function: %s",
8138                            strerror(errno));
8139         }
8140         return ret;
8141 }
8142
8143
8144 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
8145         .name = "nl80211",
8146         .desc = "Linux nl80211/cfg80211",
8147         .get_bssid = wpa_driver_nl80211_get_bssid,
8148         .get_ssid = wpa_driver_nl80211_get_ssid,
8149         .set_key = driver_nl80211_set_key,
8150         .scan2 = driver_nl80211_scan2,
8151         .sched_scan = wpa_driver_nl80211_sched_scan,
8152         .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
8153         .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
8154         .deauthenticate = driver_nl80211_deauthenticate,
8155         .authenticate = driver_nl80211_authenticate,
8156         .associate = wpa_driver_nl80211_associate,
8157         .global_init = nl80211_global_init,
8158         .global_deinit = nl80211_global_deinit,
8159         .init2 = wpa_driver_nl80211_init,
8160         .deinit = driver_nl80211_deinit,
8161         .get_capa = wpa_driver_nl80211_get_capa,
8162         .set_operstate = wpa_driver_nl80211_set_operstate,
8163         .set_supp_port = wpa_driver_nl80211_set_supp_port,
8164         .set_country = wpa_driver_nl80211_set_country,
8165         .get_country = wpa_driver_nl80211_get_country,
8166         .set_ap = wpa_driver_nl80211_set_ap,
8167         .set_acl = wpa_driver_nl80211_set_acl,
8168         .if_add = wpa_driver_nl80211_if_add,
8169         .if_remove = driver_nl80211_if_remove,
8170         .send_mlme = driver_nl80211_send_mlme,
8171         .get_hw_feature_data = nl80211_get_hw_feature_data,
8172         .sta_add = wpa_driver_nl80211_sta_add,
8173         .sta_remove = driver_nl80211_sta_remove,
8174         .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
8175         .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
8176         .hapd_init = i802_init,
8177         .hapd_deinit = i802_deinit,
8178         .set_wds_sta = i802_set_wds_sta,
8179         .get_seqnum = i802_get_seqnum,
8180         .flush = i802_flush,
8181         .get_inact_sec = i802_get_inact_sec,
8182         .sta_clear_stats = i802_sta_clear_stats,
8183         .set_rts = i802_set_rts,
8184         .set_frag = i802_set_frag,
8185         .set_tx_queue_params = i802_set_tx_queue_params,
8186         .set_sta_vlan = driver_nl80211_set_sta_vlan,
8187         .sta_deauth = i802_sta_deauth,
8188         .sta_disassoc = i802_sta_disassoc,
8189         .read_sta_data = driver_nl80211_read_sta_data,
8190         .set_freq = i802_set_freq,
8191         .send_action = driver_nl80211_send_action,
8192         .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
8193         .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
8194         .cancel_remain_on_channel =
8195         wpa_driver_nl80211_cancel_remain_on_channel,
8196         .probe_req_report = driver_nl80211_probe_req_report,
8197         .deinit_ap = wpa_driver_nl80211_deinit_ap,
8198         .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
8199         .resume = wpa_driver_nl80211_resume,
8200         .signal_monitor = nl80211_signal_monitor,
8201         .signal_poll = nl80211_signal_poll,
8202         .send_frame = nl80211_send_frame,
8203         .shared_freq = wpa_driver_nl80211_shared_freq,
8204         .set_param = nl80211_set_param,
8205         .get_radio_name = nl80211_get_radio_name,
8206         .add_pmkid = nl80211_add_pmkid,
8207         .remove_pmkid = nl80211_remove_pmkid,
8208         .flush_pmkid = nl80211_flush_pmkid,
8209         .set_rekey_info = nl80211_set_rekey_info,
8210         .poll_client = nl80211_poll_client,
8211         .set_p2p_powersave = nl80211_set_p2p_powersave,
8212         .start_dfs_cac = nl80211_start_radar_detection,
8213         .stop_ap = wpa_driver_nl80211_stop_ap,
8214 #ifdef CONFIG_TDLS
8215         .send_tdls_mgmt = nl80211_send_tdls_mgmt,
8216         .tdls_oper = nl80211_tdls_oper,
8217 #endif /* CONFIG_TDLS */
8218         .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
8219         .get_mac_addr = wpa_driver_nl80211_get_macaddr,
8220         .get_survey = wpa_driver_nl80211_get_survey,
8221         .status = wpa_driver_nl80211_status,
8222         .switch_channel = nl80211_switch_channel,
8223 #ifdef ANDROID_P2P
8224         .set_noa = wpa_driver_set_p2p_noa,
8225         .get_noa = wpa_driver_get_p2p_noa,
8226         .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
8227 #endif /* ANDROID_P2P */
8228 #ifdef ANDROID
8229         .driver_cmd = wpa_driver_nl80211_driver_cmd,
8230 #endif /* ANDROID */
8231         .vendor_cmd = nl80211_vendor_cmd,
8232         .set_qos_map = nl80211_set_qos_map,
8233         .set_wowlan = nl80211_set_wowlan,
8234         .roaming = nl80211_roaming,
8235         .set_mac_addr = nl80211_set_mac_addr,
8236 #ifdef CONFIG_MESH
8237         .init_mesh = wpa_driver_nl80211_init_mesh,
8238         .join_mesh = wpa_driver_nl80211_join_mesh,
8239         .leave_mesh = wpa_driver_nl80211_leave_mesh,
8240 #endif /* CONFIG_MESH */
8241         .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
8242         .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
8243         .br_port_set_attr = wpa_driver_br_port_set_attr,
8244         .br_set_net_param = wpa_driver_br_set_net_param,
8245         .add_tx_ts = nl80211_add_ts,
8246         .del_tx_ts = nl80211_del_ts,
8247         .do_acs = wpa_driver_do_acs,
8248 };