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