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