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