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