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