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