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