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