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