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