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