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