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