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