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