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