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