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