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