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