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