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