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