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