blob: 1fb113f759420be8809a854676d03d19f3ac654f [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"
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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;
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800711
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800718
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -0800719 if (nl80211_register_beacons(bss->drv, w)) {
720 nl_destroy_handles(&w->nl_beacons);
721 os_free(w);
722 return NULL;
723 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800724
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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;
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002504static int wpa_driver_nl80211_del_beacon(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505{
2506 struct nl_msg *msg;
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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);
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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))
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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)
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002638 return RSN_CIPHER_SUITE_WEP40;
2639 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002640 case WPA_ALG_TKIP:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002641 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002642 case WPA_ALG_CCMP:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002643 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002644 case WPA_ALG_GCMP:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002645 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002646 case WPA_ALG_CCMP_256:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002647 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002648 case WPA_ALG_GCMP_256:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002649 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002650 case WPA_ALG_IGTK:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002651 return RSN_CIPHER_SUITE_AES_128_CMAC;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002652 case WPA_ALG_BIP_GMAC_128:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002653 return RSN_CIPHER_SUITE_BIP_GMAC_128;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002654 case WPA_ALG_BIP_GMAC_256:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002655 return RSN_CIPHER_SUITE_BIP_GMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002656 case WPA_ALG_BIP_CMAC_256:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002657 return RSN_CIPHER_SUITE_BIP_CMAC_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002658 case WPA_ALG_SMS4:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002659 return RSN_CIPHER_SUITE_SMS4;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002660 case WPA_ALG_KRK:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002679 return RSN_CIPHER_SUITE_CCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002680 case WPA_CIPHER_GCMP_256:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002681 return RSN_CIPHER_SUITE_GCMP_256;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002682 case WPA_CIPHER_CCMP:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002683 return RSN_CIPHER_SUITE_CCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002684 case WPA_CIPHER_GCMP:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002685 return RSN_CIPHER_SUITE_GCMP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002686 case WPA_CIPHER_TKIP:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002687 return RSN_CIPHER_SUITE_TKIP;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002688 case WPA_CIPHER_WEP104:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002689 return RSN_CIPHER_SUITE_WEP104;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002690 case WPA_CIPHER_WEP40:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08002691 return RSN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002692 case WPA_CIPHER_GTK_NOT_USED:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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)
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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)
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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)
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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)
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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)
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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)
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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)
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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 ?
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -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;
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08003789 else if (!nl80211_get_wiphy_data_ap(bss))
3790 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003792 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3793 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003794 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3795 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003796 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003797 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003798 wpa_printf(MSG_DEBUG, "nl80211: beacon_rate=%u", params->beacon_rate);
3799 wpa_printf(MSG_DEBUG, "nl80211: rate_type=%d", params->rate_type);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003800 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003801 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3802 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003803 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
3804 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3805 params->head) ||
3806 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3807 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003808 nl80211_put_beacon_int(msg, params->beacon_int) ||
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003809 nl80211_put_beacon_rate(msg, drv->capa.flags, params) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003810 nl80211_put_dtim_period(msg, params->dtim_period) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003811 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3812 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003813 if (params->proberesp && params->proberesp_len) {
3814 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3815 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003816 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3817 params->proberesp))
3818 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003819 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003820 switch (params->hide_ssid) {
3821 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003822 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003823 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3824 NL80211_HIDDEN_SSID_NOT_IN_USE))
3825 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003826 break;
3827 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003828 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003829 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3830 NL80211_HIDDEN_SSID_ZERO_LEN))
3831 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003832 break;
3833 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003834 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003835 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3836 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3837 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003838 break;
3839 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003840 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003841 if (params->privacy &&
3842 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3843 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003844 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003845 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3846 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3847 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003848 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3849 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3850 NL80211_AUTHTYPE_SHARED_KEY))
3851 goto fail;
3852 } else {
3853 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3854 NL80211_AUTHTYPE_OPEN_SYSTEM))
3855 goto fail;
3856 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003857
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003858 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003859 ver = 0;
3860 if (params->wpa_version & WPA_PROTO_WPA)
3861 ver |= NL80211_WPA_VERSION_1;
3862 if (params->wpa_version & WPA_PROTO_RSN)
3863 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003864 if (ver &&
3865 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3866 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003867
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003868 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3869 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003870 num_suites = 0;
3871 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08003872 suites[num_suites++] = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003873 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08003874 suites[num_suites++] = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003875 if (num_suites &&
3876 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
3877 suites))
3878 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003879
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003880 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07003881 (!params->pairwise_ciphers ||
3882 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) &&
3883 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
3884 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003885 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003886
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003887 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
3888 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003889 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
3890 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003891 if (num_suites &&
3892 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
3893 num_suites * sizeof(u32), suites))
3894 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003895
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003896 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
3897 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003898 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003899 if (suite &&
3900 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
3901 goto fail;
3902
Dmitry Shmidte4663042016-04-04 10:07:49 -07003903 if (params->ht_opmode != -1) {
3904 switch (params->smps_mode) {
3905 case HT_CAP_INFO_SMPS_DYNAMIC:
3906 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
3907 smps_mode = NL80211_SMPS_DYNAMIC;
3908 break;
3909 case HT_CAP_INFO_SMPS_STATIC:
3910 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
3911 smps_mode = NL80211_SMPS_STATIC;
3912 break;
3913 default:
3914 /* invalid - fallback to smps off */
3915 case HT_CAP_INFO_SMPS_DISABLED:
3916 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
3917 smps_mode = NL80211_SMPS_OFF;
3918 break;
3919 }
3920 if (nla_put_u32(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
3921 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003922 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003923
3924 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003925 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
3926 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003927 if (nla_put(msg, NL80211_ATTR_IE,
3928 wpabuf_len(params->beacon_ies),
3929 wpabuf_head(params->beacon_ies)))
3930 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003931 }
3932 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003933 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
3934 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003935 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
3936 wpabuf_len(params->proberesp_ies),
3937 wpabuf_head(params->proberesp_ies)))
3938 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003939 }
3940 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003941 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
3942 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003943 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
3944 wpabuf_len(params->assocresp_ies),
3945 wpabuf_head(params->assocresp_ies)))
3946 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003947 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003948
Dmitry Shmidt04949592012-07-19 12:16:46 -07003949 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003950 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
3951 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003952 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
3953 params->ap_max_inactivity))
3954 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003955 }
3956
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003957#ifdef CONFIG_P2P
3958 if (params->p2p_go_ctwindow > 0) {
3959 if (drv->p2p_go_ctwindow_supported) {
3960 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
3961 params->p2p_go_ctwindow);
3962 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
3963 params->p2p_go_ctwindow))
3964 goto fail;
3965 } else {
3966 wpa_printf(MSG_INFO,
3967 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
3968 }
3969 }
3970#endif /* CONFIG_P2P */
3971
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003972 if (params->pbss) {
3973 wpa_printf(MSG_DEBUG, "nl80211: PBSS");
3974 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
3975 goto fail;
3976 }
3977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3979 if (ret) {
3980 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
3981 ret, strerror(-ret));
3982 } else {
3983 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003984 nl80211_set_bss(bss, params->cts_protect, params->preamble,
3985 params->short_slot_time, params->ht_opmode,
3986 params->isolate, params->basic_rates);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003987 nl80211_set_multicast_to_unicast(bss,
3988 params->multicast_to_unicast);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003989 if (beacon_set && params->freq &&
3990 params->freq->bandwidth != bss->bandwidth) {
3991 wpa_printf(MSG_DEBUG,
3992 "nl80211: Update BSS %s bandwidth: %d -> %d",
3993 bss->ifname, bss->bandwidth,
3994 params->freq->bandwidth);
3995 ret = nl80211_set_channel(bss, params->freq, 1);
3996 if (ret) {
3997 wpa_printf(MSG_DEBUG,
3998 "nl80211: Frequency set failed: %d (%s)",
3999 ret, strerror(-ret));
4000 } else {
4001 wpa_printf(MSG_DEBUG,
4002 "nl80211: Frequency set succeeded for ht2040 coex");
4003 bss->bandwidth = params->freq->bandwidth;
4004 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004005 } else if (!beacon_set && params->freq) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004006 /*
4007 * cfg80211 updates the driver on frequence change in AP
4008 * mode only at the point when beaconing is started, so
4009 * set the initial value here.
4010 */
4011 bss->bandwidth = params->freq->bandwidth;
4012 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004013 }
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07004014
4015#ifdef CONFIG_MESH
4016 if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
4017 os_memset(&mesh_params, 0, sizeof(mesh_params));
4018 mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
4019 mesh_params.ht_opmode = params->ht_opmode;
4020 ret = nl80211_set_mesh_config(priv, &mesh_params);
4021 if (ret < 0)
4022 return ret;
4023 }
4024#endif /* CONFIG_MESH */
4025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004026 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004027fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004028 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004029 return -ENOBUFS;
4030}
4031
4032
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004033static int nl80211_put_freq_params(struct nl_msg *msg,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004034 const struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004035{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004036 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004037 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
4038 return -ENOBUFS;
4039
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004040 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
4041 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
4042
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004043 if (freq->vht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004044 enum nl80211_chan_width cw;
4045
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004046 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004047 switch (freq->bandwidth) {
4048 case 20:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004049 cw = NL80211_CHAN_WIDTH_20;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004050 break;
4051 case 40:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004052 cw = NL80211_CHAN_WIDTH_40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004053 break;
4054 case 80:
4055 if (freq->center_freq2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004056 cw = NL80211_CHAN_WIDTH_80P80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004057 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004058 cw = NL80211_CHAN_WIDTH_80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004059 break;
4060 case 160:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004061 cw = NL80211_CHAN_WIDTH_160;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004062 break;
4063 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004064 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004065 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004066
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004067 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
4068 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
4069 freq->center_freq1);
4070 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
4071 freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004072 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
4073 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
4074 freq->center_freq1) ||
4075 (freq->center_freq2 &&
4076 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
4077 freq->center_freq2)))
4078 return -ENOBUFS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004079 } else if (freq->ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004080 enum nl80211_channel_type ct;
4081
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004082 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
4083 freq->sec_channel_offset);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004084 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004085 case -1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004086 ct = NL80211_CHAN_HT40MINUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 break;
4088 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004089 ct = NL80211_CHAN_HT40PLUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004090 break;
4091 default:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004092 ct = NL80211_CHAN_HT20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093 break;
4094 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004095
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004096 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004097 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
4098 return -ENOBUFS;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004099 } else {
4100 wpa_printf(MSG_DEBUG, " * channel_type=%d",
4101 NL80211_CHAN_NO_HT);
4102 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4103 NL80211_CHAN_NO_HT))
4104 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004105 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004106 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004107}
4108
4109
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004110static int nl80211_set_channel(struct i802_bss *bss,
4111 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004112{
4113 struct wpa_driver_nl80211_data *drv = bss->drv;
4114 struct nl_msg *msg;
4115 int ret;
4116
4117 wpa_printf(MSG_DEBUG,
4118 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
4119 freq->freq, freq->ht_enabled, freq->vht_enabled,
4120 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004121
4122 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
4123 NL80211_CMD_SET_WIPHY);
4124 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
4125 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004126 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004127 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004128
4129 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004130 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004131 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004132 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004133 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004134 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004135 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004136 return -1;
4137}
4138
4139
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004140static u32 sta_flags_nl80211(int flags)
4141{
4142 u32 f = 0;
4143
4144 if (flags & WPA_STA_AUTHORIZED)
4145 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
4146 if (flags & WPA_STA_WMM)
4147 f |= BIT(NL80211_STA_FLAG_WME);
4148 if (flags & WPA_STA_SHORT_PREAMBLE)
4149 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
4150 if (flags & WPA_STA_MFP)
4151 f |= BIT(NL80211_STA_FLAG_MFP);
4152 if (flags & WPA_STA_TDLS_PEER)
4153 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004154 if (flags & WPA_STA_AUTHENTICATED)
4155 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004156 if (flags & WPA_STA_ASSOCIATED)
4157 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004158
4159 return f;
4160}
4161
4162
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004163#ifdef CONFIG_MESH
4164static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
4165{
4166 switch (state) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004167 case PLINK_IDLE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004168 return NL80211_PLINK_LISTEN;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004169 case PLINK_OPN_SNT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004170 return NL80211_PLINK_OPN_SNT;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004171 case PLINK_OPN_RCVD:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004172 return NL80211_PLINK_OPN_RCVD;
4173 case PLINK_CNF_RCVD:
4174 return NL80211_PLINK_CNF_RCVD;
4175 case PLINK_ESTAB:
4176 return NL80211_PLINK_ESTAB;
4177 case PLINK_HOLDING:
4178 return NL80211_PLINK_HOLDING;
4179 case PLINK_BLOCKED:
4180 return NL80211_PLINK_BLOCKED;
4181 default:
4182 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
4183 state);
4184 }
4185 return -1;
4186}
4187#endif /* CONFIG_MESH */
4188
4189
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004190static int wpa_driver_nl80211_sta_add(void *priv,
4191 struct hostapd_sta_add_params *params)
4192{
4193 struct i802_bss *bss = priv;
4194 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004195 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004196 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197 int ret = -ENOBUFS;
4198
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004199 if ((params->flags & WPA_STA_TDLS_PEER) &&
4200 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
4201 return -EOPNOTSUPP;
4202
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004203 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
4204 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004205 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
4206 NL80211_CMD_NEW_STATION);
4207 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
4208 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004209
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004210 /*
4211 * Set the below properties only in one of the following cases:
4212 * 1. New station is added, already associated.
4213 * 2. Set WPA_STA_TDLS_PEER station.
4214 * 3. Set an already added unassociated station, if driver supports
4215 * full AP client state. (Set these properties after station became
4216 * associated will be rejected by the driver).
4217 */
4218 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
4219 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4220 (params->flags & WPA_STA_ASSOCIATED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004221 wpa_hexdump(MSG_DEBUG, " * supported rates",
4222 params->supp_rates, params->supp_rates_len);
4223 wpa_printf(MSG_DEBUG, " * capability=0x%x",
4224 params->capability);
4225 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
4226 params->supp_rates_len, params->supp_rates) ||
4227 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
4228 params->capability))
4229 goto fail;
4230
4231 if (params->ht_capabilities) {
4232 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
4233 (u8 *) params->ht_capabilities,
4234 sizeof(*params->ht_capabilities));
4235 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
4236 sizeof(*params->ht_capabilities),
4237 params->ht_capabilities))
4238 goto fail;
4239 }
4240
4241 if (params->vht_capabilities) {
4242 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
4243 (u8 *) params->vht_capabilities,
4244 sizeof(*params->vht_capabilities));
4245 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
4246 sizeof(*params->vht_capabilities),
4247 params->vht_capabilities))
4248 goto fail;
4249 }
4250
4251 if (params->ext_capab) {
4252 wpa_hexdump(MSG_DEBUG, " * ext_capab",
4253 params->ext_capab, params->ext_capab_len);
4254 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
4255 params->ext_capab_len, params->ext_capab))
4256 goto fail;
4257 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004258
4259 if (is_ap_interface(drv->nlmode) &&
4260 nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
4261 params->support_p2p_ps ?
4262 NL80211_P2P_PS_SUPPORTED :
4263 NL80211_P2P_PS_UNSUPPORTED))
4264 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004265 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004266 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004267 if (params->aid) {
4268 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004269 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
4270 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004271 } else {
4272 /*
4273 * cfg80211 validates that AID is non-zero, so we have
4274 * to make this a non-zero value for the TDLS case where
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004275 * a dummy STA entry is used for now and for a station
4276 * that is still not associated.
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004277 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004278 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
4279 (params->flags & WPA_STA_TDLS_PEER) ?
4280 "TDLS" : "UNASSOC_STA");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004281 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
4282 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004283 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004284 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4285 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004286 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4287 params->listen_interval))
4288 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004289 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
4290 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004291 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
4292 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004293 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4294 (params->flags & WPA_STA_ASSOCIATED)) {
4295 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
4296 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4297 params->listen_interval);
4298 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
4299 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4300 params->listen_interval))
4301 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004302 }
4303
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08004304 if (params->vht_opmode_enabled) {
4305 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004306 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
4307 params->vht_opmode))
4308 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004309 }
4310
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004311 if (params->supp_channels) {
4312 wpa_hexdump(MSG_DEBUG, " * supported channels",
4313 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004314 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
4315 params->supp_channels_len, params->supp_channels))
4316 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004317 }
4318
4319 if (params->supp_oper_classes) {
4320 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
4321 params->supp_oper_classes,
4322 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004323 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
4324 params->supp_oper_classes_len,
4325 params->supp_oper_classes))
4326 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004327 }
4328
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004329 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004330 upd.set = sta_flags_nl80211(params->flags);
4331 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004332
4333 /*
4334 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
4335 * flags, as nl80211 driver moves a new station, by default, into
4336 * associated state.
4337 *
4338 * On the other hand, if the driver supports that feature and the
4339 * station is added in unauthenticated state, set the
4340 * authenticated/associated bits in the mask to prevent moving this
4341 * station to associated state before it is actually associated.
4342 *
4343 * This is irrelevant for mesh mode where the station is added to the
4344 * driver as authenticated already, and ASSOCIATED isn't part of the
4345 * nl80211 API.
4346 */
4347 if (!is_mesh_interface(drv->nlmode)) {
4348 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
4349 wpa_printf(MSG_DEBUG,
4350 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
4351 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
4352 BIT(NL80211_STA_FLAG_AUTHENTICATED));
4353 } else if (!params->set &&
4354 !(params->flags & WPA_STA_TDLS_PEER)) {
4355 if (!(params->flags & WPA_STA_AUTHENTICATED))
4356 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
4357 if (!(params->flags & WPA_STA_ASSOCIATED))
4358 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
4359 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004360#ifdef CONFIG_MESH
4361 } else {
4362 if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
4363 ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
4364 params->peer_aid);
4365 if (ret)
4366 goto fail;
4367 }
4368#endif /* CONFIG_MESH */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004369 }
4370
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004371 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
4372 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004373 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4374 goto fail;
4375
4376#ifdef CONFIG_MESH
4377 if (params->plink_state &&
4378 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
4379 sta_plink_state_nl80211(params->plink_state)))
4380 goto fail;
4381#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004382
4383 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004384 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
4385
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004386 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004387 if (!wme ||
4388 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
4389 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
4390 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
4391 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
4392 WMM_QOSINFO_STA_SP_MASK))
4393 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004394 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004395 }
4396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004397 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004398 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004399 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004400 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
4401 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
4402 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004403 if (ret == -EEXIST)
4404 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004405fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004406 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004407 return ret;
4408}
4409
4410
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004411static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
4412{
4413#ifdef CONFIG_LIBNL3_ROUTE
4414 struct wpa_driver_nl80211_data *drv = bss->drv;
4415 struct rtnl_neigh *rn;
4416 struct nl_addr *nl_addr;
4417 int err;
4418
4419 rn = rtnl_neigh_alloc();
4420 if (!rn)
4421 return;
4422
4423 rtnl_neigh_set_family(rn, AF_BRIDGE);
4424 rtnl_neigh_set_ifindex(rn, bss->ifindex);
4425 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
4426 if (!nl_addr) {
4427 rtnl_neigh_put(rn);
4428 return;
4429 }
4430 rtnl_neigh_set_lladdr(rn, nl_addr);
4431
4432 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
4433 if (err < 0) {
4434 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
4435 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
4436 bss->ifindex, nl_geterror(err));
4437 } else {
4438 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
4439 MACSTR, MAC2STR(addr));
4440 }
4441
4442 nl_addr_put(nl_addr);
4443 rtnl_neigh_put(rn);
4444#endif /* CONFIG_LIBNL3_ROUTE */
4445}
4446
4447
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004448static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
4449 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004450{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004451 struct wpa_driver_nl80211_data *drv = bss->drv;
4452 struct nl_msg *msg;
4453 int ret;
4454
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004455 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
4456 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
4457 (deauth == 0 &&
4458 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4459 WLAN_FC_STYPE_DISASSOC)) ||
4460 (deauth == 1 &&
4461 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4462 WLAN_FC_STYPE_DEAUTH)) ||
4463 (reason_code &&
4464 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
4465 nlmsg_free(msg);
4466 return -ENOBUFS;
4467 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004468
4469 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004470 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
4471 " --> %d (%s)",
4472 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004473
4474 if (drv->rtnl_sk)
4475 rtnl_neigh_delete_fdb_entry(bss, addr);
4476
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004477 if (ret == -ENOENT)
4478 return 0;
4479 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004480}
4481
4482
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004483void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004484{
4485 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004486 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004487
4488 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004490 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004491 dl_list_for_each(drv2, &drv->global->interfaces,
4492 struct wpa_driver_nl80211_data, list)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004493 {
4494 del_ifidx(drv2, ifidx, IFIDX_ANY);
4495 /* Remove all bridges learned for this iface */
4496 del_ifidx(drv2, IFIDX_ANY, ifidx);
4497 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004498
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004499 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004500 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4501 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004502 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
4503}
4504
4505
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004506const char * nl80211_iftype_str(enum nl80211_iftype mode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004507{
4508 switch (mode) {
4509 case NL80211_IFTYPE_ADHOC:
4510 return "ADHOC";
4511 case NL80211_IFTYPE_STATION:
4512 return "STATION";
4513 case NL80211_IFTYPE_AP:
4514 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004515 case NL80211_IFTYPE_AP_VLAN:
4516 return "AP_VLAN";
4517 case NL80211_IFTYPE_WDS:
4518 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004519 case NL80211_IFTYPE_MONITOR:
4520 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004521 case NL80211_IFTYPE_MESH_POINT:
4522 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004523 case NL80211_IFTYPE_P2P_CLIENT:
4524 return "P2P_CLIENT";
4525 case NL80211_IFTYPE_P2P_GO:
4526 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004527 case NL80211_IFTYPE_P2P_DEVICE:
4528 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004529 default:
4530 return "unknown";
4531 }
4532}
4533
4534
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004535static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4536 const char *ifname,
4537 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004538 const u8 *addr, int wds,
4539 int (*handler)(struct nl_msg *, void *),
4540 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004541{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004542 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004543 int ifidx;
4544 int ret = -ENOBUFS;
4545
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004546 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4547 iftype, nl80211_iftype_str(iftype));
4548
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004549 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
4550 if (!msg ||
4551 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
4552 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
4553 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004554
4555 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004556 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004557
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004558 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004559 if (!flags ||
4560 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
4561 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004562
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004563 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004564 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004565 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
4566 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004567 }
4568
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004569 /*
4570 * Tell cfg80211 that the interface belongs to the socket that created
4571 * it, and the interface should be deleted when the socket is closed.
4572 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004573 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4574 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004575
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004576 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004577 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004578 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004579 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004580 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004581 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4582 ifname, ret, strerror(-ret));
4583 return ret;
4584 }
4585
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004586 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
4587 return 0;
4588
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004589 ifidx = if_nametoindex(ifname);
4590 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4591 ifname, ifidx);
4592
4593 if (ifidx <= 0)
4594 return -1;
4595
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004596 /*
4597 * Some virtual interfaces need to process EAPOL packets and events on
4598 * the parent interface. This is used mainly with hostapd.
4599 */
4600 if (drv->hostapd ||
4601 iftype == NL80211_IFTYPE_AP_VLAN ||
4602 iftype == NL80211_IFTYPE_WDS ||
4603 iftype == NL80211_IFTYPE_MONITOR) {
4604 /* start listening for EAPOL on this interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004605 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004606 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004607
4608 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004609 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004610 nl80211_remove_iface(drv, ifidx);
4611 return -1;
4612 }
4613
4614 return ifidx;
4615}
4616
4617
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004618int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4619 const char *ifname, enum nl80211_iftype iftype,
4620 const u8 *addr, int wds,
4621 int (*handler)(struct nl_msg *, void *),
4622 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004623{
4624 int ret;
4625
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004626 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4627 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004628
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004629 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004630 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004631 if (use_existing) {
4632 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4633 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004634 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4635 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4636 addr) < 0 &&
4637 (linux_set_iface_flags(drv->global->ioctl_sock,
4638 ifname, 0) < 0 ||
4639 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4640 addr) < 0 ||
4641 linux_set_iface_flags(drv->global->ioctl_sock,
4642 ifname, 1) < 0))
4643 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004644 return -ENFILE;
4645 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4647
4648 /* Try to remove the interface that was already there. */
4649 nl80211_remove_iface(drv, if_nametoindex(ifname));
4650
4651 /* Try to create the interface again */
4652 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004653 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004654 }
4655
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004656 if (ret >= 0 && is_p2p_net_interface(iftype)) {
4657 wpa_printf(MSG_DEBUG,
4658 "nl80211: Interface %s created for P2P - disable 11b rates",
4659 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004660 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004661 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004662
4663 return ret;
4664}
4665
4666
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004667static int nl80211_setup_ap(struct i802_bss *bss)
4668{
4669 struct wpa_driver_nl80211_data *drv = bss->drv;
4670
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004671 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4672 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004673
4674 /*
4675 * Disable Probe Request reporting unless we need it in this way for
4676 * devices that include the AP SME, in the other case (unless using
4677 * monitor iface) we'll get it through the nl_mgmt socket instead.
4678 */
4679 if (!drv->device_ap_sme)
4680 wpa_driver_nl80211_probe_req_report(bss, 0);
4681
4682 if (!drv->device_ap_sme && !drv->use_monitor)
4683 if (nl80211_mgmt_subscribe_ap(bss))
4684 return -1;
4685
4686 if (drv->device_ap_sme && !drv->use_monitor)
4687 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004688 wpa_printf(MSG_DEBUG,
4689 "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004690
4691 if (!drv->device_ap_sme && drv->use_monitor &&
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004692 nl80211_create_monitor_interface(drv) &&
4693 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004694 return -1;
4695
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004696 if (drv->device_ap_sme &&
4697 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4698 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4699 "Probe Request frame reporting in AP mode");
4700 /* Try to survive without this */
4701 }
4702
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004703 return 0;
4704}
4705
4706
4707static void nl80211_teardown_ap(struct i802_bss *bss)
4708{
4709 struct wpa_driver_nl80211_data *drv = bss->drv;
4710
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004711 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4712 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004713 if (drv->device_ap_sme) {
4714 wpa_driver_nl80211_probe_req_report(bss, 0);
4715 if (!drv->use_monitor)
4716 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4717 } else if (drv->use_monitor)
4718 nl80211_remove_monitor_interface(drv);
4719 else
4720 nl80211_mgmt_unsubscribe(bss, "AP teardown");
4721
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08004722 nl80211_put_wiphy_data_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004723 bss->beacon_set = 0;
4724}
4725
4726
4727static int nl80211_send_eapol_data(struct i802_bss *bss,
4728 const u8 *addr, const u8 *data,
4729 size_t data_len)
4730{
4731 struct sockaddr_ll ll;
4732 int ret;
4733
4734 if (bss->drv->eapol_tx_sock < 0) {
4735 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
4736 return -1;
4737 }
4738
4739 os_memset(&ll, 0, sizeof(ll));
4740 ll.sll_family = AF_PACKET;
4741 ll.sll_ifindex = bss->ifindex;
4742 ll.sll_protocol = htons(ETH_P_PAE);
4743 ll.sll_halen = ETH_ALEN;
4744 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4745 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4746 (struct sockaddr *) &ll, sizeof(ll));
4747 if (ret < 0)
4748 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4749 strerror(errno));
4750
4751 return ret;
4752}
4753
4754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004755static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4756
4757static int wpa_driver_nl80211_hapd_send_eapol(
4758 void *priv, const u8 *addr, const u8 *data,
4759 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
4760{
4761 struct i802_bss *bss = priv;
4762 struct wpa_driver_nl80211_data *drv = bss->drv;
4763 struct ieee80211_hdr *hdr;
4764 size_t len;
4765 u8 *pos;
4766 int res;
4767 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08004768
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004769 if (drv->device_ap_sme || !drv->use_monitor)
4770 return nl80211_send_eapol_data(bss, addr, data, data_len);
4771
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004772 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4773 data_len;
4774 hdr = os_zalloc(len);
4775 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004776 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4777 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004778 return -1;
4779 }
4780
4781 hdr->frame_control =
4782 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4783 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4784 if (encrypt)
4785 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4786 if (qos) {
4787 hdr->frame_control |=
4788 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4789 }
4790
4791 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4792 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4793 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4794 pos = (u8 *) (hdr + 1);
4795
4796 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004797 /* Set highest priority in QoS header */
4798 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004799 pos[1] = 0;
4800 pos += 2;
4801 }
4802
4803 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4804 pos += sizeof(rfc1042_header);
4805 WPA_PUT_BE16(pos, ETH_P_PAE);
4806 pos += 2;
4807 memcpy(pos, data, data_len);
4808
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004809 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004810 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004811 if (res < 0) {
4812 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4813 "failed: %d (%s)",
4814 (unsigned long) len, errno, strerror(errno));
4815 }
4816 os_free(hdr);
4817
4818 return res;
4819}
4820
4821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004822static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004823 unsigned int total_flags,
4824 unsigned int flags_or,
4825 unsigned int flags_and)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004826{
4827 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004828 struct nl_msg *msg;
4829 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004830 struct nl80211_sta_flag_update upd;
4831
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004832 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4833 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4834 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4835 !!(total_flags & WPA_STA_AUTHORIZED));
4836
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004837 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4838 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4839 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004840
4841 /*
4842 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4843 * can be removed eventually.
4844 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004845 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004846 if (!flags ||
4847 ((total_flags & WPA_STA_AUTHORIZED) &&
4848 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4849 ((total_flags & WPA_STA_WMM) &&
4850 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4851 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4852 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4853 ((total_flags & WPA_STA_MFP) &&
4854 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4855 ((total_flags & WPA_STA_TDLS_PEER) &&
4856 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4857 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004858
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004859 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004860
4861 os_memset(&upd, 0, sizeof(upd));
4862 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4863 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004864 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4865 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004866
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004867 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
4868fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004869 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004870 return -ENOBUFS;
4871}
4872
4873
4874static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4875 struct wpa_driver_associate_params *params)
4876{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004877 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004878
4879 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004880 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4881 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004882 nlmode = NL80211_IFTYPE_P2P_GO;
4883 } else
4884 nlmode = NL80211_IFTYPE_AP;
4885
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004886 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004887 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004888 nl80211_remove_monitor_interface(drv);
4889 return -1;
4890 }
4891
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004892 if (params->freq.freq &&
4893 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004894 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004895 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004896 nl80211_remove_monitor_interface(drv);
4897 return -1;
4898 }
4899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004900 return 0;
4901}
4902
4903
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004904static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
4905 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004906{
4907 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004908 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004909
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004910 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004911 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004912 if (ret) {
4913 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
4914 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004915 } else {
4916 wpa_printf(MSG_DEBUG,
4917 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004918 }
4919
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004920 if (reset_mode &&
4921 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004922 NL80211_IFTYPE_STATION)) {
4923 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4924 "station mode");
4925 }
4926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004927 return ret;
4928}
4929
4930
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004931static int nl80211_ht_vht_overrides(struct nl_msg *msg,
4932 struct wpa_driver_associate_params *params)
4933{
4934 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
4935 return -1;
4936
4937 if (params->htcaps && params->htcaps_mask) {
4938 int sz = sizeof(struct ieee80211_ht_capabilities);
4939 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
4940 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
4941 params->htcaps_mask, sz);
4942 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
4943 params->htcaps) ||
4944 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
4945 params->htcaps_mask))
4946 return -1;
4947 }
4948
4949#ifdef CONFIG_VHT_OVERRIDES
4950 if (params->disable_vht) {
4951 wpa_printf(MSG_DEBUG, " * VHT disabled");
4952 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
4953 return -1;
4954 }
4955
4956 if (params->vhtcaps && params->vhtcaps_mask) {
4957 int sz = sizeof(struct ieee80211_vht_capabilities);
4958 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
4959 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
4960 params->vhtcaps_mask, sz);
4961 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
4962 params->vhtcaps) ||
4963 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
4964 params->vhtcaps_mask))
4965 return -1;
4966 }
4967#endif /* CONFIG_VHT_OVERRIDES */
4968
4969 return 0;
4970}
4971
4972
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004973static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
4974 struct wpa_driver_associate_params *params)
4975{
4976 struct nl_msg *msg;
4977 int ret = -1;
4978 int count = 0;
4979
4980 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
4981
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004982 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004983 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4984 "IBSS mode");
4985 return -1;
4986 }
4987
4988retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004989 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
4990 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
4991 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004992
4993 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4994 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004995 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4996 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4998 drv->ssid_len = params->ssid_len;
4999
Dmitry Shmidtff787d52015-01-12 13:01:47 -08005000 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
5001 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005002 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005003
5004 ret = nl80211_set_conn_keys(params, msg);
5005 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005006 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005007
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005008 if (params->bssid && params->fixed_bssid) {
5009 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
5010 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005011 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5012 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005013 }
5014
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005015 if (params->fixed_freq) {
5016 wpa_printf(MSG_DEBUG, " * fixed_freq");
5017 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
5018 goto fail;
5019 }
5020
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005021 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5022 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5023 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5024 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005025 wpa_printf(MSG_DEBUG, " * control port");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005026 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5027 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005028 }
5029
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030 if (params->wpa_ie) {
5031 wpa_hexdump(MSG_DEBUG,
5032 " * Extra IEs for Beacon/Probe Response frames",
5033 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005034 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5035 params->wpa_ie))
5036 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005037 }
5038
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005039 ret = nl80211_ht_vht_overrides(msg, params);
5040 if (ret < 0)
5041 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005042
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005043 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5044 msg = NULL;
5045 if (ret) {
5046 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
5047 ret, strerror(-ret));
5048 count++;
5049 if (ret == -EALREADY && count == 1) {
5050 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
5051 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005052 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005053 nlmsg_free(msg);
5054 goto retry;
5055 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005056 } else {
5057 wpa_printf(MSG_DEBUG,
5058 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005059 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005060
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005061fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005062 nlmsg_free(msg);
5063 return ret;
5064}
5065
5066
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005067static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
5068 struct wpa_driver_associate_params *params,
5069 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005070{
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08005071 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
5072 return -1;
5073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005074 if (params->bssid) {
5075 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5076 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005077 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5078 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005079 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005080
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005081 if (params->bssid_hint) {
5082 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
5083 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005084 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
5085 params->bssid_hint))
5086 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005087 }
5088
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005089 if (params->freq.freq) {
5090 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005091 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
5092 params->freq.freq))
5093 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005094 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005095 } else
5096 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005097
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005098 if (params->freq_hint) {
5099 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005100 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
5101 params->freq_hint))
5102 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005103 }
5104
Dmitry Shmidt04949592012-07-19 12:16:46 -07005105 if (params->bg_scan_period >= 0) {
5106 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
5107 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005108 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
5109 params->bg_scan_period))
5110 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005111 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005113 if (params->ssid) {
5114 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5115 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005116 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
5117 params->ssid))
5118 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005119 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005120 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005121 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5122 drv->ssid_len = params->ssid_len;
5123 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005124
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005125 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005126 if (params->wpa_ie &&
5127 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
5128 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005129
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005130 if (params->wpa_proto) {
5131 enum nl80211_wpa_versions ver = 0;
5132
5133 if (params->wpa_proto & WPA_PROTO_WPA)
5134 ver |= NL80211_WPA_VERSION_1;
5135 if (params->wpa_proto & WPA_PROTO_RSN)
5136 ver |= NL80211_WPA_VERSION_2;
5137
5138 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005139 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
5140 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005141 }
5142
5143 if (params->pairwise_suite != WPA_CIPHER_NONE) {
5144 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
5145 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005146 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5147 cipher))
5148 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005149 }
5150
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005151 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
5152 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
5153 /*
5154 * This is likely to work even though many drivers do not
5155 * advertise support for operations without GTK.
5156 */
5157 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
5158 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005159 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
5160 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005161 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
5162 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005163 }
5164
5165 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5166 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5167 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
5168 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07005169 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005170 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
5171 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005172 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005173 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
5174 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08005175 int mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005176
5177 switch (params->key_mgmt_suite) {
5178 case WPA_KEY_MGMT_CCKM:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08005179 mgmt = RSN_AUTH_KEY_MGMT_CCKM;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005180 break;
5181 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08005182 mgmt = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005183 break;
5184 case WPA_KEY_MGMT_FT_IEEE8021X:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08005185 mgmt = RSN_AUTH_KEY_MGMT_FT_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005186 break;
5187 case WPA_KEY_MGMT_FT_PSK:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08005188 mgmt = RSN_AUTH_KEY_MGMT_FT_PSK;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005189 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005190 case WPA_KEY_MGMT_IEEE8021X_SHA256:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08005191 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005192 break;
5193 case WPA_KEY_MGMT_PSK_SHA256:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08005194 mgmt = RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005195 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005196 case WPA_KEY_MGMT_OSEN:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08005197 mgmt = RSN_AUTH_KEY_MGMT_OSEN;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005198 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005199 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08005200 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005201 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005202 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08005203 mgmt = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005204 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005205 case WPA_KEY_MGMT_PSK:
5206 default:
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08005207 mgmt = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005208 break;
5209 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07005210 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005211 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
5212 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005213 }
5214
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005215 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5216 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005217
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005218 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
5219 (params->pairwise_suite == WPA_CIPHER_NONE ||
5220 params->pairwise_suite == WPA_CIPHER_WEP104 ||
5221 params->pairwise_suite == WPA_CIPHER_WEP40) &&
5222 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
5223 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
5224 return -1;
5225
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005226 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5227 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5228 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005229
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005230 if (params->rrm_used) {
5231 u32 drv_rrm_flags = drv->capa.rrm_flags;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005232 if ((!((drv_rrm_flags &
5233 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
5234 (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
5235 !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005236 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
5237 return -1;
5238 }
5239
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005240 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005241 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005242
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005243 if (params->p2p)
5244 wpa_printf(MSG_DEBUG, " * P2P group");
5245
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005246 if (params->pbss) {
5247 wpa_printf(MSG_DEBUG, " * PBSS");
5248 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
5249 return -1;
5250 }
5251
Dmitry Shmidte4663042016-04-04 10:07:49 -07005252 drv->connect_reassoc = 0;
5253 if (params->prev_bssid) {
5254 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
5255 MAC2STR(params->prev_bssid));
5256 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
5257 params->prev_bssid))
5258 return -1;
5259 drv->connect_reassoc = 1;
5260 }
5261
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005262 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005263}
5264
5265
5266static int wpa_driver_nl80211_try_connect(
5267 struct wpa_driver_nl80211_data *drv,
5268 struct wpa_driver_associate_params *params)
5269{
5270 struct nl_msg *msg;
5271 enum nl80211_auth_type type;
5272 int ret;
5273 int algs;
5274
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005275#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005276 if (params->req_key_mgmt_offload && params->psk &&
5277 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5278 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5279 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
5280 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
5281 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
5282 if (ret)
5283 return ret;
5284 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005285#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005286
5287 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
5288 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005289 if (!msg)
5290 return -1;
5291
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005292 ret = nl80211_connect_common(drv, params, msg);
5293 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005294 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005295
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005296 algs = 0;
5297 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5298 algs++;
5299 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5300 algs++;
5301 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5302 algs++;
5303 if (algs > 1) {
5304 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
5305 "selection");
5306 goto skip_auth_type;
5307 }
5308
5309 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5310 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
5311 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5312 type = NL80211_AUTHTYPE_SHARED_KEY;
5313 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5314 type = NL80211_AUTHTYPE_NETWORK_EAP;
5315 else if (params->auth_alg & WPA_AUTH_ALG_FT)
5316 type = NL80211_AUTHTYPE_FT;
5317 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005318 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005319
5320 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005321 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
5322 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005323
5324skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005325 ret = nl80211_set_conn_keys(params, msg);
5326 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005327 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005328
5329 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5330 msg = NULL;
5331 if (ret) {
5332 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
5333 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005334 } else {
5335 wpa_printf(MSG_DEBUG,
5336 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005337 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005338
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005339fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005340 nlmsg_free(msg);
5341 return ret;
5342
5343}
5344
5345
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005346static int wpa_driver_nl80211_connect(
5347 struct wpa_driver_nl80211_data *drv,
5348 struct wpa_driver_associate_params *params)
5349{
Jithu Jancea7c60b42014-12-03 18:54:40 +05305350 int ret;
5351
5352 /* Store the connection attempted bssid for future use */
5353 if (params->bssid)
5354 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5355 else
5356 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
5357
5358 ret = wpa_driver_nl80211_try_connect(drv, params);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005359 if (ret == -EALREADY) {
5360 /*
5361 * cfg80211 does not currently accept new connections if
5362 * we are already connected. As a workaround, force
5363 * disconnection and try again.
5364 */
5365 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
5366 "disconnecting before reassociation "
5367 "attempt");
5368 if (wpa_driver_nl80211_disconnect(
5369 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
5370 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005371 ret = wpa_driver_nl80211_try_connect(drv, params);
5372 }
5373 return ret;
5374}
5375
5376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005377static int wpa_driver_nl80211_associate(
5378 void *priv, struct wpa_driver_associate_params *params)
5379{
5380 struct i802_bss *bss = priv;
5381 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005382 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005383 struct nl_msg *msg;
5384
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005385 nl80211_unmask_11b_rates(bss);
5386
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005387 if (params->mode == IEEE80211_MODE_AP)
5388 return wpa_driver_nl80211_ap(drv, params);
5389
5390 if (params->mode == IEEE80211_MODE_IBSS)
5391 return wpa_driver_nl80211_ibss(drv, params);
5392
5393 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005394 enum nl80211_iftype nlmode = params->p2p ?
5395 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5396
5397 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005398 return -1;
5399 return wpa_driver_nl80211_connect(drv, params);
5400 }
5401
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005402 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005403
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005404 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
5405 drv->ifindex);
5406 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005407 if (!msg)
5408 return -1;
5409
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005410 ret = nl80211_connect_common(drv, params, msg);
5411 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005412 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005413
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005414 if (params->fils_kek) {
5415 wpa_printf(MSG_DEBUG, " * FILS KEK (len=%u)",
5416 (unsigned int) params->fils_kek_len);
5417 if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
5418 params->fils_kek))
5419 goto fail;
5420 }
5421 if (params->fils_nonces) {
5422 wpa_hexdump(MSG_DEBUG, " * FILS nonces (for AAD)",
5423 params->fils_nonces,
5424 params->fils_nonces_len);
5425 if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
5426 params->fils_nonces_len, params->fils_nonces))
5427 goto fail;
5428 }
5429
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005430 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5431 msg = NULL;
5432 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005433 wpa_dbg(drv->ctx, MSG_DEBUG,
5434 "nl80211: MLME command failed (assoc): ret=%d (%s)",
5435 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005436 nl80211_dump_scan(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005437 } else {
5438 wpa_printf(MSG_DEBUG,
5439 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005440 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005441
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005442fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005443 nlmsg_free(msg);
5444 return ret;
5445}
5446
5447
5448static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005449 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005450{
5451 struct nl_msg *msg;
5452 int ret = -ENOBUFS;
5453
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005454 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
5455 ifindex, mode, nl80211_iftype_str(mode));
5456
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005457 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
5458 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
5459 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005460
5461 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005462 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005463 if (!ret)
5464 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005465fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005466 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005467 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
5468 " %d (%s)", ifindex, mode, ret, strerror(-ret));
5469 return ret;
5470}
5471
5472
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005473static int wpa_driver_nl80211_set_mode_impl(
5474 struct i802_bss *bss,
5475 enum nl80211_iftype nlmode,
5476 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005477{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005478 struct wpa_driver_nl80211_data *drv = bss->drv;
5479 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005480 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005481 int was_ap = is_ap_interface(drv->nlmode);
5482 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005483 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005484
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07005485 if (TEST_FAIL())
5486 return -1;
5487
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005488 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5489 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
5490 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005491
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005492 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005493 drv->nlmode = nlmode;
5494 ret = 0;
5495 goto done;
5496 }
5497
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005498 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005499 return -1;
5500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005501 if (nlmode == drv->nlmode) {
5502 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
5503 "requested mode - ignore error");
5504 ret = 0;
5505 goto done; /* Already in the requested mode */
5506 }
5507
5508 /* mac80211 doesn't allow mode changes while the device is up, so
5509 * take the device down, try to set the mode again, and bring the
5510 * device back up.
5511 */
5512 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
5513 "interface down");
5514 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005515 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005516 if (res == -EACCES || res == -ENODEV)
5517 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005518 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005519 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
5520 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005521 os_sleep(0, 100000);
5522 continue;
5523 }
5524
5525 /*
5526 * Setting the mode will fail for some drivers if the phy is
5527 * on a frequency that the mode is disallowed in.
5528 */
5529 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005530 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005531 if (res) {
5532 wpa_printf(MSG_DEBUG,
5533 "nl80211: Failed to set frequency on interface");
5534 }
5535 }
5536
5537 /* Try to set the mode again while the interface is down */
5538 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5539 if (mode_switch_res == -EBUSY) {
5540 wpa_printf(MSG_DEBUG,
5541 "nl80211: Delaying mode set while interface going down");
5542 os_sleep(0, 100000);
5543 continue;
5544 }
5545 ret = mode_switch_res;
5546 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005547 }
5548
5549 if (!ret) {
5550 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5551 "interface is down");
5552 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005553 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005554 }
5555
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005556 /* Bring the interface back up */
5557 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
5558 if (res != 0) {
5559 wpa_printf(MSG_DEBUG,
5560 "nl80211: Failed to set interface up after switching mode");
5561 ret = -1;
5562 }
5563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005564done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005565 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005566 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5567 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005568 return ret;
5569 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005570
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005571 if (is_p2p_net_interface(nlmode)) {
5572 wpa_printf(MSG_DEBUG,
5573 "nl80211: Interface %s mode change to P2P - disable 11b rates",
5574 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005575 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005576 } else if (drv->disabled_11b_rates) {
5577 wpa_printf(MSG_DEBUG,
5578 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
5579 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005580 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005581 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005582
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005583 if (is_ap_interface(nlmode)) {
5584 nl80211_mgmt_unsubscribe(bss, "start AP");
5585 /* Setup additional AP mode functionality if needed */
5586 if (nl80211_setup_ap(bss))
5587 return -1;
5588 } else if (was_ap) {
5589 /* Remove additional AP mode functionality */
5590 nl80211_teardown_ap(bss);
5591 } else {
5592 nl80211_mgmt_unsubscribe(bss, "mode change");
5593 }
5594
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005595 if (is_mesh_interface(nlmode) &&
5596 nl80211_mgmt_subscribe_mesh(bss))
5597 return -1;
5598
Dmitry Shmidt04949592012-07-19 12:16:46 -07005599 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005600 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005601 nl80211_mgmt_subscribe_non_ap(bss) < 0)
5602 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
5603 "frame processing - ignore for now");
5604
5605 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005606}
5607
5608
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005609int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
5610 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005611{
5612 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
5613}
5614
5615
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005616static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
5617 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005618{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005619 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005620 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005621}
5622
5623
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005624static int wpa_driver_nl80211_get_capa(void *priv,
5625 struct wpa_driver_capa *capa)
5626{
5627 struct i802_bss *bss = priv;
5628 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07005629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005630 if (!drv->has_capability)
5631 return -1;
5632 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005633 if (drv->extended_capa && drv->extended_capa_mask) {
5634 capa->extended_capa = drv->extended_capa;
5635 capa->extended_capa_mask = drv->extended_capa_mask;
5636 capa->extended_capa_len = drv->extended_capa_len;
5637 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005638
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005639 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005640}
5641
5642
5643static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5644{
5645 struct i802_bss *bss = priv;
5646 struct wpa_driver_nl80211_data *drv = bss->drv;
5647
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005648 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
5649 bss->ifname, drv->operstate, state,
5650 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005651 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005652 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005653 state ? IF_OPER_UP : IF_OPER_DORMANT);
5654}
5655
5656
5657static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5658{
5659 struct i802_bss *bss = priv;
5660 struct wpa_driver_nl80211_data *drv = bss->drv;
5661 struct nl_msg *msg;
5662 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005663 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005664
5665 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
5666 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
5667 return 0;
5668 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005669
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005670 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
5671 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
5672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005673 os_memset(&upd, 0, sizeof(upd));
5674 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5675 if (authorized)
5676 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005677
5678 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5679 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
5680 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
5681 nlmsg_free(msg);
5682 return -ENOBUFS;
5683 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005684
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005685 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005686 if (!ret)
5687 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005688 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
5689 ret, strerror(-ret));
5690 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005691}
5692
5693
Jouni Malinen75ecf522011-06-27 15:19:46 -07005694/* Set kernel driver on given frequency (MHz) */
5695static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005696{
Jouni Malinen75ecf522011-06-27 15:19:46 -07005697 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005698 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005699}
5700
5701
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005702static inline int min_int(int a, int b)
5703{
5704 if (a < b)
5705 return a;
5706 return b;
5707}
5708
5709
5710static int get_key_handler(struct nl_msg *msg, void *arg)
5711{
5712 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5713 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5714
5715 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5716 genlmsg_attrlen(gnlh, 0), NULL);
5717
5718 /*
5719 * TODO: validate the key index and mac address!
5720 * Otherwise, there's a race condition as soon as
5721 * the kernel starts sending key notifications.
5722 */
5723
5724 if (tb[NL80211_ATTR_KEY_SEQ])
5725 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5726 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5727 return NL_SKIP;
5728}
5729
5730
5731static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5732 int idx, u8 *seq)
5733{
5734 struct i802_bss *bss = priv;
5735 struct wpa_driver_nl80211_data *drv = bss->drv;
5736 struct nl_msg *msg;
5737
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005738 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
5739 NL80211_CMD_GET_KEY);
5740 if (!msg ||
5741 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
5742 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
5743 nlmsg_free(msg);
5744 return -ENOBUFS;
5745 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005746
5747 memset(seq, 0, 6);
5748
5749 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005750}
5751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005752
5753static int i802_set_rts(void *priv, int rts)
5754{
5755 struct i802_bss *bss = priv;
5756 struct wpa_driver_nl80211_data *drv = bss->drv;
5757 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005758 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005759 u32 val;
5760
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005761 if (rts >= 2347)
5762 val = (u32) -1;
5763 else
5764 val = rts;
5765
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005766 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5767 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
5768 nlmsg_free(msg);
5769 return -ENOBUFS;
5770 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005771
5772 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5773 if (!ret)
5774 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005775 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5776 "%d (%s)", rts, ret, strerror(-ret));
5777 return ret;
5778}
5779
5780
5781static int i802_set_frag(void *priv, int frag)
5782{
5783 struct i802_bss *bss = priv;
5784 struct wpa_driver_nl80211_data *drv = bss->drv;
5785 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005786 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005787 u32 val;
5788
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005789 if (frag >= 2346)
5790 val = (u32) -1;
5791 else
5792 val = frag;
5793
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005794 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5795 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
5796 nlmsg_free(msg);
5797 return -ENOBUFS;
5798 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005799
5800 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5801 if (!ret)
5802 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005803 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
5804 "%d: %d (%s)", frag, ret, strerror(-ret));
5805 return ret;
5806}
5807
5808
5809static int i802_flush(void *priv)
5810{
5811 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005812 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005813 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005814
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005815 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
5816 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005817
5818 /*
5819 * XXX: FIX! this needs to flush all VLANs too
5820 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005821 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
5822 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005823 if (res) {
5824 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
5825 "(%s)", res, strerror(-res));
5826 }
5827 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005828}
5829
5830
5831static int get_sta_handler(struct nl_msg *msg, void *arg)
5832{
5833 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5834 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5835 struct hostap_sta_driver_data *data = arg;
5836 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
5837 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
5838 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
5839 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
5840 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
5841 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
5842 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005843 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005844 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
5845 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005846 };
5847
5848 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5849 genlmsg_attrlen(gnlh, 0), NULL);
5850
5851 /*
5852 * TODO: validate the interface and mac address!
5853 * Otherwise, there's a race condition as soon as
5854 * the kernel starts sending station notifications.
5855 */
5856
5857 if (!tb[NL80211_ATTR_STA_INFO]) {
5858 wpa_printf(MSG_DEBUG, "sta stats missing!");
5859 return NL_SKIP;
5860 }
5861 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
5862 tb[NL80211_ATTR_STA_INFO],
5863 stats_policy)) {
5864 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
5865 return NL_SKIP;
5866 }
5867
5868 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
5869 data->inactive_msec =
5870 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005871 /* For backwards compatibility, fetch the 32-bit counters first. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005872 if (stats[NL80211_STA_INFO_RX_BYTES])
5873 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
5874 if (stats[NL80211_STA_INFO_TX_BYTES])
5875 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005876 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
5877 stats[NL80211_STA_INFO_TX_BYTES64]) {
5878 /*
5879 * The driver supports 64-bit counters, so use them to override
5880 * the 32-bit values.
5881 */
5882 data->rx_bytes =
5883 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
5884 data->tx_bytes =
5885 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
5886 data->bytes_64bit = 1;
5887 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005888 if (stats[NL80211_STA_INFO_RX_PACKETS])
5889 data->rx_packets =
5890 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
5891 if (stats[NL80211_STA_INFO_TX_PACKETS])
5892 data->tx_packets =
5893 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005894 if (stats[NL80211_STA_INFO_TX_FAILED])
5895 data->tx_retry_failed =
5896 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005897
5898 return NL_SKIP;
5899}
5900
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005901static int i802_read_sta_data(struct i802_bss *bss,
5902 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005903 const u8 *addr)
5904{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005905 struct nl_msg *msg;
5906
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005907 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
5908 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
5909 nlmsg_free(msg);
5910 return -ENOBUFS;
5911 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005912
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005913 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005914}
5915
5916
5917static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
5918 int cw_min, int cw_max, int burst_time)
5919{
5920 struct i802_bss *bss = priv;
5921 struct wpa_driver_nl80211_data *drv = bss->drv;
5922 struct nl_msg *msg;
5923 struct nlattr *txq, *params;
5924
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005925 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005926 if (!msg)
5927 return -1;
5928
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005929 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
5930 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005931 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005932
5933 /* We are only sending parameters for a single TXQ at a time */
5934 params = nla_nest_start(msg, 1);
5935 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005936 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005937
5938 switch (queue) {
5939 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005940 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
5941 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005942 break;
5943 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005944 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
5945 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005946 break;
5947 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005948 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
5949 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005950 break;
5951 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005952 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
5953 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005954 break;
5955 }
5956 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
5957 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005958 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
5959 (burst_time * 100 + 16) / 32) ||
5960 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
5961 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
5962 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
5963 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005964
5965 nla_nest_end(msg, params);
5966
5967 nla_nest_end(msg, txq);
5968
5969 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5970 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005971 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005972fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005973 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005974 return -1;
5975}
5976
5977
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005978static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005979 const char *ifname, int vlan_id)
5980{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005981 struct wpa_driver_nl80211_data *drv = bss->drv;
5982 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005983 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005984
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005985 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
5986 ", ifname=%s[%d], vlan_id=%d)",
5987 bss->ifname, if_nametoindex(bss->ifname),
5988 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005989 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5990 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5991 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
5992 nlmsg_free(msg);
5993 return -ENOBUFS;
5994 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005995
5996 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5997 if (ret < 0) {
5998 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
5999 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
6000 MAC2STR(addr), ifname, vlan_id, ret,
6001 strerror(-ret));
6002 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006003 return ret;
6004}
6005
6006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006007static int i802_get_inact_sec(void *priv, const u8 *addr)
6008{
6009 struct hostap_sta_driver_data data;
6010 int ret;
6011
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08006012 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006013 data.inactive_msec = (unsigned long) -1;
6014 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006015 if (ret == -ENOENT)
6016 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006017 if (ret || data.inactive_msec == (unsigned long) -1)
6018 return -1;
6019 return data.inactive_msec / 1000;
6020}
6021
6022
6023static int i802_sta_clear_stats(void *priv, const u8 *addr)
6024{
6025#if 0
6026 /* TODO */
6027#endif
6028 return 0;
6029}
6030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006031
6032static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
6033 int reason)
6034{
6035 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006036 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006037 struct ieee80211_mgmt mgmt;
Dmitry Shmidt29333592017-01-09 12:27:11 -08006038 u8 channel;
6039
6040 if (ieee80211_freq_to_chan(bss->freq, &channel) ==
6041 HOSTAPD_MODE_IEEE80211AD) {
6042 /* Deauthentication is not used in DMG/IEEE 802.11ad;
6043 * disassociate the STA instead. */
6044 return i802_sta_disassoc(priv, own_addr, addr, reason);
6045 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006046
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006047 if (is_mesh_interface(drv->nlmode))
6048 return -1;
6049
Dmitry Shmidt04949592012-07-19 12:16:46 -07006050 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006051 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006053 memset(&mgmt, 0, sizeof(mgmt));
6054 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6055 WLAN_FC_STYPE_DEAUTH);
6056 memcpy(mgmt.da, addr, ETH_ALEN);
6057 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6058 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6059 mgmt.u.deauth.reason_code = host_to_le16(reason);
6060 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6061 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006062 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006063 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006064}
6065
6066
6067static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
6068 int reason)
6069{
6070 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006071 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006072 struct ieee80211_mgmt mgmt;
6073
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006074 if (is_mesh_interface(drv->nlmode))
6075 return -1;
6076
Dmitry Shmidt04949592012-07-19 12:16:46 -07006077 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006078 return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006080 memset(&mgmt, 0, sizeof(mgmt));
6081 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6082 WLAN_FC_STYPE_DISASSOC);
6083 memcpy(mgmt.da, addr, ETH_ALEN);
6084 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6085 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6086 mgmt.u.disassoc.reason_code = host_to_le16(reason);
6087 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6088 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006089 sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006090 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006091}
6092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006093
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006094static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
6095{
6096 char buf[200], *pos, *end;
6097 int i, res;
6098
6099 pos = buf;
6100 end = pos + sizeof(buf);
6101
6102 for (i = 0; i < drv->num_if_indices; i++) {
6103 if (!drv->if_indices[i])
6104 continue;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006105 res = os_snprintf(pos, end - pos, " %d(%d)",
6106 drv->if_indices[i],
6107 drv->if_indices_reason[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006108 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006109 break;
6110 pos += res;
6111 }
6112 *pos = '\0';
6113
6114 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
6115 drv->num_if_indices, buf);
6116}
6117
6118
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006119static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6120 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006121{
6122 int i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006123 int *old, *old_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006124
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006125 wpa_printf(MSG_DEBUG,
6126 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
6127 ifidx, ifidx_reason);
6128 if (have_ifidx(drv, ifidx, ifidx_reason)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006129 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
6130 ifidx);
6131 return;
6132 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006133 for (i = 0; i < drv->num_if_indices; i++) {
6134 if (drv->if_indices[i] == 0) {
6135 drv->if_indices[i] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006136 drv->if_indices_reason[i] = ifidx_reason;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006137 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006138 return;
6139 }
6140 }
6141
6142 if (drv->if_indices != drv->default_if_indices)
6143 old = drv->if_indices;
6144 else
6145 old = NULL;
6146
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006147 if (drv->if_indices_reason != drv->default_if_indices_reason)
6148 old_reason = drv->if_indices_reason;
6149 else
6150 old_reason = NULL;
6151
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006152 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
6153 sizeof(int));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006154 drv->if_indices_reason = os_realloc_array(old_reason,
6155 drv->num_if_indices + 1,
6156 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006157 if (!drv->if_indices) {
6158 if (!old)
6159 drv->if_indices = drv->default_if_indices;
6160 else
6161 drv->if_indices = old;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006162 }
6163 if (!drv->if_indices_reason) {
6164 if (!old_reason)
6165 drv->if_indices_reason = drv->default_if_indices_reason;
6166 else
Dmitry Shmidte4663042016-04-04 10:07:49 -07006167 drv->if_indices_reason = old_reason;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006168 }
6169 if (!drv->if_indices || !drv->if_indices_reason) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07006170 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
6171 "interfaces");
6172 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
6173 return;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006174 }
6175 if (!old)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006176 os_memcpy(drv->if_indices, drv->default_if_indices,
6177 sizeof(drv->default_if_indices));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006178 if (!old_reason)
6179 os_memcpy(drv->if_indices_reason,
6180 drv->default_if_indices_reason,
6181 sizeof(drv->default_if_indices_reason));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006182 drv->if_indices[drv->num_if_indices] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006183 drv->if_indices_reason[drv->num_if_indices] = ifidx_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006184 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006185 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006186}
6187
6188
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006189static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6190 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006191{
6192 int i;
6193
6194 for (i = 0; i < drv->num_if_indices; i++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006195 if ((drv->if_indices[i] == ifidx || ifidx == IFIDX_ANY) &&
6196 (drv->if_indices_reason[i] == ifidx_reason ||
6197 ifidx_reason == IFIDX_ANY)) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07006198 drv->if_indices[i] = 0;
6199 break;
6200 }
6201 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006202 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006203}
6204
6205
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006206static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6207 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006208{
6209 int i;
6210
6211 for (i = 0; i < drv->num_if_indices; i++)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006212 if (drv->if_indices[i] == ifidx &&
6213 (drv->if_indices_reason[i] == ifidx_reason ||
6214 ifidx_reason == IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07006215 return 1;
6216
6217 return 0;
6218}
6219
6220
6221static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006222 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006223{
6224 struct i802_bss *bss = priv;
6225 struct wpa_driver_nl80211_data *drv = bss->drv;
6226 char name[IFNAMSIZ + 1];
6227
6228 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006229 if (ifname_wds)
6230 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
6231
Jouni Malinen75ecf522011-06-27 15:19:46 -07006232 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
6233 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
6234 if (val) {
6235 if (!if_nametoindex(name)) {
6236 if (nl80211_create_iface(drv, name,
6237 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006238 bss->addr, 1, NULL, NULL, 0) <
6239 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006240 return -1;
6241 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006242 linux_br_add_if(drv->global->ioctl_sock,
6243 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006244 return -1;
6245 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006246 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
6247 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
6248 "interface %s up", name);
6249 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006250 return i802_set_sta_vlan(priv, addr, name, 0);
6251 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006252 if (bridge_ifname)
6253 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
6254 name);
6255
Jouni Malinen75ecf522011-06-27 15:19:46 -07006256 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006257 nl80211_remove_iface(drv, if_nametoindex(name));
6258 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006259 }
6260}
6261
6262
6263static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
6264{
6265 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6266 struct sockaddr_ll lladdr;
6267 unsigned char buf[3000];
6268 int len;
6269 socklen_t fromlen = sizeof(lladdr);
6270
6271 len = recvfrom(sock, buf, sizeof(buf), 0,
6272 (struct sockaddr *)&lladdr, &fromlen);
6273 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006274 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
6275 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006276 return;
6277 }
6278
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006279 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07006280 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
6281}
6282
6283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006284static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
6285 struct i802_bss *bss,
6286 const char *brname, const char *ifname)
6287{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006288 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006289 char in_br[IFNAMSIZ];
6290
6291 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006292 br_ifindex = if_nametoindex(brname);
6293 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006294 /*
6295 * Bridge was configured, but the bridge device does
6296 * not exist. Try to add it now.
6297 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006298 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006299 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
6300 "bridge interface %s: %s",
6301 brname, strerror(errno));
6302 return -1;
6303 }
6304 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006305 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006306 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006307 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006308 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006309
6310 if (linux_br_get(in_br, ifname) == 0) {
6311 if (os_strcmp(in_br, brname) == 0)
6312 return 0; /* already in the bridge */
6313
6314 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
6315 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006316 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
6317 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006318 wpa_printf(MSG_ERROR, "nl80211: Failed to "
6319 "remove interface %s from bridge "
6320 "%s: %s",
6321 ifname, brname, strerror(errno));
6322 return -1;
6323 }
6324 }
6325
6326 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
6327 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006328 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006329 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
6330 "into bridge %s: %s",
6331 ifname, brname, strerror(errno));
6332 return -1;
6333 }
6334 bss->added_if_into_bridge = 1;
6335
6336 return 0;
6337}
6338
6339
6340static void *i802_init(struct hostapd_data *hapd,
6341 struct wpa_init_params *params)
6342{
6343 struct wpa_driver_nl80211_data *drv;
6344 struct i802_bss *bss;
6345 size_t i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006346 char master_ifname[IFNAMSIZ];
6347 int ifindex, br_ifindex = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006348 int br_added = 0;
6349
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006350 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
6351 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006352 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006353 if (bss == NULL)
6354 return NULL;
6355
6356 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006357
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006358 if (linux_br_get(master_ifname, params->ifname) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006359 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006360 params->ifname, master_ifname);
6361 br_ifindex = if_nametoindex(master_ifname);
6362 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6363 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
6364 linux_master_get(master_ifname, params->ifname) == 0) {
6365 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
6366 params->ifname, master_ifname);
6367 /* start listening for EAPOL on the master interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006368 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006369
6370 /* check if master itself is under bridge */
6371 if (linux_br_get(master_ifname, master_ifname) == 0) {
6372 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
6373 master_ifname);
6374 br_ifindex = if_nametoindex(master_ifname);
6375 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6376 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006377 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006378 master_ifname[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006379 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006380
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006381 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006383 for (i = 0; i < params->num_bridge; i++) {
6384 if (params->bridge[i]) {
6385 ifindex = if_nametoindex(params->bridge[i]);
6386 if (ifindex)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006387 add_ifidx(drv, ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006388 if (ifindex == br_ifindex)
6389 br_added = 1;
6390 }
6391 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006392
6393 /* start listening for EAPOL on the default AP interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006394 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006395
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006396 if (params->num_bridge && params->bridge[0]) {
6397 if (i802_check_bridge(drv, bss, params->bridge[0],
6398 params->ifname) < 0)
6399 goto failed;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006400 if (os_strcmp(params->bridge[0], master_ifname) != 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006401 br_added = 1;
6402 }
6403
6404 if (!br_added && br_ifindex &&
6405 (params->num_bridge == 0 || !params->bridge[0]))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006406 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006407
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006408#ifdef CONFIG_LIBNL3_ROUTE
6409 if (bss->added_if_into_bridge) {
6410 drv->rtnl_sk = nl_socket_alloc();
6411 if (drv->rtnl_sk == NULL) {
6412 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
6413 goto failed;
6414 }
6415
6416 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
6417 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
6418 strerror(errno));
6419 goto failed;
6420 }
6421 }
6422#endif /* CONFIG_LIBNL3_ROUTE */
6423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006424 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
6425 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006426 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
6427 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006428 goto failed;
6429 }
6430
6431 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
6432 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006433 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006434 goto failed;
6435 }
6436
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006437 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6438 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006439 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006440 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006441
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006442 memcpy(bss->addr, params->own_addr, ETH_ALEN);
6443
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006444 return bss;
6445
6446failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006447 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006448 return NULL;
6449}
6450
6451
6452static void i802_deinit(void *priv)
6453{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006454 struct i802_bss *bss = priv;
6455 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006456}
6457
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006458
6459static enum nl80211_iftype wpa_driver_nl80211_if_type(
6460 enum wpa_driver_if_type type)
6461{
6462 switch (type) {
6463 case WPA_IF_STATION:
6464 return NL80211_IFTYPE_STATION;
6465 case WPA_IF_P2P_CLIENT:
6466 case WPA_IF_P2P_GROUP:
6467 return NL80211_IFTYPE_P2P_CLIENT;
6468 case WPA_IF_AP_VLAN:
6469 return NL80211_IFTYPE_AP_VLAN;
6470 case WPA_IF_AP_BSS:
6471 return NL80211_IFTYPE_AP;
6472 case WPA_IF_P2P_GO:
6473 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006474 case WPA_IF_P2P_DEVICE:
6475 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006476 case WPA_IF_MESH:
6477 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006478 default:
6479 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006480 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006481}
6482
6483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006484static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
6485{
6486 struct wpa_driver_nl80211_data *drv;
6487 dl_list_for_each(drv, &global->interfaces,
6488 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006489 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006490 return 1;
6491 }
6492 return 0;
6493}
6494
6495
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006496static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006497{
6498 unsigned int idx;
6499
6500 if (!drv->global)
6501 return -1;
6502
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006503 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006504 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006505 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006506 new_addr[0] ^= idx << 2;
6507 if (!nl80211_addr_in_use(drv->global, new_addr))
6508 break;
6509 }
6510 if (idx == 64)
6511 return -1;
6512
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006513 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006514 MACSTR, MAC2STR(new_addr));
6515
6516 return 0;
6517}
6518
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006519
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006520struct wdev_info {
6521 u64 wdev_id;
6522 int wdev_id_set;
6523 u8 macaddr[ETH_ALEN];
6524};
6525
6526static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
6527{
6528 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6529 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6530 struct wdev_info *wi = arg;
6531
6532 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6533 genlmsg_attrlen(gnlh, 0), NULL);
6534 if (tb[NL80211_ATTR_WDEV]) {
6535 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
6536 wi->wdev_id_set = 1;
6537 }
6538
6539 if (tb[NL80211_ATTR_MAC])
6540 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
6541 ETH_ALEN);
6542
6543 return NL_SKIP;
6544}
6545
6546
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006547static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
6548 const char *ifname, const u8 *addr,
6549 void *bss_ctx, void **drv_priv,
6550 char *force_ifname, u8 *if_addr,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006551 const char *bridge, int use_existing,
6552 int setup_ap)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006553{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006554 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006555 struct i802_bss *bss = priv;
6556 struct wpa_driver_nl80211_data *drv = bss->drv;
6557 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006558 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006559
6560 if (addr)
6561 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006562 nlmode = wpa_driver_nl80211_if_type(type);
6563 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
6564 struct wdev_info p2pdev_info;
6565
6566 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
6567 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
6568 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006569 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006570 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
6571 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
6572 ifname);
6573 return -1;
6574 }
6575
6576 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
6577 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
6578 if (!is_zero_ether_addr(p2pdev_info.macaddr))
6579 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
6580 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
6581 ifname,
6582 (long long unsigned int) p2pdev_info.wdev_id);
6583 } else {
6584 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006585 0, NULL, NULL, use_existing);
6586 if (use_existing && ifidx == -ENFILE) {
6587 added = 0;
6588 ifidx = if_nametoindex(ifname);
6589 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006590 return -1;
6591 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006592 }
6593
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006594 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006595 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006596 os_memcpy(if_addr, bss->addr, ETH_ALEN);
6597 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006598 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006599 if (added)
6600 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006601 return -1;
6602 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006603 }
6604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006605 if (!addr &&
6606 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006607 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
6608 type == WPA_IF_STATION)) {
6609 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006610 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006611
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006612 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006613 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006614 if (added)
6615 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006616 return -1;
6617 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006618 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006619 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006620 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006621 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006622 if (added)
6623 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006624 return -1;
6625 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006626 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006627 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006628 if (added)
6629 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006630 return -1;
6631 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006632 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006633 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006634 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006635
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006636 if (type == WPA_IF_AP_BSS && setup_ap) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006637 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
6638 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006639 if (added)
6640 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006641 return -1;
6642 }
6643
6644 if (bridge &&
6645 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
6646 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
6647 "interface %s to a bridge %s",
6648 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006649 if (added)
6650 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006651 os_free(new_bss);
6652 return -1;
6653 }
6654
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006655 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
6656 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006657 if (added)
6658 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006659 os_free(new_bss);
6660 return -1;
6661 }
6662 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006663 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006664 new_bss->ifindex = ifidx;
6665 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006666 new_bss->next = drv->first_bss->next;
6667 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006668 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006669 new_bss->added_if = added;
6670 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006671 if (drv_priv)
6672 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006673 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006674
6675 /* Subscribe management frames for this WPA_IF_AP_BSS */
6676 if (nl80211_setup_ap(new_bss))
6677 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006678 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006679
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006680 if (drv->global)
6681 drv->global->if_add_ifindex = ifidx;
6682
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006683 /*
6684 * Some virtual interfaces need to process EAPOL packets and events on
6685 * the parent interface. This is used mainly with hostapd.
6686 */
6687 if (ifidx > 0 &&
6688 (drv->hostapd ||
6689 nlmode == NL80211_IFTYPE_AP_VLAN ||
6690 nlmode == NL80211_IFTYPE_WDS ||
6691 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006692 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006693
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006694 return 0;
6695}
6696
6697
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006698static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006699 enum wpa_driver_if_type type,
6700 const char *ifname)
6701{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006702 struct wpa_driver_nl80211_data *drv = bss->drv;
6703 int ifindex = if_nametoindex(ifname);
6704
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006705 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
6706 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006707 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07006708 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006709 else if (ifindex > 0 && !bss->added_if) {
6710 struct wpa_driver_nl80211_data *drv2;
6711 dl_list_for_each(drv2, &drv->global->interfaces,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006712 struct wpa_driver_nl80211_data, list) {
6713 del_ifidx(drv2, ifindex, IFIDX_ANY);
6714 del_ifidx(drv2, IFIDX_ANY, ifindex);
6715 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006716 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006717
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006718 if (type != WPA_IF_AP_BSS)
6719 return 0;
6720
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006721 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006722 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
6723 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006724 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6725 "interface %s from bridge %s: %s",
6726 bss->ifname, bss->brname, strerror(errno));
6727 }
6728 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006729 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006730 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6731 "bridge %s: %s",
6732 bss->brname, strerror(errno));
6733 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006734
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006735 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006736 struct i802_bss *tbss;
6737
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006738 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
6739 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006740 if (tbss->next == bss) {
6741 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006742 /* Unsubscribe management frames */
6743 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006744 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006745 if (!bss->added_if)
6746 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006747 os_free(bss);
6748 bss = NULL;
6749 break;
6750 }
6751 }
6752 if (bss)
6753 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
6754 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006755 } else {
6756 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
6757 nl80211_teardown_ap(bss);
6758 if (!bss->added_if && !drv->first_bss->next)
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08006759 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006760 nl80211_destroy_bss(bss);
6761 if (!bss->added_if)
6762 i802_set_iface_flags(bss, 0);
6763 if (drv->first_bss->next) {
6764 drv->first_bss = drv->first_bss->next;
6765 drv->ctx = drv->first_bss->ctx;
6766 os_free(bss);
6767 } else {
6768 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
6769 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006770 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006771
6772 return 0;
6773}
6774
6775
6776static int cookie_handler(struct nl_msg *msg, void *arg)
6777{
6778 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6779 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6780 u64 *cookie = arg;
6781 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6782 genlmsg_attrlen(gnlh, 0), NULL);
6783 if (tb[NL80211_ATTR_COOKIE])
6784 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
6785 return NL_SKIP;
6786}
6787
6788
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006789static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006790 unsigned int freq, unsigned int wait,
6791 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006792 u64 *cookie_out, int no_cck, int no_ack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006793 int offchanok, const u16 *csa_offs,
6794 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006795{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006796 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006797 struct nl_msg *msg;
6798 u64 cookie;
6799 int ret = -1;
6800
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006801 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07006802 "no_ack=%d offchanok=%d",
6803 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006804 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006805
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006806 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
6807 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
6808 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
6809 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6810 drv->test_use_roc_tx) &&
6811 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
6812 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
6813 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006814 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
6815 csa_offs_len * sizeof(u16), csa_offs)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006816 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
6817 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006818
6819 cookie = 0;
6820 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6821 msg = NULL;
6822 if (ret) {
6823 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006824 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
6825 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006826 } else {
6827 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
6828 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
6829 (long long unsigned int) cookie);
6830
6831 if (cookie_out)
6832 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006833
6834 if (drv->num_send_action_cookies == MAX_SEND_ACTION_COOKIES) {
6835 wpa_printf(MSG_DEBUG,
6836 "nl80211: Drop oldest pending send action cookie 0x%llx",
6837 (long long unsigned int)
6838 drv->send_action_cookies[0]);
6839 os_memmove(&drv->send_action_cookies[0],
6840 &drv->send_action_cookies[1],
6841 (MAX_SEND_ACTION_COOKIES - 1) *
6842 sizeof(u64));
6843 drv->num_send_action_cookies--;
6844 }
6845 drv->send_action_cookies[drv->num_send_action_cookies] = cookie;
6846 drv->num_send_action_cookies++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006847 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006848
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006849fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006850 nlmsg_free(msg);
6851 return ret;
6852}
6853
6854
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006855static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
6856 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006857 unsigned int wait_time,
6858 const u8 *dst, const u8 *src,
6859 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006860 const u8 *data, size_t data_len,
6861 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006862{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006863 struct wpa_driver_nl80211_data *drv = bss->drv;
6864 int ret = -1;
6865 u8 *buf;
6866 struct ieee80211_hdr *hdr;
6867
6868 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006869 "freq=%u MHz wait=%d ms no_cck=%d)",
6870 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006871
6872 buf = os_zalloc(24 + data_len);
6873 if (buf == NULL)
6874 return ret;
6875 os_memcpy(buf + 24, data, data_len);
6876 hdr = (struct ieee80211_hdr *) buf;
6877 hdr->frame_control =
6878 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
6879 os_memcpy(hdr->addr1, dst, ETH_ALEN);
6880 os_memcpy(hdr->addr2, src, ETH_ALEN);
6881 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
6882
Dmitry Shmidt56052862013-10-04 10:23:25 -07006883 if (is_ap_interface(drv->nlmode) &&
6884 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6885 (int) freq == bss->freq || drv->device_ap_sme ||
6886 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006887 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
6888 0, freq, no_cck, 1,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006889 wait_time, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006890 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006891 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006892 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006893 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006894 no_cck, 0, 1, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006895
6896 os_free(buf);
6897 return ret;
6898}
6899
6900
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006901static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006902{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006903 struct wpa_driver_nl80211_data *drv = bss->drv;
6904 struct nl_msg *msg;
6905 int ret;
6906
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08006907 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006908 (long long unsigned int) cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006909 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006910 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006911 nlmsg_free(msg);
6912 return;
6913 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006914
6915 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006916 if (ret)
6917 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
6918 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006919}
6920
6921
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006922static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
6923{
6924 struct i802_bss *bss = priv;
6925 struct wpa_driver_nl80211_data *drv = bss->drv;
6926 unsigned int i;
6927 u64 cookie;
6928
6929 /* Cancel the last pending TX cookie */
6930 nl80211_frame_wait_cancel(bss, drv->send_action_cookie);
6931
6932 /*
6933 * Cancel the other pending TX cookies, if any. This is needed since
6934 * the driver may keep a list of all pending offchannel TX operations
6935 * and free up the radio only once they have expired or cancelled.
6936 */
6937 for (i = drv->num_send_action_cookies; i > 0; i--) {
6938 cookie = drv->send_action_cookies[i - 1];
6939 if (cookie != drv->send_action_cookie)
6940 nl80211_frame_wait_cancel(bss, cookie);
6941 }
6942 drv->num_send_action_cookies = 0;
6943}
6944
6945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006946static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
6947 unsigned int duration)
6948{
6949 struct i802_bss *bss = priv;
6950 struct wpa_driver_nl80211_data *drv = bss->drv;
6951 struct nl_msg *msg;
6952 int ret;
6953 u64 cookie;
6954
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006955 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
6956 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
6957 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
6958 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006959 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006960 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006961
6962 cookie = 0;
6963 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6964 if (ret == 0) {
6965 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
6966 "0x%llx for freq=%u MHz duration=%u",
6967 (long long unsigned int) cookie, freq, duration);
6968 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006969 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006970 return 0;
6971 }
6972 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
6973 "(freq=%d duration=%u): %d (%s)",
6974 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006975 return -1;
6976}
6977
6978
6979static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
6980{
6981 struct i802_bss *bss = priv;
6982 struct wpa_driver_nl80211_data *drv = bss->drv;
6983 struct nl_msg *msg;
6984 int ret;
6985
6986 if (!drv->pending_remain_on_chan) {
6987 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
6988 "to cancel");
6989 return -1;
6990 }
6991
6992 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
6993 "0x%llx",
6994 (long long unsigned int) drv->remain_on_chan_cookie);
6995
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006996 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
6997 if (!msg ||
6998 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
6999 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007000 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007001 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007002
7003 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7004 if (ret == 0)
7005 return 0;
7006 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
7007 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007008 return -1;
7009}
7010
7011
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007012static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007013{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007014 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07007015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007016 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007017 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07007018 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
7019 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007020 /*
7021 * Do not disable Probe Request reporting that was
7022 * enabled in nl80211_setup_ap().
7023 */
7024 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
7025 "Probe Request reporting nl_preq=%p while "
7026 "in AP mode", bss->nl_preq);
7027 } else if (bss->nl_preq) {
7028 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
7029 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007030 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007031 }
7032 return 0;
7033 }
7034
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007035 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007036 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007037 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007038 return 0;
7039 }
7040
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007041 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
7042 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007043 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007044 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
7045 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007046
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007047 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007048 (WLAN_FC_TYPE_MGMT << 2) |
7049 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007050 NULL, 0) < 0)
7051 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07007052
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007053 nl80211_register_eloop_read(&bss->nl_preq,
7054 wpa_driver_nl80211_event_receive,
7055 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007056
7057 return 0;
7058
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007059 out_err:
7060 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007061 return -1;
7062}
7063
7064
7065static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
7066 int ifindex, int disabled)
7067{
7068 struct nl_msg *msg;
7069 struct nlattr *bands, *band;
7070 int ret;
7071
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007072 wpa_printf(MSG_DEBUG,
7073 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
7074 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
7075 "no NL80211_TXRATE_LEGACY constraint");
7076
7077 msg = nl80211_ifindex_msg(drv, ifindex, 0,
7078 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007079 if (!msg)
7080 return -1;
7081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007082 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
7083 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007084 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007085
7086 /*
7087 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
7088 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
7089 * rates. All 5 GHz rates are left enabled.
7090 */
7091 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007092 if (!band ||
7093 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
7094 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
7095 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007096 nla_nest_end(msg, band);
7097
7098 nla_nest_end(msg, bands);
7099
7100 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007101 if (ret) {
7102 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
7103 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007104 } else
7105 drv->disabled_11b_rates = disabled;
7106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107 return ret;
7108
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007109fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007110 nlmsg_free(msg);
7111 return -1;
7112}
7113
7114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007115static int wpa_driver_nl80211_deinit_ap(void *priv)
7116{
7117 struct i802_bss *bss = priv;
7118 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007119 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007120 return -1;
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08007121 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007122 bss->beacon_set = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007123
7124 /*
7125 * If the P2P GO interface was dynamically added, then it is
7126 * possible that the interface change to station is not possible.
7127 */
7128 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
7129 return 0;
7130
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007131 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007132}
7133
7134
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007135static int wpa_driver_nl80211_stop_ap(void *priv)
7136{
7137 struct i802_bss *bss = priv;
7138 struct wpa_driver_nl80211_data *drv = bss->drv;
7139 if (!is_ap_interface(drv->nlmode))
7140 return -1;
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08007141 wpa_driver_nl80211_del_beacon(bss);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007142 bss->beacon_set = 0;
7143 return 0;
7144}
7145
7146
Dmitry Shmidt04949592012-07-19 12:16:46 -07007147static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
7148{
7149 struct i802_bss *bss = priv;
7150 struct wpa_driver_nl80211_data *drv = bss->drv;
7151 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
7152 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007153
7154 /*
7155 * If the P2P Client interface was dynamically added, then it is
7156 * possible that the interface change to station is not possible.
7157 */
7158 if (bss->if_dynamic)
7159 return 0;
7160
Dmitry Shmidt04949592012-07-19 12:16:46 -07007161 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
7162}
7163
7164
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007165static void wpa_driver_nl80211_resume(void *priv)
7166{
7167 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007168 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007169
7170 if (i802_set_iface_flags(bss, 1))
7171 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007172
7173 if (is_p2p_net_interface(nlmode))
7174 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007175}
7176
7177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007178static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
7179{
7180 struct i802_bss *bss = priv;
7181 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007182 struct nl_msg *msg;
7183 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007184
7185 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
7186 "hysteresis=%d", threshold, hysteresis);
7187
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007188 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
7189 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
7190 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
7191 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
7192 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007193 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007194 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007195 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007196
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007197 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007198}
7199
7200
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007201static int get_channel_width(struct nl_msg *msg, void *arg)
7202{
7203 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7204 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7205 struct wpa_signal_info *sig_change = arg;
7206
7207 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7208 genlmsg_attrlen(gnlh, 0), NULL);
7209
7210 sig_change->center_frq1 = -1;
7211 sig_change->center_frq2 = -1;
7212 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
7213
7214 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
7215 sig_change->chanwidth = convert2width(
7216 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
7217 if (tb[NL80211_ATTR_CENTER_FREQ1])
7218 sig_change->center_frq1 =
7219 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
7220 if (tb[NL80211_ATTR_CENTER_FREQ2])
7221 sig_change->center_frq2 =
7222 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
7223 }
7224
7225 return NL_SKIP;
7226}
7227
7228
7229static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
7230 struct wpa_signal_info *sig)
7231{
7232 struct nl_msg *msg;
7233
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007234 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007235 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007236}
7237
7238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007239static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
7240{
7241 struct i802_bss *bss = priv;
7242 struct wpa_driver_nl80211_data *drv = bss->drv;
7243 int res;
7244
7245 os_memset(si, 0, sizeof(*si));
7246 res = nl80211_get_link_signal(drv, si);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007247 if (res) {
7248 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
7249 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
7250 return res;
7251 si->current_signal = 0;
7252 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007253
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007254 res = nl80211_get_channel_width(drv, si);
7255 if (res != 0)
7256 return res;
7257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007258 return nl80211_get_link_noise(drv, si);
7259}
7260
7261
7262static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
7263 int encrypt)
7264{
7265 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007266 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007267 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007268}
7269
7270
7271static int nl80211_set_param(void *priv, const char *param)
7272{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007273 struct i802_bss *bss = priv;
7274 struct wpa_driver_nl80211_data *drv = bss->drv;
7275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007276 if (param == NULL)
7277 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007278 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007279
7280#ifdef CONFIG_P2P
7281 if (os_strstr(param, "use_p2p_group_interface=1")) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007282 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
7283 "interface");
7284 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
7285 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
7286 }
7287#endif /* CONFIG_P2P */
7288
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007289 if (os_strstr(param, "use_monitor=1"))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007290 drv->use_monitor = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007291
7292 if (os_strstr(param, "force_connect_cmd=1")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007293 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007294 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007295 }
7296
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007297 if (os_strstr(param, "force_bss_selection=1"))
7298 drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
7299
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08007300 if (os_strstr(param, "no_offchannel_tx=1")) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08007301 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
7302 drv->test_use_roc_tx = 1;
7303 }
7304
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007305 return 0;
7306}
7307
7308
Dmitry Shmidte4663042016-04-04 10:07:49 -07007309static void * nl80211_global_init(void *ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007310{
7311 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007312 struct netlink_config *cfg;
7313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007314 global = os_zalloc(sizeof(*global));
7315 if (global == NULL)
7316 return NULL;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007317 global->ctx = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007318 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007319 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007320 global->if_add_ifindex = -1;
7321
7322 cfg = os_zalloc(sizeof(*cfg));
7323 if (cfg == NULL)
7324 goto err;
7325
7326 cfg->ctx = global;
7327 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
7328 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
7329 global->netlink = netlink_init(cfg);
7330 if (global->netlink == NULL) {
7331 os_free(cfg);
7332 goto err;
7333 }
7334
7335 if (wpa_driver_nl80211_init_nl_global(global) < 0)
7336 goto err;
7337
7338 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
7339 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007340 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
7341 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007342 goto err;
7343 }
7344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007345 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007346
7347err:
7348 nl80211_global_deinit(global);
7349 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007350}
7351
7352
7353static void nl80211_global_deinit(void *priv)
7354{
7355 struct nl80211_global *global = priv;
7356 if (global == NULL)
7357 return;
7358 if (!dl_list_empty(&global->interfaces)) {
7359 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
7360 "nl80211_global_deinit",
7361 dl_list_len(&global->interfaces));
7362 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007363
7364 if (global->netlink)
7365 netlink_deinit(global->netlink);
7366
7367 nl_destroy_handles(&global->nl);
7368
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007369 if (global->nl_event)
7370 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007371
7372 nl_cb_put(global->nl_cb);
7373
7374 if (global->ioctl_sock >= 0)
7375 close(global->ioctl_sock);
7376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007377 os_free(global);
7378}
7379
7380
7381static const char * nl80211_get_radio_name(void *priv)
7382{
7383 struct i802_bss *bss = priv;
7384 struct wpa_driver_nl80211_data *drv = bss->drv;
7385 return drv->phyname;
7386}
7387
7388
Jouni Malinen75ecf522011-06-27 15:19:46 -07007389static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
7390 const u8 *pmkid)
7391{
7392 struct nl_msg *msg;
7393
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007394 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
7395 (pmkid && nla_put(msg, NL80211_ATTR_PMKID, 16, pmkid)) ||
7396 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))) {
7397 nlmsg_free(msg);
7398 return -ENOBUFS;
7399 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07007400
7401 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007402}
7403
7404
7405static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
7406{
7407 struct i802_bss *bss = priv;
7408 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
7409 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
7410}
7411
7412
7413static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
7414{
7415 struct i802_bss *bss = priv;
7416 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
7417 MAC2STR(bssid));
7418 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
7419}
7420
7421
7422static int nl80211_flush_pmkid(void *priv)
7423{
7424 struct i802_bss *bss = priv;
7425 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
7426 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
7427}
7428
7429
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007430static void clean_survey_results(struct survey_results *survey_results)
7431{
7432 struct freq_survey *survey, *tmp;
7433
7434 if (dl_list_empty(&survey_results->survey_list))
7435 return;
7436
7437 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
7438 struct freq_survey, list) {
7439 dl_list_del(&survey->list);
7440 os_free(survey);
7441 }
7442}
7443
7444
7445static void add_survey(struct nlattr **sinfo, u32 ifidx,
7446 struct dl_list *survey_list)
7447{
7448 struct freq_survey *survey;
7449
7450 survey = os_zalloc(sizeof(struct freq_survey));
7451 if (!survey)
7452 return;
7453
7454 survey->ifidx = ifidx;
7455 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7456 survey->filled = 0;
7457
7458 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
7459 survey->nf = (int8_t)
7460 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
7461 survey->filled |= SURVEY_HAS_NF;
7462 }
7463
7464 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
7465 survey->channel_time =
7466 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
7467 survey->filled |= SURVEY_HAS_CHAN_TIME;
7468 }
7469
7470 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
7471 survey->channel_time_busy =
7472 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
7473 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
7474 }
7475
7476 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
7477 survey->channel_time_rx =
7478 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
7479 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
7480 }
7481
7482 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
7483 survey->channel_time_tx =
7484 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
7485 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
7486 }
7487
7488 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)",
7489 survey->freq,
7490 survey->nf,
7491 (unsigned long int) survey->channel_time,
7492 (unsigned long int) survey->channel_time_busy,
7493 (unsigned long int) survey->channel_time_tx,
7494 (unsigned long int) survey->channel_time_rx,
7495 survey->filled);
7496
7497 dl_list_add_tail(survey_list, &survey->list);
7498}
7499
7500
7501static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
7502 unsigned int freq_filter)
7503{
7504 if (!freq_filter)
7505 return 1;
7506
7507 return freq_filter == surveyed_freq;
7508}
7509
7510
7511static int survey_handler(struct nl_msg *msg, void *arg)
7512{
7513 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7514 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7515 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
7516 struct survey_results *survey_results;
7517 u32 surveyed_freq = 0;
7518 u32 ifidx;
7519
7520 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
7521 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
7522 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
7523 };
7524
7525 survey_results = (struct survey_results *) arg;
7526
7527 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7528 genlmsg_attrlen(gnlh, 0), NULL);
7529
Dmitry Shmidt97672262014-02-03 13:02:54 -08007530 if (!tb[NL80211_ATTR_IFINDEX])
7531 return NL_SKIP;
7532
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007533 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
7534
7535 if (!tb[NL80211_ATTR_SURVEY_INFO])
7536 return NL_SKIP;
7537
7538 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
7539 tb[NL80211_ATTR_SURVEY_INFO],
7540 survey_policy))
7541 return NL_SKIP;
7542
7543 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
7544 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
7545 return NL_SKIP;
7546 }
7547
7548 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7549
7550 if (!check_survey_ok(sinfo, surveyed_freq,
7551 survey_results->freq_filter))
7552 return NL_SKIP;
7553
7554 if (survey_results->freq_filter &&
7555 survey_results->freq_filter != surveyed_freq) {
7556 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
7557 surveyed_freq);
7558 return NL_SKIP;
7559 }
7560
7561 add_survey(sinfo, ifidx, &survey_results->survey_list);
7562
7563 return NL_SKIP;
7564}
7565
7566
7567static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
7568{
7569 struct i802_bss *bss = priv;
7570 struct wpa_driver_nl80211_data *drv = bss->drv;
7571 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007572 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007573 union wpa_event_data data;
7574 struct survey_results *survey_results;
7575
7576 os_memset(&data, 0, sizeof(data));
7577 survey_results = &data.survey_results;
7578
7579 dl_list_init(&survey_results->survey_list);
7580
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007581 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007582 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007583 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007584
7585 if (freq)
7586 data.survey_results.freq_filter = freq;
7587
7588 do {
7589 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
7590 err = send_and_recv_msgs(drv, msg, survey_handler,
7591 survey_results);
7592 } while (err > 0);
7593
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007594 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007595 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007596 else
7597 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007598
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007599 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007600 return err;
7601}
7602
7603
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007604static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
7605 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007606 const u8 *replay_ctr)
7607{
7608 struct i802_bss *bss = priv;
7609 struct wpa_driver_nl80211_data *drv = bss->drv;
7610 struct nlattr *replay_nested;
7611 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007612 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007613
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007614 if (!drv->set_rekey_offload)
7615 return;
7616
7617 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007618 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
7619 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007620 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
7621 nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007622 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
7623 replay_ctr)) {
7624 nl80211_nlmsg_clear(msg);
7625 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007626 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007627 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007628
7629 nla_nest_end(msg, replay_nested);
7630
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007631 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
7632 if (ret == -EOPNOTSUPP) {
7633 wpa_printf(MSG_DEBUG,
7634 "nl80211: Driver does not support rekey offload");
7635 drv->set_rekey_offload = 0;
7636 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007637}
7638
7639
7640static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
7641 const u8 *addr, int qos)
7642{
7643 /* send data frame to poll STA and check whether
7644 * this frame is ACKed */
7645 struct {
7646 struct ieee80211_hdr hdr;
7647 u16 qos_ctl;
7648 } STRUCT_PACKED nulldata;
7649 size_t size;
7650
7651 /* Send data frame to poll STA and check whether this frame is ACKed */
7652
7653 os_memset(&nulldata, 0, sizeof(nulldata));
7654
7655 if (qos) {
7656 nulldata.hdr.frame_control =
7657 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7658 WLAN_FC_STYPE_QOS_NULL);
7659 size = sizeof(nulldata);
7660 } else {
7661 nulldata.hdr.frame_control =
7662 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7663 WLAN_FC_STYPE_NULLFUNC);
7664 size = sizeof(struct ieee80211_hdr);
7665 }
7666
7667 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
7668 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
7669 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
7670 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
7671
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007672 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007673 0, 0, NULL, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007674 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
7675 "send poll frame");
7676}
7677
7678static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
7679 int qos)
7680{
7681 struct i802_bss *bss = priv;
7682 struct wpa_driver_nl80211_data *drv = bss->drv;
7683 struct nl_msg *msg;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007684 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007685
7686 if (!drv->poll_command_supported) {
7687 nl80211_send_null_frame(bss, own_addr, addr, qos);
7688 return;
7689 }
7690
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007691 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
7692 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7693 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007694 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007695 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007696
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007697 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7698 if (ret < 0) {
7699 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
7700 MACSTR " failed: ret=%d (%s)",
7701 MAC2STR(addr), ret, strerror(-ret));
7702 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007703}
7704
7705
7706static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
7707{
7708 struct nl_msg *msg;
7709
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007710 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
7711 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
7712 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
7713 nlmsg_free(msg);
7714 return -ENOBUFS;
7715 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007716 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007717}
7718
7719
7720static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
7721 int ctwindow)
7722{
7723 struct i802_bss *bss = priv;
7724
7725 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
7726 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
7727
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007728 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007729#ifdef ANDROID_P2P
7730 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007731#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007732 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007733#endif /* ANDROID_P2P */
7734 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007735
7736 if (legacy_ps == -1)
7737 return 0;
7738 if (legacy_ps != 0 && legacy_ps != 1)
7739 return -1; /* Not yet supported */
7740
7741 return nl80211_set_power_save(bss, legacy_ps);
7742}
7743
7744
Dmitry Shmidt051af732013-10-22 13:52:46 -07007745static int nl80211_start_radar_detection(void *priv,
7746 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007747{
7748 struct i802_bss *bss = priv;
7749 struct wpa_driver_nl80211_data *drv = bss->drv;
7750 struct nl_msg *msg;
7751 int ret;
7752
Dmitry Shmidt051af732013-10-22 13:52:46 -07007753 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)",
7754 freq->freq, freq->ht_enabled, freq->vht_enabled,
7755 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7756
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007757 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
7758 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
7759 "detection");
7760 return -1;
7761 }
7762
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007763 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
7764 nl80211_put_freq_params(msg, freq) < 0) {
7765 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007766 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007767 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007768
7769 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7770 if (ret == 0)
7771 return 0;
7772 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
7773 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007774 return -1;
7775}
7776
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007777#ifdef CONFIG_TDLS
7778
7779static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
7780 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007781 u32 peer_capab, int initiator, const u8 *buf,
7782 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007783{
7784 struct i802_bss *bss = priv;
7785 struct wpa_driver_nl80211_data *drv = bss->drv;
7786 struct nl_msg *msg;
7787
7788 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7789 return -EOPNOTSUPP;
7790
7791 if (!dst)
7792 return -EINVAL;
7793
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007794 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
7795 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7796 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
7797 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
7798 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
7799 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007800 if (peer_capab) {
7801 /*
7802 * The internal enum tdls_peer_capability definition is
7803 * currently identical with the nl80211 enum
7804 * nl80211_tdls_peer_capability, so no conversion is needed
7805 * here.
7806 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007807 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
7808 peer_capab))
7809 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007810 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007811 if ((initiator &&
7812 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
7813 nla_put(msg, NL80211_ATTR_IE, len, buf))
7814 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007815
7816 return send_and_recv_msgs(drv, msg, NULL, NULL);
7817
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007818fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007819 nlmsg_free(msg);
7820 return -ENOBUFS;
7821}
7822
7823
7824static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
7825{
7826 struct i802_bss *bss = priv;
7827 struct wpa_driver_nl80211_data *drv = bss->drv;
7828 struct nl_msg *msg;
7829 enum nl80211_tdls_operation nl80211_oper;
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08007830 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007831
7832 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7833 return -EOPNOTSUPP;
7834
7835 switch (oper) {
7836 case TDLS_DISCOVERY_REQ:
7837 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
7838 break;
7839 case TDLS_SETUP:
7840 nl80211_oper = NL80211_TDLS_SETUP;
7841 break;
7842 case TDLS_TEARDOWN:
7843 nl80211_oper = NL80211_TDLS_TEARDOWN;
7844 break;
7845 case TDLS_ENABLE_LINK:
7846 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
7847 break;
7848 case TDLS_DISABLE_LINK:
7849 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
7850 break;
7851 case TDLS_ENABLE:
7852 return 0;
7853 case TDLS_DISABLE:
7854 return 0;
7855 default:
7856 return -EINVAL;
7857 }
7858
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007859 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
7860 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
7861 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
7862 nlmsg_free(msg);
7863 return -ENOBUFS;
7864 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007865
Dmitry Shmidtbbcc4392017-01-30 12:38:13 -08007866 res = send_and_recv_msgs(drv, msg, NULL, NULL);
7867 wpa_printf(MSG_DEBUG, "nl80211: TDLS_OPER: oper=%d mac=" MACSTR
7868 " --> res=%d (%s)", nl80211_oper, MAC2STR(peer), res,
7869 strerror(-res));
7870 return res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007871}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007872
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007873
7874static int
7875nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
7876 const struct hostapd_freq_params *params)
7877{
7878 struct i802_bss *bss = priv;
7879 struct wpa_driver_nl80211_data *drv = bss->drv;
7880 struct nl_msg *msg;
7881 int ret = -ENOBUFS;
7882
7883 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7884 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7885 return -EOPNOTSUPP;
7886
7887 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
7888 " oper_class=%u freq=%u",
7889 MAC2STR(addr), oper_class, params->freq);
7890 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
7891 if (!msg ||
7892 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7893 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
7894 (ret = nl80211_put_freq_params(msg, params))) {
7895 nlmsg_free(msg);
7896 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
7897 return ret;
7898 }
7899
7900 return send_and_recv_msgs(drv, msg, NULL, NULL);
7901}
7902
7903
7904static int
7905nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
7906{
7907 struct i802_bss *bss = priv;
7908 struct wpa_driver_nl80211_data *drv = bss->drv;
7909 struct nl_msg *msg;
7910
7911 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7912 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7913 return -EOPNOTSUPP;
7914
7915 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
7916 MAC2STR(addr));
7917 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
7918 if (!msg ||
7919 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7920 nlmsg_free(msg);
7921 wpa_printf(MSG_DEBUG,
7922 "nl80211: Could not build TDLS cancel chan switch");
7923 return -ENOBUFS;
7924 }
7925
7926 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007927}
7928
7929#endif /* CONFIG TDLS */
7930
7931
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007932static int driver_nl80211_set_key(const char *ifname, void *priv,
7933 enum wpa_alg alg, const u8 *addr,
7934 int key_idx, int set_tx,
7935 const u8 *seq, size_t seq_len,
7936 const u8 *key, size_t key_len)
7937{
7938 struct i802_bss *bss = priv;
7939 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
7940 set_tx, seq, seq_len, key, key_len);
7941}
7942
7943
7944static int driver_nl80211_scan2(void *priv,
7945 struct wpa_driver_scan_params *params)
7946{
7947 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007948#ifdef CONFIG_DRIVER_NL80211_QCA
7949 struct wpa_driver_nl80211_data *drv = bss->drv;
7950
7951 /*
7952 * Do a vendor specific scan if possible. If only_new_results is
7953 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
7954 * cannot be achieved through a vendor scan. The below condition may
7955 * need to be modified if new scan flags are added in the future whose
7956 * functionality can only be achieved through a normal scan.
7957 */
7958 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
7959 return wpa_driver_nl80211_vendor_scan(bss, params);
7960#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007961 return wpa_driver_nl80211_scan(bss, params);
7962}
7963
7964
7965static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
7966 int reason_code)
7967{
7968 struct i802_bss *bss = priv;
7969 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
7970}
7971
7972
7973static int driver_nl80211_authenticate(void *priv,
7974 struct wpa_driver_auth_params *params)
7975{
7976 struct i802_bss *bss = priv;
7977 return wpa_driver_nl80211_authenticate(bss, params);
7978}
7979
7980
7981static void driver_nl80211_deinit(void *priv)
7982{
7983 struct i802_bss *bss = priv;
7984 wpa_driver_nl80211_deinit(bss);
7985}
7986
7987
7988static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
7989 const char *ifname)
7990{
7991 struct i802_bss *bss = priv;
7992 return wpa_driver_nl80211_if_remove(bss, type, ifname);
7993}
7994
7995
7996static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007997 size_t data_len, int noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007998 unsigned int freq,
7999 const u16 *csa_offs, size_t csa_offs_len)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008000{
8001 struct i802_bss *bss = priv;
8002 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008003 freq, 0, 0, 0, csa_offs,
8004 csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008005}
8006
8007
8008static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
8009{
8010 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008011 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008012}
8013
8014
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008015static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
8016 const char *ifname, int vlan_id)
8017{
8018 struct i802_bss *bss = priv;
8019 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
8020}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008021
8022
8023static int driver_nl80211_read_sta_data(void *priv,
8024 struct hostap_sta_driver_data *data,
8025 const u8 *addr)
8026{
8027 struct i802_bss *bss = priv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08008028
8029 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08008030 return i802_read_sta_data(bss, data, addr);
8031}
8032
8033
8034static int driver_nl80211_send_action(void *priv, unsigned int freq,
8035 unsigned int wait_time,
8036 const u8 *dst, const u8 *src,
8037 const u8 *bssid,
8038 const u8 *data, size_t data_len,
8039 int no_cck)
8040{
8041 struct i802_bss *bss = priv;
8042 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
8043 bssid, data, data_len, no_cck);
8044}
8045
8046
8047static int driver_nl80211_probe_req_report(void *priv, int report)
8048{
8049 struct i802_bss *bss = priv;
8050 return wpa_driver_nl80211_probe_req_report(bss, report);
8051}
8052
8053
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008054static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
8055 const u8 *ies, size_t ies_len)
8056{
8057 int ret;
8058 struct nl_msg *msg;
8059 struct i802_bss *bss = priv;
8060 struct wpa_driver_nl80211_data *drv = bss->drv;
8061 u16 mdid = WPA_GET_LE16(md);
8062
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008063 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008064 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
8065 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
8066 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
8067 nlmsg_free(msg);
8068 return -ENOBUFS;
8069 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008070
8071 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8072 if (ret) {
8073 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
8074 "err=%d (%s)", ret, strerror(-ret));
8075 }
8076
8077 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008078}
8079
8080
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07008081static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008082{
8083 struct i802_bss *bss = priv;
8084 struct wpa_driver_nl80211_data *drv = bss->drv;
8085
8086 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
8087 return NULL;
8088
8089 return bss->addr;
8090}
8091
8092
Dmitry Shmidt56052862013-10-04 10:23:25 -07008093static const char * scan_state_str(enum scan_states scan_state)
8094{
8095 switch (scan_state) {
8096 case NO_SCAN:
8097 return "NO_SCAN";
8098 case SCAN_REQUESTED:
8099 return "SCAN_REQUESTED";
8100 case SCAN_STARTED:
8101 return "SCAN_STARTED";
8102 case SCAN_COMPLETED:
8103 return "SCAN_COMPLETED";
8104 case SCAN_ABORTED:
8105 return "SCAN_ABORTED";
8106 case SCHED_SCAN_STARTED:
8107 return "SCHED_SCAN_STARTED";
8108 case SCHED_SCAN_STOPPED:
8109 return "SCHED_SCAN_STOPPED";
8110 case SCHED_SCAN_RESULTS:
8111 return "SCHED_SCAN_RESULTS";
8112 }
8113
8114 return "??";
8115}
8116
8117
8118static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
8119{
8120 struct i802_bss *bss = priv;
8121 struct wpa_driver_nl80211_data *drv = bss->drv;
8122 int res;
8123 char *pos, *end;
8124
8125 pos = buf;
8126 end = buf + buflen;
8127
8128 res = os_snprintf(pos, end - pos,
8129 "ifindex=%d\n"
8130 "ifname=%s\n"
8131 "brname=%s\n"
8132 "addr=" MACSTR "\n"
8133 "freq=%d\n"
8134 "%s%s%s%s%s",
8135 bss->ifindex,
8136 bss->ifname,
8137 bss->brname,
8138 MAC2STR(bss->addr),
8139 bss->freq,
8140 bss->beacon_set ? "beacon_set=1\n" : "",
8141 bss->added_if_into_bridge ?
8142 "added_if_into_bridge=1\n" : "",
8143 bss->added_bridge ? "added_bridge=1\n" : "",
8144 bss->in_deinit ? "in_deinit=1\n" : "",
8145 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008146 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008147 return pos - buf;
8148 pos += res;
8149
8150 if (bss->wdev_id_set) {
8151 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
8152 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008153 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008154 return pos - buf;
8155 pos += res;
8156 }
8157
8158 res = os_snprintf(pos, end - pos,
8159 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008160 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07008161 "drv_ifindex=%d\n"
8162 "operstate=%d\n"
8163 "scan_state=%s\n"
8164 "auth_bssid=" MACSTR "\n"
8165 "auth_attempt_bssid=" MACSTR "\n"
8166 "bssid=" MACSTR "\n"
8167 "prev_bssid=" MACSTR "\n"
8168 "associated=%d\n"
8169 "assoc_freq=%u\n"
8170 "monitor_sock=%d\n"
8171 "monitor_ifidx=%d\n"
8172 "monitor_refcount=%d\n"
8173 "last_mgmt_freq=%u\n"
8174 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008175 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008176 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008177 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -07008178 drv->ifindex,
8179 drv->operstate,
8180 scan_state_str(drv->scan_state),
8181 MAC2STR(drv->auth_bssid),
8182 MAC2STR(drv->auth_attempt_bssid),
8183 MAC2STR(drv->bssid),
8184 MAC2STR(drv->prev_bssid),
8185 drv->associated,
8186 drv->assoc_freq,
8187 drv->monitor_sock,
8188 drv->monitor_ifidx,
8189 drv->monitor_refcount,
8190 drv->last_mgmt_freq,
8191 drv->eapol_tx_sock,
8192 drv->ignore_if_down_event ?
8193 "ignore_if_down_event=1\n" : "",
8194 drv->scan_complete_events ?
8195 "scan_complete_events=1\n" : "",
8196 drv->disabled_11b_rates ?
8197 "disabled_11b_rates=1\n" : "",
8198 drv->pending_remain_on_chan ?
8199 "pending_remain_on_chan=1\n" : "",
8200 drv->in_interface_list ? "in_interface_list=1\n" : "",
8201 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
8202 drv->poll_command_supported ?
8203 "poll_command_supported=1\n" : "",
8204 drv->data_tx_status ? "data_tx_status=1\n" : "",
8205 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
8206 drv->retry_auth ? "retry_auth=1\n" : "",
8207 drv->use_monitor ? "use_monitor=1\n" : "",
8208 drv->ignore_next_local_disconnect ?
8209 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07008210 drv->ignore_next_local_deauth ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008211 "ignore_next_local_deauth=1\n" : "");
8212 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008213 return pos - buf;
8214 pos += res;
8215
8216 if (drv->has_capability) {
8217 res = os_snprintf(pos, end - pos,
8218 "capa.key_mgmt=0x%x\n"
8219 "capa.enc=0x%x\n"
8220 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008221 "capa.flags=0x%llx\n"
8222 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07008223 "capa.max_scan_ssids=%d\n"
8224 "capa.max_sched_scan_ssids=%d\n"
8225 "capa.sched_scan_supported=%d\n"
8226 "capa.max_match_sets=%d\n"
8227 "capa.max_remain_on_chan=%u\n"
8228 "capa.max_stations=%u\n"
8229 "capa.probe_resp_offloads=0x%x\n"
8230 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008231 "capa.num_multichan_concurrent=%u\n"
8232 "capa.mac_addr_rand_sched_scan_supported=%d\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008233 "capa.mac_addr_rand_scan_supported=%d\n"
8234 "capa.conc_capab=%u\n"
8235 "capa.max_conc_chan_2_4=%u\n"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008236 "capa.max_conc_chan_5_0=%u\n"
8237 "capa.max_sched_scan_plans=%u\n"
8238 "capa.max_sched_scan_plan_interval=%u\n"
8239 "capa.max_sched_scan_plan_iterations=%u\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008240 drv->capa.key_mgmt,
8241 drv->capa.enc,
8242 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008243 (unsigned long long) drv->capa.flags,
8244 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008245 drv->capa.max_scan_ssids,
8246 drv->capa.max_sched_scan_ssids,
8247 drv->capa.sched_scan_supported,
8248 drv->capa.max_match_sets,
8249 drv->capa.max_remain_on_chan,
8250 drv->capa.max_stations,
8251 drv->capa.probe_resp_offloads,
8252 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008253 drv->capa.num_multichan_concurrent,
8254 drv->capa.mac_addr_rand_sched_scan_supported,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008255 drv->capa.mac_addr_rand_scan_supported,
8256 drv->capa.conc_capab,
8257 drv->capa.max_conc_chan_2_4,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008258 drv->capa.max_conc_chan_5_0,
8259 drv->capa.max_sched_scan_plans,
8260 drv->capa.max_sched_scan_plan_interval,
8261 drv->capa.max_sched_scan_plan_iterations);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008262 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008263 return pos - buf;
8264 pos += res;
8265 }
8266
8267 return pos - buf;
8268}
8269
8270
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008271static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
8272{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008273 if ((settings->head &&
8274 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
8275 settings->head_len, settings->head)) ||
8276 (settings->tail &&
8277 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
8278 settings->tail_len, settings->tail)) ||
8279 (settings->beacon_ies &&
8280 nla_put(msg, NL80211_ATTR_IE,
8281 settings->beacon_ies_len, settings->beacon_ies)) ||
8282 (settings->proberesp_ies &&
8283 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
8284 settings->proberesp_ies_len, settings->proberesp_ies)) ||
8285 (settings->assocresp_ies &&
8286 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
8287 settings->assocresp_ies_len, settings->assocresp_ies)) ||
8288 (settings->probe_resp &&
8289 nla_put(msg, NL80211_ATTR_PROBE_RESP,
8290 settings->probe_resp_len, settings->probe_resp)))
8291 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008292
8293 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008294}
8295
8296
8297static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
8298{
8299 struct nl_msg *msg;
8300 struct i802_bss *bss = priv;
8301 struct wpa_driver_nl80211_data *drv = bss->drv;
8302 struct nlattr *beacon_csa;
8303 int ret = -ENOBUFS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008304 int csa_off_len = 0;
8305 int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008306
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008307 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 -08008308 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008309 settings->freq_params.freq, settings->freq_params.bandwidth,
8310 settings->freq_params.center_freq1,
8311 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008312
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008313 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008314 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
8315 return -EOPNOTSUPP;
8316 }
8317
8318 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
8319 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
8320 return -EOPNOTSUPP;
8321
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008322 /*
8323 * Remove empty counters, assuming Probe Response and Beacon frame
8324 * counters match. This implementation assumes that there are only two
8325 * counters.
8326 */
8327 if (settings->counter_offset_beacon[0] &&
8328 !settings->counter_offset_beacon[1]) {
8329 csa_off_len = 1;
8330 } else if (settings->counter_offset_beacon[1] &&
8331 !settings->counter_offset_beacon[0]) {
8332 csa_off_len = 1;
8333 settings->counter_offset_beacon[0] =
8334 settings->counter_offset_beacon[1];
8335 settings->counter_offset_presp[0] =
8336 settings->counter_offset_presp[1];
8337 } else if (settings->counter_offset_beacon[1] &&
8338 settings->counter_offset_beacon[0]) {
8339 csa_off_len = 2;
8340 } else {
8341 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
8342 return -EINVAL;
8343 }
8344
8345 /* Check CSA counters validity */
8346 if (drv->capa.max_csa_counters &&
8347 csa_off_len > drv->capa.max_csa_counters) {
8348 wpa_printf(MSG_ERROR,
8349 "nl80211: Too many CSA counters provided");
8350 return -EINVAL;
8351 }
8352
8353 if (!settings->beacon_csa.tail)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008354 return -EINVAL;
8355
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008356 for (i = 0; i < csa_off_len; i++) {
8357 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
8358 u16 csa_c_off_presp = settings->counter_offset_presp[i];
8359
8360 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
8361 (settings->beacon_csa.tail[csa_c_off_bcn] !=
8362 settings->cs_count))
8363 return -EINVAL;
8364
8365 if (settings->beacon_csa.probe_resp &&
8366 ((settings->beacon_csa.probe_resp_len <=
8367 csa_c_off_presp) ||
8368 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
8369 settings->cs_count)))
8370 return -EINVAL;
8371 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008372
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008373 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
8374 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
8375 settings->cs_count) ||
8376 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
8377 (settings->block_tx &&
8378 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008379 goto error;
8380
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008381 /* beacon_after params */
8382 ret = set_beacon_data(msg, &settings->beacon_after);
8383 if (ret)
8384 goto error;
8385
8386 /* beacon_csa params */
8387 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
8388 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008389 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008390
8391 ret = set_beacon_data(msg, &settings->beacon_csa);
8392 if (ret)
8393 goto error;
8394
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008395 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
8396 csa_off_len * sizeof(u16),
8397 settings->counter_offset_beacon) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008398 (settings->beacon_csa.probe_resp &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008399 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
8400 csa_off_len * sizeof(u16),
8401 settings->counter_offset_presp)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008402 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008403
8404 nla_nest_end(msg, beacon_csa);
8405 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8406 if (ret) {
8407 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
8408 ret, strerror(-ret));
8409 }
8410 return ret;
8411
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008412fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008413 ret = -ENOBUFS;
8414error:
8415 nlmsg_free(msg);
8416 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
8417 return ret;
8418}
8419
8420
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008421static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
8422 u8 user_priority, u16 admitted_time)
8423{
8424 struct i802_bss *bss = priv;
8425 struct wpa_driver_nl80211_data *drv = bss->drv;
8426 struct nl_msg *msg;
8427 int ret;
8428
8429 wpa_printf(MSG_DEBUG,
8430 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
8431 tsid, admitted_time, user_priority);
8432
8433 if (!is_sta_interface(drv->nlmode))
8434 return -ENOTSUP;
8435
8436 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
8437 if (!msg ||
8438 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8439 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8440 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
8441 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
8442 nlmsg_free(msg);
8443 return -ENOBUFS;
8444 }
8445
8446 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8447 if (ret)
8448 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
8449 ret, strerror(-ret));
8450 return ret;
8451}
8452
8453
8454static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
8455{
8456 struct i802_bss *bss = priv;
8457 struct wpa_driver_nl80211_data *drv = bss->drv;
8458 struct nl_msg *msg;
8459 int ret;
8460
8461 wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
8462
8463 if (!is_sta_interface(drv->nlmode))
8464 return -ENOTSUP;
8465
8466 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
8467 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8468 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8469 nlmsg_free(msg);
8470 return -ENOBUFS;
8471 }
8472
8473 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8474 if (ret)
8475 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
8476 ret, strerror(-ret));
8477 return ret;
8478}
8479
8480
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008481#ifdef CONFIG_TESTING_OPTIONS
8482static int cmd_reply_handler(struct nl_msg *msg, void *arg)
8483{
8484 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8485 struct wpabuf *buf = arg;
8486
8487 if (!buf)
8488 return NL_SKIP;
8489
8490 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
8491 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
8492 return NL_SKIP;
8493 }
8494
8495 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
8496 genlmsg_attrlen(gnlh, 0));
8497
8498 return NL_SKIP;
8499}
8500#endif /* CONFIG_TESTING_OPTIONS */
8501
8502
8503static int vendor_reply_handler(struct nl_msg *msg, void *arg)
8504{
8505 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8506 struct nlattr *nl_vendor_reply, *nl;
8507 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8508 struct wpabuf *buf = arg;
8509 int rem;
8510
8511 if (!buf)
8512 return NL_SKIP;
8513
8514 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8515 genlmsg_attrlen(gnlh, 0), NULL);
8516 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
8517
8518 if (!nl_vendor_reply)
8519 return NL_SKIP;
8520
8521 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
8522 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
8523 return NL_SKIP;
8524 }
8525
8526 nla_for_each_nested(nl, nl_vendor_reply, rem) {
8527 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
8528 }
8529
8530 return NL_SKIP;
8531}
8532
8533
8534static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
8535 unsigned int subcmd, const u8 *data,
8536 size_t data_len, struct wpabuf *buf)
8537{
8538 struct i802_bss *bss = priv;
8539 struct wpa_driver_nl80211_data *drv = bss->drv;
8540 struct nl_msg *msg;
8541 int ret;
8542
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008543#ifdef CONFIG_TESTING_OPTIONS
8544 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008545 msg = nlmsg_alloc();
8546 if (!msg)
8547 return -ENOMEM;
8548
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008549 nl80211_cmd(drv, msg, 0, subcmd);
8550 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
8551 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008552 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008553 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
8554 if (ret)
8555 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
8556 ret);
8557 return ret;
8558 }
8559#endif /* CONFIG_TESTING_OPTIONS */
8560
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008561 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
8562 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
8563 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
8564 (data &&
8565 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
8566 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008567
8568 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
8569 if (ret)
8570 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
8571 ret);
8572 return ret;
8573
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008574fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008575 nlmsg_free(msg);
8576 return -ENOBUFS;
8577}
8578
8579
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008580static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
8581 u8 qos_map_set_len)
8582{
8583 struct i802_bss *bss = priv;
8584 struct wpa_driver_nl80211_data *drv = bss->drv;
8585 struct nl_msg *msg;
8586 int ret;
8587
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008588 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
8589 qos_map_set, qos_map_set_len);
8590
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008591 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
8592 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
8593 nlmsg_free(msg);
8594 return -ENOBUFS;
8595 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008596
8597 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8598 if (ret)
8599 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
8600
8601 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008602}
8603
8604
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008605static int nl80211_set_wowlan(void *priv,
8606 const struct wowlan_triggers *triggers)
8607{
8608 struct i802_bss *bss = priv;
8609 struct wpa_driver_nl80211_data *drv = bss->drv;
8610 struct nl_msg *msg;
8611 struct nlattr *wowlan_triggers;
8612 int ret;
8613
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008614 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
8615
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008616 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008617 !(wowlan_triggers = nla_nest_start(msg,
8618 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
8619 (triggers->any &&
8620 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
8621 (triggers->disconnect &&
8622 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
8623 (triggers->magic_pkt &&
8624 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
8625 (triggers->gtk_rekey_failure &&
8626 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
8627 (triggers->eap_identity_req &&
8628 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
8629 (triggers->four_way_handshake &&
8630 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
8631 (triggers->rfkill_release &&
8632 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
8633 nlmsg_free(msg);
8634 return -ENOBUFS;
8635 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008636
8637 nla_nest_end(msg, wowlan_triggers);
8638
8639 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8640 if (ret)
8641 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
8642
8643 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008644}
8645
8646
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008647#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008648static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
8649{
8650 struct i802_bss *bss = priv;
8651 struct wpa_driver_nl80211_data *drv = bss->drv;
8652 struct nl_msg *msg;
8653 struct nlattr *params;
8654
8655 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
8656
8657 if (!drv->roaming_vendor_cmd_avail) {
8658 wpa_printf(MSG_DEBUG,
8659 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
8660 return -1;
8661 }
8662
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008663 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8664 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8665 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8666 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
8667 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8668 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
8669 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
8670 QCA_ROAMING_NOT_ALLOWED) ||
8671 (bssid &&
8672 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
8673 nlmsg_free(msg);
8674 return -1;
8675 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008676 nla_nest_end(msg, params);
8677
8678 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008679}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008680#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008681
8682
8683static int nl80211_set_mac_addr(void *priv, const u8 *addr)
8684{
8685 struct i802_bss *bss = priv;
8686 struct wpa_driver_nl80211_data *drv = bss->drv;
8687 int new_addr = addr != NULL;
8688
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008689 if (TEST_FAIL())
8690 return -1;
8691
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008692 if (!addr)
8693 addr = drv->perm_addr;
8694
8695 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
8696 return -1;
8697
8698 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
8699 {
8700 wpa_printf(MSG_DEBUG,
8701 "nl80211: failed to set_mac_addr for %s to " MACSTR,
8702 bss->ifname, MAC2STR(addr));
8703 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
8704 1) < 0) {
8705 wpa_printf(MSG_DEBUG,
8706 "nl80211: Could not restore interface UP after failed set_mac_addr");
8707 }
8708 return -1;
8709 }
8710
8711 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
8712 bss->ifname, MAC2STR(addr));
8713 drv->addr_changed = new_addr;
8714 os_memcpy(bss->addr, addr, ETH_ALEN);
8715
8716 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
8717 {
8718 wpa_printf(MSG_DEBUG,
8719 "nl80211: Could not restore interface UP after set_mac_addr");
8720 }
8721
8722 return 0;
8723}
8724
8725
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008726#ifdef CONFIG_MESH
8727
8728static int wpa_driver_nl80211_init_mesh(void *priv)
8729{
8730 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
8731 wpa_printf(MSG_INFO,
8732 "nl80211: Failed to set interface into mesh mode");
8733 return -1;
8734 }
8735 return 0;
8736}
8737
8738
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008739static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
8740 size_t mesh_id_len)
8741{
8742 if (mesh_id) {
8743 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
8744 mesh_id, mesh_id_len);
8745 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
8746 }
8747
8748 return 0;
8749}
8750
8751
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07008752static int nl80211_put_mesh_config(struct nl_msg *msg,
8753 struct wpa_driver_mesh_bss_params *params)
8754{
8755 struct nlattr *container;
8756
8757 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
8758 if (!container)
8759 return -1;
8760
8761 if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
8762 nla_put_u32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8763 params->auto_plinks)) ||
8764 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
8765 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
8766 params->max_peer_links)))
8767 return -1;
8768
8769 /*
8770 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
8771 * the timer could disconnect stations even in that case.
8772 */
8773 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
8774 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
8775 params->peer_link_timeout)) {
8776 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
8777 return -1;
8778 }
8779
8780 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
8781 nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
8782 wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
8783 return -1;
8784 }
8785
8786 nla_nest_end(msg, container);
8787
8788 return 0;
8789}
8790
8791
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008792static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008793 struct wpa_driver_mesh_join_params *params)
8794{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008795 struct wpa_driver_nl80211_data *drv = bss->drv;
8796 struct nl_msg *msg;
8797 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008798 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008799
8800 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
8801 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008802 if (!msg ||
8803 nl80211_put_freq_params(msg, &params->freq) ||
8804 nl80211_put_basic_rates(msg, params->basic_rates) ||
8805 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07008806 nl80211_put_beacon_int(msg, params->beacon_int) ||
8807 nl80211_put_dtim_period(msg, params->dtim_period))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008808 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008809
8810 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
8811
8812 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
8813 if (!container)
8814 goto fail;
8815
8816 if (params->ies) {
8817 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
8818 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
8819 params->ies))
8820 goto fail;
8821 }
8822 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
8823 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
8824 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
8825 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
8826 goto fail;
8827 }
8828 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
8829 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
8830 goto fail;
8831 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
8832 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
8833 goto fail;
8834 nla_nest_end(msg, container);
8835
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07008836 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
8837 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
8838 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
8839 if (nl80211_put_mesh_config(msg, &params->conf) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008840 goto fail;
8841
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008842 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8843 msg = NULL;
8844 if (ret) {
8845 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
8846 ret, strerror(-ret));
8847 goto fail;
8848 }
8849 ret = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07008850 drv->assoc_freq = bss->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008851 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
8852
8853fail:
8854 nlmsg_free(msg);
8855 return ret;
8856}
8857
8858
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008859static int
8860wpa_driver_nl80211_join_mesh(void *priv,
8861 struct wpa_driver_mesh_join_params *params)
8862{
8863 struct i802_bss *bss = priv;
8864 int ret, timeout;
8865
8866 timeout = params->conf.peer_link_timeout;
8867
8868 /* Disable kernel inactivity timer */
8869 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
8870 params->conf.peer_link_timeout = 0;
8871
8872 ret = nl80211_join_mesh(bss, params);
8873 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
8874 wpa_printf(MSG_DEBUG,
8875 "nl80211: Mesh join retry for peer_link_timeout");
8876 /*
8877 * Old kernel does not support setting
8878 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
8879 * into future from peer_link_timeout.
8880 */
8881 params->conf.peer_link_timeout = timeout + 60;
8882 ret = nl80211_join_mesh(priv, params);
8883 }
8884
8885 params->conf.peer_link_timeout = timeout;
8886 return ret;
8887}
8888
8889
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008890static int wpa_driver_nl80211_leave_mesh(void *priv)
8891{
8892 struct i802_bss *bss = priv;
8893 struct wpa_driver_nl80211_data *drv = bss->drv;
8894 struct nl_msg *msg;
8895 int ret;
8896
8897 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
8898 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
8899 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8900 if (ret) {
8901 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
8902 ret, strerror(-ret));
8903 } else {
8904 wpa_printf(MSG_DEBUG,
8905 "nl80211: mesh leave request send successfully");
8906 }
8907
8908 if (wpa_driver_nl80211_set_mode(drv->first_bss,
8909 NL80211_IFTYPE_STATION)) {
8910 wpa_printf(MSG_INFO,
8911 "nl80211: Failed to set interface into station mode");
8912 }
8913 return ret;
8914}
8915
8916#endif /* CONFIG_MESH */
8917
8918
8919static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
8920 const u8 *ipaddr, int prefixlen,
8921 const u8 *addr)
8922{
8923#ifdef CONFIG_LIBNL3_ROUTE
8924 struct i802_bss *bss = priv;
8925 struct wpa_driver_nl80211_data *drv = bss->drv;
8926 struct rtnl_neigh *rn;
8927 struct nl_addr *nl_ipaddr = NULL;
8928 struct nl_addr *nl_lladdr = NULL;
8929 int family, addrsize;
8930 int res;
8931
8932 if (!ipaddr || prefixlen == 0 || !addr)
8933 return -EINVAL;
8934
8935 if (bss->br_ifindex == 0) {
8936 wpa_printf(MSG_DEBUG,
8937 "nl80211: bridge must be set before adding an ip neigh to it");
8938 return -1;
8939 }
8940
8941 if (!drv->rtnl_sk) {
8942 wpa_printf(MSG_DEBUG,
8943 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8944 return -1;
8945 }
8946
8947 if (version == 4) {
8948 family = AF_INET;
8949 addrsize = 4;
8950 } else if (version == 6) {
8951 family = AF_INET6;
8952 addrsize = 16;
8953 } else {
8954 return -EINVAL;
8955 }
8956
8957 rn = rtnl_neigh_alloc();
8958 if (rn == NULL)
8959 return -ENOMEM;
8960
8961 /* set the destination ip address for neigh */
8962 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8963 if (nl_ipaddr == NULL) {
8964 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8965 res = -ENOMEM;
8966 goto errout;
8967 }
8968 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
8969 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8970 if (res) {
8971 wpa_printf(MSG_DEBUG,
8972 "nl80211: neigh set destination addr failed");
8973 goto errout;
8974 }
8975
8976 /* set the corresponding lladdr for neigh */
8977 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
8978 if (nl_lladdr == NULL) {
8979 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
8980 res = -ENOMEM;
8981 goto errout;
8982 }
8983 rtnl_neigh_set_lladdr(rn, nl_lladdr);
8984
8985 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8986 rtnl_neigh_set_state(rn, NUD_PERMANENT);
8987
8988 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
8989 if (res) {
8990 wpa_printf(MSG_DEBUG,
8991 "nl80211: Adding bridge ip neigh failed: %s",
8992 strerror(errno));
8993 }
8994errout:
8995 if (nl_lladdr)
8996 nl_addr_put(nl_lladdr);
8997 if (nl_ipaddr)
8998 nl_addr_put(nl_ipaddr);
8999 if (rn)
9000 rtnl_neigh_put(rn);
9001 return res;
9002#else /* CONFIG_LIBNL3_ROUTE */
9003 return -1;
9004#endif /* CONFIG_LIBNL3_ROUTE */
9005}
9006
9007
9008static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
9009 const u8 *ipaddr)
9010{
9011#ifdef CONFIG_LIBNL3_ROUTE
9012 struct i802_bss *bss = priv;
9013 struct wpa_driver_nl80211_data *drv = bss->drv;
9014 struct rtnl_neigh *rn;
9015 struct nl_addr *nl_ipaddr;
9016 int family, addrsize;
9017 int res;
9018
9019 if (!ipaddr)
9020 return -EINVAL;
9021
9022 if (version == 4) {
9023 family = AF_INET;
9024 addrsize = 4;
9025 } else if (version == 6) {
9026 family = AF_INET6;
9027 addrsize = 16;
9028 } else {
9029 return -EINVAL;
9030 }
9031
9032 if (bss->br_ifindex == 0) {
9033 wpa_printf(MSG_DEBUG,
9034 "nl80211: bridge must be set to delete an ip neigh");
9035 return -1;
9036 }
9037
9038 if (!drv->rtnl_sk) {
9039 wpa_printf(MSG_DEBUG,
9040 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9041 return -1;
9042 }
9043
9044 rn = rtnl_neigh_alloc();
9045 if (rn == NULL)
9046 return -ENOMEM;
9047
9048 /* set the destination ip address for neigh */
9049 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
9050 if (nl_ipaddr == NULL) {
9051 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9052 res = -ENOMEM;
9053 goto errout;
9054 }
9055 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9056 if (res) {
9057 wpa_printf(MSG_DEBUG,
9058 "nl80211: neigh set destination addr failed");
9059 goto errout;
9060 }
9061
9062 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9063
9064 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
9065 if (res) {
9066 wpa_printf(MSG_DEBUG,
9067 "nl80211: Deleting bridge ip neigh failed: %s",
9068 strerror(errno));
9069 }
9070errout:
9071 if (nl_ipaddr)
9072 nl_addr_put(nl_ipaddr);
9073 if (rn)
9074 rtnl_neigh_put(rn);
9075 return res;
9076#else /* CONFIG_LIBNL3_ROUTE */
9077 return -1;
9078#endif /* CONFIG_LIBNL3_ROUTE */
9079}
9080
9081
9082static int linux_write_system_file(const char *path, unsigned int val)
9083{
9084 char buf[50];
9085 int fd, len;
9086
9087 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
9088 if (os_snprintf_error(sizeof(buf), len))
9089 return -1;
9090
9091 fd = open(path, O_WRONLY);
9092 if (fd < 0)
9093 return -1;
9094
9095 if (write(fd, buf, len) < 0) {
9096 wpa_printf(MSG_DEBUG,
9097 "nl80211: Failed to write Linux system file: %s with the value of %d",
9098 path, val);
9099 close(fd);
9100 return -1;
9101 }
9102 close(fd);
9103
9104 return 0;
9105}
9106
9107
9108static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
9109{
9110 switch (attr) {
9111 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07009112 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009113 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
9114 return "hairpin_mode";
9115 }
9116
9117 return NULL;
9118}
9119
9120
9121static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
9122 unsigned int val)
9123{
9124 struct i802_bss *bss = priv;
9125 char path[128];
9126 const char *attr_txt;
9127
9128 attr_txt = drv_br_port_attr_str(attr);
9129 if (attr_txt == NULL)
9130 return -EINVAL;
9131
9132 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
9133 bss->ifname, attr_txt);
9134
9135 if (linux_write_system_file(path, val))
9136 return -1;
9137
9138 return 0;
9139}
9140
9141
9142static const char * drv_br_net_param_str(enum drv_br_net_param param)
9143{
9144 switch (param) {
9145 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9146 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -07009147 default:
9148 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009149 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009150}
9151
9152
9153static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
9154 unsigned int val)
9155{
9156 struct i802_bss *bss = priv;
9157 char path[128];
9158 const char *param_txt;
9159 int ip_version = 4;
9160
Dmitry Shmidt83474442015-04-15 13:47:09 -07009161 if (param == DRV_BR_MULTICAST_SNOOPING) {
9162 os_snprintf(path, sizeof(path),
9163 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
9164 bss->brname);
9165 goto set_val;
9166 }
9167
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009168 param_txt = drv_br_net_param_str(param);
9169 if (param_txt == NULL)
9170 return -EINVAL;
9171
9172 switch (param) {
9173 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9174 ip_version = 4;
9175 break;
9176 default:
9177 return -EINVAL;
9178 }
9179
9180 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
9181 ip_version, bss->brname, param_txt);
9182
Dmitry Shmidt83474442015-04-15 13:47:09 -07009183set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009184 if (linux_write_system_file(path, val))
9185 return -1;
9186
9187 return 0;
9188}
9189
9190
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009191#ifdef CONFIG_DRIVER_NL80211_QCA
9192
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009193static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
9194{
9195 switch (hw_mode) {
9196 case HOSTAPD_MODE_IEEE80211B:
9197 return QCA_ACS_MODE_IEEE80211B;
9198 case HOSTAPD_MODE_IEEE80211G:
9199 return QCA_ACS_MODE_IEEE80211G;
9200 case HOSTAPD_MODE_IEEE80211A:
9201 return QCA_ACS_MODE_IEEE80211A;
9202 case HOSTAPD_MODE_IEEE80211AD:
9203 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07009204 case HOSTAPD_MODE_IEEE80211ANY:
9205 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009206 default:
9207 return -1;
9208 }
9209}
9210
9211
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009212static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
9213{
9214 int i, len, ret;
9215 u32 *freqs;
9216
9217 if (!freq_list)
9218 return 0;
9219 len = int_array_len(freq_list);
9220 freqs = os_malloc(sizeof(u32) * len);
9221 if (!freqs)
9222 return -1;
9223 for (i = 0; i < len; i++)
9224 freqs[i] = freq_list[i];
9225 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
9226 sizeof(u32) * len, freqs);
9227 os_free(freqs);
9228 return ret;
9229}
9230
9231
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009232static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
9233{
9234 struct i802_bss *bss = priv;
9235 struct wpa_driver_nl80211_data *drv = bss->drv;
9236 struct nl_msg *msg;
9237 struct nlattr *data;
9238 int ret;
9239 int mode;
9240
9241 mode = hw_mode_to_qca_acs(params->hw_mode);
9242 if (mode < 0)
9243 return -1;
9244
9245 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9246 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9247 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9248 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
9249 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9250 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
9251 (params->ht_enabled &&
9252 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
9253 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07009254 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
9255 (params->vht_enabled &&
9256 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
9257 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
9258 params->ch_width) ||
9259 (params->ch_list_len &&
9260 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009261 params->ch_list)) ||
9262 add_acs_freq_list(msg, params->freq_list)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009263 nlmsg_free(msg);
9264 return -ENOBUFS;
9265 }
9266 nla_nest_end(msg, data);
9267
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07009268 wpa_printf(MSG_DEBUG,
9269 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
9270 params->hw_mode, params->ht_enabled, params->ht40_enabled,
9271 params->vht_enabled, params->ch_width, params->ch_list_len);
9272
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009273 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9274 if (ret) {
9275 wpa_printf(MSG_DEBUG,
9276 "nl80211: Failed to invoke driver ACS function: %s",
9277 strerror(errno));
9278 }
9279 return ret;
9280}
9281
9282
Ravi Joshie6ccb162015-07-16 17:45:41 -07009283static int nl80211_set_band(void *priv, enum set_band band)
9284{
9285 struct i802_bss *bss = priv;
9286 struct wpa_driver_nl80211_data *drv = bss->drv;
9287 struct nl_msg *msg;
9288 struct nlattr *data;
9289 int ret;
9290 enum qca_set_band qca_band;
9291
9292 if (!drv->setband_vendor_cmd_avail)
9293 return -1;
9294
9295 switch (band) {
9296 case WPA_SETBAND_AUTO:
9297 qca_band = QCA_SETBAND_AUTO;
9298 break;
9299 case WPA_SETBAND_5G:
9300 qca_band = QCA_SETBAND_5G;
9301 break;
9302 case WPA_SETBAND_2G:
9303 qca_band = QCA_SETBAND_2G;
9304 break;
9305 default:
9306 return -1;
9307 }
9308
9309 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9310 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9311 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9312 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
9313 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9314 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
9315 nlmsg_free(msg);
9316 return -ENOBUFS;
9317 }
9318 nla_nest_end(msg, data);
9319
9320 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9321 if (ret) {
9322 wpa_printf(MSG_DEBUG,
9323 "nl80211: Driver setband function failed: %s",
9324 strerror(errno));
9325 }
9326 return ret;
9327}
9328
9329
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009330struct nl80211_pcl {
9331 unsigned int num;
9332 unsigned int *freq_list;
9333};
9334
9335static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
9336{
9337 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9338 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9339 struct nl80211_pcl *param = arg;
9340 struct nlattr *nl_vend, *attr;
9341 enum qca_iface_type iface_type;
9342 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
9343 unsigned int num, max_num;
9344 u32 *freqs;
9345
9346 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9347 genlmsg_attrlen(gnlh, 0), NULL);
9348
9349 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
9350 if (!nl_vend)
9351 return NL_SKIP;
9352
9353 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
9354 nla_data(nl_vend), nla_len(nl_vend), NULL);
9355
9356 attr = tb_vendor[
9357 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
9358 if (!attr) {
9359 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
9360 param->num = 0;
9361 return NL_SKIP;
9362 }
9363
9364 iface_type = (enum qca_iface_type) nla_get_u32(attr);
9365 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
9366 iface_type);
9367
9368 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
9369 if (!attr) {
9370 wpa_printf(MSG_ERROR,
9371 "nl80211: preferred_freq_list couldn't be found");
9372 param->num = 0;
9373 return NL_SKIP;
9374 }
9375
9376 /*
9377 * param->num has the maximum number of entries for which there
9378 * is room in the freq_list provided by the caller.
9379 */
9380 freqs = nla_data(attr);
9381 max_num = nla_len(attr) / sizeof(u32);
9382 if (max_num > param->num)
9383 max_num = param->num;
9384 for (num = 0; num < max_num; num++)
9385 param->freq_list[num] = freqs[num];
9386 param->num = num;
9387
9388 return NL_SKIP;
9389}
9390
9391
9392static int nl80211_get_pref_freq_list(void *priv,
9393 enum wpa_driver_if_type if_type,
9394 unsigned int *num,
9395 unsigned int *freq_list)
9396{
9397 struct i802_bss *bss = priv;
9398 struct wpa_driver_nl80211_data *drv = bss->drv;
9399 struct nl_msg *msg;
9400 int ret;
9401 unsigned int i;
9402 struct nlattr *params;
9403 struct nl80211_pcl param;
9404 enum qca_iface_type iface_type;
9405
9406 if (!drv->get_pref_freq_list)
9407 return -1;
9408
9409 switch (if_type) {
9410 case WPA_IF_STATION:
9411 iface_type = QCA_IFACE_TYPE_STA;
9412 break;
9413 case WPA_IF_AP_BSS:
9414 iface_type = QCA_IFACE_TYPE_AP;
9415 break;
9416 case WPA_IF_P2P_GO:
9417 iface_type = QCA_IFACE_TYPE_P2P_GO;
9418 break;
9419 case WPA_IF_P2P_CLIENT:
9420 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
9421 break;
9422 case WPA_IF_IBSS:
9423 iface_type = QCA_IFACE_TYPE_IBSS;
9424 break;
9425 case WPA_IF_TDLS:
9426 iface_type = QCA_IFACE_TYPE_TDLS;
9427 break;
9428 default:
9429 return -1;
9430 }
9431
9432 param.num = *num;
9433 param.freq_list = freq_list;
9434
9435 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9436 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
9437 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9438 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9439 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
9440 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9441 nla_put_u32(msg,
9442 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
9443 iface_type)) {
9444 wpa_printf(MSG_ERROR,
9445 "%s: err in adding vendor_cmd and vendor_data",
9446 __func__);
9447 nlmsg_free(msg);
9448 return -1;
9449 }
9450 nla_nest_end(msg, params);
9451
9452 os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
9453 ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
9454 if (ret) {
9455 wpa_printf(MSG_ERROR,
9456 "%s: err in send_and_recv_msgs", __func__);
9457 return ret;
9458 }
9459
9460 *num = param.num;
9461
9462 for (i = 0; i < *num; i++) {
9463 wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
9464 i, freq_list[i]);
9465 }
9466
9467 return 0;
9468}
9469
9470
9471static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
9472{
9473 struct i802_bss *bss = priv;
9474 struct wpa_driver_nl80211_data *drv = bss->drv;
9475 struct nl_msg *msg;
9476 int ret;
9477 struct nlattr *params;
9478
9479 if (!drv->set_prob_oper_freq)
9480 return -1;
9481
9482 wpa_printf(MSG_DEBUG,
9483 "nl80211: Set P2P probable operating freq %u for ifindex %d",
9484 freq, bss->ifindex);
9485
9486 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9487 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9488 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9489 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
9490 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9491 nla_put_u32(msg,
9492 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
9493 QCA_IFACE_TYPE_P2P_CLIENT) ||
9494 nla_put_u32(msg,
9495 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
9496 freq)) {
9497 wpa_printf(MSG_ERROR,
9498 "%s: err in adding vendor_cmd and vendor_data",
9499 __func__);
9500 nlmsg_free(msg);
9501 return -1;
9502 }
9503 nla_nest_end(msg, params);
9504
9505 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9506 msg = NULL;
9507 if (ret) {
9508 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
9509 __func__);
9510 return ret;
9511 }
9512 nlmsg_free(msg);
9513 return 0;
9514}
9515
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009516
9517static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
9518 unsigned int period, unsigned int interval,
9519 unsigned int count, const u8 *device_types,
9520 size_t dev_types_len,
9521 const u8 *ies, size_t ies_len)
9522{
9523 struct i802_bss *bss = priv;
9524 struct wpa_driver_nl80211_data *drv = bss->drv;
9525 struct nl_msg *msg;
9526 struct nlattr *container;
9527 int ret;
9528
9529 wpa_printf(MSG_DEBUG,
9530 "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
9531 freq, period, interval, count);
9532
9533 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
9534 return -1;
9535
9536 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9537 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9538 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9539 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
9540 goto fail;
9541
9542 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
9543 if (!container)
9544 goto fail;
9545
9546 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
9547 freq) ||
9548 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
9549 period) ||
9550 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
9551 interval) ||
9552 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
9553 count) ||
9554 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
9555 dev_types_len, device_types) ||
9556 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
9557 ies_len, ies))
9558 goto fail;
9559
9560 nla_nest_end(msg, container);
9561 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9562 msg = NULL;
9563 if (ret) {
9564 wpa_printf(MSG_DEBUG,
9565 "nl80211: Failed to send P2P Listen offload vendor command");
9566 goto fail;
9567 }
9568
9569 return 0;
9570
9571fail:
9572 nlmsg_free(msg);
9573 return -1;
9574}
9575
9576
9577static int nl80211_p2p_lo_stop(void *priv)
9578{
9579 struct i802_bss *bss = priv;
9580 struct wpa_driver_nl80211_data *drv = bss->drv;
9581 struct nl_msg *msg;
9582
9583 wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
9584
9585 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
9586 return -1;
9587
9588 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9589 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9590 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9591 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
9592 nlmsg_free(msg);
9593 return -1;
9594 }
9595
9596 return send_and_recv_msgs(drv, msg, NULL, NULL);
9597}
9598
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08009599
9600static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
9601{
9602 struct i802_bss *bss = priv;
9603 struct wpa_driver_nl80211_data *drv = bss->drv;
9604 struct nl_msg *msg;
9605 struct nlattr *params;
9606 int ret;
9607 u32 tdls_mode;
9608
9609 wpa_printf(MSG_DEBUG,
9610 "nl80211: Set TDKS mode: tdls_external_control=%d",
9611 tdls_external_control);
9612
9613 if (tdls_external_control == 1)
9614 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
9615 QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
9616 else
9617 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
9618
9619 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9620 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9621 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9622 QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
9623 goto fail;
9624
9625 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
9626 if (!params)
9627 goto fail;
9628
9629 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
9630 tdls_mode))
9631 goto fail;
9632
9633 nla_nest_end(msg, params);
9634
9635 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9636 msg = NULL;
9637 if (ret) {
9638 wpa_printf(MSG_ERROR,
9639 "nl80211: Set TDLS mode failed: ret=%d (%s)",
9640 ret, strerror(-ret));
9641 goto fail;
9642 }
9643 return 0;
9644fail:
9645 nlmsg_free(msg);
9646 return -1;
9647}
9648
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009649#endif /* CONFIG_DRIVER_NL80211_QCA */
9650
9651
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009652static int nl80211_write_to_file(const char *name, unsigned int val)
9653{
9654 int fd, len;
9655 char tmp[128];
9656
9657 fd = open(name, O_RDWR);
9658 if (fd < 0) {
9659 wpa_printf(MSG_ERROR, "nl80211: Failed to open %s: %s",
9660 name, strerror(errno));
9661 return fd;
9662 }
9663
9664 len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
9665 len = write(fd, tmp, len);
9666 if (len < 0)
9667 wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
9668 name, strerror(errno));
9669 close(fd);
9670
9671 return 0;
9672}
9673
9674
9675static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
9676{
9677 struct i802_bss *bss = priv;
9678 char path[128];
9679 int ret;
9680
9681 wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
9682 filter_flags);
9683
9684 /* Configure filtering of unicast frame encrypted using GTK */
9685 ret = os_snprintf(path, sizeof(path),
9686 "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
9687 bss->ifname);
9688 if (os_snprintf_error(sizeof(path), ret))
9689 return -1;
9690
9691 ret = nl80211_write_to_file(path,
9692 !!(filter_flags &
9693 WPA_DATA_FRAME_FILTER_FLAG_GTK));
9694 if (ret) {
9695 wpa_printf(MSG_ERROR,
9696 "nl80211: Failed to set IPv4 unicast in multicast filter");
9697 return ret;
9698 }
9699
9700 os_snprintf(path, sizeof(path),
9701 "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
9702 bss->ifname);
9703 ret = nl80211_write_to_file(path,
9704 !!(filter_flags &
9705 WPA_DATA_FRAME_FILTER_FLAG_GTK));
9706
9707 if (ret) {
9708 wpa_printf(MSG_ERROR,
9709 "nl80211: Failed to set IPv6 unicast in multicast filter");
9710 return ret;
9711 }
9712
9713 /* Configure filtering of unicast frame encrypted using GTK */
9714 os_snprintf(path, sizeof(path),
9715 "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
9716 bss->ifname);
9717 ret = nl80211_write_to_file(path,
9718 !!(filter_flags &
9719 WPA_DATA_FRAME_FILTER_FLAG_ARP));
9720 if (ret) {
9721 wpa_printf(MSG_ERROR,
9722 "nl80211: Failed set gratuitous ARP filter");
9723 return ret;
9724 }
9725
9726 /* Configure filtering of IPv6 NA frames */
9727 os_snprintf(path, sizeof(path),
9728 "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
9729 bss->ifname);
9730 ret = nl80211_write_to_file(path,
9731 !!(filter_flags &
9732 WPA_DATA_FRAME_FILTER_FLAG_NA));
9733 if (ret) {
9734 wpa_printf(MSG_ERROR,
9735 "nl80211: Failed to set unsolicited NA filter");
9736 return ret;
9737 }
9738
9739 return 0;
9740}
9741
9742
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07009743static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
9744 const u8 **ext_capa, const u8 **ext_capa_mask,
9745 unsigned int *ext_capa_len)
9746{
9747 struct i802_bss *bss = priv;
9748 struct wpa_driver_nl80211_data *drv = bss->drv;
9749 enum nl80211_iftype nlmode;
9750 unsigned int i;
9751
9752 if (!ext_capa || !ext_capa_mask || !ext_capa_len)
9753 return -1;
9754
9755 nlmode = wpa_driver_nl80211_if_type(type);
9756
9757 /* By default, use the per-radio values */
9758 *ext_capa = drv->extended_capa;
9759 *ext_capa_mask = drv->extended_capa_mask;
9760 *ext_capa_len = drv->extended_capa_len;
9761
9762 /* Replace the default value if a per-interface type value exists */
9763 for (i = 0; i < drv->num_iface_ext_capa; i++) {
9764 if (nlmode == drv->iface_ext_capa[i].iftype) {
9765 *ext_capa = drv->iface_ext_capa[i].ext_capa;
9766 *ext_capa_mask = drv->iface_ext_capa[i].ext_capa_mask;
9767 *ext_capa_len = drv->iface_ext_capa[i].ext_capa_len;
9768 break;
9769 }
9770 }
9771
9772 return 0;
9773}
9774
9775
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009776const struct wpa_driver_ops wpa_driver_nl80211_ops = {
9777 .name = "nl80211",
9778 .desc = "Linux nl80211/cfg80211",
9779 .get_bssid = wpa_driver_nl80211_get_bssid,
9780 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009781 .set_key = driver_nl80211_set_key,
9782 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009783 .sched_scan = wpa_driver_nl80211_sched_scan,
9784 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009785 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009786 .abort_scan = wpa_driver_nl80211_abort_scan,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009787 .deauthenticate = driver_nl80211_deauthenticate,
9788 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009789 .associate = wpa_driver_nl80211_associate,
9790 .global_init = nl80211_global_init,
9791 .global_deinit = nl80211_global_deinit,
9792 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009793 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009794 .get_capa = wpa_driver_nl80211_get_capa,
9795 .set_operstate = wpa_driver_nl80211_set_operstate,
9796 .set_supp_port = wpa_driver_nl80211_set_supp_port,
9797 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009798 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009799 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07009800 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009801 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009802 .if_remove = driver_nl80211_if_remove,
9803 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009804 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009805 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009806 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009807 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
9808 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009809 .hapd_init = i802_init,
9810 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009811 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009812 .get_seqnum = i802_get_seqnum,
9813 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009814 .get_inact_sec = i802_get_inact_sec,
9815 .sta_clear_stats = i802_sta_clear_stats,
9816 .set_rts = i802_set_rts,
9817 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009818 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009819 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009820 .sta_deauth = i802_sta_deauth,
9821 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009822 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009823 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009824 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009825 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
9826 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
9827 .cancel_remain_on_channel =
9828 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009829 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009830 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -07009831 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009832 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009833 .signal_monitor = nl80211_signal_monitor,
9834 .signal_poll = nl80211_signal_poll,
9835 .send_frame = nl80211_send_frame,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009836 .set_param = nl80211_set_param,
9837 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009838 .add_pmkid = nl80211_add_pmkid,
9839 .remove_pmkid = nl80211_remove_pmkid,
9840 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009841 .set_rekey_info = nl80211_set_rekey_info,
9842 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009843 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -07009844 .start_dfs_cac = nl80211_start_radar_detection,
9845 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009846#ifdef CONFIG_TDLS
9847 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
9848 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009849 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
9850 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009851#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009852 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009853 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009854 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -07009855 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009856 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009857#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009858 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009859 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009860 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08009861#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07009862#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -07009863#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07009864 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -07009865#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08009866#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009867 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009868 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009869 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009870 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009871#ifdef CONFIG_MESH
9872 .init_mesh = wpa_driver_nl80211_init_mesh,
9873 .join_mesh = wpa_driver_nl80211_join_mesh,
9874 .leave_mesh = wpa_driver_nl80211_leave_mesh,
9875#endif /* CONFIG_MESH */
9876 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
9877 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
9878 .br_port_set_attr = wpa_driver_br_port_set_attr,
9879 .br_set_net_param = wpa_driver_br_set_net_param,
9880 .add_tx_ts = nl80211_add_ts,
9881 .del_tx_ts = nl80211_del_ts,
Dmitry Shmidte4663042016-04-04 10:07:49 -07009882 .get_ifindex = nl80211_get_ifindex,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009883#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009884 .roaming = nl80211_roaming,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009885 .do_acs = wpa_driver_do_acs,
Ravi Joshie6ccb162015-07-16 17:45:41 -07009886 .set_band = nl80211_set_band,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009887 .get_pref_freq_list = nl80211_get_pref_freq_list,
9888 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009889 .p2p_lo_start = nl80211_p2p_lo_start,
9890 .p2p_lo_stop = nl80211_p2p_lo_stop,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07009891 .set_default_scan_ies = nl80211_set_default_scan_ies,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08009892 .set_tdls_mode = nl80211_set_tdls_mode,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009893#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009894 .configure_data_frame_filters = nl80211_configure_data_frame_filters,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07009895 .get_ext_capab = nl80211_get_ext_capab,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009896};