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