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