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