blob: e9107b3bba8e9ef9e9bbae95a18ff748332b2fc2 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Driver interaction with Linux nl80211/cfg80211
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2002-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 * Copyright (c) 2003-2004, Instant802 Networks, Inc.
5 * Copyright (c) 2005-2006, Devicescape Software, Inc.
6 * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright (c) 2009-2010, Atheros Communications
8 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08009 * This software may be distributed under the terms of the BSD license.
10 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011 */
12
13#include "includes.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include <sys/types.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include <fcntl.h>
16#include <net/if.h>
17#include <netlink/genl/genl.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include <netlink/genl/ctrl.h>
Dmitry Shmidt661b4f72014-09-29 14:58:27 -070019#ifdef CONFIG_LIBNL3_ROUTE
20#include <netlink/route/neighbour.h>
21#endif /* CONFIG_LIBNL3_ROUTE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include <linux/rtnetlink.h>
23#include <netpacket/packet.h>
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080024#include <linux/errqueue.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025
26#include "common.h"
27#include "eloop.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080028#include "common/qca-vendor.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070029#include "common/qca-vendor-attr.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080031#include "common/ieee802_11_common.h"
Paul Stewart092955c2017-02-06 09:13:09 -080032#include "common/wpa_common.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080033#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "netlink.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080035#include "linux_defines.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036#include "linux_ioctl.h"
37#include "radiotap.h"
38#include "radiotap_iter.h"
39#include "rfkill.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080040#include "driver_nl80211.h"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080041
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080042
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080043#ifndef CONFIG_LIBNL20
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044/*
45 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
46 * but when you free a socket again it will mess up its bitmap and
47 * and use the wrong number the next time it needs a socket ID.
48 * Therefore, we wrap the handle alloc/destroy and add our own pid
49 * accounting.
50 */
51static uint32_t port_bitmap[32] = { 0 };
52
53static struct nl_handle *nl80211_handle_alloc(void *cb)
54{
55 struct nl_handle *handle;
56 uint32_t pid = getpid() & 0x3FFFFF;
57 int i;
58
59 handle = nl_handle_alloc_cb(cb);
60
61 for (i = 0; i < 1024; i++) {
62 if (port_bitmap[i / 32] & (1 << (i % 32)))
63 continue;
64 port_bitmap[i / 32] |= 1 << (i % 32);
65 pid += i << 22;
66 break;
67 }
68
69 nl_socket_set_local_port(handle, pid);
70
71 return handle;
72}
73
74static void nl80211_handle_destroy(struct nl_handle *handle)
75{
76 uint32_t port = nl_socket_get_local_port(handle);
77
78 port >>= 22;
79 port_bitmap[port / 32] &= ~(1 << (port % 32));
80
81 nl_handle_destroy(handle);
82}
83#endif /* CONFIG_LIBNL20 */
84
85
Dmitry Shmidt54605472013-11-08 11:10:19 -080086#ifdef ANDROID
87/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
Dmitry Shmidt54605472013-11-08 11:10:19 -080088#undef nl_socket_set_nonblocking
89#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080090
Dmitry Shmidt54605472013-11-08 11:10:19 -080091#endif /* ANDROID */
92
93
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080094static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
95{
96 struct nl_handle *handle;
97
98 handle = nl80211_handle_alloc(cb);
99 if (handle == NULL) {
100 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
101 "callbacks (%s)", dbg);
102 return NULL;
103 }
104
105 if (genl_connect(handle)) {
106 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
107 "netlink (%s)", dbg);
108 nl80211_handle_destroy(handle);
109 return NULL;
110 }
111
112 return handle;
113}
114
115
116static void nl_destroy_handles(struct nl_handle **handle)
117{
118 if (*handle == NULL)
119 return;
120 nl80211_handle_destroy(*handle);
121 *handle = NULL;
122}
123
124
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700125#if __WORDSIZE == 64
126#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
127#else
128#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
129#endif
130
131static void nl80211_register_eloop_read(struct nl_handle **handle,
132 eloop_sock_handler handler,
133 void *eloop_data)
134{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800135#ifdef CONFIG_LIBNL20
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 }
149#endif /* CONFIG_LIBNL20 */
150
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700151 nl_socket_set_nonblocking(*handle);
152 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
153 eloop_data, *handle);
154 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
155}
156
157
158static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
159{
160 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
161 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
162 nl_destroy_handles(handle);
163}
164
165
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800166static void nl80211_global_deinit(void *priv);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800167static void nl80211_check_global(struct nl80211_global *global);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800168
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800169static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700170static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
171 struct hostapd_freq_params *freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800174wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800175 const u8 *set_addr, int first,
176 const char *driver_params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800177static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800179 const u8 *buf, size_t buf_len, u64 *cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800180 int no_cck, int no_ack, int offchanok,
181 const u16 *csa_offs, size_t csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800182static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
183 int report);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800185#define IFIDX_ANY -1
186
187static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
188 int ifidx_reason);
189static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
190 int ifidx_reason);
191static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
192 int ifidx_reason);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700193
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700194static int nl80211_set_channel(struct i802_bss *bss,
195 struct hostapd_freq_params *freq, int set_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
197 int ifindex, int disabled);
198
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800199static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
200 int reset_mode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800201
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700202static int i802_set_iface_flags(struct i802_bss *bss, int up);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800203static int nl80211_set_param(void *priv, const char *param);
Dmitry Shmidtd13095b2016-08-22 14:02:19 -0700204#ifdef CONFIG_MESH
205static int nl80211_put_mesh_config(struct nl_msg *msg,
206 struct wpa_driver_mesh_bss_params *params);
207#endif /* CONFIG_MESH */
Dmitry Shmidt29333592017-01-09 12:27:11 -0800208static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
209 int reason);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700210
211
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800212/* Converts nl80211_chan_width to a common format */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800213enum chan_width convert2width(int width)
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800214{
215 switch (width) {
216 case NL80211_CHAN_WIDTH_20_NOHT:
217 return CHAN_WIDTH_20_NOHT;
218 case NL80211_CHAN_WIDTH_20:
219 return CHAN_WIDTH_20;
220 case NL80211_CHAN_WIDTH_40:
221 return CHAN_WIDTH_40;
222 case NL80211_CHAN_WIDTH_80:
223 return CHAN_WIDTH_80;
224 case NL80211_CHAN_WIDTH_80P80:
225 return CHAN_WIDTH_80P80;
226 case NL80211_CHAN_WIDTH_160:
227 return CHAN_WIDTH_160;
228 }
229 return CHAN_WIDTH_UNKNOWN;
230}
231
232
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800233int is_ap_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800234{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700235 return nlmode == NL80211_IFTYPE_AP ||
236 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800237}
238
239
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800240int is_sta_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800241{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700242 return nlmode == NL80211_IFTYPE_STATION ||
243 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800244}
245
246
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700247static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800248{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700249 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
250 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800251}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700252
253
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800254struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
255 int ifindex)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700256{
257 struct i802_bss *bss;
258
259 for (bss = drv->first_bss; bss; bss = bss->next) {
260 if (bss->ifindex == ifindex)
261 return bss;
262 }
263
264 return NULL;
265}
266
267
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800268static int is_mesh_interface(enum nl80211_iftype nlmode)
269{
270 return nlmode == NL80211_IFTYPE_MESH_POINT;
271}
272
273
274void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700275{
276 if (drv->associated)
277 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
278 drv->associated = 0;
279 os_memset(drv->bssid, 0, ETH_ALEN);
280}
281
282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283/* nl80211 code */
284static int ack_handler(struct nl_msg *msg, void *arg)
285{
286 int *err = arg;
287 *err = 0;
288 return NL_STOP;
289}
290
291static int finish_handler(struct nl_msg *msg, void *arg)
292{
293 int *ret = arg;
294 *ret = 0;
295 return NL_SKIP;
296}
297
298static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
299 void *arg)
300{
301 int *ret = arg;
302 *ret = err->error;
303 return NL_SKIP;
304}
305
306
307static int no_seq_check(struct nl_msg *msg, void *arg)
308{
309 return NL_OK;
310}
311
312
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800313static void nl80211_nlmsg_clear(struct nl_msg *msg)
314{
315 /*
316 * Clear nlmsg data, e.g., to make sure key material is not left in
317 * heap memory for unnecessarily long time.
318 */
319 if (msg) {
320 struct nlmsghdr *hdr = nlmsg_hdr(msg);
321 void *data = nlmsg_data(hdr);
322 /*
323 * This would use nlmsg_datalen() or the older nlmsg_len() if
324 * only libnl were to maintain a stable API.. Neither will work
325 * with all released versions, so just calculate the length
326 * here.
327 */
328 int len = hdr->nlmsg_len - NLMSG_HDRLEN;
329
330 os_memset(data, 0, len);
331 }
332}
333
334
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800335static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336 struct nl_handle *nl_handle, struct nl_msg *msg,
337 int (*valid_handler)(struct nl_msg *, void *),
338 void *valid_data)
339{
340 struct nl_cb *cb;
341 int err = -ENOMEM;
342
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800343 if (!msg)
344 return -ENOMEM;
345
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800346 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 if (!cb)
348 goto out;
349
350 err = nl_send_auto_complete(nl_handle, msg);
351 if (err < 0)
352 goto out;
353
354 err = 1;
355
356 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
357 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
358 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
359
360 if (valid_handler)
361 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
362 valid_handler, valid_data);
363
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700364 while (err > 0) {
365 int res = nl_recvmsgs(nl_handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700366 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700367 wpa_printf(MSG_INFO,
368 "nl80211: %s->nl_recvmsgs failed: %d",
369 __func__, res);
370 }
371 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372 out:
373 nl_cb_put(cb);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800374 if (!valid_handler && valid_data == (void *) -1)
375 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376 nlmsg_free(msg);
377 return err;
378}
379
380
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800381int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
382 struct nl_msg *msg,
383 int (*valid_handler)(struct nl_msg *, void *),
384 void *valid_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800386 return send_and_recv(drv->global, drv->global->nl, msg,
387 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388}
389
390
391struct family_data {
392 const char *group;
393 int id;
394};
395
396
397static int family_handler(struct nl_msg *msg, void *arg)
398{
399 struct family_data *res = arg;
400 struct nlattr *tb[CTRL_ATTR_MAX + 1];
401 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
402 struct nlattr *mcgrp;
403 int i;
404
405 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
406 genlmsg_attrlen(gnlh, 0), NULL);
407 if (!tb[CTRL_ATTR_MCAST_GROUPS])
408 return NL_SKIP;
409
410 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
411 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
412 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
413 nla_len(mcgrp), NULL);
414 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
415 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
416 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
417 res->group,
418 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
419 continue;
420 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
421 break;
422 };
423
424 return NL_SKIP;
425}
426
427
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800428static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 const char *family, const char *group)
430{
431 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800432 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433 struct family_data res = { group, -ENOENT };
434
435 msg = nlmsg_alloc();
436 if (!msg)
437 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800438 if (!genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
439 0, 0, CTRL_CMD_GETFAMILY, 0) ||
440 nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
441 nlmsg_free(msg);
442 return -1;
443 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800445 ret = send_and_recv(global, global->nl, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700446 if (ret == 0)
447 ret = res.id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448 return ret;
449}
450
451
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800452void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
453 struct nl_msg *msg, int flags, uint8_t cmd)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800454{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700455 if (TEST_FAIL())
456 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800457 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
458 0, flags, cmd, 0);
459}
460
461
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800462static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
463{
464 if (bss->wdev_id_set)
465 return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
466 return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
467}
468
469
470struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
471{
472 struct nl_msg *msg;
473
474 msg = nlmsg_alloc();
475 if (!msg)
476 return NULL;
477
478 if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
479 nl80211_set_iface_id(msg, bss) < 0) {
480 nlmsg_free(msg);
481 return NULL;
482 }
483
484 return msg;
485}
486
487
488static struct nl_msg *
489nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
490 int flags, uint8_t cmd)
491{
492 struct nl_msg *msg;
493
494 msg = nlmsg_alloc();
495 if (!msg)
496 return NULL;
497
498 if (!nl80211_cmd(drv, msg, flags, cmd) ||
499 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
500 nlmsg_free(msg);
501 return NULL;
502 }
503
504 return msg;
505}
506
507
508struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
509 uint8_t cmd)
510{
511 return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
512}
513
514
515struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
516{
517 return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
518}
519
520
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800521struct wiphy_idx_data {
522 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700523 enum nl80211_iftype nlmode;
524 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800525};
526
527
528static int netdev_info_handler(struct nl_msg *msg, void *arg)
529{
530 struct nlattr *tb[NL80211_ATTR_MAX + 1];
531 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
532 struct wiphy_idx_data *info = arg;
533
534 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
535 genlmsg_attrlen(gnlh, 0), NULL);
536
537 if (tb[NL80211_ATTR_WIPHY])
538 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
539
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700540 if (tb[NL80211_ATTR_IFTYPE])
541 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
542
543 if (tb[NL80211_ATTR_MAC] && info->macaddr)
544 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
545 ETH_ALEN);
546
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800547 return NL_SKIP;
548}
549
550
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800551int nl80211_get_wiphy_index(struct i802_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800552{
553 struct nl_msg *msg;
554 struct wiphy_idx_data data = {
555 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700556 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800557 };
558
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800559 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
560 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800561
562 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
563 return data.wiphy_idx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800564 return -1;
565}
566
567
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700568static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
569{
570 struct nl_msg *msg;
571 struct wiphy_idx_data data = {
572 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
573 .macaddr = NULL,
574 };
575
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800576 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
577 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700578
579 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
580 return data.nlmode;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700581 return NL80211_IFTYPE_UNSPECIFIED;
582}
583
584
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700585static int nl80211_get_macaddr(struct i802_bss *bss)
586{
587 struct nl_msg *msg;
588 struct wiphy_idx_data data = {
589 .macaddr = bss->addr,
590 };
591
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800592 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
593 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700594
595 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700596}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700597
598
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800599static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
600 struct nl80211_wiphy_data *w)
601{
602 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800603 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800604
605 msg = nlmsg_alloc();
606 if (!msg)
607 return -1;
608
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800609 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
610 nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
611 nlmsg_free(msg);
612 return -1;
613 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800614
615 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800616 if (ret) {
617 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
618 "failed: ret=%d (%s)",
619 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800620 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800621 return ret;
622}
623
624
625static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
626{
627 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700628 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800629
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800630 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800631
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700632 res = nl_recvmsgs(handle, w->nl_cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700633 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700634 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
635 __func__, res);
636 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800637}
638
639
640static int process_beacon_event(struct nl_msg *msg, void *arg)
641{
642 struct nl80211_wiphy_data *w = arg;
643 struct wpa_driver_nl80211_data *drv;
644 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
645 struct nlattr *tb[NL80211_ATTR_MAX + 1];
646 union wpa_event_data event;
647
648 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
649 genlmsg_attrlen(gnlh, 0), NULL);
650
651 if (gnlh->cmd != NL80211_CMD_FRAME) {
652 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
653 gnlh->cmd);
654 return NL_SKIP;
655 }
656
657 if (!tb[NL80211_ATTR_FRAME])
658 return NL_SKIP;
659
660 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
661 wiphy_list) {
662 os_memset(&event, 0, sizeof(event));
663 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
664 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
665 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
666 }
667
668 return NL_SKIP;
669}
670
671
672static struct nl80211_wiphy_data *
673nl80211_get_wiphy_data_ap(struct i802_bss *bss)
674{
675 static DEFINE_DL_LIST(nl80211_wiphys);
676 struct nl80211_wiphy_data *w;
677 int wiphy_idx, found = 0;
678 struct i802_bss *tmp_bss;
Paul Stewart092955c2017-02-06 09:13:09 -0800679 u8 channel;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800680
681 if (bss->wiphy_data != NULL)
682 return bss->wiphy_data;
683
684 wiphy_idx = nl80211_get_wiphy_index(bss);
685
686 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
687 if (w->wiphy_idx == wiphy_idx)
688 goto add;
689 }
690
691 /* alloc new one */
692 w = os_zalloc(sizeof(*w));
693 if (w == NULL)
694 return NULL;
695 w->wiphy_idx = wiphy_idx;
696 dl_list_init(&w->bsss);
697 dl_list_init(&w->drvs);
698
Paul Stewart092955c2017-02-06 09:13:09 -0800699 /* Beacon frames not supported in IEEE 802.11ad */
700 if (ieee80211_freq_to_chan(bss->freq, &channel) !=
701 HOSTAPD_MODE_IEEE80211AD) {
702 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
703 if (!w->nl_cb) {
704 os_free(w);
705 return NULL;
706 }
707 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
708 no_seq_check, NULL);
709 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
710 process_beacon_event, w);
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000711
Paul Stewart092955c2017-02-06 09:13:09 -0800712 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
713 "wiphy beacons");
714 if (w->nl_beacons == NULL) {
715 os_free(w);
716 return NULL;
717 }
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000718
Paul Stewart092955c2017-02-06 09:13:09 -0800719 if (nl80211_register_beacons(bss->drv, w)) {
720 nl_destroy_handles(&w->nl_beacons);
721 os_free(w);
722 return NULL;
723 }
Rebecca Silberstein055a67c2017-02-01 23:05:56 +0000724
Paul Stewart092955c2017-02-06 09:13:09 -0800725 nl80211_register_eloop_read(&w->nl_beacons,
726 nl80211_recv_beacons, w);
727 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800728
729 dl_list_add(&nl80211_wiphys, &w->list);
730
731add:
732 /* drv entry for this bss already there? */
733 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
734 if (tmp_bss->drv == bss->drv) {
735 found = 1;
736 break;
737 }
738 }
739 /* if not add it */
740 if (!found)
741 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
742
743 dl_list_add(&w->bsss, &bss->wiphy_list);
744 bss->wiphy_data = w;
745 return w;
746}
747
748
749static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
750{
751 struct nl80211_wiphy_data *w = bss->wiphy_data;
752 struct i802_bss *tmp_bss;
753 int found = 0;
754
755 if (w == NULL)
756 return;
757 bss->wiphy_data = NULL;
758 dl_list_del(&bss->wiphy_list);
759
760 /* still any for this drv present? */
761 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
762 if (tmp_bss->drv == bss->drv) {
763 found = 1;
764 break;
765 }
766 }
767 /* if not remove it */
768 if (!found)
769 dl_list_del(&bss->drv->wiphy_list);
770
771 if (!dl_list_empty(&w->bsss))
772 return;
773
Paul Stewart092955c2017-02-06 09:13:09 -0800774 if (w->nl_beacons)
775 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800776
777 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800778 dl_list_del(&w->list);
779 os_free(w);
780}
781
782
Dmitry Shmidte4663042016-04-04 10:07:49 -0700783static unsigned int nl80211_get_ifindex(void *priv)
784{
785 struct i802_bss *bss = priv;
786 struct wpa_driver_nl80211_data *drv = bss->drv;
787
788 return drv->ifindex;
789}
790
791
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
793{
794 struct i802_bss *bss = priv;
795 struct wpa_driver_nl80211_data *drv = bss->drv;
796 if (!drv->associated)
797 return -1;
798 os_memcpy(bssid, drv->bssid, ETH_ALEN);
799 return 0;
800}
801
802
803static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
804{
805 struct i802_bss *bss = priv;
806 struct wpa_driver_nl80211_data *drv = bss->drv;
807 if (!drv->associated)
808 return -1;
809 os_memcpy(ssid, drv->ssid, drv->ssid_len);
810 return drv->ssid_len;
811}
812
813
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800814static void wpa_driver_nl80211_event_newlink(
Dmitry Shmidte4663042016-04-04 10:07:49 -0700815 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
816 int ifindex, const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817{
818 union wpa_event_data event;
819
Dmitry Shmidte4663042016-04-04 10:07:49 -0700820 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800821 if (if_nametoindex(drv->first_bss->ifname) == 0) {
822 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
823 drv->first_bss->ifname);
824 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700825 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800826 if (!drv->if_removed)
827 return;
828 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
829 drv->first_bss->ifname);
830 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831 }
832
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800833 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -0700834 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800835 os_strlcpy(event.interface_status.ifname, ifname,
836 sizeof(event.interface_status.ifname));
837 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700838 if (drv)
839 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
840 else
841 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
842 &event);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800843}
844
845
846static void wpa_driver_nl80211_event_dellink(
Dmitry Shmidte4663042016-04-04 10:07:49 -0700847 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
848 int ifindex, const char *ifname)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800849{
850 union wpa_event_data event;
851
Dmitry Shmidte4663042016-04-04 10:07:49 -0700852 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800853 if (drv->if_removed) {
854 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
855 ifname);
856 return;
857 }
858 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
859 ifname);
860 drv->if_removed = 1;
861 } else {
862 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
863 ifname);
864 }
865
866 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -0700867 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800868 os_strlcpy(event.interface_status.ifname, ifname,
869 sizeof(event.interface_status.ifname));
870 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700871 if (drv)
872 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
873 else
874 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
875 &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700876}
877
878
879static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
880 u8 *buf, size_t len)
881{
882 int attrlen, rta_len;
883 struct rtattr *attr;
884
885 attrlen = len;
886 attr = (struct rtattr *) buf;
887
888 rta_len = RTA_ALIGN(sizeof(struct rtattr));
889 while (RTA_OK(attr, attrlen)) {
890 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800891 if (os_strcmp(((char *) attr) + rta_len,
892 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 return 1;
894 else
895 break;
896 }
897 attr = RTA_NEXT(attr, attrlen);
898 }
899
900 return 0;
901}
902
903
904static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
905 int ifindex, u8 *buf, size_t len)
906{
907 if (drv->ifindex == ifindex)
908 return 1;
909
910 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800911 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
913 "interface");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800914 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 return 1;
916 }
917
918 return 0;
919}
920
921
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800922static struct wpa_driver_nl80211_data *
923nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
924{
925 struct wpa_driver_nl80211_data *drv;
926 dl_list_for_each(drv, &global->interfaces,
927 struct wpa_driver_nl80211_data, list) {
928 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800929 have_ifidx(drv, idx, IFIDX_ANY))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800930 return drv;
931 }
932 return NULL;
933}
934
935
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
937 struct ifinfomsg *ifi,
938 u8 *buf, size_t len)
939{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800940 struct nl80211_global *global = ctx;
941 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800942 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 struct rtattr *attr;
944 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800945 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800946 char ifname[IFNAMSIZ + 1];
947 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800949 extra[0] = '\0';
950 pos = extra;
951 end = pos + sizeof(extra);
952 ifname[0] = '\0';
953
954 attrlen = len;
955 attr = (struct rtattr *) buf;
956 while (RTA_OK(attr, attrlen)) {
957 switch (attr->rta_type) {
958 case IFLA_IFNAME:
959 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
960 break;
961 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
962 ifname[RTA_PAYLOAD(attr)] = '\0';
963 break;
964 case IFLA_MASTER:
965 brid = nla_get_u32((struct nlattr *) attr);
966 pos += os_snprintf(pos, end - pos, " master=%u", brid);
967 break;
968 case IFLA_WIRELESS:
969 pos += os_snprintf(pos, end - pos, " wext");
970 break;
971 case IFLA_OPERSTATE:
972 pos += os_snprintf(pos, end - pos, " operstate=%u",
973 nla_get_u32((struct nlattr *) attr));
974 break;
975 case IFLA_LINKMODE:
976 pos += os_snprintf(pos, end - pos, " linkmode=%u",
977 nla_get_u32((struct nlattr *) attr));
978 break;
979 }
980 attr = RTA_NEXT(attr, attrlen);
981 }
982 extra[sizeof(extra) - 1] = '\0';
983
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700984 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
985 ifi->ifi_index, ifname, extra, ifi->ifi_family,
986 ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
988 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
989 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
990 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
991
Dmitry Shmidte4663042016-04-04 10:07:49 -0700992 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
993 if (!drv)
994 goto event_newlink;
995
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700996 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800997 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800998 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800999 linux_iface_up(drv->global->ioctl_sock, namebuf) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001000 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1001 "event since interface %s is up", namebuf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001002 drv->ignore_if_down_event = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001003 return;
1004 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001005 wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
1006 namebuf, ifname);
1007 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
1008 wpa_printf(MSG_DEBUG,
1009 "nl80211: Not the main interface (%s) - do not indicate interface down",
1010 drv->first_bss->ifname);
1011 } else if (drv->ignore_if_down_event) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001012 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1013 "event generated by mode change");
1014 drv->ignore_if_down_event = 0;
1015 } else {
1016 drv->if_disabled = 1;
1017 wpa_supplicant_event(drv->ctx,
1018 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001019
1020 /*
1021 * Try to get drv again, since it may be removed as
1022 * part of the EVENT_INTERFACE_DISABLED handling for
1023 * dynamic interfaces
1024 */
1025 drv = nl80211_find_drv(global, ifi->ifi_index,
1026 buf, len);
1027 if (!drv)
1028 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001029 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 }
1031
1032 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001033 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001034 linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001035 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1036 "event since interface %s is down",
1037 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001038 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001039 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1040 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001041 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001042 } else if (drv->if_removed) {
1043 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1044 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001045 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001046 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001047 struct i802_bss *bss;
1048 u8 addr[ETH_ALEN];
1049
1050 /* Re-read MAC address as it may have changed */
1051 bss = get_bss_ifindex(drv, ifi->ifi_index);
1052 if (bss &&
1053 linux_get_ifhwaddr(drv->global->ioctl_sock,
1054 bss->ifname, addr) < 0) {
1055 wpa_printf(MSG_DEBUG,
1056 "nl80211: %s: failed to re-read MAC address",
1057 bss->ifname);
1058 } else if (bss &&
1059 os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
1060 wpa_printf(MSG_DEBUG,
1061 "nl80211: Own MAC address on ifindex %d (%s) changed from "
1062 MACSTR " to " MACSTR,
1063 ifi->ifi_index, bss->ifname,
1064 MAC2STR(bss->addr),
1065 MAC2STR(addr));
1066 os_memcpy(bss->addr, addr, ETH_ALEN);
1067 }
1068
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001069 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1070 drv->if_disabled = 0;
1071 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1072 NULL);
1073 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 }
1075
1076 /*
1077 * Some drivers send the association event before the operup event--in
1078 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1079 * fails. This will hit us when wpa_supplicant does not need to do
1080 * IEEE 802.1X authentication
1081 */
1082 if (drv->operstate == 1 &&
1083 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001084 !(ifi->ifi_flags & IFF_RUNNING)) {
1085 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001086 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 }
1089
Dmitry Shmidte4663042016-04-04 10:07:49 -07001090event_newlink:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001091 if (ifname[0])
Dmitry Shmidte4663042016-04-04 10:07:49 -07001092 wpa_driver_nl80211_event_newlink(global, drv, ifi->ifi_index,
1093 ifname);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001094
Dmitry Shmidte4663042016-04-04 10:07:49 -07001095 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001096 struct i802_bss *bss;
1097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098 /* device has been added to bridge */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001099 if (!if_indextoname(brid, namebuf)) {
1100 wpa_printf(MSG_DEBUG,
1101 "nl80211: Could not find bridge ifname for ifindex %u",
1102 brid);
1103 return;
1104 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1106 brid, namebuf);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001107 add_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001108
1109 for (bss = drv->first_bss; bss; bss = bss->next) {
1110 if (os_strcmp(ifname, bss->ifname) == 0) {
1111 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1112 break;
1113 }
1114 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001116}
1117
1118
1119static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1120 struct ifinfomsg *ifi,
1121 u8 *buf, size_t len)
1122{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001123 struct nl80211_global *global = ctx;
1124 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001125 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126 struct rtattr *attr;
1127 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001128 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001129 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001131 extra[0] = '\0';
1132 pos = extra;
1133 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001134 ifname[0] = '\0';
1135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 attrlen = len;
1137 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001139 switch (attr->rta_type) {
1140 case IFLA_IFNAME:
1141 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1142 break;
1143 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1144 ifname[RTA_PAYLOAD(attr)] = '\0';
1145 break;
1146 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001148 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1149 break;
1150 case IFLA_OPERSTATE:
1151 pos += os_snprintf(pos, end - pos, " operstate=%u",
1152 nla_get_u32((struct nlattr *) attr));
1153 break;
1154 case IFLA_LINKMODE:
1155 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1156 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001157 break;
1158 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159 attr = RTA_NEXT(attr, attrlen);
1160 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001161 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001163 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1164 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1165 ifi->ifi_flags,
1166 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1167 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1168 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1169 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1170
Dmitry Shmidte4663042016-04-04 10:07:49 -07001171 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001172
Dmitry Shmidte4663042016-04-04 10:07:49 -07001173 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001174 /* device has been removed from bridge */
1175 char namebuf[IFNAMSIZ];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001176
1177 if (!if_indextoname(brid, namebuf)) {
1178 wpa_printf(MSG_DEBUG,
1179 "nl80211: Could not find bridge ifname for ifindex %u",
1180 brid);
1181 } else {
1182 wpa_printf(MSG_DEBUG,
1183 "nl80211: Remove ifindex %u for bridge %s",
1184 brid, namebuf);
1185 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001186 del_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07001188
1189 if (ifi->ifi_family != AF_BRIDGE || !brid)
1190 wpa_driver_nl80211_event_dellink(global, drv, ifi->ifi_index,
1191 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192}
1193
1194
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001195struct nl80211_get_assoc_freq_arg {
1196 struct wpa_driver_nl80211_data *drv;
1197 unsigned int assoc_freq;
1198 unsigned int ibss_freq;
1199 u8 assoc_bssid[ETH_ALEN];
1200 u8 assoc_ssid[SSID_MAX_LEN];
1201 u8 assoc_ssid_len;
1202};
1203
1204static int nl80211_get_assoc_freq_handler(struct nl_msg *msg, void *arg)
1205{
1206 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1207 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1208 struct nlattr *bss[NL80211_BSS_MAX + 1];
1209 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1210 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
1211 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1212 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
1213 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1214 };
1215 struct nl80211_get_assoc_freq_arg *ctx = arg;
1216 enum nl80211_bss_status status;
1217
1218 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1219 genlmsg_attrlen(gnlh, 0), NULL);
1220 if (!tb[NL80211_ATTR_BSS] ||
1221 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1222 bss_policy) ||
1223 !bss[NL80211_BSS_STATUS])
1224 return NL_SKIP;
1225
1226 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
1227 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1228 bss[NL80211_BSS_FREQUENCY]) {
1229 ctx->assoc_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1230 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
1231 ctx->assoc_freq);
1232 }
1233 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
1234 bss[NL80211_BSS_FREQUENCY]) {
1235 ctx->ibss_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1236 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
1237 ctx->ibss_freq);
1238 }
1239 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1240 bss[NL80211_BSS_BSSID]) {
1241 os_memcpy(ctx->assoc_bssid,
1242 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
1243 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
1244 MACSTR, MAC2STR(ctx->assoc_bssid));
1245 }
1246
1247 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1248 bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
1249 const u8 *ie, *ssid;
1250 size_t ie_len;
1251
1252 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1253 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1254 ssid = get_ie(ie, ie_len, WLAN_EID_SSID);
1255 if (ssid && ssid[1] > 0 && ssid[1] <= SSID_MAX_LEN) {
1256 ctx->assoc_ssid_len = ssid[1];
1257 os_memcpy(ctx->assoc_ssid, ssid + 2, ssid[1]);
1258 }
1259 }
1260
1261 return NL_SKIP;
1262}
1263
1264
1265int nl80211_get_assoc_ssid(struct wpa_driver_nl80211_data *drv, u8 *ssid)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001266{
1267 struct nl_msg *msg;
1268 int ret;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001269 struct nl80211_get_assoc_freq_arg arg;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001270
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001271 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001272 os_memset(&arg, 0, sizeof(arg));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001273 arg.drv = drv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001274 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1275 &arg);
1276 if (ret == 0) {
1277 os_memcpy(ssid, arg.assoc_ssid, arg.assoc_ssid_len);
1278 return arg.assoc_ssid_len;
1279 }
1280 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d (%s)",
1281 ret, strerror(-ret));
1282 return ret;
1283}
1284
1285
1286unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1287{
1288 struct nl_msg *msg;
1289 int ret;
1290 struct nl80211_get_assoc_freq_arg arg;
1291
1292 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1293 os_memset(&arg, 0, sizeof(arg));
1294 arg.drv = drv;
1295 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1296 &arg);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001297 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001298 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1299 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001300 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001301 "associated BSS from scan results: %u MHz", freq);
1302 if (freq)
1303 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001304 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001305 }
1306 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1307 "(%s)", ret, strerror(-ret));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001308 return drv->assoc_freq;
1309}
1310
1311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312static int get_link_signal(struct nl_msg *msg, void *arg)
1313{
1314 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1315 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1316 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1317 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1318 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001319 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001320 [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 };
1322 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1323 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1324 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1325 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1326 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1327 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1328 };
1329 struct wpa_signal_info *sig_change = arg;
1330
1331 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1332 genlmsg_attrlen(gnlh, 0), NULL);
1333 if (!tb[NL80211_ATTR_STA_INFO] ||
1334 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1335 tb[NL80211_ATTR_STA_INFO], policy))
1336 return NL_SKIP;
1337 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1338 return NL_SKIP;
1339
1340 sig_change->current_signal =
1341 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1342
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001343 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1344 sig_change->avg_signal =
1345 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1346 else
1347 sig_change->avg_signal = 0;
1348
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001349 if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
1350 sig_change->avg_beacon_signal =
1351 (s8)
1352 nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
1353 else
1354 sig_change->avg_beacon_signal = 0;
1355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1357 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1358 sinfo[NL80211_STA_INFO_TX_BITRATE],
1359 rate_policy)) {
1360 sig_change->current_txrate = 0;
1361 } else {
1362 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1363 sig_change->current_txrate =
1364 nla_get_u16(rinfo[
1365 NL80211_RATE_INFO_BITRATE]) * 100;
1366 }
1367 }
1368 }
1369
1370 return NL_SKIP;
1371}
1372
1373
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001374int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1375 struct wpa_signal_info *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376{
1377 struct nl_msg *msg;
1378
1379 sig->current_signal = -9999;
1380 sig->current_txrate = 0;
1381
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001382 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
1383 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1384 nlmsg_free(msg);
1385 return -ENOBUFS;
1386 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387
1388 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001389}
1390
1391
1392static int get_link_noise(struct nl_msg *msg, void *arg)
1393{
1394 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1395 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1396 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1397 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1398 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1399 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1400 };
1401 struct wpa_signal_info *sig_change = arg;
1402
1403 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1404 genlmsg_attrlen(gnlh, 0), NULL);
1405
1406 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1407 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1408 return NL_SKIP;
1409 }
1410
1411 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1412 tb[NL80211_ATTR_SURVEY_INFO],
1413 survey_policy)) {
1414 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1415 "attributes!");
1416 return NL_SKIP;
1417 }
1418
1419 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1420 return NL_SKIP;
1421
1422 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1423 sig_change->frequency)
1424 return NL_SKIP;
1425
1426 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1427 return NL_SKIP;
1428
1429 sig_change->current_noise =
1430 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1431
1432 return NL_SKIP;
1433}
1434
1435
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001436int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1437 struct wpa_signal_info *sig_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438{
1439 struct nl_msg *msg;
1440
1441 sig_change->current_noise = 9999;
1442 sig_change->frequency = drv->assoc_freq;
1443
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001444 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446}
1447
1448
1449static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1450 void *handle)
1451{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001452 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001453 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001455 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001457 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001458 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001459 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1460 __func__, res);
1461 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462}
1463
1464
1465/**
1466 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1467 * @priv: driver_nl80211 private data
1468 * @alpha2_arg: country to which to switch to
1469 * Returns: 0 on success, -1 on failure
1470 *
1471 * This asks nl80211 to set the regulatory domain for given
1472 * country ISO / IEC alpha2.
1473 */
1474static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1475{
1476 struct i802_bss *bss = priv;
1477 struct wpa_driver_nl80211_data *drv = bss->drv;
1478 char alpha2[3];
1479 struct nl_msg *msg;
1480
1481 msg = nlmsg_alloc();
1482 if (!msg)
1483 return -ENOMEM;
1484
1485 alpha2[0] = alpha2_arg[0];
1486 alpha2[1] = alpha2_arg[1];
1487 alpha2[2] = '\0';
1488
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001489 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1490 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1491 nlmsg_free(msg);
1492 return -EINVAL;
1493 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1495 return -EINVAL;
1496 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001497}
1498
1499
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001500static int nl80211_get_country(struct nl_msg *msg, void *arg)
1501{
1502 char *alpha2 = arg;
1503 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1504 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1505
1506 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1507 genlmsg_attrlen(gnlh, 0), NULL);
1508 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1509 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1510 return NL_SKIP;
1511 }
1512 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1513 return NL_SKIP;
1514}
1515
1516
1517static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1518{
1519 struct i802_bss *bss = priv;
1520 struct wpa_driver_nl80211_data *drv = bss->drv;
1521 struct nl_msg *msg;
1522 int ret;
1523
1524 msg = nlmsg_alloc();
1525 if (!msg)
1526 return -ENOMEM;
1527
1528 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1529 alpha2[0] = '\0';
1530 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1531 if (!alpha2[0])
1532 ret = -1;
1533
1534 return ret;
1535}
1536
1537
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001538static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539{
1540 int ret;
1541
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001542 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1543 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001544 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1545 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001546 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547 }
1548
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001549 global->nl = nl_create_handle(global->nl_cb, "nl");
1550 if (global->nl == NULL)
1551 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001552
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001553 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1554 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1556 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001557 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001558 }
1559
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001560 global->nl_event = nl_create_handle(global->nl_cb, "event");
1561 if (global->nl_event == NULL)
1562 goto err;
1563
1564 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001565 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001566 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001567 if (ret < 0) {
1568 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1569 "membership for scan events: %d (%s)",
1570 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001571 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572 }
1573
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001574 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001576 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577 if (ret < 0) {
1578 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1579 "membership for mlme events: %d (%s)",
1580 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001581 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001582 }
1583
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001584 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001586 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001587 if (ret < 0) {
1588 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1589 "membership for regulatory events: %d (%s)",
1590 ret, strerror(-ret));
1591 /* Continue without regulatory events */
1592 }
1593
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001594 ret = nl_get_multicast_id(global, "nl80211", "vendor");
1595 if (ret >= 0)
1596 ret = nl_socket_add_membership(global->nl_event, ret);
1597 if (ret < 0) {
1598 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1599 "membership for vendor events: %d (%s)",
1600 ret, strerror(-ret));
1601 /* Continue without vendor events */
1602 }
1603
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001604 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1605 no_seq_check, NULL);
1606 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1607 process_global_event, global);
1608
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001609 nl80211_register_eloop_read(&global->nl_event,
1610 wpa_driver_nl80211_event_receive,
1611 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001612
1613 return 0;
1614
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001615err:
1616 nl_destroy_handles(&global->nl_event);
1617 nl_destroy_handles(&global->nl);
1618 nl_cb_put(global->nl_cb);
1619 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001620 return -1;
1621}
1622
1623
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001624static void nl80211_check_global(struct nl80211_global *global)
1625{
1626 struct nl_handle *handle;
1627 const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
1628 int ret;
1629 unsigned int i;
1630
1631 /*
1632 * Try to re-add memberships to handle case of cfg80211 getting reloaded
1633 * and all registration having been cleared.
1634 */
1635 handle = (void *) (((intptr_t) global->nl_event) ^
1636 ELOOP_SOCKET_INVALID);
1637
1638 for (i = 0; groups[i]; i++) {
1639 ret = nl_get_multicast_id(global, "nl80211", groups[i]);
1640 if (ret >= 0)
1641 ret = nl_socket_add_membership(handle, ret);
1642 if (ret < 0) {
1643 wpa_printf(MSG_INFO,
1644 "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
1645 groups[i], ret, strerror(-ret));
1646 }
1647 }
1648}
1649
1650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001651static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1652{
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001653 struct wpa_driver_nl80211_data *drv = ctx;
1654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001656
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001657 /*
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001658 * rtnetlink ifdown handler will report interfaces other than the P2P
1659 * Device interface as disabled.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660 */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001661 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1662 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663}
1664
1665
1666static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1667{
1668 struct wpa_driver_nl80211_data *drv = ctx;
1669 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001670 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1672 "after rfkill unblock");
1673 return;
1674 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001675
1676 if (is_p2p_net_interface(drv->nlmode))
1677 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1678
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001679 /*
1680 * rtnetlink ifup handler will report interfaces other than the P2P
1681 * Device interface as enabled.
1682 */
1683 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1684 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685}
1686
1687
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001688static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1689 void *eloop_ctx,
1690 void *handle)
1691{
1692 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1693 u8 data[2048];
1694 struct msghdr msg;
1695 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001696 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001697 struct cmsghdr *cmsg;
1698 int res, found_ee = 0, found_wifi = 0, acked = 0;
1699 union wpa_event_data event;
1700
1701 memset(&msg, 0, sizeof(msg));
1702 msg.msg_iov = &entry;
1703 msg.msg_iovlen = 1;
1704 entry.iov_base = data;
1705 entry.iov_len = sizeof(data);
1706 msg.msg_control = &control;
1707 msg.msg_controllen = sizeof(control);
1708
1709 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1710 /* if error or not fitting 802.3 header, return */
1711 if (res < 14)
1712 return;
1713
1714 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1715 {
1716 if (cmsg->cmsg_level == SOL_SOCKET &&
1717 cmsg->cmsg_type == SCM_WIFI_STATUS) {
1718 int *ack;
1719
1720 found_wifi = 1;
1721 ack = (void *)CMSG_DATA(cmsg);
1722 acked = *ack;
1723 }
1724
1725 if (cmsg->cmsg_level == SOL_PACKET &&
1726 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1727 struct sock_extended_err *err =
1728 (struct sock_extended_err *)CMSG_DATA(cmsg);
1729
1730 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1731 found_ee = 1;
1732 }
1733 }
1734
1735 if (!found_ee || !found_wifi)
1736 return;
1737
1738 memset(&event, 0, sizeof(event));
1739 event.eapol_tx_status.dst = data;
1740 event.eapol_tx_status.data = data + 14;
1741 event.eapol_tx_status.data_len = res - 14;
1742 event.eapol_tx_status.ack = acked;
1743 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1744}
1745
1746
1747static int nl80211_init_bss(struct i802_bss *bss)
1748{
1749 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1750 if (!bss->nl_cb)
1751 return -1;
1752
1753 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1754 no_seq_check, NULL);
1755 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1756 process_bss_event, bss);
1757
1758 return 0;
1759}
1760
1761
1762static void nl80211_destroy_bss(struct i802_bss *bss)
1763{
1764 nl_cb_put(bss->nl_cb);
1765 bss->nl_cb = NULL;
1766}
1767
1768
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001769static void
1770wpa_driver_nl80211_drv_init_rfkill(struct wpa_driver_nl80211_data *drv)
1771{
1772 struct rfkill_config *rcfg;
1773
1774 if (drv->rfkill)
1775 return;
1776
1777 rcfg = os_zalloc(sizeof(*rcfg));
1778 if (!rcfg)
1779 return;
1780
1781 rcfg->ctx = drv;
1782
1783 /* rfkill uses netdev sysfs for initialization. However, P2P Device is
1784 * not associated with a netdev, so use the name of some other interface
1785 * sharing the same wiphy as the P2P Device interface.
1786 *
1787 * Note: This is valid, as a P2P Device interface is always dynamically
1788 * created and is created only once another wpa_s interface was added.
1789 */
1790 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
1791 struct nl80211_global *global = drv->global;
1792 struct wpa_driver_nl80211_data *tmp1;
1793
1794 dl_list_for_each(tmp1, &global->interfaces,
1795 struct wpa_driver_nl80211_data, list) {
1796 if (drv == tmp1 || drv->wiphy_idx != tmp1->wiphy_idx ||
1797 !tmp1->rfkill)
1798 continue;
1799
1800 wpa_printf(MSG_DEBUG,
1801 "nl80211: Use (%s) to initialize P2P Device rfkill",
1802 tmp1->first_bss->ifname);
1803 os_strlcpy(rcfg->ifname, tmp1->first_bss->ifname,
1804 sizeof(rcfg->ifname));
1805 break;
1806 }
1807 } else {
1808 os_strlcpy(rcfg->ifname, drv->first_bss->ifname,
1809 sizeof(rcfg->ifname));
1810 }
1811
1812 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
1813 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
1814 drv->rfkill = rfkill_init(rcfg);
1815 if (!drv->rfkill) {
1816 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
1817 os_free(rcfg);
1818 }
1819}
1820
1821
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001822static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
1823 void *global_priv, int hostapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001824 const u8 *set_addr,
1825 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001826{
1827 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 struct i802_bss *bss;
1829
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001830 if (global_priv == NULL)
1831 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001832 drv = os_zalloc(sizeof(*drv));
1833 if (drv == NULL)
1834 return NULL;
1835 drv->global = global_priv;
1836 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001837 drv->hostapd = !!hostapd;
1838 drv->eapol_sock = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001839
1840 /*
1841 * There is no driver capability flag for this, so assume it is
1842 * supported and disable this on first attempt to use if the driver
1843 * rejects the command due to missing support.
1844 */
1845 drv->set_rekey_offload = 1;
1846
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001847 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
1848 drv->if_indices = drv->default_if_indices;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001849 drv->if_indices_reason = drv->default_if_indices_reason;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001850
1851 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
1852 if (!drv->first_bss) {
1853 os_free(drv);
1854 return NULL;
1855 }
1856 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001857 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001858 bss->ctx = ctx;
1859
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001860 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
1861 drv->monitor_ifidx = -1;
1862 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001863 drv->eapol_tx_sock = -1;
1864 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001866 if (nl80211_init_bss(bss))
1867 goto failed;
1868
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001869 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 goto failed;
1871
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001872 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
1873 if (drv->eapol_tx_sock < 0)
1874 goto failed;
1875
1876 if (drv->data_tx_status) {
1877 int enabled = 1;
1878
1879 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
1880 &enabled, sizeof(enabled)) < 0) {
1881 wpa_printf(MSG_DEBUG,
1882 "nl80211: wifi status sockopt failed\n");
1883 drv->data_tx_status = 0;
1884 if (!drv->use_monitor)
1885 drv->capa.flags &=
1886 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1887 } else {
1888 eloop_register_read_sock(drv->eapol_tx_sock,
1889 wpa_driver_nl80211_handle_eapol_tx_status,
1890 drv, NULL);
1891 }
1892 }
1893
1894 if (drv->global) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001895 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001896 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001897 drv->in_interface_list = 1;
1898 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001899
1900 return bss;
1901
1902failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001903 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904 return NULL;
1905}
1906
1907
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001908/**
1909 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1910 * @ctx: context to be used when calling wpa_supplicant functions,
1911 * e.g., wpa_supplicant_event()
1912 * @ifname: interface name, e.g., wlan0
1913 * @global_priv: private driver global data from global_init()
1914 * Returns: Pointer to private data, %NULL on failure
1915 */
1916static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
1917 void *global_priv)
1918{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001919 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
1920 NULL);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001921}
1922
1923
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001924static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925 struct nl_handle *nl_handle,
1926 u16 type, const u8 *match, size_t match_len)
1927{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001928 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001929 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001930 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001931 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001932
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001933 buf[0] = '\0';
1934 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001935 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
1936 type, fc2str(type), nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001938 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
1939 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
1940 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
1941 nlmsg_free(msg);
1942 return -1;
1943 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001944
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001945 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001946 if (ret) {
1947 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
1948 "failed (type=%u): ret=%d (%s)",
1949 type, ret, strerror(-ret));
1950 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
1951 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001952 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953 return ret;
1954}
1955
1956
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001957static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
1958{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001959 if (bss->nl_mgmt) {
1960 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
1961 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
1962 return -1;
1963 }
1964
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001965 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001966 if (bss->nl_mgmt == NULL)
1967 return -1;
1968
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001969 return 0;
1970}
1971
1972
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001973static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
1974{
1975 nl80211_register_eloop_read(&bss->nl_mgmt,
1976 wpa_driver_nl80211_event_receive,
1977 bss->nl_cb);
1978}
1979
1980
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001981static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982 const u8 *match, size_t match_len)
1983{
1984 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001985 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001986 type, match, match_len);
1987}
1988
1989
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001990static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001991{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001992 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001993 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001994
1995 if (nl80211_alloc_mgmt_handle(bss))
1996 return -1;
1997 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
1998 "handle %p", bss->nl_mgmt);
1999
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002000 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
2001 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
2002
2003 /* register for any AUTH message */
2004 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
2005 }
2006
Dmitry Shmidt051af732013-10-22 13:52:46 -07002007#ifdef CONFIG_INTERWORKING
2008 /* QoS Map Configure */
2009 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002010 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002011#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002012#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002014 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002015 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002016 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002017 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002018 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002019 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002020 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002021 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002023 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002024 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002025 /* Protected GAS Initial Request */
2026 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
2027 ret = -1;
2028 /* Protected GAS Initial Response */
2029 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
2030 ret = -1;
2031 /* Protected GAS Comeback Request */
2032 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
2033 ret = -1;
2034 /* Protected GAS Comeback Response */
2035 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
2036 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002037#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
2038#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002040 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
2042 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002043 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002045 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002046 (u8 *) "\x7f\x50\x6f\x9a\x09",
2047 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002048 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049#endif /* CONFIG_P2P */
2050#ifdef CONFIG_IEEE80211W
2051 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002052 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002053 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002055#ifdef CONFIG_TDLS
2056 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
2057 /* TDLS Discovery Response */
2058 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
2059 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002060 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002061 }
2062#endif /* CONFIG_TDLS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002063#ifdef CONFIG_FST
2064 /* FST Action frames */
2065 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2066 ret = -1;
2067#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068
2069 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002070 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002071 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072 else
2073 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
2074 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2075
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002076 /* WNM - BSS Transition Management Request */
2077 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002078 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002079 /* WNM-Sleep Mode Response */
2080 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002081 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002082
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002083#ifdef CONFIG_HS20
2084 /* WNM-Notification */
2085 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002086 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002087#endif /* CONFIG_HS20 */
2088
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002089 /* WMM-AC ADDTS Response */
2090 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
2091 ret = -1;
2092
2093 /* WMM-AC DELTS */
2094 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
2095 ret = -1;
2096
2097 /* Radio Measurement - Neighbor Report Response */
2098 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
2099 ret = -1;
2100
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002101 /* Radio Measurement - Radio Measurement Request */
2102 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x00", 2) < 0)
2103 ret = -1;
2104
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002105 /* Radio Measurement - Link Measurement Request */
2106 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
2107 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
2108 ret = -1;
2109
2110 nl80211_mgmt_handle_register_eloop(bss);
2111
2112 return ret;
2113}
2114
2115
2116static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
2117{
2118 int ret = 0;
2119
2120 if (nl80211_alloc_mgmt_handle(bss))
2121 return -1;
2122
2123 wpa_printf(MSG_DEBUG,
2124 "nl80211: Subscribe to mgmt frames with mesh handle %p",
2125 bss->nl_mgmt);
2126
2127 /* Auth frames for mesh SAE */
2128 if (nl80211_register_frame(bss, bss->nl_mgmt,
2129 (WLAN_FC_TYPE_MGMT << 2) |
2130 (WLAN_FC_STYPE_AUTH << 4),
2131 NULL, 0) < 0)
2132 ret = -1;
2133
2134 /* Mesh peering open */
2135 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2136 ret = -1;
2137 /* Mesh peering confirm */
2138 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2139 ret = -1;
2140 /* Mesh peering close */
2141 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2142 ret = -1;
2143
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002144 nl80211_mgmt_handle_register_eloop(bss);
2145
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002146 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002147}
2148
2149
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002150static int nl80211_register_spurious_class3(struct i802_bss *bss)
2151{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002152 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002153 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002154
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002155 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
2156 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002157 if (ret) {
2158 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2159 "failed: ret=%d (%s)",
2160 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002161 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002162 return ret;
2163}
2164
2165
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002166static int nl80211_action_subscribe_ap(struct i802_bss *bss)
2167{
2168 int ret = 0;
2169
2170 /* Public Action frames */
2171 if (nl80211_register_action_frame(bss, (u8 *) "\x04", 1) < 0)
2172 ret = -1;
2173 /* RRM Measurement Report */
2174 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x01", 2) < 0)
2175 ret = -1;
Paul Stewart092955c2017-02-06 09:13:09 -08002176 /* RRM Link Measurement Report */
2177 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x03", 2) < 0)
2178 ret = -1;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002179 /* RRM Neighbor Report Request */
2180 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x04", 2) < 0)
2181 ret = -1;
2182 /* FT Action frames */
2183 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
2184 ret = -1;
2185#ifdef CONFIG_IEEE80211W
2186 /* SA Query */
2187 if (nl80211_register_action_frame(bss, (u8 *) "\x08", 1) < 0)
2188 ret = -1;
2189#endif /* CONFIG_IEEE80211W */
2190 /* Protected Dual of Public Action */
2191 if (nl80211_register_action_frame(bss, (u8 *) "\x09", 1) < 0)
2192 ret = -1;
2193 /* WNM */
2194 if (nl80211_register_action_frame(bss, (u8 *) "\x0a", 1) < 0)
2195 ret = -1;
2196 /* WMM */
2197 if (nl80211_register_action_frame(bss, (u8 *) "\x11", 1) < 0)
2198 ret = -1;
2199#ifdef CONFIG_FST
2200 /* FST Action frames */
2201 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2202 ret = -1;
2203#endif /* CONFIG_FST */
2204 /* Vendor-specific */
2205 if (nl80211_register_action_frame(bss, (u8 *) "\x7f", 1) < 0)
2206 ret = -1;
2207
2208 return ret;
2209}
2210
2211
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002212static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2213{
2214 static const int stypes[] = {
2215 WLAN_FC_STYPE_AUTH,
2216 WLAN_FC_STYPE_ASSOC_REQ,
2217 WLAN_FC_STYPE_REASSOC_REQ,
2218 WLAN_FC_STYPE_DISASSOC,
2219 WLAN_FC_STYPE_DEAUTH,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002220 WLAN_FC_STYPE_PROBE_REQ,
2221/* Beacon doesn't work as mac80211 doesn't currently allow
2222 * it, but it wouldn't really be the right thing anyway as
2223 * it isn't per interface ... maybe just dump the scan
2224 * results periodically for OLBC?
2225 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002226 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002227 };
2228 unsigned int i;
2229
2230 if (nl80211_alloc_mgmt_handle(bss))
2231 return -1;
2232 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2233 "handle %p", bss->nl_mgmt);
2234
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002235 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002236 if (nl80211_register_frame(bss, bss->nl_mgmt,
2237 (WLAN_FC_TYPE_MGMT << 2) |
2238 (stypes[i] << 4),
2239 NULL, 0) < 0) {
2240 goto out_err;
2241 }
2242 }
2243
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002244 if (nl80211_action_subscribe_ap(bss))
2245 goto out_err;
2246
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002247 if (nl80211_register_spurious_class3(bss))
2248 goto out_err;
2249
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002250 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002251 return 0;
2252
2253out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002254 nl_destroy_handles(&bss->nl_mgmt);
2255 return -1;
2256}
2257
2258
2259static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2260{
2261 if (nl80211_alloc_mgmt_handle(bss))
2262 return -1;
2263 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2264 "handle %p (device SME)", bss->nl_mgmt);
2265
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002266 if (nl80211_action_subscribe_ap(bss))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002267 goto out_err;
2268
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002269 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002270 return 0;
2271
2272out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002273 nl_destroy_handles(&bss->nl_mgmt);
2274 return -1;
2275}
2276
2277
2278static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2279{
2280 if (bss->nl_mgmt == NULL)
2281 return;
2282 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2283 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002284 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002285
2286 nl80211_put_wiphy_data_ap(bss);
2287}
2288
2289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002290static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2291{
2292 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2293}
2294
2295
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002296static void nl80211_del_p2pdev(struct i802_bss *bss)
2297{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002298 struct nl_msg *msg;
2299 int ret;
2300
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002301 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2302 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002303
2304 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2305 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002306 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002307}
2308
2309
2310static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2311{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002312 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002313 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002314
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002315 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2316 NL80211_CMD_STOP_P2P_DEVICE);
2317 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002318
2319 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2320 start ? "Start" : "Stop",
2321 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002322 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002323 return ret;
2324}
2325
2326
2327static int i802_set_iface_flags(struct i802_bss *bss, int up)
2328{
2329 enum nl80211_iftype nlmode;
2330
2331 nlmode = nl80211_get_ifmode(bss);
2332 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2333 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2334 bss->ifname, up);
2335 }
2336
2337 /* P2P Device has start/stop which is equivalent */
2338 return nl80211_set_p2pdev(bss, up);
2339}
2340
2341
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002342#ifdef CONFIG_TESTING_OPTIONS
2343static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2344{
2345 /* struct wpa_driver_nl80211_data *drv = arg; */
2346 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2347 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2348
2349
2350 wpa_printf(MSG_DEBUG,
2351 "nl80211: QCA vendor test command response received");
2352
2353 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2354 genlmsg_attrlen(gnlh, 0), NULL);
2355 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2356 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2357 return NL_SKIP;
2358 }
2359
2360 wpa_hexdump(MSG_DEBUG,
2361 "nl80211: Received QCA vendor test command response",
2362 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2363 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2364
2365 return NL_SKIP;
2366}
2367#endif /* CONFIG_TESTING_OPTIONS */
2368
2369
2370static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2371{
2372#ifdef CONFIG_TESTING_OPTIONS
2373 struct nl_msg *msg;
2374 struct nlattr *params;
2375 int ret;
2376
2377 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2378 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2379 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2380 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2381 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2382 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2383 nlmsg_free(msg);
2384 return;
2385 }
2386 nla_nest_end(msg, params);
2387
2388 ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
2389 wpa_printf(MSG_DEBUG,
2390 "nl80211: QCA vendor test command returned %d (%s)",
2391 ret, strerror(-ret));
2392#endif /* CONFIG_TESTING_OPTIONS */
2393}
2394
2395
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002396static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002397wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002398 const u8 *set_addr, int first,
2399 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002400{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002401 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002402 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002403 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002404
2405 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002406 bss->ifindex = drv->ifindex;
2407 bss->wdev_id = drv->global->if_add_wdevid;
2408 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2409
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002410 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2411 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002412 drv->global->if_add_wdevid_set = 0;
2413
Dmitry Shmidt03658832014-08-13 11:03:49 -07002414 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2415 bss->static_ap = 1;
2416
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002417 if (first &&
2418 nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
2419 linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
2420 drv->start_iface_up = 1;
2421
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002422 if (wpa_driver_nl80211_capa(drv))
2423 return -1;
2424
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002425 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2426 return -1;
2427
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002428 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2429 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002430
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002431 if (set_addr &&
2432 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2433 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2434 set_addr)))
2435 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002436
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002437 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2438 drv->start_mode_ap = 1;
2439
Dmitry Shmidt03658832014-08-13 11:03:49 -07002440 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002441 nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002442 else if (bss->if_dynamic ||
2443 nl80211_get_ifmode(bss) == NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002444 nlmode = nl80211_get_ifmode(bss);
2445 else
2446 nlmode = NL80211_IFTYPE_STATION;
2447
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002448 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002449 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002450 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002451 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002452
Dmitry Shmidt98660862014-03-11 17:26:21 -07002453 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002454 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002455
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002456 wpa_driver_nl80211_drv_init_rfkill(drv);
2457
Dmitry Shmidt98660862014-03-11 17:26:21 -07002458 if (!rfkill_is_blocked(drv->rfkill)) {
2459 int ret = i802_set_iface_flags(bss, 1);
2460 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002461 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2462 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002463 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002465
2466 if (is_p2p_net_interface(nlmode))
2467 nl80211_disable_11b_rates(bss->drv,
2468 bss->drv->ifindex, 1);
2469
Dmitry Shmidt98660862014-03-11 17:26:21 -07002470 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2471 return ret;
2472 } else {
2473 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2474 "interface '%s' due to rfkill", bss->ifname);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002475 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
2476 drv->if_disabled = 1;
2477
Dmitry Shmidt98660862014-03-11 17:26:21 -07002478 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479 }
2480
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002481 if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002482 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2483 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002485 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2486 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2487 bss->addr))
2488 return -1;
2489 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
2490 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002492 if (send_rfkill_event) {
2493 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2494 drv, drv->ctx);
2495 }
2496
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002497 if (drv->vendor_cmd_test_avail)
2498 qca_vendor_test(drv);
2499
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500 return 0;
2501}
2502
2503
Paul Stewart092955c2017-02-06 09:13:09 -08002504static int wpa_driver_nl80211_del_beacon(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505{
2506 struct nl_msg *msg;
Paul Stewart092955c2017-02-06 09:13:09 -08002507 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002508
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002509 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2510 drv->ifindex);
Paul Stewart092955c2017-02-06 09:13:09 -08002511 nl80211_put_wiphy_data_ap(bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002512 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514}
2515
2516
2517/**
2518 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002519 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520 *
2521 * Shut down driver interface and processing of driver events. Free
2522 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2523 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002524static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002525{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002526 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002527 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002529 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2530 bss->ifname, drv->disabled_11b_rates);
2531
Dmitry Shmidt04949592012-07-19 12:16:46 -07002532 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002533 if (drv->data_tx_status)
2534 eloop_unregister_read_sock(drv->eapol_tx_sock);
2535 if (drv->eapol_tx_sock >= 0)
2536 close(drv->eapol_tx_sock);
2537
2538 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539 wpa_driver_nl80211_probe_req_report(bss, 0);
2540 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002541 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2542 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002543 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2544 "interface %s from bridge %s: %s",
2545 bss->ifname, bss->brname, strerror(errno));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002546 if (drv->rtnl_sk)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002547 nl80211_handle_destroy(drv->rtnl_sk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002548 }
2549 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002550 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2551 0) < 0)
2552 wpa_printf(MSG_INFO,
2553 "nl80211: Could not set bridge %s down",
2554 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002555 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2557 "bridge %s: %s",
2558 bss->brname, strerror(errno));
2559 }
2560
2561 nl80211_remove_monitor_interface(drv);
2562
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002563 if (is_ap_interface(drv->nlmode))
Paul Stewart092955c2017-02-06 09:13:09 -08002564 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002566 if (drv->eapol_sock >= 0) {
2567 eloop_unregister_read_sock(drv->eapol_sock);
2568 close(drv->eapol_sock);
2569 }
2570
2571 if (drv->if_indices != drv->default_if_indices)
2572 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002574 if (drv->if_indices_reason != drv->default_if_indices_reason)
2575 os_free(drv->if_indices_reason);
2576
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002577 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002578 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2579
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002580 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2581 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002582 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583 rfkill_deinit(drv->rfkill);
2584
2585 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2586
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002587 if (!drv->start_iface_up)
2588 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002589
2590 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002591 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2592 0) < 0) {
2593 wpa_printf(MSG_DEBUG,
2594 "nl80211: Could not set interface down to restore permanent MAC address");
2595 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002596 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2597 drv->perm_addr) < 0) {
2598 wpa_printf(MSG_DEBUG,
2599 "nl80211: Could not restore permanent MAC address");
2600 }
2601 }
2602
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002603 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002604 if (!drv->hostapd || !drv->start_mode_ap)
2605 wpa_driver_nl80211_set_mode(bss,
2606 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002607 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002608 } else {
2609 nl80211_mgmt_unsubscribe(bss, "deinit");
2610 nl80211_del_p2pdev(bss);
2611 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002613 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002615 os_free(drv->filter_ssids);
2616
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002617 os_free(drv->auth_ie);
2618
2619 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002620 dl_list_del(&drv->list);
2621
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002622 os_free(drv->extended_capa);
2623 os_free(drv->extended_capa_mask);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002624 for (i = 0; i < drv->num_iface_ext_capa; i++) {
2625 os_free(drv->iface_ext_capa[i].ext_capa);
2626 os_free(drv->iface_ext_capa[i].ext_capa_mask);
2627 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002628 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002629 os_free(drv);
2630}
2631
2632
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002633static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2634{
2635 switch (alg) {
2636 case WPA_ALG_WEP:
2637 if (key_len == 5)
Paul Stewart092955c2017-02-06 09:13:09 -08002638 return RSN_CIPHER_SUITE_WEP40;
2639 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002640 case WPA_ALG_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08002641 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002642 case WPA_ALG_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002643 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002644 case WPA_ALG_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002645 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002646 case WPA_ALG_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002647 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002648 case WPA_ALG_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002649 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002650 case WPA_ALG_IGTK:
Paul Stewart092955c2017-02-06 09:13:09 -08002651 return RSN_CIPHER_SUITE_AES_128_CMAC;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002652 case WPA_ALG_BIP_GMAC_128:
Paul Stewart092955c2017-02-06 09:13:09 -08002653 return RSN_CIPHER_SUITE_BIP_GMAC_128;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002654 case WPA_ALG_BIP_GMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002655 return RSN_CIPHER_SUITE_BIP_GMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002656 case WPA_ALG_BIP_CMAC_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002657 return RSN_CIPHER_SUITE_BIP_CMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002658 case WPA_ALG_SMS4:
Paul Stewart092955c2017-02-06 09:13:09 -08002659 return RSN_CIPHER_SUITE_SMS4;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002660 case WPA_ALG_KRK:
Paul Stewart092955c2017-02-06 09:13:09 -08002661 return RSN_CIPHER_SUITE_KRK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002662 case WPA_ALG_NONE:
2663 case WPA_ALG_PMK:
2664 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2665 alg);
2666 return 0;
2667 }
2668
2669 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2670 alg);
2671 return 0;
2672}
2673
2674
2675static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2676{
2677 switch (cipher) {
2678 case WPA_CIPHER_CCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002679 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002680 case WPA_CIPHER_GCMP_256:
Paul Stewart092955c2017-02-06 09:13:09 -08002681 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002682 case WPA_CIPHER_CCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002683 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002684 case WPA_CIPHER_GCMP:
Paul Stewart092955c2017-02-06 09:13:09 -08002685 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002686 case WPA_CIPHER_TKIP:
Paul Stewart092955c2017-02-06 09:13:09 -08002687 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002688 case WPA_CIPHER_WEP104:
Paul Stewart092955c2017-02-06 09:13:09 -08002689 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002690 case WPA_CIPHER_WEP40:
Paul Stewart092955c2017-02-06 09:13:09 -08002691 return RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002692 case WPA_CIPHER_GTK_NOT_USED:
Paul Stewart092955c2017-02-06 09:13:09 -08002693 return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002694 }
2695
2696 return 0;
2697}
2698
2699
2700static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2701 int max_suites)
2702{
2703 int num_suites = 0;
2704
2705 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08002706 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002707 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
Paul Stewart092955c2017-02-06 09:13:09 -08002708 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002709 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08002710 suites[num_suites++] = RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002711 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
Paul Stewart092955c2017-02-06 09:13:09 -08002712 suites[num_suites++] = RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002713 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
Paul Stewart092955c2017-02-06 09:13:09 -08002714 suites[num_suites++] = RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002715 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
Paul Stewart092955c2017-02-06 09:13:09 -08002716 suites[num_suites++] = RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002717 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
Paul Stewart092955c2017-02-06 09:13:09 -08002718 suites[num_suites++] = RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002719
2720 return num_suites;
2721}
2722
2723
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002724#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002725static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2726 const u8 *key, size_t key_len)
2727{
2728 struct nl_msg *msg;
2729 int ret;
2730
2731 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
2732 return 0;
2733
2734 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2735 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2736 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2737 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2738 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
2739 nl80211_nlmsg_clear(msg);
2740 nlmsg_free(msg);
2741 return -1;
2742 }
2743 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2744 if (ret) {
2745 wpa_printf(MSG_DEBUG,
2746 "nl80211: Key management set key failed: ret=%d (%s)",
2747 ret, strerror(-ret));
2748 }
2749
2750 return ret;
2751}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002752#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002753
2754
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002755static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002756 enum wpa_alg alg, const u8 *addr,
2757 int key_idx, int set_tx,
2758 const u8 *seq, size_t seq_len,
2759 const u8 *key, size_t key_len)
2760{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002761 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002762 int ifindex;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002763 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002764 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002765 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002766
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002767 /* Ignore for P2P Device */
2768 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2769 return 0;
2770
2771 ifindex = if_nametoindex(ifname);
2772 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002774 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002775 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002776#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002777 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002778 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002779 tdls = 1;
2780 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002781#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002782
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002783#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002784 if (alg == WPA_ALG_PMK &&
2785 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
2786 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2787 __func__);
2788 ret = issue_key_mgmt_set_key(drv, key, key_len);
2789 return ret;
2790 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002791#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002792
2793 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002794 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2795 if (!msg)
2796 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002797 } else {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002798 u32 suite;
2799
2800 suite = wpa_alg_to_cipher_suite(alg, key_len);
2801 if (!suite)
2802 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002803 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2804 if (!msg ||
2805 nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002806 nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, suite))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002807 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002808 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809 }
2810
Dmitry Shmidt98660862014-03-11 17:26:21 -07002811 if (seq && seq_len) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002812 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2813 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002814 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2815 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002816
2817 if (addr && !is_broadcast_ether_addr(addr)) {
2818 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002819 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2820 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002821
2822 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2823 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002824 if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
2825 NL80211_KEYTYPE_GROUP))
2826 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827 }
2828 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002829 struct nlattr *types;
2830
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002831 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002832
2833 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002834 if (!types ||
2835 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2836 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002837 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002838 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002839 if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2840 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002842 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002843 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
2844 ret = 0;
2845 if (ret)
2846 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2847 ret, strerror(-ret));
2848
2849 /*
2850 * If we failed or don't need to set the default TX key (below),
2851 * we're done here.
2852 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002853 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002854 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002855 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856 !is_broadcast_ether_addr(addr))
2857 return ret;
2858
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002859 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
2860 if (!msg ||
2861 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002862 nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
2863 alg == WPA_ALG_BIP_GMAC_128 ||
2864 alg == WPA_ALG_BIP_GMAC_256 ||
2865 alg == WPA_ALG_BIP_CMAC_256) ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002866 NL80211_ATTR_KEY_DEFAULT_MGMT :
2867 NL80211_ATTR_KEY_DEFAULT))
2868 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002869 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002870 struct nlattr *types;
2871
2872 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002873 if (!types ||
2874 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2875 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002876 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002878 struct nlattr *types;
2879
2880 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002881 if (!types ||
2882 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
2883 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002884 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885 }
2886
2887 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2888 if (ret == -ENOENT)
2889 ret = 0;
2890 if (ret)
2891 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
2892 "err=%d %s)", ret, strerror(-ret));
2893 return ret;
2894
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002895fail:
2896 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002897 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002898 return -ENOBUFS;
2899}
2900
2901
2902static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
2903 int key_idx, int defkey,
2904 const u8 *seq, size_t seq_len,
2905 const u8 *key, size_t key_len)
2906{
2907 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002908 u32 suite;
2909
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 if (!key_attr)
2911 return -1;
2912
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002913 suite = wpa_alg_to_cipher_suite(alg, key_len);
2914 if (!suite)
2915 return -1;
2916
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002917 if (defkey && alg == WPA_ALG_IGTK) {
2918 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
2919 return -1;
2920 } else if (defkey) {
2921 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
2922 return -1;
2923 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002924
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002925 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002926 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002927 (seq && seq_len &&
2928 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
2929 nla_put(msg, NL80211_KEY_DATA, key_len, key))
2930 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931
2932 nla_nest_end(msg, key_attr);
2933
2934 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002935}
2936
2937
2938static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
2939 struct nl_msg *msg)
2940{
2941 int i, privacy = 0;
2942 struct nlattr *nl_keys, *nl_key;
2943
2944 for (i = 0; i < 4; i++) {
2945 if (!params->wep_key[i])
2946 continue;
2947 privacy = 1;
2948 break;
2949 }
2950 if (params->wps == WPS_MODE_PRIVACY)
2951 privacy = 1;
2952 if (params->pairwise_suite &&
2953 params->pairwise_suite != WPA_CIPHER_NONE)
2954 privacy = 1;
2955
2956 if (!privacy)
2957 return 0;
2958
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002959 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
2960 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002961
2962 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
2963 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002964 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965
2966 for (i = 0; i < 4; i++) {
2967 if (!params->wep_key[i])
2968 continue;
2969
2970 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002971 if (!nl_key ||
2972 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
2973 params->wep_key[i]) ||
2974 nla_put_u32(msg, NL80211_KEY_CIPHER,
2975 params->wep_key_len[i] == 5 ?
Paul Stewart092955c2017-02-06 09:13:09 -08002976 RSN_CIPHER_SUITE_WEP40 :
2977 RSN_CIPHER_SUITE_WEP104) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002978 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
2979 (i == params->wep_tx_keyidx &&
2980 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
2981 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002982
2983 nla_nest_end(msg, nl_key);
2984 }
2985 nla_nest_end(msg, nl_keys);
2986
2987 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002988}
2989
2990
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002991int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
2992 const u8 *addr, int cmd, u16 reason_code,
2993 int local_state_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002994{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002995 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002996 struct nl_msg *msg;
2997
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002998 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
2999 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
3000 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
3001 (local_state_change &&
3002 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
3003 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003004 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003005 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006
3007 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003008 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003009 wpa_dbg(drv->ctx, MSG_DEBUG,
3010 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
3011 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003012 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003013 return ret;
3014}
3015
3016
3017static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003018 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003019{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003020 int ret;
3021
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003022 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003023 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003024 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003025 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
3026 reason_code, 0);
3027 /*
3028 * For locally generated disconnect, supplicant already generates a
3029 * DEAUTH event, so ignore the event from NL80211.
3030 */
3031 drv->ignore_next_local_disconnect = ret == 0;
3032
3033 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003034}
3035
3036
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003037static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
3038 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003039{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003040 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003041 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003042
3043 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
3044 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003045 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003046 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003048 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003049 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3050 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003051 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003052 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
3053 reason_code, 0);
3054 /*
3055 * For locally generated deauthenticate, supplicant already generates a
3056 * DEAUTH event, so ignore the event from NL80211.
3057 */
3058 drv->ignore_next_local_deauth = ret == 0;
3059 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060}
3061
3062
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003063static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
3064 struct wpa_driver_auth_params *params)
3065{
3066 int i;
3067
3068 drv->auth_freq = params->freq;
3069 drv->auth_alg = params->auth_alg;
3070 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
3071 drv->auth_local_state_change = params->local_state_change;
3072 drv->auth_p2p = params->p2p;
3073
3074 if (params->bssid)
3075 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
3076 else
3077 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
3078
3079 if (params->ssid) {
3080 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
3081 drv->auth_ssid_len = params->ssid_len;
3082 } else
3083 drv->auth_ssid_len = 0;
3084
3085
3086 os_free(drv->auth_ie);
3087 drv->auth_ie = NULL;
3088 drv->auth_ie_len = 0;
3089 if (params->ie) {
3090 drv->auth_ie = os_malloc(params->ie_len);
3091 if (drv->auth_ie) {
3092 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
3093 drv->auth_ie_len = params->ie_len;
3094 }
3095 }
3096
3097 for (i = 0; i < 4; i++) {
3098 if (params->wep_key[i] && params->wep_key_len[i] &&
3099 params->wep_key_len[i] <= 16) {
3100 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
3101 params->wep_key_len[i]);
3102 drv->auth_wep_key_len[i] = params->wep_key_len[i];
3103 } else
3104 drv->auth_wep_key_len[i] = 0;
3105 }
3106}
3107
3108
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003109static void nl80211_unmask_11b_rates(struct i802_bss *bss)
3110{
3111 struct wpa_driver_nl80211_data *drv = bss->drv;
3112
3113 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
3114 return;
3115
3116 /*
3117 * Looks like we failed to unmask 11b rates previously. This could
3118 * happen, e.g., if the interface was down at the point in time when a
3119 * P2P group was terminated.
3120 */
3121 wpa_printf(MSG_DEBUG,
3122 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
3123 bss->ifname);
3124 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3125}
3126
3127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003128static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003129 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003130{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003131 struct wpa_driver_nl80211_data *drv = bss->drv;
3132 int ret = -1, i;
3133 struct nl_msg *msg;
3134 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003135 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003136 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003137 int is_retry;
3138
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003139 nl80211_unmask_11b_rates(bss);
3140
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003141 is_retry = drv->retry_auth;
3142 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003143 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003144
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003145 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003146 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003147 if (params->bssid)
3148 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
3149 else
3150 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003151 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003152 nlmode = params->p2p ?
3153 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
3154 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003155 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003156 return -1;
3157
3158retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003159 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
3160 drv->ifindex);
3161
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003162 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
3163 if (!msg)
3164 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003165
3166 for (i = 0; i < 4; i++) {
3167 if (!params->wep_key[i])
3168 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003169 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003170 NULL, i,
3171 i == params->wep_tx_keyidx, NULL, 0,
3172 params->wep_key[i],
3173 params->wep_key_len[i]);
3174 if (params->wep_tx_keyidx != i)
3175 continue;
3176 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003177 params->wep_key[i], params->wep_key_len[i]))
3178 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003179 }
3180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003181 if (params->bssid) {
3182 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3183 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003184 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
3185 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003186 }
3187 if (params->freq) {
3188 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003189 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
3190 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003191 }
3192 if (params->ssid) {
3193 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3194 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003195 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
3196 params->ssid))
3197 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003198 }
3199 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003200 if (params->ie &&
3201 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
3202 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003203 if (params->auth_data) {
3204 wpa_hexdump(MSG_DEBUG, " * auth_data", params->auth_data,
3205 params->auth_data_len);
3206 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->auth_data_len,
3207 params->auth_data))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003208 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003209 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003210 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
3211 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
3212 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
3213 type = NL80211_AUTHTYPE_SHARED_KEY;
3214 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
3215 type = NL80211_AUTHTYPE_NETWORK_EAP;
3216 else if (params->auth_alg & WPA_AUTH_ALG_FT)
3217 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003218 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
3219 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003220 else if (params->auth_alg & WPA_AUTH_ALG_FILS)
3221 type = NL80211_AUTHTYPE_FILS_SK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003222 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003223 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003224 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003225 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
3226 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003227 if (params->local_state_change) {
3228 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003229 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
3230 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003231 }
3232
3233 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3234 msg = NULL;
3235 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003236 wpa_dbg(drv->ctx, MSG_DEBUG,
3237 "nl80211: MLME command failed (auth): ret=%d (%s)",
3238 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003239 count++;
3240 if (ret == -EALREADY && count == 1 && params->bssid &&
3241 !params->local_state_change) {
3242 /*
3243 * mac80211 does not currently accept new
3244 * authentication if we are already authenticated. As a
3245 * workaround, force deauthentication and try again.
3246 */
3247 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3248 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003249 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003250 wpa_driver_nl80211_deauthenticate(
3251 bss, params->bssid,
3252 WLAN_REASON_PREV_AUTH_NOT_VALID);
3253 nlmsg_free(msg);
3254 goto retry;
3255 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003256
3257 if (ret == -ENOENT && params->freq && !is_retry) {
3258 /*
3259 * cfg80211 has likely expired the BSS entry even
3260 * though it was previously available in our internal
3261 * BSS table. To recover quickly, start a single
3262 * channel scan on the specified channel.
3263 */
3264 struct wpa_driver_scan_params scan;
3265 int freqs[2];
3266
3267 os_memset(&scan, 0, sizeof(scan));
3268 scan.num_ssids = 1;
3269 if (params->ssid) {
3270 scan.ssids[0].ssid = params->ssid;
3271 scan.ssids[0].ssid_len = params->ssid_len;
3272 }
3273 freqs[0] = params->freq;
3274 freqs[1] = 0;
3275 scan.freqs = freqs;
3276 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3277 "channel scan to refresh cfg80211 BSS "
3278 "entry");
3279 ret = wpa_driver_nl80211_scan(bss, &scan);
3280 if (ret == 0) {
3281 nl80211_copy_auth_params(drv, params);
3282 drv->scan_for_auth = 1;
3283 }
3284 } else if (is_retry) {
3285 /*
3286 * Need to indicate this with an event since the return
3287 * value from the retry is not delivered to core code.
3288 */
3289 union wpa_event_data event;
3290 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3291 "failed");
3292 os_memset(&event, 0, sizeof(event));
3293 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3294 ETH_ALEN);
3295 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3296 &event);
3297 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003298 } else {
3299 wpa_printf(MSG_DEBUG,
3300 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003301 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003302
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003303fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304 nlmsg_free(msg);
3305 return ret;
3306}
3307
3308
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003309int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003310{
3311 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003312 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003313 int i;
3314
3315 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3316
3317 os_memset(&params, 0, sizeof(params));
3318 params.freq = drv->auth_freq;
3319 params.auth_alg = drv->auth_alg;
3320 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3321 params.local_state_change = drv->auth_local_state_change;
3322 params.p2p = drv->auth_p2p;
3323
3324 if (!is_zero_ether_addr(drv->auth_bssid_))
3325 params.bssid = drv->auth_bssid_;
3326
3327 if (drv->auth_ssid_len) {
3328 params.ssid = drv->auth_ssid;
3329 params.ssid_len = drv->auth_ssid_len;
3330 }
3331
3332 params.ie = drv->auth_ie;
3333 params.ie_len = drv->auth_ie_len;
3334
3335 for (i = 0; i < 4; i++) {
3336 if (drv->auth_wep_key_len[i]) {
3337 params.wep_key[i] = drv->auth_wep_key[i];
3338 params.wep_key_len[i] = drv->auth_wep_key_len[i];
3339 }
3340 }
3341
3342 drv->retry_auth = 1;
3343 return wpa_driver_nl80211_authenticate(bss, &params);
3344}
3345
3346
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003347static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
3348 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003349 int encrypt, int noack,
3350 unsigned int freq, int no_cck,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003351 int offchanok, unsigned int wait_time,
3352 const u16 *csa_offs,
3353 size_t csa_offs_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003354{
3355 struct wpa_driver_nl80211_data *drv = bss->drv;
3356 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003357 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003358
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003359 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3360 freq = nl80211_get_assoc_freq(drv);
3361 wpa_printf(MSG_DEBUG,
3362 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
3363 freq);
3364 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07003365 if (freq == 0) {
3366 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
3367 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003368 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003369 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003370
Dmitry Shmidt56052862013-10-04 10:23:25 -07003371 if (drv->use_monitor) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003372 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
Dmitry Shmidt56052862013-10-04 10:23:25 -07003373 freq, bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003374 return nl80211_send_monitor(drv, data, len, encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07003375 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003376
Dmitry Shmidt56052862013-10-04 10:23:25 -07003377 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003378 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003379 &cookie, no_cck, noack, offchanok,
3380 csa_offs, csa_offs_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07003381 if (res == 0 && !noack) {
3382 const struct ieee80211_mgmt *mgmt;
3383 u16 fc;
3384
3385 mgmt = (const struct ieee80211_mgmt *) data;
3386 fc = le_to_host16(mgmt->frame_control);
3387 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3388 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
3389 wpa_printf(MSG_MSGDUMP,
3390 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
3391 (long long unsigned int)
3392 drv->send_action_cookie,
3393 (long long unsigned int) cookie);
3394 drv->send_action_cookie = cookie;
3395 }
3396 }
3397
3398 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003399}
3400
3401
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003402static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3403 size_t data_len, int noack,
3404 unsigned int freq, int no_cck,
3405 int offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003406 unsigned int wait_time,
3407 const u16 *csa_offs,
3408 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003409{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003410 struct wpa_driver_nl80211_data *drv = bss->drv;
3411 struct ieee80211_mgmt *mgmt;
3412 int encrypt = 1;
3413 u16 fc;
3414
3415 mgmt = (struct ieee80211_mgmt *) data;
3416 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003417 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3418 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3419 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3420 fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003421
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003422 if ((is_sta_interface(drv->nlmode) ||
3423 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003424 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3425 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3426 /*
3427 * The use of last_mgmt_freq is a bit of a hack,
3428 * but it works due to the single-threaded nature
3429 * of wpa_supplicant.
3430 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07003431 if (freq == 0) {
3432 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3433 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003434 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003435 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003436 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003437 data, data_len, NULL, 1, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003438 1, csa_offs, csa_offs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003439 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003440
3441 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07003442 if (freq == 0) {
3443 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3444 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003445 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003446 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003447 return nl80211_send_frame_cmd(bss, freq,
3448 (int) freq == bss->freq ? 0 :
3449 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003450 data, data_len,
3451 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003452 no_cck, noack, offchanok,
3453 csa_offs, csa_offs_len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003454 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07003455
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003456 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3457 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3458 /*
3459 * Only one of the authentication frame types is encrypted.
3460 * In order for static WEP encryption to work properly (i.e.,
3461 * to not encrypt the frame), we need to tell mac80211 about
3462 * the frames that must not be encrypted.
3463 */
3464 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3465 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3466 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3467 encrypt = 0;
3468 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003469
Dmitry Shmidt56052862013-10-04 10:23:25 -07003470 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003471 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003472 noack, freq, no_cck, offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003473 wait_time, csa_offs,
3474 csa_offs_len);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003475}
3476
3477
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003478static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3479{
3480 u8 rates[NL80211_MAX_SUPP_RATES];
3481 u8 rates_len = 0;
3482 int i;
3483
3484 if (!basic_rates)
3485 return 0;
3486
3487 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3488 rates[rates_len++] = basic_rates[i] / 5;
3489
3490 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3491}
3492
3493
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003494static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3495 int slot, int ht_opmode, int ap_isolate,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003496 const int *basic_rates)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003497{
3498 struct wpa_driver_nl80211_data *drv = bss->drv;
3499 struct nl_msg *msg;
3500
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003501 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3502 (cts >= 0 &&
3503 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3504 (preamble >= 0 &&
3505 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3506 (slot >= 0 &&
3507 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3508 (ht_opmode >= 0 &&
3509 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3510 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003511 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3512 nl80211_put_basic_rates(msg, basic_rates)) {
3513 nlmsg_free(msg);
3514 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003515 }
3516
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003517 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003518}
3519
3520
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003521static int wpa_driver_nl80211_set_acl(void *priv,
3522 struct hostapd_acl_params *params)
3523{
3524 struct i802_bss *bss = priv;
3525 struct wpa_driver_nl80211_data *drv = bss->drv;
3526 struct nl_msg *msg;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003527 struct nl_msg *acl;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003528 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003529 int ret;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003530
3531 if (!(drv->capa.max_acl_mac_addrs))
3532 return -ENOTSUP;
3533
3534 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3535 return -ENOTSUP;
3536
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003537 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3538 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3539
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003540 acl = nlmsg_alloc();
3541 if (!acl)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003542 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003543 for (i = 0; i < params->num_mac_acl; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003544 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3545 nlmsg_free(acl);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003546 return -ENOMEM;
3547 }
3548 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003549
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003550 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
3551 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3552 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3553 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3554 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
3555 nlmsg_free(msg);
3556 nlmsg_free(acl);
3557 return -ENOMEM;
3558 }
3559 nlmsg_free(acl);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003560
3561 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003562 if (ret) {
3563 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3564 ret, strerror(-ret));
3565 }
3566
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003567 return ret;
3568}
3569
3570
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003571static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3572{
3573 if (beacon_int > 0) {
3574 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
3575 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3576 beacon_int);
3577 }
3578
3579 return 0;
3580}
3581
3582
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003583static int nl80211_put_dtim_period(struct nl_msg *msg, int dtim_period)
3584{
3585 if (dtim_period > 0) {
3586 wpa_printf(MSG_DEBUG, " * dtim_period=%d", dtim_period);
3587 return nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
3588 }
3589
3590 return 0;
3591}
3592
3593
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07003594#ifdef CONFIG_MESH
3595static int nl80211_set_mesh_config(void *priv,
3596 struct wpa_driver_mesh_bss_params *params)
3597{
3598 struct i802_bss *bss = priv;
3599 struct wpa_driver_nl80211_data *drv = bss->drv;
3600 struct nl_msg *msg;
3601 int ret;
3602
3603 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MESH_CONFIG);
3604 if (!msg)
3605 return -1;
3606
3607 ret = nl80211_put_mesh_config(msg, params);
3608 if (ret < 0) {
3609 nlmsg_free(msg);
3610 return ret;
3611 }
3612
3613 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3614 if (ret) {
3615 wpa_printf(MSG_ERROR,
3616 "nl80211: Mesh config set failed: %d (%s)",
3617 ret, strerror(-ret));
3618 return ret;
3619 }
3620 return 0;
3621}
3622#endif /* CONFIG_MESH */
3623
3624
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003625static int nl80211_put_beacon_rate(struct nl_msg *msg, const u64 flags,
3626 struct wpa_driver_ap_params *params)
3627{
3628 struct nlattr *bands, *band;
3629 struct nl80211_txrate_vht vht_rate;
3630
3631 if (!params->freq ||
3632 (params->beacon_rate == 0 &&
3633 params->rate_type == BEACON_RATE_LEGACY))
3634 return 0;
3635
3636 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
3637 if (!bands)
3638 return -1;
3639
3640 switch (params->freq->mode) {
3641 case HOSTAPD_MODE_IEEE80211B:
3642 case HOSTAPD_MODE_IEEE80211G:
3643 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
3644 break;
3645 case HOSTAPD_MODE_IEEE80211A:
3646 band = nla_nest_start(msg, NL80211_BAND_5GHZ);
3647 break;
3648 case HOSTAPD_MODE_IEEE80211AD:
3649 band = nla_nest_start(msg, NL80211_BAND_60GHZ);
3650 break;
3651 default:
3652 return 0;
3653 }
3654
3655 if (!band)
3656 return -1;
3657
3658 os_memset(&vht_rate, 0, sizeof(vht_rate));
3659
3660 switch (params->rate_type) {
3661 case BEACON_RATE_LEGACY:
3662 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY)) {
3663 wpa_printf(MSG_INFO,
3664 "nl80211: Driver does not support setting Beacon frame rate (legacy)");
3665 return -1;
3666 }
3667
3668 if (nla_put_u8(msg, NL80211_TXRATE_LEGACY,
3669 (u8) params->beacon_rate / 5) ||
3670 nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
3671 (params->freq->vht_enabled &&
3672 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3673 &vht_rate)))
3674 return -1;
3675
3676 wpa_printf(MSG_DEBUG, " * beacon_rate = legacy:%u (* 100 kbps)",
3677 params->beacon_rate);
3678 break;
3679 case BEACON_RATE_HT:
3680 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_HT)) {
3681 wpa_printf(MSG_INFO,
3682 "nl80211: Driver does not support setting Beacon frame rate (HT)");
3683 return -1;
3684 }
3685 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
3686 nla_put_u8(msg, NL80211_TXRATE_HT, params->beacon_rate) ||
3687 (params->freq->vht_enabled &&
3688 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3689 &vht_rate)))
3690 return -1;
3691 wpa_printf(MSG_DEBUG, " * beacon_rate = HT-MCS %u",
3692 params->beacon_rate);
3693 break;
3694 case BEACON_RATE_VHT:
3695 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_VHT)) {
3696 wpa_printf(MSG_INFO,
3697 "nl80211: Driver does not support setting Beacon frame rate (VHT)");
3698 return -1;
3699 }
3700 vht_rate.mcs[0] = BIT(params->beacon_rate);
3701 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL))
3702 return -1;
3703 if (nla_put(msg, NL80211_TXRATE_HT, 0, NULL))
3704 return -1;
3705 if (nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3706 &vht_rate))
3707 return -1;
3708 wpa_printf(MSG_DEBUG, " * beacon_rate = VHT-MCS %u",
3709 params->beacon_rate);
3710 break;
3711 }
3712
3713 nla_nest_end(msg, band);
3714 nla_nest_end(msg, bands);
3715
3716 return 0;
3717}
3718
3719
3720static int nl80211_set_multicast_to_unicast(struct i802_bss *bss,
3721 int multicast_to_unicast)
3722{
3723 struct wpa_driver_nl80211_data *drv = bss->drv;
3724 struct nl_msg *msg;
3725 int ret;
3726
3727 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_MULTICAST_TO_UNICAST);
3728 if (!msg ||
3729 (multicast_to_unicast &&
3730 nla_put_flag(msg, NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED))) {
3731 wpa_printf(MSG_ERROR,
3732 "nl80211: Failed to build NL80211_CMD_SET_MULTICAST_TO_UNICAST msg for %s",
3733 bss->ifname);
3734 nlmsg_free(msg);
3735 return -ENOBUFS;
3736 }
3737
3738 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3739
3740 switch (ret) {
3741 case 0:
3742 wpa_printf(MSG_DEBUG,
3743 "nl80211: multicast to unicast %s on interface %s",
3744 multicast_to_unicast ? "enabled" : "disabled",
3745 bss->ifname);
3746 break;
3747 case -EOPNOTSUPP:
3748 if (!multicast_to_unicast)
3749 break;
3750 wpa_printf(MSG_INFO,
3751 "nl80211: multicast to unicast not supported on interface %s",
3752 bss->ifname);
3753 break;
3754 default:
3755 wpa_printf(MSG_ERROR,
3756 "nl80211: %s multicast to unicast failed with %d (%s) on interface %s",
3757 multicast_to_unicast ? "enabling" : "disabling",
3758 ret, strerror(-ret), bss->ifname);
3759 break;
3760 }
3761
3762 return ret;
3763}
3764
3765
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003766static int wpa_driver_nl80211_set_ap(void *priv,
3767 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003768{
3769 struct i802_bss *bss = priv;
3770 struct wpa_driver_nl80211_data *drv = bss->drv;
3771 struct nl_msg *msg;
3772 u8 cmd = NL80211_CMD_NEW_BEACON;
3773 int ret;
3774 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003775 int num_suites;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003776 int smps_mode;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003777 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003778 u32 ver;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07003779#ifdef CONFIG_MESH
3780 struct wpa_driver_mesh_bss_params mesh_params;
3781#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003782
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003783 beacon_set = params->reenable ? 0 : bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003785 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
3786 beacon_set);
3787 if (beacon_set)
3788 cmd = NL80211_CMD_SET_BEACON;
Paul Stewart092955c2017-02-06 09:13:09 -08003789 else if (!drv->device_ap_sme && !drv->use_monitor &&
3790 !nl80211_get_wiphy_data_ap(bss))
3791 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003792
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003793 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3794 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003795 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3796 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003797 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003798 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003799 wpa_printf(MSG_DEBUG, "nl80211: beacon_rate=%u", params->beacon_rate);
3800 wpa_printf(MSG_DEBUG, "nl80211: rate_type=%d", params->rate_type);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003801 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003802 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3803 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003804 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
3805 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3806 params->head) ||
3807 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3808 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003809 nl80211_put_beacon_int(msg, params->beacon_int) ||
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003810 nl80211_put_beacon_rate(msg, drv->capa.flags, params) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003811 nl80211_put_dtim_period(msg, params->dtim_period) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003812 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3813 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003814 if (params->proberesp && params->proberesp_len) {
3815 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3816 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003817 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3818 params->proberesp))
3819 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003820 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003821 switch (params->hide_ssid) {
3822 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003823 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003824 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3825 NL80211_HIDDEN_SSID_NOT_IN_USE))
3826 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003827 break;
3828 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003829 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003830 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3831 NL80211_HIDDEN_SSID_ZERO_LEN))
3832 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003833 break;
3834 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003835 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003836 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3837 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3838 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003839 break;
3840 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003841 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003842 if (params->privacy &&
3843 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3844 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003845 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003846 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3847 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3848 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003849 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3850 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3851 NL80211_AUTHTYPE_SHARED_KEY))
3852 goto fail;
3853 } else {
3854 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3855 NL80211_AUTHTYPE_OPEN_SYSTEM))
3856 goto fail;
3857 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003858
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003859 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003860 ver = 0;
3861 if (params->wpa_version & WPA_PROTO_WPA)
3862 ver |= NL80211_WPA_VERSION_1;
3863 if (params->wpa_version & WPA_PROTO_RSN)
3864 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003865 if (ver &&
3866 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3867 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003868
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003869 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3870 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003871 num_suites = 0;
3872 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
Paul Stewart092955c2017-02-06 09:13:09 -08003873 suites[num_suites++] = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003874 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
Paul Stewart092955c2017-02-06 09:13:09 -08003875 suites[num_suites++] = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003876 if (num_suites &&
3877 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
3878 suites))
3879 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003880
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003881 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07003882 (!params->pairwise_ciphers ||
3883 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) &&
3884 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
3885 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003886 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003887
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003888 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
3889 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003890 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
3891 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003892 if (num_suites &&
3893 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
3894 num_suites * sizeof(u32), suites))
3895 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003896
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003897 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
3898 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003899 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003900 if (suite &&
3901 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
3902 goto fail;
3903
Dmitry Shmidte4663042016-04-04 10:07:49 -07003904 if (params->ht_opmode != -1) {
3905 switch (params->smps_mode) {
3906 case HT_CAP_INFO_SMPS_DYNAMIC:
3907 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
3908 smps_mode = NL80211_SMPS_DYNAMIC;
3909 break;
3910 case HT_CAP_INFO_SMPS_STATIC:
3911 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
3912 smps_mode = NL80211_SMPS_STATIC;
3913 break;
3914 default:
3915 /* invalid - fallback to smps off */
3916 case HT_CAP_INFO_SMPS_DISABLED:
3917 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
3918 smps_mode = NL80211_SMPS_OFF;
3919 break;
3920 }
3921 if (nla_put_u32(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
3922 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003923 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003924
3925 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003926 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
3927 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003928 if (nla_put(msg, NL80211_ATTR_IE,
3929 wpabuf_len(params->beacon_ies),
3930 wpabuf_head(params->beacon_ies)))
3931 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003932 }
3933 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003934 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
3935 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003936 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
3937 wpabuf_len(params->proberesp_ies),
3938 wpabuf_head(params->proberesp_ies)))
3939 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003940 }
3941 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003942 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
3943 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003944 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
3945 wpabuf_len(params->assocresp_ies),
3946 wpabuf_head(params->assocresp_ies)))
3947 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003948 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003949
Dmitry Shmidt04949592012-07-19 12:16:46 -07003950 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003951 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
3952 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003953 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
3954 params->ap_max_inactivity))
3955 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003956 }
3957
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003958#ifdef CONFIG_P2P
3959 if (params->p2p_go_ctwindow > 0) {
3960 if (drv->p2p_go_ctwindow_supported) {
3961 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
3962 params->p2p_go_ctwindow);
3963 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
3964 params->p2p_go_ctwindow))
3965 goto fail;
3966 } else {
3967 wpa_printf(MSG_INFO,
3968 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
3969 }
3970 }
3971#endif /* CONFIG_P2P */
3972
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003973 if (params->pbss) {
3974 wpa_printf(MSG_DEBUG, "nl80211: PBSS");
3975 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
3976 goto fail;
3977 }
3978
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003979 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3980 if (ret) {
3981 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
3982 ret, strerror(-ret));
3983 } else {
3984 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003985 nl80211_set_bss(bss, params->cts_protect, params->preamble,
3986 params->short_slot_time, params->ht_opmode,
3987 params->isolate, params->basic_rates);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003988 nl80211_set_multicast_to_unicast(bss,
3989 params->multicast_to_unicast);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003990 if (beacon_set && params->freq &&
3991 params->freq->bandwidth != bss->bandwidth) {
3992 wpa_printf(MSG_DEBUG,
3993 "nl80211: Update BSS %s bandwidth: %d -> %d",
3994 bss->ifname, bss->bandwidth,
3995 params->freq->bandwidth);
3996 ret = nl80211_set_channel(bss, params->freq, 1);
3997 if (ret) {
3998 wpa_printf(MSG_DEBUG,
3999 "nl80211: Frequency set failed: %d (%s)",
4000 ret, strerror(-ret));
4001 } else {
4002 wpa_printf(MSG_DEBUG,
4003 "nl80211: Frequency set succeeded for ht2040 coex");
4004 bss->bandwidth = params->freq->bandwidth;
4005 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004006 } else if (!beacon_set && params->freq) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004007 /*
4008 * cfg80211 updates the driver on frequence change in AP
4009 * mode only at the point when beaconing is started, so
4010 * set the initial value here.
4011 */
4012 bss->bandwidth = params->freq->bandwidth;
4013 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004014 }
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004015
4016#ifdef CONFIG_MESH
4017 if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
4018 os_memset(&mesh_params, 0, sizeof(mesh_params));
4019 mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
4020 mesh_params.ht_opmode = params->ht_opmode;
4021 ret = nl80211_set_mesh_config(priv, &mesh_params);
4022 if (ret < 0)
4023 return ret;
4024 }
4025#endif /* CONFIG_MESH */
4026
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004027 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004028fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004029 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004030 return -ENOBUFS;
4031}
4032
4033
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004034static int nl80211_put_freq_params(struct nl_msg *msg,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004035 const struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004036{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004037 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004038 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
4039 return -ENOBUFS;
4040
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004041 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
4042 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
4043
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004044 if (freq->vht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004045 enum nl80211_chan_width cw;
4046
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004047 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004048 switch (freq->bandwidth) {
4049 case 20:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004050 cw = NL80211_CHAN_WIDTH_20;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004051 break;
4052 case 40:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004053 cw = NL80211_CHAN_WIDTH_40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004054 break;
4055 case 80:
4056 if (freq->center_freq2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004057 cw = NL80211_CHAN_WIDTH_80P80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004058 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004059 cw = NL80211_CHAN_WIDTH_80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004060 break;
4061 case 160:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004062 cw = NL80211_CHAN_WIDTH_160;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004063 break;
4064 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004065 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004066 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004067
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004068 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
4069 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
4070 freq->center_freq1);
4071 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
4072 freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004073 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
4074 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
4075 freq->center_freq1) ||
4076 (freq->center_freq2 &&
4077 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
4078 freq->center_freq2)))
4079 return -ENOBUFS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004080 } else if (freq->ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004081 enum nl80211_channel_type ct;
4082
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004083 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
4084 freq->sec_channel_offset);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004085 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004086 case -1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004087 ct = NL80211_CHAN_HT40MINUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088 break;
4089 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004090 ct = NL80211_CHAN_HT40PLUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091 break;
4092 default:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004093 ct = NL80211_CHAN_HT20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094 break;
4095 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004096
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004097 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004098 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
4099 return -ENOBUFS;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004100 } else {
4101 wpa_printf(MSG_DEBUG, " * channel_type=%d",
4102 NL80211_CHAN_NO_HT);
4103 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4104 NL80211_CHAN_NO_HT))
4105 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004106 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004107 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004108}
4109
4110
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004111static int nl80211_set_channel(struct i802_bss *bss,
4112 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004113{
4114 struct wpa_driver_nl80211_data *drv = bss->drv;
4115 struct nl_msg *msg;
4116 int ret;
4117
4118 wpa_printf(MSG_DEBUG,
4119 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
4120 freq->freq, freq->ht_enabled, freq->vht_enabled,
4121 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004122
4123 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
4124 NL80211_CMD_SET_WIPHY);
4125 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
4126 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004127 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004128 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004129
4130 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004131 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004132 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004133 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004134 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004135 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004136 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004137 return -1;
4138}
4139
4140
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004141static u32 sta_flags_nl80211(int flags)
4142{
4143 u32 f = 0;
4144
4145 if (flags & WPA_STA_AUTHORIZED)
4146 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
4147 if (flags & WPA_STA_WMM)
4148 f |= BIT(NL80211_STA_FLAG_WME);
4149 if (flags & WPA_STA_SHORT_PREAMBLE)
4150 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
4151 if (flags & WPA_STA_MFP)
4152 f |= BIT(NL80211_STA_FLAG_MFP);
4153 if (flags & WPA_STA_TDLS_PEER)
4154 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004155 if (flags & WPA_STA_AUTHENTICATED)
4156 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004157 if (flags & WPA_STA_ASSOCIATED)
4158 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004159
4160 return f;
4161}
4162
4163
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004164#ifdef CONFIG_MESH
4165static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
4166{
4167 switch (state) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004168 case PLINK_IDLE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004169 return NL80211_PLINK_LISTEN;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004170 case PLINK_OPN_SNT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004171 return NL80211_PLINK_OPN_SNT;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004172 case PLINK_OPN_RCVD:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004173 return NL80211_PLINK_OPN_RCVD;
4174 case PLINK_CNF_RCVD:
4175 return NL80211_PLINK_CNF_RCVD;
4176 case PLINK_ESTAB:
4177 return NL80211_PLINK_ESTAB;
4178 case PLINK_HOLDING:
4179 return NL80211_PLINK_HOLDING;
4180 case PLINK_BLOCKED:
4181 return NL80211_PLINK_BLOCKED;
4182 default:
4183 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
4184 state);
4185 }
4186 return -1;
4187}
4188#endif /* CONFIG_MESH */
4189
4190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004191static int wpa_driver_nl80211_sta_add(void *priv,
4192 struct hostapd_sta_add_params *params)
4193{
4194 struct i802_bss *bss = priv;
4195 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004196 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004197 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004198 int ret = -ENOBUFS;
4199
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004200 if ((params->flags & WPA_STA_TDLS_PEER) &&
4201 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
4202 return -EOPNOTSUPP;
4203
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004204 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
4205 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004206 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
4207 NL80211_CMD_NEW_STATION);
4208 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
4209 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004210
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004211 /*
4212 * Set the below properties only in one of the following cases:
4213 * 1. New station is added, already associated.
4214 * 2. Set WPA_STA_TDLS_PEER station.
4215 * 3. Set an already added unassociated station, if driver supports
4216 * full AP client state. (Set these properties after station became
4217 * associated will be rejected by the driver).
4218 */
4219 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
4220 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4221 (params->flags & WPA_STA_ASSOCIATED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004222 wpa_hexdump(MSG_DEBUG, " * supported rates",
4223 params->supp_rates, params->supp_rates_len);
4224 wpa_printf(MSG_DEBUG, " * capability=0x%x",
4225 params->capability);
4226 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
4227 params->supp_rates_len, params->supp_rates) ||
4228 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
4229 params->capability))
4230 goto fail;
4231
4232 if (params->ht_capabilities) {
4233 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
4234 (u8 *) params->ht_capabilities,
4235 sizeof(*params->ht_capabilities));
4236 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
4237 sizeof(*params->ht_capabilities),
4238 params->ht_capabilities))
4239 goto fail;
4240 }
4241
4242 if (params->vht_capabilities) {
4243 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
4244 (u8 *) params->vht_capabilities,
4245 sizeof(*params->vht_capabilities));
4246 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
4247 sizeof(*params->vht_capabilities),
4248 params->vht_capabilities))
4249 goto fail;
4250 }
4251
4252 if (params->ext_capab) {
4253 wpa_hexdump(MSG_DEBUG, " * ext_capab",
4254 params->ext_capab, params->ext_capab_len);
4255 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
4256 params->ext_capab_len, params->ext_capab))
4257 goto fail;
4258 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004259
4260 if (is_ap_interface(drv->nlmode) &&
4261 nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
4262 params->support_p2p_ps ?
4263 NL80211_P2P_PS_SUPPORTED :
4264 NL80211_P2P_PS_UNSUPPORTED))
4265 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004266 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004267 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004268 if (params->aid) {
4269 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004270 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
4271 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004272 } else {
4273 /*
4274 * cfg80211 validates that AID is non-zero, so we have
4275 * to make this a non-zero value for the TDLS case where
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004276 * a dummy STA entry is used for now and for a station
4277 * that is still not associated.
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004278 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004279 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
4280 (params->flags & WPA_STA_TDLS_PEER) ?
4281 "TDLS" : "UNASSOC_STA");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004282 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
4283 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004284 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004285 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4286 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004287 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4288 params->listen_interval))
4289 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004290 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
4291 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004292 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
4293 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004294 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4295 (params->flags & WPA_STA_ASSOCIATED)) {
4296 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
4297 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4298 params->listen_interval);
4299 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
4300 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4301 params->listen_interval))
4302 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004303 }
4304
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08004305 if (params->vht_opmode_enabled) {
4306 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004307 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
4308 params->vht_opmode))
4309 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004310 }
4311
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004312 if (params->supp_channels) {
4313 wpa_hexdump(MSG_DEBUG, " * supported channels",
4314 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004315 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
4316 params->supp_channels_len, params->supp_channels))
4317 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004318 }
4319
4320 if (params->supp_oper_classes) {
4321 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
4322 params->supp_oper_classes,
4323 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004324 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
4325 params->supp_oper_classes_len,
4326 params->supp_oper_classes))
4327 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004328 }
4329
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004330 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004331 upd.set = sta_flags_nl80211(params->flags);
4332 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004333
4334 /*
4335 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
4336 * flags, as nl80211 driver moves a new station, by default, into
4337 * associated state.
4338 *
4339 * On the other hand, if the driver supports that feature and the
4340 * station is added in unauthenticated state, set the
4341 * authenticated/associated bits in the mask to prevent moving this
4342 * station to associated state before it is actually associated.
4343 *
4344 * This is irrelevant for mesh mode where the station is added to the
4345 * driver as authenticated already, and ASSOCIATED isn't part of the
4346 * nl80211 API.
4347 */
4348 if (!is_mesh_interface(drv->nlmode)) {
4349 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
4350 wpa_printf(MSG_DEBUG,
4351 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
4352 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
4353 BIT(NL80211_STA_FLAG_AUTHENTICATED));
4354 } else if (!params->set &&
4355 !(params->flags & WPA_STA_TDLS_PEER)) {
4356 if (!(params->flags & WPA_STA_AUTHENTICATED))
4357 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
4358 if (!(params->flags & WPA_STA_ASSOCIATED))
4359 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
4360 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004361#ifdef CONFIG_MESH
4362 } else {
4363 if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
4364 ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
4365 params->peer_aid);
4366 if (ret)
4367 goto fail;
4368 }
4369#endif /* CONFIG_MESH */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004370 }
4371
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004372 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
4373 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004374 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4375 goto fail;
4376
4377#ifdef CONFIG_MESH
4378 if (params->plink_state &&
4379 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
4380 sta_plink_state_nl80211(params->plink_state)))
4381 goto fail;
4382#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004383
4384 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004385 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
4386
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004387 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004388 if (!wme ||
4389 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
4390 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
4391 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
4392 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
4393 WMM_QOSINFO_STA_SP_MASK))
4394 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004395 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004396 }
4397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004398 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004399 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004400 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004401 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
4402 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
4403 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404 if (ret == -EEXIST)
4405 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004406fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004407 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004408 return ret;
4409}
4410
4411
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004412static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
4413{
4414#ifdef CONFIG_LIBNL3_ROUTE
4415 struct wpa_driver_nl80211_data *drv = bss->drv;
4416 struct rtnl_neigh *rn;
4417 struct nl_addr *nl_addr;
4418 int err;
4419
4420 rn = rtnl_neigh_alloc();
4421 if (!rn)
4422 return;
4423
4424 rtnl_neigh_set_family(rn, AF_BRIDGE);
4425 rtnl_neigh_set_ifindex(rn, bss->ifindex);
4426 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
4427 if (!nl_addr) {
4428 rtnl_neigh_put(rn);
4429 return;
4430 }
4431 rtnl_neigh_set_lladdr(rn, nl_addr);
4432
4433 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
4434 if (err < 0) {
4435 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
4436 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
4437 bss->ifindex, nl_geterror(err));
4438 } else {
4439 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
4440 MACSTR, MAC2STR(addr));
4441 }
4442
4443 nl_addr_put(nl_addr);
4444 rtnl_neigh_put(rn);
4445#endif /* CONFIG_LIBNL3_ROUTE */
4446}
4447
4448
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004449static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
4450 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004451{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004452 struct wpa_driver_nl80211_data *drv = bss->drv;
4453 struct nl_msg *msg;
4454 int ret;
4455
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004456 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
4457 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
4458 (deauth == 0 &&
4459 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4460 WLAN_FC_STYPE_DISASSOC)) ||
4461 (deauth == 1 &&
4462 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4463 WLAN_FC_STYPE_DEAUTH)) ||
4464 (reason_code &&
4465 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
4466 nlmsg_free(msg);
4467 return -ENOBUFS;
4468 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004469
4470 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004471 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
4472 " --> %d (%s)",
4473 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004474
4475 if (drv->rtnl_sk)
4476 rtnl_neigh_delete_fdb_entry(bss, addr);
4477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004478 if (ret == -ENOENT)
4479 return 0;
4480 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004481}
4482
4483
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004484void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004485{
4486 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004487 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004488
4489 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4490
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004491 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004492 dl_list_for_each(drv2, &drv->global->interfaces,
4493 struct wpa_driver_nl80211_data, list)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004494 {
4495 del_ifidx(drv2, ifidx, IFIDX_ANY);
4496 /* Remove all bridges learned for this iface */
4497 del_ifidx(drv2, IFIDX_ANY, ifidx);
4498 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004499
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004500 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004501 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4502 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004503 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
4504}
4505
4506
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004507const char * nl80211_iftype_str(enum nl80211_iftype mode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004508{
4509 switch (mode) {
4510 case NL80211_IFTYPE_ADHOC:
4511 return "ADHOC";
4512 case NL80211_IFTYPE_STATION:
4513 return "STATION";
4514 case NL80211_IFTYPE_AP:
4515 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004516 case NL80211_IFTYPE_AP_VLAN:
4517 return "AP_VLAN";
4518 case NL80211_IFTYPE_WDS:
4519 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004520 case NL80211_IFTYPE_MONITOR:
4521 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004522 case NL80211_IFTYPE_MESH_POINT:
4523 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004524 case NL80211_IFTYPE_P2P_CLIENT:
4525 return "P2P_CLIENT";
4526 case NL80211_IFTYPE_P2P_GO:
4527 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004528 case NL80211_IFTYPE_P2P_DEVICE:
4529 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004530 default:
4531 return "unknown";
4532 }
4533}
4534
4535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004536static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4537 const char *ifname,
4538 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004539 const u8 *addr, int wds,
4540 int (*handler)(struct nl_msg *, void *),
4541 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004543 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004544 int ifidx;
4545 int ret = -ENOBUFS;
4546
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004547 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4548 iftype, nl80211_iftype_str(iftype));
4549
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004550 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
4551 if (!msg ||
4552 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
4553 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
4554 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004555
4556 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004557 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004558
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004559 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004560 if (!flags ||
4561 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
4562 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004563
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004564 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004565 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004566 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
4567 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004568 }
4569
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004570 /*
4571 * Tell cfg80211 that the interface belongs to the socket that created
4572 * it, and the interface should be deleted when the socket is closed.
4573 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004574 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4575 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004576
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004577 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004578 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004579 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004580 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004581 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004582 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4583 ifname, ret, strerror(-ret));
4584 return ret;
4585 }
4586
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004587 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
4588 return 0;
4589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004590 ifidx = if_nametoindex(ifname);
4591 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4592 ifname, ifidx);
4593
4594 if (ifidx <= 0)
4595 return -1;
4596
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004597 /*
4598 * Some virtual interfaces need to process EAPOL packets and events on
4599 * the parent interface. This is used mainly with hostapd.
4600 */
4601 if (drv->hostapd ||
4602 iftype == NL80211_IFTYPE_AP_VLAN ||
4603 iftype == NL80211_IFTYPE_WDS ||
4604 iftype == NL80211_IFTYPE_MONITOR) {
4605 /* start listening for EAPOL on this interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004606 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004607 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004608
4609 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004610 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611 nl80211_remove_iface(drv, ifidx);
4612 return -1;
4613 }
4614
4615 return ifidx;
4616}
4617
4618
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004619int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4620 const char *ifname, enum nl80211_iftype iftype,
4621 const u8 *addr, int wds,
4622 int (*handler)(struct nl_msg *, void *),
4623 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004624{
4625 int ret;
4626
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004627 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4628 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004629
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004630 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004631 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004632 if (use_existing) {
4633 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4634 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004635 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4636 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4637 addr) < 0 &&
4638 (linux_set_iface_flags(drv->global->ioctl_sock,
4639 ifname, 0) < 0 ||
4640 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4641 addr) < 0 ||
4642 linux_set_iface_flags(drv->global->ioctl_sock,
4643 ifname, 1) < 0))
4644 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004645 return -ENFILE;
4646 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4648
4649 /* Try to remove the interface that was already there. */
4650 nl80211_remove_iface(drv, if_nametoindex(ifname));
4651
4652 /* Try to create the interface again */
4653 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004654 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004655 }
4656
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004657 if (ret >= 0 && is_p2p_net_interface(iftype)) {
4658 wpa_printf(MSG_DEBUG,
4659 "nl80211: Interface %s created for P2P - disable 11b rates",
4660 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004661 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004662 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004663
4664 return ret;
4665}
4666
4667
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004668static int nl80211_setup_ap(struct i802_bss *bss)
4669{
4670 struct wpa_driver_nl80211_data *drv = bss->drv;
4671
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004672 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4673 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004674
4675 /*
4676 * Disable Probe Request reporting unless we need it in this way for
4677 * devices that include the AP SME, in the other case (unless using
4678 * monitor iface) we'll get it through the nl_mgmt socket instead.
4679 */
4680 if (!drv->device_ap_sme)
4681 wpa_driver_nl80211_probe_req_report(bss, 0);
4682
4683 if (!drv->device_ap_sme && !drv->use_monitor)
4684 if (nl80211_mgmt_subscribe_ap(bss))
4685 return -1;
4686
4687 if (drv->device_ap_sme && !drv->use_monitor)
4688 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004689 wpa_printf(MSG_DEBUG,
4690 "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004691
4692 if (!drv->device_ap_sme && drv->use_monitor &&
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004693 nl80211_create_monitor_interface(drv) &&
4694 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004695 return -1;
4696
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004697 if (drv->device_ap_sme &&
4698 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4699 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4700 "Probe Request frame reporting in AP mode");
4701 /* Try to survive without this */
4702 }
4703
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004704 return 0;
4705}
4706
4707
4708static void nl80211_teardown_ap(struct i802_bss *bss)
4709{
4710 struct wpa_driver_nl80211_data *drv = bss->drv;
4711
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004712 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4713 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004714 if (drv->device_ap_sme) {
4715 wpa_driver_nl80211_probe_req_report(bss, 0);
4716 if (!drv->use_monitor)
4717 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4718 } else if (drv->use_monitor)
4719 nl80211_remove_monitor_interface(drv);
4720 else
4721 nl80211_mgmt_unsubscribe(bss, "AP teardown");
4722
Paul Stewart092955c2017-02-06 09:13:09 -08004723 nl80211_put_wiphy_data_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004724 bss->beacon_set = 0;
4725}
4726
4727
4728static int nl80211_send_eapol_data(struct i802_bss *bss,
4729 const u8 *addr, const u8 *data,
4730 size_t data_len)
4731{
4732 struct sockaddr_ll ll;
4733 int ret;
4734
4735 if (bss->drv->eapol_tx_sock < 0) {
4736 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
4737 return -1;
4738 }
4739
4740 os_memset(&ll, 0, sizeof(ll));
4741 ll.sll_family = AF_PACKET;
4742 ll.sll_ifindex = bss->ifindex;
4743 ll.sll_protocol = htons(ETH_P_PAE);
4744 ll.sll_halen = ETH_ALEN;
4745 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4746 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4747 (struct sockaddr *) &ll, sizeof(ll));
4748 if (ret < 0)
4749 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4750 strerror(errno));
4751
4752 return ret;
4753}
4754
4755
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4757
4758static int wpa_driver_nl80211_hapd_send_eapol(
4759 void *priv, const u8 *addr, const u8 *data,
4760 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
4761{
4762 struct i802_bss *bss = priv;
4763 struct wpa_driver_nl80211_data *drv = bss->drv;
4764 struct ieee80211_hdr *hdr;
4765 size_t len;
4766 u8 *pos;
4767 int res;
4768 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08004769
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004770 if (drv->device_ap_sme || !drv->use_monitor)
4771 return nl80211_send_eapol_data(bss, addr, data, data_len);
4772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004773 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4774 data_len;
4775 hdr = os_zalloc(len);
4776 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004777 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4778 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004779 return -1;
4780 }
4781
4782 hdr->frame_control =
4783 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4784 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4785 if (encrypt)
4786 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4787 if (qos) {
4788 hdr->frame_control |=
4789 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4790 }
4791
4792 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4793 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4794 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4795 pos = (u8 *) (hdr + 1);
4796
4797 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004798 /* Set highest priority in QoS header */
4799 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004800 pos[1] = 0;
4801 pos += 2;
4802 }
4803
4804 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4805 pos += sizeof(rfc1042_header);
4806 WPA_PUT_BE16(pos, ETH_P_PAE);
4807 pos += 2;
4808 memcpy(pos, data, data_len);
4809
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004810 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004811 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004812 if (res < 0) {
4813 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4814 "failed: %d (%s)",
4815 (unsigned long) len, errno, strerror(errno));
4816 }
4817 os_free(hdr);
4818
4819 return res;
4820}
4821
4822
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004823static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004824 unsigned int total_flags,
4825 unsigned int flags_or,
4826 unsigned int flags_and)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004827{
4828 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004829 struct nl_msg *msg;
4830 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004831 struct nl80211_sta_flag_update upd;
4832
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004833 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4834 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4835 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4836 !!(total_flags & WPA_STA_AUTHORIZED));
4837
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004838 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4839 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4840 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004841
4842 /*
4843 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4844 * can be removed eventually.
4845 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004846 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004847 if (!flags ||
4848 ((total_flags & WPA_STA_AUTHORIZED) &&
4849 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4850 ((total_flags & WPA_STA_WMM) &&
4851 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4852 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4853 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4854 ((total_flags & WPA_STA_MFP) &&
4855 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4856 ((total_flags & WPA_STA_TDLS_PEER) &&
4857 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4858 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004859
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004860 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004861
4862 os_memset(&upd, 0, sizeof(upd));
4863 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4864 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004865 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4866 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004868 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
4869fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004870 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004871 return -ENOBUFS;
4872}
4873
4874
4875static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4876 struct wpa_driver_associate_params *params)
4877{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004878 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004879
4880 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004881 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4882 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004883 nlmode = NL80211_IFTYPE_P2P_GO;
4884 } else
4885 nlmode = NL80211_IFTYPE_AP;
4886
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004887 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004888 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004889 nl80211_remove_monitor_interface(drv);
4890 return -1;
4891 }
4892
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004893 if (params->freq.freq &&
4894 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004895 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004896 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004897 nl80211_remove_monitor_interface(drv);
4898 return -1;
4899 }
4900
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004901 return 0;
4902}
4903
4904
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004905static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
4906 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004907{
4908 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004909 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004910
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004911 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004912 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004913 if (ret) {
4914 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
4915 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004916 } else {
4917 wpa_printf(MSG_DEBUG,
4918 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004919 }
4920
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004921 if (reset_mode &&
4922 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004923 NL80211_IFTYPE_STATION)) {
4924 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4925 "station mode");
4926 }
4927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004928 return ret;
4929}
4930
4931
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004932static int nl80211_ht_vht_overrides(struct nl_msg *msg,
4933 struct wpa_driver_associate_params *params)
4934{
4935 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
4936 return -1;
4937
4938 if (params->htcaps && params->htcaps_mask) {
4939 int sz = sizeof(struct ieee80211_ht_capabilities);
4940 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
4941 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
4942 params->htcaps_mask, sz);
4943 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
4944 params->htcaps) ||
4945 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
4946 params->htcaps_mask))
4947 return -1;
4948 }
4949
4950#ifdef CONFIG_VHT_OVERRIDES
4951 if (params->disable_vht) {
4952 wpa_printf(MSG_DEBUG, " * VHT disabled");
4953 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
4954 return -1;
4955 }
4956
4957 if (params->vhtcaps && params->vhtcaps_mask) {
4958 int sz = sizeof(struct ieee80211_vht_capabilities);
4959 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
4960 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
4961 params->vhtcaps_mask, sz);
4962 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
4963 params->vhtcaps) ||
4964 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
4965 params->vhtcaps_mask))
4966 return -1;
4967 }
4968#endif /* CONFIG_VHT_OVERRIDES */
4969
4970 return 0;
4971}
4972
4973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004974static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
4975 struct wpa_driver_associate_params *params)
4976{
4977 struct nl_msg *msg;
4978 int ret = -1;
4979 int count = 0;
4980
4981 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
4982
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004983 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004984 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4985 "IBSS mode");
4986 return -1;
4987 }
4988
4989retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004990 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
4991 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
4992 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004993
4994 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4995 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004996 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4997 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4999 drv->ssid_len = params->ssid_len;
5000
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005001 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
5002 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005003 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004
5005 ret = nl80211_set_conn_keys(params, msg);
5006 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005007 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005008
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005009 if (params->bssid && params->fixed_bssid) {
5010 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
5011 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005012 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5013 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005014 }
5015
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005016 if (params->fixed_freq) {
5017 wpa_printf(MSG_DEBUG, " * fixed_freq");
5018 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
5019 goto fail;
5020 }
5021
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005022 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5023 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5024 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5025 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005026 wpa_printf(MSG_DEBUG, " * control port");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005027 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5028 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005029 }
5030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005031 if (params->wpa_ie) {
5032 wpa_hexdump(MSG_DEBUG,
5033 " * Extra IEs for Beacon/Probe Response frames",
5034 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005035 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5036 params->wpa_ie))
5037 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005038 }
5039
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005040 ret = nl80211_ht_vht_overrides(msg, params);
5041 if (ret < 0)
5042 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005043
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005044 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5045 msg = NULL;
5046 if (ret) {
5047 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
5048 ret, strerror(-ret));
5049 count++;
5050 if (ret == -EALREADY && count == 1) {
5051 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
5052 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005053 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005054 nlmsg_free(msg);
5055 goto retry;
5056 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005057 } else {
5058 wpa_printf(MSG_DEBUG,
5059 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005060 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005061
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005062fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005063 nlmsg_free(msg);
5064 return ret;
5065}
5066
5067
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005068static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
5069 struct wpa_driver_associate_params *params,
5070 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005071{
Paul Stewart092955c2017-02-06 09:13:09 -08005072 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
5073 return -1;
5074
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005075 if (params->bssid) {
5076 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5077 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005078 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5079 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005081
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005082 if (params->bssid_hint) {
5083 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
5084 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005085 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
5086 params->bssid_hint))
5087 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005088 }
5089
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005090 if (params->freq.freq) {
5091 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005092 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
5093 params->freq.freq))
5094 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005095 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005096 } else
5097 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005098
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005099 if (params->freq_hint) {
5100 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005101 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
5102 params->freq_hint))
5103 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005104 }
5105
Dmitry Shmidt04949592012-07-19 12:16:46 -07005106 if (params->bg_scan_period >= 0) {
5107 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
5108 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005109 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
5110 params->bg_scan_period))
5111 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005112 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005114 if (params->ssid) {
5115 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5116 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005117 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
5118 params->ssid))
5119 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005120 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005121 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005122 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5123 drv->ssid_len = params->ssid_len;
5124 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005125
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005126 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005127 if (params->wpa_ie &&
5128 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
5129 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005130
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005131 if (params->wpa_proto) {
5132 enum nl80211_wpa_versions ver = 0;
5133
5134 if (params->wpa_proto & WPA_PROTO_WPA)
5135 ver |= NL80211_WPA_VERSION_1;
5136 if (params->wpa_proto & WPA_PROTO_RSN)
5137 ver |= NL80211_WPA_VERSION_2;
5138
5139 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005140 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
5141 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005142 }
5143
5144 if (params->pairwise_suite != WPA_CIPHER_NONE) {
5145 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
5146 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005147 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5148 cipher))
5149 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005150 }
5151
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005152 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
5153 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
5154 /*
5155 * This is likely to work even though many drivers do not
5156 * advertise support for operations without GTK.
5157 */
5158 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
5159 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005160 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
5161 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005162 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
5163 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005164 }
5165
5166 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5167 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5168 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
5169 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07005170 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005171 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
5172 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005173 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005174 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
5175 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
Paul Stewart092955c2017-02-06 09:13:09 -08005176 int mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005177
5178 switch (params->key_mgmt_suite) {
5179 case WPA_KEY_MGMT_CCKM:
Paul Stewart092955c2017-02-06 09:13:09 -08005180 mgmt = RSN_AUTH_KEY_MGMT_CCKM;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005181 break;
5182 case WPA_KEY_MGMT_IEEE8021X:
Paul Stewart092955c2017-02-06 09:13:09 -08005183 mgmt = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005184 break;
5185 case WPA_KEY_MGMT_FT_IEEE8021X:
Paul Stewart092955c2017-02-06 09:13:09 -08005186 mgmt = RSN_AUTH_KEY_MGMT_FT_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005187 break;
5188 case WPA_KEY_MGMT_FT_PSK:
Paul Stewart092955c2017-02-06 09:13:09 -08005189 mgmt = RSN_AUTH_KEY_MGMT_FT_PSK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005190 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005191 case WPA_KEY_MGMT_IEEE8021X_SHA256:
Paul Stewart092955c2017-02-06 09:13:09 -08005192 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005193 break;
5194 case WPA_KEY_MGMT_PSK_SHA256:
Paul Stewart092955c2017-02-06 09:13:09 -08005195 mgmt = RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005196 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005197 case WPA_KEY_MGMT_OSEN:
Paul Stewart092955c2017-02-06 09:13:09 -08005198 mgmt = RSN_AUTH_KEY_MGMT_OSEN;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005199 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005200 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
Paul Stewart092955c2017-02-06 09:13:09 -08005201 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005202 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005203 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
Paul Stewart092955c2017-02-06 09:13:09 -08005204 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005205 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005206 case WPA_KEY_MGMT_PSK:
5207 default:
Paul Stewart092955c2017-02-06 09:13:09 -08005208 mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005209 break;
5210 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07005211 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005212 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
5213 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005214 }
5215
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005216 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5217 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005218
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005219 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
5220 (params->pairwise_suite == WPA_CIPHER_NONE ||
5221 params->pairwise_suite == WPA_CIPHER_WEP104 ||
5222 params->pairwise_suite == WPA_CIPHER_WEP40) &&
5223 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
5224 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
5225 return -1;
5226
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005227 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5228 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5229 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005230
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005231 if (params->rrm_used) {
5232 u32 drv_rrm_flags = drv->capa.rrm_flags;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005233 if ((!((drv_rrm_flags &
5234 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
5235 (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
5236 !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005237 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
5238 return -1;
5239 }
5240
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005241 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005242 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005243
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005244 if (params->p2p)
5245 wpa_printf(MSG_DEBUG, " * P2P group");
5246
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005247 if (params->pbss) {
5248 wpa_printf(MSG_DEBUG, " * PBSS");
5249 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
5250 return -1;
5251 }
5252
Dmitry Shmidte4663042016-04-04 10:07:49 -07005253 drv->connect_reassoc = 0;
5254 if (params->prev_bssid) {
5255 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
5256 MAC2STR(params->prev_bssid));
5257 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
5258 params->prev_bssid))
5259 return -1;
5260 drv->connect_reassoc = 1;
5261 }
5262
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005263 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005264}
5265
5266
5267static int wpa_driver_nl80211_try_connect(
5268 struct wpa_driver_nl80211_data *drv,
5269 struct wpa_driver_associate_params *params)
5270{
5271 struct nl_msg *msg;
5272 enum nl80211_auth_type type;
5273 int ret;
5274 int algs;
5275
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005276#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005277 if (params->req_key_mgmt_offload && params->psk &&
5278 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5279 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5280 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
5281 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
5282 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
5283 if (ret)
5284 return ret;
5285 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005286#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005287
5288 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
5289 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005290 if (!msg)
5291 return -1;
5292
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005293 ret = nl80211_connect_common(drv, params, msg);
5294 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005295 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005297 algs = 0;
5298 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5299 algs++;
5300 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5301 algs++;
5302 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5303 algs++;
5304 if (algs > 1) {
5305 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
5306 "selection");
5307 goto skip_auth_type;
5308 }
5309
5310 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5311 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
5312 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5313 type = NL80211_AUTHTYPE_SHARED_KEY;
5314 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5315 type = NL80211_AUTHTYPE_NETWORK_EAP;
5316 else if (params->auth_alg & WPA_AUTH_ALG_FT)
5317 type = NL80211_AUTHTYPE_FT;
5318 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005319 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005320
5321 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005322 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
5323 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005324
5325skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005326 ret = nl80211_set_conn_keys(params, msg);
5327 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005328 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005329
5330 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5331 msg = NULL;
5332 if (ret) {
5333 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
5334 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005335 } else {
5336 wpa_printf(MSG_DEBUG,
5337 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005338 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005339
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005340fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005341 nlmsg_free(msg);
5342 return ret;
5343
5344}
5345
5346
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005347static int wpa_driver_nl80211_connect(
5348 struct wpa_driver_nl80211_data *drv,
5349 struct wpa_driver_associate_params *params)
5350{
Jithu Jancea7c60b42014-12-03 18:54:40 +05305351 int ret;
5352
5353 /* Store the connection attempted bssid for future use */
5354 if (params->bssid)
5355 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5356 else
5357 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
5358
5359 ret = wpa_driver_nl80211_try_connect(drv, params);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005360 if (ret == -EALREADY) {
5361 /*
5362 * cfg80211 does not currently accept new connections if
5363 * we are already connected. As a workaround, force
5364 * disconnection and try again.
5365 */
5366 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
5367 "disconnecting before reassociation "
5368 "attempt");
5369 if (wpa_driver_nl80211_disconnect(
5370 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
5371 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005372 ret = wpa_driver_nl80211_try_connect(drv, params);
5373 }
5374 return ret;
5375}
5376
5377
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005378static int wpa_driver_nl80211_associate(
5379 void *priv, struct wpa_driver_associate_params *params)
5380{
5381 struct i802_bss *bss = priv;
5382 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005383 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005384 struct nl_msg *msg;
5385
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005386 nl80211_unmask_11b_rates(bss);
5387
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005388 if (params->mode == IEEE80211_MODE_AP)
5389 return wpa_driver_nl80211_ap(drv, params);
5390
5391 if (params->mode == IEEE80211_MODE_IBSS)
5392 return wpa_driver_nl80211_ibss(drv, params);
5393
5394 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005395 enum nl80211_iftype nlmode = params->p2p ?
5396 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5397
5398 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005399 return -1;
5400 return wpa_driver_nl80211_connect(drv, params);
5401 }
5402
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005403 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005404
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005405 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
5406 drv->ifindex);
5407 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005408 if (!msg)
5409 return -1;
5410
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005411 ret = nl80211_connect_common(drv, params, msg);
5412 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005413 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005414
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005415 if (params->fils_kek) {
5416 wpa_printf(MSG_DEBUG, " * FILS KEK (len=%u)",
5417 (unsigned int) params->fils_kek_len);
5418 if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
5419 params->fils_kek))
5420 goto fail;
5421 }
5422 if (params->fils_nonces) {
5423 wpa_hexdump(MSG_DEBUG, " * FILS nonces (for AAD)",
5424 params->fils_nonces,
5425 params->fils_nonces_len);
5426 if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
5427 params->fils_nonces_len, params->fils_nonces))
5428 goto fail;
5429 }
5430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005431 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5432 msg = NULL;
5433 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005434 wpa_dbg(drv->ctx, MSG_DEBUG,
5435 "nl80211: MLME command failed (assoc): ret=%d (%s)",
5436 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005437 nl80211_dump_scan(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005438 } else {
5439 wpa_printf(MSG_DEBUG,
5440 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005441 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005442
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005443fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005444 nlmsg_free(msg);
5445 return ret;
5446}
5447
5448
5449static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005450 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005451{
5452 struct nl_msg *msg;
5453 int ret = -ENOBUFS;
5454
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005455 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
5456 ifindex, mode, nl80211_iftype_str(mode));
5457
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005458 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
5459 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
5460 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005461
5462 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005463 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005464 if (!ret)
5465 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005466fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005467 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005468 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
5469 " %d (%s)", ifindex, mode, ret, strerror(-ret));
5470 return ret;
5471}
5472
5473
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005474static int wpa_driver_nl80211_set_mode_impl(
5475 struct i802_bss *bss,
5476 enum nl80211_iftype nlmode,
5477 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005478{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005479 struct wpa_driver_nl80211_data *drv = bss->drv;
5480 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005481 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005482 int was_ap = is_ap_interface(drv->nlmode);
5483 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005484 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005485
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07005486 if (TEST_FAIL())
5487 return -1;
5488
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005489 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5490 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
5491 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005492
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005493 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005494 drv->nlmode = nlmode;
5495 ret = 0;
5496 goto done;
5497 }
5498
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005499 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005500 return -1;
5501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005502 if (nlmode == drv->nlmode) {
5503 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
5504 "requested mode - ignore error");
5505 ret = 0;
5506 goto done; /* Already in the requested mode */
5507 }
5508
5509 /* mac80211 doesn't allow mode changes while the device is up, so
5510 * take the device down, try to set the mode again, and bring the
5511 * device back up.
5512 */
5513 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
5514 "interface down");
5515 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005516 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005517 if (res == -EACCES || res == -ENODEV)
5518 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005519 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005520 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
5521 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005522 os_sleep(0, 100000);
5523 continue;
5524 }
5525
5526 /*
5527 * Setting the mode will fail for some drivers if the phy is
5528 * on a frequency that the mode is disallowed in.
5529 */
5530 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005531 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005532 if (res) {
5533 wpa_printf(MSG_DEBUG,
5534 "nl80211: Failed to set frequency on interface");
5535 }
5536 }
5537
5538 /* Try to set the mode again while the interface is down */
5539 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5540 if (mode_switch_res == -EBUSY) {
5541 wpa_printf(MSG_DEBUG,
5542 "nl80211: Delaying mode set while interface going down");
5543 os_sleep(0, 100000);
5544 continue;
5545 }
5546 ret = mode_switch_res;
5547 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005548 }
5549
5550 if (!ret) {
5551 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5552 "interface is down");
5553 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005554 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005555 }
5556
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005557 /* Bring the interface back up */
5558 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
5559 if (res != 0) {
5560 wpa_printf(MSG_DEBUG,
5561 "nl80211: Failed to set interface up after switching mode");
5562 ret = -1;
5563 }
5564
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005565done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005566 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005567 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5568 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005569 return ret;
5570 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005571
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005572 if (is_p2p_net_interface(nlmode)) {
5573 wpa_printf(MSG_DEBUG,
5574 "nl80211: Interface %s mode change to P2P - disable 11b rates",
5575 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005576 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005577 } else if (drv->disabled_11b_rates) {
5578 wpa_printf(MSG_DEBUG,
5579 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
5580 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005581 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005582 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005583
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005584 if (is_ap_interface(nlmode)) {
5585 nl80211_mgmt_unsubscribe(bss, "start AP");
5586 /* Setup additional AP mode functionality if needed */
5587 if (nl80211_setup_ap(bss))
5588 return -1;
5589 } else if (was_ap) {
5590 /* Remove additional AP mode functionality */
5591 nl80211_teardown_ap(bss);
5592 } else {
5593 nl80211_mgmt_unsubscribe(bss, "mode change");
5594 }
5595
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005596 if (is_mesh_interface(nlmode) &&
5597 nl80211_mgmt_subscribe_mesh(bss))
5598 return -1;
5599
Dmitry Shmidt04949592012-07-19 12:16:46 -07005600 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005601 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005602 nl80211_mgmt_subscribe_non_ap(bss) < 0)
5603 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
5604 "frame processing - ignore for now");
5605
5606 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005607}
5608
5609
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005610int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
5611 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005612{
5613 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
5614}
5615
5616
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005617static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
5618 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005619{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005620 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005621 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005622}
5623
5624
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005625static int wpa_driver_nl80211_get_capa(void *priv,
5626 struct wpa_driver_capa *capa)
5627{
5628 struct i802_bss *bss = priv;
5629 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07005630
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005631 if (!drv->has_capability)
5632 return -1;
5633 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005634 if (drv->extended_capa && drv->extended_capa_mask) {
5635 capa->extended_capa = drv->extended_capa;
5636 capa->extended_capa_mask = drv->extended_capa_mask;
5637 capa->extended_capa_len = drv->extended_capa_len;
5638 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005639
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005640 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005641}
5642
5643
5644static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5645{
5646 struct i802_bss *bss = priv;
5647 struct wpa_driver_nl80211_data *drv = bss->drv;
5648
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005649 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
5650 bss->ifname, drv->operstate, state,
5651 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005652 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005653 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005654 state ? IF_OPER_UP : IF_OPER_DORMANT);
5655}
5656
5657
5658static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5659{
5660 struct i802_bss *bss = priv;
5661 struct wpa_driver_nl80211_data *drv = bss->drv;
5662 struct nl_msg *msg;
5663 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005664 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005665
5666 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
5667 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
5668 return 0;
5669 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005670
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005671 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
5672 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
5673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005674 os_memset(&upd, 0, sizeof(upd));
5675 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5676 if (authorized)
5677 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005678
5679 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5680 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
5681 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
5682 nlmsg_free(msg);
5683 return -ENOBUFS;
5684 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005685
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005686 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005687 if (!ret)
5688 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005689 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
5690 ret, strerror(-ret));
5691 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005692}
5693
5694
Jouni Malinen75ecf522011-06-27 15:19:46 -07005695/* Set kernel driver on given frequency (MHz) */
5696static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005697{
Jouni Malinen75ecf522011-06-27 15:19:46 -07005698 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005699 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005700}
5701
5702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005703static inline int min_int(int a, int b)
5704{
5705 if (a < b)
5706 return a;
5707 return b;
5708}
5709
5710
5711static int get_key_handler(struct nl_msg *msg, void *arg)
5712{
5713 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5714 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5715
5716 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5717 genlmsg_attrlen(gnlh, 0), NULL);
5718
5719 /*
5720 * TODO: validate the key index and mac address!
5721 * Otherwise, there's a race condition as soon as
5722 * the kernel starts sending key notifications.
5723 */
5724
5725 if (tb[NL80211_ATTR_KEY_SEQ])
5726 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5727 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5728 return NL_SKIP;
5729}
5730
5731
5732static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5733 int idx, u8 *seq)
5734{
5735 struct i802_bss *bss = priv;
5736 struct wpa_driver_nl80211_data *drv = bss->drv;
5737 struct nl_msg *msg;
5738
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005739 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
5740 NL80211_CMD_GET_KEY);
5741 if (!msg ||
5742 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
5743 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
5744 nlmsg_free(msg);
5745 return -ENOBUFS;
5746 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005747
5748 memset(seq, 0, 6);
5749
5750 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005751}
5752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005753
5754static int i802_set_rts(void *priv, int rts)
5755{
5756 struct i802_bss *bss = priv;
5757 struct wpa_driver_nl80211_data *drv = bss->drv;
5758 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005759 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005760 u32 val;
5761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005762 if (rts >= 2347)
5763 val = (u32) -1;
5764 else
5765 val = rts;
5766
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005767 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5768 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
5769 nlmsg_free(msg);
5770 return -ENOBUFS;
5771 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005772
5773 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5774 if (!ret)
5775 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005776 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5777 "%d (%s)", rts, ret, strerror(-ret));
5778 return ret;
5779}
5780
5781
5782static int i802_set_frag(void *priv, int frag)
5783{
5784 struct i802_bss *bss = priv;
5785 struct wpa_driver_nl80211_data *drv = bss->drv;
5786 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005787 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005788 u32 val;
5789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005790 if (frag >= 2346)
5791 val = (u32) -1;
5792 else
5793 val = frag;
5794
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005795 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5796 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
5797 nlmsg_free(msg);
5798 return -ENOBUFS;
5799 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005800
5801 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5802 if (!ret)
5803 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005804 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
5805 "%d: %d (%s)", frag, ret, strerror(-ret));
5806 return ret;
5807}
5808
5809
5810static int i802_flush(void *priv)
5811{
5812 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005813 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005814 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005815
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005816 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
5817 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005818
5819 /*
5820 * XXX: FIX! this needs to flush all VLANs too
5821 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005822 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
5823 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005824 if (res) {
5825 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
5826 "(%s)", res, strerror(-res));
5827 }
5828 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005829}
5830
5831
5832static int get_sta_handler(struct nl_msg *msg, void *arg)
5833{
5834 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5835 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5836 struct hostap_sta_driver_data *data = arg;
5837 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
5838 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
5839 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
5840 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
5841 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
5842 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
5843 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005844 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005845 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
5846 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005847 };
5848
5849 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5850 genlmsg_attrlen(gnlh, 0), NULL);
5851
5852 /*
5853 * TODO: validate the interface and mac address!
5854 * Otherwise, there's a race condition as soon as
5855 * the kernel starts sending station notifications.
5856 */
5857
5858 if (!tb[NL80211_ATTR_STA_INFO]) {
5859 wpa_printf(MSG_DEBUG, "sta stats missing!");
5860 return NL_SKIP;
5861 }
5862 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
5863 tb[NL80211_ATTR_STA_INFO],
5864 stats_policy)) {
5865 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
5866 return NL_SKIP;
5867 }
5868
5869 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
5870 data->inactive_msec =
5871 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005872 /* For backwards compatibility, fetch the 32-bit counters first. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005873 if (stats[NL80211_STA_INFO_RX_BYTES])
5874 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
5875 if (stats[NL80211_STA_INFO_TX_BYTES])
5876 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005877 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
5878 stats[NL80211_STA_INFO_TX_BYTES64]) {
5879 /*
5880 * The driver supports 64-bit counters, so use them to override
5881 * the 32-bit values.
5882 */
5883 data->rx_bytes =
5884 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
5885 data->tx_bytes =
5886 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
5887 data->bytes_64bit = 1;
5888 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005889 if (stats[NL80211_STA_INFO_RX_PACKETS])
5890 data->rx_packets =
5891 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
5892 if (stats[NL80211_STA_INFO_TX_PACKETS])
5893 data->tx_packets =
5894 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005895 if (stats[NL80211_STA_INFO_TX_FAILED])
5896 data->tx_retry_failed =
5897 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005898
5899 return NL_SKIP;
5900}
5901
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005902static int i802_read_sta_data(struct i802_bss *bss,
5903 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005904 const u8 *addr)
5905{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005906 struct nl_msg *msg;
5907
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005908 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
5909 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
5910 nlmsg_free(msg);
5911 return -ENOBUFS;
5912 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005913
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005914 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005915}
5916
5917
5918static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
5919 int cw_min, int cw_max, int burst_time)
5920{
5921 struct i802_bss *bss = priv;
5922 struct wpa_driver_nl80211_data *drv = bss->drv;
5923 struct nl_msg *msg;
5924 struct nlattr *txq, *params;
5925
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005926 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005927 if (!msg)
5928 return -1;
5929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005930 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
5931 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005932 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005933
5934 /* We are only sending parameters for a single TXQ at a time */
5935 params = nla_nest_start(msg, 1);
5936 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005937 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005938
5939 switch (queue) {
5940 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005941 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
5942 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005943 break;
5944 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005945 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
5946 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005947 break;
5948 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005949 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
5950 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005951 break;
5952 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005953 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
5954 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005955 break;
5956 }
5957 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
5958 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005959 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
5960 (burst_time * 100 + 16) / 32) ||
5961 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
5962 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
5963 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
5964 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005965
5966 nla_nest_end(msg, params);
5967
5968 nla_nest_end(msg, txq);
5969
5970 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5971 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005972 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005973fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005974 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005975 return -1;
5976}
5977
5978
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005979static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005980 const char *ifname, int vlan_id)
5981{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005982 struct wpa_driver_nl80211_data *drv = bss->drv;
5983 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005984 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005985
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005986 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
5987 ", ifname=%s[%d], vlan_id=%d)",
5988 bss->ifname, if_nametoindex(bss->ifname),
5989 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005990 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5991 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5992 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
5993 nlmsg_free(msg);
5994 return -ENOBUFS;
5995 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005996
5997 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5998 if (ret < 0) {
5999 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
6000 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
6001 MAC2STR(addr), ifname, vlan_id, ret,
6002 strerror(-ret));
6003 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006004 return ret;
6005}
6006
6007
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006008static int i802_get_inact_sec(void *priv, const u8 *addr)
6009{
6010 struct hostap_sta_driver_data data;
6011 int ret;
6012
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08006013 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006014 data.inactive_msec = (unsigned long) -1;
6015 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006016 if (ret == -ENOENT)
6017 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006018 if (ret || data.inactive_msec == (unsigned long) -1)
6019 return -1;
6020 return data.inactive_msec / 1000;
6021}
6022
6023
6024static int i802_sta_clear_stats(void *priv, const u8 *addr)
6025{
6026#if 0
6027 /* TODO */
6028#endif
6029 return 0;
6030}
6031
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006032
6033static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
6034 int reason)
6035{
6036 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006037 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006038 struct ieee80211_mgmt mgmt;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006039 u8 channel;
6040
6041 if (ieee80211_freq_to_chan(bss->freq, &channel) ==
6042 HOSTAPD_MODE_IEEE80211AD) {
6043 /* Deauthentication is not used in DMG/IEEE 802.11ad;
6044 * disassociate the STA instead. */
6045 return i802_sta_disassoc(priv, own_addr, addr, reason);
6046 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006047
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006048 if (is_mesh_interface(drv->nlmode))
6049 return -1;
6050
Dmitry Shmidt04949592012-07-19 12:16:46 -07006051 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006052 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006054 memset(&mgmt, 0, sizeof(mgmt));
6055 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6056 WLAN_FC_STYPE_DEAUTH);
6057 memcpy(mgmt.da, addr, ETH_ALEN);
6058 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6059 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6060 mgmt.u.deauth.reason_code = host_to_le16(reason);
6061 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6062 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006063 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006064 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065}
6066
6067
6068static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
6069 int reason)
6070{
6071 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006072 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006073 struct ieee80211_mgmt mgmt;
6074
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006075 if (is_mesh_interface(drv->nlmode))
6076 return -1;
6077
Dmitry Shmidt04949592012-07-19 12:16:46 -07006078 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006079 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006081 memset(&mgmt, 0, sizeof(mgmt));
6082 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6083 WLAN_FC_STYPE_DISASSOC);
6084 memcpy(mgmt.da, addr, ETH_ALEN);
6085 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6086 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6087 mgmt.u.disassoc.reason_code = host_to_le16(reason);
6088 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6089 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006090 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006091 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006092}
6093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006094
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006095static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
6096{
6097 char buf[200], *pos, *end;
6098 int i, res;
6099
6100 pos = buf;
6101 end = pos + sizeof(buf);
6102
6103 for (i = 0; i < drv->num_if_indices; i++) {
6104 if (!drv->if_indices[i])
6105 continue;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006106 res = os_snprintf(pos, end - pos, " %d(%d)",
6107 drv->if_indices[i],
6108 drv->if_indices_reason[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006109 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006110 break;
6111 pos += res;
6112 }
6113 *pos = '\0';
6114
6115 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
6116 drv->num_if_indices, buf);
6117}
6118
6119
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006120static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6121 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006122{
6123 int i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006124 int *old, *old_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006125
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006126 wpa_printf(MSG_DEBUG,
6127 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
6128 ifidx, ifidx_reason);
6129 if (have_ifidx(drv, ifidx, ifidx_reason)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006130 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
6131 ifidx);
6132 return;
6133 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006134 for (i = 0; i < drv->num_if_indices; i++) {
6135 if (drv->if_indices[i] == 0) {
6136 drv->if_indices[i] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006137 drv->if_indices_reason[i] = ifidx_reason;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006138 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006139 return;
6140 }
6141 }
6142
6143 if (drv->if_indices != drv->default_if_indices)
6144 old = drv->if_indices;
6145 else
6146 old = NULL;
6147
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006148 if (drv->if_indices_reason != drv->default_if_indices_reason)
6149 old_reason = drv->if_indices_reason;
6150 else
6151 old_reason = NULL;
6152
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006153 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
6154 sizeof(int));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006155 drv->if_indices_reason = os_realloc_array(old_reason,
6156 drv->num_if_indices + 1,
6157 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006158 if (!drv->if_indices) {
6159 if (!old)
6160 drv->if_indices = drv->default_if_indices;
6161 else
6162 drv->if_indices = old;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006163 }
6164 if (!drv->if_indices_reason) {
6165 if (!old_reason)
6166 drv->if_indices_reason = drv->default_if_indices_reason;
6167 else
Dmitry Shmidte4663042016-04-04 10:07:49 -07006168 drv->if_indices_reason = old_reason;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006169 }
6170 if (!drv->if_indices || !drv->if_indices_reason) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07006171 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
6172 "interfaces");
6173 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
6174 return;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006175 }
6176 if (!old)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006177 os_memcpy(drv->if_indices, drv->default_if_indices,
6178 sizeof(drv->default_if_indices));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006179 if (!old_reason)
6180 os_memcpy(drv->if_indices_reason,
6181 drv->default_if_indices_reason,
6182 sizeof(drv->default_if_indices_reason));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006183 drv->if_indices[drv->num_if_indices] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006184 drv->if_indices_reason[drv->num_if_indices] = ifidx_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006185 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006186 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006187}
6188
6189
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006190static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6191 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006192{
6193 int i;
6194
6195 for (i = 0; i < drv->num_if_indices; i++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006196 if ((drv->if_indices[i] == ifidx || ifidx == IFIDX_ANY) &&
6197 (drv->if_indices_reason[i] == ifidx_reason ||
6198 ifidx_reason == IFIDX_ANY)) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07006199 drv->if_indices[i] = 0;
6200 break;
6201 }
6202 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006203 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006204}
6205
6206
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006207static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6208 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006209{
6210 int i;
6211
6212 for (i = 0; i < drv->num_if_indices; i++)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006213 if (drv->if_indices[i] == ifidx &&
6214 (drv->if_indices_reason[i] == ifidx_reason ||
6215 ifidx_reason == IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07006216 return 1;
6217
6218 return 0;
6219}
6220
6221
6222static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006223 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006224{
6225 struct i802_bss *bss = priv;
6226 struct wpa_driver_nl80211_data *drv = bss->drv;
6227 char name[IFNAMSIZ + 1];
6228
6229 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006230 if (ifname_wds)
6231 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
6232
Jouni Malinen75ecf522011-06-27 15:19:46 -07006233 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
6234 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
6235 if (val) {
6236 if (!if_nametoindex(name)) {
6237 if (nl80211_create_iface(drv, name,
6238 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006239 bss->addr, 1, NULL, NULL, 0) <
6240 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006241 return -1;
6242 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006243 linux_br_add_if(drv->global->ioctl_sock,
6244 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006245 return -1;
6246 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006247 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
6248 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
6249 "interface %s up", name);
6250 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006251 return i802_set_sta_vlan(priv, addr, name, 0);
6252 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006253 if (bridge_ifname)
6254 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
6255 name);
6256
Jouni Malinen75ecf522011-06-27 15:19:46 -07006257 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006258 nl80211_remove_iface(drv, if_nametoindex(name));
6259 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006260 }
6261}
6262
6263
6264static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
6265{
6266 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6267 struct sockaddr_ll lladdr;
6268 unsigned char buf[3000];
6269 int len;
6270 socklen_t fromlen = sizeof(lladdr);
6271
6272 len = recvfrom(sock, buf, sizeof(buf), 0,
6273 (struct sockaddr *)&lladdr, &fromlen);
6274 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006275 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
6276 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006277 return;
6278 }
6279
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006280 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07006281 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
6282}
6283
6284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006285static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
6286 struct i802_bss *bss,
6287 const char *brname, const char *ifname)
6288{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006289 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006290 char in_br[IFNAMSIZ];
6291
6292 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006293 br_ifindex = if_nametoindex(brname);
6294 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006295 /*
6296 * Bridge was configured, but the bridge device does
6297 * not exist. Try to add it now.
6298 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006299 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006300 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
6301 "bridge interface %s: %s",
6302 brname, strerror(errno));
6303 return -1;
6304 }
6305 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006306 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006307 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006308 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006309 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006310
6311 if (linux_br_get(in_br, ifname) == 0) {
6312 if (os_strcmp(in_br, brname) == 0)
6313 return 0; /* already in the bridge */
6314
6315 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
6316 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006317 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
6318 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006319 wpa_printf(MSG_ERROR, "nl80211: Failed to "
6320 "remove interface %s from bridge "
6321 "%s: %s",
6322 ifname, brname, strerror(errno));
6323 return -1;
6324 }
6325 }
6326
6327 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
6328 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006329 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006330 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
6331 "into bridge %s: %s",
6332 ifname, brname, strerror(errno));
6333 return -1;
6334 }
6335 bss->added_if_into_bridge = 1;
6336
6337 return 0;
6338}
6339
6340
6341static void *i802_init(struct hostapd_data *hapd,
6342 struct wpa_init_params *params)
6343{
6344 struct wpa_driver_nl80211_data *drv;
6345 struct i802_bss *bss;
6346 size_t i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006347 char master_ifname[IFNAMSIZ];
6348 int ifindex, br_ifindex = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006349 int br_added = 0;
6350
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006351 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
6352 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006353 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006354 if (bss == NULL)
6355 return NULL;
6356
6357 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006358
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006359 if (linux_br_get(master_ifname, params->ifname) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006360 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006361 params->ifname, master_ifname);
6362 br_ifindex = if_nametoindex(master_ifname);
6363 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6364 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
6365 linux_master_get(master_ifname, params->ifname) == 0) {
6366 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
6367 params->ifname, master_ifname);
6368 /* start listening for EAPOL on the master interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006369 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006370
6371 /* check if master itself is under bridge */
6372 if (linux_br_get(master_ifname, master_ifname) == 0) {
6373 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
6374 master_ifname);
6375 br_ifindex = if_nametoindex(master_ifname);
6376 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6377 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006378 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006379 master_ifname[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006380 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006381
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006382 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006383
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006384 for (i = 0; i < params->num_bridge; i++) {
6385 if (params->bridge[i]) {
6386 ifindex = if_nametoindex(params->bridge[i]);
6387 if (ifindex)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006388 add_ifidx(drv, ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006389 if (ifindex == br_ifindex)
6390 br_added = 1;
6391 }
6392 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006393
6394 /* start listening for EAPOL on the default AP interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006395 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006396
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006397 if (params->num_bridge && params->bridge[0]) {
6398 if (i802_check_bridge(drv, bss, params->bridge[0],
6399 params->ifname) < 0)
6400 goto failed;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006401 if (os_strcmp(params->bridge[0], master_ifname) != 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006402 br_added = 1;
6403 }
6404
6405 if (!br_added && br_ifindex &&
6406 (params->num_bridge == 0 || !params->bridge[0]))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006407 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006408
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006409#ifdef CONFIG_LIBNL3_ROUTE
6410 if (bss->added_if_into_bridge) {
6411 drv->rtnl_sk = nl_socket_alloc();
6412 if (drv->rtnl_sk == NULL) {
6413 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
6414 goto failed;
6415 }
6416
6417 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
6418 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
6419 strerror(errno));
6420 goto failed;
6421 }
6422 }
6423#endif /* CONFIG_LIBNL3_ROUTE */
6424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006425 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
6426 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006427 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
6428 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006429 goto failed;
6430 }
6431
6432 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
6433 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006434 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006435 goto failed;
6436 }
6437
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006438 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6439 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006440 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006441 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006442
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006443 memcpy(bss->addr, params->own_addr, ETH_ALEN);
6444
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006445 return bss;
6446
6447failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006448 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006449 return NULL;
6450}
6451
6452
6453static void i802_deinit(void *priv)
6454{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006455 struct i802_bss *bss = priv;
6456 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006457}
6458
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006459
6460static enum nl80211_iftype wpa_driver_nl80211_if_type(
6461 enum wpa_driver_if_type type)
6462{
6463 switch (type) {
6464 case WPA_IF_STATION:
6465 return NL80211_IFTYPE_STATION;
6466 case WPA_IF_P2P_CLIENT:
6467 case WPA_IF_P2P_GROUP:
6468 return NL80211_IFTYPE_P2P_CLIENT;
6469 case WPA_IF_AP_VLAN:
6470 return NL80211_IFTYPE_AP_VLAN;
6471 case WPA_IF_AP_BSS:
6472 return NL80211_IFTYPE_AP;
6473 case WPA_IF_P2P_GO:
6474 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006475 case WPA_IF_P2P_DEVICE:
6476 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006477 case WPA_IF_MESH:
6478 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006479 default:
6480 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006481 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006482}
6483
6484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006485static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
6486{
6487 struct wpa_driver_nl80211_data *drv;
6488 dl_list_for_each(drv, &global->interfaces,
6489 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006490 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006491 return 1;
6492 }
6493 return 0;
6494}
6495
6496
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006497static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006498{
6499 unsigned int idx;
6500
6501 if (!drv->global)
6502 return -1;
6503
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006504 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006505 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006506 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006507 new_addr[0] ^= idx << 2;
6508 if (!nl80211_addr_in_use(drv->global, new_addr))
6509 break;
6510 }
6511 if (idx == 64)
6512 return -1;
6513
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006514 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006515 MACSTR, MAC2STR(new_addr));
6516
6517 return 0;
6518}
6519
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006520
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006521struct wdev_info {
6522 u64 wdev_id;
6523 int wdev_id_set;
6524 u8 macaddr[ETH_ALEN];
6525};
6526
6527static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
6528{
6529 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6530 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6531 struct wdev_info *wi = arg;
6532
6533 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6534 genlmsg_attrlen(gnlh, 0), NULL);
6535 if (tb[NL80211_ATTR_WDEV]) {
6536 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
6537 wi->wdev_id_set = 1;
6538 }
6539
6540 if (tb[NL80211_ATTR_MAC])
6541 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
6542 ETH_ALEN);
6543
6544 return NL_SKIP;
6545}
6546
6547
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006548static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
6549 const char *ifname, const u8 *addr,
6550 void *bss_ctx, void **drv_priv,
6551 char *force_ifname, u8 *if_addr,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006552 const char *bridge, int use_existing,
6553 int setup_ap)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006554{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006555 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006556 struct i802_bss *bss = priv;
6557 struct wpa_driver_nl80211_data *drv = bss->drv;
6558 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006559 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006560
6561 if (addr)
6562 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006563 nlmode = wpa_driver_nl80211_if_type(type);
6564 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
6565 struct wdev_info p2pdev_info;
6566
6567 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
6568 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
6569 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006570 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006571 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
6572 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
6573 ifname);
6574 return -1;
6575 }
6576
6577 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
6578 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
6579 if (!is_zero_ether_addr(p2pdev_info.macaddr))
6580 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
6581 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
6582 ifname,
6583 (long long unsigned int) p2pdev_info.wdev_id);
6584 } else {
6585 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006586 0, NULL, NULL, use_existing);
6587 if (use_existing && ifidx == -ENFILE) {
6588 added = 0;
6589 ifidx = if_nametoindex(ifname);
6590 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006591 return -1;
6592 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006593 }
6594
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006595 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006596 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006597 os_memcpy(if_addr, bss->addr, ETH_ALEN);
6598 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006599 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006600 if (added)
6601 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006602 return -1;
6603 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006604 }
6605
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006606 if (!addr &&
6607 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006608 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
6609 type == WPA_IF_STATION)) {
6610 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006611 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006612
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006613 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006614 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006615 if (added)
6616 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006617 return -1;
6618 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006619 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006620 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006621 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006622 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006623 if (added)
6624 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006625 return -1;
6626 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006627 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006628 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006629 if (added)
6630 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006631 return -1;
6632 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006633 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006634 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006635 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006636
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006637 if (type == WPA_IF_AP_BSS && setup_ap) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006638 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
6639 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006640 if (added)
6641 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006642 return -1;
6643 }
6644
6645 if (bridge &&
6646 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
6647 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
6648 "interface %s to a bridge %s",
6649 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006650 if (added)
6651 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006652 os_free(new_bss);
6653 return -1;
6654 }
6655
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006656 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
6657 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006658 if (added)
6659 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006660 os_free(new_bss);
6661 return -1;
6662 }
6663 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006664 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006665 new_bss->ifindex = ifidx;
6666 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006667 new_bss->next = drv->first_bss->next;
6668 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006669 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006670 new_bss->added_if = added;
6671 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006672 if (drv_priv)
6673 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006674 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006675
6676 /* Subscribe management frames for this WPA_IF_AP_BSS */
6677 if (nl80211_setup_ap(new_bss))
6678 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006679 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006680
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006681 if (drv->global)
6682 drv->global->if_add_ifindex = ifidx;
6683
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006684 /*
6685 * Some virtual interfaces need to process EAPOL packets and events on
6686 * the parent interface. This is used mainly with hostapd.
6687 */
6688 if (ifidx > 0 &&
6689 (drv->hostapd ||
6690 nlmode == NL80211_IFTYPE_AP_VLAN ||
6691 nlmode == NL80211_IFTYPE_WDS ||
6692 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006693 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006695 return 0;
6696}
6697
6698
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006699static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006700 enum wpa_driver_if_type type,
6701 const char *ifname)
6702{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006703 struct wpa_driver_nl80211_data *drv = bss->drv;
6704 int ifindex = if_nametoindex(ifname);
6705
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006706 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
6707 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006708 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07006709 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006710 else if (ifindex > 0 && !bss->added_if) {
6711 struct wpa_driver_nl80211_data *drv2;
6712 dl_list_for_each(drv2, &drv->global->interfaces,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006713 struct wpa_driver_nl80211_data, list) {
6714 del_ifidx(drv2, ifindex, IFIDX_ANY);
6715 del_ifidx(drv2, IFIDX_ANY, ifindex);
6716 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006717 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006718
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006719 if (type != WPA_IF_AP_BSS)
6720 return 0;
6721
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006722 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006723 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
6724 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006725 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6726 "interface %s from bridge %s: %s",
6727 bss->ifname, bss->brname, strerror(errno));
6728 }
6729 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006730 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006731 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6732 "bridge %s: %s",
6733 bss->brname, strerror(errno));
6734 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006735
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006736 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006737 struct i802_bss *tbss;
6738
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006739 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
6740 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006741 if (tbss->next == bss) {
6742 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006743 /* Unsubscribe management frames */
6744 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006745 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006746 if (!bss->added_if)
6747 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006748 os_free(bss);
6749 bss = NULL;
6750 break;
6751 }
6752 }
6753 if (bss)
6754 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
6755 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006756 } else {
6757 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
6758 nl80211_teardown_ap(bss);
6759 if (!bss->added_if && !drv->first_bss->next)
Paul Stewart092955c2017-02-06 09:13:09 -08006760 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006761 nl80211_destroy_bss(bss);
6762 if (!bss->added_if)
6763 i802_set_iface_flags(bss, 0);
6764 if (drv->first_bss->next) {
6765 drv->first_bss = drv->first_bss->next;
6766 drv->ctx = drv->first_bss->ctx;
6767 os_free(bss);
6768 } else {
6769 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
6770 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006771 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006772
6773 return 0;
6774}
6775
6776
6777static int cookie_handler(struct nl_msg *msg, void *arg)
6778{
6779 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6780 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6781 u64 *cookie = arg;
6782 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6783 genlmsg_attrlen(gnlh, 0), NULL);
6784 if (tb[NL80211_ATTR_COOKIE])
6785 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
6786 return NL_SKIP;
6787}
6788
6789
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006790static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006791 unsigned int freq, unsigned int wait,
6792 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006793 u64 *cookie_out, int no_cck, int no_ack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006794 int offchanok, const u16 *csa_offs,
6795 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006796{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006797 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006798 struct nl_msg *msg;
6799 u64 cookie;
6800 int ret = -1;
6801
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006802 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07006803 "no_ack=%d offchanok=%d",
6804 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006805 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006806
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006807 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
6808 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
6809 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
6810 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6811 drv->test_use_roc_tx) &&
6812 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
6813 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
6814 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006815 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
6816 csa_offs_len * sizeof(u16), csa_offs)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006817 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
6818 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006819
6820 cookie = 0;
6821 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6822 msg = NULL;
6823 if (ret) {
6824 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006825 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
6826 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006827 } else {
6828 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
6829 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
6830 (long long unsigned int) cookie);
6831
6832 if (cookie_out)
6833 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006834
6835 if (drv->num_send_action_cookies == MAX_SEND_ACTION_COOKIES) {
6836 wpa_printf(MSG_DEBUG,
6837 "nl80211: Drop oldest pending send action cookie 0x%llx",
6838 (long long unsigned int)
6839 drv->send_action_cookies[0]);
6840 os_memmove(&drv->send_action_cookies[0],
6841 &drv->send_action_cookies[1],
6842 (MAX_SEND_ACTION_COOKIES - 1) *
6843 sizeof(u64));
6844 drv->num_send_action_cookies--;
6845 }
6846 drv->send_action_cookies[drv->num_send_action_cookies] = cookie;
6847 drv->num_send_action_cookies++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006848 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006849
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006850fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006851 nlmsg_free(msg);
6852 return ret;
6853}
6854
6855
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006856static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
6857 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006858 unsigned int wait_time,
6859 const u8 *dst, const u8 *src,
6860 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006861 const u8 *data, size_t data_len,
6862 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006863{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006864 struct wpa_driver_nl80211_data *drv = bss->drv;
6865 int ret = -1;
6866 u8 *buf;
6867 struct ieee80211_hdr *hdr;
6868
6869 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006870 "freq=%u MHz wait=%d ms no_cck=%d)",
6871 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006872
6873 buf = os_zalloc(24 + data_len);
6874 if (buf == NULL)
6875 return ret;
6876 os_memcpy(buf + 24, data, data_len);
6877 hdr = (struct ieee80211_hdr *) buf;
6878 hdr->frame_control =
6879 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
6880 os_memcpy(hdr->addr1, dst, ETH_ALEN);
6881 os_memcpy(hdr->addr2, src, ETH_ALEN);
6882 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
6883
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006884 if (os_memcmp(bss->addr, src, ETH_ALEN) != 0) {
6885 wpa_printf(MSG_DEBUG, "nl80211: Use random TA " MACSTR,
6886 MAC2STR(src));
6887 os_memcpy(bss->rand_addr, src, ETH_ALEN);
6888 } else {
6889 os_memset(bss->rand_addr, 0, ETH_ALEN);
6890 }
6891
Dmitry Shmidt56052862013-10-04 10:23:25 -07006892 if (is_ap_interface(drv->nlmode) &&
6893 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6894 (int) freq == bss->freq || drv->device_ap_sme ||
6895 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006896 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
6897 0, freq, no_cck, 1,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006898 wait_time, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006899 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006900 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006901 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006902 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006903 no_cck, 0, 1, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006904
6905 os_free(buf);
6906 return ret;
6907}
6908
6909
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006910static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006911{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006912 struct wpa_driver_nl80211_data *drv = bss->drv;
6913 struct nl_msg *msg;
6914 int ret;
6915
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08006916 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006917 (long long unsigned int) cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006918 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006919 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006920 nlmsg_free(msg);
6921 return;
6922 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006923
6924 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006925 if (ret)
6926 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
6927 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006928}
6929
6930
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006931static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
6932{
6933 struct i802_bss *bss = priv;
6934 struct wpa_driver_nl80211_data *drv = bss->drv;
6935 unsigned int i;
6936 u64 cookie;
6937
6938 /* Cancel the last pending TX cookie */
6939 nl80211_frame_wait_cancel(bss, drv->send_action_cookie);
6940
6941 /*
6942 * Cancel the other pending TX cookies, if any. This is needed since
6943 * the driver may keep a list of all pending offchannel TX operations
6944 * and free up the radio only once they have expired or cancelled.
6945 */
6946 for (i = drv->num_send_action_cookies; i > 0; i--) {
6947 cookie = drv->send_action_cookies[i - 1];
6948 if (cookie != drv->send_action_cookie)
6949 nl80211_frame_wait_cancel(bss, cookie);
6950 }
6951 drv->num_send_action_cookies = 0;
6952}
6953
6954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006955static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
6956 unsigned int duration)
6957{
6958 struct i802_bss *bss = priv;
6959 struct wpa_driver_nl80211_data *drv = bss->drv;
6960 struct nl_msg *msg;
6961 int ret;
6962 u64 cookie;
6963
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006964 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
6965 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
6966 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
6967 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006968 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006969 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006970
6971 cookie = 0;
6972 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6973 if (ret == 0) {
6974 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
6975 "0x%llx for freq=%u MHz duration=%u",
6976 (long long unsigned int) cookie, freq, duration);
6977 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006978 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006979 return 0;
6980 }
6981 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
6982 "(freq=%d duration=%u): %d (%s)",
6983 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006984 return -1;
6985}
6986
6987
6988static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
6989{
6990 struct i802_bss *bss = priv;
6991 struct wpa_driver_nl80211_data *drv = bss->drv;
6992 struct nl_msg *msg;
6993 int ret;
6994
6995 if (!drv->pending_remain_on_chan) {
6996 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
6997 "to cancel");
6998 return -1;
6999 }
7000
7001 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
7002 "0x%llx",
7003 (long long unsigned int) drv->remain_on_chan_cookie);
7004
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007005 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
7006 if (!msg ||
7007 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
7008 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007009 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007010 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007011
7012 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7013 if (ret == 0)
7014 return 0;
7015 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
7016 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007017 return -1;
7018}
7019
7020
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007021static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007022{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007023 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007024
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007025 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007026 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07007027 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
7028 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007029 /*
7030 * Do not disable Probe Request reporting that was
7031 * enabled in nl80211_setup_ap().
7032 */
7033 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
7034 "Probe Request reporting nl_preq=%p while "
7035 "in AP mode", bss->nl_preq);
7036 } else if (bss->nl_preq) {
7037 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
7038 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007039 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007040 }
7041 return 0;
7042 }
7043
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007044 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007045 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007046 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007047 return 0;
7048 }
7049
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007050 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
7051 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007052 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007053 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
7054 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007055
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007056 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007057 (WLAN_FC_TYPE_MGMT << 2) |
7058 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007059 NULL, 0) < 0)
7060 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07007061
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007062 nl80211_register_eloop_read(&bss->nl_preq,
7063 wpa_driver_nl80211_event_receive,
7064 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007065
7066 return 0;
7067
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007068 out_err:
7069 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007070 return -1;
7071}
7072
7073
7074static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
7075 int ifindex, int disabled)
7076{
7077 struct nl_msg *msg;
7078 struct nlattr *bands, *band;
7079 int ret;
7080
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007081 wpa_printf(MSG_DEBUG,
7082 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
7083 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
7084 "no NL80211_TXRATE_LEGACY constraint");
7085
7086 msg = nl80211_ifindex_msg(drv, ifindex, 0,
7087 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007088 if (!msg)
7089 return -1;
7090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007091 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
7092 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007093 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007094
7095 /*
7096 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
7097 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
7098 * rates. All 5 GHz rates are left enabled.
7099 */
7100 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007101 if (!band ||
7102 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
7103 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
7104 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007105 nla_nest_end(msg, band);
7106
7107 nla_nest_end(msg, bands);
7108
7109 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007110 if (ret) {
7111 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
7112 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007113 } else
7114 drv->disabled_11b_rates = disabled;
7115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007116 return ret;
7117
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007118fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007119 nlmsg_free(msg);
7120 return -1;
7121}
7122
7123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007124static int wpa_driver_nl80211_deinit_ap(void *priv)
7125{
7126 struct i802_bss *bss = priv;
7127 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007128 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007129 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -08007130 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007131 bss->beacon_set = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007132
7133 /*
7134 * If the P2P GO interface was dynamically added, then it is
7135 * possible that the interface change to station is not possible.
7136 */
7137 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
7138 return 0;
7139
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007140 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007141}
7142
7143
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007144static int wpa_driver_nl80211_stop_ap(void *priv)
7145{
7146 struct i802_bss *bss = priv;
7147 struct wpa_driver_nl80211_data *drv = bss->drv;
7148 if (!is_ap_interface(drv->nlmode))
7149 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -08007150 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007151 bss->beacon_set = 0;
7152 return 0;
7153}
7154
7155
Dmitry Shmidt04949592012-07-19 12:16:46 -07007156static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
7157{
7158 struct i802_bss *bss = priv;
7159 struct wpa_driver_nl80211_data *drv = bss->drv;
7160 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
7161 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007162
7163 /*
7164 * If the P2P Client interface was dynamically added, then it is
7165 * possible that the interface change to station is not possible.
7166 */
7167 if (bss->if_dynamic)
7168 return 0;
7169
Dmitry Shmidt04949592012-07-19 12:16:46 -07007170 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
7171}
7172
7173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007174static void wpa_driver_nl80211_resume(void *priv)
7175{
7176 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007177 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007178
7179 if (i802_set_iface_flags(bss, 1))
7180 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007181
7182 if (is_p2p_net_interface(nlmode))
7183 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007184}
7185
7186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007187static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
7188{
7189 struct i802_bss *bss = priv;
7190 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007191 struct nl_msg *msg;
7192 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007193
7194 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
7195 "hysteresis=%d", threshold, hysteresis);
7196
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007197 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
7198 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
7199 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
7200 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
7201 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007202 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007203 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007204 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007205
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007206 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007207}
7208
7209
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007210static int get_channel_width(struct nl_msg *msg, void *arg)
7211{
7212 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7213 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7214 struct wpa_signal_info *sig_change = arg;
7215
7216 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7217 genlmsg_attrlen(gnlh, 0), NULL);
7218
7219 sig_change->center_frq1 = -1;
7220 sig_change->center_frq2 = -1;
7221 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
7222
7223 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
7224 sig_change->chanwidth = convert2width(
7225 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
7226 if (tb[NL80211_ATTR_CENTER_FREQ1])
7227 sig_change->center_frq1 =
7228 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
7229 if (tb[NL80211_ATTR_CENTER_FREQ2])
7230 sig_change->center_frq2 =
7231 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
7232 }
7233
7234 return NL_SKIP;
7235}
7236
7237
7238static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
7239 struct wpa_signal_info *sig)
7240{
7241 struct nl_msg *msg;
7242
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007243 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007244 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007245}
7246
7247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007248static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
7249{
7250 struct i802_bss *bss = priv;
7251 struct wpa_driver_nl80211_data *drv = bss->drv;
7252 int res;
7253
7254 os_memset(si, 0, sizeof(*si));
7255 res = nl80211_get_link_signal(drv, si);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007256 if (res) {
7257 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
7258 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
7259 return res;
7260 si->current_signal = 0;
7261 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007262
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007263 res = nl80211_get_channel_width(drv, si);
7264 if (res != 0)
7265 return res;
7266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007267 return nl80211_get_link_noise(drv, si);
7268}
7269
7270
7271static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
7272 int encrypt)
7273{
7274 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007275 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007276 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007277}
7278
7279
7280static int nl80211_set_param(void *priv, const char *param)
7281{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007282 struct i802_bss *bss = priv;
7283 struct wpa_driver_nl80211_data *drv = bss->drv;
7284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007285 if (param == NULL)
7286 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007287 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007288
7289#ifdef CONFIG_P2P
7290 if (os_strstr(param, "use_p2p_group_interface=1")) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007291 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
7292 "interface");
7293 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
7294 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
7295 }
7296#endif /* CONFIG_P2P */
7297
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007298 if (os_strstr(param, "use_monitor=1"))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007299 drv->use_monitor = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007300
7301 if (os_strstr(param, "force_connect_cmd=1")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007302 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007303 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007304 }
7305
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007306 if (os_strstr(param, "force_bss_selection=1"))
7307 drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
7308
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08007309 if (os_strstr(param, "no_offchannel_tx=1")) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08007310 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
7311 drv->test_use_roc_tx = 1;
7312 }
7313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007314 return 0;
7315}
7316
7317
Dmitry Shmidte4663042016-04-04 10:07:49 -07007318static void * nl80211_global_init(void *ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007319{
7320 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007321 struct netlink_config *cfg;
7322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007323 global = os_zalloc(sizeof(*global));
7324 if (global == NULL)
7325 return NULL;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007326 global->ctx = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007327 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007328 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007329 global->if_add_ifindex = -1;
7330
7331 cfg = os_zalloc(sizeof(*cfg));
7332 if (cfg == NULL)
7333 goto err;
7334
7335 cfg->ctx = global;
7336 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
7337 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
7338 global->netlink = netlink_init(cfg);
7339 if (global->netlink == NULL) {
7340 os_free(cfg);
7341 goto err;
7342 }
7343
7344 if (wpa_driver_nl80211_init_nl_global(global) < 0)
7345 goto err;
7346
7347 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
7348 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007349 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
7350 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007351 goto err;
7352 }
7353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007354 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007355
7356err:
7357 nl80211_global_deinit(global);
7358 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007359}
7360
7361
7362static void nl80211_global_deinit(void *priv)
7363{
7364 struct nl80211_global *global = priv;
7365 if (global == NULL)
7366 return;
7367 if (!dl_list_empty(&global->interfaces)) {
7368 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
7369 "nl80211_global_deinit",
7370 dl_list_len(&global->interfaces));
7371 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007372
7373 if (global->netlink)
7374 netlink_deinit(global->netlink);
7375
7376 nl_destroy_handles(&global->nl);
7377
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007378 if (global->nl_event)
7379 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007380
7381 nl_cb_put(global->nl_cb);
7382
7383 if (global->ioctl_sock >= 0)
7384 close(global->ioctl_sock);
7385
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007386 os_free(global);
7387}
7388
7389
7390static const char * nl80211_get_radio_name(void *priv)
7391{
7392 struct i802_bss *bss = priv;
7393 struct wpa_driver_nl80211_data *drv = bss->drv;
7394 return drv->phyname;
7395}
7396
7397
Jouni Malinen75ecf522011-06-27 15:19:46 -07007398static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
7399 const u8 *pmkid)
7400{
7401 struct nl_msg *msg;
7402
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007403 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
7404 (pmkid && nla_put(msg, NL80211_ATTR_PMKID, 16, pmkid)) ||
7405 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))) {
7406 nlmsg_free(msg);
7407 return -ENOBUFS;
7408 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07007409
7410 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007411}
7412
7413
7414static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
7415{
7416 struct i802_bss *bss = priv;
7417 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
7418 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
7419}
7420
7421
7422static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
7423{
7424 struct i802_bss *bss = priv;
7425 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
7426 MAC2STR(bssid));
7427 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
7428}
7429
7430
7431static int nl80211_flush_pmkid(void *priv)
7432{
7433 struct i802_bss *bss = priv;
7434 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
7435 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
7436}
7437
7438
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007439static void clean_survey_results(struct survey_results *survey_results)
7440{
7441 struct freq_survey *survey, *tmp;
7442
7443 if (dl_list_empty(&survey_results->survey_list))
7444 return;
7445
7446 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
7447 struct freq_survey, list) {
7448 dl_list_del(&survey->list);
7449 os_free(survey);
7450 }
7451}
7452
7453
7454static void add_survey(struct nlattr **sinfo, u32 ifidx,
7455 struct dl_list *survey_list)
7456{
7457 struct freq_survey *survey;
7458
7459 survey = os_zalloc(sizeof(struct freq_survey));
7460 if (!survey)
7461 return;
7462
7463 survey->ifidx = ifidx;
7464 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7465 survey->filled = 0;
7466
7467 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
7468 survey->nf = (int8_t)
7469 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
7470 survey->filled |= SURVEY_HAS_NF;
7471 }
7472
7473 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
7474 survey->channel_time =
7475 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
7476 survey->filled |= SURVEY_HAS_CHAN_TIME;
7477 }
7478
7479 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
7480 survey->channel_time_busy =
7481 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
7482 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
7483 }
7484
7485 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
7486 survey->channel_time_rx =
7487 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
7488 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
7489 }
7490
7491 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
7492 survey->channel_time_tx =
7493 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
7494 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
7495 }
7496
7497 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)",
7498 survey->freq,
7499 survey->nf,
7500 (unsigned long int) survey->channel_time,
7501 (unsigned long int) survey->channel_time_busy,
7502 (unsigned long int) survey->channel_time_tx,
7503 (unsigned long int) survey->channel_time_rx,
7504 survey->filled);
7505
7506 dl_list_add_tail(survey_list, &survey->list);
7507}
7508
7509
7510static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
7511 unsigned int freq_filter)
7512{
7513 if (!freq_filter)
7514 return 1;
7515
7516 return freq_filter == surveyed_freq;
7517}
7518
7519
7520static int survey_handler(struct nl_msg *msg, void *arg)
7521{
7522 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7523 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7524 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
7525 struct survey_results *survey_results;
7526 u32 surveyed_freq = 0;
7527 u32 ifidx;
7528
7529 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
7530 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
7531 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
7532 };
7533
7534 survey_results = (struct survey_results *) arg;
7535
7536 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7537 genlmsg_attrlen(gnlh, 0), NULL);
7538
Dmitry Shmidt97672262014-02-03 13:02:54 -08007539 if (!tb[NL80211_ATTR_IFINDEX])
7540 return NL_SKIP;
7541
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007542 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
7543
7544 if (!tb[NL80211_ATTR_SURVEY_INFO])
7545 return NL_SKIP;
7546
7547 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
7548 tb[NL80211_ATTR_SURVEY_INFO],
7549 survey_policy))
7550 return NL_SKIP;
7551
7552 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
7553 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
7554 return NL_SKIP;
7555 }
7556
7557 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7558
7559 if (!check_survey_ok(sinfo, surveyed_freq,
7560 survey_results->freq_filter))
7561 return NL_SKIP;
7562
7563 if (survey_results->freq_filter &&
7564 survey_results->freq_filter != surveyed_freq) {
7565 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
7566 surveyed_freq);
7567 return NL_SKIP;
7568 }
7569
7570 add_survey(sinfo, ifidx, &survey_results->survey_list);
7571
7572 return NL_SKIP;
7573}
7574
7575
7576static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
7577{
7578 struct i802_bss *bss = priv;
7579 struct wpa_driver_nl80211_data *drv = bss->drv;
7580 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007581 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007582 union wpa_event_data data;
7583 struct survey_results *survey_results;
7584
7585 os_memset(&data, 0, sizeof(data));
7586 survey_results = &data.survey_results;
7587
7588 dl_list_init(&survey_results->survey_list);
7589
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007590 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007591 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007592 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007593
7594 if (freq)
7595 data.survey_results.freq_filter = freq;
7596
7597 do {
7598 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
7599 err = send_and_recv_msgs(drv, msg, survey_handler,
7600 survey_results);
7601 } while (err > 0);
7602
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007603 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007604 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007605 else
7606 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007607
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007608 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007609 return err;
7610}
7611
7612
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007613static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
7614 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007615 const u8 *replay_ctr)
7616{
7617 struct i802_bss *bss = priv;
7618 struct wpa_driver_nl80211_data *drv = bss->drv;
7619 struct nlattr *replay_nested;
7620 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007621 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007622
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007623 if (!drv->set_rekey_offload)
7624 return;
7625
7626 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007627 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
7628 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007629 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
7630 nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007631 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
7632 replay_ctr)) {
7633 nl80211_nlmsg_clear(msg);
7634 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007635 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007636 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007637
7638 nla_nest_end(msg, replay_nested);
7639
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007640 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
7641 if (ret == -EOPNOTSUPP) {
7642 wpa_printf(MSG_DEBUG,
7643 "nl80211: Driver does not support rekey offload");
7644 drv->set_rekey_offload = 0;
7645 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007646}
7647
7648
7649static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
7650 const u8 *addr, int qos)
7651{
7652 /* send data frame to poll STA and check whether
7653 * this frame is ACKed */
7654 struct {
7655 struct ieee80211_hdr hdr;
7656 u16 qos_ctl;
7657 } STRUCT_PACKED nulldata;
7658 size_t size;
7659
7660 /* Send data frame to poll STA and check whether this frame is ACKed */
7661
7662 os_memset(&nulldata, 0, sizeof(nulldata));
7663
7664 if (qos) {
7665 nulldata.hdr.frame_control =
7666 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7667 WLAN_FC_STYPE_QOS_NULL);
7668 size = sizeof(nulldata);
7669 } else {
7670 nulldata.hdr.frame_control =
7671 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7672 WLAN_FC_STYPE_NULLFUNC);
7673 size = sizeof(struct ieee80211_hdr);
7674 }
7675
7676 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
7677 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
7678 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
7679 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
7680
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007681 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007682 0, 0, NULL, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007683 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
7684 "send poll frame");
7685}
7686
7687static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
7688 int qos)
7689{
7690 struct i802_bss *bss = priv;
7691 struct wpa_driver_nl80211_data *drv = bss->drv;
7692 struct nl_msg *msg;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007693 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007694
7695 if (!drv->poll_command_supported) {
7696 nl80211_send_null_frame(bss, own_addr, addr, qos);
7697 return;
7698 }
7699
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007700 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
7701 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7702 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007703 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007704 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007705
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007706 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7707 if (ret < 0) {
7708 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
7709 MACSTR " failed: ret=%d (%s)",
7710 MAC2STR(addr), ret, strerror(-ret));
7711 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007712}
7713
7714
7715static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
7716{
7717 struct nl_msg *msg;
7718
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007719 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
7720 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
7721 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
7722 nlmsg_free(msg);
7723 return -ENOBUFS;
7724 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007725 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007726}
7727
7728
7729static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
7730 int ctwindow)
7731{
7732 struct i802_bss *bss = priv;
7733
7734 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
7735 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
7736
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007737 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007738#ifdef ANDROID_P2P
7739 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007740#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007741 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007742#endif /* ANDROID_P2P */
7743 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007744
7745 if (legacy_ps == -1)
7746 return 0;
7747 if (legacy_ps != 0 && legacy_ps != 1)
7748 return -1; /* Not yet supported */
7749
7750 return nl80211_set_power_save(bss, legacy_ps);
7751}
7752
7753
Dmitry Shmidt051af732013-10-22 13:52:46 -07007754static int nl80211_start_radar_detection(void *priv,
7755 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007756{
7757 struct i802_bss *bss = priv;
7758 struct wpa_driver_nl80211_data *drv = bss->drv;
7759 struct nl_msg *msg;
7760 int ret;
7761
Dmitry Shmidt051af732013-10-22 13:52:46 -07007762 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)",
7763 freq->freq, freq->ht_enabled, freq->vht_enabled,
7764 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7765
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007766 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
7767 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
7768 "detection");
7769 return -1;
7770 }
7771
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007772 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
7773 nl80211_put_freq_params(msg, freq) < 0) {
7774 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007775 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007776 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007777
7778 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7779 if (ret == 0)
7780 return 0;
7781 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
7782 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007783 return -1;
7784}
7785
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007786#ifdef CONFIG_TDLS
7787
7788static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
7789 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007790 u32 peer_capab, int initiator, const u8 *buf,
7791 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007792{
7793 struct i802_bss *bss = priv;
7794 struct wpa_driver_nl80211_data *drv = bss->drv;
7795 struct nl_msg *msg;
7796
7797 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7798 return -EOPNOTSUPP;
7799
7800 if (!dst)
7801 return -EINVAL;
7802
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007803 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
7804 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7805 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
7806 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
7807 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
7808 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007809 if (peer_capab) {
7810 /*
7811 * The internal enum tdls_peer_capability definition is
7812 * currently identical with the nl80211 enum
7813 * nl80211_tdls_peer_capability, so no conversion is needed
7814 * here.
7815 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007816 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
7817 peer_capab))
7818 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007819 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007820 if ((initiator &&
7821 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
7822 nla_put(msg, NL80211_ATTR_IE, len, buf))
7823 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007824
7825 return send_and_recv_msgs(drv, msg, NULL, NULL);
7826
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007827fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007828 nlmsg_free(msg);
7829 return -ENOBUFS;
7830}
7831
7832
7833static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
7834{
7835 struct i802_bss *bss = priv;
7836 struct wpa_driver_nl80211_data *drv = bss->drv;
7837 struct nl_msg *msg;
7838 enum nl80211_tdls_operation nl80211_oper;
Paul Stewart092955c2017-02-06 09:13:09 -08007839 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007840
7841 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7842 return -EOPNOTSUPP;
7843
7844 switch (oper) {
7845 case TDLS_DISCOVERY_REQ:
7846 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
7847 break;
7848 case TDLS_SETUP:
7849 nl80211_oper = NL80211_TDLS_SETUP;
7850 break;
7851 case TDLS_TEARDOWN:
7852 nl80211_oper = NL80211_TDLS_TEARDOWN;
7853 break;
7854 case TDLS_ENABLE_LINK:
7855 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
7856 break;
7857 case TDLS_DISABLE_LINK:
7858 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
7859 break;
7860 case TDLS_ENABLE:
7861 return 0;
7862 case TDLS_DISABLE:
7863 return 0;
7864 default:
7865 return -EINVAL;
7866 }
7867
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007868 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
7869 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
7870 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
7871 nlmsg_free(msg);
7872 return -ENOBUFS;
7873 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007874
Paul Stewart092955c2017-02-06 09:13:09 -08007875 res = send_and_recv_msgs(drv, msg, NULL, NULL);
7876 wpa_printf(MSG_DEBUG, "nl80211: TDLS_OPER: oper=%d mac=" MACSTR
7877 " --> res=%d (%s)", nl80211_oper, MAC2STR(peer), res,
7878 strerror(-res));
7879 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007880}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007881
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007882
7883static int
7884nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
7885 const struct hostapd_freq_params *params)
7886{
7887 struct i802_bss *bss = priv;
7888 struct wpa_driver_nl80211_data *drv = bss->drv;
7889 struct nl_msg *msg;
7890 int ret = -ENOBUFS;
7891
7892 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7893 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7894 return -EOPNOTSUPP;
7895
7896 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
7897 " oper_class=%u freq=%u",
7898 MAC2STR(addr), oper_class, params->freq);
7899 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
7900 if (!msg ||
7901 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7902 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
7903 (ret = nl80211_put_freq_params(msg, params))) {
7904 nlmsg_free(msg);
7905 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
7906 return ret;
7907 }
7908
7909 return send_and_recv_msgs(drv, msg, NULL, NULL);
7910}
7911
7912
7913static int
7914nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
7915{
7916 struct i802_bss *bss = priv;
7917 struct wpa_driver_nl80211_data *drv = bss->drv;
7918 struct nl_msg *msg;
7919
7920 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7921 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7922 return -EOPNOTSUPP;
7923
7924 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
7925 MAC2STR(addr));
7926 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
7927 if (!msg ||
7928 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7929 nlmsg_free(msg);
7930 wpa_printf(MSG_DEBUG,
7931 "nl80211: Could not build TDLS cancel chan switch");
7932 return -ENOBUFS;
7933 }
7934
7935 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007936}
7937
7938#endif /* CONFIG TDLS */
7939
7940
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007941static int driver_nl80211_set_key(const char *ifname, void *priv,
7942 enum wpa_alg alg, const u8 *addr,
7943 int key_idx, int set_tx,
7944 const u8 *seq, size_t seq_len,
7945 const u8 *key, size_t key_len)
7946{
7947 struct i802_bss *bss = priv;
7948 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
7949 set_tx, seq, seq_len, key, key_len);
7950}
7951
7952
7953static int driver_nl80211_scan2(void *priv,
7954 struct wpa_driver_scan_params *params)
7955{
7956 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007957#ifdef CONFIG_DRIVER_NL80211_QCA
7958 struct wpa_driver_nl80211_data *drv = bss->drv;
7959
7960 /*
7961 * Do a vendor specific scan if possible. If only_new_results is
7962 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
7963 * cannot be achieved through a vendor scan. The below condition may
7964 * need to be modified if new scan flags are added in the future whose
7965 * functionality can only be achieved through a normal scan.
7966 */
7967 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
7968 return wpa_driver_nl80211_vendor_scan(bss, params);
7969#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007970 return wpa_driver_nl80211_scan(bss, params);
7971}
7972
7973
7974static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
7975 int reason_code)
7976{
7977 struct i802_bss *bss = priv;
7978 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
7979}
7980
7981
7982static int driver_nl80211_authenticate(void *priv,
7983 struct wpa_driver_auth_params *params)
7984{
7985 struct i802_bss *bss = priv;
7986 return wpa_driver_nl80211_authenticate(bss, params);
7987}
7988
7989
7990static void driver_nl80211_deinit(void *priv)
7991{
7992 struct i802_bss *bss = priv;
7993 wpa_driver_nl80211_deinit(bss);
7994}
7995
7996
7997static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
7998 const char *ifname)
7999{
8000 struct i802_bss *bss = priv;
8001 return wpa_driver_nl80211_if_remove(bss, type, ifname);
8002}
8003
8004
8005static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008006 size_t data_len, int noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008007 unsigned int freq,
8008 const u16 *csa_offs, size_t csa_offs_len)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008009{
8010 struct i802_bss *bss = priv;
8011 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008012 freq, 0, 0, 0, csa_offs,
8013 csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008014}
8015
8016
8017static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
8018{
8019 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008020 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008021}
8022
8023
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008024static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
8025 const char *ifname, int vlan_id)
8026{
8027 struct i802_bss *bss = priv;
8028 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
8029}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008030
8031
8032static int driver_nl80211_read_sta_data(void *priv,
8033 struct hostap_sta_driver_data *data,
8034 const u8 *addr)
8035{
8036 struct i802_bss *bss = priv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08008037
8038 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008039 return i802_read_sta_data(bss, data, addr);
8040}
8041
8042
8043static int driver_nl80211_send_action(void *priv, unsigned int freq,
8044 unsigned int wait_time,
8045 const u8 *dst, const u8 *src,
8046 const u8 *bssid,
8047 const u8 *data, size_t data_len,
8048 int no_cck)
8049{
8050 struct i802_bss *bss = priv;
8051 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
8052 bssid, data, data_len, no_cck);
8053}
8054
8055
8056static int driver_nl80211_probe_req_report(void *priv, int report)
8057{
8058 struct i802_bss *bss = priv;
8059 return wpa_driver_nl80211_probe_req_report(bss, report);
8060}
8061
8062
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008063static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
8064 const u8 *ies, size_t ies_len)
8065{
8066 int ret;
8067 struct nl_msg *msg;
8068 struct i802_bss *bss = priv;
8069 struct wpa_driver_nl80211_data *drv = bss->drv;
8070 u16 mdid = WPA_GET_LE16(md);
8071
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008072 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008073 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
8074 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
8075 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
8076 nlmsg_free(msg);
8077 return -ENOBUFS;
8078 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008079
8080 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8081 if (ret) {
8082 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
8083 "err=%d (%s)", ret, strerror(-ret));
8084 }
8085
8086 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008087}
8088
8089
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07008090static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008091{
8092 struct i802_bss *bss = priv;
8093 struct wpa_driver_nl80211_data *drv = bss->drv;
8094
8095 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
8096 return NULL;
8097
8098 return bss->addr;
8099}
8100
8101
Dmitry Shmidt56052862013-10-04 10:23:25 -07008102static const char * scan_state_str(enum scan_states scan_state)
8103{
8104 switch (scan_state) {
8105 case NO_SCAN:
8106 return "NO_SCAN";
8107 case SCAN_REQUESTED:
8108 return "SCAN_REQUESTED";
8109 case SCAN_STARTED:
8110 return "SCAN_STARTED";
8111 case SCAN_COMPLETED:
8112 return "SCAN_COMPLETED";
8113 case SCAN_ABORTED:
8114 return "SCAN_ABORTED";
8115 case SCHED_SCAN_STARTED:
8116 return "SCHED_SCAN_STARTED";
8117 case SCHED_SCAN_STOPPED:
8118 return "SCHED_SCAN_STOPPED";
8119 case SCHED_SCAN_RESULTS:
8120 return "SCHED_SCAN_RESULTS";
8121 }
8122
8123 return "??";
8124}
8125
8126
8127static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
8128{
8129 struct i802_bss *bss = priv;
8130 struct wpa_driver_nl80211_data *drv = bss->drv;
8131 int res;
8132 char *pos, *end;
8133
8134 pos = buf;
8135 end = buf + buflen;
8136
8137 res = os_snprintf(pos, end - pos,
8138 "ifindex=%d\n"
8139 "ifname=%s\n"
8140 "brname=%s\n"
8141 "addr=" MACSTR "\n"
8142 "freq=%d\n"
8143 "%s%s%s%s%s",
8144 bss->ifindex,
8145 bss->ifname,
8146 bss->brname,
8147 MAC2STR(bss->addr),
8148 bss->freq,
8149 bss->beacon_set ? "beacon_set=1\n" : "",
8150 bss->added_if_into_bridge ?
8151 "added_if_into_bridge=1\n" : "",
8152 bss->added_bridge ? "added_bridge=1\n" : "",
8153 bss->in_deinit ? "in_deinit=1\n" : "",
8154 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008155 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008156 return pos - buf;
8157 pos += res;
8158
8159 if (bss->wdev_id_set) {
8160 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
8161 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008162 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008163 return pos - buf;
8164 pos += res;
8165 }
8166
8167 res = os_snprintf(pos, end - pos,
8168 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008169 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07008170 "drv_ifindex=%d\n"
8171 "operstate=%d\n"
8172 "scan_state=%s\n"
8173 "auth_bssid=" MACSTR "\n"
8174 "auth_attempt_bssid=" MACSTR "\n"
8175 "bssid=" MACSTR "\n"
8176 "prev_bssid=" MACSTR "\n"
8177 "associated=%d\n"
8178 "assoc_freq=%u\n"
8179 "monitor_sock=%d\n"
8180 "monitor_ifidx=%d\n"
8181 "monitor_refcount=%d\n"
8182 "last_mgmt_freq=%u\n"
8183 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008184 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008185 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008186 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -07008187 drv->ifindex,
8188 drv->operstate,
8189 scan_state_str(drv->scan_state),
8190 MAC2STR(drv->auth_bssid),
8191 MAC2STR(drv->auth_attempt_bssid),
8192 MAC2STR(drv->bssid),
8193 MAC2STR(drv->prev_bssid),
8194 drv->associated,
8195 drv->assoc_freq,
8196 drv->monitor_sock,
8197 drv->monitor_ifidx,
8198 drv->monitor_refcount,
8199 drv->last_mgmt_freq,
8200 drv->eapol_tx_sock,
8201 drv->ignore_if_down_event ?
8202 "ignore_if_down_event=1\n" : "",
8203 drv->scan_complete_events ?
8204 "scan_complete_events=1\n" : "",
8205 drv->disabled_11b_rates ?
8206 "disabled_11b_rates=1\n" : "",
8207 drv->pending_remain_on_chan ?
8208 "pending_remain_on_chan=1\n" : "",
8209 drv->in_interface_list ? "in_interface_list=1\n" : "",
8210 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
8211 drv->poll_command_supported ?
8212 "poll_command_supported=1\n" : "",
8213 drv->data_tx_status ? "data_tx_status=1\n" : "",
8214 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
8215 drv->retry_auth ? "retry_auth=1\n" : "",
8216 drv->use_monitor ? "use_monitor=1\n" : "",
8217 drv->ignore_next_local_disconnect ?
8218 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07008219 drv->ignore_next_local_deauth ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008220 "ignore_next_local_deauth=1\n" : "");
8221 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008222 return pos - buf;
8223 pos += res;
8224
8225 if (drv->has_capability) {
8226 res = os_snprintf(pos, end - pos,
8227 "capa.key_mgmt=0x%x\n"
8228 "capa.enc=0x%x\n"
8229 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008230 "capa.flags=0x%llx\n"
8231 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07008232 "capa.max_scan_ssids=%d\n"
8233 "capa.max_sched_scan_ssids=%d\n"
8234 "capa.sched_scan_supported=%d\n"
8235 "capa.max_match_sets=%d\n"
8236 "capa.max_remain_on_chan=%u\n"
8237 "capa.max_stations=%u\n"
8238 "capa.probe_resp_offloads=0x%x\n"
8239 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008240 "capa.num_multichan_concurrent=%u\n"
8241 "capa.mac_addr_rand_sched_scan_supported=%d\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008242 "capa.mac_addr_rand_scan_supported=%d\n"
8243 "capa.conc_capab=%u\n"
8244 "capa.max_conc_chan_2_4=%u\n"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008245 "capa.max_conc_chan_5_0=%u\n"
8246 "capa.max_sched_scan_plans=%u\n"
8247 "capa.max_sched_scan_plan_interval=%u\n"
8248 "capa.max_sched_scan_plan_iterations=%u\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008249 drv->capa.key_mgmt,
8250 drv->capa.enc,
8251 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008252 (unsigned long long) drv->capa.flags,
8253 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008254 drv->capa.max_scan_ssids,
8255 drv->capa.max_sched_scan_ssids,
8256 drv->capa.sched_scan_supported,
8257 drv->capa.max_match_sets,
8258 drv->capa.max_remain_on_chan,
8259 drv->capa.max_stations,
8260 drv->capa.probe_resp_offloads,
8261 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008262 drv->capa.num_multichan_concurrent,
8263 drv->capa.mac_addr_rand_sched_scan_supported,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008264 drv->capa.mac_addr_rand_scan_supported,
8265 drv->capa.conc_capab,
8266 drv->capa.max_conc_chan_2_4,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008267 drv->capa.max_conc_chan_5_0,
8268 drv->capa.max_sched_scan_plans,
8269 drv->capa.max_sched_scan_plan_interval,
8270 drv->capa.max_sched_scan_plan_iterations);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008271 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008272 return pos - buf;
8273 pos += res;
8274 }
8275
8276 return pos - buf;
8277}
8278
8279
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008280static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
8281{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008282 if ((settings->head &&
8283 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
8284 settings->head_len, settings->head)) ||
8285 (settings->tail &&
8286 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
8287 settings->tail_len, settings->tail)) ||
8288 (settings->beacon_ies &&
8289 nla_put(msg, NL80211_ATTR_IE,
8290 settings->beacon_ies_len, settings->beacon_ies)) ||
8291 (settings->proberesp_ies &&
8292 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
8293 settings->proberesp_ies_len, settings->proberesp_ies)) ||
8294 (settings->assocresp_ies &&
8295 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
8296 settings->assocresp_ies_len, settings->assocresp_ies)) ||
8297 (settings->probe_resp &&
8298 nla_put(msg, NL80211_ATTR_PROBE_RESP,
8299 settings->probe_resp_len, settings->probe_resp)))
8300 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008301
8302 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008303}
8304
8305
8306static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
8307{
8308 struct nl_msg *msg;
8309 struct i802_bss *bss = priv;
8310 struct wpa_driver_nl80211_data *drv = bss->drv;
8311 struct nlattr *beacon_csa;
8312 int ret = -ENOBUFS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008313 int csa_off_len = 0;
8314 int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008315
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008316 wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008317 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008318 settings->freq_params.freq, settings->freq_params.bandwidth,
8319 settings->freq_params.center_freq1,
8320 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008321
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008322 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008323 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
8324 return -EOPNOTSUPP;
8325 }
8326
8327 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
8328 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
8329 return -EOPNOTSUPP;
8330
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008331 /*
8332 * Remove empty counters, assuming Probe Response and Beacon frame
8333 * counters match. This implementation assumes that there are only two
8334 * counters.
8335 */
8336 if (settings->counter_offset_beacon[0] &&
8337 !settings->counter_offset_beacon[1]) {
8338 csa_off_len = 1;
8339 } else if (settings->counter_offset_beacon[1] &&
8340 !settings->counter_offset_beacon[0]) {
8341 csa_off_len = 1;
8342 settings->counter_offset_beacon[0] =
8343 settings->counter_offset_beacon[1];
8344 settings->counter_offset_presp[0] =
8345 settings->counter_offset_presp[1];
8346 } else if (settings->counter_offset_beacon[1] &&
8347 settings->counter_offset_beacon[0]) {
8348 csa_off_len = 2;
8349 } else {
8350 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
8351 return -EINVAL;
8352 }
8353
8354 /* Check CSA counters validity */
8355 if (drv->capa.max_csa_counters &&
8356 csa_off_len > drv->capa.max_csa_counters) {
8357 wpa_printf(MSG_ERROR,
8358 "nl80211: Too many CSA counters provided");
8359 return -EINVAL;
8360 }
8361
8362 if (!settings->beacon_csa.tail)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008363 return -EINVAL;
8364
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008365 for (i = 0; i < csa_off_len; i++) {
8366 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
8367 u16 csa_c_off_presp = settings->counter_offset_presp[i];
8368
8369 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
8370 (settings->beacon_csa.tail[csa_c_off_bcn] !=
8371 settings->cs_count))
8372 return -EINVAL;
8373
8374 if (settings->beacon_csa.probe_resp &&
8375 ((settings->beacon_csa.probe_resp_len <=
8376 csa_c_off_presp) ||
8377 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
8378 settings->cs_count)))
8379 return -EINVAL;
8380 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008381
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008382 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
8383 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
8384 settings->cs_count) ||
8385 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
8386 (settings->block_tx &&
8387 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008388 goto error;
8389
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008390 /* beacon_after params */
8391 ret = set_beacon_data(msg, &settings->beacon_after);
8392 if (ret)
8393 goto error;
8394
8395 /* beacon_csa params */
8396 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
8397 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008398 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008399
8400 ret = set_beacon_data(msg, &settings->beacon_csa);
8401 if (ret)
8402 goto error;
8403
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008404 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
8405 csa_off_len * sizeof(u16),
8406 settings->counter_offset_beacon) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008407 (settings->beacon_csa.probe_resp &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008408 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
8409 csa_off_len * sizeof(u16),
8410 settings->counter_offset_presp)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008411 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008412
8413 nla_nest_end(msg, beacon_csa);
8414 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8415 if (ret) {
8416 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
8417 ret, strerror(-ret));
8418 }
8419 return ret;
8420
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008421fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008422 ret = -ENOBUFS;
8423error:
8424 nlmsg_free(msg);
8425 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
8426 return ret;
8427}
8428
8429
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008430static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
8431 u8 user_priority, u16 admitted_time)
8432{
8433 struct i802_bss *bss = priv;
8434 struct wpa_driver_nl80211_data *drv = bss->drv;
8435 struct nl_msg *msg;
8436 int ret;
8437
8438 wpa_printf(MSG_DEBUG,
8439 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
8440 tsid, admitted_time, user_priority);
8441
8442 if (!is_sta_interface(drv->nlmode))
8443 return -ENOTSUP;
8444
8445 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
8446 if (!msg ||
8447 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8448 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8449 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
8450 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
8451 nlmsg_free(msg);
8452 return -ENOBUFS;
8453 }
8454
8455 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8456 if (ret)
8457 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
8458 ret, strerror(-ret));
8459 return ret;
8460}
8461
8462
8463static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
8464{
8465 struct i802_bss *bss = priv;
8466 struct wpa_driver_nl80211_data *drv = bss->drv;
8467 struct nl_msg *msg;
8468 int ret;
8469
8470 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
8471
8472 if (!is_sta_interface(drv->nlmode))
8473 return -ENOTSUP;
8474
8475 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
8476 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8477 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8478 nlmsg_free(msg);
8479 return -ENOBUFS;
8480 }
8481
8482 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8483 if (ret)
8484 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
8485 ret, strerror(-ret));
8486 return ret;
8487}
8488
8489
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008490#ifdef CONFIG_TESTING_OPTIONS
8491static int cmd_reply_handler(struct nl_msg *msg, void *arg)
8492{
8493 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8494 struct wpabuf *buf = arg;
8495
8496 if (!buf)
8497 return NL_SKIP;
8498
8499 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
8500 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
8501 return NL_SKIP;
8502 }
8503
8504 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
8505 genlmsg_attrlen(gnlh, 0));
8506
8507 return NL_SKIP;
8508}
8509#endif /* CONFIG_TESTING_OPTIONS */
8510
8511
8512static int vendor_reply_handler(struct nl_msg *msg, void *arg)
8513{
8514 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8515 struct nlattr *nl_vendor_reply, *nl;
8516 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8517 struct wpabuf *buf = arg;
8518 int rem;
8519
8520 if (!buf)
8521 return NL_SKIP;
8522
8523 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8524 genlmsg_attrlen(gnlh, 0), NULL);
8525 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
8526
8527 if (!nl_vendor_reply)
8528 return NL_SKIP;
8529
8530 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
8531 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
8532 return NL_SKIP;
8533 }
8534
8535 nla_for_each_nested(nl, nl_vendor_reply, rem) {
8536 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
8537 }
8538
8539 return NL_SKIP;
8540}
8541
8542
8543static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
8544 unsigned int subcmd, const u8 *data,
8545 size_t data_len, struct wpabuf *buf)
8546{
8547 struct i802_bss *bss = priv;
8548 struct wpa_driver_nl80211_data *drv = bss->drv;
8549 struct nl_msg *msg;
8550 int ret;
8551
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008552#ifdef CONFIG_TESTING_OPTIONS
8553 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008554 msg = nlmsg_alloc();
8555 if (!msg)
8556 return -ENOMEM;
8557
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008558 nl80211_cmd(drv, msg, 0, subcmd);
8559 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
8560 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008561 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008562 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
8563 if (ret)
8564 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
8565 ret);
8566 return ret;
8567 }
8568#endif /* CONFIG_TESTING_OPTIONS */
8569
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008570 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
8571 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
8572 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
8573 (data &&
8574 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
8575 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008576
8577 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
8578 if (ret)
8579 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
8580 ret);
8581 return ret;
8582
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008583fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008584 nlmsg_free(msg);
8585 return -ENOBUFS;
8586}
8587
8588
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008589static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
8590 u8 qos_map_set_len)
8591{
8592 struct i802_bss *bss = priv;
8593 struct wpa_driver_nl80211_data *drv = bss->drv;
8594 struct nl_msg *msg;
8595 int ret;
8596
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008597 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
8598 qos_map_set, qos_map_set_len);
8599
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008600 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
8601 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
8602 nlmsg_free(msg);
8603 return -ENOBUFS;
8604 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008605
8606 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8607 if (ret)
8608 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
8609
8610 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008611}
8612
8613
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008614static int nl80211_set_wowlan(void *priv,
8615 const struct wowlan_triggers *triggers)
8616{
8617 struct i802_bss *bss = priv;
8618 struct wpa_driver_nl80211_data *drv = bss->drv;
8619 struct nl_msg *msg;
8620 struct nlattr *wowlan_triggers;
8621 int ret;
8622
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008623 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
8624
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008625 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008626 !(wowlan_triggers = nla_nest_start(msg,
8627 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
8628 (triggers->any &&
8629 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
8630 (triggers->disconnect &&
8631 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
8632 (triggers->magic_pkt &&
8633 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
8634 (triggers->gtk_rekey_failure &&
8635 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
8636 (triggers->eap_identity_req &&
8637 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
8638 (triggers->four_way_handshake &&
8639 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
8640 (triggers->rfkill_release &&
8641 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
8642 nlmsg_free(msg);
8643 return -ENOBUFS;
8644 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008645
8646 nla_nest_end(msg, wowlan_triggers);
8647
8648 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8649 if (ret)
8650 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
8651
8652 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008653}
8654
8655
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008656#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008657static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
8658{
8659 struct i802_bss *bss = priv;
8660 struct wpa_driver_nl80211_data *drv = bss->drv;
8661 struct nl_msg *msg;
8662 struct nlattr *params;
8663
8664 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
8665
8666 if (!drv->roaming_vendor_cmd_avail) {
8667 wpa_printf(MSG_DEBUG,
8668 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
8669 return -1;
8670 }
8671
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008672 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8673 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8674 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8675 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
8676 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8677 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
8678 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
8679 QCA_ROAMING_NOT_ALLOWED) ||
8680 (bssid &&
8681 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
8682 nlmsg_free(msg);
8683 return -1;
8684 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008685 nla_nest_end(msg, params);
8686
8687 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008688}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008689#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008690
8691
8692static int nl80211_set_mac_addr(void *priv, const u8 *addr)
8693{
8694 struct i802_bss *bss = priv;
8695 struct wpa_driver_nl80211_data *drv = bss->drv;
8696 int new_addr = addr != NULL;
8697
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008698 if (TEST_FAIL())
8699 return -1;
8700
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008701 if (!addr)
8702 addr = drv->perm_addr;
8703
8704 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
8705 return -1;
8706
8707 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
8708 {
8709 wpa_printf(MSG_DEBUG,
8710 "nl80211: failed to set_mac_addr for %s to " MACSTR,
8711 bss->ifname, MAC2STR(addr));
8712 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
8713 1) < 0) {
8714 wpa_printf(MSG_DEBUG,
8715 "nl80211: Could not restore interface UP after failed set_mac_addr");
8716 }
8717 return -1;
8718 }
8719
8720 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
8721 bss->ifname, MAC2STR(addr));
8722 drv->addr_changed = new_addr;
8723 os_memcpy(bss->addr, addr, ETH_ALEN);
8724
8725 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
8726 {
8727 wpa_printf(MSG_DEBUG,
8728 "nl80211: Could not restore interface UP after set_mac_addr");
8729 }
8730
8731 return 0;
8732}
8733
8734
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008735#ifdef CONFIG_MESH
8736
8737static int wpa_driver_nl80211_init_mesh(void *priv)
8738{
8739 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
8740 wpa_printf(MSG_INFO,
8741 "nl80211: Failed to set interface into mesh mode");
8742 return -1;
8743 }
8744 return 0;
8745}
8746
8747
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008748static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
8749 size_t mesh_id_len)
8750{
8751 if (mesh_id) {
8752 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
8753 mesh_id, mesh_id_len);
8754 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
8755 }
8756
8757 return 0;
8758}
8759
8760
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07008761static int nl80211_put_mesh_config(struct nl_msg *msg,
8762 struct wpa_driver_mesh_bss_params *params)
8763{
8764 struct nlattr *container;
8765
8766 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
8767 if (!container)
8768 return -1;
8769
8770 if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
8771 nla_put_u32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8772 params->auto_plinks)) ||
8773 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
8774 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
8775 params->max_peer_links)))
8776 return -1;
8777
8778 /*
8779 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
8780 * the timer could disconnect stations even in that case.
8781 */
8782 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
8783 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
8784 params->peer_link_timeout)) {
8785 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
8786 return -1;
8787 }
8788
8789 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
8790 nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
8791 wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
8792 return -1;
8793 }
8794
8795 nla_nest_end(msg, container);
8796
8797 return 0;
8798}
8799
8800
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008801static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008802 struct wpa_driver_mesh_join_params *params)
8803{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008804 struct wpa_driver_nl80211_data *drv = bss->drv;
8805 struct nl_msg *msg;
8806 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008807 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008808
8809 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
8810 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008811 if (!msg ||
8812 nl80211_put_freq_params(msg, &params->freq) ||
8813 nl80211_put_basic_rates(msg, params->basic_rates) ||
8814 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07008815 nl80211_put_beacon_int(msg, params->beacon_int) ||
8816 nl80211_put_dtim_period(msg, params->dtim_period))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008817 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008818
8819 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
8820
8821 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
8822 if (!container)
8823 goto fail;
8824
8825 if (params->ies) {
8826 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
8827 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
8828 params->ies))
8829 goto fail;
8830 }
8831 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
8832 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
8833 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
8834 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
8835 goto fail;
8836 }
8837 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
8838 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
8839 goto fail;
8840 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
8841 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
8842 goto fail;
8843 nla_nest_end(msg, container);
8844
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07008845 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
8846 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
8847 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
8848 if (nl80211_put_mesh_config(msg, &params->conf) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008849 goto fail;
8850
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008851 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8852 msg = NULL;
8853 if (ret) {
8854 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
8855 ret, strerror(-ret));
8856 goto fail;
8857 }
8858 ret = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07008859 drv->assoc_freq = bss->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008860 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
8861
8862fail:
8863 nlmsg_free(msg);
8864 return ret;
8865}
8866
8867
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008868static int
8869wpa_driver_nl80211_join_mesh(void *priv,
8870 struct wpa_driver_mesh_join_params *params)
8871{
8872 struct i802_bss *bss = priv;
8873 int ret, timeout;
8874
8875 timeout = params->conf.peer_link_timeout;
8876
8877 /* Disable kernel inactivity timer */
8878 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
8879 params->conf.peer_link_timeout = 0;
8880
8881 ret = nl80211_join_mesh(bss, params);
8882 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
8883 wpa_printf(MSG_DEBUG,
8884 "nl80211: Mesh join retry for peer_link_timeout");
8885 /*
8886 * Old kernel does not support setting
8887 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
8888 * into future from peer_link_timeout.
8889 */
8890 params->conf.peer_link_timeout = timeout + 60;
8891 ret = nl80211_join_mesh(priv, params);
8892 }
8893
8894 params->conf.peer_link_timeout = timeout;
8895 return ret;
8896}
8897
8898
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008899static int wpa_driver_nl80211_leave_mesh(void *priv)
8900{
8901 struct i802_bss *bss = priv;
8902 struct wpa_driver_nl80211_data *drv = bss->drv;
8903 struct nl_msg *msg;
8904 int ret;
8905
8906 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
8907 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
8908 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8909 if (ret) {
8910 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
8911 ret, strerror(-ret));
8912 } else {
8913 wpa_printf(MSG_DEBUG,
8914 "nl80211: mesh leave request send successfully");
8915 }
8916
8917 if (wpa_driver_nl80211_set_mode(drv->first_bss,
8918 NL80211_IFTYPE_STATION)) {
8919 wpa_printf(MSG_INFO,
8920 "nl80211: Failed to set interface into station mode");
8921 }
8922 return ret;
8923}
8924
8925#endif /* CONFIG_MESH */
8926
8927
8928static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
8929 const u8 *ipaddr, int prefixlen,
8930 const u8 *addr)
8931{
8932#ifdef CONFIG_LIBNL3_ROUTE
8933 struct i802_bss *bss = priv;
8934 struct wpa_driver_nl80211_data *drv = bss->drv;
8935 struct rtnl_neigh *rn;
8936 struct nl_addr *nl_ipaddr = NULL;
8937 struct nl_addr *nl_lladdr = NULL;
8938 int family, addrsize;
8939 int res;
8940
8941 if (!ipaddr || prefixlen == 0 || !addr)
8942 return -EINVAL;
8943
8944 if (bss->br_ifindex == 0) {
8945 wpa_printf(MSG_DEBUG,
8946 "nl80211: bridge must be set before adding an ip neigh to it");
8947 return -1;
8948 }
8949
8950 if (!drv->rtnl_sk) {
8951 wpa_printf(MSG_DEBUG,
8952 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8953 return -1;
8954 }
8955
8956 if (version == 4) {
8957 family = AF_INET;
8958 addrsize = 4;
8959 } else if (version == 6) {
8960 family = AF_INET6;
8961 addrsize = 16;
8962 } else {
8963 return -EINVAL;
8964 }
8965
8966 rn = rtnl_neigh_alloc();
8967 if (rn == NULL)
8968 return -ENOMEM;
8969
8970 /* set the destination ip address for neigh */
8971 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8972 if (nl_ipaddr == NULL) {
8973 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8974 res = -ENOMEM;
8975 goto errout;
8976 }
8977 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
8978 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8979 if (res) {
8980 wpa_printf(MSG_DEBUG,
8981 "nl80211: neigh set destination addr failed");
8982 goto errout;
8983 }
8984
8985 /* set the corresponding lladdr for neigh */
8986 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
8987 if (nl_lladdr == NULL) {
8988 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
8989 res = -ENOMEM;
8990 goto errout;
8991 }
8992 rtnl_neigh_set_lladdr(rn, nl_lladdr);
8993
8994 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8995 rtnl_neigh_set_state(rn, NUD_PERMANENT);
8996
8997 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
8998 if (res) {
8999 wpa_printf(MSG_DEBUG,
9000 "nl80211: Adding bridge ip neigh failed: %s",
9001 strerror(errno));
9002 }
9003errout:
9004 if (nl_lladdr)
9005 nl_addr_put(nl_lladdr);
9006 if (nl_ipaddr)
9007 nl_addr_put(nl_ipaddr);
9008 if (rn)
9009 rtnl_neigh_put(rn);
9010 return res;
9011#else /* CONFIG_LIBNL3_ROUTE */
9012 return -1;
9013#endif /* CONFIG_LIBNL3_ROUTE */
9014}
9015
9016
9017static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
9018 const u8 *ipaddr)
9019{
9020#ifdef CONFIG_LIBNL3_ROUTE
9021 struct i802_bss *bss = priv;
9022 struct wpa_driver_nl80211_data *drv = bss->drv;
9023 struct rtnl_neigh *rn;
9024 struct nl_addr *nl_ipaddr;
9025 int family, addrsize;
9026 int res;
9027
9028 if (!ipaddr)
9029 return -EINVAL;
9030
9031 if (version == 4) {
9032 family = AF_INET;
9033 addrsize = 4;
9034 } else if (version == 6) {
9035 family = AF_INET6;
9036 addrsize = 16;
9037 } else {
9038 return -EINVAL;
9039 }
9040
9041 if (bss->br_ifindex == 0) {
9042 wpa_printf(MSG_DEBUG,
9043 "nl80211: bridge must be set to delete an ip neigh");
9044 return -1;
9045 }
9046
9047 if (!drv->rtnl_sk) {
9048 wpa_printf(MSG_DEBUG,
9049 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9050 return -1;
9051 }
9052
9053 rn = rtnl_neigh_alloc();
9054 if (rn == NULL)
9055 return -ENOMEM;
9056
9057 /* set the destination ip address for neigh */
9058 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
9059 if (nl_ipaddr == NULL) {
9060 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9061 res = -ENOMEM;
9062 goto errout;
9063 }
9064 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9065 if (res) {
9066 wpa_printf(MSG_DEBUG,
9067 "nl80211: neigh set destination addr failed");
9068 goto errout;
9069 }
9070
9071 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9072
9073 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
9074 if (res) {
9075 wpa_printf(MSG_DEBUG,
9076 "nl80211: Deleting bridge ip neigh failed: %s",
9077 strerror(errno));
9078 }
9079errout:
9080 if (nl_ipaddr)
9081 nl_addr_put(nl_ipaddr);
9082 if (rn)
9083 rtnl_neigh_put(rn);
9084 return res;
9085#else /* CONFIG_LIBNL3_ROUTE */
9086 return -1;
9087#endif /* CONFIG_LIBNL3_ROUTE */
9088}
9089
9090
9091static int linux_write_system_file(const char *path, unsigned int val)
9092{
9093 char buf[50];
9094 int fd, len;
9095
9096 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
9097 if (os_snprintf_error(sizeof(buf), len))
9098 return -1;
9099
9100 fd = open(path, O_WRONLY);
9101 if (fd < 0)
9102 return -1;
9103
9104 if (write(fd, buf, len) < 0) {
9105 wpa_printf(MSG_DEBUG,
9106 "nl80211: Failed to write Linux system file: %s with the value of %d",
9107 path, val);
9108 close(fd);
9109 return -1;
9110 }
9111 close(fd);
9112
9113 return 0;
9114}
9115
9116
9117static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
9118{
9119 switch (attr) {
9120 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07009121 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009122 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
9123 return "hairpin_mode";
9124 }
9125
9126 return NULL;
9127}
9128
9129
9130static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
9131 unsigned int val)
9132{
9133 struct i802_bss *bss = priv;
9134 char path[128];
9135 const char *attr_txt;
9136
9137 attr_txt = drv_br_port_attr_str(attr);
9138 if (attr_txt == NULL)
9139 return -EINVAL;
9140
9141 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
9142 bss->ifname, attr_txt);
9143
9144 if (linux_write_system_file(path, val))
9145 return -1;
9146
9147 return 0;
9148}
9149
9150
9151static const char * drv_br_net_param_str(enum drv_br_net_param param)
9152{
9153 switch (param) {
9154 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9155 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -07009156 default:
9157 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009158 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009159}
9160
9161
9162static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
9163 unsigned int val)
9164{
9165 struct i802_bss *bss = priv;
9166 char path[128];
9167 const char *param_txt;
9168 int ip_version = 4;
9169
Dmitry Shmidt83474442015-04-15 13:47:09 -07009170 if (param == DRV_BR_MULTICAST_SNOOPING) {
9171 os_snprintf(path, sizeof(path),
9172 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
9173 bss->brname);
9174 goto set_val;
9175 }
9176
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009177 param_txt = drv_br_net_param_str(param);
9178 if (param_txt == NULL)
9179 return -EINVAL;
9180
9181 switch (param) {
9182 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9183 ip_version = 4;
9184 break;
9185 default:
9186 return -EINVAL;
9187 }
9188
9189 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
9190 ip_version, bss->brname, param_txt);
9191
Dmitry Shmidt83474442015-04-15 13:47:09 -07009192set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009193 if (linux_write_system_file(path, val))
9194 return -1;
9195
9196 return 0;
9197}
9198
9199
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009200#ifdef CONFIG_DRIVER_NL80211_QCA
9201
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009202static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
9203{
9204 switch (hw_mode) {
9205 case HOSTAPD_MODE_IEEE80211B:
9206 return QCA_ACS_MODE_IEEE80211B;
9207 case HOSTAPD_MODE_IEEE80211G:
9208 return QCA_ACS_MODE_IEEE80211G;
9209 case HOSTAPD_MODE_IEEE80211A:
9210 return QCA_ACS_MODE_IEEE80211A;
9211 case HOSTAPD_MODE_IEEE80211AD:
9212 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07009213 case HOSTAPD_MODE_IEEE80211ANY:
9214 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009215 default:
9216 return -1;
9217 }
9218}
9219
9220
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009221static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
9222{
9223 int i, len, ret;
9224 u32 *freqs;
9225
9226 if (!freq_list)
9227 return 0;
9228 len = int_array_len(freq_list);
9229 freqs = os_malloc(sizeof(u32) * len);
9230 if (!freqs)
9231 return -1;
9232 for (i = 0; i < len; i++)
9233 freqs[i] = freq_list[i];
9234 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
9235 sizeof(u32) * len, freqs);
9236 os_free(freqs);
9237 return ret;
9238}
9239
9240
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009241static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
9242{
9243 struct i802_bss *bss = priv;
9244 struct wpa_driver_nl80211_data *drv = bss->drv;
9245 struct nl_msg *msg;
9246 struct nlattr *data;
9247 int ret;
9248 int mode;
9249
9250 mode = hw_mode_to_qca_acs(params->hw_mode);
9251 if (mode < 0)
9252 return -1;
9253
9254 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9255 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9256 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9257 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
9258 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9259 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
9260 (params->ht_enabled &&
9261 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
9262 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07009263 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
9264 (params->vht_enabled &&
9265 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
9266 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
9267 params->ch_width) ||
9268 (params->ch_list_len &&
9269 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009270 params->ch_list)) ||
9271 add_acs_freq_list(msg, params->freq_list)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009272 nlmsg_free(msg);
9273 return -ENOBUFS;
9274 }
9275 nla_nest_end(msg, data);
9276
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07009277 wpa_printf(MSG_DEBUG,
9278 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
9279 params->hw_mode, params->ht_enabled, params->ht40_enabled,
9280 params->vht_enabled, params->ch_width, params->ch_list_len);
9281
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009282 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9283 if (ret) {
9284 wpa_printf(MSG_DEBUG,
9285 "nl80211: Failed to invoke driver ACS function: %s",
9286 strerror(errno));
9287 }
9288 return ret;
9289}
9290
9291
Ravi Joshie6ccb162015-07-16 17:45:41 -07009292static int nl80211_set_band(void *priv, enum set_band band)
9293{
9294 struct i802_bss *bss = priv;
9295 struct wpa_driver_nl80211_data *drv = bss->drv;
9296 struct nl_msg *msg;
9297 struct nlattr *data;
9298 int ret;
9299 enum qca_set_band qca_band;
9300
9301 if (!drv->setband_vendor_cmd_avail)
9302 return -1;
9303
9304 switch (band) {
9305 case WPA_SETBAND_AUTO:
9306 qca_band = QCA_SETBAND_AUTO;
9307 break;
9308 case WPA_SETBAND_5G:
9309 qca_band = QCA_SETBAND_5G;
9310 break;
9311 case WPA_SETBAND_2G:
9312 qca_band = QCA_SETBAND_2G;
9313 break;
9314 default:
9315 return -1;
9316 }
9317
9318 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9319 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9320 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9321 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
9322 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9323 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
9324 nlmsg_free(msg);
9325 return -ENOBUFS;
9326 }
9327 nla_nest_end(msg, data);
9328
9329 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9330 if (ret) {
9331 wpa_printf(MSG_DEBUG,
9332 "nl80211: Driver setband function failed: %s",
9333 strerror(errno));
9334 }
9335 return ret;
9336}
9337
9338
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009339struct nl80211_pcl {
9340 unsigned int num;
9341 unsigned int *freq_list;
9342};
9343
9344static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
9345{
9346 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9347 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9348 struct nl80211_pcl *param = arg;
9349 struct nlattr *nl_vend, *attr;
9350 enum qca_iface_type iface_type;
9351 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
9352 unsigned int num, max_num;
9353 u32 *freqs;
9354
9355 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9356 genlmsg_attrlen(gnlh, 0), NULL);
9357
9358 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
9359 if (!nl_vend)
9360 return NL_SKIP;
9361
9362 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
9363 nla_data(nl_vend), nla_len(nl_vend), NULL);
9364
9365 attr = tb_vendor[
9366 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
9367 if (!attr) {
9368 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
9369 param->num = 0;
9370 return NL_SKIP;
9371 }
9372
9373 iface_type = (enum qca_iface_type) nla_get_u32(attr);
9374 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
9375 iface_type);
9376
9377 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
9378 if (!attr) {
9379 wpa_printf(MSG_ERROR,
9380 "nl80211: preferred_freq_list couldn't be found");
9381 param->num = 0;
9382 return NL_SKIP;
9383 }
9384
9385 /*
9386 * param->num has the maximum number of entries for which there
9387 * is room in the freq_list provided by the caller.
9388 */
9389 freqs = nla_data(attr);
9390 max_num = nla_len(attr) / sizeof(u32);
9391 if (max_num > param->num)
9392 max_num = param->num;
9393 for (num = 0; num < max_num; num++)
9394 param->freq_list[num] = freqs[num];
9395 param->num = num;
9396
9397 return NL_SKIP;
9398}
9399
9400
9401static int nl80211_get_pref_freq_list(void *priv,
9402 enum wpa_driver_if_type if_type,
9403 unsigned int *num,
9404 unsigned int *freq_list)
9405{
9406 struct i802_bss *bss = priv;
9407 struct wpa_driver_nl80211_data *drv = bss->drv;
9408 struct nl_msg *msg;
9409 int ret;
9410 unsigned int i;
9411 struct nlattr *params;
9412 struct nl80211_pcl param;
9413 enum qca_iface_type iface_type;
9414
9415 if (!drv->get_pref_freq_list)
9416 return -1;
9417
9418 switch (if_type) {
9419 case WPA_IF_STATION:
9420 iface_type = QCA_IFACE_TYPE_STA;
9421 break;
9422 case WPA_IF_AP_BSS:
9423 iface_type = QCA_IFACE_TYPE_AP;
9424 break;
9425 case WPA_IF_P2P_GO:
9426 iface_type = QCA_IFACE_TYPE_P2P_GO;
9427 break;
9428 case WPA_IF_P2P_CLIENT:
9429 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
9430 break;
9431 case WPA_IF_IBSS:
9432 iface_type = QCA_IFACE_TYPE_IBSS;
9433 break;
9434 case WPA_IF_TDLS:
9435 iface_type = QCA_IFACE_TYPE_TDLS;
9436 break;
9437 default:
9438 return -1;
9439 }
9440
9441 param.num = *num;
9442 param.freq_list = freq_list;
9443
9444 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9445 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
9446 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9447 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9448 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
9449 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9450 nla_put_u32(msg,
9451 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
9452 iface_type)) {
9453 wpa_printf(MSG_ERROR,
9454 "%s: err in adding vendor_cmd and vendor_data",
9455 __func__);
9456 nlmsg_free(msg);
9457 return -1;
9458 }
9459 nla_nest_end(msg, params);
9460
9461 os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
9462 ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
9463 if (ret) {
9464 wpa_printf(MSG_ERROR,
9465 "%s: err in send_and_recv_msgs", __func__);
9466 return ret;
9467 }
9468
9469 *num = param.num;
9470
9471 for (i = 0; i < *num; i++) {
9472 wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
9473 i, freq_list[i]);
9474 }
9475
9476 return 0;
9477}
9478
9479
9480static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
9481{
9482 struct i802_bss *bss = priv;
9483 struct wpa_driver_nl80211_data *drv = bss->drv;
9484 struct nl_msg *msg;
9485 int ret;
9486 struct nlattr *params;
9487
9488 if (!drv->set_prob_oper_freq)
9489 return -1;
9490
9491 wpa_printf(MSG_DEBUG,
9492 "nl80211: Set P2P probable operating freq %u for ifindex %d",
9493 freq, bss->ifindex);
9494
9495 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9496 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9497 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9498 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
9499 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9500 nla_put_u32(msg,
9501 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
9502 QCA_IFACE_TYPE_P2P_CLIENT) ||
9503 nla_put_u32(msg,
9504 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
9505 freq)) {
9506 wpa_printf(MSG_ERROR,
9507 "%s: err in adding vendor_cmd and vendor_data",
9508 __func__);
9509 nlmsg_free(msg);
9510 return -1;
9511 }
9512 nla_nest_end(msg, params);
9513
9514 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9515 msg = NULL;
9516 if (ret) {
9517 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
9518 __func__);
9519 return ret;
9520 }
9521 nlmsg_free(msg);
9522 return 0;
9523}
9524
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009525
9526static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
9527 unsigned int period, unsigned int interval,
9528 unsigned int count, const u8 *device_types,
9529 size_t dev_types_len,
9530 const u8 *ies, size_t ies_len)
9531{
9532 struct i802_bss *bss = priv;
9533 struct wpa_driver_nl80211_data *drv = bss->drv;
9534 struct nl_msg *msg;
9535 struct nlattr *container;
9536 int ret;
9537
9538 wpa_printf(MSG_DEBUG,
9539 "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
9540 freq, period, interval, count);
9541
9542 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
9543 return -1;
9544
9545 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9546 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9547 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9548 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
9549 goto fail;
9550
9551 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
9552 if (!container)
9553 goto fail;
9554
9555 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
9556 freq) ||
9557 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
9558 period) ||
9559 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
9560 interval) ||
9561 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
9562 count) ||
9563 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
9564 dev_types_len, device_types) ||
9565 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
9566 ies_len, ies))
9567 goto fail;
9568
9569 nla_nest_end(msg, container);
9570 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9571 msg = NULL;
9572 if (ret) {
9573 wpa_printf(MSG_DEBUG,
9574 "nl80211: Failed to send P2P Listen offload vendor command");
9575 goto fail;
9576 }
9577
9578 return 0;
9579
9580fail:
9581 nlmsg_free(msg);
9582 return -1;
9583}
9584
9585
9586static int nl80211_p2p_lo_stop(void *priv)
9587{
9588 struct i802_bss *bss = priv;
9589 struct wpa_driver_nl80211_data *drv = bss->drv;
9590 struct nl_msg *msg;
9591
9592 wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
9593
9594 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
9595 return -1;
9596
9597 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9598 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9599 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9600 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
9601 nlmsg_free(msg);
9602 return -1;
9603 }
9604
9605 return send_and_recv_msgs(drv, msg, NULL, NULL);
9606}
9607
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08009608
9609static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
9610{
9611 struct i802_bss *bss = priv;
9612 struct wpa_driver_nl80211_data *drv = bss->drv;
9613 struct nl_msg *msg;
9614 struct nlattr *params;
9615 int ret;
9616 u32 tdls_mode;
9617
9618 wpa_printf(MSG_DEBUG,
9619 "nl80211: Set TDKS mode: tdls_external_control=%d",
9620 tdls_external_control);
9621
9622 if (tdls_external_control == 1)
9623 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
9624 QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
9625 else
9626 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
9627
9628 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9629 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9630 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9631 QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
9632 goto fail;
9633
9634 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
9635 if (!params)
9636 goto fail;
9637
9638 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
9639 tdls_mode))
9640 goto fail;
9641
9642 nla_nest_end(msg, params);
9643
9644 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9645 msg = NULL;
9646 if (ret) {
9647 wpa_printf(MSG_ERROR,
9648 "nl80211: Set TDLS mode failed: ret=%d (%s)",
9649 ret, strerror(-ret));
9650 goto fail;
9651 }
9652 return 0;
9653fail:
9654 nlmsg_free(msg);
9655 return -1;
9656}
9657
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009658#endif /* CONFIG_DRIVER_NL80211_QCA */
9659
9660
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009661static int nl80211_write_to_file(const char *name, unsigned int val)
9662{
9663 int fd, len;
9664 char tmp[128];
9665
9666 fd = open(name, O_RDWR);
9667 if (fd < 0) {
9668 wpa_printf(MSG_ERROR, "nl80211: Failed to open %s: %s",
9669 name, strerror(errno));
9670 return fd;
9671 }
9672
9673 len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
9674 len = write(fd, tmp, len);
9675 if (len < 0)
9676 wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
9677 name, strerror(errno));
9678 close(fd);
9679
9680 return 0;
9681}
9682
9683
9684static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
9685{
9686 struct i802_bss *bss = priv;
9687 char path[128];
9688 int ret;
9689
9690 wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
9691 filter_flags);
9692
9693 /* Configure filtering of unicast frame encrypted using GTK */
9694 ret = os_snprintf(path, sizeof(path),
9695 "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
9696 bss->ifname);
9697 if (os_snprintf_error(sizeof(path), ret))
9698 return -1;
9699
9700 ret = nl80211_write_to_file(path,
9701 !!(filter_flags &
9702 WPA_DATA_FRAME_FILTER_FLAG_GTK));
9703 if (ret) {
9704 wpa_printf(MSG_ERROR,
9705 "nl80211: Failed to set IPv4 unicast in multicast filter");
9706 return ret;
9707 }
9708
9709 os_snprintf(path, sizeof(path),
9710 "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
9711 bss->ifname);
9712 ret = nl80211_write_to_file(path,
9713 !!(filter_flags &
9714 WPA_DATA_FRAME_FILTER_FLAG_GTK));
9715
9716 if (ret) {
9717 wpa_printf(MSG_ERROR,
9718 "nl80211: Failed to set IPv6 unicast in multicast filter");
9719 return ret;
9720 }
9721
9722 /* Configure filtering of unicast frame encrypted using GTK */
9723 os_snprintf(path, sizeof(path),
9724 "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
9725 bss->ifname);
9726 ret = nl80211_write_to_file(path,
9727 !!(filter_flags &
9728 WPA_DATA_FRAME_FILTER_FLAG_ARP));
9729 if (ret) {
9730 wpa_printf(MSG_ERROR,
9731 "nl80211: Failed set gratuitous ARP filter");
9732 return ret;
9733 }
9734
9735 /* Configure filtering of IPv6 NA frames */
9736 os_snprintf(path, sizeof(path),
9737 "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
9738 bss->ifname);
9739 ret = nl80211_write_to_file(path,
9740 !!(filter_flags &
9741 WPA_DATA_FRAME_FILTER_FLAG_NA));
9742 if (ret) {
9743 wpa_printf(MSG_ERROR,
9744 "nl80211: Failed to set unsolicited NA filter");
9745 return ret;
9746 }
9747
9748 return 0;
9749}
9750
9751
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07009752static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
9753 const u8 **ext_capa, const u8 **ext_capa_mask,
9754 unsigned int *ext_capa_len)
9755{
9756 struct i802_bss *bss = priv;
9757 struct wpa_driver_nl80211_data *drv = bss->drv;
9758 enum nl80211_iftype nlmode;
9759 unsigned int i;
9760
9761 if (!ext_capa || !ext_capa_mask || !ext_capa_len)
9762 return -1;
9763
9764 nlmode = wpa_driver_nl80211_if_type(type);
9765
9766 /* By default, use the per-radio values */
9767 *ext_capa = drv->extended_capa;
9768 *ext_capa_mask = drv->extended_capa_mask;
9769 *ext_capa_len = drv->extended_capa_len;
9770
9771 /* Replace the default value if a per-interface type value exists */
9772 for (i = 0; i < drv->num_iface_ext_capa; i++) {
9773 if (nlmode == drv->iface_ext_capa[i].iftype) {
9774 *ext_capa = drv->iface_ext_capa[i].ext_capa;
9775 *ext_capa_mask = drv->iface_ext_capa[i].ext_capa_mask;
9776 *ext_capa_len = drv->iface_ext_capa[i].ext_capa_len;
9777 break;
9778 }
9779 }
9780
9781 return 0;
9782}
9783
9784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009785const struct wpa_driver_ops wpa_driver_nl80211_ops = {
9786 .name = "nl80211",
9787 .desc = "Linux nl80211/cfg80211",
9788 .get_bssid = wpa_driver_nl80211_get_bssid,
9789 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009790 .set_key = driver_nl80211_set_key,
9791 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009792 .sched_scan = wpa_driver_nl80211_sched_scan,
9793 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009794 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009795 .abort_scan = wpa_driver_nl80211_abort_scan,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009796 .deauthenticate = driver_nl80211_deauthenticate,
9797 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009798 .associate = wpa_driver_nl80211_associate,
9799 .global_init = nl80211_global_init,
9800 .global_deinit = nl80211_global_deinit,
9801 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009802 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009803 .get_capa = wpa_driver_nl80211_get_capa,
9804 .set_operstate = wpa_driver_nl80211_set_operstate,
9805 .set_supp_port = wpa_driver_nl80211_set_supp_port,
9806 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009807 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009808 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07009809 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009810 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009811 .if_remove = driver_nl80211_if_remove,
9812 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009813 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009814 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009815 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009816 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
9817 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009818 .hapd_init = i802_init,
9819 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009820 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009821 .get_seqnum = i802_get_seqnum,
9822 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009823 .get_inact_sec = i802_get_inact_sec,
9824 .sta_clear_stats = i802_sta_clear_stats,
9825 .set_rts = i802_set_rts,
9826 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009827 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009828 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009829 .sta_deauth = i802_sta_deauth,
9830 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009831 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009832 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009833 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009834 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
9835 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
9836 .cancel_remain_on_channel =
9837 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009838 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009839 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -07009840 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009841 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009842 .signal_monitor = nl80211_signal_monitor,
9843 .signal_poll = nl80211_signal_poll,
9844 .send_frame = nl80211_send_frame,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009845 .set_param = nl80211_set_param,
9846 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009847 .add_pmkid = nl80211_add_pmkid,
9848 .remove_pmkid = nl80211_remove_pmkid,
9849 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009850 .set_rekey_info = nl80211_set_rekey_info,
9851 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009852 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -07009853 .start_dfs_cac = nl80211_start_radar_detection,
9854 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009855#ifdef CONFIG_TDLS
9856 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
9857 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009858 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
9859 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009860#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009861 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009862 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009863 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -07009864 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009865 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009866#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009867 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009868 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009869 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08009870#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07009871#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -07009872#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07009873 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -07009874#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08009875#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009876 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009877 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009878 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009879 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009880#ifdef CONFIG_MESH
9881 .init_mesh = wpa_driver_nl80211_init_mesh,
9882 .join_mesh = wpa_driver_nl80211_join_mesh,
9883 .leave_mesh = wpa_driver_nl80211_leave_mesh,
9884#endif /* CONFIG_MESH */
9885 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
9886 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
9887 .br_port_set_attr = wpa_driver_br_port_set_attr,
9888 .br_set_net_param = wpa_driver_br_set_net_param,
9889 .add_tx_ts = nl80211_add_ts,
9890 .del_tx_ts = nl80211_del_ts,
Dmitry Shmidte4663042016-04-04 10:07:49 -07009891 .get_ifindex = nl80211_get_ifindex,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009892#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009893 .roaming = nl80211_roaming,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009894 .do_acs = wpa_driver_do_acs,
Ravi Joshie6ccb162015-07-16 17:45:41 -07009895 .set_band = nl80211_set_band,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009896 .get_pref_freq_list = nl80211_get_pref_freq_list,
9897 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009898 .p2p_lo_start = nl80211_p2p_lo_start,
9899 .p2p_lo_stop = nl80211_p2p_lo_stop,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07009900 .set_default_scan_ies = nl80211_set_default_scan_ies,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08009901 .set_tdls_mode = nl80211_set_tdls_mode,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009902#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009903 .configure_data_frame_filters = nl80211_configure_data_frame_filters,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07009904 .get_ext_capab = nl80211_get_ext_capab,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009905};