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