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