blob: d5716dbcc6f6f18686f90b0d0f0f1e3c84ed3cad [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"
32#include "l2_packet/l2_packet.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "netlink.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080034#include "linux_defines.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "linux_ioctl.h"
36#include "radiotap.h"
37#include "radiotap_iter.h"
38#include "rfkill.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080039#include "driver_nl80211.h"
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080040
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080041
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080042#ifndef CONFIG_LIBNL20
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043/*
44 * libnl 1.1 has a bug, it tries to allocate socket numbers densely
45 * but when you free a socket again it will mess up its bitmap and
46 * and use the wrong number the next time it needs a socket ID.
47 * Therefore, we wrap the handle alloc/destroy and add our own pid
48 * accounting.
49 */
50static uint32_t port_bitmap[32] = { 0 };
51
52static struct nl_handle *nl80211_handle_alloc(void *cb)
53{
54 struct nl_handle *handle;
55 uint32_t pid = getpid() & 0x3FFFFF;
56 int i;
57
58 handle = nl_handle_alloc_cb(cb);
59
60 for (i = 0; i < 1024; i++) {
61 if (port_bitmap[i / 32] & (1 << (i % 32)))
62 continue;
63 port_bitmap[i / 32] |= 1 << (i % 32);
64 pid += i << 22;
65 break;
66 }
67
68 nl_socket_set_local_port(handle, pid);
69
70 return handle;
71}
72
73static void nl80211_handle_destroy(struct nl_handle *handle)
74{
75 uint32_t port = nl_socket_get_local_port(handle);
76
77 port >>= 22;
78 port_bitmap[port / 32] &= ~(1 << (port % 32));
79
80 nl_handle_destroy(handle);
81}
82#endif /* CONFIG_LIBNL20 */
83
84
Dmitry Shmidt54605472013-11-08 11:10:19 -080085#ifdef ANDROID
86/* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
Dmitry Shmidt54605472013-11-08 11:10:19 -080087#undef nl_socket_set_nonblocking
88#define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080089
Dmitry Shmidt54605472013-11-08 11:10:19 -080090#endif /* ANDROID */
91
92
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080093static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
94{
95 struct nl_handle *handle;
96
97 handle = nl80211_handle_alloc(cb);
98 if (handle == NULL) {
99 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
100 "callbacks (%s)", dbg);
101 return NULL;
102 }
103
104 if (genl_connect(handle)) {
105 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
106 "netlink (%s)", dbg);
107 nl80211_handle_destroy(handle);
108 return NULL;
109 }
110
111 return handle;
112}
113
114
115static void nl_destroy_handles(struct nl_handle **handle)
116{
117 if (*handle == NULL)
118 return;
119 nl80211_handle_destroy(*handle);
120 *handle = NULL;
121}
122
123
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700124#if __WORDSIZE == 64
125#define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
126#else
127#define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
128#endif
129
130static void nl80211_register_eloop_read(struct nl_handle **handle,
131 eloop_sock_handler handler,
132 void *eloop_data)
133{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800134#ifdef CONFIG_LIBNL20
135 /*
136 * libnl uses a pretty small buffer (32 kB that gets converted to 64 kB)
137 * by default. It is possible to hit that limit in some cases where
138 * operations are blocked, e.g., with a burst of Deauthentication frames
139 * to hostapd and STA entry deletion. Try to increase the buffer to make
140 * this less likely to occur.
141 */
142 if (nl_socket_set_buffer_size(*handle, 262144, 0) < 0) {
143 wpa_printf(MSG_DEBUG,
144 "nl80211: Could not set nl_socket RX buffer size: %s",
145 strerror(errno));
146 /* continue anyway with the default (smaller) buffer */
147 }
148#endif /* CONFIG_LIBNL20 */
149
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700150 nl_socket_set_nonblocking(*handle);
151 eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
152 eloop_data, *handle);
153 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
154}
155
156
157static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
158{
159 *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
160 eloop_unregister_read_sock(nl_socket_get_fd(*handle));
161 nl_destroy_handles(handle);
162}
163
164
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800165static void nl80211_global_deinit(void *priv);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800166static void nl80211_check_global(struct nl80211_global *global);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800167
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800168static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700169static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
170 struct hostapd_freq_params *freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700171
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800173wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800174 const u8 *set_addr, int first,
175 const char *driver_params);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800176static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177 unsigned int freq, unsigned int wait,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800178 const u8 *buf, size_t buf_len, u64 *cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800179 int no_cck, int no_ack, int offchanok,
180 const u16 *csa_offs, size_t csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -0800181static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
182 int report);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800184#define IFIDX_ANY -1
185
186static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
187 int ifidx_reason);
188static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
189 int ifidx_reason);
190static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
191 int ifidx_reason);
Dmitry Shmidt738a26e2011-07-07 14:22:14 -0700192
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700193static int nl80211_set_channel(struct i802_bss *bss,
194 struct hostapd_freq_params *freq, int set_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
196 int ifindex, int disabled);
197
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800198static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
199 int reset_mode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800200
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700201static int i802_set_iface_flags(struct i802_bss *bss, int up);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800202static int nl80211_set_param(void *priv, const char *param);
Dmitry Shmidtd13095b2016-08-22 14:02:19 -0700203#ifdef CONFIG_MESH
204static int nl80211_put_mesh_config(struct nl_msg *msg,
205 struct wpa_driver_mesh_bss_params *params);
206#endif /* CONFIG_MESH */
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700207
208
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800209/* Converts nl80211_chan_width to a common format */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800210enum chan_width convert2width(int width)
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800211{
212 switch (width) {
213 case NL80211_CHAN_WIDTH_20_NOHT:
214 return CHAN_WIDTH_20_NOHT;
215 case NL80211_CHAN_WIDTH_20:
216 return CHAN_WIDTH_20;
217 case NL80211_CHAN_WIDTH_40:
218 return CHAN_WIDTH_40;
219 case NL80211_CHAN_WIDTH_80:
220 return CHAN_WIDTH_80;
221 case NL80211_CHAN_WIDTH_80P80:
222 return CHAN_WIDTH_80P80;
223 case NL80211_CHAN_WIDTH_160:
224 return CHAN_WIDTH_160;
225 }
226 return CHAN_WIDTH_UNKNOWN;
227}
228
229
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800230int is_ap_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800231{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700232 return nlmode == NL80211_IFTYPE_AP ||
233 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800234}
235
236
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800237int is_sta_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800238{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700239 return nlmode == NL80211_IFTYPE_STATION ||
240 nlmode == NL80211_IFTYPE_P2P_CLIENT;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800241}
242
243
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700244static int is_p2p_net_interface(enum nl80211_iftype nlmode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800245{
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700246 return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
247 nlmode == NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800248}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700249
250
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800251struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
252 int ifindex)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -0700253{
254 struct i802_bss *bss;
255
256 for (bss = drv->first_bss; bss; bss = bss->next) {
257 if (bss->ifindex == ifindex)
258 return bss;
259 }
260
261 return NULL;
262}
263
264
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800265static int is_mesh_interface(enum nl80211_iftype nlmode)
266{
267 return nlmode == NL80211_IFTYPE_MESH_POINT;
268}
269
270
271void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt8bae4132013-06-06 11:25:10 -0700272{
273 if (drv->associated)
274 os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
275 drv->associated = 0;
276 os_memset(drv->bssid, 0, ETH_ALEN);
277}
278
279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280/* nl80211 code */
281static int ack_handler(struct nl_msg *msg, void *arg)
282{
283 int *err = arg;
284 *err = 0;
285 return NL_STOP;
286}
287
288static int finish_handler(struct nl_msg *msg, void *arg)
289{
290 int *ret = arg;
291 *ret = 0;
292 return NL_SKIP;
293}
294
295static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
296 void *arg)
297{
298 int *ret = arg;
299 *ret = err->error;
300 return NL_SKIP;
301}
302
303
304static int no_seq_check(struct nl_msg *msg, void *arg)
305{
306 return NL_OK;
307}
308
309
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800310static void nl80211_nlmsg_clear(struct nl_msg *msg)
311{
312 /*
313 * Clear nlmsg data, e.g., to make sure key material is not left in
314 * heap memory for unnecessarily long time.
315 */
316 if (msg) {
317 struct nlmsghdr *hdr = nlmsg_hdr(msg);
318 void *data = nlmsg_data(hdr);
319 /*
320 * This would use nlmsg_datalen() or the older nlmsg_len() if
321 * only libnl were to maintain a stable API.. Neither will work
322 * with all released versions, so just calculate the length
323 * here.
324 */
325 int len = hdr->nlmsg_len - NLMSG_HDRLEN;
326
327 os_memset(data, 0, len);
328 }
329}
330
331
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800332static int send_and_recv(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333 struct nl_handle *nl_handle, struct nl_msg *msg,
334 int (*valid_handler)(struct nl_msg *, void *),
335 void *valid_data)
336{
337 struct nl_cb *cb;
338 int err = -ENOMEM;
339
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800340 if (!msg)
341 return -ENOMEM;
342
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800343 cb = nl_cb_clone(global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344 if (!cb)
345 goto out;
346
347 err = nl_send_auto_complete(nl_handle, msg);
348 if (err < 0)
349 goto out;
350
351 err = 1;
352
353 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
354 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
355 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
356
357 if (valid_handler)
358 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
359 valid_handler, valid_data);
360
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700361 while (err > 0) {
362 int res = nl_recvmsgs(nl_handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700363 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700364 wpa_printf(MSG_INFO,
365 "nl80211: %s->nl_recvmsgs failed: %d",
366 __func__, res);
367 }
368 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369 out:
370 nl_cb_put(cb);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800371 if (!valid_handler && valid_data == (void *) -1)
372 nl80211_nlmsg_clear(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373 nlmsg_free(msg);
374 return err;
375}
376
377
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800378int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
379 struct nl_msg *msg,
380 int (*valid_handler)(struct nl_msg *, void *),
381 void *valid_data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800383 return send_and_recv(drv->global, drv->global->nl, msg,
384 valid_handler, valid_data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385}
386
387
388struct family_data {
389 const char *group;
390 int id;
391};
392
393
394static int family_handler(struct nl_msg *msg, void *arg)
395{
396 struct family_data *res = arg;
397 struct nlattr *tb[CTRL_ATTR_MAX + 1];
398 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
399 struct nlattr *mcgrp;
400 int i;
401
402 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
403 genlmsg_attrlen(gnlh, 0), NULL);
404 if (!tb[CTRL_ATTR_MCAST_GROUPS])
405 return NL_SKIP;
406
407 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
408 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
409 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
410 nla_len(mcgrp), NULL);
411 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
412 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
413 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
414 res->group,
415 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
416 continue;
417 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
418 break;
419 };
420
421 return NL_SKIP;
422}
423
424
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800425static int nl_get_multicast_id(struct nl80211_global *global,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426 const char *family, const char *group)
427{
428 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800429 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700430 struct family_data res = { group, -ENOENT };
431
432 msg = nlmsg_alloc();
433 if (!msg)
434 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800435 if (!genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
436 0, 0, CTRL_CMD_GETFAMILY, 0) ||
437 nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
438 nlmsg_free(msg);
439 return -1;
440 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700441
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800442 ret = send_and_recv(global, global->nl, msg, family_handler, &res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700443 if (ret == 0)
444 ret = res.id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445 return ret;
446}
447
448
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800449void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
450 struct nl_msg *msg, int flags, uint8_t cmd)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800451{
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700452 if (TEST_FAIL())
453 return NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800454 return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
455 0, flags, cmd, 0);
456}
457
458
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800459static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
460{
461 if (bss->wdev_id_set)
462 return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
463 return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
464}
465
466
467struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
468{
469 struct nl_msg *msg;
470
471 msg = nlmsg_alloc();
472 if (!msg)
473 return NULL;
474
475 if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
476 nl80211_set_iface_id(msg, bss) < 0) {
477 nlmsg_free(msg);
478 return NULL;
479 }
480
481 return msg;
482}
483
484
485static struct nl_msg *
486nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
487 int flags, uint8_t cmd)
488{
489 struct nl_msg *msg;
490
491 msg = nlmsg_alloc();
492 if (!msg)
493 return NULL;
494
495 if (!nl80211_cmd(drv, msg, flags, cmd) ||
496 nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
497 nlmsg_free(msg);
498 return NULL;
499 }
500
501 return msg;
502}
503
504
505struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
506 uint8_t cmd)
507{
508 return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
509}
510
511
512struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
513{
514 return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
515}
516
517
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800518struct wiphy_idx_data {
519 int wiphy_idx;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700520 enum nl80211_iftype nlmode;
521 u8 *macaddr;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800522};
523
524
525static int netdev_info_handler(struct nl_msg *msg, void *arg)
526{
527 struct nlattr *tb[NL80211_ATTR_MAX + 1];
528 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
529 struct wiphy_idx_data *info = arg;
530
531 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
532 genlmsg_attrlen(gnlh, 0), NULL);
533
534 if (tb[NL80211_ATTR_WIPHY])
535 info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
536
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700537 if (tb[NL80211_ATTR_IFTYPE])
538 info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
539
540 if (tb[NL80211_ATTR_MAC] && info->macaddr)
541 os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
542 ETH_ALEN);
543
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800544 return NL_SKIP;
545}
546
547
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800548int nl80211_get_wiphy_index(struct i802_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800549{
550 struct nl_msg *msg;
551 struct wiphy_idx_data data = {
552 .wiphy_idx = -1,
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700553 .macaddr = NULL,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800554 };
555
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800556 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
557 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800558
559 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
560 return data.wiphy_idx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800561 return -1;
562}
563
564
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700565static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
566{
567 struct nl_msg *msg;
568 struct wiphy_idx_data data = {
569 .nlmode = NL80211_IFTYPE_UNSPECIFIED,
570 .macaddr = NULL,
571 };
572
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800573 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
574 return NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700575
576 if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
577 return data.nlmode;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700578 return NL80211_IFTYPE_UNSPECIFIED;
579}
580
581
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700582static int nl80211_get_macaddr(struct i802_bss *bss)
583{
584 struct nl_msg *msg;
585 struct wiphy_idx_data data = {
586 .macaddr = bss->addr,
587 };
588
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800589 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
590 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700591
592 return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700593}
Dmitry Shmidt34af3062013-07-11 10:46:32 -0700594
595
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800596static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
597 struct nl80211_wiphy_data *w)
598{
599 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800600 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800601
602 msg = nlmsg_alloc();
603 if (!msg)
604 return -1;
605
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800606 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
607 nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
608 nlmsg_free(msg);
609 return -1;
610 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800611
612 ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800613 if (ret) {
614 wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
615 "failed: ret=%d (%s)",
616 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800617 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800618 return ret;
619}
620
621
622static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
623{
624 struct nl80211_wiphy_data *w = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700625 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800626
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800627 wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800628
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700629 res = nl_recvmsgs(handle, w->nl_cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -0700630 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700631 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
632 __func__, res);
633 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800634}
635
636
637static int process_beacon_event(struct nl_msg *msg, void *arg)
638{
639 struct nl80211_wiphy_data *w = arg;
640 struct wpa_driver_nl80211_data *drv;
641 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
642 struct nlattr *tb[NL80211_ATTR_MAX + 1];
643 union wpa_event_data event;
644
645 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
646 genlmsg_attrlen(gnlh, 0), NULL);
647
648 if (gnlh->cmd != NL80211_CMD_FRAME) {
649 wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
650 gnlh->cmd);
651 return NL_SKIP;
652 }
653
654 if (!tb[NL80211_ATTR_FRAME])
655 return NL_SKIP;
656
657 dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
658 wiphy_list) {
659 os_memset(&event, 0, sizeof(event));
660 event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
661 event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
662 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
663 }
664
665 return NL_SKIP;
666}
667
668
669static struct nl80211_wiphy_data *
670nl80211_get_wiphy_data_ap(struct i802_bss *bss)
671{
672 static DEFINE_DL_LIST(nl80211_wiphys);
673 struct nl80211_wiphy_data *w;
674 int wiphy_idx, found = 0;
675 struct i802_bss *tmp_bss;
676
677 if (bss->wiphy_data != NULL)
678 return bss->wiphy_data;
679
680 wiphy_idx = nl80211_get_wiphy_index(bss);
681
682 dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
683 if (w->wiphy_idx == wiphy_idx)
684 goto add;
685 }
686
687 /* alloc new one */
688 w = os_zalloc(sizeof(*w));
689 if (w == NULL)
690 return NULL;
691 w->wiphy_idx = wiphy_idx;
692 dl_list_init(&w->bsss);
693 dl_list_init(&w->drvs);
694
695 w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
696 if (!w->nl_cb) {
697 os_free(w);
698 return NULL;
699 }
700 nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
701 nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
702 w);
703
704 w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
705 "wiphy beacons");
706 if (w->nl_beacons == NULL) {
707 os_free(w);
708 return NULL;
709 }
710
711 if (nl80211_register_beacons(bss->drv, w)) {
712 nl_destroy_handles(&w->nl_beacons);
713 os_free(w);
714 return NULL;
715 }
716
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700717 nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800718
719 dl_list_add(&nl80211_wiphys, &w->list);
720
721add:
722 /* drv entry for this bss already there? */
723 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
724 if (tmp_bss->drv == bss->drv) {
725 found = 1;
726 break;
727 }
728 }
729 /* if not add it */
730 if (!found)
731 dl_list_add(&w->drvs, &bss->drv->wiphy_list);
732
733 dl_list_add(&w->bsss, &bss->wiphy_list);
734 bss->wiphy_data = w;
735 return w;
736}
737
738
739static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
740{
741 struct nl80211_wiphy_data *w = bss->wiphy_data;
742 struct i802_bss *tmp_bss;
743 int found = 0;
744
745 if (w == NULL)
746 return;
747 bss->wiphy_data = NULL;
748 dl_list_del(&bss->wiphy_list);
749
750 /* still any for this drv present? */
751 dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
752 if (tmp_bss->drv == bss->drv) {
753 found = 1;
754 break;
755 }
756 }
757 /* if not remove it */
758 if (!found)
759 dl_list_del(&bss->drv->wiphy_list);
760
761 if (!dl_list_empty(&w->bsss))
762 return;
763
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700764 nl80211_destroy_eloop_handle(&w->nl_beacons);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800765
766 nl_cb_put(w->nl_cb);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800767 dl_list_del(&w->list);
768 os_free(w);
769}
770
771
Dmitry Shmidte4663042016-04-04 10:07:49 -0700772static unsigned int nl80211_get_ifindex(void *priv)
773{
774 struct i802_bss *bss = priv;
775 struct wpa_driver_nl80211_data *drv = bss->drv;
776
777 return drv->ifindex;
778}
779
780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700781static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
782{
783 struct i802_bss *bss = priv;
784 struct wpa_driver_nl80211_data *drv = bss->drv;
785 if (!drv->associated)
786 return -1;
787 os_memcpy(bssid, drv->bssid, ETH_ALEN);
788 return 0;
789}
790
791
792static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
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(ssid, drv->ssid, drv->ssid_len);
799 return drv->ssid_len;
800}
801
802
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800803static void wpa_driver_nl80211_event_newlink(
Dmitry Shmidte4663042016-04-04 10:07:49 -0700804 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
805 int ifindex, const char *ifname)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806{
807 union wpa_event_data event;
808
Dmitry Shmidte4663042016-04-04 10:07:49 -0700809 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800810 if (if_nametoindex(drv->first_bss->ifname) == 0) {
811 wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
812 drv->first_bss->ifname);
813 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700814 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800815 if (!drv->if_removed)
816 return;
817 wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
818 drv->first_bss->ifname);
819 drv->if_removed = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820 }
821
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800822 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -0700823 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800824 os_strlcpy(event.interface_status.ifname, ifname,
825 sizeof(event.interface_status.ifname));
826 event.interface_status.ievent = EVENT_INTERFACE_ADDED;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700827 if (drv)
828 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
829 else
830 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
831 &event);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800832}
833
834
835static void wpa_driver_nl80211_event_dellink(
Dmitry Shmidte4663042016-04-04 10:07:49 -0700836 struct nl80211_global *global, struct wpa_driver_nl80211_data *drv,
837 int ifindex, const char *ifname)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800838{
839 union wpa_event_data event;
840
Dmitry Shmidte4663042016-04-04 10:07:49 -0700841 if (drv && os_strcmp(drv->first_bss->ifname, ifname) == 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800842 if (drv->if_removed) {
843 wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
844 ifname);
845 return;
846 }
847 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
848 ifname);
849 drv->if_removed = 1;
850 } else {
851 wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
852 ifname);
853 }
854
855 os_memset(&event, 0, sizeof(event));
Dmitry Shmidte4663042016-04-04 10:07:49 -0700856 event.interface_status.ifindex = ifindex;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800857 os_strlcpy(event.interface_status.ifname, ifname,
858 sizeof(event.interface_status.ifname));
859 event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
Dmitry Shmidte4663042016-04-04 10:07:49 -0700860 if (drv)
861 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
862 else
863 wpa_supplicant_event_global(global->ctx, EVENT_INTERFACE_STATUS,
864 &event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865}
866
867
868static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
869 u8 *buf, size_t len)
870{
871 int attrlen, rta_len;
872 struct rtattr *attr;
873
874 attrlen = len;
875 attr = (struct rtattr *) buf;
876
877 rta_len = RTA_ALIGN(sizeof(struct rtattr));
878 while (RTA_OK(attr, attrlen)) {
879 if (attr->rta_type == IFLA_IFNAME) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800880 if (os_strcmp(((char *) attr) + rta_len,
881 drv->first_bss->ifname) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 return 1;
883 else
884 break;
885 }
886 attr = RTA_NEXT(attr, attrlen);
887 }
888
889 return 0;
890}
891
892
893static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
894 int ifindex, u8 *buf, size_t len)
895{
896 if (drv->ifindex == ifindex)
897 return 1;
898
899 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800900 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
902 "interface");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800903 wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 return 1;
905 }
906
907 return 0;
908}
909
910
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800911static struct wpa_driver_nl80211_data *
912nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
913{
914 struct wpa_driver_nl80211_data *drv;
915 dl_list_for_each(drv, &global->interfaces,
916 struct wpa_driver_nl80211_data, list) {
917 if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800918 have_ifidx(drv, idx, IFIDX_ANY))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800919 return drv;
920 }
921 return NULL;
922}
923
924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
926 struct ifinfomsg *ifi,
927 u8 *buf, size_t len)
928{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800929 struct nl80211_global *global = ctx;
930 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800931 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 struct rtattr *attr;
933 u32 brid = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800934 char namebuf[IFNAMSIZ];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800935 char ifname[IFNAMSIZ + 1];
936 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800938 extra[0] = '\0';
939 pos = extra;
940 end = pos + sizeof(extra);
941 ifname[0] = '\0';
942
943 attrlen = len;
944 attr = (struct rtattr *) buf;
945 while (RTA_OK(attr, attrlen)) {
946 switch (attr->rta_type) {
947 case IFLA_IFNAME:
948 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
949 break;
950 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
951 ifname[RTA_PAYLOAD(attr)] = '\0';
952 break;
953 case IFLA_MASTER:
954 brid = nla_get_u32((struct nlattr *) attr);
955 pos += os_snprintf(pos, end - pos, " master=%u", brid);
956 break;
957 case IFLA_WIRELESS:
958 pos += os_snprintf(pos, end - pos, " wext");
959 break;
960 case IFLA_OPERSTATE:
961 pos += os_snprintf(pos, end - pos, " operstate=%u",
962 nla_get_u32((struct nlattr *) attr));
963 break;
964 case IFLA_LINKMODE:
965 pos += os_snprintf(pos, end - pos, " linkmode=%u",
966 nla_get_u32((struct nlattr *) attr));
967 break;
968 }
969 attr = RTA_NEXT(attr, attrlen);
970 }
971 extra[sizeof(extra) - 1] = '\0';
972
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700973 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
974 ifi->ifi_index, ifname, extra, ifi->ifi_family,
975 ifi->ifi_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
977 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
978 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
979 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
980
Dmitry Shmidte4663042016-04-04 10:07:49 -0700981 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
982 if (!drv)
983 goto event_newlink;
984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800986 namebuf[0] = '\0';
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800987 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800988 linux_iface_up(drv->global->ioctl_sock, namebuf) > 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800989 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
990 "event since interface %s is up", namebuf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800991 drv->ignore_if_down_event = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800992 return;
993 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800994 wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
995 namebuf, ifname);
996 if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
997 wpa_printf(MSG_DEBUG,
998 "nl80211: Not the main interface (%s) - do not indicate interface down",
999 drv->first_bss->ifname);
1000 } else if (drv->ignore_if_down_event) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001001 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
1002 "event generated by mode change");
1003 drv->ignore_if_down_event = 0;
1004 } else {
1005 drv->if_disabled = 1;
1006 wpa_supplicant_event(drv->ctx,
1007 EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001008
1009 /*
1010 * Try to get drv again, since it may be removed as
1011 * part of the EVENT_INTERFACE_DISABLED handling for
1012 * dynamic interfaces
1013 */
1014 drv = nl80211_find_drv(global, ifi->ifi_index,
1015 buf, len);
1016 if (!drv)
1017 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001018 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019 }
1020
1021 if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001022 if (if_indextoname(ifi->ifi_index, namebuf) &&
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001023 linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001024 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1025 "event since interface %s is down",
1026 namebuf);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001027 } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001028 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1029 "event since interface %s does not exist",
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001030 drv->first_bss->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001031 } else if (drv->if_removed) {
1032 wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
1033 "event since interface %s is marked "
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001034 "removed", drv->first_bss->ifname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001035 } else {
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001036 struct i802_bss *bss;
1037 u8 addr[ETH_ALEN];
1038
1039 /* Re-read MAC address as it may have changed */
1040 bss = get_bss_ifindex(drv, ifi->ifi_index);
1041 if (bss &&
1042 linux_get_ifhwaddr(drv->global->ioctl_sock,
1043 bss->ifname, addr) < 0) {
1044 wpa_printf(MSG_DEBUG,
1045 "nl80211: %s: failed to re-read MAC address",
1046 bss->ifname);
1047 } else if (bss &&
1048 os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
1049 wpa_printf(MSG_DEBUG,
1050 "nl80211: Own MAC address on ifindex %d (%s) changed from "
1051 MACSTR " to " MACSTR,
1052 ifi->ifi_index, bss->ifname,
1053 MAC2STR(bss->addr),
1054 MAC2STR(addr));
1055 os_memcpy(bss->addr, addr, ETH_ALEN);
1056 }
1057
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001058 wpa_printf(MSG_DEBUG, "nl80211: Interface up");
1059 drv->if_disabled = 0;
1060 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
1061 NULL);
1062 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001063 }
1064
1065 /*
1066 * Some drivers send the association event before the operup event--in
1067 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
1068 * fails. This will hit us when wpa_supplicant does not need to do
1069 * IEEE 802.1X authentication
1070 */
1071 if (drv->operstate == 1 &&
1072 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001073 !(ifi->ifi_flags & IFF_RUNNING)) {
1074 wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001075 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076 -1, IF_OPER_UP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001077 }
1078
Dmitry Shmidte4663042016-04-04 10:07:49 -07001079event_newlink:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001080 if (ifname[0])
Dmitry Shmidte4663042016-04-04 10:07:49 -07001081 wpa_driver_nl80211_event_newlink(global, drv, ifi->ifi_index,
1082 ifname);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001083
Dmitry Shmidte4663042016-04-04 10:07:49 -07001084 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001085 struct i802_bss *bss;
1086
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087 /* device has been added to bridge */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001088 if (!if_indextoname(brid, namebuf)) {
1089 wpa_printf(MSG_DEBUG,
1090 "nl80211: Could not find bridge ifname for ifindex %u",
1091 brid);
1092 return;
1093 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
1095 brid, namebuf);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001096 add_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001097
1098 for (bss = drv->first_bss; bss; bss = bss->next) {
1099 if (os_strcmp(ifname, bss->ifname) == 0) {
1100 os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
1101 break;
1102 }
1103 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105}
1106
1107
1108static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
1109 struct ifinfomsg *ifi,
1110 u8 *buf, size_t len)
1111{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001112 struct nl80211_global *global = ctx;
1113 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001114 int attrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115 struct rtattr *attr;
1116 u32 brid = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001117 char ifname[IFNAMSIZ + 1];
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001118 char extra[100], *pos, *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001119
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001120 extra[0] = '\0';
1121 pos = extra;
1122 end = pos + sizeof(extra);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001123 ifname[0] = '\0';
1124
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125 attrlen = len;
1126 attr = (struct rtattr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 while (RTA_OK(attr, attrlen)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001128 switch (attr->rta_type) {
1129 case IFLA_IFNAME:
1130 if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
1131 break;
1132 os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
1133 ifname[RTA_PAYLOAD(attr)] = '\0';
1134 break;
1135 case IFLA_MASTER:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 brid = nla_get_u32((struct nlattr *) attr);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001137 pos += os_snprintf(pos, end - pos, " master=%u", brid);
1138 break;
1139 case IFLA_OPERSTATE:
1140 pos += os_snprintf(pos, end - pos, " operstate=%u",
1141 nla_get_u32((struct nlattr *) attr));
1142 break;
1143 case IFLA_LINKMODE:
1144 pos += os_snprintf(pos, end - pos, " linkmode=%u",
1145 nla_get_u32((struct nlattr *) attr));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001146 break;
1147 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148 attr = RTA_NEXT(attr, attrlen);
1149 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001150 extra[sizeof(extra) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001152 wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
1153 ifi->ifi_index, ifname, extra, ifi->ifi_family,
1154 ifi->ifi_flags,
1155 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
1156 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
1157 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
1158 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
1159
Dmitry Shmidte4663042016-04-04 10:07:49 -07001160 drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001161
Dmitry Shmidte4663042016-04-04 10:07:49 -07001162 if (ifi->ifi_family == AF_BRIDGE && brid && drv) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 /* device has been removed from bridge */
1164 char namebuf[IFNAMSIZ];
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001165
1166 if (!if_indextoname(brid, namebuf)) {
1167 wpa_printf(MSG_DEBUG,
1168 "nl80211: Could not find bridge ifname for ifindex %u",
1169 brid);
1170 } else {
1171 wpa_printf(MSG_DEBUG,
1172 "nl80211: Remove ifindex %u for bridge %s",
1173 brid, namebuf);
1174 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001175 del_ifidx(drv, brid, ifi->ifi_index);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07001177
1178 if (ifi->ifi_family != AF_BRIDGE || !brid)
1179 wpa_driver_nl80211_event_dellink(global, drv, ifi->ifi_index,
1180 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181}
1182
1183
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001184struct nl80211_get_assoc_freq_arg {
1185 struct wpa_driver_nl80211_data *drv;
1186 unsigned int assoc_freq;
1187 unsigned int ibss_freq;
1188 u8 assoc_bssid[ETH_ALEN];
1189 u8 assoc_ssid[SSID_MAX_LEN];
1190 u8 assoc_ssid_len;
1191};
1192
1193static int nl80211_get_assoc_freq_handler(struct nl_msg *msg, void *arg)
1194{
1195 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1196 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1197 struct nlattr *bss[NL80211_BSS_MAX + 1];
1198 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1199 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
1200 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1201 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
1202 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1203 };
1204 struct nl80211_get_assoc_freq_arg *ctx = arg;
1205 enum nl80211_bss_status status;
1206
1207 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1208 genlmsg_attrlen(gnlh, 0), NULL);
1209 if (!tb[NL80211_ATTR_BSS] ||
1210 nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1211 bss_policy) ||
1212 !bss[NL80211_BSS_STATUS])
1213 return NL_SKIP;
1214
1215 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
1216 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1217 bss[NL80211_BSS_FREQUENCY]) {
1218 ctx->assoc_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1219 wpa_printf(MSG_DEBUG, "nl80211: Associated on %u MHz",
1220 ctx->assoc_freq);
1221 }
1222 if (status == NL80211_BSS_STATUS_IBSS_JOINED &&
1223 bss[NL80211_BSS_FREQUENCY]) {
1224 ctx->ibss_freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1225 wpa_printf(MSG_DEBUG, "nl80211: IBSS-joined on %u MHz",
1226 ctx->ibss_freq);
1227 }
1228 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1229 bss[NL80211_BSS_BSSID]) {
1230 os_memcpy(ctx->assoc_bssid,
1231 nla_data(bss[NL80211_BSS_BSSID]), ETH_ALEN);
1232 wpa_printf(MSG_DEBUG, "nl80211: Associated with "
1233 MACSTR, MAC2STR(ctx->assoc_bssid));
1234 }
1235
1236 if (status == NL80211_BSS_STATUS_ASSOCIATED &&
1237 bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
1238 const u8 *ie, *ssid;
1239 size_t ie_len;
1240
1241 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1242 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1243 ssid = get_ie(ie, ie_len, WLAN_EID_SSID);
1244 if (ssid && ssid[1] > 0 && ssid[1] <= SSID_MAX_LEN) {
1245 ctx->assoc_ssid_len = ssid[1];
1246 os_memcpy(ctx->assoc_ssid, ssid + 2, ssid[1]);
1247 }
1248 }
1249
1250 return NL_SKIP;
1251}
1252
1253
1254int nl80211_get_assoc_ssid(struct wpa_driver_nl80211_data *drv, u8 *ssid)
Jouni Malinen87fd2792011-05-16 18:35:42 +03001255{
1256 struct nl_msg *msg;
1257 int ret;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001258 struct nl80211_get_assoc_freq_arg arg;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001259
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001260 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001261 os_memset(&arg, 0, sizeof(arg));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001262 arg.drv = drv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001263 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1264 &arg);
1265 if (ret == 0) {
1266 os_memcpy(ssid, arg.assoc_ssid, arg.assoc_ssid_len);
1267 return arg.assoc_ssid_len;
1268 }
1269 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d (%s)",
1270 ret, strerror(-ret));
1271 return ret;
1272}
1273
1274
1275unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
1276{
1277 struct nl_msg *msg;
1278 int ret;
1279 struct nl80211_get_assoc_freq_arg arg;
1280
1281 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
1282 os_memset(&arg, 0, sizeof(arg));
1283 arg.drv = drv;
1284 ret = send_and_recv_msgs(drv, msg, nl80211_get_assoc_freq_handler,
1285 &arg);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001286 if (ret == 0) {
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001287 unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
1288 arg.ibss_freq : arg.assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001289 wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001290 "associated BSS from scan results: %u MHz", freq);
1291 if (freq)
1292 drv->assoc_freq = freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001293 return drv->assoc_freq;
Jouni Malinen87fd2792011-05-16 18:35:42 +03001294 }
1295 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1296 "(%s)", ret, strerror(-ret));
Jouni Malinen87fd2792011-05-16 18:35:42 +03001297 return drv->assoc_freq;
1298}
1299
1300
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301static int get_link_signal(struct nl_msg *msg, void *arg)
1302{
1303 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1304 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1305 struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
1306 static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
1307 [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001308 [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001309 [NL80211_STA_INFO_BEACON_SIGNAL_AVG] = { .type = NLA_U8 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 };
1311 struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
1312 static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
1313 [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
1314 [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
1315 [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
1316 [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
1317 };
1318 struct wpa_signal_info *sig_change = arg;
1319
1320 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1321 genlmsg_attrlen(gnlh, 0), NULL);
1322 if (!tb[NL80211_ATTR_STA_INFO] ||
1323 nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
1324 tb[NL80211_ATTR_STA_INFO], policy))
1325 return NL_SKIP;
1326 if (!sinfo[NL80211_STA_INFO_SIGNAL])
1327 return NL_SKIP;
1328
1329 sig_change->current_signal =
1330 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
1331
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001332 if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
1333 sig_change->avg_signal =
1334 (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
1335 else
1336 sig_change->avg_signal = 0;
1337
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07001338 if (sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG])
1339 sig_change->avg_beacon_signal =
1340 (s8)
1341 nla_get_u8(sinfo[NL80211_STA_INFO_BEACON_SIGNAL_AVG]);
1342 else
1343 sig_change->avg_beacon_signal = 0;
1344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345 if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
1346 if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
1347 sinfo[NL80211_STA_INFO_TX_BITRATE],
1348 rate_policy)) {
1349 sig_change->current_txrate = 0;
1350 } else {
1351 if (rinfo[NL80211_RATE_INFO_BITRATE]) {
1352 sig_change->current_txrate =
1353 nla_get_u16(rinfo[
1354 NL80211_RATE_INFO_BITRATE]) * 100;
1355 }
1356 }
1357 }
1358
1359 return NL_SKIP;
1360}
1361
1362
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001363int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
1364 struct wpa_signal_info *sig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365{
1366 struct nl_msg *msg;
1367
1368 sig->current_signal = -9999;
1369 sig->current_txrate = 0;
1370
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001371 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
1372 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
1373 nlmsg_free(msg);
1374 return -ENOBUFS;
1375 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376
1377 return send_and_recv_msgs(drv, msg, get_link_signal, sig);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378}
1379
1380
1381static int get_link_noise(struct nl_msg *msg, void *arg)
1382{
1383 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1384 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1385 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
1386 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
1387 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
1388 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
1389 };
1390 struct wpa_signal_info *sig_change = arg;
1391
1392 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1393 genlmsg_attrlen(gnlh, 0), NULL);
1394
1395 if (!tb[NL80211_ATTR_SURVEY_INFO]) {
1396 wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
1397 return NL_SKIP;
1398 }
1399
1400 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
1401 tb[NL80211_ATTR_SURVEY_INFO],
1402 survey_policy)) {
1403 wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
1404 "attributes!");
1405 return NL_SKIP;
1406 }
1407
1408 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
1409 return NL_SKIP;
1410
1411 if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
1412 sig_change->frequency)
1413 return NL_SKIP;
1414
1415 if (!sinfo[NL80211_SURVEY_INFO_NOISE])
1416 return NL_SKIP;
1417
1418 sig_change->current_noise =
1419 (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
1420
1421 return NL_SKIP;
1422}
1423
1424
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001425int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
1426 struct wpa_signal_info *sig_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427{
1428 struct nl_msg *msg;
1429
1430 sig_change->current_noise = 9999;
1431 sig_change->frequency = drv->assoc_freq;
1432
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001433 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001434 return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001435}
1436
1437
1438static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
1439 void *handle)
1440{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001441 struct nl_cb *cb = eloop_ctx;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001442 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001444 wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001446 res = nl_recvmsgs(handle, cb);
Dmitry Shmidt71757432014-06-02 13:50:35 -07001447 if (res < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001448 wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
1449 __func__, res);
1450 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451}
1452
1453
1454/**
1455 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
1456 * @priv: driver_nl80211 private data
1457 * @alpha2_arg: country to which to switch to
1458 * Returns: 0 on success, -1 on failure
1459 *
1460 * This asks nl80211 to set the regulatory domain for given
1461 * country ISO / IEC alpha2.
1462 */
1463static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
1464{
1465 struct i802_bss *bss = priv;
1466 struct wpa_driver_nl80211_data *drv = bss->drv;
1467 char alpha2[3];
1468 struct nl_msg *msg;
1469
1470 msg = nlmsg_alloc();
1471 if (!msg)
1472 return -ENOMEM;
1473
1474 alpha2[0] = alpha2_arg[0];
1475 alpha2[1] = alpha2_arg[1];
1476 alpha2[2] = '\0';
1477
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001478 if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
1479 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
1480 nlmsg_free(msg);
1481 return -EINVAL;
1482 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001483 if (send_and_recv_msgs(drv, msg, NULL, NULL))
1484 return -EINVAL;
1485 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486}
1487
1488
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001489static int nl80211_get_country(struct nl_msg *msg, void *arg)
1490{
1491 char *alpha2 = arg;
1492 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
1493 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1494
1495 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1496 genlmsg_attrlen(gnlh, 0), NULL);
1497 if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
1498 wpa_printf(MSG_DEBUG, "nl80211: No country information available");
1499 return NL_SKIP;
1500 }
1501 os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
1502 return NL_SKIP;
1503}
1504
1505
1506static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
1507{
1508 struct i802_bss *bss = priv;
1509 struct wpa_driver_nl80211_data *drv = bss->drv;
1510 struct nl_msg *msg;
1511 int ret;
1512
1513 msg = nlmsg_alloc();
1514 if (!msg)
1515 return -ENOMEM;
1516
1517 nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
1518 alpha2[0] = '\0';
1519 ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
1520 if (!alpha2[0])
1521 ret = -1;
1522
1523 return ret;
1524}
1525
1526
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001527static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001528{
1529 int ret;
1530
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001531 global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1532 if (global->nl_cb == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1534 "callbacks");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001535 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001536 }
1537
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001538 global->nl = nl_create_handle(global->nl_cb, "nl");
1539 if (global->nl == NULL)
1540 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001542 global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
1543 if (global->nl80211_id < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001544 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1545 "found");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001546 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547 }
1548
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001549 global->nl_event = nl_create_handle(global->nl_cb, "event");
1550 if (global->nl_event == NULL)
1551 goto err;
1552
1553 ret = nl_get_multicast_id(global, "nl80211", "scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001555 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556 if (ret < 0) {
1557 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1558 "membership for scan events: %d (%s)",
1559 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001560 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001561 }
1562
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001563 ret = nl_get_multicast_id(global, "nl80211", "mlme");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001564 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001565 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566 if (ret < 0) {
1567 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1568 "membership for mlme events: %d (%s)",
1569 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001570 goto err;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001571 }
1572
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001573 ret = nl_get_multicast_id(global, "nl80211", "regulatory");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574 if (ret >= 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001575 ret = nl_socket_add_membership(global->nl_event, ret);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001576 if (ret < 0) {
1577 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1578 "membership for regulatory events: %d (%s)",
1579 ret, strerror(-ret));
1580 /* Continue without regulatory events */
1581 }
1582
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001583 ret = nl_get_multicast_id(global, "nl80211", "vendor");
1584 if (ret >= 0)
1585 ret = nl_socket_add_membership(global->nl_event, ret);
1586 if (ret < 0) {
1587 wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
1588 "membership for vendor events: %d (%s)",
1589 ret, strerror(-ret));
1590 /* Continue without vendor events */
1591 }
1592
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001593 nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1594 no_seq_check, NULL);
1595 nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1596 process_global_event, global);
1597
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001598 nl80211_register_eloop_read(&global->nl_event,
1599 wpa_driver_nl80211_event_receive,
1600 global->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001601
1602 return 0;
1603
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001604err:
1605 nl_destroy_handles(&global->nl_event);
1606 nl_destroy_handles(&global->nl);
1607 nl_cb_put(global->nl_cb);
1608 global->nl_cb = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609 return -1;
1610}
1611
1612
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001613static void nl80211_check_global(struct nl80211_global *global)
1614{
1615 struct nl_handle *handle;
1616 const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
1617 int ret;
1618 unsigned int i;
1619
1620 /*
1621 * Try to re-add memberships to handle case of cfg80211 getting reloaded
1622 * and all registration having been cleared.
1623 */
1624 handle = (void *) (((intptr_t) global->nl_event) ^
1625 ELOOP_SOCKET_INVALID);
1626
1627 for (i = 0; groups[i]; i++) {
1628 ret = nl_get_multicast_id(global, "nl80211", groups[i]);
1629 if (ret >= 0)
1630 ret = nl_socket_add_membership(handle, ret);
1631 if (ret < 0) {
1632 wpa_printf(MSG_INFO,
1633 "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
1634 groups[i], ret, strerror(-ret));
1635 }
1636 }
1637}
1638
1639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001640static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
1641{
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001642 struct wpa_driver_nl80211_data *drv = ctx;
1643
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001644 wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001645
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001646 /*
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001647 * rtnetlink ifdown handler will report interfaces other than the P2P
1648 * Device interface as disabled.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649 */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001650 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1651 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_DISABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001652}
1653
1654
1655static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
1656{
1657 struct wpa_driver_nl80211_data *drv = ctx;
1658 wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001659 if (i802_set_iface_flags(drv->first_bss, 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660 wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
1661 "after rfkill unblock");
1662 return;
1663 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001664
1665 if (is_p2p_net_interface(drv->nlmode))
1666 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
1667
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001668 /*
1669 * rtnetlink ifup handler will report interfaces other than the P2P
1670 * Device interface as enabled.
1671 */
1672 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
1673 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674}
1675
1676
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001677static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
1678 void *eloop_ctx,
1679 void *handle)
1680{
1681 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1682 u8 data[2048];
1683 struct msghdr msg;
1684 struct iovec entry;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001685 u8 control[512];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001686 struct cmsghdr *cmsg;
1687 int res, found_ee = 0, found_wifi = 0, acked = 0;
1688 union wpa_event_data event;
1689
1690 memset(&msg, 0, sizeof(msg));
1691 msg.msg_iov = &entry;
1692 msg.msg_iovlen = 1;
1693 entry.iov_base = data;
1694 entry.iov_len = sizeof(data);
1695 msg.msg_control = &control;
1696 msg.msg_controllen = sizeof(control);
1697
1698 res = recvmsg(sock, &msg, MSG_ERRQUEUE);
1699 /* if error or not fitting 802.3 header, return */
1700 if (res < 14)
1701 return;
1702
1703 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1704 {
1705 if (cmsg->cmsg_level == SOL_SOCKET &&
1706 cmsg->cmsg_type == SCM_WIFI_STATUS) {
1707 int *ack;
1708
1709 found_wifi = 1;
1710 ack = (void *)CMSG_DATA(cmsg);
1711 acked = *ack;
1712 }
1713
1714 if (cmsg->cmsg_level == SOL_PACKET &&
1715 cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
1716 struct sock_extended_err *err =
1717 (struct sock_extended_err *)CMSG_DATA(cmsg);
1718
1719 if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
1720 found_ee = 1;
1721 }
1722 }
1723
1724 if (!found_ee || !found_wifi)
1725 return;
1726
1727 memset(&event, 0, sizeof(event));
1728 event.eapol_tx_status.dst = data;
1729 event.eapol_tx_status.data = data + 14;
1730 event.eapol_tx_status.data_len = res - 14;
1731 event.eapol_tx_status.ack = acked;
1732 wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
1733}
1734
1735
1736static int nl80211_init_bss(struct i802_bss *bss)
1737{
1738 bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1739 if (!bss->nl_cb)
1740 return -1;
1741
1742 nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
1743 no_seq_check, NULL);
1744 nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
1745 process_bss_event, bss);
1746
1747 return 0;
1748}
1749
1750
1751static void nl80211_destroy_bss(struct i802_bss *bss)
1752{
1753 nl_cb_put(bss->nl_cb);
1754 bss->nl_cb = NULL;
1755}
1756
1757
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001758static void
1759wpa_driver_nl80211_drv_init_rfkill(struct wpa_driver_nl80211_data *drv)
1760{
1761 struct rfkill_config *rcfg;
1762
1763 if (drv->rfkill)
1764 return;
1765
1766 rcfg = os_zalloc(sizeof(*rcfg));
1767 if (!rcfg)
1768 return;
1769
1770 rcfg->ctx = drv;
1771
1772 /* rfkill uses netdev sysfs for initialization. However, P2P Device is
1773 * not associated with a netdev, so use the name of some other interface
1774 * sharing the same wiphy as the P2P Device interface.
1775 *
1776 * Note: This is valid, as a P2P Device interface is always dynamically
1777 * created and is created only once another wpa_s interface was added.
1778 */
1779 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) {
1780 struct nl80211_global *global = drv->global;
1781 struct wpa_driver_nl80211_data *tmp1;
1782
1783 dl_list_for_each(tmp1, &global->interfaces,
1784 struct wpa_driver_nl80211_data, list) {
1785 if (drv == tmp1 || drv->wiphy_idx != tmp1->wiphy_idx ||
1786 !tmp1->rfkill)
1787 continue;
1788
1789 wpa_printf(MSG_DEBUG,
1790 "nl80211: Use (%s) to initialize P2P Device rfkill",
1791 tmp1->first_bss->ifname);
1792 os_strlcpy(rcfg->ifname, tmp1->first_bss->ifname,
1793 sizeof(rcfg->ifname));
1794 break;
1795 }
1796 } else {
1797 os_strlcpy(rcfg->ifname, drv->first_bss->ifname,
1798 sizeof(rcfg->ifname));
1799 }
1800
1801 rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
1802 rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
1803 drv->rfkill = rfkill_init(rcfg);
1804 if (!drv->rfkill) {
1805 wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
1806 os_free(rcfg);
1807 }
1808}
1809
1810
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001811static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
1812 void *global_priv, int hostapd,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001813 const u8 *set_addr,
1814 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001815{
1816 struct wpa_driver_nl80211_data *drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001817 struct i802_bss *bss;
1818
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001819 if (global_priv == NULL)
1820 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821 drv = os_zalloc(sizeof(*drv));
1822 if (drv == NULL)
1823 return NULL;
1824 drv->global = global_priv;
1825 drv->ctx = ctx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001826 drv->hostapd = !!hostapd;
1827 drv->eapol_sock = -1;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001828
1829 /*
1830 * There is no driver capability flag for this, so assume it is
1831 * supported and disable this on first attempt to use if the driver
1832 * rejects the command due to missing support.
1833 */
1834 drv->set_rekey_offload = 1;
1835
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001836 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
1837 drv->if_indices = drv->default_if_indices;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001838 drv->if_indices_reason = drv->default_if_indices_reason;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001839
1840 drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
1841 if (!drv->first_bss) {
1842 os_free(drv);
1843 return NULL;
1844 }
1845 bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 bss->drv = drv;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001847 bss->ctx = ctx;
1848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001849 os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
1850 drv->monitor_ifidx = -1;
1851 drv->monitor_sock = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001852 drv->eapol_tx_sock = -1;
1853 drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001855 if (nl80211_init_bss(bss))
1856 goto failed;
1857
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001858 if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001859 goto failed;
1860
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001861 drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
1862 if (drv->eapol_tx_sock < 0)
1863 goto failed;
1864
1865 if (drv->data_tx_status) {
1866 int enabled = 1;
1867
1868 if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
1869 &enabled, sizeof(enabled)) < 0) {
1870 wpa_printf(MSG_DEBUG,
1871 "nl80211: wifi status sockopt failed\n");
1872 drv->data_tx_status = 0;
1873 if (!drv->use_monitor)
1874 drv->capa.flags &=
1875 ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
1876 } else {
1877 eloop_register_read_sock(drv->eapol_tx_sock,
1878 wpa_driver_nl80211_handle_eapol_tx_status,
1879 drv, NULL);
1880 }
1881 }
1882
1883 if (drv->global) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001884 nl80211_check_global(drv->global);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 dl_list_add(&drv->global->interfaces, &drv->list);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001886 drv->in_interface_list = 1;
1887 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888
1889 return bss;
1890
1891failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001892 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001893 return NULL;
1894}
1895
1896
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001897/**
1898 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1899 * @ctx: context to be used when calling wpa_supplicant functions,
1900 * e.g., wpa_supplicant_event()
1901 * @ifname: interface name, e.g., wlan0
1902 * @global_priv: private driver global data from global_init()
1903 * Returns: Pointer to private data, %NULL on failure
1904 */
1905static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
1906 void *global_priv)
1907{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001908 return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
1909 NULL);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001910}
1911
1912
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001913static int nl80211_register_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001914 struct nl_handle *nl_handle,
1915 u16 type, const u8 *match, size_t match_len)
1916{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001917 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001919 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001920 char buf[30];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001922 buf[0] = '\0';
1923 wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001924 wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
1925 type, fc2str(type), nl_handle, buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001927 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
1928 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
1929 nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
1930 nlmsg_free(msg);
1931 return -1;
1932 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001933
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001934 ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935 if (ret) {
1936 wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
1937 "failed (type=%u): ret=%d (%s)",
1938 type, ret, strerror(-ret));
1939 wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
1940 match, match_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001942 return ret;
1943}
1944
1945
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001946static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
1947{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001948 if (bss->nl_mgmt) {
1949 wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
1950 "already on! (nl_mgmt=%p)", bss->nl_mgmt);
1951 return -1;
1952 }
1953
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001954 bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001955 if (bss->nl_mgmt == NULL)
1956 return -1;
1957
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001958 return 0;
1959}
1960
1961
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001962static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
1963{
1964 nl80211_register_eloop_read(&bss->nl_mgmt,
1965 wpa_driver_nl80211_event_receive,
1966 bss->nl_cb);
1967}
1968
1969
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001970static int nl80211_register_action_frame(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971 const u8 *match, size_t match_len)
1972{
1973 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001974 return nl80211_register_frame(bss, bss->nl_mgmt,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001975 type, match, match_len);
1976}
1977
1978
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001979static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001981 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001982 int ret = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001983
1984 if (nl80211_alloc_mgmt_handle(bss))
1985 return -1;
1986 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
1987 "handle %p", bss->nl_mgmt);
1988
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001989 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
1990 u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
1991
1992 /* register for any AUTH message */
1993 nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
1994 }
1995
Dmitry Shmidt051af732013-10-22 13:52:46 -07001996#ifdef CONFIG_INTERWORKING
1997 /* QoS Map Configure */
1998 if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001999 ret = -1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07002000#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002001#if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 /* GAS Initial Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002003 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002004 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005 /* GAS Initial Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002006 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002007 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002008 /* GAS Comeback Request */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002009 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002010 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002011 /* GAS Comeback Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002012 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002013 ret = -1;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002014 /* Protected GAS Initial Request */
2015 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
2016 ret = -1;
2017 /* Protected GAS Initial Response */
2018 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
2019 ret = -1;
2020 /* Protected GAS Comeback Request */
2021 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
2022 ret = -1;
2023 /* Protected GAS Comeback Response */
2024 if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
2025 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002026#endif /* CONFIG_P2P || CONFIG_INTERWORKING */
2027#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002028 /* P2P Public Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002029 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030 (u8 *) "\x04\x09\x50\x6f\x9a\x09",
2031 6) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002032 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033 /* P2P Action */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002034 if (nl80211_register_action_frame(bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035 (u8 *) "\x7f\x50\x6f\x9a\x09",
2036 5) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002037 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002038#endif /* CONFIG_P2P */
2039#ifdef CONFIG_IEEE80211W
2040 /* SA Query Response */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002041 if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002042 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002044#ifdef CONFIG_TDLS
2045 if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
2046 /* TDLS Discovery Response */
2047 if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
2048 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002049 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002050 }
2051#endif /* CONFIG_TDLS */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002052#ifdef CONFIG_FST
2053 /* FST Action frames */
2054 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2055 ret = -1;
2056#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057
2058 /* FT Action frames */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002059 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002060 ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002061 else
2062 drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
2063 WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
2064
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002065 /* WNM - BSS Transition Management Request */
2066 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002067 ret = -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002068 /* WNM-Sleep Mode Response */
2069 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002070 ret = -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002071
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002072#ifdef CONFIG_HS20
2073 /* WNM-Notification */
2074 if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002075 ret = -1;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002076#endif /* CONFIG_HS20 */
2077
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002078 /* WMM-AC ADDTS Response */
2079 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
2080 ret = -1;
2081
2082 /* WMM-AC DELTS */
2083 if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
2084 ret = -1;
2085
2086 /* Radio Measurement - Neighbor Report Response */
2087 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
2088 ret = -1;
2089
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002090 /* Radio Measurement - Radio Measurement Request */
2091 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x00", 2) < 0)
2092 ret = -1;
2093
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002094 /* Radio Measurement - Link Measurement Request */
2095 if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
2096 (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
2097 ret = -1;
2098
2099 nl80211_mgmt_handle_register_eloop(bss);
2100
2101 return ret;
2102}
2103
2104
2105static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
2106{
2107 int ret = 0;
2108
2109 if (nl80211_alloc_mgmt_handle(bss))
2110 return -1;
2111
2112 wpa_printf(MSG_DEBUG,
2113 "nl80211: Subscribe to mgmt frames with mesh handle %p",
2114 bss->nl_mgmt);
2115
2116 /* Auth frames for mesh SAE */
2117 if (nl80211_register_frame(bss, bss->nl_mgmt,
2118 (WLAN_FC_TYPE_MGMT << 2) |
2119 (WLAN_FC_STYPE_AUTH << 4),
2120 NULL, 0) < 0)
2121 ret = -1;
2122
2123 /* Mesh peering open */
2124 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
2125 ret = -1;
2126 /* Mesh peering confirm */
2127 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
2128 ret = -1;
2129 /* Mesh peering close */
2130 if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
2131 ret = -1;
2132
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002133 nl80211_mgmt_handle_register_eloop(bss);
2134
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002135 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002136}
2137
2138
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002139static int nl80211_register_spurious_class3(struct i802_bss *bss)
2140{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002141 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002142 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002143
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002144 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
2145 ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002146 if (ret) {
2147 wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
2148 "failed: ret=%d (%s)",
2149 ret, strerror(-ret));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002150 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002151 return ret;
2152}
2153
2154
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002155static int nl80211_action_subscribe_ap(struct i802_bss *bss)
2156{
2157 int ret = 0;
2158
2159 /* Public Action frames */
2160 if (nl80211_register_action_frame(bss, (u8 *) "\x04", 1) < 0)
2161 ret = -1;
2162 /* RRM Measurement Report */
2163 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x01", 2) < 0)
2164 ret = -1;
2165 /* RRM Neighbor Report Request */
2166 if (nl80211_register_action_frame(bss, (u8 *) "\x05\x04", 2) < 0)
2167 ret = -1;
2168 /* FT Action frames */
2169 if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
2170 ret = -1;
2171#ifdef CONFIG_IEEE80211W
2172 /* SA Query */
2173 if (nl80211_register_action_frame(bss, (u8 *) "\x08", 1) < 0)
2174 ret = -1;
2175#endif /* CONFIG_IEEE80211W */
2176 /* Protected Dual of Public Action */
2177 if (nl80211_register_action_frame(bss, (u8 *) "\x09", 1) < 0)
2178 ret = -1;
2179 /* WNM */
2180 if (nl80211_register_action_frame(bss, (u8 *) "\x0a", 1) < 0)
2181 ret = -1;
2182 /* WMM */
2183 if (nl80211_register_action_frame(bss, (u8 *) "\x11", 1) < 0)
2184 ret = -1;
2185#ifdef CONFIG_FST
2186 /* FST Action frames */
2187 if (nl80211_register_action_frame(bss, (u8 *) "\x12", 1) < 0)
2188 ret = -1;
2189#endif /* CONFIG_FST */
2190 /* Vendor-specific */
2191 if (nl80211_register_action_frame(bss, (u8 *) "\x7f", 1) < 0)
2192 ret = -1;
2193
2194 return ret;
2195}
2196
2197
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002198static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
2199{
2200 static const int stypes[] = {
2201 WLAN_FC_STYPE_AUTH,
2202 WLAN_FC_STYPE_ASSOC_REQ,
2203 WLAN_FC_STYPE_REASSOC_REQ,
2204 WLAN_FC_STYPE_DISASSOC,
2205 WLAN_FC_STYPE_DEAUTH,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002206 WLAN_FC_STYPE_PROBE_REQ,
2207/* Beacon doesn't work as mac80211 doesn't currently allow
2208 * it, but it wouldn't really be the right thing anyway as
2209 * it isn't per interface ... maybe just dump the scan
2210 * results periodically for OLBC?
2211 */
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07002212 /* WLAN_FC_STYPE_BEACON, */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002213 };
2214 unsigned int i;
2215
2216 if (nl80211_alloc_mgmt_handle(bss))
2217 return -1;
2218 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2219 "handle %p", bss->nl_mgmt);
2220
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002221 for (i = 0; i < ARRAY_SIZE(stypes); i++) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002222 if (nl80211_register_frame(bss, bss->nl_mgmt,
2223 (WLAN_FC_TYPE_MGMT << 2) |
2224 (stypes[i] << 4),
2225 NULL, 0) < 0) {
2226 goto out_err;
2227 }
2228 }
2229
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002230 if (nl80211_action_subscribe_ap(bss))
2231 goto out_err;
2232
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002233 if (nl80211_register_spurious_class3(bss))
2234 goto out_err;
2235
2236 if (nl80211_get_wiphy_data_ap(bss) == NULL)
2237 goto out_err;
2238
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002239 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002240 return 0;
2241
2242out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002243 nl_destroy_handles(&bss->nl_mgmt);
2244 return -1;
2245}
2246
2247
2248static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
2249{
2250 if (nl80211_alloc_mgmt_handle(bss))
2251 return -1;
2252 wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
2253 "handle %p (device SME)", bss->nl_mgmt);
2254
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002255 if (nl80211_action_subscribe_ap(bss))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002256 goto out_err;
2257
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002258 nl80211_mgmt_handle_register_eloop(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002259 return 0;
2260
2261out_err:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002262 nl_destroy_handles(&bss->nl_mgmt);
2263 return -1;
2264}
2265
2266
2267static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
2268{
2269 if (bss->nl_mgmt == NULL)
2270 return;
2271 wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
2272 "(%s)", bss->nl_mgmt, reason);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002273 nl80211_destroy_eloop_handle(&bss->nl_mgmt);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002274
2275 nl80211_put_wiphy_data_ap(bss);
2276}
2277
2278
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002279static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
2280{
2281 wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
2282}
2283
2284
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002285static void nl80211_del_p2pdev(struct i802_bss *bss)
2286{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002287 struct nl_msg *msg;
2288 int ret;
2289
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002290 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
2291 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002292
2293 wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
2294 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002295 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002296}
2297
2298
2299static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
2300{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002301 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002302 int ret;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002303
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002304 msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
2305 NL80211_CMD_STOP_P2P_DEVICE);
2306 ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002307
2308 wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
2309 start ? "Start" : "Stop",
2310 bss->ifname, (long long unsigned int) bss->wdev_id,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002311 strerror(-ret));
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002312 return ret;
2313}
2314
2315
2316static int i802_set_iface_flags(struct i802_bss *bss, int up)
2317{
2318 enum nl80211_iftype nlmode;
2319
2320 nlmode = nl80211_get_ifmode(bss);
2321 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2322 return linux_set_iface_flags(bss->drv->global->ioctl_sock,
2323 bss->ifname, up);
2324 }
2325
2326 /* P2P Device has start/stop which is equivalent */
2327 return nl80211_set_p2pdev(bss, up);
2328}
2329
2330
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002331#ifdef CONFIG_TESTING_OPTIONS
2332static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
2333{
2334 /* struct wpa_driver_nl80211_data *drv = arg; */
2335 struct nlattr *tb[NL80211_ATTR_MAX + 1];
2336 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2337
2338
2339 wpa_printf(MSG_DEBUG,
2340 "nl80211: QCA vendor test command response received");
2341
2342 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2343 genlmsg_attrlen(gnlh, 0), NULL);
2344 if (!tb[NL80211_ATTR_VENDOR_DATA]) {
2345 wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
2346 return NL_SKIP;
2347 }
2348
2349 wpa_hexdump(MSG_DEBUG,
2350 "nl80211: Received QCA vendor test command response",
2351 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
2352 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
2353
2354 return NL_SKIP;
2355}
2356#endif /* CONFIG_TESTING_OPTIONS */
2357
2358
2359static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
2360{
2361#ifdef CONFIG_TESTING_OPTIONS
2362 struct nl_msg *msg;
2363 struct nlattr *params;
2364 int ret;
2365
2366 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2367 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2368 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2369 QCA_NL80211_VENDOR_SUBCMD_TEST) ||
2370 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
2371 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
2372 nlmsg_free(msg);
2373 return;
2374 }
2375 nla_nest_end(msg, params);
2376
2377 ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
2378 wpa_printf(MSG_DEBUG,
2379 "nl80211: QCA vendor test command returned %d (%s)",
2380 ret, strerror(-ret));
2381#endif /* CONFIG_TESTING_OPTIONS */
2382}
2383
2384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385static int
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002386wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002387 const u8 *set_addr, int first,
2388 const char *driver_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002390 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002391 int send_rfkill_event = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002392 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393
2394 drv->ifindex = if_nametoindex(bss->ifname);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002395 bss->ifindex = drv->ifindex;
2396 bss->wdev_id = drv->global->if_add_wdevid;
2397 bss->wdev_id_set = drv->global->if_add_wdevid_set;
2398
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002399 bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
2400 bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002401 drv->global->if_add_wdevid_set = 0;
2402
Dmitry Shmidt03658832014-08-13 11:03:49 -07002403 if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2404 bss->static_ap = 1;
2405
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002406 if (first &&
2407 nl80211_get_ifmode(bss) != NL80211_IFTYPE_P2P_DEVICE &&
2408 linux_iface_up(drv->global->ioctl_sock, bss->ifname) > 0)
2409 drv->start_iface_up = 1;
2410
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002411 if (wpa_driver_nl80211_capa(drv))
2412 return -1;
2413
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002414 if (driver_params && nl80211_set_param(bss, driver_params) < 0)
2415 return -1;
2416
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002417 wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
2418 bss->ifname, drv->phyname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002419
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002420 if (set_addr &&
2421 (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
2422 linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2423 set_addr)))
2424 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002425
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002426 if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
2427 drv->start_mode_ap = 1;
2428
Dmitry Shmidt03658832014-08-13 11:03:49 -07002429 if (drv->hostapd || bss->static_ap)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002430 nlmode = NL80211_IFTYPE_AP;
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002431 else if (bss->if_dynamic ||
2432 nl80211_get_ifmode(bss) == NL80211_IFTYPE_MESH_POINT)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002433 nlmode = nl80211_get_ifmode(bss);
2434 else
2435 nlmode = NL80211_IFTYPE_STATION;
2436
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002437 if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002438 wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002439 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002441
Dmitry Shmidt98660862014-03-11 17:26:21 -07002442 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002443 nl80211_get_macaddr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002444
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002445 wpa_driver_nl80211_drv_init_rfkill(drv);
2446
Dmitry Shmidt98660862014-03-11 17:26:21 -07002447 if (!rfkill_is_blocked(drv->rfkill)) {
2448 int ret = i802_set_iface_flags(bss, 1);
2449 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002450 wpa_printf(MSG_ERROR, "nl80211: Could not set "
2451 "interface '%s' UP", bss->ifname);
Dmitry Shmidt98660862014-03-11 17:26:21 -07002452 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002454
2455 if (is_p2p_net_interface(nlmode))
2456 nl80211_disable_11b_rates(bss->drv,
2457 bss->drv->ifindex, 1);
2458
Dmitry Shmidt98660862014-03-11 17:26:21 -07002459 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
2460 return ret;
2461 } else {
2462 wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
2463 "interface '%s' due to rfkill", bss->ifname);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002464 if (nlmode != NL80211_IFTYPE_P2P_DEVICE)
2465 drv->if_disabled = 1;
2466
Dmitry Shmidt98660862014-03-11 17:26:21 -07002467 send_rfkill_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002468 }
2469
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002470 if (!drv->hostapd && nlmode != NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002471 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
2472 1, IF_OPER_DORMANT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002474 if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
2475 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2476 bss->addr))
2477 return -1;
2478 os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
2479 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002480
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002481 if (send_rfkill_event) {
2482 eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
2483 drv, drv->ctx);
2484 }
2485
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002486 if (drv->vendor_cmd_test_avail)
2487 qca_vendor_test(drv);
2488
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002489 return 0;
2490}
2491
2492
2493static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
2494{
2495 struct nl_msg *msg;
2496
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002497 wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
2498 drv->ifindex);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002499 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002501}
2502
2503
2504/**
2505 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002506 * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002507 *
2508 * Shut down driver interface and processing of driver events. Free
2509 * private data buffer if one was allocated in wpa_driver_nl80211_init().
2510 */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002511static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002514 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002516 wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
2517 bss->ifname, drv->disabled_11b_rates);
2518
Dmitry Shmidt04949592012-07-19 12:16:46 -07002519 bss->in_deinit = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002520 if (drv->data_tx_status)
2521 eloop_unregister_read_sock(drv->eapol_tx_sock);
2522 if (drv->eapol_tx_sock >= 0)
2523 close(drv->eapol_tx_sock);
2524
2525 if (bss->nl_preq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002526 wpa_driver_nl80211_probe_req_report(bss, 0);
2527 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002528 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
2529 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2531 "interface %s from bridge %s: %s",
2532 bss->ifname, bss->brname, strerror(errno));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002533 if (drv->rtnl_sk)
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07002534 nl80211_handle_destroy(drv->rtnl_sk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535 }
2536 if (bss->added_bridge) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002537 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
2538 0) < 0)
2539 wpa_printf(MSG_INFO,
2540 "nl80211: Could not set bridge %s down",
2541 bss->brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002542 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002543 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
2544 "bridge %s: %s",
2545 bss->brname, strerror(errno));
2546 }
2547
2548 nl80211_remove_monitor_interface(drv);
2549
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002550 if (is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002551 wpa_driver_nl80211_del_beacon(drv);
2552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002553 if (drv->eapol_sock >= 0) {
2554 eloop_unregister_read_sock(drv->eapol_sock);
2555 close(drv->eapol_sock);
2556 }
2557
2558 if (drv->if_indices != drv->default_if_indices)
2559 os_free(drv->if_indices);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002560
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002561 if (drv->if_indices_reason != drv->default_if_indices_reason)
2562 os_free(drv->if_indices_reason);
2563
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002564 if (drv->disabled_11b_rates)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
2566
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002567 netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
2568 IF_OPER_UP);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002569 eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002570 rfkill_deinit(drv->rfkill);
2571
2572 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
2573
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002574 if (!drv->start_iface_up)
2575 (void) i802_set_iface_flags(bss, 0);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002576
2577 if (drv->addr_changed) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002578 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
2579 0) < 0) {
2580 wpa_printf(MSG_DEBUG,
2581 "nl80211: Could not set interface down to restore permanent MAC address");
2582 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002583 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
2584 drv->perm_addr) < 0) {
2585 wpa_printf(MSG_DEBUG,
2586 "nl80211: Could not restore permanent MAC address");
2587 }
2588 }
2589
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002590 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002591 if (!drv->hostapd || !drv->start_mode_ap)
2592 wpa_driver_nl80211_set_mode(bss,
2593 NL80211_IFTYPE_STATION);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002594 nl80211_mgmt_unsubscribe(bss, "deinit");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002595 } else {
2596 nl80211_mgmt_unsubscribe(bss, "deinit");
2597 nl80211_del_p2pdev(bss);
2598 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002599
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002600 nl80211_destroy_bss(drv->first_bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002601
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002602 os_free(drv->filter_ssids);
2603
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002604 os_free(drv->auth_ie);
2605
2606 if (drv->in_interface_list)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002607 dl_list_del(&drv->list);
2608
Dmitry Shmidt444d5672013-04-01 13:08:44 -07002609 os_free(drv->extended_capa);
2610 os_free(drv->extended_capa_mask);
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07002611 for (i = 0; i < drv->num_iface_ext_capa; i++) {
2612 os_free(drv->iface_ext_capa[i].ext_capa);
2613 os_free(drv->iface_ext_capa[i].ext_capa_mask);
2614 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002615 os_free(drv->first_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002616 os_free(drv);
2617}
2618
2619
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002620static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
2621{
2622 switch (alg) {
2623 case WPA_ALG_WEP:
2624 if (key_len == 5)
2625 return WLAN_CIPHER_SUITE_WEP40;
2626 return WLAN_CIPHER_SUITE_WEP104;
2627 case WPA_ALG_TKIP:
2628 return WLAN_CIPHER_SUITE_TKIP;
2629 case WPA_ALG_CCMP:
2630 return WLAN_CIPHER_SUITE_CCMP;
2631 case WPA_ALG_GCMP:
2632 return WLAN_CIPHER_SUITE_GCMP;
2633 case WPA_ALG_CCMP_256:
2634 return WLAN_CIPHER_SUITE_CCMP_256;
2635 case WPA_ALG_GCMP_256:
2636 return WLAN_CIPHER_SUITE_GCMP_256;
2637 case WPA_ALG_IGTK:
2638 return WLAN_CIPHER_SUITE_AES_CMAC;
2639 case WPA_ALG_BIP_GMAC_128:
2640 return WLAN_CIPHER_SUITE_BIP_GMAC_128;
2641 case WPA_ALG_BIP_GMAC_256:
2642 return WLAN_CIPHER_SUITE_BIP_GMAC_256;
2643 case WPA_ALG_BIP_CMAC_256:
2644 return WLAN_CIPHER_SUITE_BIP_CMAC_256;
2645 case WPA_ALG_SMS4:
2646 return WLAN_CIPHER_SUITE_SMS4;
2647 case WPA_ALG_KRK:
2648 return WLAN_CIPHER_SUITE_KRK;
2649 case WPA_ALG_NONE:
2650 case WPA_ALG_PMK:
2651 wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
2652 alg);
2653 return 0;
2654 }
2655
2656 wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
2657 alg);
2658 return 0;
2659}
2660
2661
2662static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
2663{
2664 switch (cipher) {
2665 case WPA_CIPHER_CCMP_256:
2666 return WLAN_CIPHER_SUITE_CCMP_256;
2667 case WPA_CIPHER_GCMP_256:
2668 return WLAN_CIPHER_SUITE_GCMP_256;
2669 case WPA_CIPHER_CCMP:
2670 return WLAN_CIPHER_SUITE_CCMP;
2671 case WPA_CIPHER_GCMP:
2672 return WLAN_CIPHER_SUITE_GCMP;
2673 case WPA_CIPHER_TKIP:
2674 return WLAN_CIPHER_SUITE_TKIP;
2675 case WPA_CIPHER_WEP104:
2676 return WLAN_CIPHER_SUITE_WEP104;
2677 case WPA_CIPHER_WEP40:
2678 return WLAN_CIPHER_SUITE_WEP40;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002679 case WPA_CIPHER_GTK_NOT_USED:
2680 return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002681 }
2682
2683 return 0;
2684}
2685
2686
2687static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
2688 int max_suites)
2689{
2690 int num_suites = 0;
2691
2692 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
2693 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
2694 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
2695 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
2696 if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
2697 suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
2698 if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
2699 suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
2700 if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
2701 suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
2702 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
2703 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
2704 if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
2705 suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
2706
2707 return num_suites;
2708}
2709
2710
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002711#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002712static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
2713 const u8 *key, size_t key_len)
2714{
2715 struct nl_msg *msg;
2716 int ret;
2717
2718 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
2719 return 0;
2720
2721 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
2722 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
2723 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
2724 QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
2725 nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
2726 nl80211_nlmsg_clear(msg);
2727 nlmsg_free(msg);
2728 return -1;
2729 }
2730 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
2731 if (ret) {
2732 wpa_printf(MSG_DEBUG,
2733 "nl80211: Key management set key failed: ret=%d (%s)",
2734 ret, strerror(-ret));
2735 }
2736
2737 return ret;
2738}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002739#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002740
2741
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002742static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002743 enum wpa_alg alg, const u8 *addr,
2744 int key_idx, int set_tx,
2745 const u8 *seq, size_t seq_len,
2746 const u8 *key, size_t key_len)
2747{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002748 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002749 int ifindex;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002750 struct nl_msg *msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002751 int ret;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002752 int tdls = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002753
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002754 /* Ignore for P2P Device */
2755 if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
2756 return 0;
2757
2758 ifindex = if_nametoindex(ifname);
2759 wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760 "set_tx=%d seq_len=%lu key_len=%lu",
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002761 __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762 (unsigned long) seq_len, (unsigned long) key_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002763#ifdef CONFIG_TDLS
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002764 if (key_idx == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002765 key_idx = 0;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002766 tdls = 1;
2767 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002768#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002769
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002770#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002771 if (alg == WPA_ALG_PMK &&
2772 (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
2773 wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
2774 __func__);
2775 ret = issue_key_mgmt_set_key(drv, key, key_len);
2776 return ret;
2777 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002778#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002779
2780 if (alg == WPA_ALG_NONE) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002781 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
2782 if (!msg)
2783 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002784 } else {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002785 u32 suite;
2786
2787 suite = wpa_alg_to_cipher_suite(alg, key_len);
2788 if (!suite)
2789 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002790 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
2791 if (!msg ||
2792 nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002793 nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER, suite))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002794 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002795 wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796 }
2797
Dmitry Shmidt98660862014-03-11 17:26:21 -07002798 if (seq && seq_len) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002799 if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
2800 goto fail;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002801 wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
2802 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002803
2804 if (addr && !is_broadcast_ether_addr(addr)) {
2805 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002806 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
2807 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002808
2809 if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
2810 wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002811 if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
2812 NL80211_KEYTYPE_GROUP))
2813 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814 }
2815 } else if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002816 struct nlattr *types;
2817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818 wpa_printf(MSG_DEBUG, " broadcast key");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002819
2820 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002821 if (!types ||
2822 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2823 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002824 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002825 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002826 if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2827 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002828
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002829 ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002830 if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
2831 ret = 0;
2832 if (ret)
2833 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
2834 ret, strerror(-ret));
2835
2836 /*
2837 * If we failed or don't need to set the default TX key (below),
2838 * we're done here.
2839 */
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07002840 if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002842 if (is_ap_interface(drv->nlmode) && addr &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002843 !is_broadcast_ether_addr(addr))
2844 return ret;
2845
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002846 msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
2847 if (!msg ||
2848 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002849 nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
2850 alg == WPA_ALG_BIP_GMAC_128 ||
2851 alg == WPA_ALG_BIP_GMAC_256 ||
2852 alg == WPA_ALG_BIP_CMAC_256) ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002853 NL80211_ATTR_KEY_DEFAULT_MGMT :
2854 NL80211_ATTR_KEY_DEFAULT))
2855 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856 if (addr && is_broadcast_ether_addr(addr)) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002857 struct nlattr *types;
2858
2859 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002860 if (!types ||
2861 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
2862 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002863 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864 } else if (addr) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002865 struct nlattr *types;
2866
2867 types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002868 if (!types ||
2869 nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
2870 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07002871 nla_nest_end(msg, types);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002872 }
2873
2874 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2875 if (ret == -ENOENT)
2876 ret = 0;
2877 if (ret)
2878 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
2879 "err=%d %s)", ret, strerror(-ret));
2880 return ret;
2881
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002882fail:
2883 nl80211_nlmsg_clear(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002884 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885 return -ENOBUFS;
2886}
2887
2888
2889static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
2890 int key_idx, int defkey,
2891 const u8 *seq, size_t seq_len,
2892 const u8 *key, size_t key_len)
2893{
2894 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002895 u32 suite;
2896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897 if (!key_attr)
2898 return -1;
2899
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002900 suite = wpa_alg_to_cipher_suite(alg, key_len);
2901 if (!suite)
2902 return -1;
2903
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002904 if (defkey && alg == WPA_ALG_IGTK) {
2905 if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
2906 return -1;
2907 } else if (defkey) {
2908 if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
2909 return -1;
2910 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002912 if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07002913 nla_put_u32(msg, NL80211_KEY_CIPHER, suite) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002914 (seq && seq_len &&
2915 nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
2916 nla_put(msg, NL80211_KEY_DATA, key_len, key))
2917 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002918
2919 nla_nest_end(msg, key_attr);
2920
2921 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002922}
2923
2924
2925static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
2926 struct nl_msg *msg)
2927{
2928 int i, privacy = 0;
2929 struct nlattr *nl_keys, *nl_key;
2930
2931 for (i = 0; i < 4; i++) {
2932 if (!params->wep_key[i])
2933 continue;
2934 privacy = 1;
2935 break;
2936 }
2937 if (params->wps == WPS_MODE_PRIVACY)
2938 privacy = 1;
2939 if (params->pairwise_suite &&
2940 params->pairwise_suite != WPA_CIPHER_NONE)
2941 privacy = 1;
2942
2943 if (!privacy)
2944 return 0;
2945
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002946 if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
2947 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002948
2949 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
2950 if (!nl_keys)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002951 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002952
2953 for (i = 0; i < 4; i++) {
2954 if (!params->wep_key[i])
2955 continue;
2956
2957 nl_key = nla_nest_start(msg, i);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002958 if (!nl_key ||
2959 nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
2960 params->wep_key[i]) ||
2961 nla_put_u32(msg, NL80211_KEY_CIPHER,
2962 params->wep_key_len[i] == 5 ?
2963 WLAN_CIPHER_SUITE_WEP40 :
2964 WLAN_CIPHER_SUITE_WEP104) ||
2965 nla_put_u8(msg, NL80211_KEY_IDX, i) ||
2966 (i == params->wep_tx_keyidx &&
2967 nla_put_flag(msg, NL80211_KEY_DEFAULT)))
2968 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002969
2970 nla_nest_end(msg, nl_key);
2971 }
2972 nla_nest_end(msg, nl_keys);
2973
2974 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975}
2976
2977
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002978int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
2979 const u8 *addr, int cmd, u16 reason_code,
2980 int local_state_change)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002981{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002982 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002983 struct nl_msg *msg;
2984
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002985 if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
2986 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
2987 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
2988 (local_state_change &&
2989 nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
2990 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002991 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002992 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993
2994 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002995 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002996 wpa_dbg(drv->ctx, MSG_DEBUG,
2997 "nl80211: MLME command failed: reason=%u ret=%d (%s)",
2998 reason_code, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002999 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003000 return ret;
3001}
3002
3003
3004static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003005 int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003007 int ret;
3008
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003009 wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003010 nl80211_mark_disconnected(drv);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003011 /* Disconnect command doesn't need BSSID - it uses cached value */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003012 ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
3013 reason_code, 0);
3014 /*
3015 * For locally generated disconnect, supplicant already generates a
3016 * DEAUTH event, so ignore the event from NL80211.
3017 */
3018 drv->ignore_next_local_disconnect = ret == 0;
3019
3020 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003021}
3022
3023
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003024static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
3025 const u8 *addr, int reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003026{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003027 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003028 int ret;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003029
3030 if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
3031 nl80211_mark_disconnected(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003032 return nl80211_leave_ibss(drv, 1);
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003033 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003034 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003035 return wpa_driver_nl80211_disconnect(drv, reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003036 wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
3037 __func__, MAC2STR(addr), reason_code);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003038 nl80211_mark_disconnected(drv);
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003039 ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
3040 reason_code, 0);
3041 /*
3042 * For locally generated deauthenticate, supplicant already generates a
3043 * DEAUTH event, so ignore the event from NL80211.
3044 */
3045 drv->ignore_next_local_deauth = ret == 0;
3046 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047}
3048
3049
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003050static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
3051 struct wpa_driver_auth_params *params)
3052{
3053 int i;
3054
3055 drv->auth_freq = params->freq;
3056 drv->auth_alg = params->auth_alg;
3057 drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
3058 drv->auth_local_state_change = params->local_state_change;
3059 drv->auth_p2p = params->p2p;
3060
3061 if (params->bssid)
3062 os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
3063 else
3064 os_memset(drv->auth_bssid_, 0, ETH_ALEN);
3065
3066 if (params->ssid) {
3067 os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
3068 drv->auth_ssid_len = params->ssid_len;
3069 } else
3070 drv->auth_ssid_len = 0;
3071
3072
3073 os_free(drv->auth_ie);
3074 drv->auth_ie = NULL;
3075 drv->auth_ie_len = 0;
3076 if (params->ie) {
3077 drv->auth_ie = os_malloc(params->ie_len);
3078 if (drv->auth_ie) {
3079 os_memcpy(drv->auth_ie, params->ie, params->ie_len);
3080 drv->auth_ie_len = params->ie_len;
3081 }
3082 }
3083
3084 for (i = 0; i < 4; i++) {
3085 if (params->wep_key[i] && params->wep_key_len[i] &&
3086 params->wep_key_len[i] <= 16) {
3087 os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
3088 params->wep_key_len[i]);
3089 drv->auth_wep_key_len[i] = params->wep_key_len[i];
3090 } else
3091 drv->auth_wep_key_len[i] = 0;
3092 }
3093}
3094
3095
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003096static void nl80211_unmask_11b_rates(struct i802_bss *bss)
3097{
3098 struct wpa_driver_nl80211_data *drv = bss->drv;
3099
3100 if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
3101 return;
3102
3103 /*
3104 * Looks like we failed to unmask 11b rates previously. This could
3105 * happen, e.g., if the interface was down at the point in time when a
3106 * P2P group was terminated.
3107 */
3108 wpa_printf(MSG_DEBUG,
3109 "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
3110 bss->ifname);
3111 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
3112}
3113
3114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003115static int wpa_driver_nl80211_authenticate(
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003116 struct i802_bss *bss, struct wpa_driver_auth_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003117{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118 struct wpa_driver_nl80211_data *drv = bss->drv;
3119 int ret = -1, i;
3120 struct nl_msg *msg;
3121 enum nl80211_auth_type type;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003122 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003123 int count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003124 int is_retry;
3125
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003126 nl80211_unmask_11b_rates(bss);
3127
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003128 is_retry = drv->retry_auth;
3129 drv->retry_auth = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07003130 drv->ignore_deauth_event = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003131
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003132 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003133 os_memset(drv->auth_bssid, 0, ETH_ALEN);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003134 if (params->bssid)
3135 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
3136 else
3137 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003138 /* FIX: IBSS mode */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003139 nlmode = params->p2p ?
3140 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
3141 if (drv->nlmode != nlmode &&
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003142 wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003143 return -1;
3144
3145retry:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003146 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
3147 drv->ifindex);
3148
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003149 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
3150 if (!msg)
3151 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003152
3153 for (i = 0; i < 4; i++) {
3154 if (!params->wep_key[i])
3155 continue;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003156 wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003157 NULL, i,
3158 i == params->wep_tx_keyidx, NULL, 0,
3159 params->wep_key[i],
3160 params->wep_key_len[i]);
3161 if (params->wep_tx_keyidx != i)
3162 continue;
3163 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003164 params->wep_key[i], params->wep_key_len[i]))
3165 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003166 }
3167
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003168 if (params->bssid) {
3169 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3170 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003171 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
3172 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003173 }
3174 if (params->freq) {
3175 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003176 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
3177 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003178 }
3179 if (params->ssid) {
3180 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3181 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003182 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
3183 params->ssid))
3184 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003185 }
3186 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003187 if (params->ie &&
3188 nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
3189 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003190 if (params->auth_data) {
3191 wpa_hexdump(MSG_DEBUG, " * auth_data", params->auth_data,
3192 params->auth_data_len);
3193 if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->auth_data_len,
3194 params->auth_data))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003195 goto fail;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003196 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003197 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
3198 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
3199 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
3200 type = NL80211_AUTHTYPE_SHARED_KEY;
3201 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
3202 type = NL80211_AUTHTYPE_NETWORK_EAP;
3203 else if (params->auth_alg & WPA_AUTH_ALG_FT)
3204 type = NL80211_AUTHTYPE_FT;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003205 else if (params->auth_alg & WPA_AUTH_ALG_SAE)
3206 type = NL80211_AUTHTYPE_SAE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003207 else if (params->auth_alg & WPA_AUTH_ALG_FILS)
3208 type = NL80211_AUTHTYPE_FILS_SK;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003209 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003210 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003212 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
3213 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 if (params->local_state_change) {
3215 wpa_printf(MSG_DEBUG, " * Local state change only");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003216 if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
3217 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003218 }
3219
3220 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3221 msg = NULL;
3222 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003223 wpa_dbg(drv->ctx, MSG_DEBUG,
3224 "nl80211: MLME command failed (auth): ret=%d (%s)",
3225 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226 count++;
3227 if (ret == -EALREADY && count == 1 && params->bssid &&
3228 !params->local_state_change) {
3229 /*
3230 * mac80211 does not currently accept new
3231 * authentication if we are already authenticated. As a
3232 * workaround, force deauthentication and try again.
3233 */
3234 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
3235 "after forced deauthentication");
Dmitry Shmidt98660862014-03-11 17:26:21 -07003236 drv->ignore_deauth_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003237 wpa_driver_nl80211_deauthenticate(
3238 bss, params->bssid,
3239 WLAN_REASON_PREV_AUTH_NOT_VALID);
3240 nlmsg_free(msg);
3241 goto retry;
3242 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003243
3244 if (ret == -ENOENT && params->freq && !is_retry) {
3245 /*
3246 * cfg80211 has likely expired the BSS entry even
3247 * though it was previously available in our internal
3248 * BSS table. To recover quickly, start a single
3249 * channel scan on the specified channel.
3250 */
3251 struct wpa_driver_scan_params scan;
3252 int freqs[2];
3253
3254 os_memset(&scan, 0, sizeof(scan));
3255 scan.num_ssids = 1;
3256 if (params->ssid) {
3257 scan.ssids[0].ssid = params->ssid;
3258 scan.ssids[0].ssid_len = params->ssid_len;
3259 }
3260 freqs[0] = params->freq;
3261 freqs[1] = 0;
3262 scan.freqs = freqs;
3263 wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
3264 "channel scan to refresh cfg80211 BSS "
3265 "entry");
3266 ret = wpa_driver_nl80211_scan(bss, &scan);
3267 if (ret == 0) {
3268 nl80211_copy_auth_params(drv, params);
3269 drv->scan_for_auth = 1;
3270 }
3271 } else if (is_retry) {
3272 /*
3273 * Need to indicate this with an event since the return
3274 * value from the retry is not delivered to core code.
3275 */
3276 union wpa_event_data event;
3277 wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
3278 "failed");
3279 os_memset(&event, 0, sizeof(event));
3280 os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
3281 ETH_ALEN);
3282 wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
3283 &event);
3284 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003285 } else {
3286 wpa_printf(MSG_DEBUG,
3287 "nl80211: Authentication request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003288 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003289
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003290fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003291 nlmsg_free(msg);
3292 return ret;
3293}
3294
3295
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003296int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003297{
3298 struct wpa_driver_auth_params params;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003299 struct i802_bss *bss = drv->first_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003300 int i;
3301
3302 wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
3303
3304 os_memset(&params, 0, sizeof(params));
3305 params.freq = drv->auth_freq;
3306 params.auth_alg = drv->auth_alg;
3307 params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
3308 params.local_state_change = drv->auth_local_state_change;
3309 params.p2p = drv->auth_p2p;
3310
3311 if (!is_zero_ether_addr(drv->auth_bssid_))
3312 params.bssid = drv->auth_bssid_;
3313
3314 if (drv->auth_ssid_len) {
3315 params.ssid = drv->auth_ssid;
3316 params.ssid_len = drv->auth_ssid_len;
3317 }
3318
3319 params.ie = drv->auth_ie;
3320 params.ie_len = drv->auth_ie_len;
3321
3322 for (i = 0; i < 4; i++) {
3323 if (drv->auth_wep_key_len[i]) {
3324 params.wep_key[i] = drv->auth_wep_key[i];
3325 params.wep_key_len[i] = drv->auth_wep_key_len[i];
3326 }
3327 }
3328
3329 drv->retry_auth = 1;
3330 return wpa_driver_nl80211_authenticate(bss, &params);
3331}
3332
3333
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003334static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
3335 const void *data, size_t len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003336 int encrypt, int noack,
3337 unsigned int freq, int no_cck,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003338 int offchanok, unsigned int wait_time,
3339 const u16 *csa_offs,
3340 size_t csa_offs_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003341{
3342 struct wpa_driver_nl80211_data *drv = bss->drv;
3343 u64 cookie;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003344 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003345
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003346 if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
3347 freq = nl80211_get_assoc_freq(drv);
3348 wpa_printf(MSG_DEBUG,
3349 "nl80211: send_frame - Use assoc_freq=%u for IBSS",
3350 freq);
3351 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07003352 if (freq == 0) {
3353 wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
3354 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003355 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003356 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003357
Dmitry Shmidt56052862013-10-04 10:23:25 -07003358 if (drv->use_monitor) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003359 wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
Dmitry Shmidt56052862013-10-04 10:23:25 -07003360 freq, bss->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003361 return nl80211_send_monitor(drv, data, len, encrypt, noack);
Dmitry Shmidt56052862013-10-04 10:23:25 -07003362 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003363
Dmitry Shmidt56052862013-10-04 10:23:25 -07003364 wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
Dmitry Shmidt051af732013-10-22 13:52:46 -07003365 res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003366 &cookie, no_cck, noack, offchanok,
3367 csa_offs, csa_offs_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07003368 if (res == 0 && !noack) {
3369 const struct ieee80211_mgmt *mgmt;
3370 u16 fc;
3371
3372 mgmt = (const struct ieee80211_mgmt *) data;
3373 fc = le_to_host16(mgmt->frame_control);
3374 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3375 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
3376 wpa_printf(MSG_MSGDUMP,
3377 "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
3378 (long long unsigned int)
3379 drv->send_action_cookie,
3380 (long long unsigned int) cookie);
3381 drv->send_action_cookie = cookie;
3382 }
3383 }
3384
3385 return res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003386}
3387
3388
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003389static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
3390 size_t data_len, int noack,
3391 unsigned int freq, int no_cck,
3392 int offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003393 unsigned int wait_time,
3394 const u16 *csa_offs,
3395 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003396{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003397 struct wpa_driver_nl80211_data *drv = bss->drv;
3398 struct ieee80211_mgmt *mgmt;
3399 int encrypt = 1;
3400 u16 fc;
3401
3402 mgmt = (struct ieee80211_mgmt *) data;
3403 fc = le_to_host16(mgmt->frame_control);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07003404 wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
3405 " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
3406 MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
3407 fc, fc2str(fc), drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003408
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003409 if ((is_sta_interface(drv->nlmode) ||
3410 drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003411 WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3412 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
3413 /*
3414 * The use of last_mgmt_freq is a bit of a hack,
3415 * but it works due to the single-threaded nature
3416 * of wpa_supplicant.
3417 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07003418 if (freq == 0) {
3419 wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
3420 drv->last_mgmt_freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003421 freq = drv->last_mgmt_freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003422 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003423 return nl80211_send_frame_cmd(bss, freq, 0,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003424 data, data_len, NULL, 1, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003425 1, csa_offs, csa_offs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003426 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003427
3428 if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07003429 if (freq == 0) {
3430 wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
3431 bss->freq);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003432 freq = bss->freq;
Dmitry Shmidt56052862013-10-04 10:23:25 -07003433 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003434 return nl80211_send_frame_cmd(bss, freq,
3435 (int) freq == bss->freq ? 0 :
3436 wait_time,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003437 data, data_len,
3438 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003439 no_cck, noack, offchanok,
3440 csa_offs, csa_offs_len);
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07003441 }
Dmitry Shmidtb638fe72012-03-20 12:51:25 -07003442
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003443 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3444 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
3445 /*
3446 * Only one of the authentication frame types is encrypted.
3447 * In order for static WEP encryption to work properly (i.e.,
3448 * to not encrypt the frame), we need to tell mac80211 about
3449 * the frames that must not be encrypted.
3450 */
3451 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
3452 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
3453 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
3454 encrypt = 0;
3455 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003456
Dmitry Shmidt56052862013-10-04 10:23:25 -07003457 wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003458 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003459 noack, freq, no_cck, offchanok,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003460 wait_time, csa_offs,
3461 csa_offs_len);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003462}
3463
3464
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003465static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
3466{
3467 u8 rates[NL80211_MAX_SUPP_RATES];
3468 u8 rates_len = 0;
3469 int i;
3470
3471 if (!basic_rates)
3472 return 0;
3473
3474 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3475 rates[rates_len++] = basic_rates[i] / 5;
3476
3477 return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3478}
3479
3480
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003481static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
3482 int slot, int ht_opmode, int ap_isolate,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003483 const int *basic_rates)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003484{
3485 struct wpa_driver_nl80211_data *drv = bss->drv;
3486 struct nl_msg *msg;
3487
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003488 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
3489 (cts >= 0 &&
3490 nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
3491 (preamble >= 0 &&
3492 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
3493 (slot >= 0 &&
3494 nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
3495 (ht_opmode >= 0 &&
3496 nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
3497 (ap_isolate >= 0 &&
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003498 nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
3499 nl80211_put_basic_rates(msg, basic_rates)) {
3500 nlmsg_free(msg);
3501 return -ENOBUFS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003502 }
3503
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003504 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003505}
3506
3507
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003508static int wpa_driver_nl80211_set_acl(void *priv,
3509 struct hostapd_acl_params *params)
3510{
3511 struct i802_bss *bss = priv;
3512 struct wpa_driver_nl80211_data *drv = bss->drv;
3513 struct nl_msg *msg;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003514 struct nl_msg *acl;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003515 unsigned int i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003516 int ret;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003517
3518 if (!(drv->capa.max_acl_mac_addrs))
3519 return -ENOTSUP;
3520
3521 if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
3522 return -ENOTSUP;
3523
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003524 wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
3525 params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
3526
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003527 acl = nlmsg_alloc();
3528 if (!acl)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003529 return -ENOMEM;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003530 for (i = 0; i < params->num_mac_acl; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003531 if (nla_put(acl, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
3532 nlmsg_free(acl);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003533 return -ENOMEM;
3534 }
3535 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003536
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003537 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
3538 nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
3539 NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
3540 NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
3541 nla_put_nested(msg, NL80211_ATTR_MAC_ADDRS, acl)) {
3542 nlmsg_free(msg);
3543 nlmsg_free(acl);
3544 return -ENOMEM;
3545 }
3546 nlmsg_free(acl);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003547
3548 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003549 if (ret) {
3550 wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
3551 ret, strerror(-ret));
3552 }
3553
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07003554 return ret;
3555}
3556
3557
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003558static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
3559{
3560 if (beacon_int > 0) {
3561 wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
3562 return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
3563 beacon_int);
3564 }
3565
3566 return 0;
3567}
3568
3569
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003570static int nl80211_put_dtim_period(struct nl_msg *msg, int dtim_period)
3571{
3572 if (dtim_period > 0) {
3573 wpa_printf(MSG_DEBUG, " * dtim_period=%d", dtim_period);
3574 return nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
3575 }
3576
3577 return 0;
3578}
3579
3580
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07003581#ifdef CONFIG_MESH
3582static int nl80211_set_mesh_config(void *priv,
3583 struct wpa_driver_mesh_bss_params *params)
3584{
3585 struct i802_bss *bss = priv;
3586 struct wpa_driver_nl80211_data *drv = bss->drv;
3587 struct nl_msg *msg;
3588 int ret;
3589
3590 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MESH_CONFIG);
3591 if (!msg)
3592 return -1;
3593
3594 ret = nl80211_put_mesh_config(msg, params);
3595 if (ret < 0) {
3596 nlmsg_free(msg);
3597 return ret;
3598 }
3599
3600 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3601 if (ret) {
3602 wpa_printf(MSG_ERROR,
3603 "nl80211: Mesh config set failed: %d (%s)",
3604 ret, strerror(-ret));
3605 return ret;
3606 }
3607 return 0;
3608}
3609#endif /* CONFIG_MESH */
3610
3611
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003612static int nl80211_put_beacon_rate(struct nl_msg *msg, const u64 flags,
3613 struct wpa_driver_ap_params *params)
3614{
3615 struct nlattr *bands, *band;
3616 struct nl80211_txrate_vht vht_rate;
3617
3618 if (!params->freq ||
3619 (params->beacon_rate == 0 &&
3620 params->rate_type == BEACON_RATE_LEGACY))
3621 return 0;
3622
3623 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
3624 if (!bands)
3625 return -1;
3626
3627 switch (params->freq->mode) {
3628 case HOSTAPD_MODE_IEEE80211B:
3629 case HOSTAPD_MODE_IEEE80211G:
3630 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
3631 break;
3632 case HOSTAPD_MODE_IEEE80211A:
3633 band = nla_nest_start(msg, NL80211_BAND_5GHZ);
3634 break;
3635 case HOSTAPD_MODE_IEEE80211AD:
3636 band = nla_nest_start(msg, NL80211_BAND_60GHZ);
3637 break;
3638 default:
3639 return 0;
3640 }
3641
3642 if (!band)
3643 return -1;
3644
3645 os_memset(&vht_rate, 0, sizeof(vht_rate));
3646
3647 switch (params->rate_type) {
3648 case BEACON_RATE_LEGACY:
3649 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_LEGACY)) {
3650 wpa_printf(MSG_INFO,
3651 "nl80211: Driver does not support setting Beacon frame rate (legacy)");
3652 return -1;
3653 }
3654
3655 if (nla_put_u8(msg, NL80211_TXRATE_LEGACY,
3656 (u8) params->beacon_rate / 5) ||
3657 nla_put(msg, NL80211_TXRATE_HT, 0, NULL) ||
3658 (params->freq->vht_enabled &&
3659 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3660 &vht_rate)))
3661 return -1;
3662
3663 wpa_printf(MSG_DEBUG, " * beacon_rate = legacy:%u (* 100 kbps)",
3664 params->beacon_rate);
3665 break;
3666 case BEACON_RATE_HT:
3667 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_HT)) {
3668 wpa_printf(MSG_INFO,
3669 "nl80211: Driver does not support setting Beacon frame rate (HT)");
3670 return -1;
3671 }
3672 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL) ||
3673 nla_put_u8(msg, NL80211_TXRATE_HT, params->beacon_rate) ||
3674 (params->freq->vht_enabled &&
3675 nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3676 &vht_rate)))
3677 return -1;
3678 wpa_printf(MSG_DEBUG, " * beacon_rate = HT-MCS %u",
3679 params->beacon_rate);
3680 break;
3681 case BEACON_RATE_VHT:
3682 if (!(flags & WPA_DRIVER_FLAGS_BEACON_RATE_VHT)) {
3683 wpa_printf(MSG_INFO,
3684 "nl80211: Driver does not support setting Beacon frame rate (VHT)");
3685 return -1;
3686 }
3687 vht_rate.mcs[0] = BIT(params->beacon_rate);
3688 if (nla_put(msg, NL80211_TXRATE_LEGACY, 0, NULL))
3689 return -1;
3690 if (nla_put(msg, NL80211_TXRATE_HT, 0, NULL))
3691 return -1;
3692 if (nla_put(msg, NL80211_TXRATE_VHT, sizeof(vht_rate),
3693 &vht_rate))
3694 return -1;
3695 wpa_printf(MSG_DEBUG, " * beacon_rate = VHT-MCS %u",
3696 params->beacon_rate);
3697 break;
3698 }
3699
3700 nla_nest_end(msg, band);
3701 nla_nest_end(msg, bands);
3702
3703 return 0;
3704}
3705
3706
3707static int nl80211_set_multicast_to_unicast(struct i802_bss *bss,
3708 int multicast_to_unicast)
3709{
3710 struct wpa_driver_nl80211_data *drv = bss->drv;
3711 struct nl_msg *msg;
3712 int ret;
3713
3714 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_MULTICAST_TO_UNICAST);
3715 if (!msg ||
3716 (multicast_to_unicast &&
3717 nla_put_flag(msg, NL80211_ATTR_MULTICAST_TO_UNICAST_ENABLED))) {
3718 wpa_printf(MSG_ERROR,
3719 "nl80211: Failed to build NL80211_CMD_SET_MULTICAST_TO_UNICAST msg for %s",
3720 bss->ifname);
3721 nlmsg_free(msg);
3722 return -ENOBUFS;
3723 }
3724
3725 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3726
3727 switch (ret) {
3728 case 0:
3729 wpa_printf(MSG_DEBUG,
3730 "nl80211: multicast to unicast %s on interface %s",
3731 multicast_to_unicast ? "enabled" : "disabled",
3732 bss->ifname);
3733 break;
3734 case -EOPNOTSUPP:
3735 if (!multicast_to_unicast)
3736 break;
3737 wpa_printf(MSG_INFO,
3738 "nl80211: multicast to unicast not supported on interface %s",
3739 bss->ifname);
3740 break;
3741 default:
3742 wpa_printf(MSG_ERROR,
3743 "nl80211: %s multicast to unicast failed with %d (%s) on interface %s",
3744 multicast_to_unicast ? "enabling" : "disabling",
3745 ret, strerror(-ret), bss->ifname);
3746 break;
3747 }
3748
3749 return ret;
3750}
3751
3752
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003753static int wpa_driver_nl80211_set_ap(void *priv,
3754 struct wpa_driver_ap_params *params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003755{
3756 struct i802_bss *bss = priv;
3757 struct wpa_driver_nl80211_data *drv = bss->drv;
3758 struct nl_msg *msg;
3759 u8 cmd = NL80211_CMD_NEW_BEACON;
3760 int ret;
3761 int beacon_set;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003762 int num_suites;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003763 int smps_mode;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003764 u32 suites[10], suite;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003765 u32 ver;
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07003766#ifdef CONFIG_MESH
3767 struct wpa_driver_mesh_bss_params mesh_params;
3768#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003769
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003770 beacon_set = params->reenable ? 0 : bss->beacon_set;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003771
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003772 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
3773 beacon_set);
3774 if (beacon_set)
3775 cmd = NL80211_CMD_SET_BEACON;
3776
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003777 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
3778 params->head, params->head_len);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003779 wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
3780 params->tail, params->tail_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003781 wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003782 wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003783 wpa_printf(MSG_DEBUG, "nl80211: beacon_rate=%u", params->beacon_rate);
3784 wpa_printf(MSG_DEBUG, "nl80211: rate_type=%d", params->rate_type);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003785 wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003786 wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
3787 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003788 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
3789 nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
3790 params->head) ||
3791 nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
3792 params->tail) ||
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003793 nl80211_put_beacon_int(msg, params->beacon_int) ||
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003794 nl80211_put_beacon_rate(msg, drv->capa.flags, params) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003795 nl80211_put_dtim_period(msg, params->dtim_period) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003796 nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
3797 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003798 if (params->proberesp && params->proberesp_len) {
3799 wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
3800 params->proberesp, params->proberesp_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003801 if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
3802 params->proberesp))
3803 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003804 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003805 switch (params->hide_ssid) {
3806 case NO_SSID_HIDING:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003807 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003808 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3809 NL80211_HIDDEN_SSID_NOT_IN_USE))
3810 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003811 break;
3812 case HIDDEN_SSID_ZERO_LEN:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003813 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003814 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3815 NL80211_HIDDEN_SSID_ZERO_LEN))
3816 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003817 break;
3818 case HIDDEN_SSID_ZERO_CONTENTS:
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003819 wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003820 if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
3821 NL80211_HIDDEN_SSID_ZERO_CONTENTS))
3822 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003823 break;
3824 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003825 wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003826 if (params->privacy &&
3827 nla_put_flag(msg, NL80211_ATTR_PRIVACY))
3828 goto fail;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003829 wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003830 if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
3831 (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
3832 /* Leave out the attribute */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003833 } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
3834 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3835 NL80211_AUTHTYPE_SHARED_KEY))
3836 goto fail;
3837 } else {
3838 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
3839 NL80211_AUTHTYPE_OPEN_SYSTEM))
3840 goto fail;
3841 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003842
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003843 wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003844 ver = 0;
3845 if (params->wpa_version & WPA_PROTO_WPA)
3846 ver |= NL80211_WPA_VERSION_1;
3847 if (params->wpa_version & WPA_PROTO_RSN)
3848 ver |= NL80211_WPA_VERSION_2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003849 if (ver &&
3850 nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
3851 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003852
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003853 wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
3854 params->key_mgmt_suites);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003855 num_suites = 0;
3856 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
3857 suites[num_suites++] = WLAN_AKM_SUITE_8021X;
3858 if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
3859 suites[num_suites++] = WLAN_AKM_SUITE_PSK;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003860 if (num_suites &&
3861 nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
3862 suites))
3863 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003864
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003865 if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07003866 (!params->pairwise_ciphers ||
3867 params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40)) &&
3868 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
3869 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003870 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003871
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003872 wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
3873 params->pairwise_ciphers);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003874 num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
3875 suites, ARRAY_SIZE(suites));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003876 if (num_suites &&
3877 nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
3878 num_suites * sizeof(u32), suites))
3879 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003880
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003881 wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
3882 params->group_cipher);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003883 suite = wpa_cipher_to_cipher_suite(params->group_cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003884 if (suite &&
3885 nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
3886 goto fail;
3887
Dmitry Shmidte4663042016-04-04 10:07:49 -07003888 if (params->ht_opmode != -1) {
3889 switch (params->smps_mode) {
3890 case HT_CAP_INFO_SMPS_DYNAMIC:
3891 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
3892 smps_mode = NL80211_SMPS_DYNAMIC;
3893 break;
3894 case HT_CAP_INFO_SMPS_STATIC:
3895 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
3896 smps_mode = NL80211_SMPS_STATIC;
3897 break;
3898 default:
3899 /* invalid - fallback to smps off */
3900 case HT_CAP_INFO_SMPS_DISABLED:
3901 wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
3902 smps_mode = NL80211_SMPS_OFF;
3903 break;
3904 }
3905 if (nla_put_u32(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
3906 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003907 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003908
3909 if (params->beacon_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003910 wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
3911 params->beacon_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003912 if (nla_put(msg, NL80211_ATTR_IE,
3913 wpabuf_len(params->beacon_ies),
3914 wpabuf_head(params->beacon_ies)))
3915 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003916 }
3917 if (params->proberesp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003918 wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
3919 params->proberesp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003920 if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
3921 wpabuf_len(params->proberesp_ies),
3922 wpabuf_head(params->proberesp_ies)))
3923 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003924 }
3925 if (params->assocresp_ies) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003926 wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
3927 params->assocresp_ies);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003928 if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
3929 wpabuf_len(params->assocresp_ies),
3930 wpabuf_head(params->assocresp_ies)))
3931 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003932 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003933
Dmitry Shmidt04949592012-07-19 12:16:46 -07003934 if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003935 wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
3936 params->ap_max_inactivity);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003937 if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
3938 params->ap_max_inactivity))
3939 goto fail;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003940 }
3941
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003942#ifdef CONFIG_P2P
3943 if (params->p2p_go_ctwindow > 0) {
3944 if (drv->p2p_go_ctwindow_supported) {
3945 wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
3946 params->p2p_go_ctwindow);
3947 if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
3948 params->p2p_go_ctwindow))
3949 goto fail;
3950 } else {
3951 wpa_printf(MSG_INFO,
3952 "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
3953 }
3954 }
3955#endif /* CONFIG_P2P */
3956
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003957 if (params->pbss) {
3958 wpa_printf(MSG_DEBUG, "nl80211: PBSS");
3959 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
3960 goto fail;
3961 }
3962
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003963 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3964 if (ret) {
3965 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
3966 ret, strerror(-ret));
3967 } else {
3968 bss->beacon_set = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003969 nl80211_set_bss(bss, params->cts_protect, params->preamble,
3970 params->short_slot_time, params->ht_opmode,
3971 params->isolate, params->basic_rates);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08003972 nl80211_set_multicast_to_unicast(bss,
3973 params->multicast_to_unicast);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003974 if (beacon_set && params->freq &&
3975 params->freq->bandwidth != bss->bandwidth) {
3976 wpa_printf(MSG_DEBUG,
3977 "nl80211: Update BSS %s bandwidth: %d -> %d",
3978 bss->ifname, bss->bandwidth,
3979 params->freq->bandwidth);
3980 ret = nl80211_set_channel(bss, params->freq, 1);
3981 if (ret) {
3982 wpa_printf(MSG_DEBUG,
3983 "nl80211: Frequency set failed: %d (%s)",
3984 ret, strerror(-ret));
3985 } else {
3986 wpa_printf(MSG_DEBUG,
3987 "nl80211: Frequency set succeeded for ht2040 coex");
3988 bss->bandwidth = params->freq->bandwidth;
3989 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003990 } else if (!beacon_set && params->freq) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003991 /*
3992 * cfg80211 updates the driver on frequence change in AP
3993 * mode only at the point when beaconing is started, so
3994 * set the initial value here.
3995 */
3996 bss->bandwidth = params->freq->bandwidth;
3997 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003998 }
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07003999
4000#ifdef CONFIG_MESH
4001 if (is_mesh_interface(drv->nlmode) && params->ht_opmode != -1) {
4002 os_memset(&mesh_params, 0, sizeof(mesh_params));
4003 mesh_params.flags |= WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE;
4004 mesh_params.ht_opmode = params->ht_opmode;
4005 ret = nl80211_set_mesh_config(priv, &mesh_params);
4006 if (ret < 0)
4007 return ret;
4008 }
4009#endif /* CONFIG_MESH */
4010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004011 return ret;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004012fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004013 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004014 return -ENOBUFS;
4015}
4016
4017
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004018static int nl80211_put_freq_params(struct nl_msg *msg,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004019 const struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004020{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004021 wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004022 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
4023 return -ENOBUFS;
4024
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004025 wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
4026 wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
4027
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004028 if (freq->vht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004029 enum nl80211_chan_width cw;
4030
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004031 wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004032 switch (freq->bandwidth) {
4033 case 20:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004034 cw = NL80211_CHAN_WIDTH_20;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004035 break;
4036 case 40:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004037 cw = NL80211_CHAN_WIDTH_40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004038 break;
4039 case 80:
4040 if (freq->center_freq2)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004041 cw = NL80211_CHAN_WIDTH_80P80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004042 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004043 cw = NL80211_CHAN_WIDTH_80;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004044 break;
4045 case 160:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004046 cw = NL80211_CHAN_WIDTH_160;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004047 break;
4048 default:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004049 return -EINVAL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004050 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004051
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004052 wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
4053 wpa_printf(MSG_DEBUG, " * center_freq1=%d",
4054 freq->center_freq1);
4055 wpa_printf(MSG_DEBUG, " * center_freq2=%d",
4056 freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004057 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
4058 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
4059 freq->center_freq1) ||
4060 (freq->center_freq2 &&
4061 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
4062 freq->center_freq2)))
4063 return -ENOBUFS;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004064 } else if (freq->ht_enabled) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004065 enum nl80211_channel_type ct;
4066
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004067 wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
4068 freq->sec_channel_offset);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004069 switch (freq->sec_channel_offset) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004070 case -1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004071 ct = NL80211_CHAN_HT40MINUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004072 break;
4073 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004074 ct = NL80211_CHAN_HT40PLUS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004075 break;
4076 default:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004077 ct = NL80211_CHAN_HT20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004078 break;
4079 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004080
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004081 wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004082 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
4083 return -ENOBUFS;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004084 } else {
4085 wpa_printf(MSG_DEBUG, " * channel_type=%d",
4086 NL80211_CHAN_NO_HT);
4087 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
4088 NL80211_CHAN_NO_HT))
4089 return -ENOBUFS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004090 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004091 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004092}
4093
4094
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004095static int nl80211_set_channel(struct i802_bss *bss,
4096 struct hostapd_freq_params *freq, int set_chan)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004097{
4098 struct wpa_driver_nl80211_data *drv = bss->drv;
4099 struct nl_msg *msg;
4100 int ret;
4101
4102 wpa_printf(MSG_DEBUG,
4103 "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
4104 freq->freq, freq->ht_enabled, freq->vht_enabled,
4105 freq->bandwidth, freq->center_freq1, freq->center_freq2);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004106
4107 msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
4108 NL80211_CMD_SET_WIPHY);
4109 if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
4110 nlmsg_free(msg);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004111 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004112 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004113
4114 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004115 if (ret == 0) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004116 bss->freq = freq->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004117 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004118 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004119 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004120 "%d (%s)", freq->freq, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004121 return -1;
4122}
4123
4124
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004125static u32 sta_flags_nl80211(int flags)
4126{
4127 u32 f = 0;
4128
4129 if (flags & WPA_STA_AUTHORIZED)
4130 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
4131 if (flags & WPA_STA_WMM)
4132 f |= BIT(NL80211_STA_FLAG_WME);
4133 if (flags & WPA_STA_SHORT_PREAMBLE)
4134 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
4135 if (flags & WPA_STA_MFP)
4136 f |= BIT(NL80211_STA_FLAG_MFP);
4137 if (flags & WPA_STA_TDLS_PEER)
4138 f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004139 if (flags & WPA_STA_AUTHENTICATED)
4140 f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004141 if (flags & WPA_STA_ASSOCIATED)
4142 f |= BIT(NL80211_STA_FLAG_ASSOCIATED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004143
4144 return f;
4145}
4146
4147
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004148#ifdef CONFIG_MESH
4149static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
4150{
4151 switch (state) {
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004152 case PLINK_IDLE:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004153 return NL80211_PLINK_LISTEN;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004154 case PLINK_OPN_SNT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004155 return NL80211_PLINK_OPN_SNT;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004156 case PLINK_OPN_RCVD:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004157 return NL80211_PLINK_OPN_RCVD;
4158 case PLINK_CNF_RCVD:
4159 return NL80211_PLINK_CNF_RCVD;
4160 case PLINK_ESTAB:
4161 return NL80211_PLINK_ESTAB;
4162 case PLINK_HOLDING:
4163 return NL80211_PLINK_HOLDING;
4164 case PLINK_BLOCKED:
4165 return NL80211_PLINK_BLOCKED;
4166 default:
4167 wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
4168 state);
4169 }
4170 return -1;
4171}
4172#endif /* CONFIG_MESH */
4173
4174
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004175static int wpa_driver_nl80211_sta_add(void *priv,
4176 struct hostapd_sta_add_params *params)
4177{
4178 struct i802_bss *bss = priv;
4179 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004180 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004181 struct nl80211_sta_flag_update upd;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004182 int ret = -ENOBUFS;
4183
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004184 if ((params->flags & WPA_STA_TDLS_PEER) &&
4185 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
4186 return -EOPNOTSUPP;
4187
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004188 wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
4189 params->set ? "Set" : "Add", MAC2STR(params->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004190 msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
4191 NL80211_CMD_NEW_STATION);
4192 if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
4193 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004194
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004195 /*
4196 * Set the below properties only in one of the following cases:
4197 * 1. New station is added, already associated.
4198 * 2. Set WPA_STA_TDLS_PEER station.
4199 * 3. Set an already added unassociated station, if driver supports
4200 * full AP client state. (Set these properties after station became
4201 * associated will be rejected by the driver).
4202 */
4203 if (!params->set || (params->flags & WPA_STA_TDLS_PEER) ||
4204 (params->set && FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4205 (params->flags & WPA_STA_ASSOCIATED))) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004206 wpa_hexdump(MSG_DEBUG, " * supported rates",
4207 params->supp_rates, params->supp_rates_len);
4208 wpa_printf(MSG_DEBUG, " * capability=0x%x",
4209 params->capability);
4210 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
4211 params->supp_rates_len, params->supp_rates) ||
4212 nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
4213 params->capability))
4214 goto fail;
4215
4216 if (params->ht_capabilities) {
4217 wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
4218 (u8 *) params->ht_capabilities,
4219 sizeof(*params->ht_capabilities));
4220 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
4221 sizeof(*params->ht_capabilities),
4222 params->ht_capabilities))
4223 goto fail;
4224 }
4225
4226 if (params->vht_capabilities) {
4227 wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
4228 (u8 *) params->vht_capabilities,
4229 sizeof(*params->vht_capabilities));
4230 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
4231 sizeof(*params->vht_capabilities),
4232 params->vht_capabilities))
4233 goto fail;
4234 }
4235
4236 if (params->ext_capab) {
4237 wpa_hexdump(MSG_DEBUG, " * ext_capab",
4238 params->ext_capab, params->ext_capab_len);
4239 if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
4240 params->ext_capab_len, params->ext_capab))
4241 goto fail;
4242 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004243
4244 if (is_ap_interface(drv->nlmode) &&
4245 nla_put_u8(msg, NL80211_ATTR_STA_SUPPORT_P2P_PS,
4246 params->support_p2p_ps ?
4247 NL80211_P2P_PS_SUPPORTED :
4248 NL80211_P2P_PS_UNSUPPORTED))
4249 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004250 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004251 if (!params->set) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004252 if (params->aid) {
4253 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004254 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
4255 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004256 } else {
4257 /*
4258 * cfg80211 validates that AID is non-zero, so we have
4259 * to make this a non-zero value for the TDLS case where
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004260 * a dummy STA entry is used for now and for a station
4261 * that is still not associated.
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004262 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004263 wpa_printf(MSG_DEBUG, " * aid=1 (%s workaround)",
4264 (params->flags & WPA_STA_TDLS_PEER) ?
4265 "TDLS" : "UNASSOC_STA");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004266 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
4267 goto fail;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004268 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004269 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4270 params->listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004271 if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4272 params->listen_interval))
4273 goto fail;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004274 } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
4275 wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004276 if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
4277 goto fail;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004278 } else if (FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags) &&
4279 (params->flags & WPA_STA_ASSOCIATED)) {
4280 wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
4281 wpa_printf(MSG_DEBUG, " * listen_interval=%u",
4282 params->listen_interval);
4283 if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid) ||
4284 nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
4285 params->listen_interval))
4286 goto fail;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004287 }
4288
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08004289 if (params->vht_opmode_enabled) {
4290 wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004291 if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
4292 params->vht_opmode))
4293 goto fail;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004294 }
4295
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004296 if (params->supp_channels) {
4297 wpa_hexdump(MSG_DEBUG, " * supported channels",
4298 params->supp_channels, params->supp_channels_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004299 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
4300 params->supp_channels_len, params->supp_channels))
4301 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004302 }
4303
4304 if (params->supp_oper_classes) {
4305 wpa_hexdump(MSG_DEBUG, " * supported operating classes",
4306 params->supp_oper_classes,
4307 params->supp_oper_classes_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004308 if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
4309 params->supp_oper_classes_len,
4310 params->supp_oper_classes))
4311 goto fail;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004312 }
4313
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004314 os_memset(&upd, 0, sizeof(upd));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004315 upd.set = sta_flags_nl80211(params->flags);
4316 upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004317
4318 /*
4319 * If the driver doesn't support full AP client state, ignore ASSOC/AUTH
4320 * flags, as nl80211 driver moves a new station, by default, into
4321 * associated state.
4322 *
4323 * On the other hand, if the driver supports that feature and the
4324 * station is added in unauthenticated state, set the
4325 * authenticated/associated bits in the mask to prevent moving this
4326 * station to associated state before it is actually associated.
4327 *
4328 * This is irrelevant for mesh mode where the station is added to the
4329 * driver as authenticated already, and ASSOCIATED isn't part of the
4330 * nl80211 API.
4331 */
4332 if (!is_mesh_interface(drv->nlmode)) {
4333 if (!FULL_AP_CLIENT_STATE_SUPP(drv->capa.flags)) {
4334 wpa_printf(MSG_DEBUG,
4335 "nl80211: Ignore ASSOC/AUTH flags since driver doesn't support full AP client state");
4336 upd.mask &= ~(BIT(NL80211_STA_FLAG_ASSOCIATED) |
4337 BIT(NL80211_STA_FLAG_AUTHENTICATED));
4338 } else if (!params->set &&
4339 !(params->flags & WPA_STA_TDLS_PEER)) {
4340 if (!(params->flags & WPA_STA_AUTHENTICATED))
4341 upd.mask |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
4342 if (!(params->flags & WPA_STA_ASSOCIATED))
4343 upd.mask |= BIT(NL80211_STA_FLAG_ASSOCIATED);
4344 }
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004345#ifdef CONFIG_MESH
4346 } else {
4347 if (params->plink_state == PLINK_ESTAB && params->peer_aid) {
4348 ret = nla_put_u16(msg, NL80211_ATTR_MESH_PEER_AID,
4349 params->peer_aid);
4350 if (ret)
4351 goto fail;
4352 }
4353#endif /* CONFIG_MESH */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004354 }
4355
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004356 wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
4357 upd.set, upd.mask);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004358 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4359 goto fail;
4360
4361#ifdef CONFIG_MESH
4362 if (params->plink_state &&
4363 nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
4364 sta_plink_state_nl80211(params->plink_state)))
4365 goto fail;
4366#endif /* CONFIG_MESH */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004367
4368 if (params->flags & WPA_STA_WMM) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004369 struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
4370
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004371 wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004372 if (!wme ||
4373 nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
4374 params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
4375 nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
4376 (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
4377 WMM_QOSINFO_STA_SP_MASK))
4378 goto fail;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004379 nla_nest_end(msg, wme);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004380 }
4381
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004382 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004383 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004384 if (ret)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004385 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
4386 "result: %d (%s)", params->set ? "SET" : "NEW", ret,
4387 strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004388 if (ret == -EEXIST)
4389 ret = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004390fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004391 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004392 return ret;
4393}
4394
4395
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004396static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
4397{
4398#ifdef CONFIG_LIBNL3_ROUTE
4399 struct wpa_driver_nl80211_data *drv = bss->drv;
4400 struct rtnl_neigh *rn;
4401 struct nl_addr *nl_addr;
4402 int err;
4403
4404 rn = rtnl_neigh_alloc();
4405 if (!rn)
4406 return;
4407
4408 rtnl_neigh_set_family(rn, AF_BRIDGE);
4409 rtnl_neigh_set_ifindex(rn, bss->ifindex);
4410 nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
4411 if (!nl_addr) {
4412 rtnl_neigh_put(rn);
4413 return;
4414 }
4415 rtnl_neigh_set_lladdr(rn, nl_addr);
4416
4417 err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
4418 if (err < 0) {
4419 wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
4420 MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
4421 bss->ifindex, nl_geterror(err));
4422 } else {
4423 wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
4424 MACSTR, MAC2STR(addr));
4425 }
4426
4427 nl_addr_put(nl_addr);
4428 rtnl_neigh_put(rn);
4429#endif /* CONFIG_LIBNL3_ROUTE */
4430}
4431
4432
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004433static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
4434 int deauth, u16 reason_code)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004435{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004436 struct wpa_driver_nl80211_data *drv = bss->drv;
4437 struct nl_msg *msg;
4438 int ret;
4439
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004440 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
4441 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
4442 (deauth == 0 &&
4443 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4444 WLAN_FC_STYPE_DISASSOC)) ||
4445 (deauth == 1 &&
4446 nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
4447 WLAN_FC_STYPE_DEAUTH)) ||
4448 (reason_code &&
4449 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
4450 nlmsg_free(msg);
4451 return -ENOBUFS;
4452 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004453
4454 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004455 wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
4456 " --> %d (%s)",
4457 bss->ifname, MAC2STR(addr), ret, strerror(-ret));
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004458
4459 if (drv->rtnl_sk)
4460 rtnl_neigh_delete_fdb_entry(bss, addr);
4461
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004462 if (ret == -ENOENT)
4463 return 0;
4464 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004465}
4466
4467
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004468void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004469{
4470 struct nl_msg *msg;
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004471 struct wpa_driver_nl80211_data *drv2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004472
4473 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
4474
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004475 /* stop listening for EAPOL on this interface */
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004476 dl_list_for_each(drv2, &drv->global->interfaces,
4477 struct wpa_driver_nl80211_data, list)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004478 {
4479 del_ifidx(drv2, ifidx, IFIDX_ANY);
4480 /* Remove all bridges learned for this iface */
4481 del_ifidx(drv2, IFIDX_ANY, ifidx);
4482 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004483
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004484 msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004485 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4486 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004487 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
4488}
4489
4490
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07004491const char * nl80211_iftype_str(enum nl80211_iftype mode)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004492{
4493 switch (mode) {
4494 case NL80211_IFTYPE_ADHOC:
4495 return "ADHOC";
4496 case NL80211_IFTYPE_STATION:
4497 return "STATION";
4498 case NL80211_IFTYPE_AP:
4499 return "AP";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004500 case NL80211_IFTYPE_AP_VLAN:
4501 return "AP_VLAN";
4502 case NL80211_IFTYPE_WDS:
4503 return "WDS";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004504 case NL80211_IFTYPE_MONITOR:
4505 return "MONITOR";
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004506 case NL80211_IFTYPE_MESH_POINT:
4507 return "MESH_POINT";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004508 case NL80211_IFTYPE_P2P_CLIENT:
4509 return "P2P_CLIENT";
4510 case NL80211_IFTYPE_P2P_GO:
4511 return "P2P_GO";
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004512 case NL80211_IFTYPE_P2P_DEVICE:
4513 return "P2P_DEVICE";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004514 default:
4515 return "unknown";
4516 }
4517}
4518
4519
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004520static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
4521 const char *ifname,
4522 enum nl80211_iftype iftype,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004523 const u8 *addr, int wds,
4524 int (*handler)(struct nl_msg *, void *),
4525 void *arg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004526{
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004527 struct nl_msg *msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004528 int ifidx;
4529 int ret = -ENOBUFS;
4530
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004531 wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
4532 iftype, nl80211_iftype_str(iftype));
4533
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004534 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
4535 if (!msg ||
4536 nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
4537 nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
4538 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004539
4540 if (iftype == NL80211_IFTYPE_MONITOR) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004541 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004543 flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004544 if (!flags ||
4545 nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
4546 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004547
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004548 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004549 } else if (wds) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004550 if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
4551 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004552 }
4553
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004554 /*
4555 * Tell cfg80211 that the interface belongs to the socket that created
4556 * it, and the interface should be deleted when the socket is closed.
4557 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004558 if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
4559 goto fail;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07004560
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004561 ret = send_and_recv_msgs(drv, msg, handler, arg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004562 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004563 if (ret) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004564 fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004565 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
4567 ifname, ret, strerror(-ret));
4568 return ret;
4569 }
4570
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004571 if (iftype == NL80211_IFTYPE_P2P_DEVICE)
4572 return 0;
4573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004574 ifidx = if_nametoindex(ifname);
4575 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
4576 ifname, ifidx);
4577
4578 if (ifidx <= 0)
4579 return -1;
4580
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004581 /*
4582 * Some virtual interfaces need to process EAPOL packets and events on
4583 * the parent interface. This is used mainly with hostapd.
4584 */
4585 if (drv->hostapd ||
4586 iftype == NL80211_IFTYPE_AP_VLAN ||
4587 iftype == NL80211_IFTYPE_WDS ||
4588 iftype == NL80211_IFTYPE_MONITOR) {
4589 /* start listening for EAPOL on this interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004590 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07004591 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004592
4593 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004594 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004595 nl80211_remove_iface(drv, ifidx);
4596 return -1;
4597 }
4598
4599 return ifidx;
4600}
4601
4602
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004603int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
4604 const char *ifname, enum nl80211_iftype iftype,
4605 const u8 *addr, int wds,
4606 int (*handler)(struct nl_msg *, void *),
4607 void *arg, int use_existing)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004608{
4609 int ret;
4610
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004611 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
4612 arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004613
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004614 /* if error occurred and interface exists already */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615 if (ret == -ENFILE && if_nametoindex(ifname)) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004616 if (use_existing) {
4617 wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
4618 ifname);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004619 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
4620 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4621 addr) < 0 &&
4622 (linux_set_iface_flags(drv->global->ioctl_sock,
4623 ifname, 0) < 0 ||
4624 linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
4625 addr) < 0 ||
4626 linux_set_iface_flags(drv->global->ioctl_sock,
4627 ifname, 1) < 0))
4628 return -1;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004629 return -ENFILE;
4630 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004631 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
4632
4633 /* Try to remove the interface that was already there. */
4634 nl80211_remove_iface(drv, if_nametoindex(ifname));
4635
4636 /* Try to create the interface again */
4637 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004638 wds, handler, arg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639 }
4640
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004641 if (ret >= 0 && is_p2p_net_interface(iftype)) {
4642 wpa_printf(MSG_DEBUG,
4643 "nl80211: Interface %s created for P2P - disable 11b rates",
4644 ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645 nl80211_disable_11b_rates(drv, ret, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004646 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647
4648 return ret;
4649}
4650
4651
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004652static int nl80211_setup_ap(struct i802_bss *bss)
4653{
4654 struct wpa_driver_nl80211_data *drv = bss->drv;
4655
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004656 wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
4657 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004658
4659 /*
4660 * Disable Probe Request reporting unless we need it in this way for
4661 * devices that include the AP SME, in the other case (unless using
4662 * monitor iface) we'll get it through the nl_mgmt socket instead.
4663 */
4664 if (!drv->device_ap_sme)
4665 wpa_driver_nl80211_probe_req_report(bss, 0);
4666
4667 if (!drv->device_ap_sme && !drv->use_monitor)
4668 if (nl80211_mgmt_subscribe_ap(bss))
4669 return -1;
4670
4671 if (drv->device_ap_sme && !drv->use_monitor)
4672 if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004673 wpa_printf(MSG_DEBUG,
4674 "nl80211: Failed to subscribe for mgmt frames from SME driver - trying to run without it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004675
4676 if (!drv->device_ap_sme && drv->use_monitor &&
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004677 nl80211_create_monitor_interface(drv) &&
4678 !drv->device_ap_sme)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004679 return -1;
4680
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004681 if (drv->device_ap_sme &&
4682 wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
4683 wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
4684 "Probe Request frame reporting in AP mode");
4685 /* Try to survive without this */
4686 }
4687
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004688 return 0;
4689}
4690
4691
4692static void nl80211_teardown_ap(struct i802_bss *bss)
4693{
4694 struct wpa_driver_nl80211_data *drv = bss->drv;
4695
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004696 wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
4697 bss->ifname, drv->device_ap_sme, drv->use_monitor);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004698 if (drv->device_ap_sme) {
4699 wpa_driver_nl80211_probe_req_report(bss, 0);
4700 if (!drv->use_monitor)
4701 nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
4702 } else if (drv->use_monitor)
4703 nl80211_remove_monitor_interface(drv);
4704 else
4705 nl80211_mgmt_unsubscribe(bss, "AP teardown");
4706
4707 bss->beacon_set = 0;
4708}
4709
4710
4711static int nl80211_send_eapol_data(struct i802_bss *bss,
4712 const u8 *addr, const u8 *data,
4713 size_t data_len)
4714{
4715 struct sockaddr_ll ll;
4716 int ret;
4717
4718 if (bss->drv->eapol_tx_sock < 0) {
4719 wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
4720 return -1;
4721 }
4722
4723 os_memset(&ll, 0, sizeof(ll));
4724 ll.sll_family = AF_PACKET;
4725 ll.sll_ifindex = bss->ifindex;
4726 ll.sll_protocol = htons(ETH_P_PAE);
4727 ll.sll_halen = ETH_ALEN;
4728 os_memcpy(ll.sll_addr, addr, ETH_ALEN);
4729 ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
4730 (struct sockaddr *) &ll, sizeof(ll));
4731 if (ret < 0)
4732 wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
4733 strerror(errno));
4734
4735 return ret;
4736}
4737
4738
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004739static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
4740
4741static int wpa_driver_nl80211_hapd_send_eapol(
4742 void *priv, const u8 *addr, const u8 *data,
4743 size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
4744{
4745 struct i802_bss *bss = priv;
4746 struct wpa_driver_nl80211_data *drv = bss->drv;
4747 struct ieee80211_hdr *hdr;
4748 size_t len;
4749 u8 *pos;
4750 int res;
4751 int qos = flags & WPA_STA_WMM;
Dmitry Shmidt641185e2013-11-06 15:17:13 -08004752
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004753 if (drv->device_ap_sme || !drv->use_monitor)
4754 return nl80211_send_eapol_data(bss, addr, data, data_len);
4755
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
4757 data_len;
4758 hdr = os_zalloc(len);
4759 if (hdr == NULL) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004760 wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
4761 (unsigned long) len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762 return -1;
4763 }
4764
4765 hdr->frame_control =
4766 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
4767 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
4768 if (encrypt)
4769 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
4770 if (qos) {
4771 hdr->frame_control |=
4772 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
4773 }
4774
4775 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
4776 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
4777 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
4778 pos = (u8 *) (hdr + 1);
4779
4780 if (qos) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004781 /* Set highest priority in QoS header */
4782 pos[0] = 7;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004783 pos[1] = 0;
4784 pos += 2;
4785 }
4786
4787 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
4788 pos += sizeof(rfc1042_header);
4789 WPA_PUT_BE16(pos, ETH_P_PAE);
4790 pos += 2;
4791 memcpy(pos, data, data_len);
4792
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004793 res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004794 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004795 if (res < 0) {
4796 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
4797 "failed: %d (%s)",
4798 (unsigned long) len, errno, strerror(errno));
4799 }
4800 os_free(hdr);
4801
4802 return res;
4803}
4804
4805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004806static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004807 unsigned int total_flags,
4808 unsigned int flags_or,
4809 unsigned int flags_and)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004810{
4811 struct i802_bss *bss = priv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004812 struct nl_msg *msg;
4813 struct nlattr *flags;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814 struct nl80211_sta_flag_update upd;
4815
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004816 wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
4817 " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
4818 bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
4819 !!(total_flags & WPA_STA_AUTHORIZED));
4820
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004821 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
4822 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
4823 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004824
4825 /*
4826 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
4827 * can be removed eventually.
4828 */
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004829 flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004830 if (!flags ||
4831 ((total_flags & WPA_STA_AUTHORIZED) &&
4832 nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
4833 ((total_flags & WPA_STA_WMM) &&
4834 nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
4835 ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
4836 nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
4837 ((total_flags & WPA_STA_MFP) &&
4838 nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
4839 ((total_flags & WPA_STA_TDLS_PEER) &&
4840 nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
4841 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004842
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07004843 nla_nest_end(msg, flags);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004844
4845 os_memset(&upd, 0, sizeof(upd));
4846 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
4847 upd.set = sta_flags_nl80211(flags_or);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004848 if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
4849 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004850
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004851 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
4852fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004853 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004854 return -ENOBUFS;
4855}
4856
4857
4858static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
4859 struct wpa_driver_associate_params *params)
4860{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004861 enum nl80211_iftype nlmode, old_mode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004862
4863 if (params->p2p) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864 wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
4865 "group (GO)");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004866 nlmode = NL80211_IFTYPE_P2P_GO;
4867 } else
4868 nlmode = NL80211_IFTYPE_AP;
4869
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004870 old_mode = drv->nlmode;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004871 if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004872 nl80211_remove_monitor_interface(drv);
4873 return -1;
4874 }
4875
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004876 if (params->freq.freq &&
4877 nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004878 if (old_mode != nlmode)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08004879 wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004880 nl80211_remove_monitor_interface(drv);
4881 return -1;
4882 }
4883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884 return 0;
4885}
4886
4887
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004888static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
4889 int reset_mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004890{
4891 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004892 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004893
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004894 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004895 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004896 if (ret) {
4897 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
4898 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004899 } else {
4900 wpa_printf(MSG_DEBUG,
4901 "nl80211: Leave IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004902 }
4903
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004904 if (reset_mode &&
4905 wpa_driver_nl80211_set_mode(drv->first_bss,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004906 NL80211_IFTYPE_STATION)) {
4907 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4908 "station mode");
4909 }
4910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004911 return ret;
4912}
4913
4914
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004915static int nl80211_ht_vht_overrides(struct nl_msg *msg,
4916 struct wpa_driver_associate_params *params)
4917{
4918 if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
4919 return -1;
4920
4921 if (params->htcaps && params->htcaps_mask) {
4922 int sz = sizeof(struct ieee80211_ht_capabilities);
4923 wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
4924 wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
4925 params->htcaps_mask, sz);
4926 if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
4927 params->htcaps) ||
4928 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
4929 params->htcaps_mask))
4930 return -1;
4931 }
4932
4933#ifdef CONFIG_VHT_OVERRIDES
4934 if (params->disable_vht) {
4935 wpa_printf(MSG_DEBUG, " * VHT disabled");
4936 if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
4937 return -1;
4938 }
4939
4940 if (params->vhtcaps && params->vhtcaps_mask) {
4941 int sz = sizeof(struct ieee80211_vht_capabilities);
4942 wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
4943 wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
4944 params->vhtcaps_mask, sz);
4945 if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
4946 params->vhtcaps) ||
4947 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
4948 params->vhtcaps_mask))
4949 return -1;
4950 }
4951#endif /* CONFIG_VHT_OVERRIDES */
4952
4953 return 0;
4954}
4955
4956
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004957static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
4958 struct wpa_driver_associate_params *params)
4959{
4960 struct nl_msg *msg;
4961 int ret = -1;
4962 int count = 0;
4963
4964 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
4965
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004966 if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
4968 "IBSS mode");
4969 return -1;
4970 }
4971
4972retry:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004973 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
4974 params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
4975 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004976
4977 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
4978 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004979 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
4980 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004981 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
4982 drv->ssid_len = params->ssid_len;
4983
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004984 if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
4985 nl80211_put_beacon_int(msg, params->beacon_int))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004986 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004987
4988 ret = nl80211_set_conn_keys(params, msg);
4989 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004990 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004991
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004992 if (params->bssid && params->fixed_bssid) {
4993 wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
4994 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004995 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
4996 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004997 }
4998
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004999 if (params->fixed_freq) {
5000 wpa_printf(MSG_DEBUG, " * fixed_freq");
5001 if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
5002 goto fail;
5003 }
5004
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005005 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5006 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5007 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
5008 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005009 wpa_printf(MSG_DEBUG, " * control port");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005010 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5011 goto fail;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005012 }
5013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 if (params->wpa_ie) {
5015 wpa_hexdump(MSG_DEBUG,
5016 " * Extra IEs for Beacon/Probe Response frames",
5017 params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005018 if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
5019 params->wpa_ie))
5020 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005021 }
5022
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08005023 ret = nl80211_ht_vht_overrides(msg, params);
5024 if (ret < 0)
5025 goto fail;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005026
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005027 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5028 msg = NULL;
5029 if (ret) {
5030 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
5031 ret, strerror(-ret));
5032 count++;
5033 if (ret == -EALREADY && count == 1) {
5034 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
5035 "forced leave");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005036 nl80211_leave_ibss(drv, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005037 nlmsg_free(msg);
5038 goto retry;
5039 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005040 } else {
5041 wpa_printf(MSG_DEBUG,
5042 "nl80211: Join IBSS request sent successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005043 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005044
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005045fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005046 nlmsg_free(msg);
5047 return ret;
5048}
5049
5050
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005051static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
5052 struct wpa_driver_associate_params *params,
5053 struct nl_msg *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005054{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005055 if (params->bssid) {
5056 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
5057 MAC2STR(params->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005058 if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
5059 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005060 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005061
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005062 if (params->bssid_hint) {
5063 wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
5064 MAC2STR(params->bssid_hint));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005065 if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
5066 params->bssid_hint))
5067 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005068 }
5069
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005070 if (params->freq.freq) {
5071 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005072 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
5073 params->freq.freq))
5074 return -1;
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005075 drv->assoc_freq = params->freq.freq;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005076 } else
5077 drv->assoc_freq = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005078
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005079 if (params->freq_hint) {
5080 wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005081 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
5082 params->freq_hint))
5083 return -1;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005084 }
5085
Dmitry Shmidt04949592012-07-19 12:16:46 -07005086 if (params->bg_scan_period >= 0) {
5087 wpa_printf(MSG_DEBUG, " * bg scan period=%d",
5088 params->bg_scan_period);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005089 if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
5090 params->bg_scan_period))
5091 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005092 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005094 if (params->ssid) {
5095 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
5096 params->ssid, params->ssid_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005097 if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
5098 params->ssid))
5099 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005100 if (params->ssid_len > sizeof(drv->ssid))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005101 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
5103 drv->ssid_len = params->ssid_len;
5104 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005106 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005107 if (params->wpa_ie &&
5108 nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
5109 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005110
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005111 if (params->wpa_proto) {
5112 enum nl80211_wpa_versions ver = 0;
5113
5114 if (params->wpa_proto & WPA_PROTO_WPA)
5115 ver |= NL80211_WPA_VERSION_1;
5116 if (params->wpa_proto & WPA_PROTO_RSN)
5117 ver |= NL80211_WPA_VERSION_2;
5118
5119 wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005120 if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
5121 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005122 }
5123
5124 if (params->pairwise_suite != WPA_CIPHER_NONE) {
5125 u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
5126 wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005127 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
5128 cipher))
5129 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005130 }
5131
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005132 if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
5133 !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
5134 /*
5135 * This is likely to work even though many drivers do not
5136 * advertise support for operations without GTK.
5137 */
5138 wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
5139 } else if (params->group_suite != WPA_CIPHER_NONE) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005140 u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
5141 wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005142 if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
5143 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005144 }
5145
5146 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
5147 params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5148 params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
5149 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07005150 params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005151 params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
5152 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005153 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005154 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
5155 params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005156 int mgmt = WLAN_AKM_SUITE_PSK;
5157
5158 switch (params->key_mgmt_suite) {
5159 case WPA_KEY_MGMT_CCKM:
5160 mgmt = WLAN_AKM_SUITE_CCKM;
5161 break;
5162 case WPA_KEY_MGMT_IEEE8021X:
5163 mgmt = WLAN_AKM_SUITE_8021X;
5164 break;
5165 case WPA_KEY_MGMT_FT_IEEE8021X:
5166 mgmt = WLAN_AKM_SUITE_FT_8021X;
5167 break;
5168 case WPA_KEY_MGMT_FT_PSK:
5169 mgmt = WLAN_AKM_SUITE_FT_PSK;
5170 break;
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07005171 case WPA_KEY_MGMT_IEEE8021X_SHA256:
5172 mgmt = WLAN_AKM_SUITE_8021X_SHA256;
5173 break;
5174 case WPA_KEY_MGMT_PSK_SHA256:
5175 mgmt = WLAN_AKM_SUITE_PSK_SHA256;
5176 break;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005177 case WPA_KEY_MGMT_OSEN:
5178 mgmt = WLAN_AKM_SUITE_OSEN;
5179 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005180 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
5181 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B;
5182 break;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005183 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
5184 mgmt = WLAN_AKM_SUITE_8021X_SUITE_B_192;
5185 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005186 case WPA_KEY_MGMT_PSK:
5187 default:
5188 mgmt = WLAN_AKM_SUITE_PSK;
5189 break;
5190 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07005191 wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005192 if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
5193 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005194 }
5195
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005196 if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
5197 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005198
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07005199 if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
5200 (params->pairwise_suite == WPA_CIPHER_NONE ||
5201 params->pairwise_suite == WPA_CIPHER_WEP104 ||
5202 params->pairwise_suite == WPA_CIPHER_WEP40) &&
5203 (nla_put_u16(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE, ETH_P_PAE) ||
5204 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT)))
5205 return -1;
5206
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005207 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
5208 nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
5209 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005210
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005211 if (params->rrm_used) {
5212 u32 drv_rrm_flags = drv->capa.rrm_flags;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005213 if ((!((drv_rrm_flags &
5214 WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) &&
5215 (drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET)) &&
5216 !(drv_rrm_flags & WPA_DRIVER_FLAGS_SUPPORT_RRM)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005217 nla_put_flag(msg, NL80211_ATTR_USE_RRM))
5218 return -1;
5219 }
5220
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005221 if (nl80211_ht_vht_overrides(msg, params) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005222 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005223
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005224 if (params->p2p)
5225 wpa_printf(MSG_DEBUG, " * P2P group");
5226
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005227 if (params->pbss) {
5228 wpa_printf(MSG_DEBUG, " * PBSS");
5229 if (nla_put_flag(msg, NL80211_ATTR_PBSS))
5230 return -1;
5231 }
5232
Dmitry Shmidte4663042016-04-04 10:07:49 -07005233 drv->connect_reassoc = 0;
5234 if (params->prev_bssid) {
5235 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
5236 MAC2STR(params->prev_bssid));
5237 if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
5238 params->prev_bssid))
5239 return -1;
5240 drv->connect_reassoc = 1;
5241 }
5242
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005243 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005244}
5245
5246
5247static int wpa_driver_nl80211_try_connect(
5248 struct wpa_driver_nl80211_data *drv,
5249 struct wpa_driver_associate_params *params)
5250{
5251 struct nl_msg *msg;
5252 enum nl80211_auth_type type;
5253 int ret;
5254 int algs;
5255
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005256#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005257 if (params->req_key_mgmt_offload && params->psk &&
5258 (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
5259 params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
5260 params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
5261 wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
5262 ret = issue_key_mgmt_set_key(drv, params->psk, 32);
5263 if (ret)
5264 return ret;
5265 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005266#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005267
5268 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
5269 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005270 if (!msg)
5271 return -1;
5272
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005273 ret = nl80211_connect_common(drv, params, msg);
5274 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005275 goto fail;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005277 algs = 0;
5278 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5279 algs++;
5280 if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5281 algs++;
5282 if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5283 algs++;
5284 if (algs > 1) {
5285 wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
5286 "selection");
5287 goto skip_auth_type;
5288 }
5289
5290 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
5291 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
5292 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
5293 type = NL80211_AUTHTYPE_SHARED_KEY;
5294 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
5295 type = NL80211_AUTHTYPE_NETWORK_EAP;
5296 else if (params->auth_alg & WPA_AUTH_ALG_FT)
5297 type = NL80211_AUTHTYPE_FT;
5298 else
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005299 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005300
5301 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005302 if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
5303 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005304
5305skip_auth_type:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005306 ret = nl80211_set_conn_keys(params, msg);
5307 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005308 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005309
5310 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5311 msg = NULL;
5312 if (ret) {
5313 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
5314 "(%s)", ret, strerror(-ret));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005315 } else {
5316 wpa_printf(MSG_DEBUG,
5317 "nl80211: Connect request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005318 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005319
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005320fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005321 nlmsg_free(msg);
5322 return ret;
5323
5324}
5325
5326
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005327static int wpa_driver_nl80211_connect(
5328 struct wpa_driver_nl80211_data *drv,
5329 struct wpa_driver_associate_params *params)
5330{
Jithu Jancea7c60b42014-12-03 18:54:40 +05305331 int ret;
5332
5333 /* Store the connection attempted bssid for future use */
5334 if (params->bssid)
5335 os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
5336 else
5337 os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
5338
5339 ret = wpa_driver_nl80211_try_connect(drv, params);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005340 if (ret == -EALREADY) {
5341 /*
5342 * cfg80211 does not currently accept new connections if
5343 * we are already connected. As a workaround, force
5344 * disconnection and try again.
5345 */
5346 wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
5347 "disconnecting before reassociation "
5348 "attempt");
5349 if (wpa_driver_nl80211_disconnect(
5350 drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
5351 return -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005352 ret = wpa_driver_nl80211_try_connect(drv, params);
5353 }
5354 return ret;
5355}
5356
5357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005358static int wpa_driver_nl80211_associate(
5359 void *priv, struct wpa_driver_associate_params *params)
5360{
5361 struct i802_bss *bss = priv;
5362 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005363 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005364 struct nl_msg *msg;
5365
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005366 nl80211_unmask_11b_rates(bss);
5367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005368 if (params->mode == IEEE80211_MODE_AP)
5369 return wpa_driver_nl80211_ap(drv, params);
5370
5371 if (params->mode == IEEE80211_MODE_IBSS)
5372 return wpa_driver_nl80211_ibss(drv, params);
5373
5374 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005375 enum nl80211_iftype nlmode = params->p2p ?
5376 NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
5377
5378 if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005379 return -1;
5380 return wpa_driver_nl80211_connect(drv, params);
5381 }
5382
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005383 nl80211_mark_disconnected(drv);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005384
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005385 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
5386 drv->ifindex);
5387 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005388 if (!msg)
5389 return -1;
5390
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005391 ret = nl80211_connect_common(drv, params, msg);
5392 if (ret)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005393 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005394
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005395 if (params->fils_kek) {
5396 wpa_printf(MSG_DEBUG, " * FILS KEK (len=%u)",
5397 (unsigned int) params->fils_kek_len);
5398 if (nla_put(msg, NL80211_ATTR_FILS_KEK, params->fils_kek_len,
5399 params->fils_kek))
5400 goto fail;
5401 }
5402 if (params->fils_nonces) {
5403 wpa_hexdump(MSG_DEBUG, " * FILS nonces (for AAD)",
5404 params->fils_nonces,
5405 params->fils_nonces_len);
5406 if (nla_put(msg, NL80211_ATTR_FILS_NONCES,
5407 params->fils_nonces_len, params->fils_nonces))
5408 goto fail;
5409 }
5410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005411 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5412 msg = NULL;
5413 if (ret) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005414 wpa_dbg(drv->ctx, MSG_DEBUG,
5415 "nl80211: MLME command failed (assoc): ret=%d (%s)",
5416 ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005417 nl80211_dump_scan(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005418 } else {
5419 wpa_printf(MSG_DEBUG,
5420 "nl80211: Association request send successfully");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005421 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005422
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005423fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005424 nlmsg_free(msg);
5425 return ret;
5426}
5427
5428
5429static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005430 int ifindex, enum nl80211_iftype mode)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005431{
5432 struct nl_msg *msg;
5433 int ret = -ENOBUFS;
5434
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005435 wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
5436 ifindex, mode, nl80211_iftype_str(mode));
5437
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005438 msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
5439 if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
5440 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005441
5442 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005443 msg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005444 if (!ret)
5445 return 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005446fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005447 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005448 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
5449 " %d (%s)", ifindex, mode, ret, strerror(-ret));
5450 return ret;
5451}
5452
5453
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005454static int wpa_driver_nl80211_set_mode_impl(
5455 struct i802_bss *bss,
5456 enum nl80211_iftype nlmode,
5457 struct hostapd_freq_params *desired_freq_params)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005459 struct wpa_driver_nl80211_data *drv = bss->drv;
5460 int ret = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005461 int i;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005462 int was_ap = is_ap_interface(drv->nlmode);
5463 int res;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005464 int mode_switch_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005465
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07005466 if (TEST_FAIL())
5467 return -1;
5468
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005469 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5470 if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
5471 mode_switch_res = 0;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005472
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005473 if (mode_switch_res == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005474 drv->nlmode = nlmode;
5475 ret = 0;
5476 goto done;
5477 }
5478
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005479 if (mode_switch_res == -ENODEV)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005480 return -1;
5481
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005482 if (nlmode == drv->nlmode) {
5483 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
5484 "requested mode - ignore error");
5485 ret = 0;
5486 goto done; /* Already in the requested mode */
5487 }
5488
5489 /* mac80211 doesn't allow mode changes while the device is up, so
5490 * take the device down, try to set the mode again, and bring the
5491 * device back up.
5492 */
5493 wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
5494 "interface down");
5495 for (i = 0; i < 10; i++) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005496 res = i802_set_iface_flags(bss, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005497 if (res == -EACCES || res == -ENODEV)
5498 break;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005499 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005500 wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
5501 "interface down");
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005502 os_sleep(0, 100000);
5503 continue;
5504 }
5505
5506 /*
5507 * Setting the mode will fail for some drivers if the phy is
5508 * on a frequency that the mode is disallowed in.
5509 */
5510 if (desired_freq_params) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005511 res = nl80211_set_channel(bss, desired_freq_params, 0);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005512 if (res) {
5513 wpa_printf(MSG_DEBUG,
5514 "nl80211: Failed to set frequency on interface");
5515 }
5516 }
5517
5518 /* Try to set the mode again while the interface is down */
5519 mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
5520 if (mode_switch_res == -EBUSY) {
5521 wpa_printf(MSG_DEBUG,
5522 "nl80211: Delaying mode set while interface going down");
5523 os_sleep(0, 100000);
5524 continue;
5525 }
5526 ret = mode_switch_res;
5527 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005528 }
5529
5530 if (!ret) {
5531 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
5532 "interface is down");
5533 drv->nlmode = nlmode;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005534 drv->ignore_if_down_event = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005535 }
5536
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005537 /* Bring the interface back up */
5538 res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
5539 if (res != 0) {
5540 wpa_printf(MSG_DEBUG,
5541 "nl80211: Failed to set interface up after switching mode");
5542 ret = -1;
5543 }
5544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005545done:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005546 if (ret) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005547 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
5548 "from %d failed", nlmode, drv->nlmode);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005549 return ret;
5550 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005551
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005552 if (is_p2p_net_interface(nlmode)) {
5553 wpa_printf(MSG_DEBUG,
5554 "nl80211: Interface %s mode change to P2P - disable 11b rates",
5555 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005556 nl80211_disable_11b_rates(drv, drv->ifindex, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005557 } else if (drv->disabled_11b_rates) {
5558 wpa_printf(MSG_DEBUG,
5559 "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
5560 bss->ifname);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005561 nl80211_disable_11b_rates(drv, drv->ifindex, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005562 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005563
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005564 if (is_ap_interface(nlmode)) {
5565 nl80211_mgmt_unsubscribe(bss, "start AP");
5566 /* Setup additional AP mode functionality if needed */
5567 if (nl80211_setup_ap(bss))
5568 return -1;
5569 } else if (was_ap) {
5570 /* Remove additional AP mode functionality */
5571 nl80211_teardown_ap(bss);
5572 } else {
5573 nl80211_mgmt_unsubscribe(bss, "mode change");
5574 }
5575
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005576 if (is_mesh_interface(nlmode) &&
5577 nl80211_mgmt_subscribe_mesh(bss))
5578 return -1;
5579
Dmitry Shmidt04949592012-07-19 12:16:46 -07005580 if (!bss->in_deinit && !is_ap_interface(nlmode) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005581 !is_mesh_interface(nlmode) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005582 nl80211_mgmt_subscribe_non_ap(bss) < 0)
5583 wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
5584 "frame processing - ignore for now");
5585
5586 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005587}
5588
5589
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005590int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
5591 enum nl80211_iftype nlmode)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005592{
5593 return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
5594}
5595
5596
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005597static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
5598 struct hostapd_freq_params *freq)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005599{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005600 return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07005601 freq);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07005602}
5603
5604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005605static int wpa_driver_nl80211_get_capa(void *priv,
5606 struct wpa_driver_capa *capa)
5607{
5608 struct i802_bss *bss = priv;
5609 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07005610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611 if (!drv->has_capability)
5612 return -1;
5613 os_memcpy(capa, &drv->capa, sizeof(*capa));
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005614 if (drv->extended_capa && drv->extended_capa_mask) {
5615 capa->extended_capa = drv->extended_capa;
5616 capa->extended_capa_mask = drv->extended_capa_mask;
5617 capa->extended_capa_len = drv->extended_capa_len;
5618 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005619
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005620 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005621}
5622
5623
5624static int wpa_driver_nl80211_set_operstate(void *priv, int state)
5625{
5626 struct i802_bss *bss = priv;
5627 struct wpa_driver_nl80211_data *drv = bss->drv;
5628
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005629 wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
5630 bss->ifname, drv->operstate, state,
5631 state ? "UP" : "DORMANT");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005632 drv->operstate = state;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005633 return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005634 state ? IF_OPER_UP : IF_OPER_DORMANT);
5635}
5636
5637
5638static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
5639{
5640 struct i802_bss *bss = priv;
5641 struct wpa_driver_nl80211_data *drv = bss->drv;
5642 struct nl_msg *msg;
5643 struct nl80211_sta_flag_update upd;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005644 int ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005645
5646 if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
5647 wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
5648 return 0;
5649 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005650
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005651 wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
5652 MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
5653
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005654 os_memset(&upd, 0, sizeof(upd));
5655 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
5656 if (authorized)
5657 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005658
5659 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5660 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
5661 nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
5662 nlmsg_free(msg);
5663 return -ENOBUFS;
5664 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005665
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005666 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005667 if (!ret)
5668 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005669 wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
5670 ret, strerror(-ret));
5671 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005672}
5673
5674
Jouni Malinen75ecf522011-06-27 15:19:46 -07005675/* Set kernel driver on given frequency (MHz) */
5676static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005677{
Jouni Malinen75ecf522011-06-27 15:19:46 -07005678 struct i802_bss *bss = priv;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07005679 return nl80211_set_channel(bss, freq, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005680}
5681
5682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005683static inline int min_int(int a, int b)
5684{
5685 if (a < b)
5686 return a;
5687 return b;
5688}
5689
5690
5691static int get_key_handler(struct nl_msg *msg, void *arg)
5692{
5693 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5694 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5695
5696 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5697 genlmsg_attrlen(gnlh, 0), NULL);
5698
5699 /*
5700 * TODO: validate the key index and mac address!
5701 * Otherwise, there's a race condition as soon as
5702 * the kernel starts sending key notifications.
5703 */
5704
5705 if (tb[NL80211_ATTR_KEY_SEQ])
5706 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
5707 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
5708 return NL_SKIP;
5709}
5710
5711
5712static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
5713 int idx, u8 *seq)
5714{
5715 struct i802_bss *bss = priv;
5716 struct wpa_driver_nl80211_data *drv = bss->drv;
5717 struct nl_msg *msg;
5718
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005719 msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
5720 NL80211_CMD_GET_KEY);
5721 if (!msg ||
5722 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
5723 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
5724 nlmsg_free(msg);
5725 return -ENOBUFS;
5726 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005727
5728 memset(seq, 0, 6);
5729
5730 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005731}
5732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005733
5734static int i802_set_rts(void *priv, int rts)
5735{
5736 struct i802_bss *bss = priv;
5737 struct wpa_driver_nl80211_data *drv = bss->drv;
5738 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005739 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005740 u32 val;
5741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005742 if (rts >= 2347)
5743 val = (u32) -1;
5744 else
5745 val = rts;
5746
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005747 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5748 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
5749 nlmsg_free(msg);
5750 return -ENOBUFS;
5751 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005752
5753 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5754 if (!ret)
5755 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005756 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
5757 "%d (%s)", rts, ret, strerror(-ret));
5758 return ret;
5759}
5760
5761
5762static int i802_set_frag(void *priv, int frag)
5763{
5764 struct i802_bss *bss = priv;
5765 struct wpa_driver_nl80211_data *drv = bss->drv;
5766 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005767 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005768 u32 val;
5769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005770 if (frag >= 2346)
5771 val = (u32) -1;
5772 else
5773 val = frag;
5774
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005775 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
5776 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
5777 nlmsg_free(msg);
5778 return -ENOBUFS;
5779 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005780
5781 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5782 if (!ret)
5783 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005784 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
5785 "%d: %d (%s)", frag, ret, strerror(-ret));
5786 return ret;
5787}
5788
5789
5790static int i802_flush(void *priv)
5791{
5792 struct i802_bss *bss = priv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005793 struct nl_msg *msg;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005794 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005795
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005796 wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
5797 bss->ifname);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005798
5799 /*
5800 * XXX: FIX! this needs to flush all VLANs too
5801 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005802 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
5803 res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005804 if (res) {
5805 wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
5806 "(%s)", res, strerror(-res));
5807 }
5808 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005809}
5810
5811
5812static int get_sta_handler(struct nl_msg *msg, void *arg)
5813{
5814 struct nlattr *tb[NL80211_ATTR_MAX + 1];
5815 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
5816 struct hostap_sta_driver_data *data = arg;
5817 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
5818 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
5819 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
5820 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
5821 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
5822 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
5823 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005824 [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005825 [NL80211_STA_INFO_RX_BYTES64] = { .type = NLA_U64 },
5826 [NL80211_STA_INFO_TX_BYTES64] = { .type = NLA_U64 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005827 };
5828
5829 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
5830 genlmsg_attrlen(gnlh, 0), NULL);
5831
5832 /*
5833 * TODO: validate the interface and mac address!
5834 * Otherwise, there's a race condition as soon as
5835 * the kernel starts sending station notifications.
5836 */
5837
5838 if (!tb[NL80211_ATTR_STA_INFO]) {
5839 wpa_printf(MSG_DEBUG, "sta stats missing!");
5840 return NL_SKIP;
5841 }
5842 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
5843 tb[NL80211_ATTR_STA_INFO],
5844 stats_policy)) {
5845 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
5846 return NL_SKIP;
5847 }
5848
5849 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
5850 data->inactive_msec =
5851 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005852 /* For backwards compatibility, fetch the 32-bit counters first. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005853 if (stats[NL80211_STA_INFO_RX_BYTES])
5854 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
5855 if (stats[NL80211_STA_INFO_TX_BYTES])
5856 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005857 if (stats[NL80211_STA_INFO_RX_BYTES64] &&
5858 stats[NL80211_STA_INFO_TX_BYTES64]) {
5859 /*
5860 * The driver supports 64-bit counters, so use them to override
5861 * the 32-bit values.
5862 */
5863 data->rx_bytes =
5864 nla_get_u64(stats[NL80211_STA_INFO_RX_BYTES64]);
5865 data->tx_bytes =
5866 nla_get_u64(stats[NL80211_STA_INFO_TX_BYTES64]);
5867 data->bytes_64bit = 1;
5868 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005869 if (stats[NL80211_STA_INFO_RX_PACKETS])
5870 data->rx_packets =
5871 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
5872 if (stats[NL80211_STA_INFO_TX_PACKETS])
5873 data->tx_packets =
5874 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
Jouni Malinen1e6c57f2012-09-05 17:07:03 +03005875 if (stats[NL80211_STA_INFO_TX_FAILED])
5876 data->tx_retry_failed =
5877 nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005878
5879 return NL_SKIP;
5880}
5881
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005882static int i802_read_sta_data(struct i802_bss *bss,
5883 struct hostap_sta_driver_data *data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005884 const u8 *addr)
5885{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005886 struct nl_msg *msg;
5887
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005888 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
5889 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
5890 nlmsg_free(msg);
5891 return -ENOBUFS;
5892 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005893
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005894 return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005895}
5896
5897
5898static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
5899 int cw_min, int cw_max, int burst_time)
5900{
5901 struct i802_bss *bss = priv;
5902 struct wpa_driver_nl80211_data *drv = bss->drv;
5903 struct nl_msg *msg;
5904 struct nlattr *txq, *params;
5905
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005906 msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005907 if (!msg)
5908 return -1;
5909
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005910 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
5911 if (!txq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005912 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005913
5914 /* We are only sending parameters for a single TXQ at a time */
5915 params = nla_nest_start(msg, 1);
5916 if (!params)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005917 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005918
5919 switch (queue) {
5920 case 0:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005921 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
5922 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005923 break;
5924 case 1:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005925 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
5926 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005927 break;
5928 case 2:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005929 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
5930 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005931 break;
5932 case 3:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005933 if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
5934 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005935 break;
5936 }
5937 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
5938 * 32 usec, so need to convert the value here. */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005939 if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
5940 (burst_time * 100 + 16) / 32) ||
5941 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
5942 nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
5943 nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
5944 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005945
5946 nla_nest_end(msg, params);
5947
5948 nla_nest_end(msg, txq);
5949
5950 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
5951 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005952 msg = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005953fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005954 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005955 return -1;
5956}
5957
5958
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005959static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005960 const char *ifname, int vlan_id)
5961{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005962 struct wpa_driver_nl80211_data *drv = bss->drv;
5963 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005964 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005965
Dmitry Shmidtcce06662013-11-04 18:44:24 -08005966 wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
5967 ", ifname=%s[%d], vlan_id=%d)",
5968 bss->ifname, if_nametoindex(bss->ifname),
5969 MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005970 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
5971 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
5972 nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
5973 nlmsg_free(msg);
5974 return -ENOBUFS;
5975 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005976
5977 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
5978 if (ret < 0) {
5979 wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
5980 MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
5981 MAC2STR(addr), ifname, vlan_id, ret,
5982 strerror(-ret));
5983 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005984 return ret;
5985}
5986
5987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005988static int i802_get_inact_sec(void *priv, const u8 *addr)
5989{
5990 struct hostap_sta_driver_data data;
5991 int ret;
5992
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08005993 os_memset(&data, 0, sizeof(data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005994 data.inactive_msec = (unsigned long) -1;
5995 ret = i802_read_sta_data(priv, &data, addr);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005996 if (ret == -ENOENT)
5997 return -ENOENT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005998 if (ret || data.inactive_msec == (unsigned long) -1)
5999 return -1;
6000 return data.inactive_msec / 1000;
6001}
6002
6003
6004static int i802_sta_clear_stats(void *priv, const u8 *addr)
6005{
6006#if 0
6007 /* TODO */
6008#endif
6009 return 0;
6010}
6011
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006012
6013static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
6014 int reason)
6015{
6016 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006017 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006018 struct ieee80211_mgmt mgmt;
6019
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006020 if (is_mesh_interface(drv->nlmode))
6021 return -1;
6022
Dmitry Shmidt04949592012-07-19 12:16:46 -07006023 if (drv->device_ap_sme)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006024 return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006026 memset(&mgmt, 0, sizeof(mgmt));
6027 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
6028 WLAN_FC_STYPE_DEAUTH);
6029 memcpy(mgmt.da, addr, ETH_ALEN);
6030 memcpy(mgmt.sa, own_addr, ETH_ALEN);
6031 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
6032 mgmt.u.deauth.reason_code = host_to_le16(reason);
6033 return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
6034 IEEE80211_HDRLEN +
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006035 sizeof(mgmt.u.deauth), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006036 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006037}
6038
6039
6040static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
6041 int reason)
6042{
6043 struct i802_bss *bss = priv;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006044 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006045 struct ieee80211_mgmt mgmt;
6046
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, 0, 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_DISASSOC);
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.disassoc.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.disassoc), 0, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006063 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006064}
6065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006066
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006067static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
6068{
6069 char buf[200], *pos, *end;
6070 int i, res;
6071
6072 pos = buf;
6073 end = pos + sizeof(buf);
6074
6075 for (i = 0; i < drv->num_if_indices; i++) {
6076 if (!drv->if_indices[i])
6077 continue;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006078 res = os_snprintf(pos, end - pos, " %d(%d)",
6079 drv->if_indices[i],
6080 drv->if_indices_reason[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006081 if (os_snprintf_error(end - pos, res))
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006082 break;
6083 pos += res;
6084 }
6085 *pos = '\0';
6086
6087 wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
6088 drv->num_if_indices, buf);
6089}
6090
6091
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006092static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6093 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006094{
6095 int i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006096 int *old, *old_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006097
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006098 wpa_printf(MSG_DEBUG,
6099 "nl80211: Add own interface ifindex %d (ifidx_reason %d)",
6100 ifidx, ifidx_reason);
6101 if (have_ifidx(drv, ifidx, ifidx_reason)) {
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006102 wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
6103 ifidx);
6104 return;
6105 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006106 for (i = 0; i < drv->num_if_indices; i++) {
6107 if (drv->if_indices[i] == 0) {
6108 drv->if_indices[i] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006109 drv->if_indices_reason[i] = ifidx_reason;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006110 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006111 return;
6112 }
6113 }
6114
6115 if (drv->if_indices != drv->default_if_indices)
6116 old = drv->if_indices;
6117 else
6118 old = NULL;
6119
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006120 if (drv->if_indices_reason != drv->default_if_indices_reason)
6121 old_reason = drv->if_indices_reason;
6122 else
6123 old_reason = NULL;
6124
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006125 drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
6126 sizeof(int));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006127 drv->if_indices_reason = os_realloc_array(old_reason,
6128 drv->num_if_indices + 1,
6129 sizeof(int));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006130 if (!drv->if_indices) {
6131 if (!old)
6132 drv->if_indices = drv->default_if_indices;
6133 else
6134 drv->if_indices = old;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006135 }
6136 if (!drv->if_indices_reason) {
6137 if (!old_reason)
6138 drv->if_indices_reason = drv->default_if_indices_reason;
6139 else
Dmitry Shmidte4663042016-04-04 10:07:49 -07006140 drv->if_indices_reason = old_reason;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006141 }
6142 if (!drv->if_indices || !drv->if_indices_reason) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07006143 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
6144 "interfaces");
6145 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
6146 return;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006147 }
6148 if (!old)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006149 os_memcpy(drv->if_indices, drv->default_if_indices,
6150 sizeof(drv->default_if_indices));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006151 if (!old_reason)
6152 os_memcpy(drv->if_indices_reason,
6153 drv->default_if_indices_reason,
6154 sizeof(drv->default_if_indices_reason));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006155 drv->if_indices[drv->num_if_indices] = ifidx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006156 drv->if_indices_reason[drv->num_if_indices] = ifidx_reason;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006157 drv->num_if_indices++;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006158 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006159}
6160
6161
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006162static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6163 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006164{
6165 int i;
6166
6167 for (i = 0; i < drv->num_if_indices; i++) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006168 if ((drv->if_indices[i] == ifidx || ifidx == IFIDX_ANY) &&
6169 (drv->if_indices_reason[i] == ifidx_reason ||
6170 ifidx_reason == IFIDX_ANY)) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07006171 drv->if_indices[i] = 0;
6172 break;
6173 }
6174 }
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006175 dump_ifidx(drv);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006176}
6177
6178
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006179static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx,
6180 int ifidx_reason)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006181{
6182 int i;
6183
6184 for (i = 0; i < drv->num_if_indices; i++)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006185 if (drv->if_indices[i] == ifidx &&
6186 (drv->if_indices_reason[i] == ifidx_reason ||
6187 ifidx_reason == IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07006188 return 1;
6189
6190 return 0;
6191}
6192
6193
6194static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006195 const char *bridge_ifname, char *ifname_wds)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006196{
6197 struct i802_bss *bss = priv;
6198 struct wpa_driver_nl80211_data *drv = bss->drv;
6199 char name[IFNAMSIZ + 1];
6200
6201 os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006202 if (ifname_wds)
6203 os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
6204
Jouni Malinen75ecf522011-06-27 15:19:46 -07006205 wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
6206 " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
6207 if (val) {
6208 if (!if_nametoindex(name)) {
6209 if (nl80211_create_iface(drv, name,
6210 NL80211_IFTYPE_AP_VLAN,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006211 bss->addr, 1, NULL, NULL, 0) <
6212 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006213 return -1;
6214 if (bridge_ifname &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006215 linux_br_add_if(drv->global->ioctl_sock,
6216 bridge_ifname, name) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006217 return -1;
6218 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006219 if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
6220 wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
6221 "interface %s up", name);
6222 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07006223 return i802_set_sta_vlan(priv, addr, name, 0);
6224 } else {
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006225 if (bridge_ifname)
6226 linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
6227 name);
6228
Jouni Malinen75ecf522011-06-27 15:19:46 -07006229 i802_set_sta_vlan(priv, addr, bss->ifname, 0);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08006230 nl80211_remove_iface(drv, if_nametoindex(name));
6231 return 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006232 }
6233}
6234
6235
6236static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
6237{
6238 struct wpa_driver_nl80211_data *drv = eloop_ctx;
6239 struct sockaddr_ll lladdr;
6240 unsigned char buf[3000];
6241 int len;
6242 socklen_t fromlen = sizeof(lladdr);
6243
6244 len = recvfrom(sock, buf, sizeof(buf), 0,
6245 (struct sockaddr *)&lladdr, &fromlen);
6246 if (len < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006247 wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
6248 strerror(errno));
Jouni Malinen75ecf522011-06-27 15:19:46 -07006249 return;
6250 }
6251
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006252 if (have_ifidx(drv, lladdr.sll_ifindex, IFIDX_ANY))
Jouni Malinen75ecf522011-06-27 15:19:46 -07006253 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
6254}
6255
6256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006257static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
6258 struct i802_bss *bss,
6259 const char *brname, const char *ifname)
6260{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006261 int br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006262 char in_br[IFNAMSIZ];
6263
6264 os_strlcpy(bss->brname, brname, IFNAMSIZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006265 br_ifindex = if_nametoindex(brname);
6266 if (br_ifindex == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006267 /*
6268 * Bridge was configured, but the bridge device does
6269 * not exist. Try to add it now.
6270 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006271 if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006272 wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
6273 "bridge interface %s: %s",
6274 brname, strerror(errno));
6275 return -1;
6276 }
6277 bss->added_bridge = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006278 br_ifindex = if_nametoindex(brname);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006279 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006280 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006281 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006282
6283 if (linux_br_get(in_br, ifname) == 0) {
6284 if (os_strcmp(in_br, brname) == 0)
6285 return 0; /* already in the bridge */
6286
6287 wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
6288 "bridge %s", ifname, in_br);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006289 if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
6290 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006291 wpa_printf(MSG_ERROR, "nl80211: Failed to "
6292 "remove interface %s from bridge "
6293 "%s: %s",
6294 ifname, brname, strerror(errno));
6295 return -1;
6296 }
6297 }
6298
6299 wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
6300 ifname, brname);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006301 if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006302 wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
6303 "into bridge %s: %s",
6304 ifname, brname, strerror(errno));
6305 return -1;
6306 }
6307 bss->added_if_into_bridge = 1;
6308
6309 return 0;
6310}
6311
6312
6313static void *i802_init(struct hostapd_data *hapd,
6314 struct wpa_init_params *params)
6315{
6316 struct wpa_driver_nl80211_data *drv;
6317 struct i802_bss *bss;
6318 size_t i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006319 char master_ifname[IFNAMSIZ];
6320 int ifindex, br_ifindex = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006321 int br_added = 0;
6322
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006323 bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
6324 params->global_priv, 1,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006325 params->bssid, params->driver_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006326 if (bss == NULL)
6327 return NULL;
6328
6329 drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006330
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006331 if (linux_br_get(master_ifname, params->ifname) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006332 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006333 params->ifname, master_ifname);
6334 br_ifindex = if_nametoindex(master_ifname);
6335 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6336 } else if ((params->num_bridge == 0 || !params->bridge[0]) &&
6337 linux_master_get(master_ifname, params->ifname) == 0) {
6338 wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in master %s",
6339 params->ifname, master_ifname);
6340 /* start listening for EAPOL on the master interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006341 add_ifidx(drv, if_nametoindex(master_ifname), drv->ifindex);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006342
6343 /* check if master itself is under bridge */
6344 if (linux_br_get(master_ifname, master_ifname) == 0) {
6345 wpa_printf(MSG_DEBUG, "nl80211: which is in bridge %s",
6346 master_ifname);
6347 br_ifindex = if_nametoindex(master_ifname);
6348 os_strlcpy(bss->brname, master_ifname, IFNAMSIZ);
6349 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006350 } else {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006351 master_ifname[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006352 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006353
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006354 bss->br_ifindex = br_ifindex;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006356 for (i = 0; i < params->num_bridge; i++) {
6357 if (params->bridge[i]) {
6358 ifindex = if_nametoindex(params->bridge[i]);
6359 if (ifindex)
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006360 add_ifidx(drv, ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006361 if (ifindex == br_ifindex)
6362 br_added = 1;
6363 }
6364 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006365
6366 /* start listening for EAPOL on the default AP interface */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006367 add_ifidx(drv, drv->ifindex, IFIDX_ANY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006368
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006369 if (params->num_bridge && params->bridge[0]) {
6370 if (i802_check_bridge(drv, bss, params->bridge[0],
6371 params->ifname) < 0)
6372 goto failed;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006373 if (os_strcmp(params->bridge[0], master_ifname) != 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006374 br_added = 1;
6375 }
6376
6377 if (!br_added && br_ifindex &&
6378 (params->num_bridge == 0 || !params->bridge[0]))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006379 add_ifidx(drv, br_ifindex, drv->ifindex);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006380
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006381#ifdef CONFIG_LIBNL3_ROUTE
6382 if (bss->added_if_into_bridge) {
6383 drv->rtnl_sk = nl_socket_alloc();
6384 if (drv->rtnl_sk == NULL) {
6385 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
6386 goto failed;
6387 }
6388
6389 if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
6390 wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
6391 strerror(errno));
6392 goto failed;
6393 }
6394 }
6395#endif /* CONFIG_LIBNL3_ROUTE */
6396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006397 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
6398 if (drv->eapol_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006399 wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
6400 strerror(errno));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006401 goto failed;
6402 }
6403
6404 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
6405 {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006406 wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006407 goto failed;
6408 }
6409
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006410 if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
6411 params->own_addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006412 goto failed;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006413 os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006414
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006415 memcpy(bss->addr, params->own_addr, ETH_ALEN);
6416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006417 return bss;
6418
6419failed:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006420 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006421 return NULL;
6422}
6423
6424
6425static void i802_deinit(void *priv)
6426{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006427 struct i802_bss *bss = priv;
6428 wpa_driver_nl80211_deinit(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006429}
6430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006431
6432static enum nl80211_iftype wpa_driver_nl80211_if_type(
6433 enum wpa_driver_if_type type)
6434{
6435 switch (type) {
6436 case WPA_IF_STATION:
6437 return NL80211_IFTYPE_STATION;
6438 case WPA_IF_P2P_CLIENT:
6439 case WPA_IF_P2P_GROUP:
6440 return NL80211_IFTYPE_P2P_CLIENT;
6441 case WPA_IF_AP_VLAN:
6442 return NL80211_IFTYPE_AP_VLAN;
6443 case WPA_IF_AP_BSS:
6444 return NL80211_IFTYPE_AP;
6445 case WPA_IF_P2P_GO:
6446 return NL80211_IFTYPE_P2P_GO;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006447 case WPA_IF_P2P_DEVICE:
6448 return NL80211_IFTYPE_P2P_DEVICE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006449 case WPA_IF_MESH:
6450 return NL80211_IFTYPE_MESH_POINT;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006451 default:
6452 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006453 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006454}
6455
6456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006457static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
6458{
6459 struct wpa_driver_nl80211_data *drv;
6460 dl_list_for_each(drv, &global->interfaces,
6461 struct wpa_driver_nl80211_data, list) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006462 if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006463 return 1;
6464 }
6465 return 0;
6466}
6467
6468
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006469static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006470{
6471 unsigned int idx;
6472
6473 if (!drv->global)
6474 return -1;
6475
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006476 os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006477 for (idx = 0; idx < 64; idx++) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006478 new_addr[0] = drv->first_bss->addr[0] | 0x02;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006479 new_addr[0] ^= idx << 2;
6480 if (!nl80211_addr_in_use(drv->global, new_addr))
6481 break;
6482 }
6483 if (idx == 64)
6484 return -1;
6485
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006486 wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006487 MACSTR, MAC2STR(new_addr));
6488
6489 return 0;
6490}
6491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006492
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006493struct wdev_info {
6494 u64 wdev_id;
6495 int wdev_id_set;
6496 u8 macaddr[ETH_ALEN];
6497};
6498
6499static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
6500{
6501 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6502 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6503 struct wdev_info *wi = arg;
6504
6505 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6506 genlmsg_attrlen(gnlh, 0), NULL);
6507 if (tb[NL80211_ATTR_WDEV]) {
6508 wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
6509 wi->wdev_id_set = 1;
6510 }
6511
6512 if (tb[NL80211_ATTR_MAC])
6513 os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
6514 ETH_ALEN);
6515
6516 return NL_SKIP;
6517}
6518
6519
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006520static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
6521 const char *ifname, const u8 *addr,
6522 void *bss_ctx, void **drv_priv,
6523 char *force_ifname, u8 *if_addr,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006524 const char *bridge, int use_existing,
6525 int setup_ap)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006526{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006527 enum nl80211_iftype nlmode;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006528 struct i802_bss *bss = priv;
6529 struct wpa_driver_nl80211_data *drv = bss->drv;
6530 int ifidx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006531 int added = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006532
6533 if (addr)
6534 os_memcpy(if_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006535 nlmode = wpa_driver_nl80211_if_type(type);
6536 if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
6537 struct wdev_info p2pdev_info;
6538
6539 os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
6540 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
6541 0, nl80211_wdev_handler,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006542 &p2pdev_info, use_existing);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006543 if (!p2pdev_info.wdev_id_set || ifidx != 0) {
6544 wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
6545 ifname);
6546 return -1;
6547 }
6548
6549 drv->global->if_add_wdevid = p2pdev_info.wdev_id;
6550 drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
6551 if (!is_zero_ether_addr(p2pdev_info.macaddr))
6552 os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
6553 wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
6554 ifname,
6555 (long long unsigned int) p2pdev_info.wdev_id);
6556 } else {
6557 ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006558 0, NULL, NULL, use_existing);
6559 if (use_existing && ifidx == -ENFILE) {
6560 added = 0;
6561 ifidx = if_nametoindex(ifname);
6562 } else if (ifidx < 0) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006563 return -1;
6564 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006565 }
6566
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006567 if (!addr) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006568 if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006569 os_memcpy(if_addr, bss->addr, ETH_ALEN);
6570 else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006571 ifname, if_addr) < 0) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006572 if (added)
6573 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006574 return -1;
6575 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006576 }
6577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006578 if (!addr &&
6579 (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006580 type == WPA_IF_P2P_GO || type == WPA_IF_MESH ||
6581 type == WPA_IF_STATION)) {
6582 /* Enforce unique address */
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006583 u8 new_addr[ETH_ALEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006584
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006585 if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006586 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006587 if (added)
6588 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006589 return -1;
6590 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006591 if (nl80211_addr_in_use(drv->global, new_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006592 wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07006593 "for interface %s type %d", ifname, type);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006594 if (nl80211_vif_addr(drv, new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006595 if (added)
6596 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006597 return -1;
6598 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006599 if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006600 new_addr) < 0) {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006601 if (added)
6602 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006603 return -1;
6604 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006605 }
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006606 os_memcpy(if_addr, new_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006607 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006608
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006609 if (type == WPA_IF_AP_BSS && setup_ap) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006610 struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
6611 if (new_bss == NULL) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006612 if (added)
6613 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006614 return -1;
6615 }
6616
6617 if (bridge &&
6618 i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
6619 wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
6620 "interface %s to a bridge %s",
6621 ifname, bridge);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006622 if (added)
6623 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006624 os_free(new_bss);
6625 return -1;
6626 }
6627
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006628 if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
6629 {
Dmitry Shmidt71757432014-06-02 13:50:35 -07006630 if (added)
6631 nl80211_remove_iface(drv, ifidx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006632 os_free(new_bss);
6633 return -1;
6634 }
6635 os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006636 os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006637 new_bss->ifindex = ifidx;
6638 new_bss->drv = drv;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006639 new_bss->next = drv->first_bss->next;
6640 new_bss->freq = drv->first_bss->freq;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006641 new_bss->ctx = bss_ctx;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006642 new_bss->added_if = added;
6643 drv->first_bss->next = new_bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006644 if (drv_priv)
6645 *drv_priv = new_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006646 nl80211_init_bss(new_bss);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006647
6648 /* Subscribe management frames for this WPA_IF_AP_BSS */
6649 if (nl80211_setup_ap(new_bss))
6650 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006651 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006652
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006653 if (drv->global)
6654 drv->global->if_add_ifindex = ifidx;
6655
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006656 /*
6657 * Some virtual interfaces need to process EAPOL packets and events on
6658 * the parent interface. This is used mainly with hostapd.
6659 */
6660 if (ifidx > 0 &&
6661 (drv->hostapd ||
6662 nlmode == NL80211_IFTYPE_AP_VLAN ||
6663 nlmode == NL80211_IFTYPE_WDS ||
6664 nlmode == NL80211_IFTYPE_MONITOR))
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006665 add_ifidx(drv, ifidx, IFIDX_ANY);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006666
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006667 return 0;
6668}
6669
6670
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006671static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006672 enum wpa_driver_if_type type,
6673 const char *ifname)
6674{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006675 struct wpa_driver_nl80211_data *drv = bss->drv;
6676 int ifindex = if_nametoindex(ifname);
6677
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006678 wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
6679 __func__, type, ifname, ifindex, bss->added_if);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006680 if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
Dmitry Shmidt051af732013-10-22 13:52:46 -07006681 nl80211_remove_iface(drv, ifindex);
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006682 else if (ifindex > 0 && !bss->added_if) {
6683 struct wpa_driver_nl80211_data *drv2;
6684 dl_list_for_each(drv2, &drv->global->interfaces,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006685 struct wpa_driver_nl80211_data, list) {
6686 del_ifidx(drv2, ifindex, IFIDX_ANY);
6687 del_ifidx(drv2, IFIDX_ANY, ifindex);
6688 }
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07006689 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006690
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006691 if (type != WPA_IF_AP_BSS)
6692 return 0;
6693
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006694 if (bss->added_if_into_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006695 if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
6696 bss->ifname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006697 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6698 "interface %s from bridge %s: %s",
6699 bss->ifname, bss->brname, strerror(errno));
6700 }
6701 if (bss->added_bridge) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006702 if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006703 wpa_printf(MSG_INFO, "nl80211: Failed to remove "
6704 "bridge %s: %s",
6705 bss->brname, strerror(errno));
6706 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006707
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006708 if (bss != drv->first_bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006709 struct i802_bss *tbss;
6710
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006711 wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
6712 for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006713 if (tbss->next == bss) {
6714 tbss->next = bss->next;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006715 /* Unsubscribe management frames */
6716 nl80211_teardown_ap(bss);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006717 nl80211_destroy_bss(bss);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006718 if (!bss->added_if)
6719 i802_set_iface_flags(bss, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006720 os_free(bss);
6721 bss = NULL;
6722 break;
6723 }
6724 }
6725 if (bss)
6726 wpa_printf(MSG_INFO, "nl80211: %s - could not find "
6727 "BSS %p in the list", __func__, bss);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08006728 } else {
6729 wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
6730 nl80211_teardown_ap(bss);
6731 if (!bss->added_if && !drv->first_bss->next)
6732 wpa_driver_nl80211_del_beacon(drv);
6733 nl80211_destroy_bss(bss);
6734 if (!bss->added_if)
6735 i802_set_iface_flags(bss, 0);
6736 if (drv->first_bss->next) {
6737 drv->first_bss = drv->first_bss->next;
6738 drv->ctx = drv->first_bss->ctx;
6739 os_free(bss);
6740 } else {
6741 wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
6742 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006743 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006744
6745 return 0;
6746}
6747
6748
6749static int cookie_handler(struct nl_msg *msg, void *arg)
6750{
6751 struct nlattr *tb[NL80211_ATTR_MAX + 1];
6752 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
6753 u64 *cookie = arg;
6754 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
6755 genlmsg_attrlen(gnlh, 0), NULL);
6756 if (tb[NL80211_ATTR_COOKIE])
6757 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
6758 return NL_SKIP;
6759}
6760
6761
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006762static int nl80211_send_frame_cmd(struct i802_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006763 unsigned int freq, unsigned int wait,
6764 const u8 *buf, size_t buf_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006765 u64 *cookie_out, int no_cck, int no_ack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006766 int offchanok, const u16 *csa_offs,
6767 size_t csa_offs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006768{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006769 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006770 struct nl_msg *msg;
6771 u64 cookie;
6772 int ret = -1;
6773
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07006774 wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
Dmitry Shmidt04949592012-07-19 12:16:46 -07006775 "no_ack=%d offchanok=%d",
6776 freq, wait, no_cck, no_ack, offchanok);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006777 wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006778
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006779 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
6780 (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
6781 (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
6782 (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6783 drv->test_use_roc_tx) &&
6784 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
6785 (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
6786 (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006787 (csa_offs && nla_put(msg, NL80211_ATTR_CSA_C_OFFSETS_TX,
6788 csa_offs_len * sizeof(u16), csa_offs)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006789 nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
6790 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006791
6792 cookie = 0;
6793 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6794 msg = NULL;
6795 if (ret) {
6796 wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006797 "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
6798 freq, wait);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006799 } else {
6800 wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
6801 "cookie 0x%llx", no_ack ? " (no ACK)" : "",
6802 (long long unsigned int) cookie);
6803
6804 if (cookie_out)
6805 *cookie_out = no_ack ? (u64) -1 : cookie;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006806
6807 if (drv->num_send_action_cookies == MAX_SEND_ACTION_COOKIES) {
6808 wpa_printf(MSG_DEBUG,
6809 "nl80211: Drop oldest pending send action cookie 0x%llx",
6810 (long long unsigned int)
6811 drv->send_action_cookies[0]);
6812 os_memmove(&drv->send_action_cookies[0],
6813 &drv->send_action_cookies[1],
6814 (MAX_SEND_ACTION_COOKIES - 1) *
6815 sizeof(u64));
6816 drv->num_send_action_cookies--;
6817 }
6818 drv->send_action_cookies[drv->num_send_action_cookies] = cookie;
6819 drv->num_send_action_cookies++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006820 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006821
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006822fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006823 nlmsg_free(msg);
6824 return ret;
6825}
6826
6827
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006828static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
6829 unsigned int freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006830 unsigned int wait_time,
6831 const u8 *dst, const u8 *src,
6832 const u8 *bssid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006833 const u8 *data, size_t data_len,
6834 int no_cck)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006835{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006836 struct wpa_driver_nl80211_data *drv = bss->drv;
6837 int ret = -1;
6838 u8 *buf;
6839 struct ieee80211_hdr *hdr;
6840
6841 wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006842 "freq=%u MHz wait=%d ms no_cck=%d)",
6843 drv->ifindex, freq, wait_time, no_cck);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006844
6845 buf = os_zalloc(24 + data_len);
6846 if (buf == NULL)
6847 return ret;
6848 os_memcpy(buf + 24, data, data_len);
6849 hdr = (struct ieee80211_hdr *) buf;
6850 hdr->frame_control =
6851 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
6852 os_memcpy(hdr->addr1, dst, ETH_ALEN);
6853 os_memcpy(hdr->addr2, src, ETH_ALEN);
6854 os_memcpy(hdr->addr3, bssid, ETH_ALEN);
6855
Dmitry Shmidt56052862013-10-04 10:23:25 -07006856 if (is_ap_interface(drv->nlmode) &&
6857 (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
6858 (int) freq == bss->freq || drv->device_ap_sme ||
6859 !drv->use_monitor))
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006860 ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
6861 0, freq, no_cck, 1,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006862 wait_time, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006863 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006864 ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006865 24 + data_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006866 &drv->send_action_cookie,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006867 no_cck, 0, 1, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006868
6869 os_free(buf);
6870 return ret;
6871}
6872
6873
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006874static void nl80211_frame_wait_cancel(struct i802_bss *bss, u64 cookie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006875{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006876 struct wpa_driver_nl80211_data *drv = bss->drv;
6877 struct nl_msg *msg;
6878 int ret;
6879
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08006880 wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006881 (long long unsigned int) cookie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006882 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006883 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006884 nlmsg_free(msg);
6885 return;
6886 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006887
6888 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006889 if (ret)
6890 wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
6891 "(%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006892}
6893
6894
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006895static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
6896{
6897 struct i802_bss *bss = priv;
6898 struct wpa_driver_nl80211_data *drv = bss->drv;
6899 unsigned int i;
6900 u64 cookie;
6901
6902 /* Cancel the last pending TX cookie */
6903 nl80211_frame_wait_cancel(bss, drv->send_action_cookie);
6904
6905 /*
6906 * Cancel the other pending TX cookies, if any. This is needed since
6907 * the driver may keep a list of all pending offchannel TX operations
6908 * and free up the radio only once they have expired or cancelled.
6909 */
6910 for (i = drv->num_send_action_cookies; i > 0; i--) {
6911 cookie = drv->send_action_cookies[i - 1];
6912 if (cookie != drv->send_action_cookie)
6913 nl80211_frame_wait_cancel(bss, cookie);
6914 }
6915 drv->num_send_action_cookies = 0;
6916}
6917
6918
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006919static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
6920 unsigned int duration)
6921{
6922 struct i802_bss *bss = priv;
6923 struct wpa_driver_nl80211_data *drv = bss->drv;
6924 struct nl_msg *msg;
6925 int ret;
6926 u64 cookie;
6927
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006928 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
6929 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
6930 nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
6931 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006932 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006933 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006934
6935 cookie = 0;
6936 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
6937 if (ret == 0) {
6938 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
6939 "0x%llx for freq=%u MHz duration=%u",
6940 (long long unsigned int) cookie, freq, duration);
6941 drv->remain_on_chan_cookie = cookie;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006942 drv->pending_remain_on_chan = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006943 return 0;
6944 }
6945 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
6946 "(freq=%d duration=%u): %d (%s)",
6947 freq, duration, ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006948 return -1;
6949}
6950
6951
6952static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
6953{
6954 struct i802_bss *bss = priv;
6955 struct wpa_driver_nl80211_data *drv = bss->drv;
6956 struct nl_msg *msg;
6957 int ret;
6958
6959 if (!drv->pending_remain_on_chan) {
6960 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
6961 "to cancel");
6962 return -1;
6963 }
6964
6965 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
6966 "0x%llx",
6967 (long long unsigned int) drv->remain_on_chan_cookie);
6968
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006969 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
6970 if (!msg ||
6971 nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
6972 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006973 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006974 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006975
6976 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
6977 if (ret == 0)
6978 return 0;
6979 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
6980 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006981 return -1;
6982}
6983
6984
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08006985static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006986{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006987 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07006988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006989 if (!report) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006990 if (bss->nl_preq && drv->device_ap_sme &&
Dmitry Shmidt03658832014-08-13 11:03:49 -07006991 is_ap_interface(drv->nlmode) && !bss->in_deinit &&
6992 !bss->static_ap) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006993 /*
6994 * Do not disable Probe Request reporting that was
6995 * enabled in nl80211_setup_ap().
6996 */
6997 wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
6998 "Probe Request reporting nl_preq=%p while "
6999 "in AP mode", bss->nl_preq);
7000 } else if (bss->nl_preq) {
7001 wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
7002 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007003 nl80211_destroy_eloop_handle(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007004 }
7005 return 0;
7006 }
7007
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007008 if (bss->nl_preq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007009 wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007010 "already on! nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007011 return 0;
7012 }
7013
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007014 bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
7015 if (bss->nl_preq == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007016 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007017 wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
7018 "reporting nl_preq=%p", bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007019
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007020 if (nl80211_register_frame(bss, bss->nl_preq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007021 (WLAN_FC_TYPE_MGMT << 2) |
7022 (WLAN_FC_STYPE_PROBE_REQ << 4),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007023 NULL, 0) < 0)
7024 goto out_err;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07007025
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007026 nl80211_register_eloop_read(&bss->nl_preq,
7027 wpa_driver_nl80211_event_receive,
7028 bss->nl_cb);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007029
7030 return 0;
7031
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007032 out_err:
7033 nl_destroy_handles(&bss->nl_preq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007034 return -1;
7035}
7036
7037
7038static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
7039 int ifindex, int disabled)
7040{
7041 struct nl_msg *msg;
7042 struct nlattr *bands, *band;
7043 int ret;
7044
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007045 wpa_printf(MSG_DEBUG,
7046 "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
7047 ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
7048 "no NL80211_TXRATE_LEGACY constraint");
7049
7050 msg = nl80211_ifindex_msg(drv, ifindex, 0,
7051 NL80211_CMD_SET_TX_BITRATE_MASK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007052 if (!msg)
7053 return -1;
7054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007055 bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
7056 if (!bands)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007057 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007058
7059 /*
7060 * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
7061 * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
7062 * rates. All 5 GHz rates are left enabled.
7063 */
7064 band = nla_nest_start(msg, NL80211_BAND_2GHZ);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007065 if (!band ||
7066 (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
7067 "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
7068 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007069 nla_nest_end(msg, band);
7070
7071 nla_nest_end(msg, bands);
7072
7073 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007074 if (ret) {
7075 wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
7076 "(%s)", ret, strerror(-ret));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007077 } else
7078 drv->disabled_11b_rates = disabled;
7079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007080 return ret;
7081
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007082fail:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007083 nlmsg_free(msg);
7084 return -1;
7085}
7086
7087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007088static int wpa_driver_nl80211_deinit_ap(void *priv)
7089{
7090 struct i802_bss *bss = priv;
7091 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007092 if (!is_ap_interface(drv->nlmode))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007093 return -1;
7094 wpa_driver_nl80211_del_beacon(drv);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007095 bss->beacon_set = 0;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007096
7097 /*
7098 * If the P2P GO interface was dynamically added, then it is
7099 * possible that the interface change to station is not possible.
7100 */
7101 if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
7102 return 0;
7103
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007104 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007105}
7106
7107
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007108static int wpa_driver_nl80211_stop_ap(void *priv)
7109{
7110 struct i802_bss *bss = priv;
7111 struct wpa_driver_nl80211_data *drv = bss->drv;
7112 if (!is_ap_interface(drv->nlmode))
7113 return -1;
7114 wpa_driver_nl80211_del_beacon(drv);
7115 bss->beacon_set = 0;
7116 return 0;
7117}
7118
7119
Dmitry Shmidt04949592012-07-19 12:16:46 -07007120static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
7121{
7122 struct i802_bss *bss = priv;
7123 struct wpa_driver_nl80211_data *drv = bss->drv;
7124 if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
7125 return -1;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007126
7127 /*
7128 * If the P2P Client interface was dynamically added, then it is
7129 * possible that the interface change to station is not possible.
7130 */
7131 if (bss->if_dynamic)
7132 return 0;
7133
Dmitry Shmidt04949592012-07-19 12:16:46 -07007134 return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
7135}
7136
7137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007138static void wpa_driver_nl80211_resume(void *priv)
7139{
7140 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007141 enum nl80211_iftype nlmode = nl80211_get_ifmode(bss);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007142
7143 if (i802_set_iface_flags(bss, 1))
7144 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007145
7146 if (is_p2p_net_interface(nlmode))
7147 nl80211_disable_11b_rates(bss->drv, bss->drv->ifindex, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007148}
7149
7150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007151static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
7152{
7153 struct i802_bss *bss = priv;
7154 struct wpa_driver_nl80211_data *drv = bss->drv;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007155 struct nl_msg *msg;
7156 struct nlattr *cqm;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007157
7158 wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
7159 "hysteresis=%d", threshold, hysteresis);
7160
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007161 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
7162 !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
7163 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
7164 nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
7165 nlmsg_free(msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007166 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007167 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07007168 nla_nest_end(msg, cqm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007169
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007170 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007171}
7172
7173
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007174static int get_channel_width(struct nl_msg *msg, void *arg)
7175{
7176 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7177 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7178 struct wpa_signal_info *sig_change = arg;
7179
7180 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7181 genlmsg_attrlen(gnlh, 0), NULL);
7182
7183 sig_change->center_frq1 = -1;
7184 sig_change->center_frq2 = -1;
7185 sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
7186
7187 if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
7188 sig_change->chanwidth = convert2width(
7189 nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
7190 if (tb[NL80211_ATTR_CENTER_FREQ1])
7191 sig_change->center_frq1 =
7192 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
7193 if (tb[NL80211_ATTR_CENTER_FREQ2])
7194 sig_change->center_frq2 =
7195 nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
7196 }
7197
7198 return NL_SKIP;
7199}
7200
7201
7202static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
7203 struct wpa_signal_info *sig)
7204{
7205 struct nl_msg *msg;
7206
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007207 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007208 return send_and_recv_msgs(drv, msg, get_channel_width, sig);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007209}
7210
7211
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007212static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
7213{
7214 struct i802_bss *bss = priv;
7215 struct wpa_driver_nl80211_data *drv = bss->drv;
7216 int res;
7217
7218 os_memset(si, 0, sizeof(*si));
7219 res = nl80211_get_link_signal(drv, si);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007220 if (res) {
7221 if (drv->nlmode != NL80211_IFTYPE_ADHOC &&
7222 drv->nlmode != NL80211_IFTYPE_MESH_POINT)
7223 return res;
7224 si->current_signal = 0;
7225 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007226
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007227 res = nl80211_get_channel_width(drv, si);
7228 if (res != 0)
7229 return res;
7230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007231 return nl80211_get_link_noise(drv, si);
7232}
7233
7234
7235static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
7236 int encrypt)
7237{
7238 struct i802_bss *bss = priv;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007239 return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007240 0, 0, 0, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007241}
7242
7243
7244static int nl80211_set_param(void *priv, const char *param)
7245{
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007246 struct i802_bss *bss = priv;
7247 struct wpa_driver_nl80211_data *drv = bss->drv;
7248
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007249 if (param == NULL)
7250 return 0;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08007251 wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007252
7253#ifdef CONFIG_P2P
7254 if (os_strstr(param, "use_p2p_group_interface=1")) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007255 wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
7256 "interface");
7257 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
7258 drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
7259 }
7260#endif /* CONFIG_P2P */
7261
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007262 if (os_strstr(param, "use_monitor=1"))
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007263 drv->use_monitor = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007264
7265 if (os_strstr(param, "force_connect_cmd=1")) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007266 drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007267 drv->force_connect_cmd = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007268 }
7269
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07007270 if (os_strstr(param, "force_bss_selection=1"))
7271 drv->capa.flags |= WPA_DRIVER_FLAGS_BSS_SELECTION;
7272
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08007273 if (os_strstr(param, "no_offchannel_tx=1")) {
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -08007274 drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
7275 drv->test_use_roc_tx = 1;
7276 }
7277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007278 return 0;
7279}
7280
7281
Dmitry Shmidte4663042016-04-04 10:07:49 -07007282static void * nl80211_global_init(void *ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007283{
7284 struct nl80211_global *global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007285 struct netlink_config *cfg;
7286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007287 global = os_zalloc(sizeof(*global));
7288 if (global == NULL)
7289 return NULL;
Dmitry Shmidte4663042016-04-04 10:07:49 -07007290 global->ctx = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007291 global->ioctl_sock = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007292 dl_list_init(&global->interfaces);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007293 global->if_add_ifindex = -1;
7294
7295 cfg = os_zalloc(sizeof(*cfg));
7296 if (cfg == NULL)
7297 goto err;
7298
7299 cfg->ctx = global;
7300 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
7301 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
7302 global->netlink = netlink_init(cfg);
7303 if (global->netlink == NULL) {
7304 os_free(cfg);
7305 goto err;
7306 }
7307
7308 if (wpa_driver_nl80211_init_nl_global(global) < 0)
7309 goto err;
7310
7311 global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
7312 if (global->ioctl_sock < 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007313 wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
7314 strerror(errno));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007315 goto err;
7316 }
7317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007318 return global;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007319
7320err:
7321 nl80211_global_deinit(global);
7322 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007323}
7324
7325
7326static void nl80211_global_deinit(void *priv)
7327{
7328 struct nl80211_global *global = priv;
7329 if (global == NULL)
7330 return;
7331 if (!dl_list_empty(&global->interfaces)) {
7332 wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
7333 "nl80211_global_deinit",
7334 dl_list_len(&global->interfaces));
7335 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007336
7337 if (global->netlink)
7338 netlink_deinit(global->netlink);
7339
7340 nl_destroy_handles(&global->nl);
7341
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007342 if (global->nl_event)
7343 nl80211_destroy_eloop_handle(&global->nl_event);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007344
7345 nl_cb_put(global->nl_cb);
7346
7347 if (global->ioctl_sock >= 0)
7348 close(global->ioctl_sock);
7349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007350 os_free(global);
7351}
7352
7353
7354static const char * nl80211_get_radio_name(void *priv)
7355{
7356 struct i802_bss *bss = priv;
7357 struct wpa_driver_nl80211_data *drv = bss->drv;
7358 return drv->phyname;
7359}
7360
7361
Jouni Malinen75ecf522011-06-27 15:19:46 -07007362static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
7363 const u8 *pmkid)
7364{
7365 struct nl_msg *msg;
7366
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007367 if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
7368 (pmkid && nla_put(msg, NL80211_ATTR_PMKID, 16, pmkid)) ||
7369 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))) {
7370 nlmsg_free(msg);
7371 return -ENOBUFS;
7372 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07007373
7374 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007375}
7376
7377
7378static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
7379{
7380 struct i802_bss *bss = priv;
7381 wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
7382 return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
7383}
7384
7385
7386static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
7387{
7388 struct i802_bss *bss = priv;
7389 wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
7390 MAC2STR(bssid));
7391 return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
7392}
7393
7394
7395static int nl80211_flush_pmkid(void *priv)
7396{
7397 struct i802_bss *bss = priv;
7398 wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
7399 return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
7400}
7401
7402
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007403static void clean_survey_results(struct survey_results *survey_results)
7404{
7405 struct freq_survey *survey, *tmp;
7406
7407 if (dl_list_empty(&survey_results->survey_list))
7408 return;
7409
7410 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
7411 struct freq_survey, list) {
7412 dl_list_del(&survey->list);
7413 os_free(survey);
7414 }
7415}
7416
7417
7418static void add_survey(struct nlattr **sinfo, u32 ifidx,
7419 struct dl_list *survey_list)
7420{
7421 struct freq_survey *survey;
7422
7423 survey = os_zalloc(sizeof(struct freq_survey));
7424 if (!survey)
7425 return;
7426
7427 survey->ifidx = ifidx;
7428 survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7429 survey->filled = 0;
7430
7431 if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
7432 survey->nf = (int8_t)
7433 nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
7434 survey->filled |= SURVEY_HAS_NF;
7435 }
7436
7437 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
7438 survey->channel_time =
7439 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
7440 survey->filled |= SURVEY_HAS_CHAN_TIME;
7441 }
7442
7443 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
7444 survey->channel_time_busy =
7445 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
7446 survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
7447 }
7448
7449 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
7450 survey->channel_time_rx =
7451 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
7452 survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
7453 }
7454
7455 if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
7456 survey->channel_time_tx =
7457 nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
7458 survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
7459 }
7460
7461 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)",
7462 survey->freq,
7463 survey->nf,
7464 (unsigned long int) survey->channel_time,
7465 (unsigned long int) survey->channel_time_busy,
7466 (unsigned long int) survey->channel_time_tx,
7467 (unsigned long int) survey->channel_time_rx,
7468 survey->filled);
7469
7470 dl_list_add_tail(survey_list, &survey->list);
7471}
7472
7473
7474static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
7475 unsigned int freq_filter)
7476{
7477 if (!freq_filter)
7478 return 1;
7479
7480 return freq_filter == surveyed_freq;
7481}
7482
7483
7484static int survey_handler(struct nl_msg *msg, void *arg)
7485{
7486 struct nlattr *tb[NL80211_ATTR_MAX + 1];
7487 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
7488 struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
7489 struct survey_results *survey_results;
7490 u32 surveyed_freq = 0;
7491 u32 ifidx;
7492
7493 static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
7494 [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
7495 [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
7496 };
7497
7498 survey_results = (struct survey_results *) arg;
7499
7500 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
7501 genlmsg_attrlen(gnlh, 0), NULL);
7502
Dmitry Shmidt97672262014-02-03 13:02:54 -08007503 if (!tb[NL80211_ATTR_IFINDEX])
7504 return NL_SKIP;
7505
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007506 ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
7507
7508 if (!tb[NL80211_ATTR_SURVEY_INFO])
7509 return NL_SKIP;
7510
7511 if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
7512 tb[NL80211_ATTR_SURVEY_INFO],
7513 survey_policy))
7514 return NL_SKIP;
7515
7516 if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
7517 wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
7518 return NL_SKIP;
7519 }
7520
7521 surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
7522
7523 if (!check_survey_ok(sinfo, surveyed_freq,
7524 survey_results->freq_filter))
7525 return NL_SKIP;
7526
7527 if (survey_results->freq_filter &&
7528 survey_results->freq_filter != surveyed_freq) {
7529 wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
7530 surveyed_freq);
7531 return NL_SKIP;
7532 }
7533
7534 add_survey(sinfo, ifidx, &survey_results->survey_list);
7535
7536 return NL_SKIP;
7537}
7538
7539
7540static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
7541{
7542 struct i802_bss *bss = priv;
7543 struct wpa_driver_nl80211_data *drv = bss->drv;
7544 struct nl_msg *msg;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007545 int err;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007546 union wpa_event_data data;
7547 struct survey_results *survey_results;
7548
7549 os_memset(&data, 0, sizeof(data));
7550 survey_results = &data.survey_results;
7551
7552 dl_list_init(&survey_results->survey_list);
7553
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007554 msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007555 if (!msg)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007556 return -ENOBUFS;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007557
7558 if (freq)
7559 data.survey_results.freq_filter = freq;
7560
7561 do {
7562 wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
7563 err = send_and_recv_msgs(drv, msg, survey_handler,
7564 survey_results);
7565 } while (err > 0);
7566
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007567 if (err)
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007568 wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007569 else
7570 wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007571
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007572 clean_survey_results(survey_results);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07007573 return err;
7574}
7575
7576
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007577static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
7578 const u8 *kck, size_t kck_len,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007579 const u8 *replay_ctr)
7580{
7581 struct i802_bss *bss = priv;
7582 struct wpa_driver_nl80211_data *drv = bss->drv;
7583 struct nlattr *replay_nested;
7584 struct nl_msg *msg;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007585 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007586
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007587 if (!drv->set_rekey_offload)
7588 return;
7589
7590 wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007591 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
7592 !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
Dmitry Shmidt807291d2015-01-27 13:40:23 -08007593 nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
7594 nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007595 nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
7596 replay_ctr)) {
7597 nl80211_nlmsg_clear(msg);
7598 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007599 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007600 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007601
7602 nla_nest_end(msg, replay_nested);
7603
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007604 ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
7605 if (ret == -EOPNOTSUPP) {
7606 wpa_printf(MSG_DEBUG,
7607 "nl80211: Driver does not support rekey offload");
7608 drv->set_rekey_offload = 0;
7609 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007610}
7611
7612
7613static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
7614 const u8 *addr, int qos)
7615{
7616 /* send data frame to poll STA and check whether
7617 * this frame is ACKed */
7618 struct {
7619 struct ieee80211_hdr hdr;
7620 u16 qos_ctl;
7621 } STRUCT_PACKED nulldata;
7622 size_t size;
7623
7624 /* Send data frame to poll STA and check whether this frame is ACKed */
7625
7626 os_memset(&nulldata, 0, sizeof(nulldata));
7627
7628 if (qos) {
7629 nulldata.hdr.frame_control =
7630 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7631 WLAN_FC_STYPE_QOS_NULL);
7632 size = sizeof(nulldata);
7633 } else {
7634 nulldata.hdr.frame_control =
7635 IEEE80211_FC(WLAN_FC_TYPE_DATA,
7636 WLAN_FC_STYPE_NULLFUNC);
7637 size = sizeof(struct ieee80211_hdr);
7638 }
7639
7640 nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
7641 os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
7642 os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
7643 os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
7644
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007645 if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007646 0, 0, NULL, 0) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007647 wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
7648 "send poll frame");
7649}
7650
7651static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
7652 int qos)
7653{
7654 struct i802_bss *bss = priv;
7655 struct wpa_driver_nl80211_data *drv = bss->drv;
7656 struct nl_msg *msg;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007657 int ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007658
7659 if (!drv->poll_command_supported) {
7660 nl80211_send_null_frame(bss, own_addr, addr, qos);
7661 return;
7662 }
7663
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007664 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
7665 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7666 nlmsg_free(msg);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007667 return;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007668 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007669
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007670 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7671 if (ret < 0) {
7672 wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
7673 MACSTR " failed: ret=%d (%s)",
7674 MAC2STR(addr), ret, strerror(-ret));
7675 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007676}
7677
7678
7679static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
7680{
7681 struct nl_msg *msg;
7682
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007683 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
7684 nla_put_u32(msg, NL80211_ATTR_PS_STATE,
7685 enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
7686 nlmsg_free(msg);
7687 return -ENOBUFS;
7688 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007689 return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007690}
7691
7692
7693static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
7694 int ctwindow)
7695{
7696 struct i802_bss *bss = priv;
7697
7698 wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
7699 "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
7700
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007701 if (opp_ps != -1 || ctwindow != -1) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007702#ifdef ANDROID_P2P
7703 wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007704#else /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007705 return -1; /* Not yet supported */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08007706#endif /* ANDROID_P2P */
7707 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007708
7709 if (legacy_ps == -1)
7710 return 0;
7711 if (legacy_ps != 0 && legacy_ps != 1)
7712 return -1; /* Not yet supported */
7713
7714 return nl80211_set_power_save(bss, legacy_ps);
7715}
7716
7717
Dmitry Shmidt051af732013-10-22 13:52:46 -07007718static int nl80211_start_radar_detection(void *priv,
7719 struct hostapd_freq_params *freq)
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007720{
7721 struct i802_bss *bss = priv;
7722 struct wpa_driver_nl80211_data *drv = bss->drv;
7723 struct nl_msg *msg;
7724 int ret;
7725
Dmitry Shmidt051af732013-10-22 13:52:46 -07007726 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)",
7727 freq->freq, freq->ht_enabled, freq->vht_enabled,
7728 freq->bandwidth, freq->center_freq1, freq->center_freq2);
7729
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007730 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
7731 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
7732 "detection");
7733 return -1;
7734 }
7735
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007736 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
7737 nl80211_put_freq_params(msg, freq) < 0) {
7738 nlmsg_free(msg);
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007739 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007740 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007741
7742 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
7743 if (ret == 0)
7744 return 0;
7745 wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
7746 "%d (%s)", ret, strerror(-ret));
Dmitry Shmidtea69e842013-05-13 14:52:28 -07007747 return -1;
7748}
7749
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007750#ifdef CONFIG_TDLS
7751
7752static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
7753 u8 dialog_token, u16 status_code,
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07007754 u32 peer_capab, int initiator, const u8 *buf,
7755 size_t len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007756{
7757 struct i802_bss *bss = priv;
7758 struct wpa_driver_nl80211_data *drv = bss->drv;
7759 struct nl_msg *msg;
7760
7761 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7762 return -EOPNOTSUPP;
7763
7764 if (!dst)
7765 return -EINVAL;
7766
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007767 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
7768 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
7769 nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
7770 nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
7771 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
7772 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007773 if (peer_capab) {
7774 /*
7775 * The internal enum tdls_peer_capability definition is
7776 * currently identical with the nl80211 enum
7777 * nl80211_tdls_peer_capability, so no conversion is needed
7778 * here.
7779 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007780 if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
7781 peer_capab))
7782 goto fail;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007783 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007784 if ((initiator &&
7785 nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
7786 nla_put(msg, NL80211_ATTR_IE, len, buf))
7787 goto fail;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007788
7789 return send_and_recv_msgs(drv, msg, NULL, NULL);
7790
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007791fail:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007792 nlmsg_free(msg);
7793 return -ENOBUFS;
7794}
7795
7796
7797static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
7798{
7799 struct i802_bss *bss = priv;
7800 struct wpa_driver_nl80211_data *drv = bss->drv;
7801 struct nl_msg *msg;
7802 enum nl80211_tdls_operation nl80211_oper;
7803
7804 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
7805 return -EOPNOTSUPP;
7806
7807 switch (oper) {
7808 case TDLS_DISCOVERY_REQ:
7809 nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
7810 break;
7811 case TDLS_SETUP:
7812 nl80211_oper = NL80211_TDLS_SETUP;
7813 break;
7814 case TDLS_TEARDOWN:
7815 nl80211_oper = NL80211_TDLS_TEARDOWN;
7816 break;
7817 case TDLS_ENABLE_LINK:
7818 nl80211_oper = NL80211_TDLS_ENABLE_LINK;
7819 break;
7820 case TDLS_DISABLE_LINK:
7821 nl80211_oper = NL80211_TDLS_DISABLE_LINK;
7822 break;
7823 case TDLS_ENABLE:
7824 return 0;
7825 case TDLS_DISABLE:
7826 return 0;
7827 default:
7828 return -EINVAL;
7829 }
7830
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007831 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
7832 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
7833 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
7834 nlmsg_free(msg);
7835 return -ENOBUFS;
7836 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007837
7838 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007839}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007840
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007841
7842static int
7843nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
7844 const struct hostapd_freq_params *params)
7845{
7846 struct i802_bss *bss = priv;
7847 struct wpa_driver_nl80211_data *drv = bss->drv;
7848 struct nl_msg *msg;
7849 int ret = -ENOBUFS;
7850
7851 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7852 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7853 return -EOPNOTSUPP;
7854
7855 wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
7856 " oper_class=%u freq=%u",
7857 MAC2STR(addr), oper_class, params->freq);
7858 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
7859 if (!msg ||
7860 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
7861 nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
7862 (ret = nl80211_put_freq_params(msg, params))) {
7863 nlmsg_free(msg);
7864 wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
7865 return ret;
7866 }
7867
7868 return send_and_recv_msgs(drv, msg, NULL, NULL);
7869}
7870
7871
7872static int
7873nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
7874{
7875 struct i802_bss *bss = priv;
7876 struct wpa_driver_nl80211_data *drv = bss->drv;
7877 struct nl_msg *msg;
7878
7879 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
7880 !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
7881 return -EOPNOTSUPP;
7882
7883 wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
7884 MAC2STR(addr));
7885 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
7886 if (!msg ||
7887 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
7888 nlmsg_free(msg);
7889 wpa_printf(MSG_DEBUG,
7890 "nl80211: Could not build TDLS cancel chan switch");
7891 return -ENOBUFS;
7892 }
7893
7894 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007895}
7896
7897#endif /* CONFIG TDLS */
7898
7899
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007900static int driver_nl80211_set_key(const char *ifname, void *priv,
7901 enum wpa_alg alg, const u8 *addr,
7902 int key_idx, int set_tx,
7903 const u8 *seq, size_t seq_len,
7904 const u8 *key, size_t key_len)
7905{
7906 struct i802_bss *bss = priv;
7907 return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
7908 set_tx, seq, seq_len, key, key_len);
7909}
7910
7911
7912static int driver_nl80211_scan2(void *priv,
7913 struct wpa_driver_scan_params *params)
7914{
7915 struct i802_bss *bss = priv;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007916#ifdef CONFIG_DRIVER_NL80211_QCA
7917 struct wpa_driver_nl80211_data *drv = bss->drv;
7918
7919 /*
7920 * Do a vendor specific scan if possible. If only_new_results is
7921 * set, do a normal scan since a kernel (cfg80211) BSS cache flush
7922 * cannot be achieved through a vendor scan. The below condition may
7923 * need to be modified if new scan flags are added in the future whose
7924 * functionality can only be achieved through a normal scan.
7925 */
7926 if (drv->scan_vendor_cmd_avail && !params->only_new_results)
7927 return wpa_driver_nl80211_vendor_scan(bss, params);
7928#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007929 return wpa_driver_nl80211_scan(bss, params);
7930}
7931
7932
7933static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
7934 int reason_code)
7935{
7936 struct i802_bss *bss = priv;
7937 return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
7938}
7939
7940
7941static int driver_nl80211_authenticate(void *priv,
7942 struct wpa_driver_auth_params *params)
7943{
7944 struct i802_bss *bss = priv;
7945 return wpa_driver_nl80211_authenticate(bss, params);
7946}
7947
7948
7949static void driver_nl80211_deinit(void *priv)
7950{
7951 struct i802_bss *bss = priv;
7952 wpa_driver_nl80211_deinit(bss);
7953}
7954
7955
7956static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
7957 const char *ifname)
7958{
7959 struct i802_bss *bss = priv;
7960 return wpa_driver_nl80211_if_remove(bss, type, ifname);
7961}
7962
7963
7964static int driver_nl80211_send_mlme(void *priv, const u8 *data,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007965 size_t data_len, int noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007966 unsigned int freq,
7967 const u16 *csa_offs, size_t csa_offs_len)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007968{
7969 struct i802_bss *bss = priv;
7970 return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007971 freq, 0, 0, 0, csa_offs,
7972 csa_offs_len);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007973}
7974
7975
7976static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
7977{
7978 struct i802_bss *bss = priv;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007979 return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007980}
7981
7982
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007983static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
7984 const char *ifname, int vlan_id)
7985{
7986 struct i802_bss *bss = priv;
7987 return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
7988}
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007989
7990
7991static int driver_nl80211_read_sta_data(void *priv,
7992 struct hostap_sta_driver_data *data,
7993 const u8 *addr)
7994{
7995 struct i802_bss *bss = priv;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08007996
7997 os_memset(data, 0, sizeof(*data));
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08007998 return i802_read_sta_data(bss, data, addr);
7999}
8000
8001
8002static int driver_nl80211_send_action(void *priv, unsigned int freq,
8003 unsigned int wait_time,
8004 const u8 *dst, const u8 *src,
8005 const u8 *bssid,
8006 const u8 *data, size_t data_len,
8007 int no_cck)
8008{
8009 struct i802_bss *bss = priv;
8010 return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
8011 bssid, data, data_len, no_cck);
8012}
8013
8014
8015static int driver_nl80211_probe_req_report(void *priv, int report)
8016{
8017 struct i802_bss *bss = priv;
8018 return wpa_driver_nl80211_probe_req_report(bss, report);
8019}
8020
8021
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008022static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
8023 const u8 *ies, size_t ies_len)
8024{
8025 int ret;
8026 struct nl_msg *msg;
8027 struct i802_bss *bss = priv;
8028 struct wpa_driver_nl80211_data *drv = bss->drv;
8029 u16 mdid = WPA_GET_LE16(md);
8030
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008031 wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008032 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
8033 nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
8034 nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
8035 nlmsg_free(msg);
8036 return -ENOBUFS;
8037 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008038
8039 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8040 if (ret) {
8041 wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
8042 "err=%d (%s)", ret, strerror(-ret));
8043 }
8044
8045 return ret;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07008046}
8047
8048
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07008049static const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07008050{
8051 struct i802_bss *bss = priv;
8052 struct wpa_driver_nl80211_data *drv = bss->drv;
8053
8054 if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
8055 return NULL;
8056
8057 return bss->addr;
8058}
8059
8060
Dmitry Shmidt56052862013-10-04 10:23:25 -07008061static const char * scan_state_str(enum scan_states scan_state)
8062{
8063 switch (scan_state) {
8064 case NO_SCAN:
8065 return "NO_SCAN";
8066 case SCAN_REQUESTED:
8067 return "SCAN_REQUESTED";
8068 case SCAN_STARTED:
8069 return "SCAN_STARTED";
8070 case SCAN_COMPLETED:
8071 return "SCAN_COMPLETED";
8072 case SCAN_ABORTED:
8073 return "SCAN_ABORTED";
8074 case SCHED_SCAN_STARTED:
8075 return "SCHED_SCAN_STARTED";
8076 case SCHED_SCAN_STOPPED:
8077 return "SCHED_SCAN_STOPPED";
8078 case SCHED_SCAN_RESULTS:
8079 return "SCHED_SCAN_RESULTS";
8080 }
8081
8082 return "??";
8083}
8084
8085
8086static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
8087{
8088 struct i802_bss *bss = priv;
8089 struct wpa_driver_nl80211_data *drv = bss->drv;
8090 int res;
8091 char *pos, *end;
8092
8093 pos = buf;
8094 end = buf + buflen;
8095
8096 res = os_snprintf(pos, end - pos,
8097 "ifindex=%d\n"
8098 "ifname=%s\n"
8099 "brname=%s\n"
8100 "addr=" MACSTR "\n"
8101 "freq=%d\n"
8102 "%s%s%s%s%s",
8103 bss->ifindex,
8104 bss->ifname,
8105 bss->brname,
8106 MAC2STR(bss->addr),
8107 bss->freq,
8108 bss->beacon_set ? "beacon_set=1\n" : "",
8109 bss->added_if_into_bridge ?
8110 "added_if_into_bridge=1\n" : "",
8111 bss->added_bridge ? "added_bridge=1\n" : "",
8112 bss->in_deinit ? "in_deinit=1\n" : "",
8113 bss->if_dynamic ? "if_dynamic=1\n" : "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008114 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008115 return pos - buf;
8116 pos += res;
8117
8118 if (bss->wdev_id_set) {
8119 res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
8120 (unsigned long long) bss->wdev_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008121 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008122 return pos - buf;
8123 pos += res;
8124 }
8125
8126 res = os_snprintf(pos, end - pos,
8127 "phyname=%s\n"
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008128 "perm_addr=" MACSTR "\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07008129 "drv_ifindex=%d\n"
8130 "operstate=%d\n"
8131 "scan_state=%s\n"
8132 "auth_bssid=" MACSTR "\n"
8133 "auth_attempt_bssid=" MACSTR "\n"
8134 "bssid=" MACSTR "\n"
8135 "prev_bssid=" MACSTR "\n"
8136 "associated=%d\n"
8137 "assoc_freq=%u\n"
8138 "monitor_sock=%d\n"
8139 "monitor_ifidx=%d\n"
8140 "monitor_refcount=%d\n"
8141 "last_mgmt_freq=%u\n"
8142 "eapol_tx_sock=%d\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008143 "%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008144 drv->phyname,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008145 MAC2STR(drv->perm_addr),
Dmitry Shmidt56052862013-10-04 10:23:25 -07008146 drv->ifindex,
8147 drv->operstate,
8148 scan_state_str(drv->scan_state),
8149 MAC2STR(drv->auth_bssid),
8150 MAC2STR(drv->auth_attempt_bssid),
8151 MAC2STR(drv->bssid),
8152 MAC2STR(drv->prev_bssid),
8153 drv->associated,
8154 drv->assoc_freq,
8155 drv->monitor_sock,
8156 drv->monitor_ifidx,
8157 drv->monitor_refcount,
8158 drv->last_mgmt_freq,
8159 drv->eapol_tx_sock,
8160 drv->ignore_if_down_event ?
8161 "ignore_if_down_event=1\n" : "",
8162 drv->scan_complete_events ?
8163 "scan_complete_events=1\n" : "",
8164 drv->disabled_11b_rates ?
8165 "disabled_11b_rates=1\n" : "",
8166 drv->pending_remain_on_chan ?
8167 "pending_remain_on_chan=1\n" : "",
8168 drv->in_interface_list ? "in_interface_list=1\n" : "",
8169 drv->device_ap_sme ? "device_ap_sme=1\n" : "",
8170 drv->poll_command_supported ?
8171 "poll_command_supported=1\n" : "",
8172 drv->data_tx_status ? "data_tx_status=1\n" : "",
8173 drv->scan_for_auth ? "scan_for_auth=1\n" : "",
8174 drv->retry_auth ? "retry_auth=1\n" : "",
8175 drv->use_monitor ? "use_monitor=1\n" : "",
8176 drv->ignore_next_local_disconnect ?
8177 "ignore_next_local_disconnect=1\n" : "",
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07008178 drv->ignore_next_local_deauth ?
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008179 "ignore_next_local_deauth=1\n" : "");
8180 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008181 return pos - buf;
8182 pos += res;
8183
8184 if (drv->has_capability) {
8185 res = os_snprintf(pos, end - pos,
8186 "capa.key_mgmt=0x%x\n"
8187 "capa.enc=0x%x\n"
8188 "capa.auth=0x%x\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008189 "capa.flags=0x%llx\n"
8190 "capa.rrm_flags=0x%x\n"
Dmitry Shmidt56052862013-10-04 10:23:25 -07008191 "capa.max_scan_ssids=%d\n"
8192 "capa.max_sched_scan_ssids=%d\n"
8193 "capa.sched_scan_supported=%d\n"
8194 "capa.max_match_sets=%d\n"
8195 "capa.max_remain_on_chan=%u\n"
8196 "capa.max_stations=%u\n"
8197 "capa.probe_resp_offloads=0x%x\n"
8198 "capa.max_acl_mac_addrs=%u\n"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008199 "capa.num_multichan_concurrent=%u\n"
8200 "capa.mac_addr_rand_sched_scan_supported=%d\n"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008201 "capa.mac_addr_rand_scan_supported=%d\n"
8202 "capa.conc_capab=%u\n"
8203 "capa.max_conc_chan_2_4=%u\n"
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008204 "capa.max_conc_chan_5_0=%u\n"
8205 "capa.max_sched_scan_plans=%u\n"
8206 "capa.max_sched_scan_plan_interval=%u\n"
8207 "capa.max_sched_scan_plan_iterations=%u\n",
Dmitry Shmidt56052862013-10-04 10:23:25 -07008208 drv->capa.key_mgmt,
8209 drv->capa.enc,
8210 drv->capa.auth,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008211 (unsigned long long) drv->capa.flags,
8212 drv->capa.rrm_flags,
Dmitry Shmidt56052862013-10-04 10:23:25 -07008213 drv->capa.max_scan_ssids,
8214 drv->capa.max_sched_scan_ssids,
8215 drv->capa.sched_scan_supported,
8216 drv->capa.max_match_sets,
8217 drv->capa.max_remain_on_chan,
8218 drv->capa.max_stations,
8219 drv->capa.probe_resp_offloads,
8220 drv->capa.max_acl_mac_addrs,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008221 drv->capa.num_multichan_concurrent,
8222 drv->capa.mac_addr_rand_sched_scan_supported,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008223 drv->capa.mac_addr_rand_scan_supported,
8224 drv->capa.conc_capab,
8225 drv->capa.max_conc_chan_2_4,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008226 drv->capa.max_conc_chan_5_0,
8227 drv->capa.max_sched_scan_plans,
8228 drv->capa.max_sched_scan_plan_interval,
8229 drv->capa.max_sched_scan_plan_iterations);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008230 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt56052862013-10-04 10:23:25 -07008231 return pos - buf;
8232 pos += res;
8233 }
8234
8235 return pos - buf;
8236}
8237
8238
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008239static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
8240{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008241 if ((settings->head &&
8242 nla_put(msg, NL80211_ATTR_BEACON_HEAD,
8243 settings->head_len, settings->head)) ||
8244 (settings->tail &&
8245 nla_put(msg, NL80211_ATTR_BEACON_TAIL,
8246 settings->tail_len, settings->tail)) ||
8247 (settings->beacon_ies &&
8248 nla_put(msg, NL80211_ATTR_IE,
8249 settings->beacon_ies_len, settings->beacon_ies)) ||
8250 (settings->proberesp_ies &&
8251 nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
8252 settings->proberesp_ies_len, settings->proberesp_ies)) ||
8253 (settings->assocresp_ies &&
8254 nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
8255 settings->assocresp_ies_len, settings->assocresp_ies)) ||
8256 (settings->probe_resp &&
8257 nla_put(msg, NL80211_ATTR_PROBE_RESP,
8258 settings->probe_resp_len, settings->probe_resp)))
8259 return -ENOBUFS;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008260
8261 return 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008262}
8263
8264
8265static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
8266{
8267 struct nl_msg *msg;
8268 struct i802_bss *bss = priv;
8269 struct wpa_driver_nl80211_data *drv = bss->drv;
8270 struct nlattr *beacon_csa;
8271 int ret = -ENOBUFS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008272 int csa_off_len = 0;
8273 int i;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008274
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008275 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 -08008276 settings->cs_count, settings->block_tx,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008277 settings->freq_params.freq, settings->freq_params.bandwidth,
8278 settings->freq_params.center_freq1,
8279 settings->freq_params.center_freq2);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008280
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008281 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008282 wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
8283 return -EOPNOTSUPP;
8284 }
8285
8286 if ((drv->nlmode != NL80211_IFTYPE_AP) &&
8287 (drv->nlmode != NL80211_IFTYPE_P2P_GO))
8288 return -EOPNOTSUPP;
8289
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008290 /*
8291 * Remove empty counters, assuming Probe Response and Beacon frame
8292 * counters match. This implementation assumes that there are only two
8293 * counters.
8294 */
8295 if (settings->counter_offset_beacon[0] &&
8296 !settings->counter_offset_beacon[1]) {
8297 csa_off_len = 1;
8298 } else if (settings->counter_offset_beacon[1] &&
8299 !settings->counter_offset_beacon[0]) {
8300 csa_off_len = 1;
8301 settings->counter_offset_beacon[0] =
8302 settings->counter_offset_beacon[1];
8303 settings->counter_offset_presp[0] =
8304 settings->counter_offset_presp[1];
8305 } else if (settings->counter_offset_beacon[1] &&
8306 settings->counter_offset_beacon[0]) {
8307 csa_off_len = 2;
8308 } else {
8309 wpa_printf(MSG_ERROR, "nl80211: No CSA counters provided");
8310 return -EINVAL;
8311 }
8312
8313 /* Check CSA counters validity */
8314 if (drv->capa.max_csa_counters &&
8315 csa_off_len > drv->capa.max_csa_counters) {
8316 wpa_printf(MSG_ERROR,
8317 "nl80211: Too many CSA counters provided");
8318 return -EINVAL;
8319 }
8320
8321 if (!settings->beacon_csa.tail)
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008322 return -EINVAL;
8323
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008324 for (i = 0; i < csa_off_len; i++) {
8325 u16 csa_c_off_bcn = settings->counter_offset_beacon[i];
8326 u16 csa_c_off_presp = settings->counter_offset_presp[i];
8327
8328 if ((settings->beacon_csa.tail_len <= csa_c_off_bcn) ||
8329 (settings->beacon_csa.tail[csa_c_off_bcn] !=
8330 settings->cs_count))
8331 return -EINVAL;
8332
8333 if (settings->beacon_csa.probe_resp &&
8334 ((settings->beacon_csa.probe_resp_len <=
8335 csa_c_off_presp) ||
8336 (settings->beacon_csa.probe_resp[csa_c_off_presp] !=
8337 settings->cs_count)))
8338 return -EINVAL;
8339 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008340
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008341 if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
8342 nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
8343 settings->cs_count) ||
8344 (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
8345 (settings->block_tx &&
8346 nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008347 goto error;
8348
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008349 /* beacon_after params */
8350 ret = set_beacon_data(msg, &settings->beacon_after);
8351 if (ret)
8352 goto error;
8353
8354 /* beacon_csa params */
8355 beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
8356 if (!beacon_csa)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008357 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008358
8359 ret = set_beacon_data(msg, &settings->beacon_csa);
8360 if (ret)
8361 goto error;
8362
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008363 if (nla_put(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
8364 csa_off_len * sizeof(u16),
8365 settings->counter_offset_beacon) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008366 (settings->beacon_csa.probe_resp &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008367 nla_put(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
8368 csa_off_len * sizeof(u16),
8369 settings->counter_offset_presp)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008370 goto fail;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008371
8372 nla_nest_end(msg, beacon_csa);
8373 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8374 if (ret) {
8375 wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
8376 ret, strerror(-ret));
8377 }
8378 return ret;
8379
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008380fail:
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08008381 ret = -ENOBUFS;
8382error:
8383 nlmsg_free(msg);
8384 wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
8385 return ret;
8386}
8387
8388
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008389static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
8390 u8 user_priority, u16 admitted_time)
8391{
8392 struct i802_bss *bss = priv;
8393 struct wpa_driver_nl80211_data *drv = bss->drv;
8394 struct nl_msg *msg;
8395 int ret;
8396
8397 wpa_printf(MSG_DEBUG,
8398 "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
8399 tsid, admitted_time, user_priority);
8400
8401 if (!is_sta_interface(drv->nlmode))
8402 return -ENOTSUP;
8403
8404 msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
8405 if (!msg ||
8406 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8407 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8408 nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
8409 nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
8410 nlmsg_free(msg);
8411 return -ENOBUFS;
8412 }
8413
8414 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8415 if (ret)
8416 wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
8417 ret, strerror(-ret));
8418 return ret;
8419}
8420
8421
8422static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
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, "nl80211: del_ts request: tsid=%u", tsid);
8430
8431 if (!is_sta_interface(drv->nlmode))
8432 return -ENOTSUP;
8433
8434 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
8435 nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
8436 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
8437 nlmsg_free(msg);
8438 return -ENOBUFS;
8439 }
8440
8441 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8442 if (ret)
8443 wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
8444 ret, strerror(-ret));
8445 return ret;
8446}
8447
8448
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008449#ifdef CONFIG_TESTING_OPTIONS
8450static int cmd_reply_handler(struct nl_msg *msg, void *arg)
8451{
8452 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8453 struct wpabuf *buf = arg;
8454
8455 if (!buf)
8456 return NL_SKIP;
8457
8458 if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
8459 wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
8460 return NL_SKIP;
8461 }
8462
8463 wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
8464 genlmsg_attrlen(gnlh, 0));
8465
8466 return NL_SKIP;
8467}
8468#endif /* CONFIG_TESTING_OPTIONS */
8469
8470
8471static int vendor_reply_handler(struct nl_msg *msg, void *arg)
8472{
8473 struct nlattr *tb[NL80211_ATTR_MAX + 1];
8474 struct nlattr *nl_vendor_reply, *nl;
8475 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
8476 struct wpabuf *buf = arg;
8477 int rem;
8478
8479 if (!buf)
8480 return NL_SKIP;
8481
8482 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
8483 genlmsg_attrlen(gnlh, 0), NULL);
8484 nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
8485
8486 if (!nl_vendor_reply)
8487 return NL_SKIP;
8488
8489 if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
8490 wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
8491 return NL_SKIP;
8492 }
8493
8494 nla_for_each_nested(nl, nl_vendor_reply, rem) {
8495 wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
8496 }
8497
8498 return NL_SKIP;
8499}
8500
8501
8502static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
8503 unsigned int subcmd, const u8 *data,
8504 size_t data_len, struct wpabuf *buf)
8505{
8506 struct i802_bss *bss = priv;
8507 struct wpa_driver_nl80211_data *drv = bss->drv;
8508 struct nl_msg *msg;
8509 int ret;
8510
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008511#ifdef CONFIG_TESTING_OPTIONS
8512 if (vendor_id == 0xffffffff) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008513 msg = nlmsg_alloc();
8514 if (!msg)
8515 return -ENOMEM;
8516
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008517 nl80211_cmd(drv, msg, 0, subcmd);
8518 if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
8519 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008520 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008521 ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
8522 if (ret)
8523 wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
8524 ret);
8525 return ret;
8526 }
8527#endif /* CONFIG_TESTING_OPTIONS */
8528
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008529 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
8530 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
8531 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
8532 (data &&
8533 nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
8534 goto fail;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008535
8536 ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
8537 if (ret)
8538 wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
8539 ret);
8540 return ret;
8541
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008542fail:
Dmitry Shmidta38abf92014-03-06 13:38:44 -08008543 nlmsg_free(msg);
8544 return -ENOBUFS;
8545}
8546
8547
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008548static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
8549 u8 qos_map_set_len)
8550{
8551 struct i802_bss *bss = priv;
8552 struct wpa_driver_nl80211_data *drv = bss->drv;
8553 struct nl_msg *msg;
8554 int ret;
8555
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008556 wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
8557 qos_map_set, qos_map_set_len);
8558
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008559 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
8560 nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
8561 nlmsg_free(msg);
8562 return -ENOBUFS;
8563 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008564
8565 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8566 if (ret)
8567 wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
8568
8569 return ret;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008570}
8571
8572
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008573static int nl80211_set_wowlan(void *priv,
8574 const struct wowlan_triggers *triggers)
8575{
8576 struct i802_bss *bss = priv;
8577 struct wpa_driver_nl80211_data *drv = bss->drv;
8578 struct nl_msg *msg;
8579 struct nlattr *wowlan_triggers;
8580 int ret;
8581
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008582 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
8583
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008584 if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_SET_WOWLAN)) ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008585 !(wowlan_triggers = nla_nest_start(msg,
8586 NL80211_ATTR_WOWLAN_TRIGGERS)) ||
8587 (triggers->any &&
8588 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
8589 (triggers->disconnect &&
8590 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
8591 (triggers->magic_pkt &&
8592 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
8593 (triggers->gtk_rekey_failure &&
8594 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
8595 (triggers->eap_identity_req &&
8596 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
8597 (triggers->four_way_handshake &&
8598 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
8599 (triggers->rfkill_release &&
8600 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
8601 nlmsg_free(msg);
8602 return -ENOBUFS;
8603 }
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008604
8605 nla_nest_end(msg, wowlan_triggers);
8606
8607 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8608 if (ret)
8609 wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
8610
8611 return ret;
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07008612}
8613
8614
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008615#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008616static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
8617{
8618 struct i802_bss *bss = priv;
8619 struct wpa_driver_nl80211_data *drv = bss->drv;
8620 struct nl_msg *msg;
8621 struct nlattr *params;
8622
8623 wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
8624
8625 if (!drv->roaming_vendor_cmd_avail) {
8626 wpa_printf(MSG_DEBUG,
8627 "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
8628 return -1;
8629 }
8630
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008631 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
8632 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
8633 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
8634 QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
8635 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
8636 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
8637 allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
8638 QCA_ROAMING_NOT_ALLOWED) ||
8639 (bssid &&
8640 nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
8641 nlmsg_free(msg);
8642 return -1;
8643 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008644 nla_nest_end(msg, params);
8645
8646 return send_and_recv_msgs(drv, msg, NULL, NULL);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008647}
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008648#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008649
8650
8651static int nl80211_set_mac_addr(void *priv, const u8 *addr)
8652{
8653 struct i802_bss *bss = priv;
8654 struct wpa_driver_nl80211_data *drv = bss->drv;
8655 int new_addr = addr != NULL;
8656
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008657 if (TEST_FAIL())
8658 return -1;
8659
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07008660 if (!addr)
8661 addr = drv->perm_addr;
8662
8663 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
8664 return -1;
8665
8666 if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
8667 {
8668 wpa_printf(MSG_DEBUG,
8669 "nl80211: failed to set_mac_addr for %s to " MACSTR,
8670 bss->ifname, MAC2STR(addr));
8671 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
8672 1) < 0) {
8673 wpa_printf(MSG_DEBUG,
8674 "nl80211: Could not restore interface UP after failed set_mac_addr");
8675 }
8676 return -1;
8677 }
8678
8679 wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
8680 bss->ifname, MAC2STR(addr));
8681 drv->addr_changed = new_addr;
8682 os_memcpy(bss->addr, addr, ETH_ALEN);
8683
8684 if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
8685 {
8686 wpa_printf(MSG_DEBUG,
8687 "nl80211: Could not restore interface UP after set_mac_addr");
8688 }
8689
8690 return 0;
8691}
8692
8693
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008694#ifdef CONFIG_MESH
8695
8696static int wpa_driver_nl80211_init_mesh(void *priv)
8697{
8698 if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
8699 wpa_printf(MSG_INFO,
8700 "nl80211: Failed to set interface into mesh mode");
8701 return -1;
8702 }
8703 return 0;
8704}
8705
8706
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008707static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
8708 size_t mesh_id_len)
8709{
8710 if (mesh_id) {
8711 wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
8712 mesh_id, mesh_id_len);
8713 return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
8714 }
8715
8716 return 0;
8717}
8718
8719
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07008720static int nl80211_put_mesh_config(struct nl_msg *msg,
8721 struct wpa_driver_mesh_bss_params *params)
8722{
8723 struct nlattr *container;
8724
8725 container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
8726 if (!container)
8727 return -1;
8728
8729 if (((params->flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
8730 nla_put_u32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
8731 params->auto_plinks)) ||
8732 ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS) &&
8733 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
8734 params->max_peer_links)))
8735 return -1;
8736
8737 /*
8738 * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
8739 * the timer could disconnect stations even in that case.
8740 */
8741 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT) &&
8742 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
8743 params->peer_link_timeout)) {
8744 wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
8745 return -1;
8746 }
8747
8748 if ((params->flags & WPA_DRIVER_MESH_CONF_FLAG_HT_OP_MODE) &&
8749 nla_put_u16(msg, NL80211_MESHCONF_HT_OPMODE, params->ht_opmode)) {
8750 wpa_printf(MSG_ERROR, "nl80211: Failed to set HT_OP_MODE");
8751 return -1;
8752 }
8753
8754 nla_nest_end(msg, container);
8755
8756 return 0;
8757}
8758
8759
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008760static int nl80211_join_mesh(struct i802_bss *bss,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008761 struct wpa_driver_mesh_join_params *params)
8762{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008763 struct wpa_driver_nl80211_data *drv = bss->drv;
8764 struct nl_msg *msg;
8765 struct nlattr *container;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08008766 int ret = -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008767
8768 wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
8769 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008770 if (!msg ||
8771 nl80211_put_freq_params(msg, &params->freq) ||
8772 nl80211_put_basic_rates(msg, params->basic_rates) ||
8773 nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07008774 nl80211_put_beacon_int(msg, params->beacon_int) ||
8775 nl80211_put_dtim_period(msg, params->dtim_period))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008776 goto fail;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008777
8778 wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
8779
8780 container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
8781 if (!container)
8782 goto fail;
8783
8784 if (params->ies) {
8785 wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
8786 if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
8787 params->ies))
8788 goto fail;
8789 }
8790 /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
8791 if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
8792 if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
8793 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
8794 goto fail;
8795 }
8796 if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
8797 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
8798 goto fail;
8799 if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
8800 nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
8801 goto fail;
8802 nla_nest_end(msg, container);
8803
Dmitry Shmidtd13095b2016-08-22 14:02:19 -07008804 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS;
8805 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_PEER_LINK_TIMEOUT;
8806 params->conf.flags |= WPA_DRIVER_MESH_CONF_FLAG_MAX_PEER_LINKS;
8807 if (nl80211_put_mesh_config(msg, &params->conf) < 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008808 goto fail;
8809
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008810 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8811 msg = NULL;
8812 if (ret) {
8813 wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
8814 ret, strerror(-ret));
8815 goto fail;
8816 }
8817 ret = 0;
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07008818 drv->assoc_freq = bss->freq = params->freq.freq;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008819 wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
8820
8821fail:
8822 nlmsg_free(msg);
8823 return ret;
8824}
8825
8826
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008827static int
8828wpa_driver_nl80211_join_mesh(void *priv,
8829 struct wpa_driver_mesh_join_params *params)
8830{
8831 struct i802_bss *bss = priv;
8832 int ret, timeout;
8833
8834 timeout = params->conf.peer_link_timeout;
8835
8836 /* Disable kernel inactivity timer */
8837 if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
8838 params->conf.peer_link_timeout = 0;
8839
8840 ret = nl80211_join_mesh(bss, params);
8841 if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
8842 wpa_printf(MSG_DEBUG,
8843 "nl80211: Mesh join retry for peer_link_timeout");
8844 /*
8845 * Old kernel does not support setting
8846 * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
8847 * into future from peer_link_timeout.
8848 */
8849 params->conf.peer_link_timeout = timeout + 60;
8850 ret = nl80211_join_mesh(priv, params);
8851 }
8852
8853 params->conf.peer_link_timeout = timeout;
8854 return ret;
8855}
8856
8857
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008858static int wpa_driver_nl80211_leave_mesh(void *priv)
8859{
8860 struct i802_bss *bss = priv;
8861 struct wpa_driver_nl80211_data *drv = bss->drv;
8862 struct nl_msg *msg;
8863 int ret;
8864
8865 wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
8866 msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
8867 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
8868 if (ret) {
8869 wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
8870 ret, strerror(-ret));
8871 } else {
8872 wpa_printf(MSG_DEBUG,
8873 "nl80211: mesh leave request send successfully");
8874 }
8875
8876 if (wpa_driver_nl80211_set_mode(drv->first_bss,
8877 NL80211_IFTYPE_STATION)) {
8878 wpa_printf(MSG_INFO,
8879 "nl80211: Failed to set interface into station mode");
8880 }
8881 return ret;
8882}
8883
8884#endif /* CONFIG_MESH */
8885
8886
8887static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
8888 const u8 *ipaddr, int prefixlen,
8889 const u8 *addr)
8890{
8891#ifdef CONFIG_LIBNL3_ROUTE
8892 struct i802_bss *bss = priv;
8893 struct wpa_driver_nl80211_data *drv = bss->drv;
8894 struct rtnl_neigh *rn;
8895 struct nl_addr *nl_ipaddr = NULL;
8896 struct nl_addr *nl_lladdr = NULL;
8897 int family, addrsize;
8898 int res;
8899
8900 if (!ipaddr || prefixlen == 0 || !addr)
8901 return -EINVAL;
8902
8903 if (bss->br_ifindex == 0) {
8904 wpa_printf(MSG_DEBUG,
8905 "nl80211: bridge must be set before adding an ip neigh to it");
8906 return -1;
8907 }
8908
8909 if (!drv->rtnl_sk) {
8910 wpa_printf(MSG_DEBUG,
8911 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
8912 return -1;
8913 }
8914
8915 if (version == 4) {
8916 family = AF_INET;
8917 addrsize = 4;
8918 } else if (version == 6) {
8919 family = AF_INET6;
8920 addrsize = 16;
8921 } else {
8922 return -EINVAL;
8923 }
8924
8925 rn = rtnl_neigh_alloc();
8926 if (rn == NULL)
8927 return -ENOMEM;
8928
8929 /* set the destination ip address for neigh */
8930 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
8931 if (nl_ipaddr == NULL) {
8932 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
8933 res = -ENOMEM;
8934 goto errout;
8935 }
8936 nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
8937 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
8938 if (res) {
8939 wpa_printf(MSG_DEBUG,
8940 "nl80211: neigh set destination addr failed");
8941 goto errout;
8942 }
8943
8944 /* set the corresponding lladdr for neigh */
8945 nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
8946 if (nl_lladdr == NULL) {
8947 wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
8948 res = -ENOMEM;
8949 goto errout;
8950 }
8951 rtnl_neigh_set_lladdr(rn, nl_lladdr);
8952
8953 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
8954 rtnl_neigh_set_state(rn, NUD_PERMANENT);
8955
8956 res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
8957 if (res) {
8958 wpa_printf(MSG_DEBUG,
8959 "nl80211: Adding bridge ip neigh failed: %s",
8960 strerror(errno));
8961 }
8962errout:
8963 if (nl_lladdr)
8964 nl_addr_put(nl_lladdr);
8965 if (nl_ipaddr)
8966 nl_addr_put(nl_ipaddr);
8967 if (rn)
8968 rtnl_neigh_put(rn);
8969 return res;
8970#else /* CONFIG_LIBNL3_ROUTE */
8971 return -1;
8972#endif /* CONFIG_LIBNL3_ROUTE */
8973}
8974
8975
8976static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
8977 const u8 *ipaddr)
8978{
8979#ifdef CONFIG_LIBNL3_ROUTE
8980 struct i802_bss *bss = priv;
8981 struct wpa_driver_nl80211_data *drv = bss->drv;
8982 struct rtnl_neigh *rn;
8983 struct nl_addr *nl_ipaddr;
8984 int family, addrsize;
8985 int res;
8986
8987 if (!ipaddr)
8988 return -EINVAL;
8989
8990 if (version == 4) {
8991 family = AF_INET;
8992 addrsize = 4;
8993 } else if (version == 6) {
8994 family = AF_INET6;
8995 addrsize = 16;
8996 } else {
8997 return -EINVAL;
8998 }
8999
9000 if (bss->br_ifindex == 0) {
9001 wpa_printf(MSG_DEBUG,
9002 "nl80211: bridge must be set to delete an ip neigh");
9003 return -1;
9004 }
9005
9006 if (!drv->rtnl_sk) {
9007 wpa_printf(MSG_DEBUG,
9008 "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
9009 return -1;
9010 }
9011
9012 rn = rtnl_neigh_alloc();
9013 if (rn == NULL)
9014 return -ENOMEM;
9015
9016 /* set the destination ip address for neigh */
9017 nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
9018 if (nl_ipaddr == NULL) {
9019 wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
9020 res = -ENOMEM;
9021 goto errout;
9022 }
9023 res = rtnl_neigh_set_dst(rn, nl_ipaddr);
9024 if (res) {
9025 wpa_printf(MSG_DEBUG,
9026 "nl80211: neigh set destination addr failed");
9027 goto errout;
9028 }
9029
9030 rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
9031
9032 res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
9033 if (res) {
9034 wpa_printf(MSG_DEBUG,
9035 "nl80211: Deleting bridge ip neigh failed: %s",
9036 strerror(errno));
9037 }
9038errout:
9039 if (nl_ipaddr)
9040 nl_addr_put(nl_ipaddr);
9041 if (rn)
9042 rtnl_neigh_put(rn);
9043 return res;
9044#else /* CONFIG_LIBNL3_ROUTE */
9045 return -1;
9046#endif /* CONFIG_LIBNL3_ROUTE */
9047}
9048
9049
9050static int linux_write_system_file(const char *path, unsigned int val)
9051{
9052 char buf[50];
9053 int fd, len;
9054
9055 len = os_snprintf(buf, sizeof(buf), "%u\n", val);
9056 if (os_snprintf_error(sizeof(buf), len))
9057 return -1;
9058
9059 fd = open(path, O_WRONLY);
9060 if (fd < 0)
9061 return -1;
9062
9063 if (write(fd, buf, len) < 0) {
9064 wpa_printf(MSG_DEBUG,
9065 "nl80211: Failed to write Linux system file: %s with the value of %d",
9066 path, val);
9067 close(fd);
9068 return -1;
9069 }
9070 close(fd);
9071
9072 return 0;
9073}
9074
9075
9076static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
9077{
9078 switch (attr) {
9079 case DRV_BR_PORT_ATTR_PROXYARP:
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07009080 return "proxyarp_wifi";
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009081 case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
9082 return "hairpin_mode";
9083 }
9084
9085 return NULL;
9086}
9087
9088
9089static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
9090 unsigned int val)
9091{
9092 struct i802_bss *bss = priv;
9093 char path[128];
9094 const char *attr_txt;
9095
9096 attr_txt = drv_br_port_attr_str(attr);
9097 if (attr_txt == NULL)
9098 return -EINVAL;
9099
9100 os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
9101 bss->ifname, attr_txt);
9102
9103 if (linux_write_system_file(path, val))
9104 return -1;
9105
9106 return 0;
9107}
9108
9109
9110static const char * drv_br_net_param_str(enum drv_br_net_param param)
9111{
9112 switch (param) {
9113 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9114 return "arp_accept";
Dmitry Shmidt83474442015-04-15 13:47:09 -07009115 default:
9116 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009117 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009118}
9119
9120
9121static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
9122 unsigned int val)
9123{
9124 struct i802_bss *bss = priv;
9125 char path[128];
9126 const char *param_txt;
9127 int ip_version = 4;
9128
Dmitry Shmidt83474442015-04-15 13:47:09 -07009129 if (param == DRV_BR_MULTICAST_SNOOPING) {
9130 os_snprintf(path, sizeof(path),
9131 "/sys/devices/virtual/net/%s/bridge/multicast_snooping",
9132 bss->brname);
9133 goto set_val;
9134 }
9135
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009136 param_txt = drv_br_net_param_str(param);
9137 if (param_txt == NULL)
9138 return -EINVAL;
9139
9140 switch (param) {
9141 case DRV_BR_NET_PARAM_GARP_ACCEPT:
9142 ip_version = 4;
9143 break;
9144 default:
9145 return -EINVAL;
9146 }
9147
9148 os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
9149 ip_version, bss->brname, param_txt);
9150
Dmitry Shmidt83474442015-04-15 13:47:09 -07009151set_val:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009152 if (linux_write_system_file(path, val))
9153 return -1;
9154
9155 return 0;
9156}
9157
9158
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009159#ifdef CONFIG_DRIVER_NL80211_QCA
9160
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009161static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
9162{
9163 switch (hw_mode) {
9164 case HOSTAPD_MODE_IEEE80211B:
9165 return QCA_ACS_MODE_IEEE80211B;
9166 case HOSTAPD_MODE_IEEE80211G:
9167 return QCA_ACS_MODE_IEEE80211G;
9168 case HOSTAPD_MODE_IEEE80211A:
9169 return QCA_ACS_MODE_IEEE80211A;
9170 case HOSTAPD_MODE_IEEE80211AD:
9171 return QCA_ACS_MODE_IEEE80211AD;
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07009172 case HOSTAPD_MODE_IEEE80211ANY:
9173 return QCA_ACS_MODE_IEEE80211ANY;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009174 default:
9175 return -1;
9176 }
9177}
9178
9179
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009180static int add_acs_freq_list(struct nl_msg *msg, const int *freq_list)
9181{
9182 int i, len, ret;
9183 u32 *freqs;
9184
9185 if (!freq_list)
9186 return 0;
9187 len = int_array_len(freq_list);
9188 freqs = os_malloc(sizeof(u32) * len);
9189 if (!freqs)
9190 return -1;
9191 for (i = 0; i < len; i++)
9192 freqs[i] = freq_list[i];
9193 ret = nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_FREQ_LIST,
9194 sizeof(u32) * len, freqs);
9195 os_free(freqs);
9196 return ret;
9197}
9198
9199
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009200static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
9201{
9202 struct i802_bss *bss = priv;
9203 struct wpa_driver_nl80211_data *drv = bss->drv;
9204 struct nl_msg *msg;
9205 struct nlattr *data;
9206 int ret;
9207 int mode;
9208
9209 mode = hw_mode_to_qca_acs(params->hw_mode);
9210 if (mode < 0)
9211 return -1;
9212
9213 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9214 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9215 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9216 QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
9217 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9218 nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
9219 (params->ht_enabled &&
9220 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
9221 (params->ht40_enabled &&
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07009222 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED)) ||
9223 (params->vht_enabled &&
9224 nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_VHT_ENABLED)) ||
9225 nla_put_u16(msg, QCA_WLAN_VENDOR_ATTR_ACS_CHWIDTH,
9226 params->ch_width) ||
9227 (params->ch_list_len &&
9228 nla_put(msg, QCA_WLAN_VENDOR_ATTR_ACS_CH_LIST, params->ch_list_len,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009229 params->ch_list)) ||
9230 add_acs_freq_list(msg, params->freq_list)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009231 nlmsg_free(msg);
9232 return -ENOBUFS;
9233 }
9234 nla_nest_end(msg, data);
9235
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07009236 wpa_printf(MSG_DEBUG,
9237 "nl80211: ACS Params: HW_MODE: %d HT: %d HT40: %d VHT: %d BW: %d CH_LIST_LEN: %u",
9238 params->hw_mode, params->ht_enabled, params->ht40_enabled,
9239 params->vht_enabled, params->ch_width, params->ch_list_len);
9240
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009241 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9242 if (ret) {
9243 wpa_printf(MSG_DEBUG,
9244 "nl80211: Failed to invoke driver ACS function: %s",
9245 strerror(errno));
9246 }
9247 return ret;
9248}
9249
9250
Ravi Joshie6ccb162015-07-16 17:45:41 -07009251static int nl80211_set_band(void *priv, enum set_band band)
9252{
9253 struct i802_bss *bss = priv;
9254 struct wpa_driver_nl80211_data *drv = bss->drv;
9255 struct nl_msg *msg;
9256 struct nlattr *data;
9257 int ret;
9258 enum qca_set_band qca_band;
9259
9260 if (!drv->setband_vendor_cmd_avail)
9261 return -1;
9262
9263 switch (band) {
9264 case WPA_SETBAND_AUTO:
9265 qca_band = QCA_SETBAND_AUTO;
9266 break;
9267 case WPA_SETBAND_5G:
9268 qca_band = QCA_SETBAND_5G;
9269 break;
9270 case WPA_SETBAND_2G:
9271 qca_band = QCA_SETBAND_2G;
9272 break;
9273 default:
9274 return -1;
9275 }
9276
9277 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9278 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9279 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9280 QCA_NL80211_VENDOR_SUBCMD_SETBAND) ||
9281 !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9282 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_SETBAND_VALUE, qca_band)) {
9283 nlmsg_free(msg);
9284 return -ENOBUFS;
9285 }
9286 nla_nest_end(msg, data);
9287
9288 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9289 if (ret) {
9290 wpa_printf(MSG_DEBUG,
9291 "nl80211: Driver setband function failed: %s",
9292 strerror(errno));
9293 }
9294 return ret;
9295}
9296
9297
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009298struct nl80211_pcl {
9299 unsigned int num;
9300 unsigned int *freq_list;
9301};
9302
9303static int preferred_freq_info_handler(struct nl_msg *msg, void *arg)
9304{
9305 struct nlattr *tb[NL80211_ATTR_MAX + 1];
9306 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
9307 struct nl80211_pcl *param = arg;
9308 struct nlattr *nl_vend, *attr;
9309 enum qca_iface_type iface_type;
9310 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_MAX + 1];
9311 unsigned int num, max_num;
9312 u32 *freqs;
9313
9314 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
9315 genlmsg_attrlen(gnlh, 0), NULL);
9316
9317 nl_vend = tb[NL80211_ATTR_VENDOR_DATA];
9318 if (!nl_vend)
9319 return NL_SKIP;
9320
9321 nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_MAX,
9322 nla_data(nl_vend), nla_len(nl_vend), NULL);
9323
9324 attr = tb_vendor[
9325 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE];
9326 if (!attr) {
9327 wpa_printf(MSG_ERROR, "nl80211: iface_type couldn't be found");
9328 param->num = 0;
9329 return NL_SKIP;
9330 }
9331
9332 iface_type = (enum qca_iface_type) nla_get_u32(attr);
9333 wpa_printf(MSG_DEBUG, "nl80211: Driver returned iface_type=%d",
9334 iface_type);
9335
9336 attr = tb_vendor[QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST];
9337 if (!attr) {
9338 wpa_printf(MSG_ERROR,
9339 "nl80211: preferred_freq_list couldn't be found");
9340 param->num = 0;
9341 return NL_SKIP;
9342 }
9343
9344 /*
9345 * param->num has the maximum number of entries for which there
9346 * is room in the freq_list provided by the caller.
9347 */
9348 freqs = nla_data(attr);
9349 max_num = nla_len(attr) / sizeof(u32);
9350 if (max_num > param->num)
9351 max_num = param->num;
9352 for (num = 0; num < max_num; num++)
9353 param->freq_list[num] = freqs[num];
9354 param->num = num;
9355
9356 return NL_SKIP;
9357}
9358
9359
9360static int nl80211_get_pref_freq_list(void *priv,
9361 enum wpa_driver_if_type if_type,
9362 unsigned int *num,
9363 unsigned int *freq_list)
9364{
9365 struct i802_bss *bss = priv;
9366 struct wpa_driver_nl80211_data *drv = bss->drv;
9367 struct nl_msg *msg;
9368 int ret;
9369 unsigned int i;
9370 struct nlattr *params;
9371 struct nl80211_pcl param;
9372 enum qca_iface_type iface_type;
9373
9374 if (!drv->get_pref_freq_list)
9375 return -1;
9376
9377 switch (if_type) {
9378 case WPA_IF_STATION:
9379 iface_type = QCA_IFACE_TYPE_STA;
9380 break;
9381 case WPA_IF_AP_BSS:
9382 iface_type = QCA_IFACE_TYPE_AP;
9383 break;
9384 case WPA_IF_P2P_GO:
9385 iface_type = QCA_IFACE_TYPE_P2P_GO;
9386 break;
9387 case WPA_IF_P2P_CLIENT:
9388 iface_type = QCA_IFACE_TYPE_P2P_CLIENT;
9389 break;
9390 case WPA_IF_IBSS:
9391 iface_type = QCA_IFACE_TYPE_IBSS;
9392 break;
9393 case WPA_IF_TDLS:
9394 iface_type = QCA_IFACE_TYPE_TDLS;
9395 break;
9396 default:
9397 return -1;
9398 }
9399
9400 param.num = *num;
9401 param.freq_list = freq_list;
9402
9403 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9404 nla_put_u32(msg, NL80211_ATTR_IFINDEX, drv->ifindex) ||
9405 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9406 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9407 QCA_NL80211_VENDOR_SUBCMD_GET_PREFERRED_FREQ_LIST) ||
9408 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9409 nla_put_u32(msg,
9410 QCA_WLAN_VENDOR_ATTR_GET_PREFERRED_FREQ_LIST_IFACE_TYPE,
9411 iface_type)) {
9412 wpa_printf(MSG_ERROR,
9413 "%s: err in adding vendor_cmd and vendor_data",
9414 __func__);
9415 nlmsg_free(msg);
9416 return -1;
9417 }
9418 nla_nest_end(msg, params);
9419
9420 os_memset(freq_list, 0, *num * sizeof(freq_list[0]));
9421 ret = send_and_recv_msgs(drv, msg, preferred_freq_info_handler, &param);
9422 if (ret) {
9423 wpa_printf(MSG_ERROR,
9424 "%s: err in send_and_recv_msgs", __func__);
9425 return ret;
9426 }
9427
9428 *num = param.num;
9429
9430 for (i = 0; i < *num; i++) {
9431 wpa_printf(MSG_DEBUG, "nl80211: preferred_channel_list[%d]=%d",
9432 i, freq_list[i]);
9433 }
9434
9435 return 0;
9436}
9437
9438
9439static int nl80211_set_prob_oper_freq(void *priv, unsigned int freq)
9440{
9441 struct i802_bss *bss = priv;
9442 struct wpa_driver_nl80211_data *drv = bss->drv;
9443 struct nl_msg *msg;
9444 int ret;
9445 struct nlattr *params;
9446
9447 if (!drv->set_prob_oper_freq)
9448 return -1;
9449
9450 wpa_printf(MSG_DEBUG,
9451 "nl80211: Set P2P probable operating freq %u for ifindex %d",
9452 freq, bss->ifindex);
9453
9454 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9455 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9456 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9457 QCA_NL80211_VENDOR_SUBCMD_SET_PROBABLE_OPER_CHANNEL) ||
9458 !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
9459 nla_put_u32(msg,
9460 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_IFACE_TYPE,
9461 QCA_IFACE_TYPE_P2P_CLIENT) ||
9462 nla_put_u32(msg,
9463 QCA_WLAN_VENDOR_ATTR_PROBABLE_OPER_CHANNEL_FREQ,
9464 freq)) {
9465 wpa_printf(MSG_ERROR,
9466 "%s: err in adding vendor_cmd and vendor_data",
9467 __func__);
9468 nlmsg_free(msg);
9469 return -1;
9470 }
9471 nla_nest_end(msg, params);
9472
9473 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9474 msg = NULL;
9475 if (ret) {
9476 wpa_printf(MSG_ERROR, "%s: err in send_and_recv_msgs",
9477 __func__);
9478 return ret;
9479 }
9480 nlmsg_free(msg);
9481 return 0;
9482}
9483
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009484
9485static int nl80211_p2p_lo_start(void *priv, unsigned int freq,
9486 unsigned int period, unsigned int interval,
9487 unsigned int count, const u8 *device_types,
9488 size_t dev_types_len,
9489 const u8 *ies, size_t ies_len)
9490{
9491 struct i802_bss *bss = priv;
9492 struct wpa_driver_nl80211_data *drv = bss->drv;
9493 struct nl_msg *msg;
9494 struct nlattr *container;
9495 int ret;
9496
9497 wpa_printf(MSG_DEBUG,
9498 "nl80211: Start P2P Listen offload: freq=%u, period=%u, interval=%u, count=%u",
9499 freq, period, interval, count);
9500
9501 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
9502 return -1;
9503
9504 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9505 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9506 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9507 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_START))
9508 goto fail;
9509
9510 container = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
9511 if (!container)
9512 goto fail;
9513
9514 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_CHANNEL,
9515 freq) ||
9516 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_PERIOD,
9517 period) ||
9518 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_INTERVAL,
9519 interval) ||
9520 nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_COUNT,
9521 count) ||
9522 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_DEVICE_TYPES,
9523 dev_types_len, device_types) ||
9524 nla_put(msg, QCA_WLAN_VENDOR_ATTR_P2P_LISTEN_OFFLOAD_VENDOR_IE,
9525 ies_len, ies))
9526 goto fail;
9527
9528 nla_nest_end(msg, container);
9529 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9530 msg = NULL;
9531 if (ret) {
9532 wpa_printf(MSG_DEBUG,
9533 "nl80211: Failed to send P2P Listen offload vendor command");
9534 goto fail;
9535 }
9536
9537 return 0;
9538
9539fail:
9540 nlmsg_free(msg);
9541 return -1;
9542}
9543
9544
9545static int nl80211_p2p_lo_stop(void *priv)
9546{
9547 struct i802_bss *bss = priv;
9548 struct wpa_driver_nl80211_data *drv = bss->drv;
9549 struct nl_msg *msg;
9550
9551 wpa_printf(MSG_DEBUG, "nl80211: Stop P2P Listen offload");
9552
9553 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD))
9554 return -1;
9555
9556 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9557 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9558 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9559 QCA_NL80211_VENDOR_SUBCMD_P2P_LISTEN_OFFLOAD_STOP)) {
9560 nlmsg_free(msg);
9561 return -1;
9562 }
9563
9564 return send_and_recv_msgs(drv, msg, NULL, NULL);
9565}
9566
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08009567
9568static int nl80211_set_tdls_mode(void *priv, int tdls_external_control)
9569{
9570 struct i802_bss *bss = priv;
9571 struct wpa_driver_nl80211_data *drv = bss->drv;
9572 struct nl_msg *msg;
9573 struct nlattr *params;
9574 int ret;
9575 u32 tdls_mode;
9576
9577 wpa_printf(MSG_DEBUG,
9578 "nl80211: Set TDKS mode: tdls_external_control=%d",
9579 tdls_external_control);
9580
9581 if (tdls_external_control == 1)
9582 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_IMPLICIT |
9583 QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXTERNAL;
9584 else
9585 tdls_mode = QCA_WLAN_VENDOR_TDLS_TRIGGER_MODE_EXPLICIT;
9586
9587 if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
9588 nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
9589 nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
9590 QCA_NL80211_VENDOR_SUBCMD_CONFIGURE_TDLS))
9591 goto fail;
9592
9593 params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
9594 if (!params)
9595 goto fail;
9596
9597 if (nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TDLS_CONFIG_TRIGGER_MODE,
9598 tdls_mode))
9599 goto fail;
9600
9601 nla_nest_end(msg, params);
9602
9603 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
9604 msg = NULL;
9605 if (ret) {
9606 wpa_printf(MSG_ERROR,
9607 "nl80211: Set TDLS mode failed: ret=%d (%s)",
9608 ret, strerror(-ret));
9609 goto fail;
9610 }
9611 return 0;
9612fail:
9613 nlmsg_free(msg);
9614 return -1;
9615}
9616
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009617#endif /* CONFIG_DRIVER_NL80211_QCA */
9618
9619
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009620static int nl80211_write_to_file(const char *name, unsigned int val)
9621{
9622 int fd, len;
9623 char tmp[128];
9624
9625 fd = open(name, O_RDWR);
9626 if (fd < 0) {
9627 wpa_printf(MSG_ERROR, "nl80211: Failed to open %s: %s",
9628 name, strerror(errno));
9629 return fd;
9630 }
9631
9632 len = os_snprintf(tmp, sizeof(tmp), "%u\n", val);
9633 len = write(fd, tmp, len);
9634 if (len < 0)
9635 wpa_printf(MSG_ERROR, "nl80211: Failed to write to %s: %s",
9636 name, strerror(errno));
9637 close(fd);
9638
9639 return 0;
9640}
9641
9642
9643static int nl80211_configure_data_frame_filters(void *priv, u32 filter_flags)
9644{
9645 struct i802_bss *bss = priv;
9646 char path[128];
9647 int ret;
9648
9649 wpa_printf(MSG_DEBUG, "nl80211: Data frame filter flags=0x%x",
9650 filter_flags);
9651
9652 /* Configure filtering of unicast frame encrypted using GTK */
9653 ret = os_snprintf(path, sizeof(path),
9654 "/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
9655 bss->ifname);
9656 if (os_snprintf_error(sizeof(path), ret))
9657 return -1;
9658
9659 ret = nl80211_write_to_file(path,
9660 !!(filter_flags &
9661 WPA_DATA_FRAME_FILTER_FLAG_GTK));
9662 if (ret) {
9663 wpa_printf(MSG_ERROR,
9664 "nl80211: Failed to set IPv4 unicast in multicast filter");
9665 return ret;
9666 }
9667
9668 os_snprintf(path, sizeof(path),
9669 "/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
9670 bss->ifname);
9671 ret = nl80211_write_to_file(path,
9672 !!(filter_flags &
9673 WPA_DATA_FRAME_FILTER_FLAG_GTK));
9674
9675 if (ret) {
9676 wpa_printf(MSG_ERROR,
9677 "nl80211: Failed to set IPv6 unicast in multicast filter");
9678 return ret;
9679 }
9680
9681 /* Configure filtering of unicast frame encrypted using GTK */
9682 os_snprintf(path, sizeof(path),
9683 "/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
9684 bss->ifname);
9685 ret = nl80211_write_to_file(path,
9686 !!(filter_flags &
9687 WPA_DATA_FRAME_FILTER_FLAG_ARP));
9688 if (ret) {
9689 wpa_printf(MSG_ERROR,
9690 "nl80211: Failed set gratuitous ARP filter");
9691 return ret;
9692 }
9693
9694 /* Configure filtering of IPv6 NA frames */
9695 os_snprintf(path, sizeof(path),
9696 "/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
9697 bss->ifname);
9698 ret = nl80211_write_to_file(path,
9699 !!(filter_flags &
9700 WPA_DATA_FRAME_FILTER_FLAG_NA));
9701 if (ret) {
9702 wpa_printf(MSG_ERROR,
9703 "nl80211: Failed to set unsolicited NA filter");
9704 return ret;
9705 }
9706
9707 return 0;
9708}
9709
9710
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07009711static int nl80211_get_ext_capab(void *priv, enum wpa_driver_if_type type,
9712 const u8 **ext_capa, const u8 **ext_capa_mask,
9713 unsigned int *ext_capa_len)
9714{
9715 struct i802_bss *bss = priv;
9716 struct wpa_driver_nl80211_data *drv = bss->drv;
9717 enum nl80211_iftype nlmode;
9718 unsigned int i;
9719
9720 if (!ext_capa || !ext_capa_mask || !ext_capa_len)
9721 return -1;
9722
9723 nlmode = wpa_driver_nl80211_if_type(type);
9724
9725 /* By default, use the per-radio values */
9726 *ext_capa = drv->extended_capa;
9727 *ext_capa_mask = drv->extended_capa_mask;
9728 *ext_capa_len = drv->extended_capa_len;
9729
9730 /* Replace the default value if a per-interface type value exists */
9731 for (i = 0; i < drv->num_iface_ext_capa; i++) {
9732 if (nlmode == drv->iface_ext_capa[i].iftype) {
9733 *ext_capa = drv->iface_ext_capa[i].ext_capa;
9734 *ext_capa_mask = drv->iface_ext_capa[i].ext_capa_mask;
9735 *ext_capa_len = drv->iface_ext_capa[i].ext_capa_len;
9736 break;
9737 }
9738 }
9739
9740 return 0;
9741}
9742
9743
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009744const struct wpa_driver_ops wpa_driver_nl80211_ops = {
9745 .name = "nl80211",
9746 .desc = "Linux nl80211/cfg80211",
9747 .get_bssid = wpa_driver_nl80211_get_bssid,
9748 .get_ssid = wpa_driver_nl80211_get_ssid,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009749 .set_key = driver_nl80211_set_key,
9750 .scan2 = driver_nl80211_scan2,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009751 .sched_scan = wpa_driver_nl80211_sched_scan,
9752 .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009753 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009754 .abort_scan = wpa_driver_nl80211_abort_scan,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009755 .deauthenticate = driver_nl80211_deauthenticate,
9756 .authenticate = driver_nl80211_authenticate,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009757 .associate = wpa_driver_nl80211_associate,
9758 .global_init = nl80211_global_init,
9759 .global_deinit = nl80211_global_deinit,
9760 .init2 = wpa_driver_nl80211_init,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009761 .deinit = driver_nl80211_deinit,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009762 .get_capa = wpa_driver_nl80211_get_capa,
9763 .set_operstate = wpa_driver_nl80211_set_operstate,
9764 .set_supp_port = wpa_driver_nl80211_set_supp_port,
9765 .set_country = wpa_driver_nl80211_set_country,
Dmitry Shmidtcce06662013-11-04 18:44:24 -08009766 .get_country = wpa_driver_nl80211_get_country,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009767 .set_ap = wpa_driver_nl80211_set_ap,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07009768 .set_acl = wpa_driver_nl80211_set_acl,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009769 .if_add = wpa_driver_nl80211_if_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009770 .if_remove = driver_nl80211_if_remove,
9771 .send_mlme = driver_nl80211_send_mlme,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009772 .get_hw_feature_data = nl80211_get_hw_feature_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009773 .sta_add = wpa_driver_nl80211_sta_add,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009774 .sta_remove = driver_nl80211_sta_remove,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009775 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
9776 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009777 .hapd_init = i802_init,
9778 .hapd_deinit = i802_deinit,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009779 .set_wds_sta = i802_set_wds_sta,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009780 .get_seqnum = i802_get_seqnum,
9781 .flush = i802_flush,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009782 .get_inact_sec = i802_get_inact_sec,
9783 .sta_clear_stats = i802_sta_clear_stats,
9784 .set_rts = i802_set_rts,
9785 .set_frag = i802_set_frag,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009786 .set_tx_queue_params = i802_set_tx_queue_params,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009787 .set_sta_vlan = driver_nl80211_set_sta_vlan,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009788 .sta_deauth = i802_sta_deauth,
9789 .sta_disassoc = i802_sta_disassoc,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009790 .read_sta_data = driver_nl80211_read_sta_data,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009791 .set_freq = i802_set_freq,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009792 .send_action = driver_nl80211_send_action,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009793 .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
9794 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
9795 .cancel_remain_on_channel =
9796 wpa_driver_nl80211_cancel_remain_on_channel,
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08009797 .probe_req_report = driver_nl80211_probe_req_report,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009798 .deinit_ap = wpa_driver_nl80211_deinit_ap,
Dmitry Shmidt04949592012-07-19 12:16:46 -07009799 .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009800 .resume = wpa_driver_nl80211_resume,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009801 .signal_monitor = nl80211_signal_monitor,
9802 .signal_poll = nl80211_signal_poll,
9803 .send_frame = nl80211_send_frame,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009804 .set_param = nl80211_set_param,
9805 .get_radio_name = nl80211_get_radio_name,
Jouni Malinen75ecf522011-06-27 15:19:46 -07009806 .add_pmkid = nl80211_add_pmkid,
9807 .remove_pmkid = nl80211_remove_pmkid,
9808 .flush_pmkid = nl80211_flush_pmkid,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009809 .set_rekey_info = nl80211_set_rekey_info,
9810 .poll_client = nl80211_poll_client,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009811 .set_p2p_powersave = nl80211_set_p2p_powersave,
Dmitry Shmidtea69e842013-05-13 14:52:28 -07009812 .start_dfs_cac = nl80211_start_radar_detection,
9813 .stop_ap = wpa_driver_nl80211_stop_ap,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009814#ifdef CONFIG_TDLS
9815 .send_tdls_mgmt = nl80211_send_tdls_mgmt,
9816 .tdls_oper = nl80211_tdls_oper,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009817 .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
9818 .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009819#endif /* CONFIG_TDLS */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07009820 .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
Dmitry Shmidt34af3062013-07-11 10:46:32 -07009821 .get_mac_addr = wpa_driver_nl80211_get_macaddr,
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07009822 .get_survey = wpa_driver_nl80211_get_survey,
Dmitry Shmidt56052862013-10-04 10:23:25 -07009823 .status = wpa_driver_nl80211_status,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08009824 .switch_channel = nl80211_switch_channel,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009825#ifdef ANDROID_P2P
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009826 .set_noa = wpa_driver_set_p2p_noa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08009827 .get_noa = wpa_driver_get_p2p_noa,
Dmitry Shmidt6e933c12011-09-27 12:29:26 -07009828 .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08009829#endif /* ANDROID_P2P */
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07009830#ifdef ANDROID
Dmitry Shmidt41712582015-06-29 11:02:15 -07009831#ifndef ANDROID_LIB_STUB
Dmitry Shmidt738a26e2011-07-07 14:22:14 -07009832 .driver_cmd = wpa_driver_nl80211_driver_cmd,
Dmitry Shmidt41712582015-06-29 11:02:15 -07009833#endif /* !ANDROID_LIB_STUB */
Dmitry Shmidt292b0c32013-11-22 12:54:42 -08009834#endif /* ANDROID */
Dmitry Shmidta38abf92014-03-06 13:38:44 -08009835 .vendor_cmd = nl80211_vendor_cmd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08009836 .set_qos_map = nl80211_set_qos_map,
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07009837 .set_wowlan = nl80211_set_wowlan,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07009838 .set_mac_addr = nl80211_set_mac_addr,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009839#ifdef CONFIG_MESH
9840 .init_mesh = wpa_driver_nl80211_init_mesh,
9841 .join_mesh = wpa_driver_nl80211_join_mesh,
9842 .leave_mesh = wpa_driver_nl80211_leave_mesh,
9843#endif /* CONFIG_MESH */
9844 .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
9845 .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
9846 .br_port_set_attr = wpa_driver_br_port_set_attr,
9847 .br_set_net_param = wpa_driver_br_set_net_param,
9848 .add_tx_ts = nl80211_add_ts,
9849 .del_tx_ts = nl80211_del_ts,
Dmitry Shmidte4663042016-04-04 10:07:49 -07009850 .get_ifindex = nl80211_get_ifindex,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009851#ifdef CONFIG_DRIVER_NL80211_QCA
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009852 .roaming = nl80211_roaming,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08009853 .do_acs = wpa_driver_do_acs,
Ravi Joshie6ccb162015-07-16 17:45:41 -07009854 .set_band = nl80211_set_band,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009855 .get_pref_freq_list = nl80211_get_pref_freq_list,
9856 .set_prob_oper_freq = nl80211_set_prob_oper_freq,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009857 .p2p_lo_start = nl80211_p2p_lo_start,
9858 .p2p_lo_stop = nl80211_p2p_lo_stop,
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07009859 .set_default_scan_ies = nl80211_set_default_scan_ies,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08009860 .set_tdls_mode = nl80211_set_tdls_mode,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009861#endif /* CONFIG_DRIVER_NL80211_QCA */
Dmitry Shmidt849734c2016-05-27 09:59:01 -07009862 .configure_data_frame_filters = nl80211_configure_data_frame_filters,
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07009863 .get_ext_capab = nl80211_get_ext_capab,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07009864};