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