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