]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/drivers/driver_nl80211.c
Android: Rename ANDROID_P2P_STUB to ANDROID_LIB_STUB
[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 */
046b26a2 1881
7b90c16a 1882 /* FT Action frames */
a11241fa 1883 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
6f06766e 1884 ret = -1;
7b90c16a
JM
1885 else
1886 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
1887 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
1888
71269b37 1889 /* WNM - BSS Transition Management Request */
a11241fa 1890 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
6f06766e 1891 ret = -1;
bd896433
JM
1892 /* WNM-Sleep Mode Response */
1893 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
6f06766e 1894 ret = -1;
a11241fa 1895
95a3ea94
JM
1896#ifdef CONFIG_HS20
1897 /* WNM-Notification */
1898 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
3ee18569 1899 ret = -1;
95a3ea94
JM
1900#endif /* CONFIG_HS20 */
1901
dfa87878
MB
1902 /* WMM-AC ADDTS Response */
1903 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
730a0d16 1904 ret = -1;
dfa87878
MB
1905
1906 /* WMM-AC DELTS */
1907 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
730a0d16 1908 ret = -1;
dfa87878 1909
2526ccd9
AK
1910 /* Radio Measurement - Neighbor Report Response */
1911 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
1912 ret = -1;
1913
7dc03388
AO
1914 /* Radio Measurement - Link Measurement Request */
1915 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
1916 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
1917 ret = -1;
1918
5f65e9f7
JB
1919 nl80211_mgmt_handle_register_eloop(bss);
1920
6f06766e 1921 return ret;
a11241fa
JB
1922}
1923
1924
afb0550a
BC
1925static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
1926{
1927 int ret = 0;
1928
1929 if (nl80211_alloc_mgmt_handle(bss))
1930 return -1;
1931
1932 wpa_printf(MSG_DEBUG,
1933 "nl80211: Subscribe to mgmt frames with mesh handle %p",
1934 bss->nl_mgmt);
1935
1936 /* Auth frames for mesh SAE */
1937 if (nl80211_register_frame(bss, bss->nl_mgmt,
1938 (WLAN_FC_TYPE_MGMT << 2) |
1939 (WLAN_FC_STYPE_AUTH << 4),
1940 NULL, 0) < 0)
1941 ret = -1;
1942
1943 /* Mesh peering open */
1944 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
1945 ret = -1;
1946 /* Mesh peering confirm */
1947 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
1948 ret = -1;
1949 /* Mesh peering close */
1950 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
1951 ret = -1;
1952
1953 nl80211_mgmt_handle_register_eloop(bss);
1954
1955 return ret;
1956}
1957
1958
02bb32c3
JB
1959static int nl80211_register_spurious_class3(struct i802_bss *bss)
1960{
02bb32c3 1961 struct nl_msg *msg;
a862e4a3 1962 int ret;
02bb32c3 1963
13f83980
JM
1964 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
1965 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
02bb32c3
JB
1966 if (ret) {
1967 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
1968 "failed: ret=%d (%s)",
1969 ret, strerror(-ret));
02bb32c3 1970 }
02bb32c3
JB
1971 return ret;
1972}
1973
1974
a11241fa
JB
1975static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
1976{
1977 static const int stypes[] = {
1978 WLAN_FC_STYPE_AUTH,
1979 WLAN_FC_STYPE_ASSOC_REQ,
1980 WLAN_FC_STYPE_REASSOC_REQ,
1981 WLAN_FC_STYPE_DISASSOC,
1982 WLAN_FC_STYPE_DEAUTH,
1983 WLAN_FC_STYPE_ACTION,
1984 WLAN_FC_STYPE_PROBE_REQ,
1985/* Beacon doesn't work as mac80211 doesn't currently allow
1986 * it, but it wouldn't really be the right thing anyway as
1987 * it isn't per interface ... maybe just dump the scan
1988 * results periodically for OLBC?
1989 */
0e80ea2c 1990 /* WLAN_FC_STYPE_BEACON, */
a11241fa
JB
1991 };
1992 unsigned int i;
1993
1994 if (nl80211_alloc_mgmt_handle(bss))
71269b37 1995 return -1;
36488c05
JM
1996 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
1997 "handle %p", bss->nl_mgmt);
71269b37 1998
e7ecab4a 1999 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
481234cf 2000 if (nl80211_register_frame(bss, bss->nl_mgmt,
a11241fa
JB
2001 (WLAN_FC_TYPE_MGMT << 2) |
2002 (stypes[i] << 4),
2003 NULL, 0) < 0) {
2004 goto out_err;
2005 }
2006 }
2007
02bb32c3
JB
2008 if (nl80211_register_spurious_class3(bss))
2009 goto out_err;
2010
e32ad281
JB
2011 if (nl80211_get_wiphy_data_ap(bss) == NULL)
2012 goto out_err;
2013
5f65e9f7 2014 nl80211_mgmt_handle_register_eloop(bss);
58f6fbe0 2015 return 0;
a11241fa
JB
2016
2017out_err:
a11241fa
JB
2018 nl_destroy_handles(&bss->nl_mgmt);
2019 return -1;
2020}
2021
2022
a6cc0602
JM
2023static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2024{
2025 if (nl80211_alloc_mgmt_handle(bss))
2026 return -1;
2027 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2028 "handle %p (device SME)", bss->nl_mgmt);
2029
2030 if (nl80211_register_frame(bss, bss->nl_mgmt,
2031 (WLAN_FC_TYPE_MGMT << 2) |
2032 (WLAN_FC_STYPE_ACTION << 4),
2033 NULL, 0) < 0)
2034 goto out_err;
2035
5f65e9f7 2036 nl80211_mgmt_handle_register_eloop(bss);
a6cc0602
JM
2037 return 0;
2038
2039out_err:
a6cc0602
JM
2040 nl_destroy_handles(&bss->nl_mgmt);
2041 return -1;
2042}
2043
2044
36488c05 2045static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
a11241fa 2046{
481234cf 2047 if (bss->nl_mgmt == NULL)
a11241fa 2048 return;
36488c05
JM
2049 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2050 "(%s)", bss->nl_mgmt, reason);
5f65e9f7 2051 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
e32ad281
JB
2052
2053 nl80211_put_wiphy_data_ap(bss);
58f6fbe0
JM
2054}
2055
2056
8401a6b0
JM
2057static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2058{
2059 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2060}
2061
2062
eb4582f2
AS
2063static void nl80211_del_p2pdev(struct i802_bss *bss)
2064{
eb4582f2
AS
2065 struct nl_msg *msg;
2066 int ret;
2067
a3249fdf
JM
2068 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2069 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
eb4582f2
AS
2070
2071 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2072 bss->ifname, (long long unsigned int) bss->wdev_id,
6cb4f11d 2073 strerror(-ret));
eb4582f2
AS
2074}
2075
2076
eb4582f2 2077static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
f632e483 2078{
f632e483 2079 struct nl_msg *msg;
a862e4a3 2080 int ret;
f632e483 2081
a3249fdf
JM
2082 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2083 NL80211_CMD_STOP_P2P_DEVICE);
2084 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
f632e483 2085
eb4582f2
AS
2086 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2087 start ? "Start" : "Stop",
2088 bss->ifname, (long long unsigned int) bss->wdev_id,
6cb4f11d 2089 strerror(-ret));
f632e483
AS
2090 return ret;
2091}
f632e483
AS
2092
2093
91724d6f
AS
2094static int i802_set_iface_flags(struct i802_bss *bss, int up)
2095{
2096 enum nl80211_iftype nlmode;
2097
2098 nlmode = nl80211_get_ifmode(bss);
2099 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2100 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2101 bss->ifname, up);
2102 }
2103
2104 /* P2P Device has start/stop which is equivalent */
2105 return nl80211_set_p2pdev(bss, up);
2106}
2107
2108
1db718b3
JM
2109#ifdef CONFIG_TESTING_OPTIONS
2110static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2111{
2112 /* struct wpa_driver_nl80211_data *drv = arg; */
2113 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2114 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2115
2116
2117 wpa_printf(MSG_DEBUG,
2118 "nl80211: QCA vendor test command response received");
2119
2120 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2121 genlmsg_attrlen(gnlh, 0), NULL);
2122 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2123 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2124 return NL_SKIP;
2125 }
2126
2127 wpa_hexdump(MSG_DEBUG,
2128 "nl80211: Received QCA vendor test command response",
2129 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2130 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2131
2132 return NL_SKIP;
2133}
2134#endif /* CONFIG_TESTING_OPTIONS */
2135
2136
2137static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2138{
2139#ifdef CONFIG_TESTING_OPTIONS
2140 struct nl_msg *msg;
2141 struct nlattr *params;
2142 int ret;
2143
2144 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2145 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2146 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2147 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2148 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2149 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2150 nlmsg_free(msg);
2151 return;
2152 }
2153 nla_nest_end(msg, params);
2154
2155 ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
2156 wpa_printf(MSG_DEBUG,
2157 "nl80211: QCA vendor test command returned %d (%s)",
2158 ret, strerror(-ret));
2159#endif /* CONFIG_TESTING_OPTIONS */
2160}
2161
2162
362f781e 2163static int
0d547d5f 2164wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
0ecff8d7
JM
2165 const u8 *set_addr, int first,
2166 const char *driver_params)
7524cfb1 2167{
834ee56f 2168 struct i802_bss *bss = drv->first_bss;
8401a6b0 2169 int send_rfkill_event = 0;
0d547d5f 2170 enum nl80211_iftype nlmode;
a2e40bb6
FF
2171
2172 drv->ifindex = if_nametoindex(bss->ifname);
f632e483
AS
2173 bss->ifindex = drv->ifindex;
2174 bss->wdev_id = drv->global->if_add_wdevid;
2175 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2176
60b13c20
IP
2177 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2178 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
f632e483
AS
2179 drv->global->if_add_wdevid_set = 0;
2180
bf144cf6
AP
2181 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2182 bss->static_ap = 1;
2183
f632e483
AS
2184 if (wpa_driver_nl80211_capa(drv))
2185 return -1;
a87c9d96 2186
0ecff8d7
JM
2187 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2188 return -1;
2189
5fbcb45d
AS
2190 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2191 bss->ifname, drv->phyname);
2192
0d547d5f
JM
2193 if (set_addr &&
2194 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2195 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2196 set_addr)))
2197 return -1;
2198
49b4b205
JM
2199 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2200 drv->start_mode_ap = 1;
2201
bf144cf6 2202 if (drv->hostapd || bss->static_ap)
0d547d5f
JM
2203 nlmode = NL80211_IFTYPE_AP;
2204 else if (bss->if_dynamic)
8e12685c 2205 nlmode = nl80211_get_ifmode(bss);
0d547d5f
JM
2206 else
2207 nlmode = NL80211_IFTYPE_STATION;
f632e483 2208
8e12685c 2209 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
0d547d5f 2210 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
8e12685c 2211 return -1;
a87c9d96
JM
2212 }
2213
8c06db70 2214 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
597b94f5 2215 nl80211_get_macaddr(bss);
f632e483 2216
8c06db70
MB
2217 if (!rfkill_is_blocked(drv->rfkill)) {
2218 int ret = i802_set_iface_flags(bss, 1);
2219 if (ret) {
8401a6b0
JM
2220 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2221 "interface '%s' UP", bss->ifname);
8c06db70 2222 return ret;
8401a6b0 2223 }
8c06db70
MB
2224 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2225 return ret;
2226 } else {
2227 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2228 "interface '%s' due to rfkill", bss->ifname);
2229 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2230 return 0;
2231 drv->if_disabled = 1;
2232 send_rfkill_event = 1;
362f781e 2233 }
3f5285e8 2234
0d547d5f
JM
2235 if (!drv->hostapd)
2236 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2237 1, IF_OPER_DORMANT);
362f781e 2238
c81eff1a 2239 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
341eebee 2240 bss->addr))
2136f480 2241 return -1;
fee354c7 2242 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
f2ed8023 2243
8401a6b0
JM
2244 if (send_rfkill_event) {
2245 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2246 drv, drv->ctx);
2247 }
2248
1db718b3
JM
2249 if (drv->vendor_cmd_test_avail)
2250 qca_vendor_test(drv);
2251
362f781e 2252 return 0;
3f5285e8
JM
2253}
2254
2255
8a27af5c
JM
2256static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
2257{
2258 struct nl_msg *msg;
2259
08e55ebb
JM
2260 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2261 drv->ifindex);
9725b784 2262 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
8a27af5c 2263 return send_and_recv_msgs(drv, msg, NULL, NULL);
8a27af5c 2264}
8a27af5c
JM
2265
2266
3f5285e8 2267/**
7e5ba1b9 2268 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
9ebce9c5 2269 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
3f5285e8
JM
2270 *
2271 * Shut down driver interface and processing of driver events. Free
2272 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2273 */
9ebce9c5 2274static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
3f5285e8 2275{
a2e40bb6 2276 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8 2277
3e208481
JM
2278 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2279 bss->ifname, drv->disabled_11b_rates);
2280
873d0fcf 2281 bss->in_deinit = 1;
32ab4855
JB
2282 if (drv->data_tx_status)
2283 eloop_unregister_read_sock(drv->eapol_tx_sock);
d12dab4c
JB
2284 if (drv->eapol_tx_sock >= 0)
2285 close(drv->eapol_tx_sock);
f10bfc9a 2286
481234cf 2287 if (bss->nl_preq)
5582a5d1 2288 wpa_driver_nl80211_probe_req_report(bss, 0);
e17a2477 2289 if (bss->added_if_into_bridge) {
c81eff1a
BG
2290 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2291 bss->ifname) < 0)
94627f6c
JM
2292 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2293 "interface %s from bridge %s: %s",
e17a2477 2294 bss->ifname, bss->brname, strerror(errno));
97ed9a06 2295 if (drv->rtnl_sk)
ca3c6b4d 2296 nl80211_handle_destroy(drv->rtnl_sk);
94627f6c 2297 }
e17a2477 2298 if (bss->added_bridge) {
f2535da8
JM
2299 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2300 0) < 0)
2301 wpa_printf(MSG_INFO,
2302 "nl80211: Could not set bridge %s down",
2303 bss->brname);
c81eff1a 2304 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
94627f6c
JM
2305 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2306 "bridge %s: %s",
e17a2477 2307 bss->brname, strerror(errno));
94627f6c
JM
2308 }
2309
460456f8 2310 nl80211_remove_monitor_interface(drv);
8a27af5c 2311
b1f625e0 2312 if (is_ap_interface(drv->nlmode))
8a27af5c 2313 wpa_driver_nl80211_del_beacon(drv);
0915d02c 2314
bbaf0837
JM
2315 if (drv->eapol_sock >= 0) {
2316 eloop_unregister_read_sock(drv->eapol_sock);
2317 close(drv->eapol_sock);
2318 }
2319
2320 if (drv->if_indices != drv->default_if_indices)
2321 os_free(drv->if_indices);
3f5285e8 2322
b3af99d2 2323 if (drv->disabled_11b_rates)
4e5cb1a3
JM
2324 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2325
36d84860
BG
2326 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2327 IF_OPER_UP);
e390df05 2328 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
8401a6b0 2329 rfkill_deinit(drv->rfkill);
3f5285e8 2330
bbaf0837
JM
2331 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2332
146fa9b3
JM
2333 if (!drv->start_iface_up)
2334 (void) i802_set_iface_flags(bss, 0);
fee354c7
JM
2335
2336 if (drv->addr_changed) {
93da0498
JM
2337 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2338 0) < 0) {
2339 wpa_printf(MSG_DEBUG,
2340 "nl80211: Could not set interface down to restore permanent MAC address");
2341 }
fee354c7
JM
2342 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2343 drv->perm_addr) < 0) {
2344 wpa_printf(MSG_DEBUG,
2345 "nl80211: Could not restore permanent MAC address");
2346 }
2347 }
2348
8e12685c 2349 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
49b4b205
JM
2350 if (!drv->hostapd || !drv->start_mode_ap)
2351 wpa_driver_nl80211_set_mode(bss,
2352 NL80211_IFTYPE_STATION);
b378c41f 2353 nl80211_mgmt_unsubscribe(bss, "deinit");
8e12685c
AS
2354 } else {
2355 nl80211_mgmt_unsubscribe(bss, "deinit");
eb4582f2 2356 nl80211_del_p2pdev(bss);
8e12685c 2357 }
3f5285e8 2358
834ee56f 2359 nl80211_destroy_bss(drv->first_bss);
cc7a48d1 2360
3812464c
JM
2361 os_free(drv->filter_ssids);
2362
536fd62d
JM
2363 os_free(drv->auth_ie);
2364
dac12351 2365 if (drv->in_interface_list)
f2ed8023
JM
2366 dl_list_del(&drv->list);
2367
8cd6b7bc
JB
2368 os_free(drv->extended_capa);
2369 os_free(drv->extended_capa_mask);
834ee56f 2370 os_free(drv->first_bss);
3f5285e8
JM
2371 os_free(drv);
2372}
2373
2374
de4ed4a8
JM
2375static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2376{
2377 switch (alg) {
2378 case WPA_ALG_WEP:
2379 if (key_len == 5)
2380 return WLAN_CIPHER_SUITE_WEP40;
2381 return WLAN_CIPHER_SUITE_WEP104;
2382 case WPA_ALG_TKIP:
2383 return WLAN_CIPHER_SUITE_TKIP;
2384 case WPA_ALG_CCMP:
2385 return WLAN_CIPHER_SUITE_CCMP;
2386 case WPA_ALG_GCMP:
2387 return WLAN_CIPHER_SUITE_GCMP;
2388 case WPA_ALG_CCMP_256:
2389 return WLAN_CIPHER_SUITE_CCMP_256;
2390 case WPA_ALG_GCMP_256:
2391 return WLAN_CIPHER_SUITE_GCMP_256;
2392 case WPA_ALG_IGTK:
2393 return WLAN_CIPHER_SUITE_AES_CMAC;
2394 case WPA_ALG_BIP_GMAC_128:
2395 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
2396 case WPA_ALG_BIP_GMAC_256:
2397 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
2398 case WPA_ALG_BIP_CMAC_256:
2399 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
2400 case WPA_ALG_SMS4:
2401 return WLAN_CIPHER_SUITE_SMS4;
2402 case WPA_ALG_KRK:
2403 return WLAN_CIPHER_SUITE_KRK;
2404 case WPA_ALG_NONE:
2405 case WPA_ALG_PMK:
2406 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2407 alg);
2408 return 0;
2409 }
2410
2411 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2412 alg);
2413 return 0;
2414}
2415
2416
2417static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2418{
2419 switch (cipher) {
2420 case WPA_CIPHER_CCMP_256:
2421 return WLAN_CIPHER_SUITE_CCMP_256;
2422 case WPA_CIPHER_GCMP_256:
2423 return WLAN_CIPHER_SUITE_GCMP_256;
2424 case WPA_CIPHER_CCMP:
2425 return WLAN_CIPHER_SUITE_CCMP;
2426 case WPA_CIPHER_GCMP:
2427 return WLAN_CIPHER_SUITE_GCMP;
2428 case WPA_CIPHER_TKIP:
2429 return WLAN_CIPHER_SUITE_TKIP;
2430 case WPA_CIPHER_WEP104:
2431 return WLAN_CIPHER_SUITE_WEP104;
2432 case WPA_CIPHER_WEP40:
2433 return WLAN_CIPHER_SUITE_WEP40;
ae6f9272
JM
2434 case WPA_CIPHER_GTK_NOT_USED:
2435 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
de4ed4a8
JM
2436 }
2437
2438 return 0;
2439}
2440
2441
2442static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2443 int max_suites)
2444{
2445 int num_suites = 0;
2446
2447 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
2448 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
2449 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
2450 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
2451 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
2452 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
2453 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
2454 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
2455 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
2456 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
2457 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
2458 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
2459 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
2460 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
2461
2462 return num_suites;
2463}
2464
2465
b41f2684
CL
2466static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2467 const u8 *key, size_t key_len)
2468{
2469 struct nl_msg *msg;
a862e4a3 2470 int ret;
b41f2684 2471
15badebd 2472 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
b41f2684
CL
2473 return 0;
2474
9725b784 2475 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
a862e4a3
JM
2476 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2477 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2478 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2479 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
bbd89bfc 2480 nl80211_nlmsg_clear(msg);
a862e4a3
JM
2481 nlmsg_free(msg);
2482 return -1;
2483 }
bbd89bfc 2484 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
b41f2684
CL
2485 if (ret) {
2486 wpa_printf(MSG_DEBUG,
2487 "nl80211: Key management set key failed: ret=%d (%s)",
2488 ret, strerror(-ret));
2489 }
2490
b41f2684
CL
2491 return ret;
2492}
2493
2494
9ebce9c5 2495static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
71934751
JM
2496 enum wpa_alg alg, const u8 *addr,
2497 int key_idx, int set_tx,
642187d6
JM
2498 const u8 *seq, size_t seq_len,
2499 const u8 *key, size_t key_len)
3f5285e8 2500{
a2e40bb6 2501 struct wpa_driver_nl80211_data *drv = bss->drv;
e472e1b4 2502 int ifindex;
34651767 2503 struct nl_msg *msg = NULL;
1ad1cdc2 2504 int ret;
dc01de8a 2505 int tdls = 0;
3f5285e8 2506
e472e1b4
AS
2507 /* Ignore for P2P Device */
2508 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2509 return 0;
2510
2511 ifindex = if_nametoindex(ifname);
8393e1a0 2512 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
1ad1cdc2 2513 "set_tx=%d seq_len=%lu key_len=%lu",
8393e1a0 2514 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
3f5285e8 2515 (unsigned long) seq_len, (unsigned long) key_len);
8c66e185 2516#ifdef CONFIG_TDLS
dc01de8a 2517 if (key_idx == -1) {
8c66e185 2518 key_idx = 0;
dc01de8a
JM
2519 tdls = 1;
2520 }
8c66e185 2521#endif /* CONFIG_TDLS */
3f5285e8 2522
15badebd
CL
2523 if (alg == WPA_ALG_PMK &&
2524 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
b41f2684
CL
2525 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2526 __func__);
2527 ret = issue_key_mgmt_set_key(drv, key, key_len);
2528 return ret;
2529 }
2530
3f5285e8 2531 if (alg == WPA_ALG_NONE) {
95376e1a
JM
2532 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2533 if (!msg)
2534 return -ENOBUFS;
3f5285e8 2535 } else {
34651767
JM
2536 u32 suite;
2537
2538 suite = wpa_alg_to_cipher_suite(alg, key_len);
2539 if (!suite)
2540 goto fail;
95376e1a
JM
2541 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2542 if (!msg ||
a862e4a3 2543 nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
34651767 2544 nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, suite))
a862e4a3 2545 goto fail;
e6ef73f1 2546 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
3f5285e8
JM
2547 }
2548
e6ef73f1 2549 if (seq && seq_len) {
a862e4a3
JM
2550 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2551 goto fail;
e6ef73f1
JM
2552 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2553 }
1ad1cdc2 2554
0382097e 2555 if (addr && !is_broadcast_ether_addr(addr)) {
3f5285e8 2556 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
a862e4a3
JM
2557 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2558 goto fail;
89c38e32
JM
2559
2560 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2561 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
a862e4a3
JM
2562 if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
2563 NL80211_KEYTYPE_GROUP))
2564 goto fail;
89c38e32 2565 }
60ea8187 2566 } else if (addr && is_broadcast_ether_addr(addr)) {
8970bae8
JB
2567 struct nlattr *types;
2568
60ea8187 2569 wpa_printf(MSG_DEBUG, " broadcast key");
8970bae8
JB
2570
2571 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
a862e4a3
JM
2572 if (!types ||
2573 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2574 goto fail;
8970bae8 2575 nla_nest_end(msg, types);
3f5285e8 2576 }
95376e1a 2577 if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
a862e4a3 2578 goto fail;
3f5285e8 2579
bbd89bfc 2580 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
15664ad0 2581 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
1ad1cdc2
JM
2582 ret = 0;
2583 if (ret)
2584 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2585 ret, strerror(-ret));
3f5285e8 2586
1ad1cdc2
JM
2587 /*
2588 * If we failed or don't need to set the default TX key (below),
2589 * we're done here.
2590 */
dc01de8a 2591 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
1ad1cdc2 2592 return ret;
b1f625e0 2593 if (is_ap_interface(drv->nlmode) && addr &&
0382097e 2594 !is_broadcast_ether_addr(addr))
de12717a 2595 return ret;
3f5285e8 2596
95376e1a
JM
2597 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
2598 if (!msg ||
a862e4a3 2599 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
86f9b1c7
JM
2600 nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
2601 alg == WPA_ALG_BIP_GMAC_128 ||
2602 alg == WPA_ALG_BIP_GMAC_256 ||
2603 alg == WPA_ALG_BIP_CMAC_256) ?
a862e4a3
JM
2604 NL80211_ATTR_KEY_DEFAULT_MGMT :
2605 NL80211_ATTR_KEY_DEFAULT))
2606 goto fail;
60ea8187 2607 if (addr && is_broadcast_ether_addr(addr)) {
8970bae8
JB
2608 struct nlattr *types;
2609
2610 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
a862e4a3
JM
2611 if (!types ||
2612 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2613 goto fail;
8970bae8 2614 nla_nest_end(msg, types);
60ea8187 2615 } else if (addr) {
8970bae8
JB
2616 struct nlattr *types;
2617
2618 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
a862e4a3
JM
2619 if (!types ||
2620 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
2621 goto fail;
8970bae8 2622 nla_nest_end(msg, types);
60ea8187 2623 }
3f5285e8 2624
1ad1cdc2
JM
2625 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2626 if (ret == -ENOENT)
2627 ret = 0;
2628 if (ret)
2629 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
2630 "err=%d %s)", ret, strerror(-ret));
2631 return ret;
3f5285e8 2632
a862e4a3 2633fail:
bbd89bfc 2634 nl80211_nlmsg_clear(msg);
5883168a 2635 nlmsg_free(msg);
6241fcb1 2636 return -ENOBUFS;
3f5285e8
JM
2637}
2638
2639
71934751 2640static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
0194fedb
JB
2641 int key_idx, int defkey,
2642 const u8 *seq, size_t seq_len,
2643 const u8 *key, size_t key_len)
2644{
2645 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
34651767
JM
2646 u32 suite;
2647
0194fedb
JB
2648 if (!key_attr)
2649 return -1;
2650
34651767
JM
2651 suite = wpa_alg_to_cipher_suite(alg, key_len);
2652 if (!suite)
2653 return -1;
2654
a862e4a3
JM
2655 if (defkey && alg == WPA_ALG_IGTK) {
2656 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
2657 return -1;
2658 } else if (defkey) {
2659 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
2660 return -1;
2661 }
0194fedb 2662
a862e4a3 2663 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
34651767 2664 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
a862e4a3
JM
2665 (seq && seq_len &&
2666 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
2667 nla_put(msg, NL80211_KEY_DATA, key_len, key))
2668 return -1;
0194fedb
JB
2669
2670 nla_nest_end(msg, key_attr);
2671
2672 return 0;
0194fedb
JB
2673}
2674
c811d5bc 2675
cfaab580
ZY
2676static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
2677 struct nl_msg *msg)
2678{
2679 int i, privacy = 0;
2680 struct nlattr *nl_keys, *nl_key;
2681
2682 for (i = 0; i < 4; i++) {
2683 if (!params->wep_key[i])
2684 continue;
2685 privacy = 1;
2686 break;
2687 }
ce04af5a
JM
2688 if (params->wps == WPS_MODE_PRIVACY)
2689 privacy = 1;
2690 if (params->pairwise_suite &&
2691 params->pairwise_suite != WPA_CIPHER_NONE)
2692 privacy = 1;
2693
cfaab580
ZY
2694 if (!privacy)
2695 return 0;
2696
a862e4a3
JM
2697 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
2698 return -ENOBUFS;
cfaab580
ZY
2699
2700 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
2701 if (!nl_keys)
a862e4a3 2702 return -ENOBUFS;
cfaab580
ZY
2703
2704 for (i = 0; i < 4; i++) {
2705 if (!params->wep_key[i])
2706 continue;
2707
2708 nl_key = nla_nest_start(msg, i);
a862e4a3
JM
2709 if (!nl_key ||
2710 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
2711 params->wep_key[i]) ||
2712 nla_put_u32(msg, NL80211_KEY_CIPHER,
2713 params->wep_key_len[i] == 5 ?
2714 WLAN_CIPHER_SUITE_WEP40 :
2715 WLAN_CIPHER_SUITE_WEP104) ||
2716 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
2717 (i == params->wep_tx_keyidx &&
2718 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
2719 return -ENOBUFS;
cfaab580
ZY
2720
2721 nla_nest_end(msg, nl_key);
2722 }
2723 nla_nest_end(msg, nl_keys);
2724
2725 return 0;
cfaab580
ZY
2726}
2727
2728
477af8f8
JM
2729int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
2730 const u8 *addr, int cmd, u16 reason_code,
2731 int local_state_change)
c2a04078 2732{
a862e4a3 2733 int ret;
c2a04078
JM
2734 struct nl_msg *msg;
2735
9725b784 2736 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
a862e4a3
JM
2737 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
2738 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
2739 (local_state_change &&
2740 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
2741 nlmsg_free(msg);
2742 return -1;
2743 }
c2a04078
JM
2744
2745 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
c2a04078 2746 if (ret) {
3b7ea880
BG
2747 wpa_dbg(drv->ctx, MSG_DEBUG,
2748 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
2749 reason_code, ret, strerror(-ret));
c2a04078 2750 }
c2a04078
JM
2751 return ret;
2752}
3f5285e8
JM
2753
2754
cfaab580 2755static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
817762d9 2756 int reason_code)
cfaab580 2757{
3f53c006
JJ
2758 int ret;
2759
817762d9 2760 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
add9b7a4 2761 nl80211_mark_disconnected(drv);
817762d9 2762 /* Disconnect command doesn't need BSSID - it uses cached value */
3f53c006
JJ
2763 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
2764 reason_code, 0);
2765 /*
2766 * For locally generated disconnect, supplicant already generates a
2767 * DEAUTH event, so ignore the event from NL80211.
2768 */
2769 drv->ignore_next_local_disconnect = ret == 0;
2770
2771 return ret;
cfaab580
ZY
2772}
2773
2774
9ebce9c5
JM
2775static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
2776 const u8 *addr, int reason_code)
3f5285e8 2777{
a2e40bb6 2778 struct wpa_driver_nl80211_data *drv = bss->drv;
d6a36f39 2779 int ret;
9392c9be
AS
2780
2781 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
2782 nl80211_mark_disconnected(drv);
666e508c 2783 return nl80211_leave_ibss(drv, 1);
9392c9be 2784 }
cfaab580 2785 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
817762d9 2786 return wpa_driver_nl80211_disconnect(drv, reason_code);
2e75a2b3
JM
2787 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
2788 __func__, MAC2STR(addr), reason_code);
add9b7a4 2789 nl80211_mark_disconnected(drv);
d6a36f39
JM
2790 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
2791 reason_code, 0);
2792 /*
2793 * For locally generated deauthenticate, supplicant already generates a
2794 * DEAUTH event, so ignore the event from NL80211.
2795 */
2796 drv->ignore_next_local_deauth = ret == 0;
2797 return ret;
3f5285e8
JM
2798}
2799
2800
536fd62d
JM
2801static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
2802 struct wpa_driver_auth_params *params)
2803{
2804 int i;
2805
2806 drv->auth_freq = params->freq;
2807 drv->auth_alg = params->auth_alg;
2808 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
2809 drv->auth_local_state_change = params->local_state_change;
2810 drv->auth_p2p = params->p2p;
2811
2812 if (params->bssid)
2813 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
2814 else
2815 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
2816
2817 if (params->ssid) {
2818 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
2819 drv->auth_ssid_len = params->ssid_len;
2820 } else
2821 drv->auth_ssid_len = 0;
2822
2823
2824 os_free(drv->auth_ie);
2825 drv->auth_ie = NULL;
2826 drv->auth_ie_len = 0;
2827 if (params->ie) {
2828 drv->auth_ie = os_malloc(params->ie_len);
2829 if (drv->auth_ie) {
2830 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
2831 drv->auth_ie_len = params->ie_len;
2832 }
2833 }
2834
2835 for (i = 0; i < 4; i++) {
2836 if (params->wep_key[i] && params->wep_key_len[i] &&
2837 params->wep_key_len[i] <= 16) {
2838 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
2839 params->wep_key_len[i]);
2840 drv->auth_wep_key_len[i] = params->wep_key_len[i];
2841 } else
2842 drv->auth_wep_key_len[i] = 0;
2843 }
2844}
2845
2846
4bd71954
JM
2847static void nl80211_unmask_11b_rates(struct i802_bss *bss)
2848{
2849 struct wpa_driver_nl80211_data *drv = bss->drv;
2850
2851 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
2852 return;
2853
2854 /*
2855 * Looks like we failed to unmask 11b rates previously. This could
2856 * happen, e.g., if the interface was down at the point in time when a
2857 * P2P group was terminated.
2858 */
2859 wpa_printf(MSG_DEBUG,
2860 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
2861 bss->ifname);
2862 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2863}
2864
2865
c2a04078 2866static int wpa_driver_nl80211_authenticate(
9ebce9c5 2867 struct i802_bss *bss, struct wpa_driver_auth_params *params)
c2a04078 2868{
a2e40bb6 2869 struct wpa_driver_nl80211_data *drv = bss->drv;
a0b2f99b 2870 int ret = -1, i;
c2a04078
JM
2871 struct nl_msg *msg;
2872 enum nl80211_auth_type type;
2f4f73b1 2873 enum nl80211_iftype nlmode;
6d6f4bb8 2874 int count = 0;
536fd62d
JM
2875 int is_retry;
2876
4bd71954
JM
2877 nl80211_unmask_11b_rates(bss);
2878
536fd62d
JM
2879 is_retry = drv->retry_auth;
2880 drv->retry_auth = 0;
e8d70a73 2881 drv->ignore_deauth_event = 0;
c2a04078 2882
add9b7a4 2883 nl80211_mark_disconnected(drv);
e6b8efeb 2884 os_memset(drv->auth_bssid, 0, ETH_ALEN);
add9b7a4
JM
2885 if (params->bssid)
2886 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
2887 else
2888 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
af473088 2889 /* FIX: IBSS mode */
2f4f73b1
EP
2890 nlmode = params->p2p ?
2891 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
2892 if (drv->nlmode != nlmode &&
9ebce9c5 2893 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
4a867032
JM
2894 return -1;
2895
6d6f4bb8 2896retry:
c2a04078
JM
2897 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
2898 drv->ifindex);
a0b2f99b 2899
9725b784
JM
2900 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
2901 if (!msg)
2902 goto fail;
0194fedb 2903
a0b2f99b
JM
2904 for (i = 0; i < 4; i++) {
2905 if (!params->wep_key[i])
2906 continue;
9ebce9c5 2907 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
2ea2fcc7 2908 NULL, i,
a0b2f99b
JM
2909 i == params->wep_tx_keyidx, NULL, 0,
2910 params->wep_key[i],
2911 params->wep_key_len[i]);
0194fedb
JB
2912 if (params->wep_tx_keyidx != i)
2913 continue;
2914 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
9725b784
JM
2915 params->wep_key[i], params->wep_key_len[i]))
2916 goto fail;
a0b2f99b
JM
2917 }
2918
c2a04078
JM
2919 if (params->bssid) {
2920 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
2921 MAC2STR(params->bssid));
a862e4a3
JM
2922 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
2923 goto fail;
c2a04078
JM
2924 }
2925 if (params->freq) {
2926 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
a862e4a3
JM
2927 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
2928 goto fail;
c2a04078
JM
2929 }
2930 if (params->ssid) {
2931 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
2932 params->ssid, params->ssid_len);
a862e4a3
JM
2933 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
2934 params->ssid))
2935 goto fail;
c2a04078
JM
2936 }
2937 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
a862e4a3
JM
2938 if (params->ie &&
2939 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
2940 goto fail;
569fed90
JM
2941 if (params->sae_data) {
2942 wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
2943 params->sae_data_len);
a862e4a3
JM
2944 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
2945 params->sae_data))
2946 goto fail;
569fed90 2947 }
abd9fafa 2948 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
c2a04078 2949 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
abd9fafa 2950 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
c2a04078 2951 type = NL80211_AUTHTYPE_SHARED_KEY;
abd9fafa 2952 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
c2a04078 2953 type = NL80211_AUTHTYPE_NETWORK_EAP;
abd9fafa 2954 else if (params->auth_alg & WPA_AUTH_ALG_FT)
c2a04078 2955 type = NL80211_AUTHTYPE_FT;
569fed90
JM
2956 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
2957 type = NL80211_AUTHTYPE_SAE;
c2a04078 2958 else
a862e4a3 2959 goto fail;
c2a04078 2960 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
a862e4a3
JM
2961 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
2962 goto fail;
77339912
JM
2963 if (params->local_state_change) {
2964 wpa_printf(MSG_DEBUG, " * Local state change only");
a862e4a3
JM
2965 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
2966 goto fail;
77339912 2967 }
c2a04078
JM
2968
2969 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2970 msg = NULL;
2971 if (ret) {
3b7ea880
BG
2972 wpa_dbg(drv->ctx, MSG_DEBUG,
2973 "nl80211: MLME command failed (auth): ret=%d (%s)",
2974 ret, strerror(-ret));
6d6f4bb8 2975 count++;
77339912
JM
2976 if (ret == -EALREADY && count == 1 && params->bssid &&
2977 !params->local_state_change) {
6d6f4bb8
JM
2978 /*
2979 * mac80211 does not currently accept new
2980 * authentication if we are already authenticated. As a
2981 * workaround, force deauthentication and try again.
2982 */
2983 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
2984 "after forced deauthentication");
e8d70a73 2985 drv->ignore_deauth_event = 1;
6d6f4bb8 2986 wpa_driver_nl80211_deauthenticate(
5205c4f9 2987 bss, params->bssid,
6d6f4bb8
JM
2988 WLAN_REASON_PREV_AUTH_NOT_VALID);
2989 nlmsg_free(msg);
2990 goto retry;
2991 }
536fd62d
JM
2992
2993 if (ret == -ENOENT && params->freq && !is_retry) {
2994 /*
2995 * cfg80211 has likely expired the BSS entry even
2996 * though it was previously available in our internal
2997 * BSS table. To recover quickly, start a single
2998 * channel scan on the specified channel.
2999 */
3000 struct wpa_driver_scan_params scan;
3001 int freqs[2];
3002
3003 os_memset(&scan, 0, sizeof(scan));
3004 scan.num_ssids = 1;
3005 if (params->ssid) {
3006 scan.ssids[0].ssid = params->ssid;
3007 scan.ssids[0].ssid_len = params->ssid_len;
3008 }
3009 freqs[0] = params->freq;
3010 freqs[1] = 0;
3011 scan.freqs = freqs;
3012 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3013 "channel scan to refresh cfg80211 BSS "
3014 "entry");
3015 ret = wpa_driver_nl80211_scan(bss, &scan);
3016 if (ret == 0) {
3017 nl80211_copy_auth_params(drv, params);
3018 drv->scan_for_auth = 1;
3019 }
8c3ba078
JM
3020 } else if (is_retry) {
3021 /*
3022 * Need to indicate this with an event since the return
3023 * value from the retry is not delivered to core code.
3024 */
3025 union wpa_event_data event;
3026 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3027 "failed");
3028 os_memset(&event, 0, sizeof(event));
3029 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3030 ETH_ALEN);
3031 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3032 &event);
536fd62d 3033 }
a862e4a3
JM
3034 } else {
3035 wpa_printf(MSG_DEBUG,
3036 "nl80211: Authentication request send successfully");
c2a04078 3037 }
c2a04078 3038
a862e4a3 3039fail:
c2a04078
JM
3040 nlmsg_free(msg);
3041 return ret;
3042}
3043
3044
f3407c66 3045int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
536fd62d
JM
3046{
3047 struct wpa_driver_auth_params params;
834ee56f 3048 struct i802_bss *bss = drv->first_bss;
536fd62d
JM
3049 int i;
3050
3051 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3052
3053 os_memset(&params, 0, sizeof(params));
3054 params.freq = drv->auth_freq;
3055 params.auth_alg = drv->auth_alg;
3056 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3057 params.local_state_change = drv->auth_local_state_change;
3058 params.p2p = drv->auth_p2p;
3059
3060 if (!is_zero_ether_addr(drv->auth_bssid_))
3061 params.bssid = drv->auth_bssid_;
3062
3063 if (drv->auth_ssid_len) {
3064 params.ssid = drv->auth_ssid;
3065 params.ssid_len = drv->auth_ssid_len;
3066 }
3067
3068 params.ie = drv->auth_ie;
3069 params.ie_len = drv->auth_ie_len;
3070
3071 for (i = 0; i < 4; i++) {
3072 if (drv->auth_wep_key_len[i]) {
3073 params.wep_key[i] = drv->auth_wep_key[i];
3074 params.wep_key_len[i] = drv->auth_wep_key_len[i];
3075 }
3076 }
3077
3078 drv->retry_auth = 1;
3079 return wpa_driver_nl80211_authenticate(bss, &params);
3080}
3081
3082
a11241fa
JB
3083static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
3084 const void *data, size_t len,
55231068
JM
3085 int encrypt, int noack,
3086 unsigned int freq, int no_cck,
3087 int offchanok, unsigned int wait_time)
a11241fa
JB
3088{
3089 struct wpa_driver_nl80211_data *drv = bss->drv;
3090 u64 cookie;
41cc50d1 3091 int res;
a11241fa 3092
c7caac56
JM
3093 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3094 freq = nl80211_get_assoc_freq(drv);
3095 wpa_printf(MSG_DEBUG,
3096 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
3097 freq);
3098 }
af964484
JM
3099 if (freq == 0) {
3100 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
3101 bss->freq);
55231068 3102 freq = bss->freq;
af964484 3103 }
55231068 3104
739faee2 3105 if (drv->use_monitor) {
981cf85a 3106 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
739faee2 3107 freq, bss->freq);
981cf85a 3108 return nl80211_send_monitor(drv, data, len, encrypt, noack);
739faee2 3109 }
a11241fa 3110
739faee2 3111 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
41cc50d1
JM
3112 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
3113 &cookie, no_cck, noack, offchanok);
3114 if (res == 0 && !noack) {
3115 const struct ieee80211_mgmt *mgmt;
3116 u16 fc;
3117
3118 mgmt = (const struct ieee80211_mgmt *) data;
3119 fc = le_to_host16(mgmt->frame_control);
3120 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3121 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
3122 wpa_printf(MSG_MSGDUMP,
3123 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
3124 (long long unsigned int)
3125 drv->send_action_cookie,
3126 (long long unsigned int) cookie);
3127 drv->send_action_cookie = cookie;
3128 }
3129 }
3130
3131 return res;
a11241fa
JB
3132}
3133
3134
9ebce9c5
JM
3135static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3136 size_t data_len, int noack,
3137 unsigned int freq, int no_cck,
3138 int offchanok,
3139 unsigned int wait_time)
2c2010ac 3140{
a2e40bb6 3141 struct wpa_driver_nl80211_data *drv = bss->drv;
2c2010ac 3142 struct ieee80211_mgmt *mgmt;
7a47d567 3143 int encrypt = 1;
2c2010ac
JM
3144 u16 fc;
3145
3146 mgmt = (struct ieee80211_mgmt *) data;
3147 fc = le_to_host16(mgmt->frame_control);
f95a4524
PF
3148 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3149 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3150 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3151 fc, fc2str(fc), drv->nlmode);
2c2010ac 3152
8e12685c
AS
3153 if ((is_sta_interface(drv->nlmode) ||
3154 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
5582a5d1
JB
3155 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3156 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3157 /*
3158 * The use of last_mgmt_freq is a bit of a hack,
3159 * but it works due to the single-threaded nature
3160 * of wpa_supplicant.
3161 */
af964484
JM
3162 if (freq == 0) {
3163 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3164 drv->last_mgmt_freq);
55231068 3165 freq = drv->last_mgmt_freq;
af964484 3166 }
55231068 3167 return nl80211_send_frame_cmd(bss, freq, 0,
88df0ef7
JB
3168 data, data_len, NULL, 1, noack,
3169 1);
5582a5d1
JB
3170 }
3171
61cbe2ff 3172 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
af964484
JM
3173 if (freq == 0) {
3174 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3175 bss->freq);
55231068 3176 freq = bss->freq;
af964484 3177 }
b5671498
JM
3178 return nl80211_send_frame_cmd(bss, freq,
3179 (int) freq == bss->freq ? 0 :
3180 wait_time,
d8d6b32e
DG
3181 data, data_len,
3182 &drv->send_action_cookie,
55231068 3183 no_cck, noack, offchanok);
86957e62
JM
3184 }
3185
2c2010ac
JM
3186 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3187 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3188 /*
3189 * Only one of the authentication frame types is encrypted.
3190 * In order for static WEP encryption to work properly (i.e.,
3191 * to not encrypt the frame), we need to tell mac80211 about
3192 * the frames that must not be encrypted.
3193 */
3194 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3195 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
7a47d567
JB
3196 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3197 encrypt = 0;
2c2010ac
JM
3198 }
3199
739faee2 3200 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
a11241fa 3201 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
55231068
JM
3202 noack, freq, no_cck, offchanok,
3203 wait_time);
3204}
3205
3206
cae87abd
JM
3207static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3208{
3209 u8 rates[NL80211_MAX_SUPP_RATES];
3210 u8 rates_len = 0;
3211 int i;
3212
3213 if (!basic_rates)
3214 return 0;
3215
3216 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3217 rates[rates_len++] = basic_rates[i] / 5;
3218
3219 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3220}
3221
3222
31357268 3223static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
e5693c47 3224 int slot, int ht_opmode, int ap_isolate,
cae87abd 3225 const int *basic_rates)
31357268
JM
3226{
3227 struct wpa_driver_nl80211_data *drv = bss->drv;
3228 struct nl_msg *msg;
3229
13f83980 3230 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
a862e4a3
JM
3231 (cts >= 0 &&
3232 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3233 (preamble >= 0 &&
3234 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3235 (slot >= 0 &&
3236 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3237 (ht_opmode >= 0 &&
3238 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3239 (ap_isolate >= 0 &&
cae87abd
JM
3240 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3241 nl80211_put_basic_rates(msg, basic_rates)) {
3242 nlmsg_free(msg);
3243 return -ENOBUFS;
e5693c47
JM
3244 }
3245
31357268 3246 return send_and_recv_msgs(drv, msg, NULL, NULL);
31357268
JM
3247}
3248
3249
3c4ca363
VN
3250static int wpa_driver_nl80211_set_acl(void *priv,
3251 struct hostapd_acl_params *params)
3252{
3253 struct i802_bss *bss = priv;
3254 struct wpa_driver_nl80211_data *drv = bss->drv;
3255 struct nl_msg *msg;
3256 struct nlattr *acl;
3257 unsigned int i;
a862e4a3 3258 int ret;
3c4ca363
VN
3259
3260 if (!(drv->capa.max_acl_mac_addrs))
3261 return -ENOTSUP;
3262
3263 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3264 return -ENOTSUP;
3265
3c4ca363
VN
3266 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3267 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3268
9725b784 3269 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
a862e4a3
JM
3270 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3271 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3272 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3273 (acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS)) == NULL) {
3274 nlmsg_free(msg);
3275 return -ENOMEM;
3276 }
3c4ca363 3277
a862e4a3
JM
3278 for (i = 0; i < params->num_mac_acl; i++) {
3279 if (nla_put(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3280 nlmsg_free(msg);
3281 return -ENOMEM;
3282 }
3283 }
3c4ca363
VN
3284
3285 nla_nest_end(msg, acl);
3286
3287 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3c4ca363
VN
3288 if (ret) {
3289 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3290 ret, strerror(-ret));
3291 }
3292
3c4ca363
VN
3293 return ret;
3294}
3295
3296
85e1fad8
JM
3297static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3298{
3299 if (beacon_int > 0) {
3300 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
3301 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3302 beacon_int);
3303 }
3304
3305 return 0;
3306}
3307
3308
19c3b566
JM
3309static int wpa_driver_nl80211_set_ap(void *priv,
3310 struct wpa_driver_ap_params *params)
d2440ba0 3311{
a2e40bb6
FF
3312 struct i802_bss *bss = priv;
3313 struct wpa_driver_nl80211_data *drv = bss->drv;
d2440ba0
JM
3314 struct nl_msg *msg;
3315 u8 cmd = NL80211_CMD_NEW_BEACON;
3316 int ret;
b4fd6fab 3317 int beacon_set;
b11d1d64 3318 int num_suites;
da1080d7 3319 int smps_mode;
de4ed4a8 3320 u32 suites[10], suite;
b11d1d64 3321 u32 ver;
b4fd6fab 3322
f33c8606 3323 beacon_set = params->reenable ? 0 : bss->beacon_set;
d2440ba0 3324
d2440ba0 3325 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
b4fd6fab
JM
3326 beacon_set);
3327 if (beacon_set)
d2440ba0
JM
3328 cmd = NL80211_CMD_SET_BEACON;
3329
b92e08fc
JM
3330 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3331 params->head, params->head_len);
b92e08fc
JM
3332 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3333 params->tail, params->tail_len);
13f83980 3334 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
b92e08fc 3335 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
b92e08fc 3336 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
b92e08fc
JM
3337 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3338 params->ssid, params->ssid_len);
13f83980 3339 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
a862e4a3
JM
3340 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3341 params->head) ||
3342 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3343 params->tail) ||
85e1fad8 3344 nl80211_put_beacon_int(msg, params->beacon_int) ||
a862e4a3
JM
3345 nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period) ||
3346 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3347 goto fail;
b92e08fc
JM
3348 if (params->proberesp && params->proberesp_len) {
3349 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3350 params->proberesp, params->proberesp_len);
a862e4a3
JM
3351 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3352 params->proberesp))
3353 goto fail;
b92e08fc 3354 }
97a7a0b5
JM
3355 switch (params->hide_ssid) {
3356 case NO_SSID_HIDING:
b92e08fc 3357 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
a862e4a3
JM
3358 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3359 NL80211_HIDDEN_SSID_NOT_IN_USE))
3360 goto fail;
97a7a0b5
JM
3361 break;
3362 case HIDDEN_SSID_ZERO_LEN:
b92e08fc 3363 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
a862e4a3
JM
3364 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3365 NL80211_HIDDEN_SSID_ZERO_LEN))
3366 goto fail;
97a7a0b5
JM
3367 break;
3368 case HIDDEN_SSID_ZERO_CONTENTS:
b92e08fc 3369 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
a862e4a3
JM
3370 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3371 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3372 goto fail;
97a7a0b5
JM
3373 break;
3374 }
b92e08fc 3375 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
a862e4a3
JM
3376 if (params->privacy &&
3377 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3378 goto fail;
b92e08fc 3379 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
b11d1d64
JM
3380 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3381 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3382 /* Leave out the attribute */
a862e4a3
JM
3383 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3384 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3385 NL80211_AUTHTYPE_SHARED_KEY))
3386 goto fail;
3387 } else {
3388 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3389 NL80211_AUTHTYPE_OPEN_SYSTEM))
3390 goto fail;
3391 }
b11d1d64 3392
b92e08fc 3393 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
b11d1d64
JM
3394 ver = 0;
3395 if (params->wpa_version & WPA_PROTO_WPA)
3396 ver |= NL80211_WPA_VERSION_1;
3397 if (params->wpa_version & WPA_PROTO_RSN)
3398 ver |= NL80211_WPA_VERSION_2;
a862e4a3
JM
3399 if (ver &&
3400 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3401 goto fail;
b11d1d64 3402
b92e08fc
JM
3403 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3404 params->key_mgmt_suites);
b11d1d64
JM
3405 num_suites = 0;
3406 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
3407 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
3408 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
3409 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
a862e4a3
JM
3410 if (num_suites &&
3411 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
3412 suites))
3413 goto fail;
b11d1d64 3414
533fe09b 3415 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
a862e4a3
JM
3416 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40) &&
3417 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
3418 goto fail;
9f12614b 3419
b92e08fc
JM
3420 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
3421 params->pairwise_ciphers);
de4ed4a8
JM
3422 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
3423 suites, ARRAY_SIZE(suites));
a862e4a3
JM
3424 if (num_suites &&
3425 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
3426 num_suites * sizeof(u32), suites))
3427 goto fail;
b11d1d64 3428
b92e08fc
JM
3429 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
3430 params->group_cipher);
de4ed4a8 3431 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
a862e4a3
JM
3432 if (suite &&
3433 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
3434 goto fail;
d2440ba0 3435
da1080d7
EP
3436 switch (params->smps_mode) {
3437 case HT_CAP_INFO_SMPS_DYNAMIC:
3438 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
3439 smps_mode = NL80211_SMPS_DYNAMIC;
3440 break;
3441 case HT_CAP_INFO_SMPS_STATIC:
3442 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
3443 smps_mode = NL80211_SMPS_STATIC;
3444 break;
3445 default:
3446 /* invalid - fallback to smps off */
3447 case HT_CAP_INFO_SMPS_DISABLED:
3448 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
3449 smps_mode = NL80211_SMPS_OFF;
3450 break;
3451 }
a862e4a3
JM
3452 if (nla_put_u32(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
3453 goto fail;
da1080d7 3454
fb91db56 3455 if (params->beacon_ies) {
b92e08fc
JM
3456 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
3457 params->beacon_ies);
a862e4a3
JM
3458 if (nla_put(msg, NL80211_ATTR_IE,
3459 wpabuf_len(params->beacon_ies),
3460 wpabuf_head(params->beacon_ies)))
3461 goto fail;
fb91db56
JM
3462 }
3463 if (params->proberesp_ies) {
b92e08fc
JM
3464 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
3465 params->proberesp_ies);
a862e4a3
JM
3466 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
3467 wpabuf_len(params->proberesp_ies),
3468 wpabuf_head(params->proberesp_ies)))
3469 goto fail;
fb91db56
JM
3470 }
3471 if (params->assocresp_ies) {
b92e08fc
JM
3472 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
3473 params->assocresp_ies);
a862e4a3
JM
3474 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
3475 wpabuf_len(params->assocresp_ies),
3476 wpabuf_head(params->assocresp_ies)))
3477 goto fail;
fb91db56
JM
3478 }
3479
a0133ee1 3480 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
b92e08fc
JM
3481 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
3482 params->ap_max_inactivity);
a862e4a3
JM
3483 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
3484 params->ap_max_inactivity))
3485 goto fail;
a0133ee1
VT
3486 }
3487
abb8d08b
EP
3488#ifdef CONFIG_P2P
3489 if (params->p2p_go_ctwindow > 0) {
3490 if (drv->p2p_go_ctwindow_supported) {
3491 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
3492 params->p2p_go_ctwindow);
3493 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
3494 params->p2p_go_ctwindow))
3495 goto fail;
3496 } else {
3497 wpa_printf(MSG_INFO,
3498 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
3499 }
3500 }
3501#endif /* CONFIG_P2P */
3502
d2440ba0
JM
3503 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3504 if (ret) {
3505 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
3506 ret, strerror(-ret));
b4fd6fab 3507 } else {
b4fd6fab 3508 bss->beacon_set = 1;
31357268 3509 nl80211_set_bss(bss, params->cts_protect, params->preamble,
d03e8d11 3510 params->short_slot_time, params->ht_opmode,
e5693c47 3511 params->isolate, params->basic_rates);
e87ef751
PX
3512 if (beacon_set && params->freq &&
3513 params->freq->bandwidth != bss->bandwidth) {
3514 wpa_printf(MSG_DEBUG,
3515 "nl80211: Update BSS %s bandwidth: %d -> %d",
3516 bss->ifname, bss->bandwidth,
3517 params->freq->bandwidth);
3518 ret = nl80211_set_channel(bss, params->freq, 1);
3519 if (ret) {
3520 wpa_printf(MSG_DEBUG,
3521 "nl80211: Frequency set failed: %d (%s)",
3522 ret, strerror(-ret));
3523 } else {
3524 wpa_printf(MSG_DEBUG,
3525 "nl80211: Frequency set succeeded for ht2040 coex");
3526 bss->bandwidth = params->freq->bandwidth;
3527 }
3528 } else if (!beacon_set) {
3529 /*
3530 * cfg80211 updates the driver on frequence change in AP
3531 * mode only at the point when beaconing is started, so
3532 * set the initial value here.
3533 */
3534 bss->bandwidth = params->freq->bandwidth;
3535 }
b4fd6fab 3536 }
d2440ba0 3537 return ret;
a862e4a3 3538fail:
5883168a 3539 nlmsg_free(msg);
d2440ba0
JM
3540 return -ENOBUFS;
3541}
3542
3543
1c4ffa87 3544static int nl80211_put_freq_params(struct nl_msg *msg,
72b2605f 3545 const struct hostapd_freq_params *freq)
1581b38b 3546{
1fc4ab23 3547 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
a862e4a3
JM
3548 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
3549 return -ENOBUFS;
3550
1fc4ab23
JM
3551 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
3552 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
3553
89b800d7 3554 if (freq->vht_enabled) {
a862e4a3
JM
3555 enum nl80211_chan_width cw;
3556
1fc4ab23 3557 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
89b800d7
JB
3558 switch (freq->bandwidth) {
3559 case 20:
a862e4a3 3560 cw = NL80211_CHAN_WIDTH_20;
89b800d7
JB
3561 break;
3562 case 40:
a862e4a3 3563 cw = NL80211_CHAN_WIDTH_40;
89b800d7
JB
3564 break;
3565 case 80:
3566 if (freq->center_freq2)
a862e4a3 3567 cw = NL80211_CHAN_WIDTH_80P80;
89b800d7 3568 else
a862e4a3 3569 cw = NL80211_CHAN_WIDTH_80;
89b800d7
JB
3570 break;
3571 case 160:
a862e4a3 3572 cw = NL80211_CHAN_WIDTH_160;
89b800d7
JB
3573 break;
3574 default:
1c4ffa87 3575 return -EINVAL;
89b800d7 3576 }
a862e4a3 3577
1fc4ab23
JM
3578 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
3579 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
3580 freq->center_freq1);
3581 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
3582 freq->center_freq2);
a862e4a3
JM
3583 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
3584 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
3585 freq->center_freq1) ||
3586 (freq->center_freq2 &&
3587 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
3588 freq->center_freq2)))
3589 return -ENOBUFS;
89b800d7 3590 } else if (freq->ht_enabled) {
a862e4a3
JM
3591 enum nl80211_channel_type ct;
3592
1fc4ab23
JM
3593 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
3594 freq->sec_channel_offset);
89b800d7 3595 switch (freq->sec_channel_offset) {
f019981a 3596 case -1:
a862e4a3 3597 ct = NL80211_CHAN_HT40MINUS;
f019981a
JM
3598 break;
3599 case 1:
a862e4a3 3600 ct = NL80211_CHAN_HT40PLUS;
f019981a
JM
3601 break;
3602 default:
a862e4a3 3603 ct = NL80211_CHAN_HT20;
f019981a
JM
3604 break;
3605 }
a862e4a3 3606
1fc4ab23 3607 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
a862e4a3
JM
3608 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
3609 return -ENOBUFS;
f019981a 3610 }
1c4ffa87 3611 return 0;
1c4ffa87
AO
3612}
3613
3614
e87ef751
PX
3615static int nl80211_set_channel(struct i802_bss *bss,
3616 struct hostapd_freq_params *freq, int set_chan)
1c4ffa87
AO
3617{
3618 struct wpa_driver_nl80211_data *drv = bss->drv;
3619 struct nl_msg *msg;
3620 int ret;
3621
3622 wpa_printf(MSG_DEBUG,
3623 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
3624 freq->freq, freq->ht_enabled, freq->vht_enabled,
3625 freq->bandwidth, freq->center_freq1, freq->center_freq2);
1c4ffa87 3626
9725b784
JM
3627 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
3628 NL80211_CMD_SET_WIPHY);
3629 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
a862e4a3
JM
3630 nlmsg_free(msg);
3631 return -1;
3632 }
1581b38b 3633
d2440ba0 3634 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
e4fb2167 3635 if (ret == 0) {
89b800d7 3636 bss->freq = freq->freq;
1581b38b 3637 return 0;
e4fb2167 3638 }
f019981a 3639 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
89b800d7 3640 "%d (%s)", freq->freq, ret, strerror(-ret));
1581b38b
JM
3641 return -1;
3642}
3643
0f4e8b4f 3644
95ab6063
AN
3645static u32 sta_flags_nl80211(int flags)
3646{
3647 u32 f = 0;
3648
3649 if (flags & WPA_STA_AUTHORIZED)
3650 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
3651 if (flags & WPA_STA_WMM)
3652 f |= BIT(NL80211_STA_FLAG_WME);
3653 if (flags & WPA_STA_SHORT_PREAMBLE)
3654 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
3655 if (flags & WPA_STA_MFP)
3656 f |= BIT(NL80211_STA_FLAG_MFP);
45b722f1
AN
3657 if (flags & WPA_STA_TDLS_PEER)
3658 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
7a228b5c
BC
3659 if (flags & WPA_STA_AUTHENTICATED)
3660 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
95ab6063
AN
3661
3662 return f;
3663}
3664
3665
7c7e7877
BC
3666#ifdef CONFIG_MESH
3667static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
3668{
3669 switch (state) {
3670 case PLINK_LISTEN:
3671 return NL80211_PLINK_LISTEN;
3672 case PLINK_OPEN_SENT:
3673 return NL80211_PLINK_OPN_SNT;
3674 case PLINK_OPEN_RCVD:
3675 return NL80211_PLINK_OPN_RCVD;
3676 case PLINK_CNF_RCVD:
3677 return NL80211_PLINK_CNF_RCVD;
3678 case PLINK_ESTAB:
3679 return NL80211_PLINK_ESTAB;
3680 case PLINK_HOLDING:
3681 return NL80211_PLINK_HOLDING;
3682 case PLINK_BLOCKED:
3683 return NL80211_PLINK_BLOCKED;
3684 default:
3685 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
3686 state);
3687 }
3688 return -1;
3689}
3690#endif /* CONFIG_MESH */
3691
3692
62847751 3693static int wpa_driver_nl80211_sta_add(void *priv,
0f4e8b4f
JM
3694 struct hostapd_sta_add_params *params)
3695{
a2e40bb6
FF
3696 struct i802_bss *bss = priv;
3697 struct wpa_driver_nl80211_data *drv = bss->drv;
8970bae8 3698 struct nl_msg *msg;
95ab6063 3699 struct nl80211_sta_flag_update upd;
0f4e8b4f
JM
3700 int ret = -ENOBUFS;
3701
45b722f1
AN
3702 if ((params->flags & WPA_STA_TDLS_PEER) &&
3703 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
3704 return -EOPNOTSUPP;
3705
e4dea253
JM
3706 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
3707 params->set ? "Set" : "Add", MAC2STR(params->addr));
13f83980
JM
3708 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
3709 NL80211_CMD_NEW_STATION);
3710 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
a862e4a3 3711 goto fail;
5551bc9c
BC
3712
3713 if (!params->set || (params->flags & WPA_STA_TDLS_PEER)) {
5551bc9c
BC
3714 wpa_hexdump(MSG_DEBUG, " * supported rates",
3715 params->supp_rates, params->supp_rates_len);
a862e4a3
JM
3716 wpa_printf(MSG_DEBUG, " * capability=0x%x",
3717 params->capability);
3718 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
3719 params->supp_rates_len, params->supp_rates) ||
3720 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
3721 params->capability))
3722 goto fail;
e503861f
JM
3723
3724 if (params->ht_capabilities) {
3725 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
3726 (u8 *) params->ht_capabilities,
3727 sizeof(*params->ht_capabilities));
a862e4a3
JM
3728 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
3729 sizeof(*params->ht_capabilities),
3730 params->ht_capabilities))
3731 goto fail;
e503861f
JM
3732 }
3733
3734 if (params->vht_capabilities) {
3735 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
3736 (u8 *) params->vht_capabilities,
3737 sizeof(*params->vht_capabilities));
a862e4a3
JM
3738 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
3739 sizeof(*params->vht_capabilities),
3740 params->vht_capabilities))
3741 goto fail;
e503861f
JM
3742 }
3743
e503861f
JM
3744 if (params->ext_capab) {
3745 wpa_hexdump(MSG_DEBUG, " * ext_capab",
3746 params->ext_capab, params->ext_capab_len);
a862e4a3
JM
3747 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
3748 params->ext_capab_len, params->ext_capab))
3749 goto fail;
e503861f 3750 }
5551bc9c 3751 }
45b722f1 3752 if (!params->set) {
f11b72c3
JM
3753 if (params->aid) {
3754 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
a862e4a3
JM
3755 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
3756 goto fail;
f11b72c3
JM
3757 } else {
3758 /*
3759 * cfg80211 validates that AID is non-zero, so we have
3760 * to make this a non-zero value for the TDLS case where
3761 * a dummy STA entry is used for now.
3762 */
3763 wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
a862e4a3
JM
3764 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
3765 goto fail;
f11b72c3 3766 }
e4dea253
JM
3767 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
3768 params->listen_interval);
a862e4a3
JM
3769 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
3770 params->listen_interval))
3771 goto fail;
e112764e
JM
3772 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
3773 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
a862e4a3
JM
3774 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
3775 goto fail;
45b722f1 3776 }
05a8d422 3777
8a458116
MK
3778 if (params->vht_opmode_enabled) {
3779 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
a862e4a3
JM
3780 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
3781 params->vht_opmode))
3782 goto fail;
8a458116
MK
3783 }
3784
efc64886
SD
3785 if (params->supp_channels) {
3786 wpa_hexdump(MSG_DEBUG, " * supported channels",
3787 params->supp_channels, params->supp_channels_len);
a862e4a3
JM
3788 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
3789 params->supp_channels_len, params->supp_channels))
3790 goto fail;
efc64886
SD
3791 }
3792
3793 if (params->supp_oper_classes) {
3794 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
3795 params->supp_oper_classes,
3796 params->supp_oper_classes_len);
a862e4a3
JM
3797 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
3798 params->supp_oper_classes_len,
3799 params->supp_oper_classes))
3800 goto fail;
efc64886
SD
3801 }
3802
95ab6063 3803 os_memset(&upd, 0, sizeof(upd));
7e31703e
BC
3804 upd.set = sta_flags_nl80211(params->flags);
3805 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
e4dea253
JM
3806 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
3807 upd.set, upd.mask);
a862e4a3
JM
3808 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
3809 goto fail;
95ab6063 3810
7c7e7877 3811#ifdef CONFIG_MESH
a862e4a3
JM
3812 if (params->plink_state &&
3813 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
3814 sta_plink_state_nl80211(params->plink_state)))
3815 goto fail;
7c7e7877
BC
3816#endif /* CONFIG_MESH */
3817
774bfa62 3818 if (params->flags & WPA_STA_WMM) {
8970bae8
JB
3819 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
3820
e4dea253 3821 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
a862e4a3
JM
3822 if (!wme ||
3823 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
3824 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
3825 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
3826 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
3827 WMM_QOSINFO_STA_SP_MASK))
3828 goto fail;
8970bae8 3829 nla_nest_end(msg, wme);
774bfa62
EP
3830 }
3831
0f4e8b4f 3832 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 3833 msg = NULL;
0f4e8b4f 3834 if (ret)
45b722f1
AN
3835 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
3836 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
3837 strerror(-ret));
0f4e8b4f
JM
3838 if (ret == -EEXIST)
3839 ret = 0;
a862e4a3 3840fail:
5883168a 3841 nlmsg_free(msg);
0f4e8b4f
JM
3842 return ret;
3843}
3844
3845
97ed9a06
KP
3846static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
3847{
3848#ifdef CONFIG_LIBNL3_ROUTE
3849 struct wpa_driver_nl80211_data *drv = bss->drv;
3850 struct rtnl_neigh *rn;
3851 struct nl_addr *nl_addr;
3852 int err;
3853
3854 rn = rtnl_neigh_alloc();
3855 if (!rn)
3856 return;
3857
3858 rtnl_neigh_set_family(rn, AF_BRIDGE);
3859 rtnl_neigh_set_ifindex(rn, bss->ifindex);
3860 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
3861 if (!nl_addr) {
3862 rtnl_neigh_put(rn);
3863 return;
3864 }
3865 rtnl_neigh_set_lladdr(rn, nl_addr);
3866
3867 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
3868 if (err < 0) {
3869 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
3870 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
3871 bss->ifindex, nl_geterror(err));
3872 } else {
3873 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
3874 MACSTR, MAC2STR(addr));
3875 }
3876
3877 nl_addr_put(nl_addr);
3878 rtnl_neigh_put(rn);
3879#endif /* CONFIG_LIBNL3_ROUTE */
3880}
3881
3882
59d7148a
JM
3883static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
3884 int deauth, u16 reason_code)
0f4e8b4f 3885{
a2e40bb6 3886 struct wpa_driver_nl80211_data *drv = bss->drv;
0f4e8b4f
JM
3887 struct nl_msg *msg;
3888 int ret;
3889
13f83980 3890 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
a862e4a3
JM
3891 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
3892 (deauth == 0 &&
3893 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
3894 WLAN_FC_STYPE_DISASSOC)) ||
3895 (deauth == 1 &&
3896 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
3897 WLAN_FC_STYPE_DEAUTH)) ||
3898 (reason_code &&
3899 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
3900 nlmsg_free(msg);
3901 return -ENOBUFS;
3902 }
0f4e8b4f
JM
3903
3904 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
83e7bb0e
JM
3905 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
3906 " --> %d (%s)",
3907 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
97ed9a06
KP
3908
3909 if (drv->rtnl_sk)
3910 rtnl_neigh_delete_fdb_entry(bss, addr);
3911
0f4e8b4f
JM
3912 if (ret == -ENOENT)
3913 return 0;
3914 return ret;
0f4e8b4f
JM
3915}
3916
1581b38b 3917
f3407c66 3918void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
0915d02c
JM
3919{
3920 struct nl_msg *msg;
de884303 3921 struct wpa_driver_nl80211_data *drv2;
0915d02c 3922
c6e8e8e4
JM
3923 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
3924
2135f224 3925 /* stop listening for EAPOL on this interface */
de884303
JM
3926 dl_list_for_each(drv2, &drv->global->interfaces,
3927 struct wpa_driver_nl80211_data, list)
3928 del_ifidx(drv2, ifidx);
2135f224 3929
95376e1a 3930 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
0915d02c
JM
3931 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
3932 return;
e748062b 3933 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
0915d02c
JM
3934}
3935
3936
a1922f93
JM
3937static const char * nl80211_iftype_str(enum nl80211_iftype mode)
3938{
3939 switch (mode) {
3940 case NL80211_IFTYPE_ADHOC:
3941 return "ADHOC";
3942 case NL80211_IFTYPE_STATION:
3943 return "STATION";
3944 case NL80211_IFTYPE_AP:
3945 return "AP";
1045ec36
JM
3946 case NL80211_IFTYPE_AP_VLAN:
3947 return "AP_VLAN";
3948 case NL80211_IFTYPE_WDS:
3949 return "WDS";
a1922f93
JM
3950 case NL80211_IFTYPE_MONITOR:
3951 return "MONITOR";
1045ec36
JM
3952 case NL80211_IFTYPE_MESH_POINT:
3953 return "MESH_POINT";
a1922f93
JM
3954 case NL80211_IFTYPE_P2P_CLIENT:
3955 return "P2P_CLIENT";
3956 case NL80211_IFTYPE_P2P_GO:
3957 return "P2P_GO";
7aad838c
NS
3958 case NL80211_IFTYPE_P2P_DEVICE:
3959 return "P2P_DEVICE";
a1922f93
JM
3960 default:
3961 return "unknown";
3962 }
3963}
3964
3965
a35187e7
KH
3966static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
3967 const char *ifname,
3968 enum nl80211_iftype iftype,
d6dcfcda
DS
3969 const u8 *addr, int wds,
3970 int (*handler)(struct nl_msg *, void *),
3971 void *arg)
0915d02c 3972{
8970bae8 3973 struct nl_msg *msg;
0915d02c
JM
3974 int ifidx;
3975 int ret = -ENOBUFS;
3976
a1922f93
JM
3977 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
3978 iftype, nl80211_iftype_str(iftype));
3979
56f77852
JM
3980 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
3981 if (!msg ||
a862e4a3
JM
3982 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
3983 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
3984 goto fail;
0915d02c
JM
3985
3986 if (iftype == NL80211_IFTYPE_MONITOR) {
8970bae8 3987 struct nlattr *flags;
0915d02c 3988
8970bae8 3989 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
a862e4a3
JM
3990 if (!flags ||
3991 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
3992 goto fail;
0915d02c 3993
8970bae8 3994 nla_nest_end(msg, flags);
fbbfcbac 3995 } else if (wds) {
a862e4a3
JM
3996 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
3997 goto fail;
0915d02c
JM
3998 }
3999
52f5877a
IP
4000 /*
4001 * Tell cfg80211 that the interface belongs to the socket that created
4002 * it, and the interface should be deleted when the socket is closed.
4003 */
a862e4a3
JM
4004 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4005 goto fail;
52f5877a 4006
d6dcfcda 4007 ret = send_and_recv_msgs(drv, msg, handler, arg);
5883168a 4008 msg = NULL;
0915d02c 4009 if (ret) {
a862e4a3 4010 fail:
5883168a 4011 nlmsg_free(msg);
a35187e7
KH
4012 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4013 ifname, ret, strerror(-ret));
0915d02c
JM
4014 return ret;
4015 }
4016
f632e483 4017 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
6bae92e0 4018 return 0;
6bae92e0 4019
0915d02c 4020 ifidx = if_nametoindex(ifname);
c6e8e8e4
JM
4021 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4022 ifname, ifidx);
0915d02c
JM
4023
4024 if (ifidx <= 0)
4025 return -1;
4026
147848ec
JM
4027 /*
4028 * Some virtual interfaces need to process EAPOL packets and events on
4029 * the parent interface. This is used mainly with hostapd.
4030 */
4031 if (drv->hostapd ||
4032 iftype == NL80211_IFTYPE_AP_VLAN ||
4033 iftype == NL80211_IFTYPE_WDS ||
4034 iftype == NL80211_IFTYPE_MONITOR) {
4035 /* start listening for EAPOL on this interface */
4036 add_ifidx(drv, ifidx);
4037 }
2135f224 4038
7bfc47c3 4039 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
c81eff1a 4040 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
41d931ee
JM
4041 nl80211_remove_iface(drv, ifidx);
4042 return -1;
2135f224 4043 }
2135f224 4044
0915d02c
JM
4045 return ifidx;
4046}
22a7c9d7
JM
4047
4048
f3407c66
JM
4049int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4050 const char *ifname, enum nl80211_iftype iftype,
4051 const u8 *addr, int wds,
4052 int (*handler)(struct nl_msg *, void *),
4053 void *arg, int use_existing)
a35187e7
KH
4054{
4055 int ret;
4056
d6dcfcda
DS
4057 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4058 arg);
a35187e7 4059
ffbf1eaa 4060 /* if error occurred and interface exists already */
a35187e7 4061 if (ret == -ENFILE && if_nametoindex(ifname)) {
2aec4f3c
JM
4062 if (use_existing) {
4063 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4064 ifname);
6997f8ba
JM
4065 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4066 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4067 addr) < 0 &&
4068 (linux_set_iface_flags(drv->global->ioctl_sock,
4069 ifname, 0) < 0 ||
4070 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4071 addr) < 0 ||
4072 linux_set_iface_flags(drv->global->ioctl_sock,
4073 ifname, 1) < 0))
4074 return -1;
2aec4f3c
JM
4075 return -ENFILE;
4076 }
a35187e7
KH
4077 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4078
4079 /* Try to remove the interface that was already there. */
4080 nl80211_remove_iface(drv, if_nametoindex(ifname));
4081
4082 /* Try to create the interface again */
fbbfcbac 4083 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
d6dcfcda 4084 wds, handler, arg);
a35187e7
KH
4085 }
4086
3e208481
JM
4087 if (ret >= 0 && is_p2p_net_interface(iftype)) {
4088 wpa_printf(MSG_DEBUG,
4089 "nl80211: Interface %s created for P2P - disable 11b rates",
4090 ifname);
4e5cb1a3 4091 nl80211_disable_11b_rates(drv, ret, 1);
3e208481 4092 }
4e5cb1a3 4093
a35187e7
KH
4094 return ret;
4095}
0915d02c 4096
2135f224 4097
3fd1cefb
JB
4098static int nl80211_setup_ap(struct i802_bss *bss)
4099{
4100 struct wpa_driver_nl80211_data *drv = bss->drv;
4101
748c0ac0
JM
4102 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4103 bss->ifname, drv->device_ap_sme, drv->use_monitor);
36488c05 4104
a11241fa
JB
4105 /*
4106 * Disable Probe Request reporting unless we need it in this way for
4107 * devices that include the AP SME, in the other case (unless using
4108 * monitor iface) we'll get it through the nl_mgmt socket instead.
4109 */
4110 if (!drv->device_ap_sme)
4111 wpa_driver_nl80211_probe_req_report(bss, 0);
4112
4113 if (!drv->device_ap_sme && !drv->use_monitor)
4114 if (nl80211_mgmt_subscribe_ap(bss))
4115 return -1;
4116
a6cc0602
JM
4117 if (drv->device_ap_sme && !drv->use_monitor)
4118 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
4119 return -1;
4120
a11241fa 4121 if (!drv->device_ap_sme && drv->use_monitor &&
3fd1cefb
JB
4122 nl80211_create_monitor_interface(drv) &&
4123 !drv->device_ap_sme)
4124 return -1;
4125
4126 if (drv->device_ap_sme &&
4127 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4128 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4129 "Probe Request frame reporting in AP mode");
4130 /* Try to survive without this */
4131 }
4132
4133 return 0;
4134}
4135
4136
4137static void nl80211_teardown_ap(struct i802_bss *bss)
4138{
4139 struct wpa_driver_nl80211_data *drv = bss->drv;
4140
748c0ac0
JM
4141 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4142 bss->ifname, drv->device_ap_sme, drv->use_monitor);
a6cc0602 4143 if (drv->device_ap_sme) {
3fd1cefb 4144 wpa_driver_nl80211_probe_req_report(bss, 0);
a6cc0602
JM
4145 if (!drv->use_monitor)
4146 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4147 } else if (drv->use_monitor)
3fd1cefb 4148 nl80211_remove_monitor_interface(drv);
a11241fa 4149 else
36488c05 4150 nl80211_mgmt_unsubscribe(bss, "AP teardown");
a11241fa 4151
3fd1cefb
JB
4152 bss->beacon_set = 0;
4153}
4154
4155
f10bfc9a
JM
4156static int nl80211_send_eapol_data(struct i802_bss *bss,
4157 const u8 *addr, const u8 *data,
d12dab4c 4158 size_t data_len)
f10bfc9a 4159{
d12dab4c
JB
4160 struct sockaddr_ll ll;
4161 int ret;
4162
4163 if (bss->drv->eapol_tx_sock < 0) {
4164 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
f10bfc9a
JM
4165 return -1;
4166 }
4167
d12dab4c
JB
4168 os_memset(&ll, 0, sizeof(ll));
4169 ll.sll_family = AF_PACKET;
4170 ll.sll_ifindex = bss->ifindex;
4171 ll.sll_protocol = htons(ETH_P_PAE);
4172 ll.sll_halen = ETH_ALEN;
4173 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4174 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4175 (struct sockaddr *) &ll, sizeof(ll));
4176 if (ret < 0)
4177 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4178 strerror(errno));
4179
4180 return ret;
f10bfc9a 4181}
5fb1a232 4182
f10bfc9a 4183
db149ac9
JM
4184static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4185
4186static int wpa_driver_nl80211_hapd_send_eapol(
4187 void *priv, const u8 *addr, const u8 *data,
4378fc14 4188 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
db149ac9 4189{
a2e40bb6
FF
4190 struct i802_bss *bss = priv;
4191 struct wpa_driver_nl80211_data *drv = bss->drv;
db149ac9
JM
4192 struct ieee80211_hdr *hdr;
4193 size_t len;
4194 u8 *pos;
4195 int res;
4378fc14 4196 int qos = flags & WPA_STA_WMM;
db149ac9 4197
a11241fa 4198 if (drv->device_ap_sme || !drv->use_monitor)
d12dab4c 4199 return nl80211_send_eapol_data(bss, addr, data, data_len);
f10bfc9a 4200
db149ac9
JM
4201 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4202 data_len;
4203 hdr = os_zalloc(len);
4204 if (hdr == NULL) {
7ac3616d
JM
4205 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4206 (unsigned long) len);
db149ac9
JM
4207 return -1;
4208 }
4209
4210 hdr->frame_control =
4211 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4212 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4213 if (encrypt)
4214 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
db149ac9
JM
4215 if (qos) {
4216 hdr->frame_control |=
4217 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4218 }
db149ac9
JM
4219
4220 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4221 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4222 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4223 pos = (u8 *) (hdr + 1);
4224
db149ac9 4225 if (qos) {
92d521d8
FF
4226 /* Set highest priority in QoS header */
4227 pos[0] = 7;
db149ac9
JM
4228 pos[1] = 0;
4229 pos += 2;
4230 }
db149ac9
JM
4231
4232 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4233 pos += sizeof(rfc1042_header);
4234 WPA_PUT_BE16(pos, ETH_P_PAE);
4235 pos += 2;
4236 memcpy(pos, data, data_len);
4237
55231068
JM
4238 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
4239 0, 0, 0, 0);
db149ac9
JM
4240 if (res < 0) {
4241 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4242 "failed: %d (%s)",
4243 (unsigned long) len, errno, strerror(errno));
4244 }
7bf12757 4245 os_free(hdr);
db149ac9
JM
4246
4247 return res;
4248}
4249
a8d6ffa4 4250
3234cba4
JM
4251static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
4252 int total_flags,
4c32757d 4253 int flags_or, int flags_and)
a8d6ffa4 4254{
a2e40bb6 4255 struct i802_bss *bss = priv;
8970bae8
JB
4256 struct nl_msg *msg;
4257 struct nlattr *flags;
7e76ee9c 4258 struct nl80211_sta_flag_update upd;
a8d6ffa4 4259
0cd9846c
JM
4260 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4261 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4262 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4263 !!(total_flags & WPA_STA_AUTHORIZED));
4264
13f83980 4265 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
a862e4a3
JM
4266 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4267 goto fail;
a8d6ffa4 4268
7e76ee9c
JM
4269 /*
4270 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4271 * can be removed eventually.
4272 */
8970bae8 4273 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
a862e4a3
JM
4274 if (!flags ||
4275 ((total_flags & WPA_STA_AUTHORIZED) &&
4276 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4277 ((total_flags & WPA_STA_WMM) &&
4278 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4279 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4280 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4281 ((total_flags & WPA_STA_MFP) &&
4282 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4283 ((total_flags & WPA_STA_TDLS_PEER) &&
4284 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4285 goto fail;
45b722f1 4286
8970bae8 4287 nla_nest_end(msg, flags);
a8d6ffa4 4288
7e76ee9c
JM
4289 os_memset(&upd, 0, sizeof(upd));
4290 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4291 upd.set = sta_flags_nl80211(flags_or);
a862e4a3
JM
4292 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4293 goto fail;
7e76ee9c 4294
13f83980 4295 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
a862e4a3 4296fail:
5883168a 4297 nlmsg_free(msg);
a8d6ffa4
JM
4298 return -ENOBUFS;
4299}
4300
0915d02c 4301
1581b38b
JM
4302static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4303 struct wpa_driver_associate_params *params)
4304{
708bc8e0 4305 enum nl80211_iftype nlmode, old_mode;
b1f625e0
EP
4306
4307 if (params->p2p) {
046b26a2
JM
4308 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4309 "group (GO)");
b1f625e0
EP
4310 nlmode = NL80211_IFTYPE_P2P_GO;
4311 } else
4312 nlmode = NL80211_IFTYPE_AP;
4313
708bc8e0 4314 old_mode = drv->nlmode;
834ee56f 4315 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
708bc8e0
JM
4316 nl80211_remove_monitor_interface(drv);
4317 return -1;
4318 }
4319
6e9023ea
JM
4320 if (params->freq.freq &&
4321 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
708bc8e0 4322 if (old_mode != nlmode)
834ee56f 4323 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
460456f8 4324 nl80211_remove_monitor_interface(drv);
1581b38b 4325 return -1;
0915d02c 4326 }
1581b38b 4327
1581b38b
JM
4328 return 0;
4329}
1581b38b
JM
4330
4331
666e508c
JM
4332static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
4333 int reset_mode)
5cc4d64b
JM
4334{
4335 struct nl_msg *msg;
9725b784 4336 int ret;
a862e4a3 4337
9725b784 4338 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
5cc4d64b 4339 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5cc4d64b
JM
4340 if (ret) {
4341 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
4342 "(%s)", ret, strerror(-ret));
9725b784
JM
4343 } else {
4344 wpa_printf(MSG_DEBUG,
4345 "nl80211: Leave IBSS request sent successfully");
5cc4d64b
JM
4346 }
4347
666e508c
JM
4348 if (reset_mode &&
4349 wpa_driver_nl80211_set_mode(drv->first_bss,
5d4c78fb
JM
4350 NL80211_IFTYPE_STATION)) {
4351 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4352 "station mode");
4353 }
4354
5cc4d64b
JM
4355 return ret;
4356}
4357
4358
95ff3069
JD
4359static int nl80211_ht_vht_overrides(struct nl_msg *msg,
4360 struct wpa_driver_associate_params *params)
4361{
4362 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
4363 return -1;
4364
4365 if (params->htcaps && params->htcaps_mask) {
4366 int sz = sizeof(struct ieee80211_ht_capabilities);
4367 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
4368 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
4369 params->htcaps_mask, sz);
4370 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
4371 params->htcaps) ||
4372 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
4373 params->htcaps_mask))
4374 return -1;
4375 }
4376
4377#ifdef CONFIG_VHT_OVERRIDES
4378 if (params->disable_vht) {
4379 wpa_printf(MSG_DEBUG, " * VHT disabled");
4380 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
4381 return -1;
4382 }
4383
4384 if (params->vhtcaps && params->vhtcaps_mask) {
4385 int sz = sizeof(struct ieee80211_vht_capabilities);
4386 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
4387 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
4388 params->vhtcaps_mask, sz);
4389 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
4390 params->vhtcaps) ||
4391 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
4392 params->vhtcaps_mask))
4393 return -1;
4394 }
4395#endif /* CONFIG_VHT_OVERRIDES */
4396
4397 return 0;
4398}
4399
4400
5cc4d64b
JM
4401static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
4402 struct wpa_driver_associate_params *params)
4403{
4404 struct nl_msg *msg;
4405 int ret = -1;
4406 int count = 0;
4407
4408 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
4409
4ec68377 4410 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
5cc4d64b
JM
4411 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4412 "IBSS mode");
4413 return -1;
4414 }
4415
4416retry:
9725b784 4417 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
a862e4a3
JM
4418 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
4419 goto fail;
5cc4d64b
JM
4420
4421 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4422 params->ssid, params->ssid_len);
a862e4a3
JM
4423 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4424 goto fail;
5cc4d64b
JM
4425 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4426 drv->ssid_len = params->ssid_len;
4427
85e1fad8
JM
4428 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
4429 nl80211_put_beacon_int(msg, params->beacon_int))
a862e4a3 4430 goto fail;
5cc4d64b
JM
4431
4432 ret = nl80211_set_conn_keys(params, msg);
4433 if (ret)
a862e4a3 4434 goto fail;
5cc4d64b 4435
913e3cf7
NC
4436 if (params->bssid && params->fixed_bssid) {
4437 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
4438 MAC2STR(params->bssid));
a862e4a3
JM
4439 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4440 goto fail;
913e3cf7
NC
4441 }
4442
4d9e6fba
JD
4443 if (params->fixed_freq) {
4444 wpa_printf(MSG_DEBUG, " * fixed_freq");
4445 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
4446 goto fail;
4447 }
4448
4848a38d
JM
4449 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4450 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4451 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
4452 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
e640888c 4453 wpa_printf(MSG_DEBUG, " * control port");
a862e4a3
JM
4454 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4455 goto fail;
e640888c
AQ
4456 }
4457
a95795ad
JM
4458 if (params->wpa_ie) {
4459 wpa_hexdump(MSG_DEBUG,
4460 " * Extra IEs for Beacon/Probe Response frames",
4461 params->wpa_ie, params->wpa_ie_len);
a862e4a3
JM
4462 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
4463 params->wpa_ie))
4464 goto fail;
a95795ad
JM
4465 }
4466
95ff3069
JD
4467 if (nl80211_ht_vht_overrides(msg, params) < 0)
4468 return -1;
4469
5cc4d64b
JM
4470 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4471 msg = NULL;
4472 if (ret) {
4473 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
4474 ret, strerror(-ret));
4475 count++;
4476 if (ret == -EALREADY && count == 1) {
4477 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
4478 "forced leave");
666e508c 4479 nl80211_leave_ibss(drv, 0);
5cc4d64b
JM
4480 nlmsg_free(msg);
4481 goto retry;
4482 }
a862e4a3
JM
4483 } else {
4484 wpa_printf(MSG_DEBUG,
4485 "nl80211: Join IBSS request sent successfully");
5cc4d64b 4486 }
5cc4d64b 4487
a862e4a3 4488fail:
5cc4d64b
JM
4489 nlmsg_free(msg);
4490 return ret;
4491}
4492
4493
a0bdd191
JM
4494static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
4495 struct wpa_driver_associate_params *params,
4496 struct nl_msg *msg)
cfaab580 4497{
cfaab580
ZY
4498 if (params->bssid) {
4499 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
4500 MAC2STR(params->bssid));
a862e4a3
JM
4501 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4502 return -1;
cfaab580 4503 }
a0bdd191 4504
7ac7fd43
DS
4505 if (params->bssid_hint) {
4506 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
4507 MAC2STR(params->bssid_hint));
a862e4a3
JM
4508 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
4509 params->bssid_hint))
4510 return -1;
7ac7fd43
DS
4511 }
4512
4ec68377
JD
4513 if (params->freq.freq) {
4514 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
a862e4a3
JM
4515 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
4516 params->freq.freq))
4517 return -1;
4ec68377 4518 drv->assoc_freq = params->freq.freq;
30158a0d
SD
4519 } else
4520 drv->assoc_freq = 0;
a0bdd191 4521
7ac7fd43
DS
4522 if (params->freq_hint) {
4523 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
a862e4a3
JM
4524 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
4525 params->freq_hint))
4526 return -1;
7ac7fd43
DS
4527 }
4528
1f6c0ab8
BS
4529 if (params->bg_scan_period >= 0) {
4530 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
4531 params->bg_scan_period);
a862e4a3
JM
4532 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
4533 params->bg_scan_period))
4534 return -1;
1f6c0ab8 4535 }
a0bdd191 4536
cfaab580
ZY
4537 if (params->ssid) {
4538 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4539 params->ssid, params->ssid_len);
a862e4a3
JM
4540 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
4541 params->ssid))
4542 return -1;
cfaab580 4543 if (params->ssid_len > sizeof(drv->ssid))
a862e4a3 4544 return -1;
cfaab580
ZY
4545 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4546 drv->ssid_len = params->ssid_len;
4547 }
a0bdd191 4548
cfaab580 4549 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
a862e4a3
JM
4550 if (params->wpa_ie &&
4551 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
4552 return -1;
cfaab580 4553
64fa840a
JM
4554 if (params->wpa_proto) {
4555 enum nl80211_wpa_versions ver = 0;
cfaab580 4556
64fa840a
JM
4557 if (params->wpa_proto & WPA_PROTO_WPA)
4558 ver |= NL80211_WPA_VERSION_1;
4559 if (params->wpa_proto & WPA_PROTO_RSN)
4560 ver |= NL80211_WPA_VERSION_2;
cfaab580 4561
64fa840a 4562 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
a862e4a3
JM
4563 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
4564 return -1;
cfaab580
ZY
4565 }
4566
4848a38d 4567 if (params->pairwise_suite != WPA_CIPHER_NONE) {
a0bdd191
JM
4568 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
4569 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
a862e4a3
JM
4570 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
4571 cipher))
4572 return -1;
cfaab580
ZY
4573 }
4574
ae6f9272
JM
4575 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
4576 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
4577 /*
4578 * This is likely to work even though many drivers do not
4579 * advertise support for operations without GTK.
4580 */
4581 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
4582 } else if (params->group_suite != WPA_CIPHER_NONE) {
a0bdd191
JM
4583 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
4584 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
a862e4a3
JM
4585 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
4586 return -1;
cfaab580
ZY
4587 }
4588
4848a38d
JM
4589 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
4590 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4591 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
4592 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
163f801e 4593 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
8802326f
JJ
4594 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
4595 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
666497c8 4596 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5e3b5197
JM
4597 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
4598 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
cfaab580
ZY
4599 int mgmt = WLAN_AKM_SUITE_PSK;
4600
4601 switch (params->key_mgmt_suite) {
4848a38d 4602 case WPA_KEY_MGMT_CCKM:
369c8d7b
JM
4603 mgmt = WLAN_AKM_SUITE_CCKM;
4604 break;
4848a38d 4605 case WPA_KEY_MGMT_IEEE8021X:
cfaab580
ZY
4606 mgmt = WLAN_AKM_SUITE_8021X;
4607 break;
4848a38d 4608 case WPA_KEY_MGMT_FT_IEEE8021X:
6a1ce395
DG
4609 mgmt = WLAN_AKM_SUITE_FT_8021X;
4610 break;
4848a38d 4611 case WPA_KEY_MGMT_FT_PSK:
6a1ce395
DG
4612 mgmt = WLAN_AKM_SUITE_FT_PSK;
4613 break;
8802326f
JJ
4614 case WPA_KEY_MGMT_IEEE8021X_SHA256:
4615 mgmt = WLAN_AKM_SUITE_8021X_SHA256;
4616 break;
4617 case WPA_KEY_MGMT_PSK_SHA256:
4618 mgmt = WLAN_AKM_SUITE_PSK_SHA256;
4619 break;
163f801e
JM
4620 case WPA_KEY_MGMT_OSEN:
4621 mgmt = WLAN_AKM_SUITE_OSEN;
4622 break;
666497c8
JM
4623 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
4624 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B;
4625 break;
5e3b5197
JM
4626 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
4627 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B_192;
4628 break;
4848a38d 4629 case WPA_KEY_MGMT_PSK:
cfaab580
ZY
4630 default:
4631 mgmt = WLAN_AKM_SUITE_PSK;
4632 break;
4633 }
163f801e 4634 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
a862e4a3
JM
4635 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
4636 return -1;
cfaab580
ZY
4637 }
4638
a862e4a3
JM
4639 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
4640 return -1;
a0bdd191 4641
a862e4a3
JM
4642 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
4643 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
4644 return -1;
a565084f 4645
58162adf
AK
4646 if (params->rrm_used) {
4647 u32 drv_rrm_flags = drv->capa.rrm_flags;
4648 if (!(drv_rrm_flags &
4649 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) ||
a862e4a3
JM
4650 !(drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET) ||
4651 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
4652 return -1;
58162adf
AK
4653 }
4654
95ff3069 4655 if (nl80211_ht_vht_overrides(msg, params) < 0)
a862e4a3 4656 return -1;
80e8a5ee 4657
a0bdd191
JM
4658 if (params->p2p)
4659 wpa_printf(MSG_DEBUG, " * P2P group");
4660
4661 return 0;
a0bdd191
JM
4662}
4663
4664
4665static int wpa_driver_nl80211_try_connect(
4666 struct wpa_driver_nl80211_data *drv,
4667 struct wpa_driver_associate_params *params)
4668{
4669 struct nl_msg *msg;
4670 enum nl80211_auth_type type;
4671 int ret;
4672 int algs;
4673
b41f2684
CL
4674 if (params->req_key_mgmt_offload && params->psk &&
4675 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
4676 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
4677 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
4678 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
4679 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
4680 if (ret)
4681 return ret;
4682 }
4683
9725b784
JM
4684 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
4685 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
a0bdd191
JM
4686 if (!msg)
4687 return -1;
4688
a0bdd191
JM
4689 ret = nl80211_connect_common(drv, params, msg);
4690 if (ret)
a862e4a3 4691 goto fail;
a0bdd191
JM
4692
4693 algs = 0;
4694 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4695 algs++;
4696 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4697 algs++;
4698 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4699 algs++;
4700 if (algs > 1) {
4701 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
4702 "selection");
4703 goto skip_auth_type;
4704 }
4705
4706 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
4707 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
4708 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
4709 type = NL80211_AUTHTYPE_SHARED_KEY;
4710 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
4711 type = NL80211_AUTHTYPE_NETWORK_EAP;
4712 else if (params->auth_alg & WPA_AUTH_ALG_FT)
4713 type = NL80211_AUTHTYPE_FT;
4714 else
a862e4a3 4715 goto fail;
a0bdd191
JM
4716
4717 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
a862e4a3
JM
4718 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
4719 goto fail;
a0bdd191
JM
4720
4721skip_auth_type:
cfaab580
ZY
4722 ret = nl80211_set_conn_keys(params, msg);
4723 if (ret)
a862e4a3 4724 goto fail;
cfaab580
ZY
4725
4726 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4727 msg = NULL;
4728 if (ret) {
4729 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
4730 "(%s)", ret, strerror(-ret));
a862e4a3
JM
4731 } else {
4732 wpa_printf(MSG_DEBUG,
4733 "nl80211: Connect request send successfully");
cfaab580 4734 }
cfaab580 4735
a862e4a3 4736fail:
cfaab580
ZY
4737 nlmsg_free(msg);
4738 return ret;
4739
4740}
4741
4742
a8c5b43a
CW
4743static int wpa_driver_nl80211_connect(
4744 struct wpa_driver_nl80211_data *drv,
4745 struct wpa_driver_associate_params *params)
4746{
0d4e3d1d
JJ
4747 int ret;
4748
4749 /* Store the connection attempted bssid for future use */
4750 if (params->bssid)
4751 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
4752 else
4753 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
4754
4755 ret = wpa_driver_nl80211_try_connect(drv, params);
a8c5b43a
CW
4756 if (ret == -EALREADY) {
4757 /*
4758 * cfg80211 does not currently accept new connections if
4759 * we are already connected. As a workaround, force
4760 * disconnection and try again.
4761 */
4762 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
4763 "disconnecting before reassociation "
4764 "attempt");
4765 if (wpa_driver_nl80211_disconnect(
4766 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
4767 return -1;
a8c5b43a
CW
4768 ret = wpa_driver_nl80211_try_connect(drv, params);
4769 }
4770 return ret;
4771}
4772
4773
c2a04078
JM
4774static int wpa_driver_nl80211_associate(
4775 void *priv, struct wpa_driver_associate_params *params)
4776{
a2e40bb6
FF
4777 struct i802_bss *bss = priv;
4778 struct wpa_driver_nl80211_data *drv = bss->drv;
a862e4a3 4779 int ret = -1;
c2a04078
JM
4780 struct nl_msg *msg;
4781
4bd71954
JM
4782 nl80211_unmask_11b_rates(bss);
4783
5cc4d64b 4784 if (params->mode == IEEE80211_MODE_AP)
1581b38b 4785 return wpa_driver_nl80211_ap(drv, params);
1581b38b 4786
5cc4d64b
JM
4787 if (params->mode == IEEE80211_MODE_IBSS)
4788 return wpa_driver_nl80211_ibss(drv, params);
4789
4a867032 4790 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
b1f625e0
EP
4791 enum nl80211_iftype nlmode = params->p2p ?
4792 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
4793
4794 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
4a867032 4795 return -1;
cfaab580 4796 return wpa_driver_nl80211_connect(drv, params);
4a867032 4797 }
cfaab580 4798
add9b7a4 4799 nl80211_mark_disconnected(drv);
c2a04078 4800
c2a04078
JM
4801 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
4802 drv->ifindex);
9725b784
JM
4803 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
4804 if (!msg)
4805 return -1;
c2a04078 4806
a0bdd191
JM
4807 ret = nl80211_connect_common(drv, params, msg);
4808 if (ret)
a862e4a3 4809 goto fail;
01652550 4810
62fa124c
JM
4811 if (params->prev_bssid) {
4812 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
4813 MAC2STR(params->prev_bssid));
a862e4a3
JM
4814 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
4815 params->prev_bssid))
4816 goto fail;
62fa124c
JM
4817 }
4818
c2a04078
JM
4819 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4820 msg = NULL;
4821 if (ret) {
3b7ea880
BG
4822 wpa_dbg(drv->ctx, MSG_DEBUG,
4823 "nl80211: MLME command failed (assoc): ret=%d (%s)",
4824 ret, strerror(-ret));
8856462d 4825 nl80211_dump_scan(drv);
a862e4a3
JM
4826 } else {
4827 wpa_printf(MSG_DEBUG,
4828 "nl80211: Association request send successfully");
c2a04078 4829 }
c2a04078 4830
a862e4a3 4831fail:
c2a04078
JM
4832 nlmsg_free(msg);
4833 return ret;
4834}
3f5285e8
JM
4835
4836
ad1e68e6 4837static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
a1922f93 4838 int ifindex, enum nl80211_iftype mode)
3f5285e8 4839{
3f5285e8 4840 struct nl_msg *msg;
ad1e68e6
JM
4841 int ret = -ENOBUFS;
4842
a1922f93
JM
4843 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
4844 ifindex, mode, nl80211_iftype_str(mode));
4845
56f77852
JM
4846 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
4847 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
a862e4a3 4848 goto fail;
ad1e68e6
JM
4849
4850 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5883168a 4851 msg = NULL;
ad1e68e6
JM
4852 if (!ret)
4853 return 0;
a862e4a3 4854fail:
5883168a 4855 nlmsg_free(msg);
ad1e68e6
JM
4856 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
4857 " %d (%s)", ifindex, mode, ret, strerror(-ret));
4858 return ret;
4859}
4860
4861
3c5d34e3
CW
4862static int wpa_driver_nl80211_set_mode_impl(
4863 struct i802_bss *bss,
4864 enum nl80211_iftype nlmode,
4865 struct hostapd_freq_params *desired_freq_params)
ad1e68e6 4866{
a2e40bb6 4867 struct wpa_driver_nl80211_data *drv = bss->drv;
ad1e68e6 4868 int ret = -1;
26af9dca 4869 int i;
86957e62 4870 int was_ap = is_ap_interface(drv->nlmode);
671a5039 4871 int res;
ebffdbc4 4872 int mode_switch_res;
1581b38b 4873
ebffdbc4
CW
4874 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
4875 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
4876 mode_switch_res = 0;
8e12685c 4877
ebffdbc4 4878 if (mode_switch_res == 0) {
ad1e68e6 4879 drv->nlmode = nlmode;
460456f8
JM
4880 ret = 0;
4881 goto done;
ad1e68e6 4882 }
3f5285e8 4883
ebffdbc4 4884 if (mode_switch_res == -ENODEV)
671a5039
JM
4885 return -1;
4886
460456f8 4887 if (nlmode == drv->nlmode) {
c6e8e8e4
JM
4888 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
4889 "requested mode - ignore error");
460456f8
JM
4890 ret = 0;
4891 goto done; /* Already in the requested mode */
4892 }
3f5285e8 4893
3f5285e8
JM
4894 /* mac80211 doesn't allow mode changes while the device is up, so
4895 * take the device down, try to set the mode again, and bring the
4896 * device back up.
4897 */
26af9dca
JM
4898 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
4899 "interface down");
4900 for (i = 0; i < 10; i++) {
91724d6f 4901 res = i802_set_iface_flags(bss, 0);
6e8183d7
JM
4902 if (res == -EACCES || res == -ENODEV)
4903 break;
ebffdbc4 4904 if (res != 0) {
26af9dca
JM
4905 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
4906 "interface down");
ebffdbc4
CW
4907 os_sleep(0, 100000);
4908 continue;
4909 }
3c5d34e3
CW
4910
4911 /*
4912 * Setting the mode will fail for some drivers if the phy is
4913 * on a frequency that the mode is disallowed in.
4914 */
4915 if (desired_freq_params) {
bdf5ccb2 4916 res = nl80211_set_channel(bss, desired_freq_params, 0);
3c5d34e3
CW
4917 if (res) {
4918 wpa_printf(MSG_DEBUG,
4919 "nl80211: Failed to set frequency on interface");
4920 }
4921 }
4922
ebffdbc4
CW
4923 /* Try to set the mode again while the interface is down */
4924 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
4925 if (mode_switch_res == -EBUSY) {
4926 wpa_printf(MSG_DEBUG,
4927 "nl80211: Delaying mode set while interface going down");
4928 os_sleep(0, 100000);
4929 continue;
4930 }
4931 ret = mode_switch_res;
4932 break;
3f5285e8
JM
4933 }
4934
c6e8e8e4
JM
4935 if (!ret) {
4936 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
4937 "interface is down");
ad1e68e6 4938 drv->nlmode = nlmode;
7d9c3698 4939 drv->ignore_if_down_event = 1;
c6e8e8e4 4940 }
ad1e68e6 4941
ebffdbc4
CW
4942 /* Bring the interface back up */
4943 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
4944 if (res != 0) {
4945 wpa_printf(MSG_DEBUG,
4946 "nl80211: Failed to set interface up after switching mode");
4947 ret = -1;
4948 }
4949
460456f8 4950done:
3fd1cefb
JB
4951 if (ret) {
4952 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
4953 "from %d failed", nlmode, drv->nlmode);
4954 return ret;
4955 }
4956
3e208481
JM
4957 if (is_p2p_net_interface(nlmode)) {
4958 wpa_printf(MSG_DEBUG,
4959 "nl80211: Interface %s mode change to P2P - disable 11b rates",
4960 bss->ifname);
edb9bfba 4961 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
3e208481
JM
4962 } else if (drv->disabled_11b_rates) {
4963 wpa_printf(MSG_DEBUG,
4964 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
4965 bss->ifname);
1d0c6fb1 4966 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3e208481 4967 }
edb9bfba 4968
3fd1cefb 4969 if (is_ap_interface(nlmode)) {
36488c05 4970 nl80211_mgmt_unsubscribe(bss, "start AP");
460456f8 4971 /* Setup additional AP mode functionality if needed */
3fd1cefb 4972 if (nl80211_setup_ap(bss))
460456f8 4973 return -1;
3fd1cefb 4974 } else if (was_ap) {
460456f8 4975 /* Remove additional AP mode functionality */
3fd1cefb 4976 nl80211_teardown_ap(bss);
a11241fa 4977 } else {
36488c05 4978 nl80211_mgmt_unsubscribe(bss, "mode change");
460456f8 4979 }
460456f8 4980
afb0550a
BC
4981 if (is_mesh_interface(nlmode) &&
4982 nl80211_mgmt_subscribe_mesh(bss))
4983 return -1;
4984
873d0fcf 4985 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
afb0550a 4986 !is_mesh_interface(nlmode) &&
a11241fa
JB
4987 nl80211_mgmt_subscribe_non_ap(bss) < 0)
4988 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
4989 "frame processing - ignore for now");
08359050 4990
3fd1cefb 4991 return 0;
3f5285e8
JM
4992}
4993
4994
f3407c66
JM
4995int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
4996 enum nl80211_iftype nlmode)
3c5d34e3
CW
4997{
4998 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
4999}
5000
5001
4ec68377
JD
5002static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
5003 struct hostapd_freq_params *freq)
3c5d34e3 5004{
3c5d34e3 5005 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
4ec68377 5006 freq);
3c5d34e3 5007}
65d645ce
AS
5008
5009
3f5285e8
JM
5010static int wpa_driver_nl80211_get_capa(void *priv,
5011 struct wpa_driver_capa *capa)
5012{
a2e40bb6
FF
5013 struct i802_bss *bss = priv;
5014 struct wpa_driver_nl80211_data *drv = bss->drv;
65d645ce 5015
3f5285e8
JM
5016 if (!drv->has_capability)
5017 return -1;
5018 os_memcpy(capa, &drv->capa, sizeof(*capa));
8cd6b7bc
JB
5019 if (drv->extended_capa && drv->extended_capa_mask) {
5020 capa->extended_capa = drv->extended_capa;
5021 capa->extended_capa_mask = drv->extended_capa_mask;
5022 capa->extended_capa_len = drv->extended_capa_len;
5023 }
851b0c55 5024
906bc4c7 5025 return 0;
3f5285e8
JM
5026}
5027
5028
5029static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5030{
a2e40bb6
FF
5031 struct i802_bss *bss = priv;
5032 struct wpa_driver_nl80211_data *drv = bss->drv;
3f5285e8 5033
90a545cc
JM
5034 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
5035 bss->ifname, drv->operstate, state,
5036 state ? "UP" : "DORMANT");
3f5285e8 5037 drv->operstate = state;
36d84860 5038 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
e2d02c29 5039 state ? IF_OPER_UP : IF_OPER_DORMANT);
3f5285e8
JM
5040}
5041
01652550
JM
5042
5043static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5044{
a2e40bb6
FF
5045 struct i802_bss *bss = priv;
5046 struct wpa_driver_nl80211_data *drv = bss->drv;
01652550
JM
5047 struct nl_msg *msg;
5048 struct nl80211_sta_flag_update upd;
a862e4a3 5049 int ret;
2eef5177
JM
5050
5051 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
5052 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
5053 return 0;
5054 }
01652550 5055
1ba51ec0
JM
5056 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
5057 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
5058
01652550
JM
5059 os_memset(&upd, 0, sizeof(upd));
5060 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5061 if (authorized)
5062 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
a862e4a3 5063
13f83980 5064 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
a862e4a3
JM
5065 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
5066 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
5067 nlmsg_free(msg);
5068 return -ENOBUFS;
5069 }
01652550 5070
2eef5177 5071 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2eef5177
JM
5072 if (!ret)
5073 return 0;
2eef5177
JM
5074 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
5075 ret, strerror(-ret));
5076 return ret;
01652550
JM
5077}
5078
3f5285e8 5079
f07ead6a
JM
5080/* Set kernel driver on given frequency (MHz) */
5081static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
c5121837 5082{
f07ead6a 5083 struct i802_bss *bss = priv;
13a524a3 5084 return nl80211_set_channel(bss, freq, 0);
c5121837
JM
5085}
5086
f7b3920c 5087
c5121837
JM
5088static inline int min_int(int a, int b)
5089{
5090 if (a < b)
5091 return a;
5092 return b;
5093}
5094
5095
5096static int get_key_handler(struct nl_msg *msg, void *arg)
5097{
5098 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5099 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5100
5101 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5102 genlmsg_attrlen(gnlh, 0), NULL);
5103
5104 /*
5105 * TODO: validate the key index and mac address!
5106 * Otherwise, there's a race condition as soon as
5107 * the kernel starts sending key notifications.
5108 */
5109
5110 if (tb[NL80211_ATTR_KEY_SEQ])
5111 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5112 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5113 return NL_SKIP;
5114}
5115
5116
5117static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5118 int idx, u8 *seq)
5119{
a2e40bb6
FF
5120 struct i802_bss *bss = priv;
5121 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
5122 struct nl_msg *msg;
5123
95376e1a
JM
5124 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
5125 NL80211_CMD_GET_KEY);
5126 if (!msg ||
a862e4a3 5127 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
95376e1a 5128 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
a862e4a3
JM
5129 nlmsg_free(msg);
5130 return -ENOBUFS;
5131 }
c5121837
JM
5132
5133 memset(seq, 0, 6);
5134
5135 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
c5121837
JM
5136}
5137
5138
c5121837
JM
5139static int i802_set_rts(void *priv, int rts)
5140{
a2e40bb6
FF
5141 struct i802_bss *bss = priv;
5142 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451 5143 struct nl_msg *msg;
a862e4a3 5144 int ret;
ad649451 5145 u32 val;
c5121837 5146
ad649451
JM
5147 if (rts >= 2347)
5148 val = (u32) -1;
5149 else
5150 val = rts;
c5121837 5151
9725b784 5152 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
a862e4a3
JM
5153 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
5154 nlmsg_free(msg);
5155 return -ENOBUFS;
5156 }
ad649451
JM
5157
5158 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5159 if (!ret)
5160 return 0;
ad649451
JM
5161 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5162 "%d (%s)", rts, ret, strerror(-ret));
5163 return ret;
c5121837
JM
5164}
5165
5166
5167static int i802_set_frag(void *priv, int frag)
5168{
a2e40bb6
FF
5169 struct i802_bss *bss = priv;
5170 struct wpa_driver_nl80211_data *drv = bss->drv;
ad649451 5171 struct nl_msg *msg;
a862e4a3 5172 int ret;
ad649451 5173 u32 val;
c5121837 5174
ad649451
JM
5175 if (frag >= 2346)
5176 val = (u32) -1;
5177 else
5178 val = frag;
c5121837 5179
9725b784 5180 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
a862e4a3
JM
5181 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
5182 nlmsg_free(msg);
5183 return -ENOBUFS;
5184 }
ad649451
JM
5185
5186 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5187 if (!ret)
5188 return 0;
ad649451
JM
5189 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
5190 "%d: %d (%s)", frag, ret, strerror(-ret));
5191 return ret;
c5121837
JM
5192}
5193
5194
c5121837
JM
5195static int i802_flush(void *priv)
5196{
a2e40bb6 5197 struct i802_bss *bss = priv;
c5121837 5198 struct nl_msg *msg;
82554b10 5199 int res;
c5121837 5200
83e7bb0e
JM
5201 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
5202 bss->ifname);
c5121837
JM
5203
5204 /*
5205 * XXX: FIX! this needs to flush all VLANs too
5206 */
13f83980
JM
5207 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
5208 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
82554b10
JM
5209 if (res) {
5210 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
5211 "(%s)", res, strerror(-res));
5212 }
5213 return res;
c5121837
JM
5214}
5215
5216
5217static int get_sta_handler(struct nl_msg *msg, void *arg)
5218{
5219 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5220 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5221 struct hostap_sta_driver_data *data = arg;
5222 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
5223 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
5224 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
5225 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
5226 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
5227 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
5228 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
dc7785f8 5229 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
c5121837
JM
5230 };
5231
5232 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5233 genlmsg_attrlen(gnlh, 0), NULL);
5234
5235 /*
5236 * TODO: validate the interface and mac address!
5237 * Otherwise, there's a race condition as soon as
5238 * the kernel starts sending station notifications.
5239 */
5240
5241 if (!tb[NL80211_ATTR_STA_INFO]) {
5242 wpa_printf(MSG_DEBUG, "sta stats missing!");
5243 return NL_SKIP;
5244 }
5245 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
5246 tb[NL80211_ATTR_STA_INFO],
5247 stats_policy)) {
5248 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
5249 return NL_SKIP;
5250 }
5251
5252 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
5253 data->inactive_msec =
5254 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
5255 if (stats[NL80211_STA_INFO_RX_BYTES])
5256 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
5257 if (stats[NL80211_STA_INFO_TX_BYTES])
5258 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
5259 if (stats[NL80211_STA_INFO_RX_PACKETS])
5260 data->rx_packets =
5261 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
5262 if (stats[NL80211_STA_INFO_TX_PACKETS])
5263 data->tx_packets =
5264 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
dc7785f8
YZ
5265 if (stats[NL80211_STA_INFO_TX_FAILED])
5266 data->tx_retry_failed =
5267 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
c5121837
JM
5268
5269 return NL_SKIP;
5270}
5271
9ebce9c5
JM
5272static int i802_read_sta_data(struct i802_bss *bss,
5273 struct hostap_sta_driver_data *data,
c5121837
JM
5274 const u8 *addr)
5275{
c5121837
JM
5276 struct nl_msg *msg;
5277
5278 os_memset(data, 0, sizeof(*data));
c5121837 5279
13f83980
JM
5280 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
5281 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
a862e4a3
JM
5282 nlmsg_free(msg);
5283 return -ENOBUFS;
5284 }
c5121837 5285
13f83980 5286 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
c5121837
JM
5287}
5288
5289
c5121837
JM
5290static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
5291 int cw_min, int cw_max, int burst_time)
5292{
a2e40bb6
FF
5293 struct i802_bss *bss = priv;
5294 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
5295 struct nl_msg *msg;
5296 struct nlattr *txq, *params;
5297
13f83980 5298 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
c5121837
JM
5299 if (!msg)
5300 return -1;
5301
c5121837
JM
5302 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
5303 if (!txq)
a862e4a3 5304 goto fail;
c5121837
JM
5305
5306 /* We are only sending parameters for a single TXQ at a time */
5307 params = nla_nest_start(msg, 1);
5308 if (!params)
a862e4a3 5309 goto fail;
c5121837 5310
7e3c1781
JM
5311 switch (queue) {
5312 case 0:
a862e4a3
JM
5313 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
5314 goto fail;
7e3c1781
JM
5315 break;
5316 case 1:
a862e4a3
JM
5317 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
5318 goto fail;
7e3c1781
JM
5319 break;
5320 case 2:
a862e4a3
JM
5321 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
5322 goto fail;
7e3c1781
JM
5323 break;
5324 case 3:
a862e4a3
JM
5325 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
5326 goto fail;
7e3c1781
JM
5327 break;
5328 }
c5121837
JM
5329 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
5330 * 32 usec, so need to convert the value here. */
a862e4a3
JM
5331 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
5332 (burst_time * 100 + 16) / 32) ||
5333 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
5334 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
5335 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
5336 goto fail;
c5121837
JM
5337
5338 nla_nest_end(msg, params);
5339
5340 nla_nest_end(msg, txq);
5341
5342 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5343 return 0;
9e088e74 5344 msg = NULL;
a862e4a3 5345fail:
9e088e74 5346 nlmsg_free(msg);
c5121837
JM
5347 return -1;
5348}
5349
5350
9ebce9c5 5351static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
c5121837
JM
5352 const char *ifname, int vlan_id)
5353{
a2e40bb6 5354 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837 5355 struct nl_msg *msg;
a862e4a3 5356 int ret;
c5121837 5357
bbc706a3
JM
5358 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
5359 ", ifname=%s[%d], vlan_id=%d)",
5360 bss->ifname, if_nametoindex(bss->ifname),
5361 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
13f83980 5362 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
a862e4a3
JM
5363 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5364 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
5365 nlmsg_free(msg);
5366 return -ENOBUFS;
5367 }
c5121837 5368
cd1d72c1
JM
5369 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5370 if (ret < 0) {
5371 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
5372 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
5373 MAC2STR(addr), ifname, vlan_id, ret,
5374 strerror(-ret));
5375 }
cd1d72c1 5376 return ret;
c5121837
JM
5377}
5378
fbbfcbac 5379
c5121837
JM
5380static int i802_get_inact_sec(void *priv, const u8 *addr)
5381{
5382 struct hostap_sta_driver_data data;
5383 int ret;
5384
5385 data.inactive_msec = (unsigned long) -1;
5386 ret = i802_read_sta_data(priv, &data, addr);
b9749bac
MH
5387 if (ret == -ENOENT)
5388 return -ENOENT;
c5121837
JM
5389 if (ret || data.inactive_msec == (unsigned long) -1)
5390 return -1;
5391 return data.inactive_msec / 1000;
5392}
5393
5394
5395static int i802_sta_clear_stats(void *priv, const u8 *addr)
5396{
5397#if 0
5398 /* TODO */
5399#endif
5400 return 0;
5401}
5402
5403
731723a5
JM
5404static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
5405 int reason)
c5121837 5406{
a2e40bb6 5407 struct i802_bss *bss = priv;
e1bd4e19 5408 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
5409 struct ieee80211_mgmt mgmt;
5410
0d391b03
BC
5411 if (is_mesh_interface(drv->nlmode))
5412 return -1;
5413
e1bd4e19 5414 if (drv->device_ap_sme)
59d7148a 5415 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
e1bd4e19 5416
c5121837
JM
5417 memset(&mgmt, 0, sizeof(mgmt));
5418 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5419 WLAN_FC_STYPE_DEAUTH);
5420 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
5421 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5422 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 5423 mgmt.u.deauth.reason_code = host_to_le16(reason);
a2e40bb6 5424 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 5425 IEEE80211_HDRLEN +
9ebce9c5
JM
5426 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
5427 0);
c5121837
JM
5428}
5429
5430
731723a5
JM
5431static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
5432 int reason)
c5121837 5433{
a2e40bb6 5434 struct i802_bss *bss = priv;
e1bd4e19 5435 struct wpa_driver_nl80211_data *drv = bss->drv;
c5121837
JM
5436 struct ieee80211_mgmt mgmt;
5437
0d391b03
BC
5438 if (is_mesh_interface(drv->nlmode))
5439 return -1;
5440
e1bd4e19 5441 if (drv->device_ap_sme)
59d7148a 5442 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
e1bd4e19 5443
c5121837
JM
5444 memset(&mgmt, 0, sizeof(mgmt));
5445 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
5446 WLAN_FC_STYPE_DISASSOC);
5447 memcpy(mgmt.da, addr, ETH_ALEN);
731723a5
JM
5448 memcpy(mgmt.sa, own_addr, ETH_ALEN);
5449 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
c5121837 5450 mgmt.u.disassoc.reason_code = host_to_le16(reason);
a2e40bb6 5451 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
9f324b61 5452 IEEE80211_HDRLEN +
9ebce9c5
JM
5453 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
5454 0);
c5121837
JM
5455}
5456
5457
9b4d9c8b
JM
5458static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
5459{
5460 char buf[200], *pos, *end;
5461 int i, res;
5462
5463 pos = buf;
5464 end = pos + sizeof(buf);
5465
5466 for (i = 0; i < drv->num_if_indices; i++) {
5467 if (!drv->if_indices[i])
5468 continue;
5469 res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
d85e1fc8 5470 if (os_snprintf_error(end - pos, res))
9b4d9c8b
JM
5471 break;
5472 pos += res;
5473 }
5474 *pos = '\0';
5475
5476 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
5477 drv->num_if_indices, buf);
5478}
5479
5480
f07ead6a
JM
5481static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5482{
5483 int i;
5484 int *old;
5485
5486 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
5487 ifidx);
b36935be
MB
5488 if (have_ifidx(drv, ifidx)) {
5489 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
5490 ifidx);
5491 return;
5492 }
f07ead6a
JM
5493 for (i = 0; i < drv->num_if_indices; i++) {
5494 if (drv->if_indices[i] == 0) {
5495 drv->if_indices[i] = ifidx;
9b4d9c8b 5496 dump_ifidx(drv);
f07ead6a
JM
5497 return;
5498 }
5499 }
5500
5501 if (drv->if_indices != drv->default_if_indices)
5502 old = drv->if_indices;
5503 else
5504 old = NULL;
5505
067ffa26
JM
5506 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
5507 sizeof(int));
f07ead6a
JM
5508 if (!drv->if_indices) {
5509 if (!old)
5510 drv->if_indices = drv->default_if_indices;
5511 else
5512 drv->if_indices = old;
5513 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
5514 "interfaces");
5515 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
5516 return;
5517 } else if (!old)
5518 os_memcpy(drv->if_indices, drv->default_if_indices,
5519 sizeof(drv->default_if_indices));
5520 drv->if_indices[drv->num_if_indices] = ifidx;
5521 drv->num_if_indices++;
9b4d9c8b 5522 dump_ifidx(drv);
f07ead6a
JM
5523}
5524
5525
5526static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5527{
5528 int i;
5529
5530 for (i = 0; i < drv->num_if_indices; i++) {
5531 if (drv->if_indices[i] == ifidx) {
5532 drv->if_indices[i] = 0;
5533 break;
5534 }
5535 }
9b4d9c8b 5536 dump_ifidx(drv);
f07ead6a
JM
5537}
5538
5539
5540static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
5541{
5542 int i;
5543
5544 for (i = 0; i < drv->num_if_indices; i++)
5545 if (drv->if_indices[i] == ifidx)
5546 return 1;
5547
5548 return 0;
5549}
5550
5551
5552static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
0e80ea2c 5553 const char *bridge_ifname, char *ifname_wds)
f07ead6a
JM
5554{
5555 struct i802_bss *bss = priv;
5556 struct wpa_driver_nl80211_data *drv = bss->drv;
5557 char name[IFNAMSIZ + 1];
5558
5559 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
69dd2967
SM
5560 if (ifname_wds)
5561 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
5562
f07ead6a
JM
5563 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
5564 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
5565 if (val) {
5566 if (!if_nametoindex(name)) {
5567 if (nl80211_create_iface(drv, name,
5568 NL80211_IFTYPE_AP_VLAN,
2aec4f3c
JM
5569 bss->addr, 1, NULL, NULL, 0) <
5570 0)
f07ead6a
JM
5571 return -1;
5572 if (bridge_ifname &&
c81eff1a
BG
5573 linux_br_add_if(drv->global->ioctl_sock,
5574 bridge_ifname, name) < 0)
f07ead6a
JM
5575 return -1;
5576 }
75227f3a
JM
5577 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
5578 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
5579 "interface %s up", name);
5580 }
f07ead6a
JM
5581 return i802_set_sta_vlan(priv, addr, name, 0);
5582 } else {
c34e618d
FF
5583 if (bridge_ifname)
5584 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
5585 name);
5586
f07ead6a 5587 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
d0595b25
FF
5588 nl80211_remove_iface(drv, if_nametoindex(name));
5589 return 0;
f07ead6a
JM
5590 }
5591}
5592
5593
5594static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
5595{
5596 struct wpa_driver_nl80211_data *drv = eloop_ctx;
5597 struct sockaddr_ll lladdr;
5598 unsigned char buf[3000];
5599 int len;
5600 socklen_t fromlen = sizeof(lladdr);
5601
5602 len = recvfrom(sock, buf, sizeof(buf), 0,
5603 (struct sockaddr *)&lladdr, &fromlen);
5604 if (len < 0) {
7ac3616d
JM
5605 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
5606 strerror(errno));
f07ead6a
JM
5607 return;
5608 }
5609
5610 if (have_ifidx(drv, lladdr.sll_ifindex))
5611 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
5612}
5613
5614
94627f6c 5615static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
e17a2477 5616 struct i802_bss *bss,
94627f6c
JM
5617 const char *brname, const char *ifname)
5618{
6c6678e7 5619 int br_ifindex;
94627f6c
JM
5620 char in_br[IFNAMSIZ];
5621
e17a2477 5622 os_strlcpy(bss->brname, brname, IFNAMSIZ);
6c6678e7 5623 br_ifindex = if_nametoindex(brname);
6c6678e7 5624 if (br_ifindex == 0) {
94627f6c
JM
5625 /*
5626 * Bridge was configured, but the bridge device does
5627 * not exist. Try to add it now.
5628 */
c81eff1a 5629 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
94627f6c
JM
5630 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
5631 "bridge interface %s: %s",
5632 brname, strerror(errno));
5633 return -1;
5634 }
e17a2477 5635 bss->added_bridge = 1;
8997613c
JM
5636 br_ifindex = if_nametoindex(brname);
5637 add_ifidx(drv, br_ifindex);
94627f6c 5638 }
8997613c 5639 bss->br_ifindex = br_ifindex;
94627f6c
JM
5640
5641 if (linux_br_get(in_br, ifname) == 0) {
5642 if (os_strcmp(in_br, brname) == 0)
5643 return 0; /* already in the bridge */
5644
5645 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
5646 "bridge %s", ifname, in_br);
c81eff1a
BG
5647 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
5648 0) {
94627f6c
JM
5649 wpa_printf(MSG_ERROR, "nl80211: Failed to "
5650 "remove interface %s from bridge "
5651 "%s: %s",
5652 ifname, brname, strerror(errno));
5653 return -1;
5654 }
5655 }
5656
5657 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
5658 ifname, brname);
c81eff1a 5659 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
94627f6c
JM
5660 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
5661 "into bridge %s: %s",
5662 ifname, brname, strerror(errno));
5663 return -1;
5664 }
e17a2477 5665 bss->added_if_into_bridge = 1;
94627f6c
JM
5666
5667 return 0;
5668}
5669
5670
92f475b4
JM
5671static void *i802_init(struct hostapd_data *hapd,
5672 struct wpa_init_params *params)
c5121837
JM
5673{
5674 struct wpa_driver_nl80211_data *drv;
a2e40bb6 5675 struct i802_bss *bss;
c5121837 5676 size_t i;
94627f6c
JM
5677 char brname[IFNAMSIZ];
5678 int ifindex, br_ifindex;
5679 int br_added = 0;
c5121837 5680
0d547d5f
JM
5681 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
5682 params->global_priv, 1,
0ecff8d7 5683 params->bssid, params->driver_params);
a2e40bb6 5684 if (bss == NULL)
c5121837 5685 return NULL;
c5121837 5686
a2e40bb6 5687 drv = bss->drv;
7635bfb0 5688
94627f6c
JM
5689 if (linux_br_get(brname, params->ifname) == 0) {
5690 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
5691 params->ifname, brname);
5692 br_ifindex = if_nametoindex(brname);
392dfd37 5693 os_strlcpy(bss->brname, brname, IFNAMSIZ);
94627f6c
JM
5694 } else {
5695 brname[0] = '\0';
5696 br_ifindex = 0;
5697 }
6c6678e7 5698 bss->br_ifindex = br_ifindex;
94627f6c 5699
92f475b4 5700 for (i = 0; i < params->num_bridge; i++) {
94627f6c
JM
5701 if (params->bridge[i]) {
5702 ifindex = if_nametoindex(params->bridge[i]);
5703 if (ifindex)
5704 add_ifidx(drv, ifindex);
5705 if (ifindex == br_ifindex)
5706 br_added = 1;
5707 }
c5121837 5708 }
c5121837 5709
ad1e68e6
JM
5710 /* start listening for EAPOL on the default AP interface */
5711 add_ifidx(drv, drv->ifindex);
5712
5ef8e39f
JM
5713 if (params->num_bridge && params->bridge[0]) {
5714 if (i802_check_bridge(drv, bss, params->bridge[0],
5715 params->ifname) < 0)
5716 goto failed;
5717 if (os_strcmp(params->bridge[0], brname) != 0)
5718 br_added = 1;
5719 }
5720
5721 if (!br_added && br_ifindex &&
5722 (params->num_bridge == 0 || !params->bridge[0]))
5723 add_ifidx(drv, br_ifindex);
94627f6c 5724
97ed9a06
KP
5725#ifdef CONFIG_LIBNL3_ROUTE
5726 if (bss->added_if_into_bridge) {
5727 drv->rtnl_sk = nl_socket_alloc();
5728 if (drv->rtnl_sk == NULL) {
5729 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
5730 goto failed;
5731 }
5732
5733 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
5734 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
5735 strerror(errno));
5736 goto failed;
5737 }
5738 }
5739#endif /* CONFIG_LIBNL3_ROUTE */
5740
ad1e68e6
JM
5741 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
5742 if (drv->eapol_sock < 0) {
7ac3616d
JM
5743 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
5744 strerror(errno));
bbaf0837 5745 goto failed;
ad1e68e6
JM
5746 }
5747
5748 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
5749 {
7ac3616d 5750 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
c5121837 5751 goto failed;
ad1e68e6
JM
5752 }
5753
c81eff1a
BG
5754 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
5755 params->own_addr))
bbaf0837 5756 goto failed;
fee354c7 5757 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
c5121837 5758
341eebee
JB
5759 memcpy(bss->addr, params->own_addr, ETH_ALEN);
5760
a2e40bb6 5761 return bss;
c5121837
JM
5762
5763failed:
7635bfb0 5764 wpa_driver_nl80211_deinit(bss);
bbaf0837
JM
5765 return NULL;
5766}
c5121837 5767
c5121837 5768
bbaf0837
JM
5769static void i802_deinit(void *priv)
5770{
9ebce9c5
JM
5771 struct i802_bss *bss = priv;
5772 wpa_driver_nl80211_deinit(bss);
c5121837
JM
5773}
5774
c5121837 5775
22a7c9d7
JM
5776static enum nl80211_iftype wpa_driver_nl80211_if_type(
5777 enum wpa_driver_if_type type)
5778{
5779 switch (type) {
5780 case WPA_IF_STATION:
9f51b113 5781 return NL80211_IFTYPE_STATION;
75bde05d
JM
5782 case WPA_IF_P2P_CLIENT:
5783 case WPA_IF_P2P_GROUP:
9f51b113 5784 return NL80211_IFTYPE_P2P_CLIENT;
22a7c9d7
JM
5785 case WPA_IF_AP_VLAN:
5786 return NL80211_IFTYPE_AP_VLAN;
5787 case WPA_IF_AP_BSS:
5788 return NL80211_IFTYPE_AP;
9f51b113
JB
5789 case WPA_IF_P2P_GO:
5790 return NL80211_IFTYPE_P2P_GO;
7aad838c
NS
5791 case WPA_IF_P2P_DEVICE:
5792 return NL80211_IFTYPE_P2P_DEVICE;
5b78493f
MH
5793 case WPA_IF_MESH:
5794 return NL80211_IFTYPE_MESH_POINT;
22a7c9d7
JM
5795 }
5796 return -1;
5797}
5798
5799
f2ed8023
JM
5800static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
5801{
5802 struct wpa_driver_nl80211_data *drv;
5803 dl_list_for_each(drv, &global->interfaces,
5804 struct wpa_driver_nl80211_data, list) {
834ee56f 5805 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
f2ed8023
JM
5806 return 1;
5807 }
5808 return 0;
5809}
5810
5811
5b78493f 5812static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
f2ed8023
JM
5813{
5814 unsigned int idx;
5815
5816 if (!drv->global)
5817 return -1;
5818
834ee56f 5819 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
f2ed8023 5820 for (idx = 0; idx < 64; idx++) {
834ee56f 5821 new_addr[0] = drv->first_bss->addr[0] | 0x02;
f2ed8023
JM
5822 new_addr[0] ^= idx << 2;
5823 if (!nl80211_addr_in_use(drv->global, new_addr))
5824 break;
5825 }
5826 if (idx == 64)
5827 return -1;
5828
5b78493f 5829 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
f2ed8023
JM
5830 MACSTR, MAC2STR(new_addr));
5831
5832 return 0;
5833}
5834
5835
f632e483
AS
5836struct wdev_info {
5837 u64 wdev_id;
5838 int wdev_id_set;
5839 u8 macaddr[ETH_ALEN];
5840};
5841
5842static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
e472e1b4
AS
5843{
5844 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5845 struct nlattr *tb[NL80211_ATTR_MAX + 1];
f632e483 5846 struct wdev_info *wi = arg;
e472e1b4
AS
5847
5848 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5849 genlmsg_attrlen(gnlh, 0), NULL);
5850 if (tb[NL80211_ATTR_WDEV]) {
f632e483
AS
5851 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
5852 wi->wdev_id_set = 1;
e472e1b4
AS
5853 }
5854
5855 if (tb[NL80211_ATTR_MAC])
f632e483 5856 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
e472e1b4
AS
5857 ETH_ALEN);
5858
5859 return NL_SKIP;
5860}
5861
5862
7ab68865 5863static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
8043e725 5864 const char *ifname, const u8 *addr,
f3585c8a 5865 void *bss_ctx, void **drv_priv,
e17a2477 5866 char *force_ifname, u8 *if_addr,
2aec4f3c 5867 const char *bridge, int use_existing)
22a7c9d7 5868{
e472e1b4 5869 enum nl80211_iftype nlmode;
a2e40bb6
FF
5870 struct i802_bss *bss = priv;
5871 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7 5872 int ifidx;
2aec4f3c 5873 int added = 1;
22a7c9d7 5874
f3585c8a
JM
5875 if (addr)
5876 os_memcpy(if_addr, addr, ETH_ALEN);
e472e1b4
AS
5877 nlmode = wpa_driver_nl80211_if_type(type);
5878 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
f632e483
AS
5879 struct wdev_info p2pdev_info;
5880
5881 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
e472e1b4 5882 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
f632e483 5883 0, nl80211_wdev_handler,
2aec4f3c 5884 &p2pdev_info, use_existing);
f632e483 5885 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
e472e1b4
AS
5886 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
5887 ifname);
e472e1b4
AS
5888 return -1;
5889 }
f632e483
AS
5890
5891 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
5892 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
5893 if (!is_zero_ether_addr(p2pdev_info.macaddr))
5894 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
5895 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
5896 ifname,
5897 (long long unsigned int) p2pdev_info.wdev_id);
e472e1b4
AS
5898 } else {
5899 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
2aec4f3c
JM
5900 0, NULL, NULL, use_existing);
5901 if (use_existing && ifidx == -ENFILE) {
5902 added = 0;
5903 ifidx = if_nametoindex(ifname);
5904 } else if (ifidx < 0) {
e472e1b4
AS
5905 return -1;
5906 }
22a7c9d7
JM
5907 }
5908
ab7a1add 5909 if (!addr) {
8ea8a89c 5910 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
ab7a1add
AS
5911 os_memcpy(if_addr, bss->addr, ETH_ALEN);
5912 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
8ea8a89c 5913 ifname, if_addr) < 0) {
2aec4f3c
JM
5914 if (added)
5915 nl80211_remove_iface(drv, ifidx);
ab7a1add
AS
5916 return -1;
5917 }
c55f774d
JM
5918 }
5919
c55f774d
JM
5920 if (!addr &&
5921 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
8ea8a89c
JM
5922 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
5923 type == WPA_IF_STATION)) {
5924 /* Enforce unique address */
ab7a1add 5925 u8 new_addr[ETH_ALEN];
c55f774d 5926
ab7a1add 5927 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
c81eff1a 5928 new_addr) < 0) {
ea39367c
MK
5929 if (added)
5930 nl80211_remove_iface(drv, ifidx);
c55f774d
JM
5931 return -1;
5932 }
f608081c 5933 if (nl80211_addr_in_use(drv->global, new_addr)) {
c55f774d 5934 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
8ea8a89c 5935 "for interface %s type %d", ifname, type);
5b78493f 5936 if (nl80211_vif_addr(drv, new_addr) < 0) {
ea39367c
MK
5937 if (added)
5938 nl80211_remove_iface(drv, ifidx);
c55f774d
JM
5939 return -1;
5940 }
c81eff1a 5941 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
c55f774d 5942 new_addr) < 0) {
ea39367c
MK
5943 if (added)
5944 nl80211_remove_iface(drv, ifidx);
c55f774d
JM
5945 return -1;
5946 }
c55f774d 5947 }
f67eeb5c 5948 os_memcpy(if_addr, new_addr, ETH_ALEN);
c55f774d 5949 }
f3585c8a 5950
22a7c9d7 5951 if (type == WPA_IF_AP_BSS) {
f5eb9da3
JM
5952 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
5953 if (new_bss == NULL) {
2aec4f3c
JM
5954 if (added)
5955 nl80211_remove_iface(drv, ifidx);
f5eb9da3
JM
5956 return -1;
5957 }
5958
5959 if (bridge &&
5960 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
5961 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
5962 "interface %s to a bridge %s",
5963 ifname, bridge);
2aec4f3c
JM
5964 if (added)
5965 nl80211_remove_iface(drv, ifidx);
f5eb9da3
JM
5966 os_free(new_bss);
5967 return -1;
5968 }
5969
c81eff1a
BG
5970 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
5971 {
ea39367c
MK
5972 if (added)
5973 nl80211_remove_iface(drv, ifidx);
07179987 5974 os_free(new_bss);
22a7c9d7
JM
5975 return -1;
5976 }
a2e40bb6 5977 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
341eebee 5978 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
a2e40bb6
FF
5979 new_bss->ifindex = ifidx;
5980 new_bss->drv = drv;
834ee56f
KP
5981 new_bss->next = drv->first_bss->next;
5982 new_bss->freq = drv->first_bss->freq;
a5e1eb20 5983 new_bss->ctx = bss_ctx;
2aec4f3c 5984 new_bss->added_if = added;
834ee56f 5985 drv->first_bss->next = new_bss;
a2e40bb6
FF
5986 if (drv_priv)
5987 *drv_priv = new_bss;
cc7a48d1 5988 nl80211_init_bss(new_bss);
3dd1d890
YAP
5989
5990 /* Subscribe management frames for this WPA_IF_AP_BSS */
5991 if (nl80211_setup_ap(new_bss))
5992 return -1;
22a7c9d7 5993 }
22a7c9d7 5994
ff6a158b
JM
5995 if (drv->global)
5996 drv->global->if_add_ifindex = ifidx;
5997
d1bb7aed
JJ
5998 /*
5999 * Some virtual interfaces need to process EAPOL packets and events on
6000 * the parent interface. This is used mainly with hostapd.
6001 */
6002 if (ifidx > 0 &&
6003 (drv->hostapd ||
6004 nlmode == NL80211_IFTYPE_AP_VLAN ||
6005 nlmode == NL80211_IFTYPE_WDS ||
6006 nlmode == NL80211_IFTYPE_MONITOR))
b36935be
MB
6007 add_ifidx(drv, ifidx);
6008
22a7c9d7
JM
6009 return 0;
6010}
6011
6012
9ebce9c5 6013static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
22a7c9d7
JM
6014 enum wpa_driver_if_type type,
6015 const char *ifname)
6016{
a2e40bb6 6017 struct wpa_driver_nl80211_data *drv = bss->drv;
22a7c9d7
JM
6018 int ifindex = if_nametoindex(ifname);
6019
2aec4f3c
JM
6020 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
6021 __func__, type, ifname, ifindex, bss->added_if);
158b090c 6022 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
2b72df63 6023 nl80211_remove_iface(drv, ifindex);
de884303
JM
6024 else if (ifindex > 0 && !bss->added_if) {
6025 struct wpa_driver_nl80211_data *drv2;
6026 dl_list_for_each(drv2, &drv->global->interfaces,
6027 struct wpa_driver_nl80211_data, list)
6028 del_ifidx(drv2, ifindex);
6029 }
c34e618d 6030
c34e618d
FF
6031 if (type != WPA_IF_AP_BSS)
6032 return 0;
6033
e17a2477 6034 if (bss->added_if_into_bridge) {
c81eff1a
BG
6035 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
6036 bss->ifname) < 0)
e17a2477
JM
6037 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6038 "interface %s from bridge %s: %s",
6039 bss->ifname, bss->brname, strerror(errno));
6040 }
6041 if (bss->added_bridge) {
c81eff1a 6042 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
e17a2477
JM
6043 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6044 "bridge %s: %s",
6045 bss->brname, strerror(errno));
6046 }
a2e40bb6 6047
834ee56f 6048 if (bss != drv->first_bss) {
8546ea19 6049 struct i802_bss *tbss;
a2e40bb6 6050
2aec4f3c 6051 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
834ee56f 6052 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
8546ea19
JM
6053 if (tbss->next == bss) {
6054 tbss->next = bss->next;
3dd1d890
YAP
6055 /* Unsubscribe management frames */
6056 nl80211_teardown_ap(bss);
cc7a48d1 6057 nl80211_destroy_bss(bss);
5c9da160
MB
6058 if (!bss->added_if)
6059 i802_set_iface_flags(bss, 0);
8546ea19
JM
6060 os_free(bss);
6061 bss = NULL;
6062 break;
6063 }
22a7c9d7 6064 }
8546ea19
JM
6065 if (bss)
6066 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
6067 "BSS %p in the list", __func__, bss);
390e489c 6068 } else {
2aec4f3c 6069 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
390e489c 6070 nl80211_teardown_ap(bss);
2aec4f3c
JM
6071 if (!bss->added_if && !drv->first_bss->next)
6072 wpa_driver_nl80211_del_beacon(drv);
390e489c 6073 nl80211_destroy_bss(bss);
2aec4f3c
JM
6074 if (!bss->added_if)
6075 i802_set_iface_flags(bss, 0);
390e489c
KP
6076 if (drv->first_bss->next) {
6077 drv->first_bss = drv->first_bss->next;
6078 drv->ctx = drv->first_bss->ctx;
6079 os_free(bss);
6080 } else {
6081 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
6082 }
22a7c9d7 6083 }
22a7c9d7
JM
6084
6085 return 0;
6086}
6087
6088
55777702
JM
6089static int cookie_handler(struct nl_msg *msg, void *arg)
6090{
6091 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6092 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6093 u64 *cookie = arg;
6094 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6095 genlmsg_attrlen(gnlh, 0), NULL);
6096 if (tb[NL80211_ATTR_COOKIE])
6097 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
6098 return NL_SKIP;
6099}
6100
6101
88df0ef7 6102static int nl80211_send_frame_cmd(struct i802_bss *bss,
5dfca53f
JB
6103 unsigned int freq, unsigned int wait,
6104 const u8 *buf, size_t buf_len,
88df0ef7
JB
6105 u64 *cookie_out, int no_cck, int no_ack,
6106 int offchanok)
9884f9cc 6107{
88df0ef7 6108 struct wpa_driver_nl80211_data *drv = bss->drv;
9884f9cc
JB
6109 struct nl_msg *msg;
6110 u64 cookie;
6111 int ret = -1;
6112
cc2ada86 6113 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
2e3e4566
JM
6114 "no_ack=%d offchanok=%d",
6115 freq, wait, no_cck, no_ack, offchanok);
c91f796f 6116 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
9884f9cc 6117
56f77852 6118 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
a862e4a3
JM
6119 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
6120 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
6121 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6122 drv->test_use_roc_tx) &&
6123 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
6124 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
6125 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
6126 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
6127 goto fail;
9884f9cc
JB
6128
6129 cookie = 0;
6130 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6131 msg = NULL;
6132 if (ret) {
6133 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
a05225c8
JM
6134 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
6135 freq, wait);
a862e4a3
JM
6136 } else {
6137 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
6138 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
6139 (long long unsigned int) cookie);
9884f9cc 6140
a862e4a3
JM
6141 if (cookie_out)
6142 *cookie_out = no_ack ? (u64) -1 : cookie;
6143 }
9884f9cc 6144
a862e4a3 6145fail:
9884f9cc
JB
6146 nlmsg_free(msg);
6147 return ret;
6148}
6149
6150
9ebce9c5
JM
6151static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
6152 unsigned int freq,
190b9062 6153 unsigned int wait_time,
58f6fbe0
JM
6154 const u8 *dst, const u8 *src,
6155 const u8 *bssid,
b106173a
JM
6156 const u8 *data, size_t data_len,
6157 int no_cck)
58f6fbe0 6158{
a2e40bb6 6159 struct wpa_driver_nl80211_data *drv = bss->drv;
58f6fbe0 6160 int ret = -1;
58f6fbe0
JM
6161 u8 *buf;
6162 struct ieee80211_hdr *hdr;
58f6fbe0 6163
5dfca53f 6164 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
55231068
JM
6165 "freq=%u MHz wait=%d ms no_cck=%d)",
6166 drv->ifindex, freq, wait_time, no_cck);
58f6fbe0
JM
6167
6168 buf = os_zalloc(24 + data_len);
6169 if (buf == NULL)
6170 return ret;
6171 os_memcpy(buf + 24, data, data_len);
6172 hdr = (struct ieee80211_hdr *) buf;
6173 hdr->frame_control =
6174 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
6175 os_memcpy(hdr->addr1, dst, ETH_ALEN);
6176 os_memcpy(hdr->addr2, src, ETH_ALEN);
6177 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
6178
f78f2785
JM
6179 if (is_ap_interface(drv->nlmode) &&
6180 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6181 (int) freq == bss->freq || drv->device_ap_sme ||
6182 !drv->use_monitor))
9ebce9c5
JM
6183 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
6184 0, freq, no_cck, 1,
6185 wait_time);
9884f9cc 6186 else
88df0ef7 6187 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
5dfca53f 6188 24 + data_len,
b106173a 6189 &drv->send_action_cookie,
88df0ef7 6190 no_cck, 0, 1);
58f6fbe0 6191
f8bf1421 6192 os_free(buf);
58f6fbe0
JM
6193 return ret;
6194}
6195
6196
5dfca53f
JB
6197static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
6198{
6199 struct i802_bss *bss = priv;
6200 struct wpa_driver_nl80211_data *drv = bss->drv;
6201 struct nl_msg *msg;
6202 int ret;
6203
316a9e4d
JM
6204 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
6205 (long long unsigned int) drv->send_action_cookie);
56f77852 6206 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
a862e4a3
JM
6207 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie)) {
6208 nlmsg_free(msg);
6209 return;
6210 }
5dfca53f
JB
6211
6212 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5dfca53f
JB
6213 if (ret)
6214 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
6215 "(%s)", ret, strerror(-ret));
5dfca53f
JB
6216}
6217
6218
55777702
JM
6219static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
6220 unsigned int duration)
6221{
a2e40bb6
FF
6222 struct i802_bss *bss = priv;
6223 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
6224 struct nl_msg *msg;
6225 int ret;
6226 u64 cookie;
6227
56f77852 6228 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
a862e4a3
JM
6229 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
6230 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
6231 nlmsg_free(msg);
6232 return -1;
6233 }
55777702
JM
6234
6235 cookie = 0;
6236 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6237 if (ret == 0) {
6238 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
6239 "0x%llx for freq=%u MHz duration=%u",
6240 (long long unsigned int) cookie, freq, duration);
6241 drv->remain_on_chan_cookie = cookie;
531f0331 6242 drv->pending_remain_on_chan = 1;
55777702
JM
6243 return 0;
6244 }
6245 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
15ed5535
JM
6246 "(freq=%d duration=%u): %d (%s)",
6247 freq, duration, ret, strerror(-ret));
55777702
JM
6248 return -1;
6249}
6250
6251
6252static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
6253{
a2e40bb6
FF
6254 struct i802_bss *bss = priv;
6255 struct wpa_driver_nl80211_data *drv = bss->drv;
55777702
JM
6256 struct nl_msg *msg;
6257 int ret;
6258
6259 if (!drv->pending_remain_on_chan) {
6260 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
6261 "to cancel");
6262 return -1;
6263 }
6264
6265 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
6266 "0x%llx",
6267 (long long unsigned int) drv->remain_on_chan_cookie);
6268
56f77852
JM
6269 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
6270 if (!msg ||
a862e4a3
JM
6271 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
6272 nlmsg_free(msg);
6273 return -1;
6274 }
55777702
JM
6275
6276 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6277 if (ret == 0)
6278 return 0;
6279 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
6280 "%d (%s)", ret, strerror(-ret));
55777702
JM
6281 return -1;
6282}
6283
6284
9ebce9c5 6285static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
504e905c 6286{
a2e40bb6 6287 struct wpa_driver_nl80211_data *drv = bss->drv;
504e905c 6288
5582a5d1 6289 if (!report) {
0d891981 6290 if (bss->nl_preq && drv->device_ap_sme &&
b8d87ed2
AP
6291 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
6292 !bss->static_ap) {
0d891981
JM
6293 /*
6294 * Do not disable Probe Request reporting that was
6295 * enabled in nl80211_setup_ap().
6296 */
6297 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
6298 "Probe Request reporting nl_preq=%p while "
6299 "in AP mode", bss->nl_preq);
6300 } else if (bss->nl_preq) {
36488c05
JM
6301 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
6302 "reporting nl_preq=%p", bss->nl_preq);
5f65e9f7 6303 nl80211_destroy_eloop_handle(&bss->nl_preq);
5582a5d1
JB
6304 }
6305 return 0;
6306 }
6307
481234cf 6308 if (bss->nl_preq) {
5582a5d1 6309 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
36488c05 6310 "already on! nl_preq=%p", bss->nl_preq);
5582a5d1
JB
6311 return 0;
6312 }
6313
481234cf
JM
6314 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
6315 if (bss->nl_preq == NULL)
5582a5d1 6316 return -1;
36488c05
JM
6317 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
6318 "reporting nl_preq=%p", bss->nl_preq);
5582a5d1 6319
481234cf 6320 if (nl80211_register_frame(bss, bss->nl_preq,
5582a5d1
JB
6321 (WLAN_FC_TYPE_MGMT << 2) |
6322 (WLAN_FC_STYPE_PROBE_REQ << 4),
a92dfde8
JB
6323 NULL, 0) < 0)
6324 goto out_err;
5582a5d1 6325
5f65e9f7
JB
6326 nl80211_register_eloop_read(&bss->nl_preq,
6327 wpa_driver_nl80211_event_receive,
6328 bss->nl_cb);
5582a5d1 6329
504e905c 6330 return 0;
5582a5d1 6331
a92dfde8 6332 out_err:
221a59c9 6333 nl_destroy_handles(&bss->nl_preq);
5582a5d1 6334 return -1;
504e905c
JM
6335}
6336
6337
4e5cb1a3
JM
6338static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
6339 int ifindex, int disabled)
6340{
6341 struct nl_msg *msg;
6342 struct nlattr *bands, *band;
6343 int ret;
6344
3e208481
JM
6345 wpa_printf(MSG_DEBUG,
6346 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
6347 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
6348 "no NL80211_TXRATE_LEGACY constraint");
6349
95376e1a
JM
6350 msg = nl80211_ifindex_msg(drv, ifindex, 0,
6351 NL80211_CMD_SET_TX_BITRATE_MASK);
4e5cb1a3
JM
6352 if (!msg)
6353 return -1;
6354
4e5cb1a3
JM
6355 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
6356 if (!bands)
a862e4a3 6357 goto fail;
4e5cb1a3
JM
6358
6359 /*
6360 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
6361 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
6362 * rates. All 5 GHz rates are left enabled.
6363 */
6364 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
a862e4a3
JM
6365 if (!band ||
6366 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
6367 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
6368 goto fail;
4e5cb1a3
JM
6369 nla_nest_end(msg, band);
6370
6371 nla_nest_end(msg, bands);
6372
6373 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4e5cb1a3
JM
6374 if (ret) {
6375 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
6376 "(%s)", ret, strerror(-ret));
1d0c6fb1
JM
6377 } else
6378 drv->disabled_11b_rates = disabled;
4e5cb1a3
JM
6379
6380 return ret;
6381
a862e4a3 6382fail:
4e5cb1a3
JM
6383 nlmsg_free(msg);
6384 return -1;
6385}
6386
6387
af473088
JM
6388static int wpa_driver_nl80211_deinit_ap(void *priv)
6389{
a2e40bb6
FF
6390 struct i802_bss *bss = priv;
6391 struct wpa_driver_nl80211_data *drv = bss->drv;
b1f625e0 6392 if (!is_ap_interface(drv->nlmode))
af473088
JM
6393 return -1;
6394 wpa_driver_nl80211_del_beacon(drv);
9bc5cfa3 6395 bss->beacon_set = 0;
60b13c20
IP
6396
6397 /*
6398 * If the P2P GO interface was dynamically added, then it is
6399 * possible that the interface change to station is not possible.
6400 */
6401 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
6402 return 0;
6403
b1f625e0 6404 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
af473088
JM
6405}
6406
6407
695c7038
SW
6408static int wpa_driver_nl80211_stop_ap(void *priv)
6409{
6410 struct i802_bss *bss = priv;
6411 struct wpa_driver_nl80211_data *drv = bss->drv;
6412 if (!is_ap_interface(drv->nlmode))
6413 return -1;
6414 wpa_driver_nl80211_del_beacon(drv);
6415 bss->beacon_set = 0;
6416 return 0;
6417}
6418
6419
3c29244e
EP
6420static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
6421{
6422 struct i802_bss *bss = priv;
6423 struct wpa_driver_nl80211_data *drv = bss->drv;
6424 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
6425 return -1;
60b13c20
IP
6426
6427 /*
6428 * If the P2P Client interface was dynamically added, then it is
6429 * possible that the interface change to station is not possible.
6430 */
6431 if (bss->if_dynamic)
6432 return 0;
6433
3c29244e
EP
6434 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
6435}
6436
6437
207ef3fb
JM
6438static void wpa_driver_nl80211_resume(void *priv)
6439{
a2e40bb6 6440 struct i802_bss *bss = priv;
91724d6f
AS
6441
6442 if (i802_set_iface_flags(bss, 1))
6443 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
207ef3fb
JM
6444}
6445
6446
b625473c
JM
6447static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
6448{
6449 struct i802_bss *bss = priv;
6450 struct wpa_driver_nl80211_data *drv = bss->drv;
8970bae8
JB
6451 struct nl_msg *msg;
6452 struct nlattr *cqm;
b625473c
JM
6453
6454 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
6455 "hysteresis=%d", threshold, hysteresis);
6456
13f83980 6457 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
a862e4a3
JM
6458 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
6459 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
6460 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
6461 nlmsg_free(msg);
6462 return -1;
6463 }
8970bae8 6464 nla_nest_end(msg, cqm);
21270bb4 6465
a862e4a3 6466 return send_and_recv_msgs(drv, msg, NULL, NULL);
b625473c
JM
6467}
6468
6469
2cc8d8f4
AO
6470static int get_channel_width(struct nl_msg *msg, void *arg)
6471{
6472 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6473 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6474 struct wpa_signal_info *sig_change = arg;
6475
6476 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6477 genlmsg_attrlen(gnlh, 0), NULL);
6478
6479 sig_change->center_frq1 = -1;
6480 sig_change->center_frq2 = -1;
6481 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
6482
6483 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
6484 sig_change->chanwidth = convert2width(
6485 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
6486 if (tb[NL80211_ATTR_CENTER_FREQ1])
6487 sig_change->center_frq1 =
6488 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
6489 if (tb[NL80211_ATTR_CENTER_FREQ2])
6490 sig_change->center_frq2 =
6491 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
6492 }
6493
6494 return NL_SKIP;
6495}
6496
6497
6498static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
6499 struct wpa_signal_info *sig)
6500{
6501 struct nl_msg *msg;
6502
9725b784 6503 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
2cc8d8f4 6504 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
2cc8d8f4
AO
6505}
6506
6507
1c5c7273
PS
6508static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
6509{
6510 struct i802_bss *bss = priv;
6511 struct wpa_driver_nl80211_data *drv = bss->drv;
6512 int res;
6513
6514 os_memset(si, 0, sizeof(*si));
6515 res = nl80211_get_link_signal(drv, si);
6516 if (res != 0)
6517 return res;
6518
2cc8d8f4
AO
6519 res = nl80211_get_channel_width(drv, si);
6520 if (res != 0)
6521 return res;
6522
1c5c7273
PS
6523 return nl80211_get_link_noise(drv, si);
6524}
6525
6526
b91ab76e
JM
6527static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
6528 int encrypt)
6529{
6530 struct i802_bss *bss = priv;
55231068
JM
6531 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
6532 0, 0, 0, 0);
b91ab76e
JM
6533}
6534
6535
c55f774d
JM
6536static int nl80211_set_param(void *priv, const char *param)
6537{
c55f774d
JM
6538 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
6539 if (param == NULL)
6540 return 0;
6541
6542#ifdef CONFIG_P2P
6543 if (os_strstr(param, "use_p2p_group_interface=1")) {
482856c8
JM
6544 struct i802_bss *bss = priv;
6545 struct wpa_driver_nl80211_data *drv = bss->drv;
6546
c55f774d
JM
6547 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
6548 "interface");
6549 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
6550 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
6551 }
6552#endif /* CONFIG_P2P */
6553
327b01d3
JM
6554 if (os_strstr(param, "use_monitor=1")) {
6555 struct i802_bss *bss = priv;
6556 struct wpa_driver_nl80211_data *drv = bss->drv;
6557 drv->use_monitor = 1;
6558 }
6559
6560 if (os_strstr(param, "force_connect_cmd=1")) {
6561 struct i802_bss *bss = priv;
6562 struct wpa_driver_nl80211_data *drv = bss->drv;
6563 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
b497a212 6564 drv->force_connect_cmd = 1;
327b01d3
JM
6565 }
6566
64abb725
JM
6567 if (os_strstr(param, "no_offchannel_tx=1")) {
6568 struct i802_bss *bss = priv;
6569 struct wpa_driver_nl80211_data *drv = bss->drv;
6570 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
6571 drv->test_use_roc_tx = 1;
6572 }
6573
c55f774d
JM
6574 return 0;
6575}
6576
6577
f2ed8023
JM
6578static void * nl80211_global_init(void)
6579{
6580 struct nl80211_global *global;
36d84860
BG
6581 struct netlink_config *cfg;
6582
f2ed8023
JM
6583 global = os_zalloc(sizeof(*global));
6584 if (global == NULL)
6585 return NULL;
c81eff1a 6586 global->ioctl_sock = -1;
f2ed8023 6587 dl_list_init(&global->interfaces);
ff6a158b 6588 global->if_add_ifindex = -1;
36d84860
BG
6589
6590 cfg = os_zalloc(sizeof(*cfg));
6591 if (cfg == NULL)
6592 goto err;
6593
6594 cfg->ctx = global;
6595 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
6596 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
6597 global->netlink = netlink_init(cfg);
6598 if (global->netlink == NULL) {
6599 os_free(cfg);
6600 goto err;
6601 }
6602
2a7b66f5
BG
6603 if (wpa_driver_nl80211_init_nl_global(global) < 0)
6604 goto err;
6605
c81eff1a
BG
6606 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
6607 if (global->ioctl_sock < 0) {
7ac3616d
JM
6608 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
6609 strerror(errno));
c81eff1a
BG
6610 goto err;
6611 }
6612
f2ed8023 6613 return global;
36d84860
BG
6614
6615err:
6616 nl80211_global_deinit(global);
6617 return NULL;
f2ed8023
JM
6618}
6619
6620
6621static void nl80211_global_deinit(void *priv)
6622{
6623 struct nl80211_global *global = priv;
6624 if (global == NULL)
6625 return;
6626 if (!dl_list_empty(&global->interfaces)) {
6627 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
6628 "nl80211_global_deinit",
6629 dl_list_len(&global->interfaces));
6630 }
36d84860
BG
6631
6632 if (global->netlink)
6633 netlink_deinit(global->netlink);
6634
276e2d67
BG
6635 nl_destroy_handles(&global->nl);
6636
5f65e9f7
JB
6637 if (global->nl_event)
6638 nl80211_destroy_eloop_handle(&global->nl_event);
d6c9aab8
JB
6639
6640 nl_cb_put(global->nl_cb);
2a7b66f5 6641
c81eff1a
BG
6642 if (global->ioctl_sock >= 0)
6643 close(global->ioctl_sock);
6644
f2ed8023
JM
6645 os_free(global);
6646}
6647
6648
6859f1cb
BG
6649static const char * nl80211_get_radio_name(void *priv)
6650{
6651 struct i802_bss *bss = priv;
6652 struct wpa_driver_nl80211_data *drv = bss->drv;
6653 return drv->phyname;
6654}
6655
6656
a6efc65d
JM
6657static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
6658 const u8 *pmkid)
6659{
6660 struct nl_msg *msg;
6661
13f83980 6662 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
a862e4a3
JM
6663 (pmkid && nla_put(msg, NL80211_ATTR_PMKID, 16, pmkid)) ||
6664 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))) {
6665 nlmsg_free(msg);
6666 return -ENOBUFS;
6667 }
a6efc65d
JM
6668
6669 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
a6efc65d
JM
6670}
6671
6672
6673static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6674{
6675 struct i802_bss *bss = priv;
6676 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
6677 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
6678}
6679
6680
6681static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
6682{
6683 struct i802_bss *bss = priv;
6684 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
6685 MAC2STR(bssid));
6686 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
6687}
6688
6689
6690static int nl80211_flush_pmkid(void *priv)
6691{
6692 struct i802_bss *bss = priv;
6693 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
6694 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
6695}
6696
6697
0185007c
MK
6698static void clean_survey_results(struct survey_results *survey_results)
6699{
6700 struct freq_survey *survey, *tmp;
6701
6702 if (dl_list_empty(&survey_results->survey_list))
6703 return;
6704
6705 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
6706 struct freq_survey, list) {
6707 dl_list_del(&survey->list);
6708 os_free(survey);
6709 }
6710}
6711
6712
6713static void add_survey(struct nlattr **sinfo, u32 ifidx,
6714 struct dl_list *survey_list)
6715{
6716 struct freq_survey *survey;
6717
6718 survey = os_zalloc(sizeof(struct freq_survey));
6719 if (!survey)
6720 return;
6721
6722 survey->ifidx = ifidx;
6723 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
6724 survey->filled = 0;
6725
6726 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
6727 survey->nf = (int8_t)
6728 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
6729 survey->filled |= SURVEY_HAS_NF;
6730 }
6731
6732 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
6733 survey->channel_time =
6734 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
6735 survey->filled |= SURVEY_HAS_CHAN_TIME;
6736 }
6737
6738 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
6739 survey->channel_time_busy =
6740 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
6741 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
6742 }
6743
6744 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
6745 survey->channel_time_rx =
6746 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
6747 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
6748 }
6749
6750 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
6751 survey->channel_time_tx =
6752 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
6753 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
6754 }
6755
6756 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)",
6757 survey->freq,
6758 survey->nf,
6759 (unsigned long int) survey->channel_time,
6760 (unsigned long int) survey->channel_time_busy,
6761 (unsigned long int) survey->channel_time_tx,
6762 (unsigned long int) survey->channel_time_rx,
6763 survey->filled);
6764
6765 dl_list_add_tail(survey_list, &survey->list);
6766}
6767
6768
6769static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
6770 unsigned int freq_filter)
6771{
6772 if (!freq_filter)
6773 return 1;
6774
6775 return freq_filter == surveyed_freq;
6776}
6777
6778
6779static int survey_handler(struct nl_msg *msg, void *arg)
6780{
6781 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6782 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6783 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
6784 struct survey_results *survey_results;
6785 u32 surveyed_freq = 0;
6786 u32 ifidx;
6787
6788 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
6789 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
6790 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
6791 };
6792
6793 survey_results = (struct survey_results *) arg;
6794
6795 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6796 genlmsg_attrlen(gnlh, 0), NULL);
6797
e28f39b7
SJ
6798 if (!tb[NL80211_ATTR_IFINDEX])
6799 return NL_SKIP;
6800
0185007c
MK
6801 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
6802
6803 if (!tb[NL80211_ATTR_SURVEY_INFO])
6804 return NL_SKIP;
6805
6806 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
6807 tb[NL80211_ATTR_SURVEY_INFO],
6808 survey_policy))
6809 return NL_SKIP;
6810
6811 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
6812 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
6813 return NL_SKIP;
6814 }
6815
6816 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
6817
6818 if (!check_survey_ok(sinfo, surveyed_freq,
6819 survey_results->freq_filter))
6820 return NL_SKIP;
6821
6822 if (survey_results->freq_filter &&
6823 survey_results->freq_filter != surveyed_freq) {
6824 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
6825 surveyed_freq);
6826 return NL_SKIP;
6827 }
6828
6829 add_survey(sinfo, ifidx, &survey_results->survey_list);
6830
6831 return NL_SKIP;
6832}
6833
6834
6835static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
6836{
6837 struct i802_bss *bss = priv;
6838 struct wpa_driver_nl80211_data *drv = bss->drv;
6839 struct nl_msg *msg;
9725b784 6840 int err;
0185007c
MK
6841 union wpa_event_data data;
6842 struct survey_results *survey_results;
6843
6844 os_memset(&data, 0, sizeof(data));
6845 survey_results = &data.survey_results;
6846
6847 dl_list_init(&survey_results->survey_list);
6848
9725b784 6849 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
0185007c 6850 if (!msg)
a862e4a3 6851 return -ENOBUFS;
0185007c 6852
0185007c
MK
6853 if (freq)
6854 data.survey_results.freq_filter = freq;
6855
6856 do {
6857 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
6858 err = send_and_recv_msgs(drv, msg, survey_handler,
6859 survey_results);
6860 } while (err > 0);
6861
a862e4a3 6862 if (err)
0185007c 6863 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
a862e4a3
JM
6864 else
6865 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
0185007c 6866
0185007c 6867 clean_survey_results(survey_results);
0185007c
MK
6868 return err;
6869}
6870
6871
98cd3d1c
JM
6872static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
6873 const u8 *kck, size_t kck_len,
b14a210c
JB
6874 const u8 *replay_ctr)
6875{
6876 struct i802_bss *bss = priv;
6877 struct wpa_driver_nl80211_data *drv = bss->drv;
6878 struct nlattr *replay_nested;
6879 struct nl_msg *msg;
64ae2447 6880 int ret;
b14a210c 6881
64ae2447
JM
6882 if (!drv->set_rekey_offload)
6883 return;
6884
6885 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
13f83980 6886 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
a862e4a3 6887 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
98cd3d1c
JM
6888 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
6889 nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck) ||
a862e4a3
JM
6890 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
6891 replay_ctr)) {
bbd89bfc 6892 nl80211_nlmsg_clear(msg);
a862e4a3
JM
6893 nlmsg_free(msg);
6894 return;
6895 }
b14a210c
JB
6896
6897 nla_nest_end(msg, replay_nested);
6898
64ae2447
JM
6899 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
6900 if (ret == -EOPNOTSUPP) {
6901 wpa_printf(MSG_DEBUG,
6902 "nl80211: Driver does not support rekey offload");
6903 drv->set_rekey_offload = 0;
6904 }
b14a210c
JB
6905}
6906
6907
39718852
JB
6908static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
6909 const u8 *addr, int qos)
bcf24348 6910{
39718852
JB
6911 /* send data frame to poll STA and check whether
6912 * this frame is ACKed */
bcf24348
JB
6913 struct {
6914 struct ieee80211_hdr hdr;
6915 u16 qos_ctl;
6916 } STRUCT_PACKED nulldata;
6917 size_t size;
6918
6919 /* Send data frame to poll STA and check whether this frame is ACKed */
6920
6921 os_memset(&nulldata, 0, sizeof(nulldata));
6922
6923 if (qos) {
6924 nulldata.hdr.frame_control =
6925 IEEE80211_FC(WLAN_FC_TYPE_DATA,
6926 WLAN_FC_STYPE_QOS_NULL);
6927 size = sizeof(nulldata);
6928 } else {
6929 nulldata.hdr.frame_control =
6930 IEEE80211_FC(WLAN_FC_TYPE_DATA,
6931 WLAN_FC_STYPE_NULLFUNC);
6932 size = sizeof(struct ieee80211_hdr);
6933 }
6934
6935 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
6936 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
6937 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
6938 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
6939
9ebce9c5
JM
6940 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
6941 0, 0) < 0)
bcf24348
JB
6942 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
6943 "send poll frame");
6944}
6945
39718852
JB
6946static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
6947 int qos)
6948{
6949 struct i802_bss *bss = priv;
6950 struct wpa_driver_nl80211_data *drv = bss->drv;
6951 struct nl_msg *msg;
fc99fab7 6952 int ret;
39718852
JB
6953
6954 if (!drv->poll_command_supported) {
6955 nl80211_send_null_frame(bss, own_addr, addr, qos);
6956 return;
6957 }
6958
13f83980 6959 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
a862e4a3
JM
6960 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
6961 nlmsg_free(msg);
6962 return;
6963 }
39718852 6964
fc99fab7
JM
6965 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6966 if (ret < 0) {
6967 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
6968 MACSTR " failed: ret=%d (%s)",
6969 MAC2STR(addr), ret, strerror(-ret));
6970 }
39718852
JB
6971}
6972
bcf24348 6973
29f338af
JM
6974static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
6975{
6976 struct nl_msg *msg;
6977
13f83980 6978 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
a862e4a3
JM
6979 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
6980 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
6981 nlmsg_free(msg);
6982 return -ENOBUFS;
6983 }
29f338af 6984 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
29f338af
JM
6985}
6986
6987
6988static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
6989 int ctwindow)
6990{
6991 struct i802_bss *bss = priv;
6992
6993 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
6994 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
6995
0de38036
JM
6996 if (opp_ps != -1 || ctwindow != -1) {
6997#ifdef ANDROID_P2P
6998 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
6999#else /* ANDROID_P2P */
29f338af 7000 return -1; /* Not yet supported */
0de38036
JM
7001#endif /* ANDROID_P2P */
7002 }
29f338af
JM
7003
7004 if (legacy_ps == -1)
7005 return 0;
7006 if (legacy_ps != 0 && legacy_ps != 1)
7007 return -1; /* Not yet supported */
7008
7009 return nl80211_set_power_save(bss, legacy_ps);
7010}
7011
7012
04e8003c
JD
7013static int nl80211_start_radar_detection(void *priv,
7014 struct hostapd_freq_params *freq)
f90e9c1c
SW
7015{
7016 struct i802_bss *bss = priv;
7017 struct wpa_driver_nl80211_data *drv = bss->drv;
7018 struct nl_msg *msg;
7019 int ret;
7020
04e8003c
JD
7021 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)",
7022 freq->freq, freq->ht_enabled, freq->vht_enabled,
7023 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7024
f90e9c1c
SW
7025 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
7026 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
7027 "detection");
7028 return -1;
7029 }
7030
9725b784 7031 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
a862e4a3
JM
7032 nl80211_put_freq_params(msg, freq) < 0) {
7033 nlmsg_free(msg);
7034 return -1;
7035 }
f90e9c1c
SW
7036
7037 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7038 if (ret == 0)
7039 return 0;
7040 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
7041 "%d (%s)", ret, strerror(-ret));
f90e9c1c
SW
7042 return -1;
7043}
7044
03ea1786
AN
7045#ifdef CONFIG_TDLS
7046
7047static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
7048 u8 dialog_token, u16 status_code,
984dadc2
AN
7049 u32 peer_capab, int initiator, const u8 *buf,
7050 size_t len)
03ea1786
AN
7051{
7052 struct i802_bss *bss = priv;
7053 struct wpa_driver_nl80211_data *drv = bss->drv;
7054 struct nl_msg *msg;
7055
7056 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7057 return -EOPNOTSUPP;
7058
7059 if (!dst)
7060 return -EINVAL;
7061
9725b784 7062 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
a862e4a3
JM
7063 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7064 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
7065 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
7066 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
7067 goto fail;
96ecea5e
SD
7068 if (peer_capab) {
7069 /*
7070 * The internal enum tdls_peer_capability definition is
7071 * currently identical with the nl80211 enum
7072 * nl80211_tdls_peer_capability, so no conversion is needed
7073 * here.
7074 */
a862e4a3
JM
7075 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
7076 peer_capab))
7077 goto fail;
96ecea5e 7078 }
a862e4a3
JM
7079 if ((initiator &&
7080 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
7081 nla_put(msg, NL80211_ATTR_IE, len, buf))
7082 goto fail;
03ea1786
AN
7083
7084 return send_and_recv_msgs(drv, msg, NULL, NULL);
7085
a862e4a3 7086fail:
03ea1786
AN
7087 nlmsg_free(msg);
7088 return -ENOBUFS;
7089}
7090
7091
7092static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
7093{
7094 struct i802_bss *bss = priv;
7095 struct wpa_driver_nl80211_data *drv = bss->drv;
7096 struct nl_msg *msg;
7097 enum nl80211_tdls_operation nl80211_oper;
7098
7099 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7100 return -EOPNOTSUPP;
7101
7102 switch (oper) {
7103 case TDLS_DISCOVERY_REQ:
7104 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
7105 break;
7106 case TDLS_SETUP:
7107 nl80211_oper = NL80211_TDLS_SETUP;
7108 break;
7109 case TDLS_TEARDOWN:
7110 nl80211_oper = NL80211_TDLS_TEARDOWN;
7111 break;
7112 case TDLS_ENABLE_LINK:
7113 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
7114 break;
7115 case TDLS_DISABLE_LINK:
7116 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
7117 break;
7118 case TDLS_ENABLE:
7119 return 0;
7120 case TDLS_DISABLE:
7121 return 0;
7122 default:
7123 return -EINVAL;
7124 }
7125
9725b784 7126 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
a862e4a3 7127 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
a862e4a3
JM
7128 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
7129 nlmsg_free(msg);
7130 return -ENOBUFS;
7131 }
03ea1786
AN
7132
7133 return send_and_recv_msgs(drv, msg, NULL, NULL);
03ea1786
AN
7134}
7135
72b2605f
AN
7136
7137static int
7138nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
7139 const struct hostapd_freq_params *params)
7140{
7141 struct i802_bss *bss = priv;
7142 struct wpa_driver_nl80211_data *drv = bss->drv;
7143 struct nl_msg *msg;
7144 int ret = -ENOBUFS;
7145
7146 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7147 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7148 return -EOPNOTSUPP;
7149
7150 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
7151 " oper_class=%u freq=%u",
7152 MAC2STR(addr), oper_class, params->freq);
7153 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
7154 if (!msg ||
7155 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7156 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
7157 (ret = nl80211_put_freq_params(msg, params))) {
7158 nlmsg_free(msg);
7159 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
7160 return ret;
7161 }
7162
7163 return send_and_recv_msgs(drv, msg, NULL, NULL);
7164}
7165
7166
7167static int
7168nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
7169{
7170 struct i802_bss *bss = priv;
7171 struct wpa_driver_nl80211_data *drv = bss->drv;
7172 struct nl_msg *msg;
7173
7174 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7175 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7176 return -EOPNOTSUPP;
7177
7178 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
7179 MAC2STR(addr));
7180 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
7181 if (!msg ||
7182 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7183 nlmsg_free(msg);
7184 wpa_printf(MSG_DEBUG,
7185 "nl80211: Could not build TDLS cancel chan switch");
7186 return -ENOBUFS;
7187 }
7188
7189 return send_and_recv_msgs(drv, msg, NULL, NULL);
7190}
7191
03ea1786
AN
7192#endif /* CONFIG TDLS */
7193
7194
9ebce9c5
JM
7195static int driver_nl80211_set_key(const char *ifname, void *priv,
7196 enum wpa_alg alg, const u8 *addr,
7197 int key_idx, int set_tx,
7198 const u8 *seq, size_t seq_len,
7199 const u8 *key, size_t key_len)
7200{
7201 struct i802_bss *bss = priv;
7202 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
7203 set_tx, seq, seq_len, key, key_len);
7204}
7205
7206
7207static int driver_nl80211_scan2(void *priv,
7208 struct wpa_driver_scan_params *params)
7209{
7210 struct i802_bss *bss = priv;
7211 return wpa_driver_nl80211_scan(bss, params);
7212}
7213
7214
7215static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
7216 int reason_code)
7217{
7218 struct i802_bss *bss = priv;
7219 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
7220}
7221
7222
7223static int driver_nl80211_authenticate(void *priv,
7224 struct wpa_driver_auth_params *params)
7225{
7226 struct i802_bss *bss = priv;
7227 return wpa_driver_nl80211_authenticate(bss, params);
7228}
7229
7230
7231static void driver_nl80211_deinit(void *priv)
7232{
7233 struct i802_bss *bss = priv;
7234 wpa_driver_nl80211_deinit(bss);
7235}
7236
7237
7238static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
7239 const char *ifname)
7240{
7241 struct i802_bss *bss = priv;
7242 return wpa_driver_nl80211_if_remove(bss, type, ifname);
7243}
7244
7245
7246static int driver_nl80211_send_mlme(void *priv, const u8 *data,
5d180a77
AO
7247 size_t data_len, int noack,
7248 unsigned int freq)
9ebce9c5
JM
7249{
7250 struct i802_bss *bss = priv;
7251 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
5d180a77 7252 freq, 0, 0, 0);
9ebce9c5
JM
7253}
7254
7255
7256static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
7257{
7258 struct i802_bss *bss = priv;
59d7148a 7259 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
9ebce9c5
JM
7260}
7261
7262
9ebce9c5
JM
7263static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
7264 const char *ifname, int vlan_id)
7265{
7266 struct i802_bss *bss = priv;
7267 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
7268}
9ebce9c5
JM
7269
7270
7271static int driver_nl80211_read_sta_data(void *priv,
7272 struct hostap_sta_driver_data *data,
7273 const u8 *addr)
7274{
7275 struct i802_bss *bss = priv;
7276 return i802_read_sta_data(bss, data, addr);
7277}
7278
7279
7280static int driver_nl80211_send_action(void *priv, unsigned int freq,
7281 unsigned int wait_time,
7282 const u8 *dst, const u8 *src,
7283 const u8 *bssid,
7284 const u8 *data, size_t data_len,
7285 int no_cck)
7286{
7287 struct i802_bss *bss = priv;
7288 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
7289 bssid, data, data_len, no_cck);
7290}
7291
7292
7293static int driver_nl80211_probe_req_report(void *priv, int report)
7294{
7295 struct i802_bss *bss = priv;
7296 return wpa_driver_nl80211_probe_req_report(bss, report);
7297}
7298
7299
6a1ce395
DG
7300static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
7301 const u8 *ies, size_t ies_len)
7302{
7303 int ret;
7304 struct nl_msg *msg;
7305 struct i802_bss *bss = priv;
7306 struct wpa_driver_nl80211_data *drv = bss->drv;
7307 u16 mdid = WPA_GET_LE16(md);
7308
6a1ce395 7309 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
9725b784 7310 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
a862e4a3
JM
7311 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
7312 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
7313 nlmsg_free(msg);
7314 return -ENOBUFS;
7315 }
6a1ce395
DG
7316
7317 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7318 if (ret) {
7319 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
7320 "err=%d (%s)", ret, strerror(-ret));
7321 }
7322
7323 return ret;
6a1ce395
DG
7324}
7325
7326
597b94f5
AS
7327const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
7328{
7329 struct i802_bss *bss = priv;
7330 struct wpa_driver_nl80211_data *drv = bss->drv;
7331
7332 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
7333 return NULL;
7334
7335 return bss->addr;
7336}
7337
7338
a771c07d
JM
7339static const char * scan_state_str(enum scan_states scan_state)
7340{
7341 switch (scan_state) {
7342 case NO_SCAN:
7343 return "NO_SCAN";
7344 case SCAN_REQUESTED:
7345 return "SCAN_REQUESTED";
7346 case SCAN_STARTED:
7347 return "SCAN_STARTED";
7348 case SCAN_COMPLETED:
7349 return "SCAN_COMPLETED";
7350 case SCAN_ABORTED:
7351 return "SCAN_ABORTED";
7352 case SCHED_SCAN_STARTED:
7353 return "SCHED_SCAN_STARTED";
7354 case SCHED_SCAN_STOPPED:
7355 return "SCHED_SCAN_STOPPED";
7356 case SCHED_SCAN_RESULTS:
7357 return "SCHED_SCAN_RESULTS";
7358 }
7359
7360 return "??";
7361}
7362
7363
7364static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
7365{
7366 struct i802_bss *bss = priv;
7367 struct wpa_driver_nl80211_data *drv = bss->drv;
7368 int res;
7369 char *pos, *end;
7370
7371 pos = buf;
7372 end = buf + buflen;
7373
7374 res = os_snprintf(pos, end - pos,
7375 "ifindex=%d\n"
7376 "ifname=%s\n"
7377 "brname=%s\n"
7378 "addr=" MACSTR "\n"
7379 "freq=%d\n"
7380 "%s%s%s%s%s",
7381 bss->ifindex,
7382 bss->ifname,
7383 bss->brname,
7384 MAC2STR(bss->addr),
7385 bss->freq,
7386 bss->beacon_set ? "beacon_set=1\n" : "",
7387 bss->added_if_into_bridge ?
7388 "added_if_into_bridge=1\n" : "",
7389 bss->added_bridge ? "added_bridge=1\n" : "",
7390 bss->in_deinit ? "in_deinit=1\n" : "",
7391 bss->if_dynamic ? "if_dynamic=1\n" : "");
d85e1fc8 7392 if (os_snprintf_error(end - pos, res))
a771c07d
JM
7393 return pos - buf;
7394 pos += res;
7395
7396 if (bss->wdev_id_set) {
7397 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
7398 (unsigned long long) bss->wdev_id);
d85e1fc8 7399 if (os_snprintf_error(end - pos, res))
a771c07d
JM
7400 return pos - buf;
7401 pos += res;
7402 }
7403
7404 res = os_snprintf(pos, end - pos,
7405 "phyname=%s\n"
fee354c7 7406 "perm_addr=" MACSTR "\n"
a771c07d
JM
7407 "drv_ifindex=%d\n"
7408 "operstate=%d\n"
7409 "scan_state=%s\n"
7410 "auth_bssid=" MACSTR "\n"
7411 "auth_attempt_bssid=" MACSTR "\n"
7412 "bssid=" MACSTR "\n"
7413 "prev_bssid=" MACSTR "\n"
7414 "associated=%d\n"
7415 "assoc_freq=%u\n"
7416 "monitor_sock=%d\n"
7417 "monitor_ifidx=%d\n"
7418 "monitor_refcount=%d\n"
7419 "last_mgmt_freq=%u\n"
7420 "eapol_tx_sock=%d\n"
97752f79 7421 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
a771c07d 7422 drv->phyname,
fee354c7 7423 MAC2STR(drv->perm_addr),
a771c07d
JM
7424 drv->ifindex,
7425 drv->operstate,
7426 scan_state_str(drv->scan_state),
7427 MAC2STR(drv->auth_bssid),
7428 MAC2STR(drv->auth_attempt_bssid),
7429 MAC2STR(drv->bssid),
7430 MAC2STR(drv->prev_bssid),
7431 drv->associated,
7432 drv->assoc_freq,
7433 drv->monitor_sock,
7434 drv->monitor_ifidx,
7435 drv->monitor_refcount,
7436 drv->last_mgmt_freq,
7437 drv->eapol_tx_sock,
7438 drv->ignore_if_down_event ?
7439 "ignore_if_down_event=1\n" : "",
7440 drv->scan_complete_events ?
7441 "scan_complete_events=1\n" : "",
7442 drv->disabled_11b_rates ?
7443 "disabled_11b_rates=1\n" : "",
7444 drv->pending_remain_on_chan ?
7445 "pending_remain_on_chan=1\n" : "",
7446 drv->in_interface_list ? "in_interface_list=1\n" : "",
7447 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
7448 drv->poll_command_supported ?
7449 "poll_command_supported=1\n" : "",
7450 drv->data_tx_status ? "data_tx_status=1\n" : "",
7451 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
7452 drv->retry_auth ? "retry_auth=1\n" : "",
7453 drv->use_monitor ? "use_monitor=1\n" : "",
7454 drv->ignore_next_local_disconnect ?
7455 "ignore_next_local_disconnect=1\n" : "",
d6a36f39 7456 drv->ignore_next_local_deauth ?
97752f79 7457 "ignore_next_local_deauth=1\n" : "");
d85e1fc8 7458 if (os_snprintf_error(end - pos, res))
a771c07d
JM
7459 return pos - buf;
7460 pos += res;
7461
7462 if (drv->has_capability) {
7463 res = os_snprintf(pos, end - pos,
7464 "capa.key_mgmt=0x%x\n"
7465 "capa.enc=0x%x\n"
7466 "capa.auth=0x%x\n"
24bd4e0b 7467 "capa.flags=0x%llx\n"
a0a34d53 7468 "capa.rrm_flags=0x%x\n"
a771c07d
JM
7469 "capa.max_scan_ssids=%d\n"
7470 "capa.max_sched_scan_ssids=%d\n"
7471 "capa.sched_scan_supported=%d\n"
7472 "capa.max_match_sets=%d\n"
7473 "capa.max_remain_on_chan=%u\n"
7474 "capa.max_stations=%u\n"
7475 "capa.probe_resp_offloads=0x%x\n"
7476 "capa.max_acl_mac_addrs=%u\n"
86056fea
IP
7477 "capa.num_multichan_concurrent=%u\n"
7478 "capa.mac_addr_rand_sched_scan_supported=%d\n"
7479 "capa.mac_addr_rand_scan_supported=%d\n",
a771c07d
JM
7480 drv->capa.key_mgmt,
7481 drv->capa.enc,
7482 drv->capa.auth,
24bd4e0b 7483 (unsigned long long) drv->capa.flags,
a0a34d53 7484 drv->capa.rrm_flags,
a771c07d
JM
7485 drv->capa.max_scan_ssids,
7486 drv->capa.max_sched_scan_ssids,
7487 drv->capa.sched_scan_supported,
7488 drv->capa.max_match_sets,
7489 drv->capa.max_remain_on_chan,
7490 drv->capa.max_stations,
7491 drv->capa.probe_resp_offloads,
7492 drv->capa.max_acl_mac_addrs,
86056fea
IP
7493 drv->capa.num_multichan_concurrent,
7494 drv->capa.mac_addr_rand_sched_scan_supported,
7495 drv->capa.mac_addr_rand_scan_supported);
d85e1fc8 7496 if (os_snprintf_error(end - pos, res))
a771c07d
JM
7497 return pos - buf;
7498 pos += res;
7499 }
7500
7501 return pos - buf;
7502}
7503
7504
1c4ffa87
AO
7505static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
7506{
a862e4a3
JM
7507 if ((settings->head &&
7508 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
7509 settings->head_len, settings->head)) ||
7510 (settings->tail &&
7511 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
7512 settings->tail_len, settings->tail)) ||
7513 (settings->beacon_ies &&
7514 nla_put(msg, NL80211_ATTR_IE,
7515 settings->beacon_ies_len, settings->beacon_ies)) ||
7516 (settings->proberesp_ies &&
7517 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
7518 settings->proberesp_ies_len, settings->proberesp_ies)) ||
7519 (settings->assocresp_ies &&
7520 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
7521 settings->assocresp_ies_len, settings->assocresp_ies)) ||
7522 (settings->probe_resp &&
7523 nla_put(msg, NL80211_ATTR_PROBE_RESP,
7524 settings->probe_resp_len, settings->probe_resp)))
7525 return -ENOBUFS;
1c4ffa87
AO
7526
7527 return 0;
1c4ffa87
AO
7528}
7529
7530
7531static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
7532{
7533 struct nl_msg *msg;
7534 struct i802_bss *bss = priv;
7535 struct wpa_driver_nl80211_data *drv = bss->drv;
7536 struct nlattr *beacon_csa;
7537 int ret = -ENOBUFS;
7538
8d1fdde7 7539 wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
1c4ffa87 7540 settings->cs_count, settings->block_tx,
8d1fdde7
JD
7541 settings->freq_params.freq, settings->freq_params.bandwidth,
7542 settings->freq_params.center_freq1,
7543 settings->freq_params.center_freq2);
1c4ffa87 7544
991aa9c7 7545 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
1c4ffa87
AO
7546 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
7547 return -EOPNOTSUPP;
7548 }
7549
7550 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
7551 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
7552 return -EOPNOTSUPP;
7553
7554 /* check settings validity */
7555 if (!settings->beacon_csa.tail ||
7556 ((settings->beacon_csa.tail_len <=
7557 settings->counter_offset_beacon) ||
7558 (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
7559 settings->cs_count)))
7560 return -EINVAL;
7561
7562 if (settings->beacon_csa.probe_resp &&
7563 ((settings->beacon_csa.probe_resp_len <=
7564 settings->counter_offset_presp) ||
7565 (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
7566 settings->cs_count)))
7567 return -EINVAL;
7568
13f83980 7569 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
a862e4a3
JM
7570 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
7571 settings->cs_count) ||
7572 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
7573 (settings->block_tx &&
7574 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
1c4ffa87
AO
7575 goto error;
7576
1c4ffa87
AO
7577 /* beacon_after params */
7578 ret = set_beacon_data(msg, &settings->beacon_after);
7579 if (ret)
7580 goto error;
7581
7582 /* beacon_csa params */
7583 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
7584 if (!beacon_csa)
a862e4a3 7585 goto fail;
1c4ffa87
AO
7586
7587 ret = set_beacon_data(msg, &settings->beacon_csa);
7588 if (ret)
7589 goto error;
7590
a862e4a3
JM
7591 if (nla_put_u16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
7592 settings->counter_offset_beacon) ||
7593 (settings->beacon_csa.probe_resp &&
7594 nla_put_u16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
7595 settings->counter_offset_presp)))
7596 goto fail;
1c4ffa87
AO
7597
7598 nla_nest_end(msg, beacon_csa);
7599 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7600 if (ret) {
7601 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
7602 ret, strerror(-ret));
7603 }
7604 return ret;
7605
a862e4a3 7606fail:
1c4ffa87
AO
7607 ret = -ENOBUFS;
7608error:
7609 nlmsg_free(msg);
7610 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
7611 return ret;
7612}
7613
7614
dfa87878
MB
7615static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
7616 u8 user_priority, u16 admitted_time)
7617{
7618 struct i802_bss *bss = priv;
7619 struct wpa_driver_nl80211_data *drv = bss->drv;
7620 struct nl_msg *msg;
7621 int ret;
7622
7623 wpa_printf(MSG_DEBUG,
7624 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
7625 tsid, admitted_time, user_priority);
7626
7627 if (!is_sta_interface(drv->nlmode))
7628 return -ENOTSUP;
7629
56f77852
JM
7630 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
7631 if (!msg ||
a862e4a3
JM
7632 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
7633 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7634 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
7635 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
7636 nlmsg_free(msg);
7637 return -ENOBUFS;
7638 }
dfa87878
MB
7639
7640 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7641 if (ret)
7642 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
7643 ret, strerror(-ret));
7644 return ret;
dfa87878
MB
7645}
7646
7647
7648static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
7649{
7650 struct i802_bss *bss = priv;
7651 struct wpa_driver_nl80211_data *drv = bss->drv;
7652 struct nl_msg *msg;
7653 int ret;
7654
7655 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
7656
7657 if (!is_sta_interface(drv->nlmode))
7658 return -ENOTSUP;
7659
56f77852 7660 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
a862e4a3
JM
7661 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
7662 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7663 nlmsg_free(msg);
7664 return -ENOBUFS;
7665 }
dfa87878
MB
7666
7667 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7668 if (ret)
7669 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
7670 ret, strerror(-ret));
7671 return ret;
dfa87878
MB
7672}
7673
7674
6b9f7af6
JM
7675#ifdef CONFIG_TESTING_OPTIONS
7676static int cmd_reply_handler(struct nl_msg *msg, void *arg)
7677{
7678 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7679 struct wpabuf *buf = arg;
7680
7681 if (!buf)
7682 return NL_SKIP;
7683
7684 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
7685 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
7686 return NL_SKIP;
7687 }
7688
7689 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
7690 genlmsg_attrlen(gnlh, 0));
7691
7692 return NL_SKIP;
7693}
7694#endif /* CONFIG_TESTING_OPTIONS */
7695
7696
adef8948
BL
7697static int vendor_reply_handler(struct nl_msg *msg, void *arg)
7698{
7699 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7700 struct nlattr *nl_vendor_reply, *nl;
7701 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7702 struct wpabuf *buf = arg;
7703 int rem;
7704
7705 if (!buf)
7706 return NL_SKIP;
7707
7708 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7709 genlmsg_attrlen(gnlh, 0), NULL);
7710 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
7711
7712 if (!nl_vendor_reply)
7713 return NL_SKIP;
7714
7715 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
7716 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
7717 return NL_SKIP;
7718 }
7719
7720 nla_for_each_nested(nl, nl_vendor_reply, rem) {
7721 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
7722 }
7723
7724 return NL_SKIP;
7725}
7726
7727
7728static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
7729 unsigned int subcmd, const u8 *data,
7730 size_t data_len, struct wpabuf *buf)
7731{
7732 struct i802_bss *bss = priv;
7733 struct wpa_driver_nl80211_data *drv = bss->drv;
7734 struct nl_msg *msg;
7735 int ret;
7736
6b9f7af6
JM
7737#ifdef CONFIG_TESTING_OPTIONS
7738 if (vendor_id == 0xffffffff) {
56f77852
JM
7739 msg = nlmsg_alloc();
7740 if (!msg)
7741 return -ENOMEM;
7742
6b9f7af6
JM
7743 nl80211_cmd(drv, msg, 0, subcmd);
7744 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
7745 0)
a862e4a3 7746 goto fail;
6b9f7af6
JM
7747 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
7748 if (ret)
7749 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
7750 ret);
7751 return ret;
7752 }
7753#endif /* CONFIG_TESTING_OPTIONS */
7754
56f77852 7755 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
a862e4a3
JM
7756 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
7757 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
7758 (data &&
7759 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
7760 goto fail;
adef8948
BL
7761
7762 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
7763 if (ret)
7764 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
7765 ret);
7766 return ret;
7767
a862e4a3 7768fail:
adef8948
BL
7769 nlmsg_free(msg);
7770 return -ENOBUFS;
7771}
7772
7773
049105b4
KP
7774static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
7775 u8 qos_map_set_len)
7776{
7777 struct i802_bss *bss = priv;
7778 struct wpa_driver_nl80211_data *drv = bss->drv;
7779 struct nl_msg *msg;
7780 int ret;
7781
049105b4
KP
7782 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
7783 qos_map_set, qos_map_set_len);
7784
9725b784 7785 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
a862e4a3
JM
7786 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
7787 nlmsg_free(msg);
7788 return -ENOBUFS;
7789 }
049105b4
KP
7790
7791 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7792 if (ret)
7793 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
7794
7795 return ret;
049105b4
KP
7796}
7797
7798
e4fa8b12
EP
7799static int nl80211_set_wowlan(void *priv,
7800 const struct wowlan_triggers *triggers)
7801{
7802 struct i802_bss *bss = priv;
7803 struct wpa_driver_nl80211_data *drv = bss->drv;
7804 struct nl_msg *msg;
7805 struct nlattr *wowlan_triggers;
7806 int ret;
7807
e4fa8b12
EP
7808 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
7809
1ac977bd 7810 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
a862e4a3
JM
7811 !(wowlan_triggers = nla_nest_start(msg,
7812 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
7813 (triggers->any &&
7814 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
7815 (triggers->disconnect &&
7816 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
7817 (triggers->magic_pkt &&
7818 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
7819 (triggers->gtk_rekey_failure &&
7820 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
7821 (triggers->eap_identity_req &&
7822 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
7823 (triggers->four_way_handshake &&
7824 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
7825 (triggers->rfkill_release &&
7826 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
7827 nlmsg_free(msg);
7828 return -ENOBUFS;
7829 }
e4fa8b12
EP
7830
7831 nla_nest_end(msg, wowlan_triggers);
7832
7833 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7834 if (ret)
7835 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
7836
7837 return ret;
e4fa8b12
EP
7838}
7839
7840
0800f9ee
JM
7841static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
7842{
7843 struct i802_bss *bss = priv;
7844 struct wpa_driver_nl80211_data *drv = bss->drv;
7845 struct nl_msg *msg;
7846 struct nlattr *params;
7847
7848 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
7849
7850 if (!drv->roaming_vendor_cmd_avail) {
7851 wpa_printf(MSG_DEBUG,
7852 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
7853 return -1;
7854 }
7855
9725b784 7856 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
a862e4a3
JM
7857 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
7858 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
7859 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
7860 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
7861 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
7862 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
7863 QCA_ROAMING_NOT_ALLOWED) ||
7864 (bssid &&
7865 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
7866 nlmsg_free(msg);
7867 return -1;
7868 }
0800f9ee
JM
7869 nla_nest_end(msg, params);
7870
7871 return send_and_recv_msgs(drv, msg, NULL, NULL);
0800f9ee
JM
7872}
7873
7874
fee354c7
JM
7875static int nl80211_set_mac_addr(void *priv, const u8 *addr)
7876{
7877 struct i802_bss *bss = priv;
7878 struct wpa_driver_nl80211_data *drv = bss->drv;
7879 int new_addr = addr != NULL;
7880
7881 if (!addr)
7882 addr = drv->perm_addr;
7883
7884 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
7885 return -1;
7886
7887 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
7888 {
7889 wpa_printf(MSG_DEBUG,
7890 "nl80211: failed to set_mac_addr for %s to " MACSTR,
7891 bss->ifname, MAC2STR(addr));
7892 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
7893 1) < 0) {
7894 wpa_printf(MSG_DEBUG,
7895 "nl80211: Could not restore interface UP after failed set_mac_addr");
7896 }
7897 return -1;
7898 }
7899
7900 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
7901 bss->ifname, MAC2STR(addr));
7902 drv->addr_changed = new_addr;
7903 os_memcpy(bss->addr, addr, ETH_ALEN);
7904
7905 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
7906 {
7907 wpa_printf(MSG_DEBUG,
7908 "nl80211: Could not restore interface UP after set_mac_addr");
7909 }
7910
7911 return 0;
7912}
7913
7914
6c1664f6
BC
7915#ifdef CONFIG_MESH
7916
7917static int wpa_driver_nl80211_init_mesh(void *priv)
7918{
7919 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
7920 wpa_printf(MSG_INFO,
7921 "nl80211: Failed to set interface into mesh mode");
7922 return -1;
7923 }
7924 return 0;
7925}
7926
7927
21c74e84
JM
7928static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
7929 size_t mesh_id_len)
7930{
7931 if (mesh_id) {
7932 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
7933 mesh_id, mesh_id_len);
7934 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
7935 }
7936
7937 return 0;
7938}
7939
7940
0cb5f8d9 7941static int nl80211_join_mesh(struct i802_bss *bss,
6c1664f6
BC
7942 struct wpa_driver_mesh_join_params *params)
7943{
6c1664f6
BC
7944 struct wpa_driver_nl80211_data *drv = bss->drv;
7945 struct nl_msg *msg;
7946 struct nlattr *container;
7451a217 7947 int ret = -1;
6c1664f6 7948
6c1664f6 7949 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
9725b784 7950 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
f7e889fa
JM
7951 if (!msg ||
7952 nl80211_put_freq_params(msg, &params->freq) ||
21c74e84
JM
7953 nl80211_put_basic_rates(msg, params->basic_rates) ||
7954 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
7955 nl80211_put_beacon_int(msg, params->beacon_int))
85e1fad8 7956 goto fail;
9c58c5f7 7957
6c1664f6
BC
7958 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
7959
7960 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
7961 if (!container)
a862e4a3 7962 goto fail;
6c1664f6
BC
7963
7964 if (params->ies) {
7965 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
a862e4a3
JM
7966 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
7967 params->ies))
7968 goto fail;
6c1664f6
BC
7969 }
7970 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
7971 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
a862e4a3
JM
7972 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
7973 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
7974 goto fail;
6c1664f6 7975 }
a862e4a3
JM
7976 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
7977 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
7978 goto fail;
7979 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
7980 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
7981 goto fail;
6c1664f6
BC
7982 nla_nest_end(msg, container);
7983
7984 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
7985 if (!container)
a862e4a3 7986 goto fail;
6c1664f6 7987
a862e4a3
JM
7988 if (!(params->conf.flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
7989 nla_put_u32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS, 0))
7990 goto fail;
4b409368
MH
7991 if ((params->conf.flags & WPA_DRIVER_MESH_FLAG_DRIVER_MPM) &&
7992 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
7993 params->max_peer_links))
7994 goto fail;
5a2a6de6
MH
7995
7996 /*
7997 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
7998 * the timer could disconnect stations even in that case.
5a2a6de6 7999 */
0cb5f8d9
MH
8000 if (nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
8001 params->conf.peer_link_timeout)) {
5a2a6de6
MH
8002 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
8003 goto fail;
8004 }
8005
6c1664f6
BC
8006 nla_nest_end(msg, container);
8007
8008 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8009 msg = NULL;
8010 if (ret) {
8011 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
8012 ret, strerror(-ret));
a862e4a3 8013 goto fail;
6c1664f6
BC
8014 }
8015 ret = 0;
f7e889fa 8016 bss->freq = params->freq.freq;
6c1664f6
BC
8017 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
8018
a862e4a3 8019fail:
6c1664f6
BC
8020 nlmsg_free(msg);
8021 return ret;
8022}
8023
8024
0cb5f8d9
MH
8025static int
8026wpa_driver_nl80211_join_mesh(void *priv,
8027 struct wpa_driver_mesh_join_params *params)
8028{
8029 struct i802_bss *bss = priv;
8030 int ret, timeout;
8031
8032 timeout = params->conf.peer_link_timeout;
8033
8034 /* Disable kernel inactivity timer */
8035 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
8036 params->conf.peer_link_timeout = 0;
8037
8038 ret = nl80211_join_mesh(bss, params);
8039 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
8040 wpa_printf(MSG_DEBUG,
8041 "nl80211: Mesh join retry for peer_link_timeout");
8042 /*
8043 * Old kernel does not support setting
8044 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
8045 * into future from peer_link_timeout.
8046 */
8047 params->conf.peer_link_timeout = timeout + 60;
8048 ret = nl80211_join_mesh(priv, params);
8049 }
8050
8051 params->conf.peer_link_timeout = timeout;
8052 return ret;
8053}
8054
8055
6c1664f6
BC
8056static int wpa_driver_nl80211_leave_mesh(void *priv)
8057{
8058 struct i802_bss *bss = priv;
8059 struct wpa_driver_nl80211_data *drv = bss->drv;
8060 struct nl_msg *msg;
9725b784 8061 int ret;
6c1664f6
BC
8062
8063 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
9725b784 8064 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
6c1664f6 8065 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6c1664f6
BC
8066 if (ret) {
8067 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
8068 ret, strerror(-ret));
a862e4a3
JM
8069 } else {
8070 wpa_printf(MSG_DEBUG,
8071 "nl80211: mesh leave request send successfully");
6c1664f6 8072 }
6c1664f6 8073
f33c82d2
JM
8074 if (wpa_driver_nl80211_set_mode(drv->first_bss,
8075 NL80211_IFTYPE_STATION)) {
8076 wpa_printf(MSG_INFO,
8077 "nl80211: Failed to set interface into station mode");
8078 }
6c1664f6
BC
8079 return ret;
8080}
8081
8082#endif /* CONFIG_MESH */
8083
8084
ed4ddb6d
KP
8085static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
8086 const u8 *ipaddr, int prefixlen,
8087 const u8 *addr)
71103bed
KP
8088{
8089#ifdef CONFIG_LIBNL3_ROUTE
8090 struct i802_bss *bss = priv;
8091 struct wpa_driver_nl80211_data *drv = bss->drv;
8092 struct rtnl_neigh *rn;
8093 struct nl_addr *nl_ipaddr = NULL;
8094 struct nl_addr *nl_lladdr = NULL;
ed4ddb6d 8095 int family, addrsize;
71103bed
KP
8096 int res;
8097
ed4ddb6d 8098 if (!ipaddr || prefixlen == 0 || !addr)
71103bed
KP
8099 return -EINVAL;
8100
8101 if (bss->br_ifindex == 0) {
8102 wpa_printf(MSG_DEBUG,
8103 "nl80211: bridge must be set before adding an ip neigh to it");
8104 return -1;
8105 }
8106
8107 if (!drv->rtnl_sk) {
8108 wpa_printf(MSG_DEBUG,
8109 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8110 return -1;
8111 }
8112
ed4ddb6d
KP
8113 if (version == 4) {
8114 family = AF_INET;
8115 addrsize = 4;
8116 } else if (version == 6) {
8117 family = AF_INET6;
8118 addrsize = 16;
8119 } else {
8120 return -EINVAL;
8121 }
8122
71103bed
KP
8123 rn = rtnl_neigh_alloc();
8124 if (rn == NULL)
8125 return -ENOMEM;
8126
8127 /* set the destination ip address for neigh */
ed4ddb6d 8128 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
71103bed
KP
8129 if (nl_ipaddr == NULL) {
8130 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8131 res = -ENOMEM;
8132 goto errout;
8133 }
8134 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
8135 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8136 if (res) {
8137 wpa_printf(MSG_DEBUG,
8138 "nl80211: neigh set destination addr failed");
8139 goto errout;
8140 }
8141
8142 /* set the corresponding lladdr for neigh */
8143 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
8144 if (nl_lladdr == NULL) {
8145 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
8146 res = -ENOMEM;
8147 goto errout;
8148 }
8149 rtnl_neigh_set_lladdr(rn, nl_lladdr);
8150
8151 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8152 rtnl_neigh_set_state(rn, NUD_PERMANENT);
8153
8154 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
8155 if (res) {
8156 wpa_printf(MSG_DEBUG,
8157 "nl80211: Adding bridge ip neigh failed: %s",
8158 strerror(errno));
8159 }
8160errout:
8161 if (nl_lladdr)
8162 nl_addr_put(nl_lladdr);
8163 if (nl_ipaddr)
8164 nl_addr_put(nl_ipaddr);
8165 if (rn)
8166 rtnl_neigh_put(rn);
8167 return res;
8168#else /* CONFIG_LIBNL3_ROUTE */
8169 return -1;
8170#endif /* CONFIG_LIBNL3_ROUTE */
8171}
8172
8173
ed4ddb6d
KP
8174static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
8175 const u8 *ipaddr)
71103bed
KP
8176{
8177#ifdef CONFIG_LIBNL3_ROUTE
8178 struct i802_bss *bss = priv;
8179 struct wpa_driver_nl80211_data *drv = bss->drv;
8180 struct rtnl_neigh *rn;
8181 struct nl_addr *nl_ipaddr;
ed4ddb6d 8182 int family, addrsize;
71103bed
KP
8183 int res;
8184
ed4ddb6d 8185 if (!ipaddr)
71103bed
KP
8186 return -EINVAL;
8187
ed4ddb6d
KP
8188 if (version == 4) {
8189 family = AF_INET;
8190 addrsize = 4;
8191 } else if (version == 6) {
8192 family = AF_INET6;
8193 addrsize = 16;
8194 } else {
8195 return -EINVAL;
8196 }
8197
71103bed
KP
8198 if (bss->br_ifindex == 0) {
8199 wpa_printf(MSG_DEBUG,
8200 "nl80211: bridge must be set to delete an ip neigh");
8201 return -1;
8202 }
8203
8204 if (!drv->rtnl_sk) {
8205 wpa_printf(MSG_DEBUG,
8206 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8207 return -1;
8208 }
8209
8210 rn = rtnl_neigh_alloc();
8211 if (rn == NULL)
8212 return -ENOMEM;
8213
8214 /* set the destination ip address for neigh */
ed4ddb6d 8215 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
71103bed
KP
8216 if (nl_ipaddr == NULL) {
8217 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8218 res = -ENOMEM;
8219 goto errout;
8220 }
8221 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8222 if (res) {
8223 wpa_printf(MSG_DEBUG,
8224 "nl80211: neigh set destination addr failed");
8225 goto errout;
8226 }
8227
8228 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8229
8230 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
8231 if (res) {
8232 wpa_printf(MSG_DEBUG,
8233 "nl80211: Deleting bridge ip neigh failed: %s",
8234 strerror(errno));
8235 }
8236errout:
8237 if (nl_ipaddr)
8238 nl_addr_put(nl_ipaddr);
8239 if (rn)
8240 rtnl_neigh_put(rn);
8241 return res;
8242#else /* CONFIG_LIBNL3_ROUTE */
8243 return -1;
8244#endif /* CONFIG_LIBNL3_ROUTE */
8245}
8246
8247
73d2294f
KP
8248static int linux_write_system_file(const char *path, unsigned int val)
8249{
8250 char buf[50];
8251 int fd, len;
8252
8253 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
a9aaacbb 8254 if (os_snprintf_error(sizeof(buf), len))
73d2294f
KP
8255 return -1;
8256
8257 fd = open(path, O_WRONLY);
8258 if (fd < 0)
8259 return -1;
8260
8261 if (write(fd, buf, len) < 0) {
8262 wpa_printf(MSG_DEBUG,
8263 "nl80211: Failed to write Linux system file: %s with the value of %d",
8264 path, val);
8265 close(fd);
8266 return -1;
8267 }
8268 close(fd);
8269
8270 return 0;
8271}
8272
8273
8274static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
8275{
8276 switch (attr) {
8277 case DRV_BR_PORT_ATTR_PROXYARP:
bea8d9a3 8278 return "proxyarp_wifi";
73d2294f
KP
8279 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
8280 return "hairpin_mode";
8281 }
8282
8283 return NULL;
8284}
8285
8286
8287static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
8288 unsigned int val)
8289{
8290 struct i802_bss *bss = priv;
8291 char path[128];
8292 const char *attr_txt;
8293
8294 attr_txt = drv_br_port_attr_str(attr);
8295 if (attr_txt == NULL)
8296 return -EINVAL;
8297
8298 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
8299 bss->ifname, attr_txt);
8300
8301 if (linux_write_system_file(path, val))
8302 return -1;
8303
8304 return 0;
8305}
8306
8307
7565752d
KP
8308static const char * drv_br_net_param_str(enum drv_br_net_param param)
8309{
8310 switch (param) {
8311 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8312 return "arp_accept";
60eb9e17
JM
8313 default:
8314 return NULL;
7565752d 8315 }
7565752d
KP
8316}
8317
8318
8319static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
8320 unsigned int val)
8321{
8322 struct i802_bss *bss = priv;
8323 char path[128];
8324 const char *param_txt;
8325 int ip_version = 4;
8326
60eb9e17
JM
8327 if (param == DRV_BR_MULTICAST_SNOOPING) {
8328 os_snprintf(path, sizeof(path),
8329 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
8330 bss->brname);
8331 goto set_val;
8332 }
8333
7565752d
KP
8334 param_txt = drv_br_net_param_str(param);
8335 if (param_txt == NULL)
8336 return -EINVAL;
8337
8338 switch (param) {
8339 case DRV_BR_NET_PARAM_GARP_ACCEPT:
8340 ip_version = 4;
8341 break;
8342 default:
8343 return -EINVAL;
8344 }
8345
8346 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
8347 ip_version, bss->brname, param_txt);
8348
60eb9e17 8349set_val:
7565752d
KP
8350 if (linux_write_system_file(path, val))
8351 return -1;
8352
8353 return 0;
8354}
8355
8356
16689c7c
PX
8357static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
8358{
8359 switch (hw_mode) {
8360 case HOSTAPD_MODE_IEEE80211B:
8361 return QCA_ACS_MODE_IEEE80211B;
8362 case HOSTAPD_MODE_IEEE80211G:
8363 return QCA_ACS_MODE_IEEE80211G;
8364 case HOSTAPD_MODE_IEEE80211A:
8365 return QCA_ACS_MODE_IEEE80211A;
8366 case HOSTAPD_MODE_IEEE80211AD:
8367 return QCA_ACS_MODE_IEEE80211AD;
3784c058
PX
8368 case HOSTAPD_MODE_IEEE80211ANY:
8369 return QCA_ACS_MODE_IEEE80211ANY;
16689c7c
PX
8370 default:
8371 return -1;
8372 }
8373}
8374
8375
8376static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
8377{
8378 struct i802_bss *bss = priv;
8379 struct wpa_driver_nl80211_data *drv = bss->drv;
8380 struct nl_msg *msg;
8381 struct nlattr *data;
a862e4a3 8382 int ret;
16689c7c
PX
8383 int mode;
8384
8385 mode = hw_mode_to_qca_acs(params->hw_mode);
8386 if (mode < 0)
8387 return -1;
8388
9725b784 8389 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
a862e4a3
JM
8390 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8391 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8392 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
8393 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8394 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
8395 (params->ht_enabled &&
8396 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
8397 (params->ht40_enabled &&
857d9422
MM
8398 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
8399 (params->vht_enabled &&
8400 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
8401 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
8402 params->ch_width) ||
8403 (params->ch_list_len &&
8404 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
8405 params->ch_list))) {
a862e4a3
JM
8406 nlmsg_free(msg);
8407 return -ENOBUFS;
8408 }
16689c7c
PX
8409 nla_nest_end(msg, data);
8410
857d9422
MM
8411 wpa_printf(MSG_DEBUG,
8412 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
8413 params->hw_mode, params->ht_enabled, params->ht40_enabled,
8414 params->vht_enabled, params->ch_width, params->ch_list_len);
8415
16689c7c 8416 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
16689c7c
PX
8417 if (ret) {
8418 wpa_printf(MSG_DEBUG,
8419 "nl80211: Failed to invoke driver ACS function: %s",
8420 strerror(errno));
8421 }
16689c7c
PX
8422 return ret;
8423}
8424
8425
3f5285e8
JM
8426const struct wpa_driver_ops wpa_driver_nl80211_ops = {
8427 .name = "nl80211",
8428 .desc = "Linux nl80211/cfg80211",
8429 .get_bssid = wpa_driver_nl80211_get_bssid,
8430 .get_ssid = wpa_driver_nl80211_get_ssid,
9ebce9c5
JM
8431 .set_key = driver_nl80211_set_key,
8432 .scan2 = driver_nl80211_scan2,
d21c63b9
LC
8433 .sched_scan = wpa_driver_nl80211_sched_scan,
8434 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
3f5285e8 8435 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
9ebce9c5
JM
8436 .deauthenticate = driver_nl80211_deauthenticate,
8437 .authenticate = driver_nl80211_authenticate,
3f5285e8 8438 .associate = wpa_driver_nl80211_associate,
f2ed8023
JM
8439 .global_init = nl80211_global_init,
8440 .global_deinit = nl80211_global_deinit,
8441 .init2 = wpa_driver_nl80211_init,
9ebce9c5 8442 .deinit = driver_nl80211_deinit,
3f5285e8
JM
8443 .get_capa = wpa_driver_nl80211_get_capa,
8444 .set_operstate = wpa_driver_nl80211_set_operstate,
01652550 8445 .set_supp_port = wpa_driver_nl80211_set_supp_port,
6d158490 8446 .set_country = wpa_driver_nl80211_set_country,
f0793bf1 8447 .get_country = wpa_driver_nl80211_get_country,
19c3b566 8448 .set_ap = wpa_driver_nl80211_set_ap,
3c4ca363 8449 .set_acl = wpa_driver_nl80211_set_acl,
22a7c9d7 8450 .if_add = wpa_driver_nl80211_if_add,
9ebce9c5
JM
8451 .if_remove = driver_nl80211_if_remove,
8452 .send_mlme = driver_nl80211_send_mlme,
0fafeb54 8453 .get_hw_feature_data = nl80211_get_hw_feature_data,
0f4e8b4f 8454 .sta_add = wpa_driver_nl80211_sta_add,
9ebce9c5 8455 .sta_remove = driver_nl80211_sta_remove,
db149ac9 8456 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
a8d6ffa4 8457 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
c5121837 8458 .hapd_init = i802_init,
c5121837 8459 .hapd_deinit = i802_deinit,
f7b3920c 8460 .set_wds_sta = i802_set_wds_sta,
c5121837
JM
8461 .get_seqnum = i802_get_seqnum,
8462 .flush = i802_flush,
c5121837
JM
8463 .get_inact_sec = i802_get_inact_sec,
8464 .sta_clear_stats = i802_sta_clear_stats,
c5121837
JM
8465 .set_rts = i802_set_rts,
8466 .set_frag = i802_set_frag,
c5121837 8467 .set_tx_queue_params = i802_set_tx_queue_params,
9ebce9c5 8468 .set_sta_vlan = driver_nl80211_set_sta_vlan,
ee7ab173
JB
8469 .sta_deauth = i802_sta_deauth,
8470 .sta_disassoc = i802_sta_disassoc,
9ebce9c5 8471 .read_sta_data = driver_nl80211_read_sta_data,
e3802622 8472 .set_freq = i802_set_freq,
9ebce9c5 8473 .send_action = driver_nl80211_send_action,
5dfca53f 8474 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
55777702
JM
8475 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
8476 .cancel_remain_on_channel =
8477 wpa_driver_nl80211_cancel_remain_on_channel,
9ebce9c5 8478 .probe_req_report = driver_nl80211_probe_req_report,
af473088 8479 .deinit_ap = wpa_driver_nl80211_deinit_ap,
3c29244e 8480 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
207ef3fb 8481 .resume = wpa_driver_nl80211_resume,
b625473c 8482 .signal_monitor = nl80211_signal_monitor,
1c5c7273 8483 .signal_poll = nl80211_signal_poll,
b91ab76e 8484 .send_frame = nl80211_send_frame,
c55f774d 8485 .set_param = nl80211_set_param,
6859f1cb 8486 .get_radio_name = nl80211_get_radio_name,
a6efc65d
JM
8487 .add_pmkid = nl80211_add_pmkid,
8488 .remove_pmkid = nl80211_remove_pmkid,
8489 .flush_pmkid = nl80211_flush_pmkid,
b14a210c 8490 .set_rekey_info = nl80211_set_rekey_info,
bcf24348 8491 .poll_client = nl80211_poll_client,
29f338af 8492 .set_p2p_powersave = nl80211_set_p2p_powersave,
f90e9c1c 8493 .start_dfs_cac = nl80211_start_radar_detection,
695c7038 8494 .stop_ap = wpa_driver_nl80211_stop_ap,
03ea1786
AN
8495#ifdef CONFIG_TDLS
8496 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
8497 .tdls_oper = nl80211_tdls_oper,
72b2605f
AN
8498 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
8499 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
03ea1786 8500#endif /* CONFIG_TDLS */
6a1ce395 8501 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
597b94f5 8502 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
0185007c 8503 .get_survey = wpa_driver_nl80211_get_survey,
a771c07d 8504 .status = wpa_driver_nl80211_status,
1c4ffa87 8505 .switch_channel = nl80211_switch_channel,
0de38036
JM
8506#ifdef ANDROID_P2P
8507 .set_noa = wpa_driver_set_p2p_noa,
8508 .get_noa = wpa_driver_get_p2p_noa,
8509 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
8510#endif /* ANDROID_P2P */
5e2c3490
JM
8511#ifdef ANDROID
8512 .driver_cmd = wpa_driver_nl80211_driver_cmd,
8513#endif /* ANDROID */
adef8948 8514 .vendor_cmd = nl80211_vendor_cmd,
049105b4 8515 .set_qos_map = nl80211_set_qos_map,
e4fa8b12 8516 .set_wowlan = nl80211_set_wowlan,
0800f9ee 8517 .roaming = nl80211_roaming,
fee354c7 8518 .set_mac_addr = nl80211_set_mac_addr,
6c1664f6
BC
8519#ifdef CONFIG_MESH
8520 .init_mesh = wpa_driver_nl80211_init_mesh,
8521 .join_mesh = wpa_driver_nl80211_join_mesh,
8522 .leave_mesh = wpa_driver_nl80211_leave_mesh,
8523#endif /* CONFIG_MESH */
71103bed
KP
8524 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
8525 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
73d2294f 8526 .br_port_set_attr = wpa_driver_br_port_set_attr,
7565752d 8527 .br_set_net_param = wpa_driver_br_set_net_param,
dfa87878
MB
8528 .add_tx_ts = nl80211_add_ts,
8529 .del_tx_ts = nl80211_del_ts,
16689c7c 8530 .do_acs = wpa_driver_do_acs,
3f5285e8 8531};